]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2017 Merlok | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // EMV core functions | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "emvcore.h" | |
12 | #include "emvjson.h" | |
13 | #include "util_posix.h" | |
14 | #include "protocols.h" | |
15 | #ifdef WITH_SMARTCARD | |
16 | #include "cmdsmartcard.h" | |
17 | #endif | |
18 | ||
19 | // Got from here. Thanks) | |
20 | // https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix | |
21 | static const char *PSElist [] = { | |
22 | "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE | |
23 | "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE | |
24 | }; | |
25 | //static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*); | |
26 | ||
27 | char *TransactionTypeStr[] = { | |
28 | "MSD", | |
29 | "VSDC", | |
30 | "qVCDCMCHIP", | |
31 | "CDA" | |
32 | }; | |
33 | ||
34 | typedef struct { | |
35 | enum CardPSVendor vendor; | |
36 | const char* aid; | |
37 | } TAIDList; | |
38 | ||
39 | static const TAIDList AIDlist [] = { | |
40 | // Visa International | |
41 | { CV_VISA, "A00000000305076010"}, // VISA ELO Credit | |
42 | { CV_VISA, "A0000000031010" }, // VISA Debit/Credit (Classic) | |
43 | { CV_VISA, "A000000003101001" }, // VISA Credit | |
44 | { CV_VISA, "A000000003101002" }, // VISA Debit | |
45 | { CV_VISA, "A0000000032010" }, // VISA Electron | |
46 | { CV_VISA, "A0000000032020" }, // VISA | |
47 | { CV_VISA, "A0000000033010" }, // VISA Interlink | |
48 | { CV_VISA, "A0000000034010" }, // VISA Specific | |
49 | { CV_VISA, "A0000000035010" }, // VISA Specific | |
50 | { CV_VISA, "A0000000036010" }, // Domestic Visa Cash Stored Value | |
51 | { CV_VISA, "A0000000036020" }, // International Visa Cash Stored Value | |
52 | { CV_VISA, "A0000000038002" }, // VISA Auth, VisaRemAuthen EMV-CAP (DPA) | |
53 | { CV_VISA, "A0000000038010" }, // VISA Plus | |
54 | { CV_VISA, "A0000000039010" }, // VISA Loyalty | |
55 | { CV_VISA, "A000000003999910" }, // VISA Proprietary ATM | |
56 | // Visa USA | |
57 | { CV_VISA, "A000000098" }, // Debit Card | |
58 | { CV_VISA, "A0000000980848" }, // Debit Card | |
59 | // Mastercard International | |
60 | { CV_MASTERCARD, "A00000000401" }, // MasterCard PayPass | |
61 | { CV_MASTERCARD, "A0000000041010" }, // MasterCard Credit | |
62 | { CV_MASTERCARD, "A00000000410101213" }, // MasterCard Credit | |
63 | { CV_MASTERCARD, "A00000000410101215" }, // MasterCard Credit | |
64 | { CV_MASTERCARD, "A0000000042010" }, // MasterCard Specific | |
65 | { CV_MASTERCARD, "A0000000043010" }, // MasterCard Specific | |
66 | { CV_MASTERCARD, "A0000000043060" }, // Maestro (Debit) | |
67 | { CV_MASTERCARD, "A000000004306001" }, // Maestro (Debit) | |
68 | { CV_MASTERCARD, "A0000000044010" }, // MasterCard Specific | |
69 | { CV_MASTERCARD, "A0000000045010" }, // MasterCard Specific | |
70 | { CV_MASTERCARD, "A0000000046000" }, // Cirrus | |
71 | { CV_MASTERCARD, "A0000000048002" }, // SecureCode Auth EMV-CAP | |
72 | { CV_MASTERCARD, "A0000000049999" }, // MasterCard PayPass | |
73 | // American Express | |
74 | { CV_AMERICANEXPRESS, "A000000025" }, | |
75 | { CV_AMERICANEXPRESS, "A0000000250000" }, | |
76 | { CV_AMERICANEXPRESS, "A00000002501" }, | |
77 | { CV_AMERICANEXPRESS, "A000000025010402" }, | |
78 | { CV_AMERICANEXPRESS, "A000000025010701" }, | |
79 | { CV_AMERICANEXPRESS, "A000000025010801" }, | |
80 | // Groupement des Cartes Bancaires "CB" | |
81 | { CV_CB, "A0000000421010" }, // Cartes Bancaire EMV Card | |
82 | { CV_CB, "A0000000422010" }, | |
83 | { CV_CB, "A0000000423010" }, | |
84 | { CV_CB, "A0000000424010" }, | |
85 | { CV_CB, "A0000000425010" }, | |
86 | // JCB CO., LTD. | |
87 | { CV_JCB, "A00000006510" }, // JCB | |
88 | { CV_JCB, "A0000000651010" }, // JCB J Smart Credit | |
89 | // Other | |
90 | { CV_OTHER, "A0000001544442" }, // Banricompras Debito - Banrisul - Banco do Estado do Rio Grande do SUL - S.A. | |
91 | { CV_OTHER, "F0000000030001" }, // BRADESCO | |
92 | { CV_OTHER, "A0000005241010" }, // RuPay - RuPay | |
93 | { CV_OTHER, "D5780000021010" } // Bankaxept - Bankaxept | |
94 | }; | |
95 | static const size_t AIDlistLen = sizeof(AIDlist)/sizeof(TAIDList); | |
96 | ||
97 | static bool APDULogging = false; | |
98 | void SetAPDULogging(bool logging) { | |
99 | APDULogging = logging; | |
100 | } | |
101 | ||
102 | enum CardPSVendor GetCardPSVendor(uint8_t * AID, size_t AIDlen) { | |
103 | char buf[100] = {0}; | |
104 | if (AIDlen < 1) | |
105 | return CV_NA; | |
106 | ||
107 | hex_to_buffer((uint8_t *)buf, AID, AIDlen, sizeof(buf) - 1, 0, 0, true); | |
108 | ||
109 | for(int i = 0; i < AIDlistLen; i ++) { | |
110 | if (strncmp(AIDlist[i].aid, buf, strlen(AIDlist[i].aid)) == 0){ | |
111 | return AIDlist[i].vendor; | |
112 | } | |
113 | } | |
114 | ||
115 | return CV_NA; | |
116 | } | |
117 | ||
118 | static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) { | |
119 | emv_tag_dump(tlv, stdout, level); | |
120 | if (is_leaf) { | |
121 | dump_buffer(tlv->value, tlv->len, stdout, level); | |
122 | } | |
123 | ||
124 | return true; | |
125 | } | |
126 | ||
127 | bool TLVPrintFromBuffer(uint8_t *data, int datalen) { | |
128 | struct tlvdb *t = NULL; | |
129 | t = tlvdb_parse_multi(data, datalen); | |
130 | if (t) { | |
131 | PrintAndLogEx(NORMAL, "-------------------- TLV decoded --------------------"); | |
132 | ||
133 | tlvdb_visit(t, print_cb, NULL, 0); | |
134 | tlvdb_free(t); | |
135 | return true; | |
136 | } else { | |
137 | PrintAndLogEx(WARNING, "TLV ERROR: Can't parse response as TLV tree."); | |
138 | } | |
139 | return false; | |
140 | } | |
141 | ||
142 | void TLVPrintFromTLVLev(struct tlvdb *tlv, int level) { | |
143 | if (!tlv) | |
144 | return; | |
145 | ||
146 | tlvdb_visit(tlv, print_cb, NULL, level); | |
147 | } | |
148 | ||
149 | void TLVPrintFromTLV(struct tlvdb *tlv) { | |
150 | TLVPrintFromTLVLev(tlv, 0); | |
151 | } | |
152 | ||
153 | void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) { | |
154 | PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|"); | |
155 | PrintAndLogEx(NORMAL, "| AID |Priority| Name |"); | |
156 | PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|"); | |
157 | ||
158 | struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f); | |
159 | if (!ttmp) | |
160 | PrintAndLogEx(NORMAL, "| none |"); | |
161 | ||
162 | while (ttmp) { | |
163 | const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL); | |
164 | const struct tlv *tgName = tlvdb_get_inchild(ttmp, 0x50, NULL); | |
165 | const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL); | |
166 | if (!tgAID) | |
167 | break; | |
168 | PrintAndLogEx(NORMAL, "|%s| %s |%s|", | |
169 | sprint_hex_inrow_ex(tgAID->value, tgAID->len, 18), | |
170 | (tgPrio) ? sprint_hex(tgPrio->value, 1) : " ", | |
171 | (tgName) ? sprint_ascii_ex(tgName->value, tgName->len, 25) : " "); | |
172 | ||
173 | ttmp = tlvdb_find_next(ttmp, 0x6f); | |
174 | } | |
175 | ||
176 | PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|"); | |
177 | } | |
178 | ||
179 | struct tlvdb *GetPANFromTrack2(const struct tlv *track2) { | |
180 | char track2Hex[200] = {0}; | |
181 | uint8_t PAN[100] = {0}; | |
182 | int PANlen = 0; | |
183 | char *tmp = track2Hex; | |
184 | ||
185 | if (!track2) | |
186 | return NULL; | |
187 | ||
188 | for (int i = 0; i < track2->len; ++i, tmp += 2) | |
189 | sprintf(tmp, "%02x", (unsigned int)track2->value[i]); | |
190 | ||
191 | int posD = strchr(track2Hex, 'd') - track2Hex; | |
192 | if (posD < 1) | |
193 | return NULL; | |
194 | ||
195 | track2Hex[posD] = 0; | |
196 | if (strlen(track2Hex) % 2) { | |
197 | track2Hex[posD] = 'F'; | |
198 | track2Hex[posD + 1] = '\0'; | |
199 | } | |
200 | ||
201 | param_gethex_to_eol(track2Hex, 0, PAN, sizeof(PAN), &PANlen); | |
202 | ||
203 | return tlvdb_fixed(0x5a, PANlen, PAN); | |
204 | } | |
205 | ||
206 | struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2) { | |
207 | char track2Hex[200] = {0}; | |
208 | char dCVVHex[100] = {0}; | |
209 | uint8_t dCVV[100] = {0}; | |
210 | int dCVVlen = 0; | |
211 | const int PINlen = 5; // must calculated from 9F67 MSD Offset but i have not seen this tag) | |
212 | char *tmp = track2Hex; | |
213 | ||
214 | if (!track2) | |
215 | return NULL; | |
216 | ||
217 | for (int i = 0; i < track2->len; ++i, tmp += 2) | |
218 | sprintf(tmp, "%02x", (unsigned int)track2->value[i]); | |
219 | ||
220 | int posD = strchr(track2Hex, 'd') - track2Hex; | |
221 | if (posD < 1) | |
222 | return NULL; | |
223 | ||
224 | memset(dCVVHex, '0', 32); | |
225 | // ATC | |
226 | memcpy(dCVVHex + 0, track2Hex + posD + PINlen + 11, 4); | |
227 | // PAN 5 hex | |
228 | memcpy(dCVVHex + 4, track2Hex, 5); | |
229 | // expire date | |
230 | memcpy(dCVVHex + 9, track2Hex + posD + 1, 4); | |
231 | // service code | |
232 | memcpy(dCVVHex + 13, track2Hex + posD + 5, 3); | |
233 | ||
234 | param_gethex_to_eol(dCVVHex, 0, dCVV, sizeof(dCVV), &dCVVlen); | |
235 | ||
236 | return tlvdb_fixed(0x02, dCVVlen, dCVV); | |
237 | } | |
238 | ||
239 | ||
240 | static int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *apdu, int apdu_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
241 | { | |
242 | *ResultLen = 0; | |
243 | if (sw) *sw = 0; | |
244 | uint16_t isw = 0; | |
245 | int res = 0; | |
246 | ||
247 | if (ActivateField && channel == ECC_CONTACTLESS) { | |
248 | DropField(); | |
249 | msleep(50); | |
250 | } | |
251 | ||
252 | if (APDULogging) | |
253 | PrintAndLogEx(SUCCESS, ">>>> %s", sprint_hex(apdu, apdu_len)); | |
254 | ||
255 | switch(channel) { | |
256 | case ECC_CONTACTLESS: | |
257 | // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe) | |
258 | res = ExchangeAPDU14a(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen); | |
259 | if (res) { | |
260 | return res; | |
261 | } | |
262 | break; | |
263 | case ECC_CONTACT: | |
264 | //int ExchangeAPDUSC(uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); | |
265 | #ifdef WITH_SMARTCARD | |
266 | res = ExchangeAPDUSC(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen); | |
267 | if (res) { | |
268 | return res; | |
269 | } | |
270 | #endif | |
271 | break; | |
272 | } | |
273 | ||
274 | if (APDULogging) | |
275 | PrintAndLogEx(SUCCESS, "<<<< %s", sprint_hex(Result, *ResultLen)); | |
276 | ||
277 | if (*ResultLen < 2) { | |
278 | return 200; | |
279 | } | |
280 | ||
281 | if (Result[*ResultLen-2] == 0x61) { | |
282 | uint8_t La = Result[*ResultLen-1]; | |
283 | uint8_t get_response[5] = {apdu[0], ISO7816_GET_RESPONSE, 0x00, 0x00, La}; | |
284 | return EMVExchangeEx(channel, false, LeaveFieldON, get_response, sizeof(get_response), Result, MaxResultLen, ResultLen, sw, tlv); | |
285 | } | |
286 | ||
287 | *ResultLen -= 2; | |
288 | isw = Result[*ResultLen] * 0x0100 + Result[*ResultLen + 1]; | |
289 | if (sw) | |
290 | *sw = isw; | |
291 | ||
292 | if (isw != 0x9000) { | |
293 | if (APDULogging) { | |
294 | PrintAndLogEx(ERR, "APDU(%02x%02x) ERROR: [%4X] %s", apdu[0], apdu[1], isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff)); | |
295 | return 5; | |
296 | } | |
297 | } | |
298 | ||
299 | // add to tlv tree | |
300 | if (tlv) { | |
301 | struct tlvdb *t = tlvdb_parse_multi(Result, *ResultLen); | |
302 | tlvdb_add(tlv, t); | |
303 | } | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
308 | int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *apdu, int apdu_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
309 | { | |
310 | uint8_t APDU[APDU_COMMAND_LEN]; | |
311 | memcpy(APDU, apdu, apdu_len); | |
312 | APDU[apdu_len] = 0x00; | |
313 | if (channel == ECC_CONTACTLESS) { | |
314 | if (apdu_len == 5 && apdu[4] == 0) { | |
315 | // there is no Lc but an Le == 0 already | |
316 | } else if (apdu_len > 5 && apdu_len == 5 + apdu[4] + 1) { | |
317 | // there is Lc, data and Le | |
318 | } else { | |
319 | apdu_len++; // no Le, add Le = 0x00 because some vendors require it for contactless | |
320 | } | |
321 | } | |
322 | return EMVExchangeEx(channel, false, LeaveFieldON, APDU, apdu_len, Result, MaxResultLen, ResultLen, sw, tlv); | |
323 | } | |
324 | ||
325 | int EMVSelect(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
326 | { | |
327 | uint8_t Select_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_SELECT_FILE, 0x04, 0x00, AIDLen, 0x00}; | |
328 | memcpy(Select_APDU + 5, AID, AIDLen); | |
329 | int apdulen = 5 + AIDLen; | |
330 | if (channel == ECC_CONTACTLESS) { | |
331 | apdulen++; // some vendors require Le = 0x00 for contactless operations | |
332 | } | |
333 | return EMVExchangeEx(channel, ActivateField, LeaveFieldON, Select_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv); | |
334 | } | |
335 | ||
336 | int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) { | |
337 | uint8_t buf[APDU_DATA_LEN] = {0}; | |
338 | *ResultLen = 0; | |
339 | int len = 0; | |
340 | int res = 0; | |
341 | switch (PSENum) { | |
342 | case 1: | |
343 | param_gethex_to_eol(PSElist[1], 0, buf, sizeof(buf), &len); | |
344 | break; | |
345 | case 2: | |
346 | ||
347 | param_gethex_to_eol(PSElist[0], 0, buf, sizeof(buf), &len); | |
348 | break; | |
349 | default: | |
350 | return -1; | |
351 | } | |
352 | ||
353 | // select | |
354 | res = EMVSelect(channel, ActivateField, LeaveFieldON, buf, len, Result, MaxResultLen, ResultLen, sw, NULL); | |
355 | ||
356 | return res; | |
357 | } | |
358 | ||
359 | int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) { | |
360 | uint8_t data[APDU_RESPONSE_LEN] = {0}; | |
361 | size_t datalen = 0; | |
362 | uint16_t sw = 0; | |
363 | int res; | |
364 | ||
365 | char *PSE_or_PPSE = PSENum == 1 ? "PSE" : "PPSE"; | |
366 | ||
367 | // select PPSE | |
368 | res = EMVSelectPSE(channel, ActivateField, true, PSENum, data, sizeof(data), &datalen, &sw); | |
369 | ||
370 | if (!res){ | |
371 | struct tlvdb *t = NULL; | |
372 | t = tlvdb_parse_multi(data, datalen); | |
373 | if (t) { | |
374 | int retrycnt = 0; | |
375 | struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00}); | |
376 | if (!ttmp) | |
377 | PrintAndLogEx(FAILED, "%s doesn't have any records.", PSE_or_PPSE); | |
378 | ||
379 | while (ttmp) { | |
380 | const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x4f, NULL); | |
381 | if (tgAID) { | |
382 | res = EMVSelect(channel, false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv); | |
383 | ||
384 | // retry if error and not returned sw error | |
385 | if (res && res != 5) { | |
386 | if (++retrycnt < 3){ | |
387 | continue; | |
388 | } else { | |
389 | // card select error, proxmark error | |
390 | if (res == 1) { | |
391 | PrintAndLogEx(WARNING, "Exit..."); | |
392 | return 1; | |
393 | } | |
394 | ||
395 | retrycnt = 0; | |
396 | PrintAndLogEx(NORMAL, "Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID->value, tgAID->len)); | |
397 | } | |
398 | ||
399 | // next element | |
400 | ttmp = tlvdb_find_next(ttmp, 0x61); | |
401 | continue; | |
402 | } | |
403 | retrycnt = 0; | |
404 | ||
405 | // all is ok | |
406 | if (decodeTLV){ | |
407 | PrintAndLogEx(NORMAL, "%s:", sprint_hex_inrow(tgAID->value, tgAID->len)); | |
408 | TLVPrintFromBuffer(data, datalen); | |
409 | } | |
410 | } | |
411 | ||
412 | ttmp = tlvdb_find_next(ttmp, 0x61); | |
413 | } | |
414 | ||
415 | tlvdb_free(t); | |
416 | } else { | |
417 | PrintAndLogEx(WARNING, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE); | |
418 | } | |
419 | } else { | |
420 | PrintAndLogEx(WARNING, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE, res); | |
421 | } | |
422 | ||
423 | if(!LeaveFieldON && channel == ECC_CONTACTLESS) | |
424 | DropField(); | |
425 | ||
426 | return res; | |
427 | } | |
428 | ||
429 | int EMVSearch(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) { | |
430 | uint8_t aidbuf[APDU_DATA_LEN] = {0}; | |
431 | int aidlen = 0; | |
432 | uint8_t data[APDU_RESPONSE_LEN] = {0}; | |
433 | size_t datalen = 0; | |
434 | uint16_t sw = 0; | |
435 | ||
436 | int res = 0; | |
437 | int retrycnt = 0; | |
438 | for(int i = 0; i < AIDlistLen; i ++) { | |
439 | param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen); | |
440 | res = EMVSelect(channel, (i == 0) ? ActivateField : false, (i == AIDlistLen - 1) ? LeaveFieldON : true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv); | |
441 | // retry if error and not returned sw error | |
442 | if (res && res != 5) { | |
443 | if (++retrycnt < 3){ | |
444 | i--; | |
445 | } else { | |
446 | // (1) - card select error, proxmark error OR (200) - result length = 0 | |
447 | if (res == 1 || res == 200) { | |
448 | PrintAndLogEx(WARNING, "Exit..."); | |
449 | return 1; | |
450 | } | |
451 | ||
452 | retrycnt = 0; | |
453 | PrintAndLogEx(FAILED, "Retry failed [%s]. Skipped...", AIDlist[i].aid); | |
454 | } | |
455 | continue; | |
456 | } | |
457 | retrycnt = 0; | |
458 | ||
459 | if (res) | |
460 | continue; | |
461 | ||
462 | if (!datalen) | |
463 | continue; | |
464 | ||
465 | if (decodeTLV) { | |
466 | PrintAndLogEx(SUCCESS, "%s", AIDlist[i].aid); | |
467 | TLVPrintFromBuffer(data, datalen); | |
468 | } | |
469 | } | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | int EMVSelectApplication(struct tlvdb *tlv, uint8_t *AID, size_t *AIDlen) { | |
475 | // check priority. 0x00 - highest | |
476 | int prio = 0xffff; | |
477 | ||
478 | *AIDlen = 0; | |
479 | ||
480 | struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f); | |
481 | if (!ttmp) | |
482 | return 1; | |
483 | ||
484 | while (ttmp) { | |
485 | const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL); | |
486 | const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL); | |
487 | ||
488 | if (!tgAID) | |
489 | break; | |
490 | ||
491 | if (tgPrio) { | |
492 | int pt = bytes_to_num((uint8_t*)tgPrio->value, (tgPrio->len < 2) ? tgPrio->len : 2); | |
493 | if (pt < prio) { | |
494 | prio = pt; | |
495 | ||
496 | memcpy(AID, tgAID->value, tgAID->len); | |
497 | *AIDlen = tgAID->len; | |
498 | } | |
499 | } else { | |
500 | // takes the first application from list wo priority | |
501 | if (!*AIDlen) { | |
502 | memcpy(AID, tgAID->value, tgAID->len); | |
503 | *AIDlen = tgAID->len; | |
504 | } | |
505 | } | |
506 | ||
507 | ttmp = tlvdb_find_next(ttmp, 0x6f); | |
508 | } | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | int EMVGPO(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *PDOL, size_t PDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
514 | { | |
515 | uint8_t GPO_APDU[APDU_COMMAND_LEN] = {0x80, 0xa8, 0x00, 0x00, PDOLLen, 0x00}; | |
516 | memcpy(GPO_APDU + 5, PDOL, PDOLLen); | |
517 | int apdulen = 5 + PDOLLen; | |
518 | ||
519 | return EMVExchange(channel, LeaveFieldON, GPO_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv); | |
520 | } | |
521 | ||
522 | int EMVReadRecord(EMVCommandChannel channel, bool LeaveFieldON, uint8_t SFI, uint8_t SFIrec, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
523 | { | |
524 | uint8_t read_APDU[5] = {0x00, ISO7816_READ_RECORDS, SFIrec, (SFI << 3) | 0x04, 0x00}; | |
525 | int res = EMVExchange(channel, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv); | |
526 | if (*sw == 0x6700) { | |
527 | PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le..."); | |
528 | res = EMVExchangeEx(channel, false, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv); | |
529 | } | |
530 | return res; | |
531 | } | |
532 | ||
533 | int EMVAC(EMVCommandChannel channel, bool LeaveFieldON, uint8_t RefControl, uint8_t *CDOL, size_t CDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
534 | { | |
535 | uint8_t CDOL_APDU[APDU_COMMAND_LEN] = {0x80, 0xae, RefControl, 0x00, CDOLLen, 0x00}; | |
536 | memcpy(CDOL_APDU + 5, CDOL, CDOLLen); | |
537 | int apdulen = 5 + CDOLLen; | |
538 | ||
539 | return EMVExchange(channel, LeaveFieldON, CDOL_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv); | |
540 | } | |
541 | ||
542 | int EMVGenerateChallenge(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
543 | { | |
544 | uint8_t get_challenge_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_GET_CHALLENGE, 0x00, 0x00}; | |
545 | ||
546 | int res = EMVExchange(channel, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv); | |
547 | if (*sw == 0x6700) { | |
548 | PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le..."); | |
549 | res = EMVExchangeEx(channel, false, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv); | |
550 | } | |
551 | return res; | |
552 | } | |
553 | ||
554 | int EMVInternalAuthenticate(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *DDOL, size_t DDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
555 | { | |
556 | uint8_t authenticate_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_INTERNAL_AUTHENTICATION, 0x00, 0x00, DDOLLen, 0x00}; | |
557 | memcpy(authenticate_APDU + 5, DDOL, DDOLLen); | |
558 | int apdulen = 5 + DDOLLen; | |
559 | ||
560 | return EMVExchange(channel, LeaveFieldON, authenticate_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv); | |
561 | } | |
562 | ||
563 | int MSCComputeCryptoChecksum(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *UDOL, uint8_t UDOLlen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) | |
564 | { | |
565 | uint8_t checksum_APDU[APDU_COMMAND_LEN] = {0x80, 0x2a, 0x8e, 0x80, UDOLlen, 0x00}; | |
566 | memcpy(checksum_APDU + 5, UDOL, UDOLlen); | |
567 | int apdulen = 5 + UDOLlen; | |
568 | ||
569 | return EMVExchange(channel, LeaveFieldON, checksum_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv); | |
570 | } | |
571 | ||
572 | // Authentication | |
573 | struct emv_pk *get_ca_pk(struct tlvdb *db) { | |
574 | const struct tlv *df_tlv = tlvdb_get(db, 0x84, NULL); | |
575 | const struct tlv *caidx_tlv = tlvdb_get(db, 0x8f, NULL); | |
576 | ||
577 | if (!df_tlv || !caidx_tlv || df_tlv->len < 6 || caidx_tlv->len != 1) | |
578 | return NULL; | |
579 | ||
580 | PrintAndLogEx(NORMAL, "CA public key index 0x%0x", caidx_tlv->value[0]); | |
581 | return emv_pk_get_ca_pk(df_tlv->value, caidx_tlv->value[0]); | |
582 | } | |
583 | ||
584 | int trSDA(struct tlvdb *tlv) { | |
585 | ||
586 | struct emv_pk *pk = get_ca_pk(tlv); | |
587 | if (!pk) { | |
588 | PrintAndLogEx(WARNING, "Error: Key not found. Exit."); | |
589 | return 2; | |
590 | } | |
591 | ||
592 | struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv); | |
593 | if (!issuer_pk) { | |
594 | emv_pk_free(pk); | |
595 | PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit."); | |
596 | return 2; | |
597 | } | |
598 | ||
599 | PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx", | |
600 | issuer_pk->rid[0], | |
601 | issuer_pk->rid[1], | |
602 | issuer_pk->rid[2], | |
603 | issuer_pk->rid[3], | |
604 | issuer_pk->rid[4], | |
605 | issuer_pk->index, | |
606 | issuer_pk->serial[0], | |
607 | issuer_pk->serial[1], | |
608 | issuer_pk->serial[2] | |
609 | ); | |
610 | ||
611 | const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL); | |
612 | if (!sda_tlv || sda_tlv->len < 1) { | |
613 | emv_pk_free(issuer_pk); | |
614 | emv_pk_free(pk); | |
615 | PrintAndLogEx(WARNING, "Can't find input list for Offline Data Authentication. Exit."); | |
616 | return 3; | |
617 | } | |
618 | ||
619 | struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv); | |
620 | if (dac_db) { | |
621 | const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL); | |
622 | PrintAndLogEx(NORMAL, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]); | |
623 | tlvdb_add(tlv, dac_db); | |
624 | } else { | |
625 | emv_pk_free(issuer_pk); | |
626 | emv_pk_free(pk); | |
627 | PrintAndLogEx(WARNING, "SSAD verify error"); | |
628 | return 4; | |
629 | } | |
630 | ||
631 | emv_pk_free(issuer_pk); | |
632 | emv_pk_free(pk); | |
633 | return 0; | |
634 | } | |
635 | ||
636 | static const unsigned char default_ddol_value[] = {0x9f, 0x37, 0x04}; | |
637 | static struct tlv default_ddol_tlv = {.tag = 0x9f49, .len = 3, .value = default_ddol_value }; | |
638 | ||
639 | int trDDA(EMVCommandChannel channel, bool decodeTLV, struct tlvdb *tlv) { | |
640 | uint8_t buf[APDU_RESPONSE_LEN] = {0}; | |
641 | size_t len = 0; | |
642 | uint16_t sw = 0; | |
643 | ||
644 | struct emv_pk *pk = get_ca_pk(tlv); | |
645 | if (!pk) { | |
646 | PrintAndLogEx(WARNING, "Error: Key not found. Exit."); | |
647 | return 2; | |
648 | } | |
649 | ||
650 | const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL); | |
651 | if (!sda_tlv || sda_tlv->len < 1) { | |
652 | emv_pk_free(pk); | |
653 | PrintAndLogEx(WARNING, "Error: Can't find input list for Offline Data Authentication. Exit."); | |
654 | return 3; | |
655 | } | |
656 | ||
657 | struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv); | |
658 | if (!issuer_pk) { | |
659 | emv_pk_free(pk); | |
660 | PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit."); | |
661 | return 2; | |
662 | } | |
663 | PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
664 | issuer_pk->rid[0], | |
665 | issuer_pk->rid[1], | |
666 | issuer_pk->rid[2], | |
667 | issuer_pk->rid[3], | |
668 | issuer_pk->rid[4], | |
669 | issuer_pk->index, | |
670 | issuer_pk->serial[0], | |
671 | issuer_pk->serial[1], | |
672 | issuer_pk->serial[2] | |
673 | ); | |
674 | ||
675 | struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv); | |
676 | if (!icc_pk) { | |
677 | emv_pk_free(pk); | |
678 | emv_pk_free(issuer_pk); | |
679 | PrintAndLogEx(WARNING, "Error: ICC setrificate not found. Exit."); | |
680 | return 2; | |
681 | } | |
682 | PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
683 | icc_pk->rid[0], | |
684 | icc_pk->rid[1], | |
685 | icc_pk->rid[2], | |
686 | icc_pk->rid[3], | |
687 | icc_pk->rid[4], | |
688 | icc_pk->index, | |
689 | icc_pk->serial[0], | |
690 | icc_pk->serial[1], | |
691 | icc_pk->serial[2] | |
692 | ); | |
693 | ||
694 | struct emv_pk *icc_pe_pk = emv_pki_recover_icc_pe_cert(issuer_pk, tlv); | |
695 | if (!icc_pe_pk) { | |
696 | PrintAndLogEx(WARNING, "WARNING: ICC PE PK recover error. "); | |
697 | } else { | |
698 | PrintAndLogEx(SUCCESS, "ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
699 | icc_pe_pk->rid[0], | |
700 | icc_pe_pk->rid[1], | |
701 | icc_pe_pk->rid[2], | |
702 | icc_pe_pk->rid[3], | |
703 | icc_pe_pk->rid[4], | |
704 | icc_pe_pk->index, | |
705 | icc_pe_pk->serial[0], | |
706 | icc_pe_pk->serial[1], | |
707 | icc_pe_pk->serial[2] | |
708 | ); | |
709 | } | |
710 | ||
711 | // 9F4B: Signed Dynamic Application Data | |
712 | const struct tlv *sdad_tlv = tlvdb_get(tlv, 0x9f4b, NULL); | |
713 | // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result) | |
714 | // EMV kernel3 v2.4, contactless book C-3, C.1., page 147 | |
715 | if (sdad_tlv) { | |
716 | PrintAndLogEx(NORMAL, "\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA..."); | |
717 | ||
718 | const struct tlvdb *atc_db = emv_pki_recover_atc_ex(icc_pk, tlv, true); | |
719 | if (!atc_db) { | |
720 | PrintAndLogEx(WARNING, "Error: Can't recover IDN (ICC Dynamic Number)"); | |
721 | emv_pk_free(pk); | |
722 | emv_pk_free(issuer_pk); | |
723 | emv_pk_free(icc_pk); | |
724 | return 8; | |
725 | } | |
726 | ||
727 | // 9f36 Application Transaction Counter (ATC) | |
728 | const struct tlv *atc_tlv = tlvdb_get(atc_db, 0x9f36, NULL); | |
729 | if(atc_tlv) { | |
730 | PrintAndLogEx(NORMAL, "\nATC (Application Transaction Counter) [%zu] %s", atc_tlv->len, sprint_hex_inrow(atc_tlv->value, atc_tlv->len)); | |
731 | ||
732 | const struct tlv *core_atc_tlv = tlvdb_get(tlv, 0x9f36, NULL); | |
733 | if(tlv_equal(core_atc_tlv, atc_tlv)) { | |
734 | PrintAndLogEx(SUCCESS, "ATC check OK."); | |
735 | PrintAndLogEx(SUCCESS, "fDDA (fast DDA) verified OK."); | |
736 | } else { | |
737 | PrintAndLogEx(WARNING, "Error: fDDA verified, but ATC in the certificate and ATC in the record not the same."); | |
738 | } | |
739 | } else { | |
740 | PrintAndLogEx(NORMAL, "\nERROR: fDDA (fast DDA) verify error"); | |
741 | emv_pk_free(pk); | |
742 | emv_pk_free(issuer_pk); | |
743 | emv_pk_free(icc_pk); | |
744 | return 9; | |
745 | } | |
746 | } else { | |
747 | struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv); | |
748 | if (dac_db) { | |
749 | const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL); | |
750 | PrintAndLogEx(NORMAL, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]); | |
751 | tlvdb_add(tlv, dac_db); | |
752 | } else { | |
753 | PrintAndLogEx(WARNING, "Error: SSAD verify error"); | |
754 | emv_pk_free(pk); | |
755 | emv_pk_free(issuer_pk); | |
756 | emv_pk_free(icc_pk); | |
757 | return 4; | |
758 | } | |
759 | ||
760 | PrintAndLogEx(NORMAL, "\n* Calc DDOL"); | |
761 | const struct tlv *ddol_tlv = tlvdb_get(tlv, 0x9f49, NULL); | |
762 | if (!ddol_tlv) { | |
763 | ddol_tlv = &default_ddol_tlv; | |
764 | PrintAndLogEx(NORMAL, "DDOL [9f49] not found. Using default DDOL"); | |
765 | } | |
766 | ||
767 | struct tlv *ddol_data_tlv = dol_process(ddol_tlv, tlv, 0); | |
768 | if (!ddol_data_tlv) { | |
769 | PrintAndLogEx(WARNING, "Error: Can't create DDOL TLV"); | |
770 | emv_pk_free(pk); | |
771 | emv_pk_free(issuer_pk); | |
772 | emv_pk_free(icc_pk); | |
773 | return 5; | |
774 | } | |
775 | ||
776 | PrintAndLogEx(NORMAL, "DDOL data[%d]: %s", ddol_data_tlv->len, sprint_hex(ddol_data_tlv->value, ddol_data_tlv->len)); | |
777 | ||
778 | PrintAndLogEx(NORMAL, "\n* Internal Authenticate"); | |
779 | int res = EMVInternalAuthenticate(channel, true, (uint8_t *)ddol_data_tlv->value, ddol_data_tlv->len, buf, sizeof(buf), &len, &sw, NULL); | |
780 | if (res) { | |
781 | PrintAndLogEx(WARNING, "Internal Authenticate error(%d): %4x. Exit...", res, sw); | |
782 | free(ddol_data_tlv); | |
783 | emv_pk_free(pk); | |
784 | emv_pk_free(issuer_pk); | |
785 | emv_pk_free(icc_pk); | |
786 | return 6; | |
787 | } | |
788 | ||
789 | struct tlvdb *dda_db = NULL; | |
790 | if (buf[0] == 0x80) { | |
791 | if (len < 3 ) { | |
792 | PrintAndLogEx(WARNING, "Error: Internal Authenticate format1 parsing error. length=%d", len); | |
793 | } else { | |
794 | // 9f4b Signed Dynamic Application Data | |
795 | dda_db = tlvdb_fixed(0x9f4b, len - 2, buf + 2); | |
796 | tlvdb_add(tlv, dda_db); | |
797 | if (decodeTLV){ | |
798 | PrintAndLogEx(NORMAL, "* * Decode response format 1:"); | |
799 | TLVPrintFromTLV(dda_db); | |
800 | } | |
801 | } | |
802 | } else { | |
803 | dda_db = tlvdb_parse_multi(buf, len); | |
804 | if(!dda_db) { | |
805 | PrintAndLogEx(WARNING, "Error: Can't parse Internal Authenticate result as TLV"); | |
806 | free(ddol_data_tlv); | |
807 | emv_pk_free(pk); | |
808 | emv_pk_free(issuer_pk); | |
809 | emv_pk_free(icc_pk); | |
810 | return 7; | |
811 | } | |
812 | tlvdb_add(tlv, dda_db); | |
813 | ||
814 | if (decodeTLV) | |
815 | TLVPrintFromTLV(dda_db); | |
816 | } | |
817 | ||
818 | struct tlvdb *idn_db = emv_pki_recover_idn_ex(icc_pk, dda_db, ddol_data_tlv, true); | |
819 | free(ddol_data_tlv); | |
820 | if (!idn_db) { | |
821 | PrintAndLogEx(WARNING, "Error: Can't recover IDN (ICC Dynamic Number)"); | |
822 | tlvdb_free(dda_db); | |
823 | emv_pk_free(pk); | |
824 | emv_pk_free(issuer_pk); | |
825 | emv_pk_free(icc_pk); | |
826 | return 8; | |
827 | } | |
828 | tlvdb_free(dda_db); | |
829 | ||
830 | // 9f4c ICC Dynamic Number | |
831 | const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL); | |
832 | if(idn_tlv) { | |
833 | PrintAndLogEx(NORMAL, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len)); | |
834 | PrintAndLogEx(NORMAL, "DDA verified OK."); | |
835 | tlvdb_add(tlv, idn_db); | |
836 | tlvdb_free(idn_db); | |
837 | } else { | |
838 | PrintAndLogEx(NORMAL, "\nERROR: DDA verify error"); | |
839 | tlvdb_free(idn_db); | |
840 | ||
841 | emv_pk_free(pk); | |
842 | emv_pk_free(issuer_pk); | |
843 | emv_pk_free(icc_pk); | |
844 | return 9; | |
845 | } | |
846 | } | |
847 | ||
848 | emv_pk_free(pk); | |
849 | emv_pk_free(issuer_pk); | |
850 | emv_pk_free(icc_pk); | |
851 | return 0; | |
852 | } | |
853 | ||
854 | int trCDA(struct tlvdb *tlv, struct tlvdb *ac_tlv, struct tlv *pdol_data_tlv, struct tlv *ac_data_tlv) { | |
855 | ||
856 | struct emv_pk *pk = get_ca_pk(tlv); | |
857 | if (!pk) { | |
858 | PrintAndLogEx(WARNING, "Error: Key not found. Exit."); | |
859 | return 2; | |
860 | } | |
861 | ||
862 | const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL); | |
863 | if (!sda_tlv || sda_tlv->len < 1) { | |
864 | PrintAndLogEx(WARNING, "Error: Can't find input list for Offline Data Authentication. Exit."); | |
865 | emv_pk_free(pk); | |
866 | return 3; | |
867 | } | |
868 | ||
869 | struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv); | |
870 | if (!issuer_pk) { | |
871 | PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit."); | |
872 | emv_pk_free(pk); | |
873 | return 2; | |
874 | } | |
875 | PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
876 | issuer_pk->rid[0], | |
877 | issuer_pk->rid[1], | |
878 | issuer_pk->rid[2], | |
879 | issuer_pk->rid[3], | |
880 | issuer_pk->rid[4], | |
881 | issuer_pk->index, | |
882 | issuer_pk->serial[0], | |
883 | issuer_pk->serial[1], | |
884 | issuer_pk->serial[2] | |
885 | ); | |
886 | ||
887 | struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv); | |
888 | if (!icc_pk) { | |
889 | PrintAndLogEx(WARNING, "Error: ICC setrificate not found. Exit."); | |
890 | emv_pk_free(pk); | |
891 | emv_pk_free(issuer_pk); | |
892 | return 2; | |
893 | } | |
894 | PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
895 | icc_pk->rid[0], | |
896 | icc_pk->rid[1], | |
897 | icc_pk->rid[2], | |
898 | icc_pk->rid[3], | |
899 | icc_pk->rid[4], | |
900 | icc_pk->index, | |
901 | icc_pk->serial[0], | |
902 | icc_pk->serial[1], | |
903 | icc_pk->serial[2] | |
904 | ); | |
905 | ||
906 | struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv); | |
907 | if (dac_db) { | |
908 | const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL); | |
909 | PrintAndLogEx(NORMAL, "SSAD verified OK. (%02hhx:%02hhx)", dac_tlv->value[0], dac_tlv->value[1]); | |
910 | tlvdb_add(tlv, dac_db); | |
911 | } else { | |
912 | PrintAndLogEx(WARNING, "Error: SSAD verify error"); | |
913 | emv_pk_free(pk); | |
914 | emv_pk_free(issuer_pk); | |
915 | emv_pk_free(icc_pk); | |
916 | return 4; | |
917 | } | |
918 | ||
919 | PrintAndLogEx(NORMAL, "\n* * Check Signed Dynamic Application Data (SDAD)"); | |
920 | struct tlvdb *idn_db = emv_pki_perform_cda_ex(icc_pk, tlv, ac_tlv, | |
921 | pdol_data_tlv, // pdol | |
922 | ac_data_tlv, // cdol1 | |
923 | NULL, // cdol2 | |
924 | true); | |
925 | if (idn_db) { | |
926 | const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL); | |
927 | PrintAndLogEx(NORMAL, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len)); | |
928 | PrintAndLogEx(NORMAL, "CDA verified OK."); | |
929 | tlvdb_add(tlv, idn_db); | |
930 | } else { | |
931 | PrintAndLogEx(NORMAL, "\nERROR: CDA verify error"); | |
932 | } | |
933 | ||
934 | emv_pk_free(pk); | |
935 | emv_pk_free(issuer_pk); | |
936 | emv_pk_free(icc_pk); | |
937 | return 0; | |
938 | } | |
939 | ||
940 | int RecoveryCertificates(struct tlvdb *tlvRoot, json_t *root) { | |
941 | ||
942 | struct emv_pk *pk = get_ca_pk(tlvRoot); | |
943 | if (!pk) { | |
944 | PrintAndLog("ERROR: Key not found. Exit."); | |
945 | return 1; | |
946 | } | |
947 | ||
948 | struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlvRoot); | |
949 | if (!issuer_pk) { | |
950 | emv_pk_free(pk); | |
951 | PrintAndLog("WARNING: Issuer certificate not found. Exit."); | |
952 | return 2; | |
953 | } | |
954 | PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx", | |
955 | issuer_pk->rid[0], | |
956 | issuer_pk->rid[1], | |
957 | issuer_pk->rid[2], | |
958 | issuer_pk->rid[3], | |
959 | issuer_pk->rid[4], | |
960 | issuer_pk->index, | |
961 | issuer_pk->serial[0], | |
962 | issuer_pk->serial[1], | |
963 | issuer_pk->serial[2] | |
964 | ); | |
965 | ||
966 | JsonSaveBufAsHex(root, "$.ApplicationData.RID", issuer_pk->rid, 5); | |
967 | ||
968 | char *issuer_pk_c = emv_pk_dump_pk(issuer_pk); | |
969 | JsonSaveStr(root, "$.ApplicationData.IssuerPublicKeyDec", issuer_pk_c); | |
970 | JsonSaveBufAsHex(root, "$.ApplicationData.IssuerPublicKeyModulus", issuer_pk->modulus, issuer_pk->mlen); | |
971 | free(issuer_pk_c); | |
972 | ||
973 | struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlvRoot, NULL); | |
974 | if (!icc_pk) { | |
975 | emv_pk_free(pk); | |
976 | emv_pk_free(issuer_pk); | |
977 | PrintAndLogEx(WARNING, "WARNING: ICC certificate not found. Exit."); | |
978 | return 2; | |
979 | } | |
980 | PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n", | |
981 | icc_pk->rid[0], | |
982 | icc_pk->rid[1], | |
983 | icc_pk->rid[2], | |
984 | icc_pk->rid[3], | |
985 | icc_pk->rid[4], | |
986 | icc_pk->index, | |
987 | icc_pk->serial[0], | |
988 | icc_pk->serial[1], | |
989 | icc_pk->serial[2] | |
990 | ); | |
991 | ||
992 | char *icc_pk_c = emv_pk_dump_pk(icc_pk); | |
993 | JsonSaveStr(root, "$.ApplicationData.ICCPublicKeyDec", icc_pk_c); | |
994 | JsonSaveBufAsHex(root, "$.ApplicationData.ICCPublicKeyModulus", icc_pk->modulus, icc_pk->mlen); | |
995 | free(issuer_pk_c); | |
996 | ||
997 | return 0; | |
998 | } |