emv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "emv.h"
  2. #include <core/common_defines.h>
  3. #define TAG "Emv"
  4. const PDOLValue pdol_term_info = {0x9F59, {0xC8, 0x80, 0x00}}; // Terminal transaction information
  5. const PDOLValue pdol_term_type = {0x9F5A, {0x00}}; // Terminal transaction type
  6. const PDOLValue pdol_merchant_type = {0x9F58, {0x01}}; // Merchant type indicator
  7. const PDOLValue pdol_term_trans_qualifies = {
  8. 0x9F66,
  9. {0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
  10. const PDOLValue pdol_addtnl_term_qualifies = {
  11. 0x9F40,
  12. {0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
  13. const PDOLValue pdol_amount_authorise = {
  14. 0x9F02,
  15. {0x00, 0x00, 0x00, 0x10, 0x00, 0x00}}; // Amount, authorised
  16. const PDOLValue pdol_amount = {0x9F03, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; // Amount
  17. const PDOLValue pdol_country_code = {0x9F1A, {0x01, 0x24}}; // Terminal country code
  18. const PDOLValue pdol_currency_code = {0x5F2A, {0x01, 0x24}}; // Transaction currency code
  19. const PDOLValue pdol_term_verification = {
  20. 0x95,
  21. {0x00, 0x00, 0x00, 0x00, 0x00}}; // Terminal verification results
  22. const PDOLValue pdol_transaction_date = {0x9A, {0x19, 0x01, 0x01}}; // Transaction date
  23. const PDOLValue pdol_transaction_type = {0x9C, {0x00}}; // Transaction type
  24. const PDOLValue pdol_transaction_cert = {0x98, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; // Transaction cert
  26. const PDOLValue pdol_unpredict_number = {0x9F37, {0x82, 0x3D, 0xDE, 0x7A}}; // Unpredictable number
  27. const PDOLValue* const pdol_values[] = {
  28. &pdol_term_info,
  29. &pdol_term_type,
  30. &pdol_merchant_type,
  31. &pdol_term_trans_qualifies,
  32. &pdol_addtnl_term_qualifies,
  33. &pdol_amount_authorise,
  34. &pdol_amount,
  35. &pdol_country_code,
  36. &pdol_currency_code,
  37. &pdol_term_verification,
  38. &pdol_transaction_date,
  39. &pdol_transaction_type,
  40. &pdol_transaction_cert,
  41. &pdol_unpredict_number,
  42. };
  43. static const uint8_t select_ppse_ans[] = {0x6F, 0x29, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E,
  44. 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31,
  45. 0xA5, 0x17, 0xBF, 0x0C, 0x14, 0x61, 0x12, 0x4F, 0x07,
  46. 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x50, 0x04,
  47. 0x56, 0x49, 0x53, 0x41, 0x87, 0x01, 0x01, 0x90, 0x00};
  48. static const uint8_t select_app_ans[] = {0x6F, 0x20, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03,
  49. 0x10, 0x10, 0xA5, 0x15, 0x50, 0x04, 0x56, 0x49, 0x53,
  50. 0x41, 0x9F, 0x38, 0x0C, 0x9F, 0x66, 0x04, 0x9F, 0x02,
  51. 0x06, 0x9F, 0x37, 0x04, 0x5F, 0x2A, 0x02, 0x90, 0x00};
  52. static const uint8_t pdol_ans[] = {0x77, 0x40, 0x82, 0x02, 0x20, 0x00, 0x57, 0x13, 0x55, 0x70,
  53. 0x73, 0x83, 0x85, 0x87, 0x73, 0x31, 0xD1, 0x80, 0x22, 0x01,
  54. 0x38, 0x84, 0x77, 0x94, 0x00, 0x00, 0x1F, 0x5F, 0x34, 0x01,
  55. 0x00, 0x9F, 0x10, 0x07, 0x06, 0x01, 0x11, 0x03, 0x80, 0x00,
  56. 0x00, 0x9F, 0x26, 0x08, 0x7A, 0x65, 0x7F, 0xD3, 0x52, 0x96,
  57. 0xC9, 0x85, 0x9F, 0x27, 0x01, 0x00, 0x9F, 0x36, 0x02, 0x06,
  58. 0x0C, 0x9F, 0x6C, 0x02, 0x10, 0x00, 0x90, 0x00};
  59. static void emv_trace(FuriHalNfcTxRxContext* tx_rx, const char* message) {
  60. if(furi_log_get_level() == FuriLogLevelTrace) {
  61. FURI_LOG_T(TAG, "%s", message);
  62. printf("TX: ");
  63. for(size_t i = 0; i < tx_rx->tx_bits / 8; i++) {
  64. printf("%02X ", tx_rx->tx_data[i]);
  65. }
  66. printf("\r\nRX: ");
  67. for(size_t i = 0; i < tx_rx->rx_bits / 8; i++) {
  68. printf("%02X ", tx_rx->rx_data[i]);
  69. }
  70. printf("\r\n");
  71. }
  72. }
  73. static bool emv_decode_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  74. uint16_t i = 0;
  75. uint16_t tag = 0, first_byte = 0;
  76. uint16_t tlen = 0;
  77. bool success = false;
  78. while(i < len) {
  79. first_byte = buff[i];
  80. if((first_byte & 31) == 31) { // 2-byte tag
  81. tag = buff[i] << 8 | buff[i + 1];
  82. i++;
  83. FURI_LOG_T(TAG, " 2-byte TLV EMV tag: %x", tag);
  84. } else {
  85. tag = buff[i];
  86. FURI_LOG_T(TAG, " 1-byte TLV EMV tag: %x", tag);
  87. }
  88. i++;
  89. tlen = buff[i];
  90. if((tlen & 128) == 128) { // long length value
  91. i++;
  92. tlen = buff[i];
  93. FURI_LOG_T(TAG, " 2-byte TLV length: %d", tlen);
  94. } else {
  95. FURI_LOG_T(TAG, " 1-byte TLV length: %d", tlen);
  96. }
  97. i++;
  98. if((first_byte & 32) == 32) { // "Constructed" -- contains more TLV data to parse
  99. FURI_LOG_T(TAG, "Constructed TLV %x", tag);
  100. if(!emv_decode_response(&buff[i], tlen, app)) {
  101. FURI_LOG_T(TAG, "Failed to decode response for %x", tag);
  102. // return false;
  103. } else {
  104. success = true;
  105. }
  106. } else {
  107. switch(tag) {
  108. case EMV_TAG_AID:
  109. app->aid_len = tlen;
  110. memcpy(app->aid, &buff[i], tlen);
  111. success = true;
  112. FURI_LOG_T(TAG, "found EMV_TAG_AID %x", tag);
  113. break;
  114. case EMV_TAG_PRIORITY:
  115. memcpy(&app->priority, &buff[i], tlen);
  116. success = true;
  117. break;
  118. case EMV_TAG_CARD_NAME:
  119. memcpy(app->name, &buff[i], tlen);
  120. app->name[tlen] = '\0';
  121. app->name_found = true;
  122. success = true;
  123. FURI_LOG_T(TAG, "found EMV_TAG_CARD_NAME %x : %s", tag, app->name);
  124. break;
  125. case EMV_TAG_PDOL:
  126. memcpy(app->pdol.data, &buff[i], tlen);
  127. app->pdol.size = tlen;
  128. success = true;
  129. FURI_LOG_T(TAG, "found EMV_TAG_PDOL %x (len=%d)", tag, tlen);
  130. break;
  131. case EMV_TAG_AFL:
  132. memcpy(app->afl.data, &buff[i], tlen);
  133. app->afl.size = tlen;
  134. success = true;
  135. FURI_LOG_T(TAG, "found EMV_TAG_AFL %x (len=%d)", tag, tlen);
  136. break;
  137. case EMV_TAG_TRACK_1_EQUIV: {
  138. char track_1_equiv[80];
  139. memcpy(track_1_equiv, &buff[i], tlen);
  140. track_1_equiv[tlen] = '\0';
  141. success = true;
  142. FURI_LOG_T(TAG, "found EMV_TAG_TRACK_1_EQUIV %x : %s", tag, track_1_equiv);
  143. break;
  144. }
  145. case EMV_TAG_TRACK_2_EQUIV: {
  146. // 0xD0 delimits PAN from expiry (YYMM)
  147. for(int x = 1; x < tlen; x++) {
  148. if(buff[i + x + 1] > 0xD0) {
  149. memcpy(app->card_number, &buff[i], x + 1);
  150. app->card_number_len = x + 1;
  151. app->exp_year = (buff[i + x + 1] << 4) | (buff[i + x + 2] >> 4);
  152. app->exp_month = (buff[i + x + 2] << 4) | (buff[i + x + 3] >> 4);
  153. break;
  154. }
  155. }
  156. // Convert 4-bit to ASCII representation
  157. char track_2_equiv[41];
  158. uint8_t track_2_equiv_len = 0;
  159. for(int x = 0; x < tlen; x++) {
  160. char top = (buff[i + x] >> 4) + '0';
  161. char bottom = (buff[i + x] & 0x0F) + '0';
  162. track_2_equiv[x * 2] = top;
  163. track_2_equiv_len++;
  164. if(top == '?') break;
  165. track_2_equiv[x * 2 + 1] = bottom;
  166. track_2_equiv_len++;
  167. if(bottom == '?') break;
  168. }
  169. track_2_equiv[track_2_equiv_len] = '\0';
  170. success = true;
  171. FURI_LOG_T(TAG, "found EMV_TAG_TRACK_2_EQUIV %x : %s", tag, track_2_equiv);
  172. break;
  173. }
  174. case EMV_TAG_PAN:
  175. memcpy(app->card_number, &buff[i], tlen);
  176. app->card_number_len = tlen;
  177. success = true;
  178. break;
  179. case EMV_TAG_EXP_DATE:
  180. app->exp_year = buff[i];
  181. app->exp_month = buff[i + 1];
  182. success = true;
  183. break;
  184. case EMV_TAG_CURRENCY_CODE:
  185. app->currency_code = (buff[i] << 8 | buff[i + 1]);
  186. success = true;
  187. break;
  188. case EMV_TAG_COUNTRY_CODE:
  189. app->country_code = (buff[i] << 8 | buff[i + 1]);
  190. success = true;
  191. break;
  192. }
  193. }
  194. i += tlen;
  195. }
  196. return success;
  197. }
  198. static bool emv_select_ppse(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  199. bool app_aid_found = false;
  200. const uint8_t emv_select_ppse_cmd[] = {
  201. 0x00, 0xA4, // SELECT ppse
  202. 0x04, 0x00, // P1:By name, P2: empty
  203. 0x0e, // Lc: Data length
  204. 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
  205. 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
  206. 0x00 // Le
  207. };
  208. memcpy(tx_rx->tx_data, emv_select_ppse_cmd, sizeof(emv_select_ppse_cmd));
  209. tx_rx->tx_bits = sizeof(emv_select_ppse_cmd) * 8;
  210. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  211. FURI_LOG_D(TAG, "Send select PPSE");
  212. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  213. emv_trace(tx_rx, "Select PPSE answer:");
  214. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  215. app_aid_found = true;
  216. } else {
  217. FURI_LOG_E(TAG, "Failed to parse application");
  218. }
  219. } else {
  220. FURI_LOG_E(TAG, "Failed select PPSE");
  221. }
  222. return app_aid_found;
  223. }
  224. static bool emv_select_app(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  225. app->app_started = false;
  226. const uint8_t emv_select_header[] = {
  227. 0x00,
  228. 0xA4, // SELECT application
  229. 0x04,
  230. 0x00 // P1:By name, P2:First or only occurence
  231. };
  232. uint16_t size = sizeof(emv_select_header);
  233. // Copy header
  234. memcpy(tx_rx->tx_data, emv_select_header, size);
  235. // Copy AID
  236. tx_rx->tx_data[size++] = app->aid_len;
  237. memcpy(&tx_rx->tx_data[size], app->aid, app->aid_len);
  238. size += app->aid_len;
  239. tx_rx->tx_data[size++] = 0x00;
  240. tx_rx->tx_bits = size * 8;
  241. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  242. FURI_LOG_D(TAG, "Start application");
  243. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  244. emv_trace(tx_rx, "Start application answer:");
  245. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  246. app->app_started = true;
  247. } else {
  248. FURI_LOG_E(TAG, "Failed to read PAN or PDOL");
  249. }
  250. } else {
  251. FURI_LOG_E(TAG, "Failed to start application");
  252. }
  253. return app->app_started;
  254. }
  255. static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
  256. bool tag_found;
  257. for(uint16_t i = 0; i < src->size; i++) {
  258. tag_found = false;
  259. for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
  260. if(src->data[i] == pdol_values[j]->tag) {
  261. // Found tag with 1 byte length
  262. uint8_t len = src->data[++i];
  263. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  264. dest->size += len;
  265. tag_found = true;
  266. break;
  267. } else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
  268. // Found tag with 2 byte length
  269. i += 2;
  270. uint8_t len = src->data[i];
  271. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  272. dest->size += len;
  273. tag_found = true;
  274. break;
  275. }
  276. }
  277. if(!tag_found) {
  278. // Unknown tag, fill zeros
  279. i += 2;
  280. uint8_t len = src->data[i];
  281. memset(dest->data + dest->size, 0, len);
  282. dest->size += len;
  283. }
  284. }
  285. return dest->size;
  286. }
  287. static bool emv_get_processing_options(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  288. bool card_num_read = false;
  289. const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
  290. uint16_t size = sizeof(emv_gpo_header);
  291. // Copy header
  292. memcpy(tx_rx->tx_data, emv_gpo_header, size);
  293. APDU pdol_data = {0, {0}};
  294. // Prepare and copy pdol parameters
  295. emv_prepare_pdol(&pdol_data, &app->pdol);
  296. tx_rx->tx_data[size++] = 0x02 + pdol_data.size;
  297. tx_rx->tx_data[size++] = 0x83;
  298. tx_rx->tx_data[size++] = pdol_data.size;
  299. memcpy(tx_rx->tx_data + size, pdol_data.data, pdol_data.size);
  300. size += pdol_data.size;
  301. tx_rx->tx_data[size++] = 0;
  302. tx_rx->tx_bits = size * 8;
  303. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  304. FURI_LOG_D(TAG, "Get proccessing options");
  305. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  306. emv_trace(tx_rx, "Get processing options answer:");
  307. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  308. if(app->card_number_len > 0) {
  309. card_num_read = true;
  310. }
  311. }
  312. } else {
  313. FURI_LOG_E(TAG, "Failed to get processing options");
  314. }
  315. return card_num_read;
  316. }
  317. static bool emv_read_sfi_record(
  318. FuriHalNfcTxRxContext* tx_rx,
  319. EmvApplication* app,
  320. uint8_t sfi,
  321. uint8_t record_num) {
  322. bool card_num_read = false;
  323. uint8_t sfi_param = (sfi << 3) | (1 << 2);
  324. uint8_t emv_sfi_header[] = {
  325. 0x00,
  326. 0xB2, // READ RECORD
  327. record_num, // P1:record_number
  328. sfi_param, // P2:SFI
  329. 0x00 // Le
  330. };
  331. memcpy(tx_rx->tx_data, emv_sfi_header, sizeof(emv_sfi_header));
  332. tx_rx->tx_bits = sizeof(emv_sfi_header) * 8;
  333. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  334. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  335. emv_trace(tx_rx, "SFI record:");
  336. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  337. card_num_read = true;
  338. }
  339. } else {
  340. FURI_LOG_E(TAG, "Failed to read SFI record %d", record_num);
  341. }
  342. return card_num_read;
  343. }
  344. static bool emv_read_files(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  345. bool card_num_read = false;
  346. if(app->afl.size == 0) {
  347. return false;
  348. }
  349. FURI_LOG_D(TAG, "Search PAN in SFI");
  350. // Iterate through all files
  351. for(size_t i = 0; i < app->afl.size; i += 4) {
  352. uint8_t sfi = app->afl.data[i] >> 3;
  353. uint8_t record_start = app->afl.data[i + 1];
  354. uint8_t record_end = app->afl.data[i + 2];
  355. // Iterate through all records in file
  356. for(uint8_t record = record_start; record <= record_end; ++record) {
  357. card_num_read |= emv_read_sfi_record(tx_rx, app, sfi, record);
  358. }
  359. }
  360. return card_num_read;
  361. }
  362. bool emv_read_bank_card(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
  363. furi_assert(tx_rx);
  364. furi_assert(emv_app);
  365. bool card_num_read = false;
  366. memset(emv_app, 0, sizeof(EmvApplication));
  367. do {
  368. if(!emv_select_ppse(tx_rx, emv_app)) break;
  369. if(!emv_select_app(tx_rx, emv_app)) break;
  370. if(emv_get_processing_options(tx_rx, emv_app)) {
  371. card_num_read = true;
  372. } else {
  373. card_num_read = emv_read_files(tx_rx, emv_app);
  374. }
  375. } while(false);
  376. return card_num_read;
  377. }
  378. bool emv_card_emulation(FuriHalNfcTxRxContext* tx_rx) {
  379. furi_assert(tx_rx);
  380. bool emulation_complete = false;
  381. tx_rx->tx_bits = 0;
  382. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  383. do {
  384. FURI_LOG_D(TAG, "Read select PPSE command");
  385. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  386. memcpy(tx_rx->tx_data, select_ppse_ans, sizeof(select_ppse_ans));
  387. tx_rx->tx_bits = sizeof(select_ppse_ans) * 8;
  388. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  389. FURI_LOG_D(TAG, "Send select PPSE answer and read select App command");
  390. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  391. memcpy(tx_rx->tx_data, select_app_ans, sizeof(select_app_ans));
  392. tx_rx->tx_bits = sizeof(select_app_ans) * 8;
  393. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  394. FURI_LOG_D(TAG, "Send select App answer and read get PDOL command");
  395. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  396. memcpy(tx_rx->tx_data, pdol_ans, sizeof(pdol_ans));
  397. tx_rx->tx_bits = sizeof(pdol_ans) * 8;
  398. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  399. FURI_LOG_D(TAG, "Send get PDOL answer");
  400. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  401. emulation_complete = true;
  402. } while(false);
  403. return emulation_complete;
  404. }