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