emv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "emv.h"
  2. #include <furi/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_CARD_NUM: // Track 2 Equivalent Data. 0xD0 delimits PAN from expiry (YYMM)
  138. for(int x = 1; x < tlen; x++) {
  139. if(buff[i + x + 1] > 0xD0) {
  140. memcpy(app->card_number, &buff[i], x + 1);
  141. app->card_number_len = x + 1;
  142. break;
  143. }
  144. }
  145. success = true;
  146. FURI_LOG_T(
  147. TAG,
  148. "found EMV_TAG_CARD_NUM %x (len=%d)",
  149. EMV_TAG_CARD_NUM,
  150. app->card_number_len);
  151. break;
  152. case EMV_TAG_PAN:
  153. memcpy(app->card_number, &buff[i], tlen);
  154. app->card_number_len = tlen;
  155. success = true;
  156. break;
  157. case EMV_TAG_EXP_DATE:
  158. app->exp_year = buff[i];
  159. app->exp_month = buff[i + 1];
  160. success = true;
  161. break;
  162. case EMV_TAG_CURRENCY_CODE:
  163. app->currency_code = (buff[i] << 8 | buff[i + 1]);
  164. success = true;
  165. break;
  166. case EMV_TAG_COUNTRY_CODE:
  167. app->country_code = (buff[i] << 8 | buff[i + 1]);
  168. success = true;
  169. break;
  170. }
  171. }
  172. i += tlen;
  173. }
  174. return success;
  175. }
  176. bool emv_select_ppse(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  177. bool app_aid_found = false;
  178. const uint8_t emv_select_ppse_cmd[] = {
  179. 0x00, 0xA4, // SELECT ppse
  180. 0x04, 0x00, // P1:By name, P2: empty
  181. 0x0e, // Lc: Data length
  182. 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
  183. 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
  184. 0x00 // Le
  185. };
  186. memcpy(tx_rx->tx_data, emv_select_ppse_cmd, sizeof(emv_select_ppse_cmd));
  187. tx_rx->tx_bits = sizeof(emv_select_ppse_cmd) * 8;
  188. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  189. FURI_LOG_D(TAG, "Send select PPSE");
  190. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  191. emv_trace(tx_rx, "Select PPSE answer:");
  192. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  193. app_aid_found = true;
  194. } else {
  195. FURI_LOG_E(TAG, "Failed to parse application");
  196. }
  197. } else {
  198. FURI_LOG_E(TAG, "Failed select PPSE");
  199. }
  200. return app_aid_found;
  201. }
  202. bool emv_select_app(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  203. bool select_app_success = false;
  204. const uint8_t emv_select_header[] = {
  205. 0x00,
  206. 0xA4, // SELECT application
  207. 0x04,
  208. 0x00 // P1:By name, P2:First or only occurence
  209. };
  210. uint16_t size = sizeof(emv_select_header);
  211. // Copy header
  212. memcpy(tx_rx->tx_data, emv_select_header, size);
  213. // Copy AID
  214. tx_rx->tx_data[size++] = app->aid_len;
  215. memcpy(&tx_rx->tx_data[size], app->aid, app->aid_len);
  216. size += app->aid_len;
  217. tx_rx->tx_data[size++] = 0x00;
  218. tx_rx->tx_bits = size * 8;
  219. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  220. FURI_LOG_D(TAG, "Start application");
  221. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  222. emv_trace(tx_rx, "Start application answer:");
  223. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  224. select_app_success = true;
  225. } else {
  226. FURI_LOG_E(TAG, "Failed to read PAN or PDOL");
  227. }
  228. } else {
  229. FURI_LOG_E(TAG, "Failed to start application");
  230. }
  231. return select_app_success;
  232. }
  233. static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
  234. bool tag_found;
  235. for(uint16_t i = 0; i < src->size; i++) {
  236. tag_found = false;
  237. for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
  238. if(src->data[i] == pdol_values[j]->tag) {
  239. // Found tag with 1 byte length
  240. uint8_t len = src->data[++i];
  241. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  242. dest->size += len;
  243. tag_found = true;
  244. break;
  245. } else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
  246. // Found tag with 2 byte length
  247. i += 2;
  248. uint8_t len = src->data[i];
  249. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  250. dest->size += len;
  251. tag_found = true;
  252. break;
  253. }
  254. }
  255. if(!tag_found) {
  256. // Unknown tag, fill zeros
  257. i += 2;
  258. uint8_t len = src->data[i];
  259. memset(dest->data + dest->size, 0, len);
  260. dest->size += len;
  261. }
  262. }
  263. return dest->size;
  264. }
  265. static bool emv_get_processing_options(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  266. bool card_num_read = false;
  267. const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
  268. uint16_t size = sizeof(emv_gpo_header);
  269. // Copy header
  270. memcpy(tx_rx->tx_data, emv_gpo_header, size);
  271. APDU pdol_data = {0, {0}};
  272. // Prepare and copy pdol parameters
  273. emv_prepare_pdol(&pdol_data, &app->pdol);
  274. tx_rx->tx_data[size++] = 0x02 + pdol_data.size;
  275. tx_rx->tx_data[size++] = 0x83;
  276. tx_rx->tx_data[size++] = pdol_data.size;
  277. memcpy(tx_rx->tx_data + size, pdol_data.data, pdol_data.size);
  278. size += pdol_data.size;
  279. tx_rx->tx_data[size++] = 0;
  280. tx_rx->tx_bits = size * 8;
  281. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  282. FURI_LOG_D(TAG, "Get proccessing options");
  283. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  284. emv_trace(tx_rx, "Get processing options answer:");
  285. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  286. if(app->card_number_len > 0) {
  287. card_num_read = true;
  288. }
  289. }
  290. } else {
  291. FURI_LOG_E(TAG, "Failed to get processing options");
  292. }
  293. return card_num_read;
  294. }
  295. static bool emv_read_sfi_record(
  296. FuriHalNfcTxRxContext* tx_rx,
  297. EmvApplication* app,
  298. uint8_t sfi,
  299. uint8_t record_num) {
  300. bool card_num_read = false;
  301. uint8_t sfi_param = (sfi << 3) | (1 << 2);
  302. uint8_t emv_sfi_header[] = {
  303. 0x00,
  304. 0xB2, // READ RECORD
  305. record_num, // P1:record_number
  306. sfi_param, // P2:SFI
  307. 0x00 // Le
  308. };
  309. memcpy(tx_rx->tx_data, emv_sfi_header, sizeof(emv_sfi_header));
  310. tx_rx->tx_bits = sizeof(emv_sfi_header) * 8;
  311. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  312. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  313. emv_trace(tx_rx, "SFI record:");
  314. if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  315. card_num_read = true;
  316. }
  317. } else {
  318. FURI_LOG_E(TAG, "Failed to read SFI record %d", record_num);
  319. }
  320. return card_num_read;
  321. }
  322. static bool emv_read_files(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  323. bool card_num_read = false;
  324. if(app->afl.size == 0) {
  325. return false;
  326. }
  327. FURI_LOG_D(TAG, "Search PAN in SFI");
  328. // Iterate through all files
  329. for(size_t i = 0; i < app->afl.size; i += 4) {
  330. uint8_t sfi = app->afl.data[i] >> 3;
  331. uint8_t record_start = app->afl.data[i + 1];
  332. uint8_t record_end = app->afl.data[i + 2];
  333. // Iterate through all records in file
  334. for(uint8_t record = record_start; record <= record_end; ++record) {
  335. card_num_read |= emv_read_sfi_record(tx_rx, app, sfi, record);
  336. }
  337. }
  338. return card_num_read;
  339. }
  340. bool emv_search_application(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
  341. furi_assert(tx_rx);
  342. furi_assert(emv_app);
  343. memset(emv_app, 0, sizeof(EmvApplication));
  344. return emv_select_ppse(tx_rx, emv_app);
  345. }
  346. bool emv_read_bank_card(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
  347. furi_assert(tx_rx);
  348. furi_assert(emv_app);
  349. bool card_num_read = false;
  350. memset(emv_app, 0, sizeof(EmvApplication));
  351. do {
  352. if(!emv_select_ppse(tx_rx, emv_app)) break;
  353. if(!emv_select_app(tx_rx, emv_app)) break;
  354. if(emv_get_processing_options(tx_rx, emv_app)) {
  355. card_num_read = true;
  356. } else {
  357. card_num_read = emv_read_files(tx_rx, emv_app);
  358. }
  359. } while(false);
  360. return card_num_read;
  361. }
  362. bool emv_card_emulation(FuriHalNfcTxRxContext* tx_rx) {
  363. furi_assert(tx_rx);
  364. bool emulation_complete = false;
  365. tx_rx->tx_bits = 0;
  366. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  367. do {
  368. FURI_LOG_D(TAG, "Read select PPSE command");
  369. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  370. memcpy(tx_rx->tx_data, select_ppse_ans, sizeof(select_ppse_ans));
  371. tx_rx->tx_bits = sizeof(select_ppse_ans) * 8;
  372. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  373. FURI_LOG_D(TAG, "Send select PPSE answer and read select App command");
  374. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  375. memcpy(tx_rx->tx_data, select_app_ans, sizeof(select_app_ans));
  376. tx_rx->tx_bits = sizeof(select_app_ans) * 8;
  377. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  378. FURI_LOG_D(TAG, "Send select App answer and read get PDOL command");
  379. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  380. memcpy(tx_rx->tx_data, pdol_ans, sizeof(pdol_ans));
  381. tx_rx->tx_bits = sizeof(pdol_ans) * 8;
  382. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  383. FURI_LOG_D(TAG, "Send get PDOL answer");
  384. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  385. emulation_complete = true;
  386. } while(false);
  387. return emulation_complete;
  388. }