seos_emulator.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. #include "seos_emulator_i.h"
  2. #define TAG "SeosEmulator"
  3. #define NAD_MASK 0x08
  4. static uint8_t select_header[] = {0x00, 0xa4, 0x04, 0x00};
  5. static uint8_t standard_seos_aid[] = {0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  6. static uint8_t SEOS_APPLET_FCI[] =
  7. {0x6F, 0x0C, 0x84, 0x0A, 0xA0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  8. static uint8_t FILE_NOT_FOUND[] = {0x6A, 0x82};
  9. static uint8_t success[] = {0x90, 0x00};
  10. static uint8_t select_adf_header[] = {0x80, 0xa5, 0x04, 0x00};
  11. static uint8_t general_authenticate_1[] =
  12. {0x00, 0x87, 0x00, 0x01, 0x04, 0x7c, 0x02, 0x81, 0x00, 0x00};
  13. static uint8_t general_authenticate_1_response_header[] = {0x7c, 0x0a, 0x81, 0x08};
  14. static uint8_t general_authenticate_2_header[] = {0x00, 0x87, 0x00, 0x01};
  15. static uint8_t secure_messaging_header[] = {0x0c, 0xcb, 0x3f, 0xff};
  16. static uint8_t empty[16] =
  17. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  18. SeosEmulator* seos_emulator_alloc(SeosCredential* credential) {
  19. SeosEmulator* seos_emulator = malloc(sizeof(SeosEmulator));
  20. memset(seos_emulator, 0, sizeof(SeosEmulator));
  21. // Using DES for greater compatibilty
  22. seos_emulator->params.cipher = TWO_KEY_3DES_CBC_MODE;
  23. seos_emulator->params.hash = SHA1;
  24. memset(seos_emulator->params.rndICC, 0x0d, sizeof(seos_emulator->params.rndICC));
  25. memset(seos_emulator->params.rNonce, 0x0c, sizeof(seos_emulator->params.rNonce));
  26. seos_emulator->credential = credential;
  27. seos_emulator->secure_messaging = NULL;
  28. seos_emulator->storage = furi_record_open(RECORD_STORAGE);
  29. seos_emulator->dialogs = furi_record_open(RECORD_DIALOGS);
  30. seos_emulator->load_path = furi_string_alloc();
  31. seos_emulator->tx_buffer = bit_buffer_alloc(SEOS_WORKER_MAX_BUFFER_SIZE);
  32. return seos_emulator;
  33. }
  34. void seos_emulator_free(SeosEmulator* seos_emulator) {
  35. furi_assert(seos_emulator);
  36. if(seos_emulator->secure_messaging) {
  37. secure_messaging_free(seos_emulator->secure_messaging);
  38. }
  39. furi_record_close(RECORD_STORAGE);
  40. furi_record_close(RECORD_DIALOGS);
  41. furi_string_free(seos_emulator->load_path);
  42. bit_buffer_free(seos_emulator->tx_buffer);
  43. free(seos_emulator);
  44. }
  45. void seos_emulator_set_loading_callback(
  46. SeosEmulator* seos_emulator,
  47. SeosLoadingCallback callback,
  48. void* context) {
  49. furi_assert(seos_emulator);
  50. seos_emulator->loading_cb = callback;
  51. seos_emulator->loading_cb_ctx = context;
  52. }
  53. static bool
  54. seos_emulator_file_load(SeosEmulator* seos_emulator, FuriString* path, bool show_dialog) {
  55. bool parsed = false;
  56. FlipperFormat* file = flipper_format_file_alloc(seos_emulator->storage);
  57. FuriString* temp_str;
  58. temp_str = furi_string_alloc();
  59. bool deprecated_version = false;
  60. if(seos_emulator->loading_cb) {
  61. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, true);
  62. }
  63. memset(
  64. seos_emulator->credential->diversifier, 0, sizeof(seos_emulator->credential->diversifier));
  65. memset(seos_emulator->credential->sio, 0, sizeof(seos_emulator->credential->sio));
  66. do {
  67. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  68. // Read and verify file header
  69. uint32_t version = 0;
  70. if(!flipper_format_read_header(file, temp_str, &version)) break;
  71. if(furi_string_cmp_str(temp_str, seos_file_header) || (version != seos_file_version)) {
  72. deprecated_version = true;
  73. break;
  74. }
  75. if(!flipper_format_read_uint32(
  76. file,
  77. "Diversifier Length",
  78. (uint32_t*)&(seos_emulator->credential->diversifier_len),
  79. 1))
  80. break;
  81. if(!flipper_format_read_hex(
  82. file,
  83. "Diversifier",
  84. seos_emulator->credential->diversifier,
  85. seos_emulator->credential->diversifier_len))
  86. break;
  87. if(!flipper_format_read_uint32(
  88. file, "SIO Length", (uint32_t*)&(seos_emulator->credential->sio_len), 1))
  89. break;
  90. if(!flipper_format_read_hex(
  91. file, "SIO", seos_emulator->credential->sio, seos_emulator->credential->sio_len))
  92. break;
  93. // optional
  94. memset(
  95. seos_emulator->credential->priv_key, 0, sizeof(seos_emulator->credential->priv_key));
  96. memset(
  97. seos_emulator->credential->auth_key, 0, sizeof(seos_emulator->credential->auth_key));
  98. memset(
  99. seos_emulator->credential->adf_response,
  100. 0,
  101. sizeof(seos_emulator->credential->adf_response));
  102. flipper_format_read_hex(
  103. file,
  104. "Priv Key",
  105. seos_emulator->credential->priv_key,
  106. sizeof(seos_emulator->credential->priv_key));
  107. flipper_format_read_hex(
  108. file,
  109. "Auth Key",
  110. seos_emulator->credential->auth_key,
  111. sizeof(seos_emulator->credential->auth_key));
  112. if(memcmp(seos_emulator->credential->priv_key, empty, sizeof(empty)) != 0) {
  113. FURI_LOG_I(TAG, "+ Priv Key");
  114. }
  115. if(memcmp(seos_emulator->credential->priv_key, empty, sizeof(empty)) != 0) {
  116. FURI_LOG_I(TAG, "+ Auth Key");
  117. }
  118. flipper_format_read_hex(
  119. file,
  120. "ADF Response",
  121. seos_emulator->credential->adf_response,
  122. sizeof(seos_emulator->credential->adf_response));
  123. parsed = true;
  124. } while(false);
  125. if(seos_emulator->loading_cb) {
  126. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, false);
  127. }
  128. if((!parsed) && (show_dialog)) {
  129. if(deprecated_version) {
  130. dialog_message_show_storage_error(seos_emulator->dialogs, "File format deprecated");
  131. } else {
  132. dialog_message_show_storage_error(seos_emulator->dialogs, "Can not parse\nfile");
  133. }
  134. }
  135. furi_string_free(temp_str);
  136. flipper_format_free(file);
  137. return parsed;
  138. }
  139. bool seos_emulator_file_select(SeosEmulator* seos_emulator) {
  140. furi_assert(seos_emulator);
  141. bool res = false;
  142. FuriString* seos_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  143. DialogsFileBrowserOptions browser_options;
  144. dialog_file_browser_set_basic_options(&browser_options, SEOS_APP_EXTENSION, &I_Nfc_10px);
  145. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  146. res = dialog_file_browser_show(
  147. seos_emulator->dialogs, seos_emulator->load_path, seos_app_folder, &browser_options);
  148. furi_string_free(seos_app_folder);
  149. if(res) {
  150. FuriString* filename;
  151. filename = furi_string_alloc();
  152. path_extract_filename(seos_emulator->load_path, filename, true);
  153. strncpy(seos_emulator->name, furi_string_get_cstr(filename), SEOS_FILE_NAME_MAX_LENGTH);
  154. res = seos_emulator_file_load(seos_emulator, seos_emulator->load_path, true);
  155. furi_string_free(filename);
  156. }
  157. return res;
  158. }
  159. bool seos_emulator_delete(SeosEmulator* seos_emulator, bool use_load_path) {
  160. furi_assert(seos_emulator);
  161. bool deleted = false;
  162. FuriString* file_path;
  163. file_path = furi_string_alloc();
  164. do {
  165. // Delete original file
  166. if(use_load_path && !furi_string_empty(seos_emulator->load_path)) {
  167. furi_string_set(file_path, seos_emulator->load_path);
  168. } else {
  169. furi_string_printf(
  170. file_path, APP_DATA_PATH("%s%s"), seos_emulator->name, SEOS_APP_EXTENSION);
  171. }
  172. if(!storage_simply_remove(seos_emulator->storage, furi_string_get_cstr(file_path))) break;
  173. deleted = true;
  174. } while(0);
  175. if(!deleted) {
  176. dialog_message_show_storage_error(seos_emulator->dialogs, "Can not remove file");
  177. }
  178. furi_string_free(file_path);
  179. return deleted;
  180. }
  181. void seos_emulator_select_aid(BitBuffer* tx_buffer) {
  182. FURI_LOG_D(TAG, "Select AID");
  183. bit_buffer_append_bytes(tx_buffer, SEOS_APPLET_FCI, sizeof(SEOS_APPLET_FCI));
  184. }
  185. void seos_emulator_general_authenticate_1(BitBuffer* tx_buffer, AuthParameters params) {
  186. bit_buffer_append_bytes(
  187. tx_buffer,
  188. general_authenticate_1_response_header,
  189. sizeof(general_authenticate_1_response_header));
  190. bit_buffer_append_bytes(tx_buffer, params.rndICC, sizeof(params.rndICC));
  191. }
  192. // 0a00
  193. // 00870001 2c7c 2a82 28 bbb4e9156136f27f687e2967865dfe812e33c95ddcf9294a4340d26da3e76db0220d1163c591e5b8 00
  194. bool seos_emulator_general_authenticate_2(
  195. const uint8_t* buffer,
  196. size_t buffer_len,
  197. SeosCredential* credential,
  198. AuthParameters* params,
  199. BitBuffer* tx_buffer) {
  200. FURI_LOG_D(TAG, "seos_emulator_general_authenticate_2");
  201. UNUSED(buffer_len);
  202. uint8_t* rx_data = (uint8_t*)buffer;
  203. uint8_t* cryptogram = rx_data + sizeof(general_authenticate_2_header) + 5;
  204. size_t encrypted_len = 32;
  205. uint8_t* mac = cryptogram + encrypted_len;
  206. params->key_no = rx_data[3];
  207. if(memcmp(credential->priv_key, empty, sizeof(empty)) == 0) {
  208. seos_worker_diversify_key(
  209. SEOS_ADF1_READ,
  210. credential->diversifier,
  211. credential->diversifier_len,
  212. SEOS_ADF_OID,
  213. SEOS_ADF_OID_LEN,
  214. params->cipher,
  215. params->hash,
  216. params->key_no,
  217. true,
  218. params->priv_key);
  219. } else {
  220. memcpy(params->priv_key, credential->priv_key, sizeof(params->priv_key));
  221. }
  222. if(memcmp(credential->auth_key, empty, sizeof(empty)) == 0) {
  223. seos_worker_diversify_key(
  224. SEOS_ADF1_READ,
  225. credential->diversifier,
  226. credential->diversifier_len,
  227. SEOS_ADF_OID,
  228. SEOS_ADF_OID_LEN,
  229. params->cipher,
  230. params->hash,
  231. params->key_no,
  232. false,
  233. params->auth_key);
  234. } else {
  235. memcpy(params->auth_key, credential->auth_key, sizeof(params->auth_key));
  236. }
  237. uint8_t cmac[16];
  238. if(params->cipher == AES_128_CBC) {
  239. aes_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  240. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  241. des_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  242. } else {
  243. FURI_LOG_W(TAG, "Cipher not matched");
  244. return false;
  245. }
  246. if(memcmp(cmac, mac, SEOS_WORKER_CMAC_SIZE) != 0) {
  247. FURI_LOG_W(TAG, "Incorrect cryptogram mac %02x... vs %02x...", cmac[0], mac[0]);
  248. return false;
  249. }
  250. uint8_t clear[32];
  251. if(params->cipher == AES_128_CBC) {
  252. seos_worker_aes_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  253. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  254. seos_worker_des_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  255. } else {
  256. FURI_LOG_W(TAG, "Cipher not matched");
  257. }
  258. size_t index = 0;
  259. memcpy(params->UID, clear + index, sizeof(params->UID));
  260. index += sizeof(params->UID);
  261. if(memcmp(clear + index, params->rndICC, sizeof(params->rndICC)) != 0) {
  262. FURI_LOG_W(TAG, "Incorrect rndICC returned");
  263. return false;
  264. }
  265. index += sizeof(params->rndICC);
  266. memcpy(params->cNonce, clear + index, sizeof(params->cNonce));
  267. index += sizeof(params->cNonce);
  268. // Construct response
  269. uint8_t response_header[] = {0x7c, 0x2a, 0x82, 0x28};
  270. memset(clear, 0, sizeof(clear));
  271. memset(cmac, 0, sizeof(cmac));
  272. index = 0;
  273. memcpy(clear + index, params->rndICC, sizeof(params->rndICC));
  274. index += sizeof(params->rndICC);
  275. memcpy(clear + index, params->UID, sizeof(params->UID));
  276. index += sizeof(params->UID);
  277. memcpy(clear + index, params->rNonce, sizeof(params->rNonce));
  278. index += sizeof(params->rNonce);
  279. uint8_t encrypted[32];
  280. if(params->cipher == AES_128_CBC) {
  281. seos_worker_aes_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  282. aes_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  283. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  284. seos_worker_des_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  285. des_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  286. } else {
  287. FURI_LOG_W(TAG, "Cipher not matched");
  288. }
  289. bit_buffer_append_bytes(tx_buffer, response_header, sizeof(response_header));
  290. bit_buffer_append_bytes(tx_buffer, encrypted, sizeof(encrypted));
  291. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  292. return true;
  293. }
  294. void seos_emulator_des_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  295. // Synethic IV
  296. /// random bytes
  297. uint8_t rnd[4] = {0, 0, 0, 0};
  298. uint8_t cmac[8] = {0};
  299. /// cmac
  300. des_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  301. uint8_t iv[8];
  302. memcpy(iv + 0, rnd, sizeof(rnd));
  303. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  304. // Copy IV to buffer because mbedtls_des3_crypt_cbc mutates it
  305. memcpy(buffer + 0, iv, sizeof(iv));
  306. uint8_t clear[0x30];
  307. memset(clear, 0, sizeof(clear));
  308. size_t index = 0;
  309. // OID
  310. clear[index++] = 0x06;
  311. clear[index++] = SEOS_ADF_OID_LEN, memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  312. index += SEOS_ADF_OID_LEN;
  313. // diversifier
  314. clear[index++] = 0xcf;
  315. clear[index++] = credential->diversifier_len;
  316. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  317. index += credential->diversifier_len;
  318. mbedtls_des3_context ctx;
  319. mbedtls_des3_init(&ctx);
  320. mbedtls_des3_set2key_enc(&ctx, SEOS_ADF1_PRIV_ENC);
  321. mbedtls_des3_crypt_cbc(
  322. &ctx, MBEDTLS_DES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  323. mbedtls_des3_free(&ctx);
  324. }
  325. void seos_emulator_aes_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  326. // Synethic IV
  327. /// random bytes
  328. uint8_t rnd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  329. uint8_t cmac[16] = {0};
  330. /// cmac
  331. aes_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  332. uint8_t iv[16];
  333. memcpy(iv + 0, rnd, sizeof(rnd));
  334. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  335. // Copy IV to buffer because mbedtls_aes_crypt_cbc mutates it
  336. memcpy(buffer + 0, iv, sizeof(iv));
  337. uint8_t clear[0x30];
  338. memset(clear, 0, sizeof(clear));
  339. size_t index = 0;
  340. // OID
  341. clear[index++] = 0x06;
  342. clear[index++] = SEOS_ADF_OID_LEN;
  343. memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  344. index += SEOS_ADF_OID_LEN;
  345. // diversifier
  346. clear[index++] = 0xcf;
  347. clear[index++] = credential->diversifier_len;
  348. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  349. index += credential->diversifier_len;
  350. mbedtls_aes_context ctx;
  351. mbedtls_aes_init(&ctx);
  352. mbedtls_aes_setkey_enc(&ctx, SEOS_ADF1_PRIV_ENC, sizeof(SEOS_ADF1_PRIV_ENC) * 8);
  353. mbedtls_aes_crypt_cbc(
  354. &ctx, MBEDTLS_AES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  355. mbedtls_aes_free(&ctx);
  356. }
  357. void seos_emulator_select_adf(
  358. AuthParameters* params,
  359. SeosCredential* credential,
  360. BitBuffer* tx_buffer) {
  361. FURI_LOG_D(TAG, "Select ADF");
  362. // Shortcut if the credential file contained the hardcoded response
  363. if(credential->adf_response[2] != 0x00 && credential->adf_response[2] == params->cipher) {
  364. FURI_LOG_I(TAG, "Using hardcoded ADF Response");
  365. bit_buffer_append_bytes(
  366. tx_buffer, credential->adf_response, sizeof(credential->adf_response));
  367. seos_log_bitbuffer(TAG, "Select ADF (0xcd02...)", tx_buffer);
  368. return;
  369. }
  370. size_t prefix_len = bit_buffer_get_size_bytes(tx_buffer);
  371. size_t des_cryptogram_length = 56;
  372. size_t aes_cryptogram_length = 64;
  373. uint8_t header[] = {0xcd, 0x02, params->cipher, params->hash};
  374. bit_buffer_append_bytes(tx_buffer, header, sizeof(header));
  375. // cryptogram
  376. // 06112b0601040181e438010102011801010202 cf 07 3d4c010c71cfa7 e2d0b41a00cc5e494c8d52b6e562592399fe614a
  377. uint8_t buffer[64];
  378. uint8_t cmac[16];
  379. memset(buffer, 0, sizeof(buffer));
  380. if(params->cipher == AES_128_CBC) {
  381. uint8_t cryptogram_prefix[] = {0x85, aes_cryptogram_length};
  382. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  383. seos_emulator_aes_adf_payload(credential, buffer);
  384. bit_buffer_append_bytes(tx_buffer, buffer, aes_cryptogram_length);
  385. aes_cmac(
  386. SEOS_ADF1_PRIV_MAC,
  387. sizeof(SEOS_ADF1_PRIV_MAC),
  388. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  389. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  390. cmac);
  391. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  392. uint8_t cryptogram_prefix[] = {0x85, des_cryptogram_length};
  393. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  394. seos_emulator_des_adf_payload(credential, buffer);
  395. bit_buffer_append_bytes(tx_buffer, buffer, des_cryptogram_length);
  396. // +2 / -2 is to ignore iso14a framing
  397. des_cmac(
  398. SEOS_ADF1_PRIV_MAC,
  399. sizeof(SEOS_ADF1_PRIV_MAC),
  400. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  401. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  402. cmac);
  403. }
  404. uint8_t cmac_prefix[] = {0x8e, 0x08};
  405. bit_buffer_append_bytes(tx_buffer, cmac_prefix, sizeof(cmac_prefix));
  406. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  407. seos_log_bitbuffer(TAG, "Select ADF (0xcd02...)", tx_buffer);
  408. }
  409. NfcCommand seos_worker_listener_callback(NfcGenericEvent event, void* context) {
  410. furi_assert(context);
  411. furi_assert(event.protocol == NfcProtocolIso14443_4a);
  412. furi_assert(event.event_data);
  413. Seos* seos = context;
  414. SeosEmulator* seos_emulator = seos->seos_emulator;
  415. NfcCommand ret = NfcCommandContinue;
  416. Iso14443_4aListenerEvent* iso14443_4a_event = event.event_data;
  417. Iso14443_3aListener* iso14443_listener = event.instance;
  418. seos_emulator->iso14443_listener = iso14443_listener;
  419. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  420. bit_buffer_reset(tx_buffer);
  421. switch(iso14443_4a_event->type) {
  422. case Iso14443_4aListenerEventTypeReceivedData:
  423. seos_emulator->rx_buffer = iso14443_4a_event->data->buffer;
  424. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  425. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  426. uint8_t offset = NAD ? 2 : 1;
  427. if(bit_buffer_get_size_bytes(iso14443_4a_event->data->buffer) == offset) {
  428. FURI_LOG_I(TAG, "No contents in frame");
  429. break;
  430. }
  431. // + x to skip stuff before APDU
  432. const uint8_t* apdu = rx_data + offset;
  433. seos_log_bitbuffer(TAG, "NFC received", seos_emulator->rx_buffer);
  434. // Some ISO14443a framing I need to figure out
  435. bit_buffer_append_bytes(tx_buffer, rx_data, offset);
  436. if(memcmp(apdu, select_header, sizeof(select_header)) == 0) {
  437. if(memcmp(
  438. apdu + sizeof(select_header) + 1,
  439. standard_seos_aid,
  440. sizeof(standard_seos_aid)) == 0) {
  441. seos_emulator_select_aid(seos_emulator->tx_buffer);
  442. view_dispatcher_send_custom_event(
  443. seos->view_dispatcher, SeosCustomEventAIDSelected);
  444. } else {
  445. bit_buffer_append_bytes(
  446. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  447. }
  448. } else if(memcmp(apdu, select_adf_header, sizeof(select_adf_header)) == 0) {
  449. // is our adf in the list?
  450. // +1 to skip APDU length byte
  451. void* p = memmem(
  452. apdu + sizeof(select_adf_header) + 1,
  453. apdu[sizeof(select_adf_header)],
  454. SEOS_ADF_OID,
  455. SEOS_ADF_OID_LEN);
  456. if(p) {
  457. BitBuffer* tmp = bit_buffer_alloc(SEOS_ADF_OID_LEN);
  458. bit_buffer_append_bytes(tmp, p, SEOS_ADF_OID_LEN);
  459. seos_log_bitbuffer(TAG, "Matched ADF", tmp);
  460. bit_buffer_free(tmp);
  461. view_dispatcher_send_custom_event(
  462. seos->view_dispatcher, SeosCustomEventADFMatched);
  463. seos_emulator_select_adf(
  464. &seos_emulator->params, seos_emulator->credential, seos_emulator->tx_buffer);
  465. } else {
  466. FURI_LOG_W(TAG, "Failed to match any ADF OID");
  467. }
  468. } else if(memcmp(apdu, general_authenticate_1, sizeof(general_authenticate_1)) == 0) {
  469. seos_emulator_general_authenticate_1(seos_emulator->tx_buffer, seos_emulator->params);
  470. } else if(
  471. memcmp(apdu, general_authenticate_2_header, sizeof(general_authenticate_2_header)) ==
  472. 0) {
  473. if(!seos_emulator_general_authenticate_2(
  474. apdu,
  475. bit_buffer_get_size_bytes(seos_emulator->rx_buffer),
  476. seos_emulator->credential,
  477. &seos_emulator->params,
  478. seos_emulator->tx_buffer)) {
  479. FURI_LOG_W(TAG, "Failure in General Authenticate 2");
  480. ret = NfcCommandStop;
  481. return ret;
  482. }
  483. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAuthenticated);
  484. // Prepare for future communication
  485. seos_emulator->secure_messaging = secure_messaging_alloc(&seos_emulator->params);
  486. } else if(memcmp(apdu, secure_messaging_header, sizeof(secure_messaging_header)) == 0) {
  487. uint8_t request_sio[] = {0x5c, 0x02, 0xff, 0x00};
  488. if(seos_emulator->secure_messaging) {
  489. FURI_LOG_D(TAG, "Unwrap secure message");
  490. // 0b00 0ccb3fff 16 8508fa8395d30de4e8e097008e085da7edbd833b002d00
  491. // Ignore 2 iso frame bytes
  492. size_t bytes_to_ignore = offset;
  493. BitBuffer* tmp =
  494. bit_buffer_alloc(bit_buffer_get_size_bytes(seos_emulator->rx_buffer));
  495. bit_buffer_append_bytes(
  496. tmp,
  497. bit_buffer_get_data(seos_emulator->rx_buffer) + bytes_to_ignore,
  498. bit_buffer_get_size_bytes(seos_emulator->rx_buffer) - bytes_to_ignore);
  499. seos_log_bitbuffer(TAG, "NFC received(wrapped)", tmp);
  500. secure_messaging_unwrap_apdu(seos_emulator->secure_messaging, tmp);
  501. seos_log_bitbuffer(TAG, "NFC received(clear)", tmp);
  502. const uint8_t* message = bit_buffer_get_data(tmp);
  503. if(memcmp(message, request_sio, sizeof(request_sio)) == 0) {
  504. view_dispatcher_send_custom_event(
  505. seos->view_dispatcher, SeosCustomEventSIORequested);
  506. BitBuffer* sio_file = bit_buffer_alloc(128);
  507. bit_buffer_append_bytes(sio_file, message + 2, 2); // fileId
  508. bit_buffer_append_byte(sio_file, seos_emulator->credential->sio_len);
  509. bit_buffer_append_bytes(
  510. sio_file,
  511. seos_emulator->credential->sio,
  512. seos_emulator->credential->sio_len);
  513. secure_messaging_wrap_rapdu(
  514. seos_emulator->secure_messaging,
  515. (uint8_t*)bit_buffer_get_data(sio_file),
  516. bit_buffer_get_size_bytes(sio_file),
  517. tx_buffer);
  518. bit_buffer_free(sio_file);
  519. }
  520. bit_buffer_free(tmp);
  521. } else {
  522. uint8_t no_sm[] = {0x69, 0x88};
  523. bit_buffer_append_bytes(tx_buffer, no_sm, sizeof(no_sm));
  524. }
  525. } else {
  526. // I'm trying to find a good place to re-assert that we're emulating so we don't get stuck on a previous UI screen when we emulate repeatedly
  527. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventEmulate);
  528. }
  529. if(bit_buffer_get_size_bytes(seos_emulator->tx_buffer) >
  530. offset) { // contents belong iso framing
  531. bit_buffer_append_bytes(tx_buffer, success, sizeof(success));
  532. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  533. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  534. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  535. if(error != NfcErrorNone) {
  536. FURI_LOG_W(TAG, "Tx error: %d", error);
  537. break;
  538. }
  539. } else {
  540. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  541. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  542. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  543. if(error != NfcErrorNone) {
  544. FURI_LOG_W(TAG, "Tx error: %d", error);
  545. break;
  546. }
  547. }
  548. break;
  549. case Iso14443_4aListenerEventTypeHalted:
  550. FURI_LOG_I(TAG, "Halted");
  551. break;
  552. case Iso14443_4aListenerEventTypeFieldOff:
  553. FURI_LOG_I(TAG, "Field Off");
  554. break;
  555. }
  556. return ret;
  557. }