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