]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/emv/emv_tags.c
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2015 Dmitry Eremin-Solenikov
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.
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.
25 #define PRINT_INDENT(level) {for (int i = 0; i < (level); i++) fprintf(f, "\t");}
49 #define EMV_BIT(byte, bit) ((byte - 1) * 8 + (8 - bit))
50 #define EMV_BIT_FINISH { (~0), NULL }
52 static 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" },
67 static 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" },
81 static 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" },
116 static 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" },
132 { 0x6f , "File Control Information (FCI) Template" },
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" },
203 static int emv_sort_tag(tlv_tag_t tag
)
205 return (int)(tag
>= 0x100 ? tag
: tag
<< 8);
208 static int emv_tlv_compare(const void *a
, const void *b
)
210 const struct tlv
*tlv
= a
;
211 const struct emv_tag
*tag
= b
;
213 return emv_sort_tag(tlv
->tag
) - (emv_sort_tag(tag
->tag
));
216 static const struct emv_tag
*emv_get_tag(const struct tlv
*tlv
)
218 struct emv_tag
*tag
= bsearch(tlv
, emv_tags
, sizeof(emv_tags
)/sizeof(emv_tags
[0]),
219 sizeof(emv_tags
[0]), emv_tlv_compare
);
221 return tag
? tag
: &emv_tags
[0];
224 static const char *bitstrings
[] = {
235 static void emv_tag_dump_bitmask(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
237 const struct emv_tag_bit
*bits
= tag
->data
;
240 for (byte
= 1; byte
<= tlv
->len
; byte
++) {
241 unsigned char val
= tlv
->value
[byte
- 1];
243 fprintf(f
, "\tByte %u (%02x)\n", byte
, val
);
244 for (bit
= 8; bit
> 0; bit
--, val
<<= 1) {
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
))
255 static void emv_tag_dump_dol(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
257 const unsigned char *buf
= tlv
->value
;
258 size_t left
= tlv
->len
;
262 const struct emv_tag
*doltag
;
264 if (!tlv_parse_tl(&buf
, &left
, &doltlv
)) {
266 fprintf(f
, "Invalid Tag-Len\n");
270 doltag
= emv_get_tag(&doltlv
);
273 fprintf(f
, "\tTag %4hx len %02zx ('%s')\n", doltlv
.tag
, doltlv
.len
, doltag
->name
);
277 static void emv_tag_dump_string(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
280 fprintf(f
, "\tString value '");
281 fwrite(tlv
->value
, 1, tlv
->len
, f
);
286 static unsigned long emv_value_numeric(const struct tlv
*tlv
, unsigned start
, unsigned end
)
288 unsigned long ret
= 0;
291 if (end
> tlv
->len
* 2)
297 ret
+= tlv
->value
[start
/2] & 0xf;
302 for (; i
< end
- 1; i
+= 2) {
304 ret
+= tlv
->value
[i
/2] >> 4;
306 ret
+= tlv
->value
[i
/2] & 0xf;
311 ret
+= tlv
->value
[end
/2] >> 4;
317 static void emv_tag_dump_numeric(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
320 fprintf(f
, "\tNumeric value %lu\n", emv_value_numeric(tlv
, 0, tlv
->len
* 2));
323 static void emv_tag_dump_yymmdd(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
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));
332 static uint32_t emv_get_binary(const unsigned char *S
)
334 return (S
[0] << 24) | (S
[1] << 16) | (S
[2] << 8) | (S
[3] << 0);
337 static void emv_tag_dump_cvm_list(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
, int level
)
342 if (tlv
->len
< 10 || tlv
->len
% 2) {
344 fprintf(f
, "\tINVALID!\n");
348 X
= emv_get_binary(tlv
->value
);
349 Y
= emv_get_binary(tlv
->value
+ 4);
352 fprintf(f
, "\tX: %d\n", X
);
354 fprintf(f
, "\tY: %d\n", Y
);
356 for (i
= 8; i
< tlv
->len
; i
+= 2) {
358 const char *condition
;
360 switch (tlv
->value
[i
] & 0x3f) {
362 method
= "Fail CVM processing";
365 method
= "Plaintext PIN verification performed by ICC";
368 method
= "Enciphered PIN verified online";
371 method
= "Plaintext PIN verification performed by ICC and signature (paper)";
374 method
= "Enciphered PIN verification performed by ICC";
377 method
= "Enciphered PIN verification performed by ICC and signature (paper)";
380 method
= "Signature (paper)";
383 method
= "No CVM required";
386 method
= "NOT AVAILABLE!";
393 switch (tlv
->value
[i
+1]) {
395 condition
= "Always";
398 condition
= "If unattended cash";
401 condition
= "If not unattended cash and not manual cash and not purchase with cashback";
404 condition
= "If terminal supports the CVM";
407 condition
= "If manual cash";
410 condition
= "If purchase with cashback";
413 condition
= "If transaction is in the application currency and is under X value";
416 condition
= "If transaction is in the application currency and is over X value";
419 condition
= "If transaction is in the application currency and is under Y value";
422 condition
= "If transaction is in the application currency and is over Y value";
425 condition
= "Unknown";
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");
436 bool emv_tag_dump(const struct tlv
*tlv
, FILE *f
, int level
)
439 fprintf(f
, "NULL\n");
443 const struct emv_tag
*tag
= emv_get_tag(tlv
);
446 fprintf(f
, "--%2hx[%02zx] '%s':\n", tlv
->tag
, tlv
->len
, tag
->name
);
449 case EMV_TAG_GENERIC
:
451 case EMV_TAG_BITMASK
:
452 emv_tag_dump_bitmask(tlv
, tag
, f
, level
);
455 emv_tag_dump_dol(tlv
, tag
, f
, level
);
457 case EMV_TAG_CVM_LIST
:
458 emv_tag_dump_cvm_list(tlv
, tag
, f
, level
);
461 emv_tag_dump_string(tlv
, tag
, f
, level
);
463 case EMV_TAG_NUMERIC
:
464 emv_tag_dump_numeric(tlv
, tag
, f
, level
);
467 emv_tag_dump_yymmdd(tlv
, tag
, f
, level
);