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