]>
git.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.
47 #define EMV_BIT(byte, bit) ((byte - 1) * 8 + (8 - bit))
48 #define EMV_BIT_FINISH { (~0), NULL }
50 static const struct emv_tag_bit EMV_AIP
[] = {
51 { EMV_BIT(1, 7), "SDA supported" },
52 { EMV_BIT(1, 6), "DDA supported" },
53 { EMV_BIT(1, 5), "Cardholder verification is supported" },
54 { EMV_BIT(1, 4), "Terminal risk management is to be performed" },
55 { EMV_BIT(1, 3), "Issuer authentication is supported" },
56 { EMV_BIT(1, 2), "Reserved for use by the EMV Contactless Specifications" },
57 { EMV_BIT(1, 1), "CDA supported" },
58 { EMV_BIT(2, 8), "Reserved for use by the EMV Contactless Specifications" },
59 { EMV_BIT(2, 7), "Reserved for use by the EMV Contactless Specifications" },
60 { EMV_BIT(2, 6), "Reserved for use by the EMV Contactless Specifications" },
61 { EMV_BIT(2, 1), "Reserved for use by the EMV Contactless Specifications" },
65 static const struct emv_tag_bit EMV_AUC
[] = {
66 { EMV_BIT(1, 8), "Valid for domestic cash transactions" },
67 { EMV_BIT(1, 7), "Valid for international cash transactions" },
68 { EMV_BIT(1, 6), "Valid for domestic goods" },
69 { EMV_BIT(1, 5), "Valid for international goods" },
70 { EMV_BIT(1, 4), "Valid for domestic services" },
71 { EMV_BIT(1, 3), "Valid for international services" },
72 { EMV_BIT(1, 2), "Valid for ATMs" },
73 { EMV_BIT(1, 1), "Valid at terminals other than ATMs" },
74 { EMV_BIT(2, 8), "Domestic cashback allowed" },
75 { EMV_BIT(2, 7), "International cashback allowed" },
79 static const struct emv_tag_bit EMV_TVR
[] = {
80 { EMV_BIT(1, 8), "Offline data authentication was not performed" },
81 { EMV_BIT(1, 7), "SDA failed" },
82 { EMV_BIT(1, 6), "ICC data missing" },
83 { EMV_BIT(1, 5), "Card appears on terminal exception file" },
84 { EMV_BIT(1, 4), "DDA failed" },
85 { EMV_BIT(1, 3), "CDA failed" },
86 { EMV_BIT(1, 2), "SDA selected" },
87 { EMV_BIT(2, 8), "ICC and terminal have different application versions" },
88 { EMV_BIT(2, 7), "Expired application" },
89 { EMV_BIT(2, 6), "Application not yet effective" },
90 { EMV_BIT(2, 5), "Requested service not allowed for card product" },
91 { EMV_BIT(2, 4), "New card" },
92 { EMV_BIT(3, 8), "Cardholder verification was not successful" },
93 { EMV_BIT(3, 7), "Unrecognised CVM" },
94 { EMV_BIT(3, 6), "PIN Try Limit exceeded" },
95 { EMV_BIT(3, 5), "PIN entry required and PIN pad not present or not working" },
96 { EMV_BIT(3, 4), "PIN entry required, PIN pad present, but PIN was not entered" },
97 { EMV_BIT(3, 3), "Online PIN entered" },
98 { EMV_BIT(4, 8), "Transaction exceeds floor limit" },
99 { EMV_BIT(4, 7), "Lower consecutive offline limit exceeded" },
100 { EMV_BIT(4, 6), "Upper consecutive offline limit exceeded" },
101 { EMV_BIT(4, 5), "Transaction selected randomly for online processing" },
102 { EMV_BIT(4, 4), "Merchant forced transaction online" },
103 { EMV_BIT(5, 8), "Default TDOL used" },
104 { EMV_BIT(5, 7), "Issuer authentication failed" },
105 { EMV_BIT(5, 6), "Script processing failed before final GENERATE AC" },
106 { EMV_BIT(5, 5), "Script processing failed after final GENERATE AC" },
107 { EMV_BIT(5, 4), "Reserved for use by the EMV Contactless Specifications" },
108 { EMV_BIT(5, 3), "Reserved for use by the EMV Contactless Specifications" },
109 { EMV_BIT(5, 2), "Reserved for use by the EMV Contactless Specifications" },
110 { EMV_BIT(5, 1), "Reserved for use by the EMV Contactless Specifications" },
114 static const struct emv_tag emv_tags
[] = {
115 { 0x00 , "Unknown ???" },
116 { 0x4f , "Application Dedicated File (ADF) Name" },
117 { 0x50 , "Application Label", EMV_TAG_STRING
},
118 { 0x56 , "Track 1 Data" },
119 { 0x57 , "Track 2 Equivalent Data" },
120 { 0x5a , "Application Primary Account Number (PAN)" },
121 { 0x5f20, "Cardholder Name", EMV_TAG_STRING
},
122 { 0x5f24, "Application Expiration Date", EMV_TAG_YYMMDD
},
123 { 0x5f25, "Application Effective Date", EMV_TAG_YYMMDD
},
124 { 0x5f28, "Issuer Country Code", EMV_TAG_NUMERIC
},
125 { 0x5f2a, "Transaction Currency Code", EMV_TAG_NUMERIC
},
126 { 0x5f2d, "Language Preference", EMV_TAG_STRING
},
127 { 0x5f30, "Service Code", EMV_TAG_NUMERIC
},
128 { 0x5f34, "Application Primary Account Number (PAN) Sequence Number", EMV_TAG_NUMERIC
},
129 { 0x61 , "Application Template" },
130 { 0x6f , "File Control Information (FCI) Template" },
131 { 0x70 , "READ RECORD Response Message Template" },
132 { 0x77 , "Response Message Template Format 2" },
133 { 0x80 , "Response Message Template Format 1" },
134 { 0x82 , "Application Interchange Profile", EMV_TAG_BITMASK
, &EMV_AIP
},
135 { 0x83 , "Command Template" },
136 { 0x84 , "Dedicated File (DF) Name" },
137 { 0x87 , "Application Priority Indicator" },
138 { 0x88 , "Short File Identifier (SFI)" },
139 { 0x8a , "Authorisation Response Code" },
140 { 0x8c , "Card Risk Management Data Object List 1 (CDOL1)", EMV_TAG_DOL
},
141 { 0x8d , "Card Risk Management Data Object List 2 (CDOL2)", EMV_TAG_DOL
},
142 { 0x8e , "Cardholder Verification Method (CVM) List", EMV_TAG_CVM_LIST
},
143 { 0x8f , "Certification Authority Public Key Index" },
144 { 0x90 , "Issuer Public Key Certificate" },
145 { 0x91 , "Issuer Authentication Data" },
146 { 0x92 , "Issuer Public Key Remainder" },
147 { 0x93 , "Signed Static Application Data" },
148 { 0x94 , "Application File Locator (AFL)" },
149 { 0x95 , "Terminal Verification Results" },
150 { 0x9a , "Transaction Date", EMV_TAG_YYMMDD
},
151 { 0x9c , "Transaction Type" },
152 { 0x9f02, "Amount, Authorised (Numeric)", EMV_TAG_NUMERIC
},
153 { 0x9f03, "Amount, Other (Numeric)", EMV_TAG_NUMERIC
, },
154 { 0x9f07, "Application Usage Control", EMV_TAG_BITMASK
, &EMV_AUC
},
155 { 0x9f08, "Application Version Number" },
156 { 0x9f0d, "Issuer Action Code - Default", EMV_TAG_BITMASK
, &EMV_TVR
},
157 { 0x9f0e, "Issuer Action Code - Denial", EMV_TAG_BITMASK
, &EMV_TVR
},
158 { 0x9f0f, "Issuer Action Code - Online", EMV_TAG_BITMASK
, &EMV_TVR
},
159 { 0x9f10, "Issuer Application Data" },
160 { 0x9f11, "Issuer Code Table Index", EMV_TAG_NUMERIC
},
161 { 0x9f12, "Application Preferred Name", EMV_TAG_STRING
},
162 { 0x9f13, "Last Online Application Transaction Counter (ATC) Register" },
163 { 0x9f17, "Personal Identification Number (PIN) Try Counter" },
164 { 0x9f1a, "Terminal Country Code" },
165 { 0x9f1f, "Track 1 Discretionary Data", EMV_TAG_STRING
},
166 { 0x9f21, "Transaction Time" },
167 { 0x9f26, "Application Cryptogram" },
168 { 0x9f27, "Cryptogram Information Data" },
169 { 0x9f2d, "ICC PIN Encipherment Public Key Certificate" },
170 { 0x9f2e, "ICC PIN Encipherment Public Key Exponent" },
171 { 0x9f2f, "ICC PIN Encipherment Public Key Remainder" },
172 { 0x9f32, "Issuer Public Key Exponent" },
173 { 0x9f34, "Cardholder Verification Method (CVM) Results" },
174 { 0x9f35, "Terminal Type" },
175 { 0x9f36, "Application Transaction Counter (ATC)" },
176 { 0x9f37, "Unpredictable Number" },
177 { 0x9f38, "Processing Options Data Object List (PDOL)", EMV_TAG_DOL
},
178 { 0x9f42, "Application Currency Code", EMV_TAG_NUMERIC
},
179 { 0x9f44, "Application Currency Exponent", EMV_TAG_NUMERIC
},
180 { 0x9f45, "Data Authentication Code" },
181 { 0x9f46, "ICC Public Key Certificate" },
182 { 0x9f47, "ICC Public Key Exponent" },
183 { 0x9f48, "ICC Public Key Remainder" },
184 { 0x9f49, "Dynamic Data Authentication Data Object List (DDOL)", EMV_TAG_DOL
},
185 { 0x9f4a, "Static Data Authentication Tag List" },
186 { 0x9f4b, "Signed Dynamic Application Data" },
187 { 0x9f4c, "ICC Dynamic Number" },
188 { 0x9f4d, "Log Entry" },
189 { 0x9f4f, "Log Format", EMV_TAG_DOL
},
190 { 0x9f62, "PCVC3(Track1)" },
191 { 0x9f63, "PUNATC(Track1)" },
192 { 0x9f64, "NATC(Track1)" },
193 { 0x9f65, "PCVC3(Track2)" },
194 { 0x9f66, "PUNATC(Track2)" },
195 { 0x9f67, "NATC(Track2)" },
196 { 0x9f6b, "Track 2 Data" },
197 { 0xa5 , "File Control Information (FCI) Proprietary Template" },
198 { 0xbf0c, "File Control Information (FCI) Issuer Discretionary Data" },
201 static int emv_sort_tag(tlv_tag_t tag
)
203 return (int)(tag
>= 0x100 ? tag
: tag
<< 8);
206 static int emv_tlv_compare(const void *a
, const void *b
)
208 const struct tlv
*tlv
= a
;
209 const struct emv_tag
*tag
= b
;
211 return emv_sort_tag(tlv
->tag
) - (emv_sort_tag(tag
->tag
));
214 static const struct emv_tag
*emv_get_tag(const struct tlv
*tlv
)
216 struct emv_tag
*tag
= bsearch(tlv
, emv_tags
, sizeof(emv_tags
)/sizeof(emv_tags
[0]),
217 sizeof(emv_tags
[0]), emv_tlv_compare
);
219 return tag
? tag
: &emv_tags
[0];
222 static const char *bitstrings
[] = {
233 static void emv_tag_dump_bitmask(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
235 const struct emv_tag_bit
*bits
= tag
->data
;
238 for (byte
= 1; byte
<= tlv
->len
; byte
++) {
239 unsigned char val
= tlv
->value
[byte
- 1];
240 fprintf(f
, "\tByte %u (%02x)\n", byte
, val
);
241 for (bit
= 8; bit
> 0; bit
--, val
<<= 1) {
243 fprintf(f
, "\t\t%s - '%s'\n", bitstrings
[bit
- 1],
244 bits
->bit
== EMV_BIT(byte
, bit
) ? bits
->name
: "Unknown");
245 if (bits
->bit
== EMV_BIT(byte
, bit
))
251 static void emv_tag_dump_dol(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
253 const unsigned char *buf
= tlv
->value
;
254 size_t left
= tlv
->len
;
258 const struct emv_tag
*doltag
;
260 if (!tlv_parse_tl(&buf
, &left
, &doltlv
)) {
261 fprintf(f
, "Invalid Tag-Len\n");
265 doltag
= emv_get_tag(&doltlv
);
267 fprintf(f
, "\tTag %4hx len %02zx ('%s')\n", doltlv
.tag
, doltlv
.len
, doltag
->name
);
271 static void emv_tag_dump_string(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
273 fprintf(f
, "\tString value '");
274 fwrite(tlv
->value
, 1, tlv
->len
, f
);
278 static unsigned long emv_value_numeric(const struct tlv
*tlv
, unsigned start
, unsigned end
)
280 unsigned long ret
= 0;
283 if (end
> tlv
->len
* 2)
289 ret
+= tlv
->value
[start
/2] & 0xf;
294 for (; i
< end
- 1; i
+= 2) {
296 ret
+= tlv
->value
[i
/2] >> 4;
298 ret
+= tlv
->value
[i
/2] & 0xf;
303 ret
+= tlv
->value
[end
/2] >> 4;
309 static void emv_tag_dump_numeric(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
311 fprintf(f
, "\tNumeric value %lu\n", emv_value_numeric(tlv
, 0, tlv
->len
* 2));
314 static void emv_tag_dump_yymmdd(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
316 fprintf(f
, "\tDate: 20%02ld.%ld.%ld\n",
317 emv_value_numeric(tlv
, 0, 2),
318 emv_value_numeric(tlv
, 2, 4),
319 emv_value_numeric(tlv
, 4, 6));
322 static uint32_t emv_get_binary(const unsigned char *S
)
324 return (S
[0] << 24) | (S
[1] << 16) | (S
[2] << 8) | (S
[3] << 0);
327 static void emv_tag_dump_cvm_list(const struct tlv
*tlv
, const struct emv_tag
*tag
, FILE *f
)
332 if (tlv
->len
< 10 || tlv
->len
% 2) {
333 fprintf(f
, "\tINVALID!\n");
337 X
= emv_get_binary(tlv
->value
);
338 Y
= emv_get_binary(tlv
->value
+ 4);
340 fprintf(f
, "\tX: %d\n", X
);
341 fprintf(f
, "\tY: %d\n", Y
);
343 for (i
= 8; i
< tlv
->len
; i
+= 2) {
345 const char *condition
;
347 switch (tlv
->value
[i
] & 0x3f) {
349 method
= "Fail CVM processing";
352 method
= "Plaintext PIN verification performed by ICC";
355 method
= "Enciphered PIN verified online";
358 method
= "Plaintext PIN verification performed by ICC and signature (paper)";
361 method
= "Enciphered PIN verification performed by ICC";
364 method
= "Enciphered PIN verification performed by ICC and signature (paper)";
367 method
= "Signature (paper)";
370 method
= "No CVM required";
373 method
= "NOT AVAILABLE!";
380 switch (tlv
->value
[i
+1]) {
382 condition
= "Always";
385 condition
= "If unattended cash";
388 condition
= "If not unattended cash and not manual cash and not purchase with cashback";
391 condition
= "If terminal supports the CVM";
394 condition
= "If manual cash";
397 condition
= "If purchase with cashback";
400 condition
= "If transaction is in the application currency and is under X value";
403 condition
= "If transaction is in the application currency and is over X value";
406 condition
= "If transaction is in the application currency and is under Y value";
409 condition
= "If transaction is in the application currency and is over Y value";
412 condition
= "Unknown";
416 fprintf(f
, "\t%02x %02x: '%s' '%s' and '%s' if this CVM is unsuccessful\n",
417 tlv
->value
[i
], tlv
->value
[i
+1],
418 method
, condition
, (tlv
->value
[i
] & 0x40) ? "continue" : "fail");
422 bool emv_tag_dump(const struct tlv
*tlv
, FILE *f
)
425 fprintf(f
, "NULL\n");
429 const struct emv_tag
*tag
= emv_get_tag(tlv
);
431 fprintf(f
, "--%2hx[%02zx] '%s':\n", tlv
->tag
, tlv
->len
, tag
->name
);
434 case EMV_TAG_GENERIC
:
436 case EMV_TAG_BITMASK
:
437 emv_tag_dump_bitmask(tlv
, tag
, f
);
440 emv_tag_dump_dol(tlv
, tag
, f
);
442 case EMV_TAG_CVM_LIST
:
443 emv_tag_dump_cvm_list(tlv
, tag
, f
);
446 emv_tag_dump_string(tlv
, tag
, f
);
448 case EMV_TAG_NUMERIC
:
449 emv_tag_dump_numeric(tlv
, tag
, f
);
452 emv_tag_dump_yymmdd(tlv
, tag
, f
);