emv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 uint16_t emv_parse_TLV(uint8_t* dest, uint8_t* src, uint16_t* idx) {
  56. uint8_t len = src[*idx + 1];
  57. memcpy(dest, &src[*idx + 2], len);
  58. *idx = *idx + len + 1;
  59. return len;
  60. }
  61. static bool emv_decode_search_tag_u16_r(uint16_t tag, uint8_t* buff, uint16_t* idx) {
  62. if((buff[*idx] << 8 | buff[*idx + 1]) == tag) {
  63. *idx = *idx + 3;
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool emv_decode_ppse_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  69. uint16_t i = 0;
  70. bool app_aid_found = false;
  71. while(i < len) {
  72. if(buff[i] == EMV_TAG_APP_TEMPLATE) {
  73. uint8_t app_len = buff[++i];
  74. for(uint16_t j = i; j < MIN(i + app_len, len - 1); j++) {
  75. if(buff[j] == EMV_TAG_AID) {
  76. app_aid_found = true;
  77. app->aid_len = buff[j + 1];
  78. emv_parse_TLV(app->aid, buff, &j);
  79. } else if(buff[j] == EMV_TAG_PRIORITY) {
  80. emv_parse_TLV(&app->priority, buff, &j);
  81. }
  82. }
  83. i += app_len;
  84. }
  85. i++;
  86. }
  87. return app_aid_found;
  88. }
  89. bool emv_select_ppse(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  90. bool app_aid_found = false;
  91. const uint8_t emv_select_ppse_cmd[] = {
  92. 0x00, 0xA4, // SELECT ppse
  93. 0x04, 0x00, // P1:By name, P2: empty
  94. 0x0e, // Lc: Data length
  95. 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
  96. 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
  97. 0x00 // Le
  98. };
  99. memcpy(tx_rx->tx_data, emv_select_ppse_cmd, sizeof(emv_select_ppse_cmd));
  100. tx_rx->tx_bits = sizeof(emv_select_ppse_cmd) * 8;
  101. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  102. FURI_LOG_D(TAG, "Send select PPSE");
  103. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  104. if(emv_decode_ppse_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  105. app_aid_found = true;
  106. } else {
  107. FURI_LOG_E(TAG, "Failed to parse application");
  108. }
  109. } else {
  110. FURI_LOG_E(TAG, "Failed select PPSE");
  111. }
  112. return app_aid_found;
  113. }
  114. static bool emv_decode_select_app_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  115. uint16_t i = 0;
  116. bool decode_success = false;
  117. while(i < len) {
  118. if(buff[i] == EMV_TAG_CARD_NAME) {
  119. uint8_t name_len = buff[i + 1];
  120. emv_parse_TLV((uint8_t*)app->name, buff, &i);
  121. app->name[name_len] = '\0';
  122. app->name_found = true;
  123. decode_success = true;
  124. } else if(((buff[i] << 8) | buff[i + 1]) == EMV_TAG_PDOL) {
  125. i++;
  126. app->pdol.size = emv_parse_TLV(app->pdol.data, buff, &i);
  127. decode_success = true;
  128. }
  129. i++;
  130. }
  131. return decode_success;
  132. }
  133. bool emv_select_app(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  134. bool select_app_success = false;
  135. const uint8_t emv_select_header[] = {
  136. 0x00,
  137. 0xA4, // SELECT application
  138. 0x04,
  139. 0x00 // P1:By name, P2:First or only occurence
  140. };
  141. uint16_t size = sizeof(emv_select_header);
  142. // Copy header
  143. memcpy(tx_rx->tx_data, emv_select_header, size);
  144. // Copy AID
  145. tx_rx->tx_data[size++] = app->aid_len;
  146. memcpy(&tx_rx->tx_data[size], app->aid, app->aid_len);
  147. size += app->aid_len;
  148. tx_rx->tx_data[size++] = 0x00;
  149. tx_rx->tx_bits = size * 8;
  150. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  151. FURI_LOG_D(TAG, "Start application");
  152. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  153. if(emv_decode_select_app_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  154. select_app_success = true;
  155. } else {
  156. FURI_LOG_E(TAG, "Failed to read PAN or PDOL");
  157. }
  158. } else {
  159. FURI_LOG_E(TAG, "Failed to start application");
  160. }
  161. return select_app_success;
  162. }
  163. static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
  164. bool tag_found;
  165. for(uint16_t i = 0; i < src->size; i++) {
  166. tag_found = false;
  167. for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
  168. if(src->data[i] == pdol_values[j]->tag) {
  169. // Found tag with 1 byte length
  170. uint8_t len = src->data[++i];
  171. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  172. dest->size += len;
  173. tag_found = true;
  174. break;
  175. } else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
  176. // Found tag with 2 byte length
  177. i += 2;
  178. uint8_t len = src->data[i];
  179. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  180. dest->size += len;
  181. tag_found = true;
  182. break;
  183. }
  184. }
  185. if(!tag_found) {
  186. // Unknown tag, fill zeros
  187. i += 2;
  188. uint8_t len = src->data[i];
  189. memset(dest->data + dest->size, 0, len);
  190. dest->size += len;
  191. }
  192. }
  193. return dest->size;
  194. }
  195. static bool emv_decode_get_proc_opt(uint8_t* buff, uint16_t len, EmvApplication* app) {
  196. bool card_num_read = false;
  197. for(uint16_t i = 0; i < len; i++) {
  198. if(buff[i] == EMV_TAG_CARD_NUM) {
  199. app->card_number_len = 8;
  200. memcpy(app->card_number, &buff[i + 2], app->card_number_len);
  201. card_num_read = true;
  202. } else if(buff[i] == EMV_TAG_AFL) {
  203. app->afl.size = emv_parse_TLV(app->afl.data, buff, &i);
  204. }
  205. }
  206. return card_num_read;
  207. }
  208. static bool emv_get_processing_options(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  209. bool card_num_read = false;
  210. const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
  211. uint16_t size = sizeof(emv_gpo_header);
  212. // Copy header
  213. memcpy(tx_rx->tx_data, emv_gpo_header, size);
  214. APDU pdol_data = {0, {0}};
  215. // Prepare and copy pdol parameters
  216. emv_prepare_pdol(&pdol_data, &app->pdol);
  217. tx_rx->tx_data[size++] = 0x02 + pdol_data.size;
  218. tx_rx->tx_data[size++] = 0x83;
  219. tx_rx->tx_data[size++] = pdol_data.size;
  220. memcpy(tx_rx->tx_data + size, pdol_data.data, pdol_data.size);
  221. size += pdol_data.size;
  222. tx_rx->tx_data[size++] = 0;
  223. tx_rx->tx_bits = size * 8;
  224. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  225. FURI_LOG_D(TAG, "Get proccessing options");
  226. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  227. if(emv_decode_get_proc_opt(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  228. card_num_read = true;
  229. }
  230. } else {
  231. FURI_LOG_E(TAG, "Failed to get processing options");
  232. }
  233. return card_num_read;
  234. }
  235. static bool emv_decode_read_sfi_record(uint8_t* buff, uint16_t len, EmvApplication* app) {
  236. bool pan_parsed = false;
  237. for(uint16_t i = 0; i < len; i++) {
  238. if(buff[i] == EMV_TAG_PAN) {
  239. if(buff[i + 1] == 8 || buff[i + 1] == 10) {
  240. app->card_number_len = buff[i + 1];
  241. memcpy(app->card_number, &buff[i + 2], app->card_number_len);
  242. pan_parsed = true;
  243. }
  244. } else if(emv_decode_search_tag_u16_r(EMV_TAG_EXP_DATE, buff, &i)) {
  245. app->exp_year = buff[i++];
  246. app->exp_month = buff[i++];
  247. } else if(emv_decode_search_tag_u16_r(EMV_TAG_CURRENCY_CODE, buff, &i)) {
  248. app->currency_code = (buff[i] << 8) | buff[i + 1];
  249. i += 2;
  250. } else if(emv_decode_search_tag_u16_r(EMV_TAG_COUNTRY_CODE, buff, &i)) {
  251. app->country_code = (buff[i] << 8) | buff[i + 1];
  252. i += 2;
  253. }
  254. }
  255. return pan_parsed;
  256. }
  257. static bool emv_read_sfi_record(
  258. FuriHalNfcTxRxContext* tx_rx,
  259. EmvApplication* app,
  260. uint8_t sfi,
  261. uint8_t record_num) {
  262. bool card_num_read = false;
  263. uint8_t sfi_param = (sfi << 3) | (1 << 2);
  264. uint8_t emv_sfi_header[] = {
  265. 0x00,
  266. 0xB2, // READ RECORD
  267. record_num, // P1:record_number
  268. sfi_param, // P2:SFI
  269. 0x00 // Le
  270. };
  271. memcpy(tx_rx->tx_data, emv_sfi_header, sizeof(emv_sfi_header));
  272. tx_rx->tx_bits = sizeof(emv_sfi_header) * 8;
  273. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  274. if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
  275. if(emv_decode_read_sfi_record(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
  276. card_num_read = true;
  277. }
  278. } else {
  279. FURI_LOG_E(TAG, "Failed to read SFI record %d", record_num);
  280. }
  281. return card_num_read;
  282. }
  283. static bool emv_read_files(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
  284. bool card_num_read = false;
  285. if(app->afl.size == 0) {
  286. return false;
  287. }
  288. FURI_LOG_D(TAG, "Search PAN in SFI");
  289. // Iterate through all files
  290. for(size_t i = 0; i < app->afl.size; i += 4) {
  291. uint8_t sfi = app->afl.data[i] >> 3;
  292. uint8_t record_start = app->afl.data[i + 1];
  293. uint8_t record_end = app->afl.data[i + 2];
  294. // Iterate through all records in file
  295. for(uint8_t record = record_start; record <= record_end; ++record) {
  296. card_num_read |= emv_read_sfi_record(tx_rx, app, sfi, record);
  297. }
  298. }
  299. return card_num_read;
  300. }
  301. bool emv_search_application(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
  302. furi_assert(tx_rx);
  303. furi_assert(emv_app);
  304. memset(emv_app, 0, sizeof(EmvApplication));
  305. return emv_select_ppse(tx_rx, emv_app);
  306. }
  307. bool emv_read_bank_card(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
  308. furi_assert(tx_rx);
  309. furi_assert(emv_app);
  310. bool card_num_read = false;
  311. memset(emv_app, 0, sizeof(EmvApplication));
  312. do {
  313. if(!emv_select_ppse(tx_rx, emv_app)) break;
  314. if(!emv_select_app(tx_rx, emv_app)) break;
  315. if(emv_get_processing_options(tx_rx, emv_app)) {
  316. card_num_read = true;
  317. } else {
  318. card_num_read = emv_read_files(tx_rx, emv_app);
  319. }
  320. } while(false);
  321. return card_num_read;
  322. }
  323. bool emv_card_emulation(FuriHalNfcTxRxContext* tx_rx) {
  324. furi_assert(tx_rx);
  325. bool emulation_complete = false;
  326. memset(tx_rx, 0, sizeof(FuriHalNfcTxRxContext));
  327. do {
  328. FURI_LOG_D(TAG, "Read select PPSE command");
  329. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  330. memcpy(tx_rx->tx_data, select_ppse_ans, sizeof(select_ppse_ans));
  331. tx_rx->tx_bits = sizeof(select_ppse_ans) * 8;
  332. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  333. FURI_LOG_D(TAG, "Send select PPSE answer and read select App command");
  334. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  335. memcpy(tx_rx->tx_data, select_app_ans, sizeof(select_app_ans));
  336. tx_rx->tx_bits = sizeof(select_app_ans) * 8;
  337. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  338. FURI_LOG_D(TAG, "Send select App answer and read get PDOL command");
  339. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  340. memcpy(tx_rx->tx_data, pdol_ans, sizeof(pdol_ans));
  341. tx_rx->tx_bits = sizeof(pdol_ans) * 8;
  342. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  343. FURI_LOG_D(TAG, "Send get PDOL answer");
  344. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  345. emulation_complete = true;
  346. } while(false);
  347. return emulation_complete;
  348. }