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