]> git.zerfleddert.de Git - proxmark3-svn/blame - client/emv/emv_tags.c
fix: avoid SspClk overflow due to incomplete reset
[proxmark3-svn] / client / emv / emv_tags.c
CommitLineData
a2bb2735 1/*
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2015 Dmitry Eremin-Solenikov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 */
15
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "tlv.h"
21#include "emv_tags.h"
22
23#include <stdlib.h>
24
43912d63 25#define PRINT_INDENT(level) {for (int i = 0; i < (level); i++) fprintf(f, "\t");}
26
a2bb2735 27enum emv_tag_t {
28 EMV_TAG_GENERIC,
29 EMV_TAG_BITMASK,
30 EMV_TAG_DOL,
31 EMV_TAG_CVM_LIST,
32 EMV_TAG_STRING,
33 EMV_TAG_NUMERIC,
34 EMV_TAG_YYMMDD,
a2bb2735 35};
36
37struct emv_tag {
38 tlv_tag_t tag;
39 char *name;
40 enum emv_tag_t type;
41 const void *data;
42};
43
44struct emv_tag_bit {
45 unsigned bit;
46 const char *name;
47};
48
49#define EMV_BIT(byte, bit) ((byte - 1) * 8 + (8 - bit))
50#define EMV_BIT_FINISH { (~0), NULL }
51
52static const struct emv_tag_bit EMV_AIP[] = {
53 { EMV_BIT(1, 7), "SDA supported" },
54 { EMV_BIT(1, 6), "DDA supported" },
55 { EMV_BIT(1, 5), "Cardholder verification is supported" },
56 { EMV_BIT(1, 4), "Terminal risk management is to be performed" },
57 { EMV_BIT(1, 3), "Issuer authentication is supported" },
58 { EMV_BIT(1, 2), "Reserved for use by the EMV Contactless Specifications" },
59 { EMV_BIT(1, 1), "CDA supported" },
60 { EMV_BIT(2, 8), "Reserved for use by the EMV Contactless Specifications" },
61 { EMV_BIT(2, 7), "Reserved for use by the EMV Contactless Specifications" },
62 { EMV_BIT(2, 6), "Reserved for use by the EMV Contactless Specifications" },
63 { EMV_BIT(2, 1), "Reserved for use by the EMV Contactless Specifications" },
64 EMV_BIT_FINISH,
65};
66
67static const struct emv_tag_bit EMV_AUC[] = {
68 { EMV_BIT(1, 8), "Valid for domestic cash transactions" },
69 { EMV_BIT(1, 7), "Valid for international cash transactions" },
70 { EMV_BIT(1, 6), "Valid for domestic goods" },
71 { EMV_BIT(1, 5), "Valid for international goods" },
72 { EMV_BIT(1, 4), "Valid for domestic services" },
73 { EMV_BIT(1, 3), "Valid for international services" },
74 { EMV_BIT(1, 2), "Valid for ATMs" },
75 { EMV_BIT(1, 1), "Valid at terminals other than ATMs" },
76 { EMV_BIT(2, 8), "Domestic cashback allowed" },
77 { EMV_BIT(2, 7), "International cashback allowed" },
78 EMV_BIT_FINISH,
79};
80
81static const struct emv_tag_bit EMV_TVR[] = {
82 { EMV_BIT(1, 8), "Offline data authentication was not performed" },
83 { EMV_BIT(1, 7), "SDA failed" },
84 { EMV_BIT(1, 6), "ICC data missing" },
85 { EMV_BIT(1, 5), "Card appears on terminal exception file" },
86 { EMV_BIT(1, 4), "DDA failed" },
87 { EMV_BIT(1, 3), "CDA failed" },
88 { EMV_BIT(1, 2), "SDA selected" },
89 { EMV_BIT(2, 8), "ICC and terminal have different application versions" },
90 { EMV_BIT(2, 7), "Expired application" },
91 { EMV_BIT(2, 6), "Application not yet effective" },
92 { EMV_BIT(2, 5), "Requested service not allowed for card product" },
93 { EMV_BIT(2, 4), "New card" },
94 { EMV_BIT(3, 8), "Cardholder verification was not successful" },
95 { EMV_BIT(3, 7), "Unrecognised CVM" },
96 { EMV_BIT(3, 6), "PIN Try Limit exceeded" },
97 { EMV_BIT(3, 5), "PIN entry required and PIN pad not present or not working" },
98 { EMV_BIT(3, 4), "PIN entry required, PIN pad present, but PIN was not entered" },
99 { EMV_BIT(3, 3), "Online PIN entered" },
100 { EMV_BIT(4, 8), "Transaction exceeds floor limit" },
101 { EMV_BIT(4, 7), "Lower consecutive offline limit exceeded" },
102 { EMV_BIT(4, 6), "Upper consecutive offline limit exceeded" },
103 { EMV_BIT(4, 5), "Transaction selected randomly for online processing" },
104 { EMV_BIT(4, 4), "Merchant forced transaction online" },
105 { EMV_BIT(5, 8), "Default TDOL used" },
106 { EMV_BIT(5, 7), "Issuer authentication failed" },
107 { EMV_BIT(5, 6), "Script processing failed before final GENERATE AC" },
108 { EMV_BIT(5, 5), "Script processing failed after final GENERATE AC" },
109 { EMV_BIT(5, 4), "Reserved for use by the EMV Contactless Specifications" },
110 { EMV_BIT(5, 3), "Reserved for use by the EMV Contactless Specifications" },
111 { EMV_BIT(5, 2), "Reserved for use by the EMV Contactless Specifications" },
112 { EMV_BIT(5, 1), "Reserved for use by the EMV Contactless Specifications" },
113 EMV_BIT_FINISH,
114};
115
116static const struct emv_tag emv_tags[] = {
117 { 0x00 , "Unknown ???" },
118 { 0x4f , "Application Dedicated File (ADF) Name" },
119 { 0x50 , "Application Label", EMV_TAG_STRING },
120 { 0x56 , "Track 1 Data" },
121 { 0x57 , "Track 2 Equivalent Data" },
122 { 0x5a , "Application Primary Account Number (PAN)" },
123 { 0x5f20, "Cardholder Name", EMV_TAG_STRING },
124 { 0x5f24, "Application Expiration Date", EMV_TAG_YYMMDD },
125 { 0x5f25, "Application Effective Date", EMV_TAG_YYMMDD },
126 { 0x5f28, "Issuer Country Code", EMV_TAG_NUMERIC },
127 { 0x5f2a, "Transaction Currency Code", EMV_TAG_NUMERIC },
128 { 0x5f2d, "Language Preference", EMV_TAG_STRING },
129 { 0x5f30, "Service Code", EMV_TAG_NUMERIC },
130 { 0x5f34, "Application Primary Account Number (PAN) Sequence Number", EMV_TAG_NUMERIC },
131 { 0x61 , "Application Template" },
23207d74 132 { 0x6f , "File Control Information (FCI) Template" },
a2bb2735 133 { 0x70 , "READ RECORD Response Message Template" },
134 { 0x77 , "Response Message Template Format 2" },
135 { 0x80 , "Response Message Template Format 1" },
136 { 0x82 , "Application Interchange Profile", EMV_TAG_BITMASK, &EMV_AIP },
137 { 0x83 , "Command Template" },
138 { 0x84 , "Dedicated File (DF) Name" },
139 { 0x87 , "Application Priority Indicator" },
140 { 0x88 , "Short File Identifier (SFI)" },
141 { 0x8a , "Authorisation Response Code" },
142 { 0x8c , "Card Risk Management Data Object List 1 (CDOL1)", EMV_TAG_DOL },
143 { 0x8d , "Card Risk Management Data Object List 2 (CDOL2)", EMV_TAG_DOL },
144 { 0x8e , "Cardholder Verification Method (CVM) List", EMV_TAG_CVM_LIST },
145 { 0x8f , "Certification Authority Public Key Index" },
146 { 0x90 , "Issuer Public Key Certificate" },
147 { 0x91 , "Issuer Authentication Data" },
148 { 0x92 , "Issuer Public Key Remainder" },
149 { 0x93 , "Signed Static Application Data" },
150 { 0x94 , "Application File Locator (AFL)" },
151 { 0x95 , "Terminal Verification Results" },
152 { 0x9a , "Transaction Date", EMV_TAG_YYMMDD },
153 { 0x9c , "Transaction Type" },
154 { 0x9f02, "Amount, Authorised (Numeric)", EMV_TAG_NUMERIC },
155 { 0x9f03, "Amount, Other (Numeric)", EMV_TAG_NUMERIC, },
156 { 0x9f07, "Application Usage Control", EMV_TAG_BITMASK, &EMV_AUC },
157 { 0x9f08, "Application Version Number" },
158 { 0x9f0d, "Issuer Action Code - Default", EMV_TAG_BITMASK, &EMV_TVR },
159 { 0x9f0e, "Issuer Action Code - Denial", EMV_TAG_BITMASK, &EMV_TVR },
160 { 0x9f0f, "Issuer Action Code - Online", EMV_TAG_BITMASK, &EMV_TVR },
161 { 0x9f10, "Issuer Application Data" },
162 { 0x9f11, "Issuer Code Table Index", EMV_TAG_NUMERIC },
163 { 0x9f12, "Application Preferred Name", EMV_TAG_STRING },
164 { 0x9f13, "Last Online Application Transaction Counter (ATC) Register" },
165 { 0x9f17, "Personal Identification Number (PIN) Try Counter" },
166 { 0x9f1a, "Terminal Country Code" },
167 { 0x9f1f, "Track 1 Discretionary Data", EMV_TAG_STRING },
168 { 0x9f21, "Transaction Time" },
169 { 0x9f26, "Application Cryptogram" },
170 { 0x9f27, "Cryptogram Information Data" },
171 { 0x9f2d, "ICC PIN Encipherment Public Key Certificate" },
172 { 0x9f2e, "ICC PIN Encipherment Public Key Exponent" },
173 { 0x9f2f, "ICC PIN Encipherment Public Key Remainder" },
174 { 0x9f32, "Issuer Public Key Exponent" },
175 { 0x9f34, "Cardholder Verification Method (CVM) Results" },
176 { 0x9f35, "Terminal Type" },
177 { 0x9f36, "Application Transaction Counter (ATC)" },
178 { 0x9f37, "Unpredictable Number" },
179 { 0x9f38, "Processing Options Data Object List (PDOL)", EMV_TAG_DOL },
180 { 0x9f42, "Application Currency Code", EMV_TAG_NUMERIC },
181 { 0x9f44, "Application Currency Exponent", EMV_TAG_NUMERIC },
182 { 0x9f45, "Data Authentication Code" },
183 { 0x9f46, "ICC Public Key Certificate" },
184 { 0x9f47, "ICC Public Key Exponent" },
185 { 0x9f48, "ICC Public Key Remainder" },
186 { 0x9f49, "Dynamic Data Authentication Data Object List (DDOL)", EMV_TAG_DOL },
187 { 0x9f4a, "Static Data Authentication Tag List" },
188 { 0x9f4b, "Signed Dynamic Application Data" },
189 { 0x9f4c, "ICC Dynamic Number" },
190 { 0x9f4d, "Log Entry" },
191 { 0x9f4f, "Log Format", EMV_TAG_DOL },
192 { 0x9f62, "PCVC3(Track1)" },
193 { 0x9f63, "PUNATC(Track1)" },
194 { 0x9f64, "NATC(Track1)" },
195 { 0x9f65, "PCVC3(Track2)" },
196 { 0x9f66, "PUNATC(Track2)" },
197 { 0x9f67, "NATC(Track2)" },
198 { 0x9f6b, "Track 2 Data" },
199 { 0xa5 , "File Control Information (FCI) Proprietary Template" },
200 { 0xbf0c, "File Control Information (FCI) Issuer Discretionary Data" },
201};
202
203static int emv_sort_tag(tlv_tag_t tag)
204{
205 return (int)(tag >= 0x100 ? tag : tag << 8);
206}
207
208static int emv_tlv_compare(const void *a, const void *b)
209{
210 const struct tlv *tlv = a;
211 const struct emv_tag *tag = b;
212
213 return emv_sort_tag(tlv->tag) - (emv_sort_tag(tag->tag));
214}
215
216static const struct emv_tag *emv_get_tag(const struct tlv *tlv)
217{
218 struct emv_tag *tag = bsearch(tlv, emv_tags, sizeof(emv_tags)/sizeof(emv_tags[0]),
219 sizeof(emv_tags[0]), emv_tlv_compare);
220
221 return tag ? tag : &emv_tags[0];
222}
223
224static const char *bitstrings[] = {
225 ".......1",
226 "......1.",
227 ".....1..",
228 "....1...",
229 "...1....",
230 "..1.....",
231 ".1......",
232 "1.......",
233};
234
43912d63 235static void emv_tag_dump_bitmask(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 236{
237 const struct emv_tag_bit *bits = tag->data;
238 unsigned bit, byte;
239
240 for (byte = 1; byte <= tlv->len; byte ++) {
241 unsigned char val = tlv->value[byte - 1];
43912d63 242 PRINT_INDENT(level);
a2bb2735 243 fprintf(f, "\tByte %u (%02x)\n", byte, val);
244 for (bit = 8; bit > 0; bit--, val <<= 1) {
245 if (val & 0x80)
43912d63 246 PRINT_INDENT(level);
a2bb2735 247 fprintf(f, "\t\t%s - '%s'\n", bitstrings[bit - 1],
248 bits->bit == EMV_BIT(byte, bit) ? bits->name : "Unknown");
249 if (bits->bit == EMV_BIT(byte, bit))
250 bits ++;
251 }
252 }
253}
254
43912d63 255static void emv_tag_dump_dol(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 256{
257 const unsigned char *buf = tlv->value;
258 size_t left = tlv->len;
259
260 while (left) {
261 struct tlv doltlv;
262 const struct emv_tag *doltag;
263
264 if (!tlv_parse_tl(&buf, &left, &doltlv)) {
43912d63 265 PRINT_INDENT(level);
a2bb2735 266 fprintf(f, "Invalid Tag-Len\n");
267 continue;
268 }
269
270 doltag = emv_get_tag(&doltlv);
271
43912d63 272 PRINT_INDENT(level);
a2bb2735 273 fprintf(f, "\tTag %4hx len %02zx ('%s')\n", doltlv.tag, doltlv.len, doltag->name);
274 }
275}
276
43912d63 277static void emv_tag_dump_string(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 278{
43912d63 279 PRINT_INDENT(level);
a2bb2735 280 fprintf(f, "\tString value '");
281 fwrite(tlv->value, 1, tlv->len, f);
43912d63 282 PRINT_INDENT(level);
a2bb2735 283 fprintf(f, "'\n");
284}
285
286static unsigned long emv_value_numeric(const struct tlv *tlv, unsigned start, unsigned end)
287{
288 unsigned long ret = 0;
289 int i;
290
291 if (end > tlv->len * 2)
292 return ret;
293 if (start >= end)
294 return ret;
295
296 if (start & 1) {
297 ret += tlv->value[start/2] & 0xf;
298 i = start + 1;
299 } else
300 i = start;
301
302 for (; i < end - 1; i += 2) {
303 ret *= 10;
304 ret += tlv->value[i/2] >> 4;
305 ret *= 10;
306 ret += tlv->value[i/2] & 0xf;
307 }
308
309 if (end & 1) {
310 ret *= 10;
311 ret += tlv->value[end/2] >> 4;
312 }
313
314 return ret;
315}
316
43912d63 317static void emv_tag_dump_numeric(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 318{
43912d63 319 PRINT_INDENT(level);
a2bb2735 320 fprintf(f, "\tNumeric value %lu\n", emv_value_numeric(tlv, 0, tlv->len * 2));
321}
322
43912d63 323static void emv_tag_dump_yymmdd(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 324{
43912d63 325 PRINT_INDENT(level);
a2bb2735 326 fprintf(f, "\tDate: 20%02ld.%ld.%ld\n",
327 emv_value_numeric(tlv, 0, 2),
328 emv_value_numeric(tlv, 2, 4),
329 emv_value_numeric(tlv, 4, 6));
330}
331
332static uint32_t emv_get_binary(const unsigned char *S)
333{
334 return (S[0] << 24) | (S[1] << 16) | (S[2] << 8) | (S[3] << 0);
335}
336
43912d63 337static void emv_tag_dump_cvm_list(const struct tlv *tlv, const struct emv_tag *tag, FILE *f, int level)
a2bb2735 338{
339 uint32_t X, Y;
340 int i;
341
342 if (tlv->len < 10 || tlv->len % 2) {
43912d63 343 PRINT_INDENT(level);
a2bb2735 344 fprintf(f, "\tINVALID!\n");
345 return;
346 }
347
348 X = emv_get_binary(tlv->value);
349 Y = emv_get_binary(tlv->value + 4);
350
43912d63 351 PRINT_INDENT(level);
a2bb2735 352 fprintf(f, "\tX: %d\n", X);
43912d63 353 PRINT_INDENT(level);
a2bb2735 354 fprintf(f, "\tY: %d\n", Y);
355
356 for (i = 8; i < tlv->len; i+= 2) {
357 const char *method;
358 const char *condition;
359
360 switch (tlv->value[i] & 0x3f) {
361 case 0x0:
362 method = "Fail CVM processing";
363 break;
364 case 0x1:
365 method = "Plaintext PIN verification performed by ICC";
366 break;
367 case 0x2:
368 method = "Enciphered PIN verified online";
369 break;
370 case 0x3:
371 method = "Plaintext PIN verification performed by ICC and signature (paper)";
372 break;
373 case 0x4:
374 method = "Enciphered PIN verification performed by ICC";
375 break;
376 case 0x5:
377 method = "Enciphered PIN verification performed by ICC and signature (paper)";
378 break;
379 case 0x1e:
380 method = "Signature (paper)";
381 break;
382 case 0x1f:
383 method = "No CVM required";
384 break;
385 case 0x3f:
386 method = "NOT AVAILABLE!";
387 break;
388 default:
389 method = "Unknown";
390 break;
391 }
392
393 switch (tlv->value[i+1]) {
394 case 0x00:
395 condition = "Always";
396 break;
397 case 0x01:
398 condition = "If unattended cash";
399 break;
400 case 0x02:
401 condition = "If not unattended cash and not manual cash and not purchase with cashback";
402 break;
403 case 0x03:
404 condition = "If terminal supports the CVM";
405 break;
406 case 0x04:
407 condition = "If manual cash";
408 break;
409 case 0x05:
410 condition = "If purchase with cashback";
411 break;
412 case 0x06:
413 condition = "If transaction is in the application currency and is under X value";
414 break;
415 case 0x07:
416 condition = "If transaction is in the application currency and is over X value";
417 break;
418 case 0x08:
419 condition = "If transaction is in the application currency and is under Y value";
420 break;
421 case 0x09:
422 condition = "If transaction is in the application currency and is over Y value";
423 break;
424 default:
425 condition = "Unknown";
426 break;
427 }
428
43912d63 429 PRINT_INDENT(level);
a2bb2735 430 fprintf(f, "\t%02x %02x: '%s' '%s' and '%s' if this CVM is unsuccessful\n",
431 tlv->value[i], tlv->value[i+1],
432 method, condition, (tlv->value[i] & 0x40) ? "continue" : "fail");
433 }
434}
435
43912d63 436bool emv_tag_dump(const struct tlv *tlv, FILE *f, int level)
a2bb2735 437{
438 if (!tlv) {
439 fprintf(f, "NULL\n");
440 return false;
441 }
442
443 const struct emv_tag *tag = emv_get_tag(tlv);
444
43912d63 445 PRINT_INDENT(level);
a2bb2735 446 fprintf(f, "--%2hx[%02zx] '%s':\n", tlv->tag, tlv->len, tag->name);
447
448 switch (tag->type) {
449 case EMV_TAG_GENERIC:
450 break;
451 case EMV_TAG_BITMASK:
43912d63 452 emv_tag_dump_bitmask(tlv, tag, f, level);
a2bb2735 453 break;
454 case EMV_TAG_DOL:
43912d63 455 emv_tag_dump_dol(tlv, tag, f, level);
a2bb2735 456 break;
457 case EMV_TAG_CVM_LIST:
43912d63 458 emv_tag_dump_cvm_list(tlv, tag, f, level);
a2bb2735 459 break;
460 case EMV_TAG_STRING:
43912d63 461 emv_tag_dump_string(tlv, tag, f, level);
a2bb2735 462 break;
463 case EMV_TAG_NUMERIC:
43912d63 464 emv_tag_dump_numeric(tlv, tag, f, level);
a2bb2735 465 break;
466 case EMV_TAG_YYMMDD:
43912d63 467 emv_tag_dump_yymmdd(tlv, tag, f, level);
a2bb2735 468 break;
a2bb2735 469 };
470
471 return true;
472}
Impressum, Datenschutz