emv.c 15 KB

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