]>
Commit | Line | Data |
---|---|---|
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 | ||
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, | |
a2bb2735 | 33 | }; |
34 | ||
35 | struct emv_tag { | |
36 | tlv_tag_t tag; | |
37 | char *name; | |
38 | enum emv_tag_t type; | |
39 | const void *data; | |
40 | }; | |
41 | ||
42 | struct emv_tag_bit { | |
43 | unsigned bit; | |
44 | const char *name; | |
45 | }; | |
46 | ||
47 | #define EMV_BIT(byte, bit) ((byte - 1) * 8 + (8 - bit)) | |
48 | #define EMV_BIT_FINISH { (~0), NULL } | |
49 | ||
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" }, | |
62 | EMV_BIT_FINISH, | |
63 | }; | |
64 | ||
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" }, | |
76 | EMV_BIT_FINISH, | |
77 | }; | |
78 | ||
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" }, | |
111 | EMV_BIT_FINISH, | |
112 | }; | |
113 | ||
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" }, | |
23207d74 | 130 | { 0x6f , "File Control Information (FCI) Template" }, |
a2bb2735 | 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" }, | |
199 | }; | |
200 | ||
201 | static int emv_sort_tag(tlv_tag_t tag) | |
202 | { | |
203 | return (int)(tag >= 0x100 ? tag : tag << 8); | |
204 | } | |
205 | ||
206 | static int emv_tlv_compare(const void *a, const void *b) | |
207 | { | |
208 | const struct tlv *tlv = a; | |
209 | const struct emv_tag *tag = b; | |
210 | ||
211 | return emv_sort_tag(tlv->tag) - (emv_sort_tag(tag->tag)); | |
212 | } | |
213 | ||
214 | static const struct emv_tag *emv_get_tag(const struct tlv *tlv) | |
215 | { | |
216 | struct emv_tag *tag = bsearch(tlv, emv_tags, sizeof(emv_tags)/sizeof(emv_tags[0]), | |
217 | sizeof(emv_tags[0]), emv_tlv_compare); | |
218 | ||
219 | return tag ? tag : &emv_tags[0]; | |
220 | } | |
221 | ||
222 | static const char *bitstrings[] = { | |
223 | ".......1", | |
224 | "......1.", | |
225 | ".....1..", | |
226 | "....1...", | |
227 | "...1....", | |
228 | "..1.....", | |
229 | ".1......", | |
230 | "1.......", | |
231 | }; | |
232 | ||
233 | static void emv_tag_dump_bitmask(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) | |
234 | { | |
235 | const struct emv_tag_bit *bits = tag->data; | |
236 | unsigned bit, byte; | |
237 | ||
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) { | |
242 | if (val & 0x80) | |
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)) | |
246 | bits ++; | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | static void emv_tag_dump_dol(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) | |
252 | { | |
253 | const unsigned char *buf = tlv->value; | |
254 | size_t left = tlv->len; | |
255 | ||
256 | while (left) { | |
257 | struct tlv doltlv; | |
258 | const struct emv_tag *doltag; | |
259 | ||
260 | if (!tlv_parse_tl(&buf, &left, &doltlv)) { | |
261 | fprintf(f, "Invalid Tag-Len\n"); | |
262 | continue; | |
263 | } | |
264 | ||
265 | doltag = emv_get_tag(&doltlv); | |
266 | ||
267 | fprintf(f, "\tTag %4hx len %02zx ('%s')\n", doltlv.tag, doltlv.len, doltag->name); | |
268 | } | |
269 | } | |
270 | ||
a2bb2735 | 271 | static void emv_tag_dump_string(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) |
272 | { | |
273 | fprintf(f, "\tString value '"); | |
274 | fwrite(tlv->value, 1, tlv->len, f); | |
275 | fprintf(f, "'\n"); | |
276 | } | |
277 | ||
278 | static unsigned long emv_value_numeric(const struct tlv *tlv, unsigned start, unsigned end) | |
279 | { | |
280 | unsigned long ret = 0; | |
281 | int i; | |
282 | ||
283 | if (end > tlv->len * 2) | |
284 | return ret; | |
285 | if (start >= end) | |
286 | return ret; | |
287 | ||
288 | if (start & 1) { | |
289 | ret += tlv->value[start/2] & 0xf; | |
290 | i = start + 1; | |
291 | } else | |
292 | i = start; | |
293 | ||
294 | for (; i < end - 1; i += 2) { | |
295 | ret *= 10; | |
296 | ret += tlv->value[i/2] >> 4; | |
297 | ret *= 10; | |
298 | ret += tlv->value[i/2] & 0xf; | |
299 | } | |
300 | ||
301 | if (end & 1) { | |
302 | ret *= 10; | |
303 | ret += tlv->value[end/2] >> 4; | |
304 | } | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
309 | static void emv_tag_dump_numeric(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) | |
310 | { | |
311 | fprintf(f, "\tNumeric value %lu\n", emv_value_numeric(tlv, 0, tlv->len * 2)); | |
312 | } | |
313 | ||
314 | static void emv_tag_dump_yymmdd(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) | |
315 | { | |
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)); | |
320 | } | |
321 | ||
322 | static uint32_t emv_get_binary(const unsigned char *S) | |
323 | { | |
324 | return (S[0] << 24) | (S[1] << 16) | (S[2] << 8) | (S[3] << 0); | |
325 | } | |
326 | ||
327 | static void emv_tag_dump_cvm_list(const struct tlv *tlv, const struct emv_tag *tag, FILE *f) | |
328 | { | |
329 | uint32_t X, Y; | |
330 | int i; | |
331 | ||
332 | if (tlv->len < 10 || tlv->len % 2) { | |
333 | fprintf(f, "\tINVALID!\n"); | |
334 | return; | |
335 | } | |
336 | ||
337 | X = emv_get_binary(tlv->value); | |
338 | Y = emv_get_binary(tlv->value + 4); | |
339 | ||
340 | fprintf(f, "\tX: %d\n", X); | |
341 | fprintf(f, "\tY: %d\n", Y); | |
342 | ||
343 | for (i = 8; i < tlv->len; i+= 2) { | |
344 | const char *method; | |
345 | const char *condition; | |
346 | ||
347 | switch (tlv->value[i] & 0x3f) { | |
348 | case 0x0: | |
349 | method = "Fail CVM processing"; | |
350 | break; | |
351 | case 0x1: | |
352 | method = "Plaintext PIN verification performed by ICC"; | |
353 | break; | |
354 | case 0x2: | |
355 | method = "Enciphered PIN verified online"; | |
356 | break; | |
357 | case 0x3: | |
358 | method = "Plaintext PIN verification performed by ICC and signature (paper)"; | |
359 | break; | |
360 | case 0x4: | |
361 | method = "Enciphered PIN verification performed by ICC"; | |
362 | break; | |
363 | case 0x5: | |
364 | method = "Enciphered PIN verification performed by ICC and signature (paper)"; | |
365 | break; | |
366 | case 0x1e: | |
367 | method = "Signature (paper)"; | |
368 | break; | |
369 | case 0x1f: | |
370 | method = "No CVM required"; | |
371 | break; | |
372 | case 0x3f: | |
373 | method = "NOT AVAILABLE!"; | |
374 | break; | |
375 | default: | |
376 | method = "Unknown"; | |
377 | break; | |
378 | } | |
379 | ||
380 | switch (tlv->value[i+1]) { | |
381 | case 0x00: | |
382 | condition = "Always"; | |
383 | break; | |
384 | case 0x01: | |
385 | condition = "If unattended cash"; | |
386 | break; | |
387 | case 0x02: | |
388 | condition = "If not unattended cash and not manual cash and not purchase with cashback"; | |
389 | break; | |
390 | case 0x03: | |
391 | condition = "If terminal supports the CVM"; | |
392 | break; | |
393 | case 0x04: | |
394 | condition = "If manual cash"; | |
395 | break; | |
396 | case 0x05: | |
397 | condition = "If purchase with cashback"; | |
398 | break; | |
399 | case 0x06: | |
400 | condition = "If transaction is in the application currency and is under X value"; | |
401 | break; | |
402 | case 0x07: | |
403 | condition = "If transaction is in the application currency and is over X value"; | |
404 | break; | |
405 | case 0x08: | |
406 | condition = "If transaction is in the application currency and is under Y value"; | |
407 | break; | |
408 | case 0x09: | |
409 | condition = "If transaction is in the application currency and is over Y value"; | |
410 | break; | |
411 | default: | |
412 | condition = "Unknown"; | |
413 | break; | |
414 | } | |
415 | ||
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"); | |
419 | } | |
420 | } | |
421 | ||
422 | bool emv_tag_dump(const struct tlv *tlv, FILE *f) | |
423 | { | |
424 | if (!tlv) { | |
425 | fprintf(f, "NULL\n"); | |
426 | return false; | |
427 | } | |
428 | ||
429 | const struct emv_tag *tag = emv_get_tag(tlv); | |
430 | ||
431 | fprintf(f, "--%2hx[%02zx] '%s':\n", tlv->tag, tlv->len, tag->name); | |
432 | ||
433 | switch (tag->type) { | |
434 | case EMV_TAG_GENERIC: | |
435 | break; | |
436 | case EMV_TAG_BITMASK: | |
437 | emv_tag_dump_bitmask(tlv, tag, f); | |
438 | break; | |
439 | case EMV_TAG_DOL: | |
440 | emv_tag_dump_dol(tlv, tag, f); | |
441 | break; | |
442 | case EMV_TAG_CVM_LIST: | |
443 | emv_tag_dump_cvm_list(tlv, tag, f); | |
444 | break; | |
445 | case EMV_TAG_STRING: | |
446 | emv_tag_dump_string(tlv, tag, f); | |
447 | break; | |
448 | case EMV_TAG_NUMERIC: | |
449 | emv_tag_dump_numeric(tlv, tag, f); | |
450 | break; | |
451 | case EMV_TAG_YYMMDD: | |
452 | emv_tag_dump_yymmdd(tlv, tag, f); | |
453 | break; | |
a2bb2735 | 454 | }; |
455 | ||
456 | return true; | |
457 | } |