seos_emulator.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 MOBILE_SEOS_ADMIN_CARD[] =
  7. {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x2d, 0x00, 0x01, 0x01};
  8. static uint8_t OPERATION_SELECTOR[] = {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x2f, 0x00, 0x01, 0x01};
  9. static uint8_t OPERATION_SELECTOR_POST_RESET[] =
  10. {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x31, 0x00, 0x01, 0x01};
  11. static uint8_t SEOS_APPLET_FCI[] =
  12. {0x6F, 0x0C, 0x84, 0x0A, 0xA0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  13. static uint8_t FILE_NOT_FOUND[] = {0x6A, 0x82};
  14. static uint8_t success[] = {0x90, 0x00};
  15. static uint8_t select_adf_header[] = {0x80, 0xa5, 0x04, 0x00};
  16. static uint8_t general_authenticate_1[] =
  17. {0x00, 0x87, 0x00, 0x01, 0x04, 0x7c, 0x02, 0x81, 0x00, 0x00};
  18. static uint8_t general_authenticate_1_response_header[] = {0x7c, 0x0a, 0x81, 0x08};
  19. static uint8_t general_authenticate_2_header[] = {0x00, 0x87, 0x00, 0x01};
  20. static uint8_t secure_messaging_header[] = {0x0c, 0xcb, 0x3f, 0xff};
  21. SeosEmulator* seos_emulator_alloc(SeosCredential* credential) {
  22. SeosEmulator* seos_emulator = malloc(sizeof(SeosEmulator));
  23. memset(seos_emulator, 0, sizeof(SeosEmulator));
  24. // Using DES for greater compatibilty
  25. seos_emulator->params.cipher = TWO_KEY_3DES_CBC_MODE;
  26. seos_emulator->params.hash = SHA1;
  27. memset(seos_emulator->params.rndICC, 0x0d, sizeof(seos_emulator->params.rndICC));
  28. memset(seos_emulator->params.rNonce, 0x0c, sizeof(seos_emulator->params.rNonce));
  29. seos_emulator->credential = credential;
  30. seos_emulator->secure_messaging = NULL;
  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. bit_buffer_free(seos_emulator->tx_buffer);
  40. free(seos_emulator);
  41. }
  42. void seos_emulator_select_aid(BitBuffer* tx_buffer) {
  43. FURI_LOG_D(TAG, "Select AID");
  44. bit_buffer_append_bytes(tx_buffer, SEOS_APPLET_FCI, sizeof(SEOS_APPLET_FCI));
  45. }
  46. void seos_emulator_general_authenticate_1(BitBuffer* tx_buffer, AuthParameters params) {
  47. bit_buffer_append_bytes(
  48. tx_buffer,
  49. general_authenticate_1_response_header,
  50. sizeof(general_authenticate_1_response_header));
  51. bit_buffer_append_bytes(tx_buffer, params.rndICC, sizeof(params.rndICC));
  52. }
  53. // 0a00
  54. // 00870001 2c7c 2a82 28 bbb4e9156136f27f687e2967865dfe812e33c95ddcf9294a4340d26da3e76db0220d1163c591e5b8 00
  55. bool seos_emulator_general_authenticate_2(
  56. const uint8_t* buffer,
  57. size_t buffer_len,
  58. SeosCredential* credential,
  59. AuthParameters* params,
  60. BitBuffer* tx_buffer) {
  61. FURI_LOG_D(TAG, "seos_emulator_general_authenticate_2");
  62. UNUSED(buffer_len);
  63. uint8_t* rx_data = (uint8_t*)buffer;
  64. uint8_t* cryptogram = rx_data + sizeof(general_authenticate_2_header) + 5;
  65. size_t encrypted_len = 32;
  66. uint8_t* mac = cryptogram + encrypted_len;
  67. params->key_no = rx_data[3];
  68. if(credential->use_hardcoded) {
  69. memcpy(params->priv_key, credential->priv_key, sizeof(params->priv_key));
  70. memcpy(params->auth_key, credential->auth_key, sizeof(params->auth_key));
  71. } else {
  72. seos_worker_diversify_key(
  73. SEOS_ADF1_READ,
  74. credential->diversifier,
  75. credential->diversifier_len,
  76. SEOS_ADF_OID,
  77. SEOS_ADF_OID_LEN,
  78. params->cipher,
  79. params->hash,
  80. params->key_no,
  81. true,
  82. params->priv_key);
  83. seos_worker_diversify_key(
  84. SEOS_ADF1_READ,
  85. credential->diversifier,
  86. credential->diversifier_len,
  87. SEOS_ADF_OID,
  88. SEOS_ADF_OID_LEN,
  89. params->cipher,
  90. params->hash,
  91. params->key_no,
  92. false,
  93. params->auth_key);
  94. }
  95. uint8_t cmac[16];
  96. if(params->cipher == AES_128_CBC) {
  97. aes_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  98. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  99. des_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  100. } else {
  101. FURI_LOG_W(TAG, "Cipher not matched");
  102. return false;
  103. }
  104. if(memcmp(cmac, mac, SEOS_WORKER_CMAC_SIZE) != 0) {
  105. FURI_LOG_W(TAG, "Incorrect cryptogram mac %02x... vs %02x...", cmac[0], mac[0]);
  106. return false;
  107. }
  108. uint8_t clear[32];
  109. if(params->cipher == AES_128_CBC) {
  110. seos_worker_aes_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  111. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  112. seos_worker_des_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  113. } else {
  114. FURI_LOG_W(TAG, "Cipher not matched");
  115. }
  116. size_t index = 0;
  117. memcpy(params->UID, clear + index, sizeof(params->UID));
  118. index += sizeof(params->UID);
  119. if(memcmp(clear + index, params->rndICC, sizeof(params->rndICC)) != 0) {
  120. FURI_LOG_W(TAG, "Incorrect rndICC returned");
  121. return false;
  122. }
  123. index += sizeof(params->rndICC);
  124. memcpy(params->cNonce, clear + index, sizeof(params->cNonce));
  125. index += sizeof(params->cNonce);
  126. // Construct response
  127. uint8_t response_header[] = {0x7c, 0x2a, 0x82, 0x28};
  128. memset(clear, 0, sizeof(clear));
  129. memset(cmac, 0, sizeof(cmac));
  130. index = 0;
  131. memcpy(clear + index, params->rndICC, sizeof(params->rndICC));
  132. index += sizeof(params->rndICC);
  133. memcpy(clear + index, params->UID, sizeof(params->UID));
  134. index += sizeof(params->UID);
  135. memcpy(clear + index, params->rNonce, sizeof(params->rNonce));
  136. index += sizeof(params->rNonce);
  137. uint8_t encrypted[32];
  138. if(params->cipher == AES_128_CBC) {
  139. seos_worker_aes_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  140. aes_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  141. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  142. seos_worker_des_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  143. des_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  144. } else {
  145. FURI_LOG_W(TAG, "Cipher not matched");
  146. }
  147. bit_buffer_append_bytes(tx_buffer, response_header, sizeof(response_header));
  148. bit_buffer_append_bytes(tx_buffer, encrypted, sizeof(encrypted));
  149. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  150. return true;
  151. }
  152. void seos_emulator_des_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  153. // Synethic IV
  154. /// random bytes
  155. uint8_t rnd[4] = {0, 0, 0, 0};
  156. uint8_t cmac[8] = {0};
  157. /// cmac
  158. des_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  159. uint8_t iv[8];
  160. memcpy(iv + 0, rnd, sizeof(rnd));
  161. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  162. // Copy IV to buffer because mbedtls_des3_crypt_cbc mutates it
  163. memcpy(buffer + 0, iv, sizeof(iv));
  164. uint8_t clear[0x30];
  165. memset(clear, 0, sizeof(clear));
  166. size_t index = 0;
  167. // OID
  168. clear[index++] = 0x06;
  169. clear[index++] = SEOS_ADF_OID_LEN, memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  170. index += SEOS_ADF_OID_LEN;
  171. // diversifier
  172. clear[index++] = 0xcf;
  173. clear[index++] = credential->diversifier_len;
  174. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  175. index += credential->diversifier_len;
  176. mbedtls_des3_context ctx;
  177. mbedtls_des3_init(&ctx);
  178. mbedtls_des3_set2key_enc(&ctx, SEOS_ADF1_PRIV_ENC);
  179. mbedtls_des3_crypt_cbc(
  180. &ctx, MBEDTLS_DES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  181. mbedtls_des3_free(&ctx);
  182. }
  183. void seos_emulator_aes_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  184. // Synethic IV
  185. /// random bytes
  186. uint8_t rnd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  187. uint8_t cmac[16] = {0};
  188. /// cmac
  189. aes_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  190. uint8_t iv[16];
  191. memcpy(iv + 0, rnd, sizeof(rnd));
  192. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  193. // Copy IV to buffer because mbedtls_aes_crypt_cbc mutates it
  194. memcpy(buffer + 0, iv, sizeof(iv));
  195. uint8_t clear[0x30];
  196. memset(clear, 0, sizeof(clear));
  197. size_t index = 0;
  198. // OID
  199. clear[index++] = 0x06;
  200. clear[index++] = SEOS_ADF_OID_LEN;
  201. memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  202. index += SEOS_ADF_OID_LEN;
  203. // diversifier
  204. clear[index++] = 0xcf;
  205. clear[index++] = credential->diversifier_len;
  206. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  207. index += credential->diversifier_len;
  208. mbedtls_aes_context ctx;
  209. mbedtls_aes_init(&ctx);
  210. mbedtls_aes_setkey_enc(&ctx, SEOS_ADF1_PRIV_ENC, sizeof(SEOS_ADF1_PRIV_ENC) * 8);
  211. mbedtls_aes_crypt_cbc(
  212. &ctx, MBEDTLS_AES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  213. mbedtls_aes_free(&ctx);
  214. }
  215. bool seos_emulator_select_adf(
  216. const uint8_t* oid_list,
  217. size_t oid_list_len,
  218. AuthParameters* params,
  219. SeosCredential* credential,
  220. BitBuffer* tx_buffer) {
  221. FURI_LOG_D(TAG, "Select ADF");
  222. void* p = NULL;
  223. if(credential->adf_oid_len > 0) {
  224. p = memmem(oid_list, oid_list_len, credential->adf_oid, credential->adf_oid_len);
  225. if(p) {
  226. seos_log_buffer(TAG, "Select ADF OID(credential)", p, credential->adf_oid_len);
  227. if(credential->adf_response[0] == 0xCD) {
  228. FURI_LOG_I(TAG, "Using hardcoded ADF Response");
  229. // 4 byte cipher/hash
  230. // 2 byte cryptogram header
  231. // x bytes of cryptogram
  232. // 10 bytes for mac (2 byte header + 8 byte cmac)
  233. size_t adf_response_len = 4 + 2 + credential->adf_response[5] + 10;
  234. bit_buffer_append_bytes(tx_buffer, credential->adf_response, adf_response_len);
  235. params->cipher = credential->adf_response[2];
  236. params->hash = credential->adf_response[3];
  237. credential->use_hardcoded = true;
  238. return true;
  239. }
  240. }
  241. }
  242. // Next we try to match the ADF OID from the keys file
  243. p = memmem(oid_list, oid_list_len, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  244. if(p) {
  245. seos_log_buffer(TAG, "Select ADF OID(keys)", p, SEOS_ADF_OID_LEN);
  246. } else {
  247. return false;
  248. }
  249. size_t prefix_len = bit_buffer_get_size_bytes(tx_buffer);
  250. size_t des_cryptogram_length = 56;
  251. size_t aes_cryptogram_length = 64;
  252. uint8_t header[] = {0xcd, 0x02, params->cipher, params->hash};
  253. bit_buffer_append_bytes(tx_buffer, header, sizeof(header));
  254. // cryptogram
  255. // 06112b0601040181e438010102011801010202 cf 07 3d4c010c71cfa7 e2d0b41a00cc5e494c8d52b6e562592399fe614a
  256. uint8_t buffer[64];
  257. uint8_t cmac[16];
  258. memset(buffer, 0, sizeof(buffer));
  259. if(params->cipher == AES_128_CBC) {
  260. uint8_t cryptogram_prefix[] = {0x85, aes_cryptogram_length};
  261. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  262. seos_emulator_aes_adf_payload(credential, buffer);
  263. bit_buffer_append_bytes(tx_buffer, buffer, aes_cryptogram_length);
  264. aes_cmac(
  265. SEOS_ADF1_PRIV_MAC,
  266. sizeof(SEOS_ADF1_PRIV_MAC),
  267. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  268. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  269. cmac);
  270. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  271. uint8_t cryptogram_prefix[] = {0x85, des_cryptogram_length};
  272. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  273. seos_emulator_des_adf_payload(credential, buffer);
  274. bit_buffer_append_bytes(tx_buffer, buffer, des_cryptogram_length);
  275. // +2 / -2 is to ignore iso14a framing
  276. des_cmac(
  277. SEOS_ADF1_PRIV_MAC,
  278. sizeof(SEOS_ADF1_PRIV_MAC),
  279. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  280. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  281. cmac);
  282. }
  283. uint8_t cmac_prefix[] = {0x8e, 0x08};
  284. bit_buffer_append_bytes(tx_buffer, cmac_prefix, sizeof(cmac_prefix));
  285. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  286. return true;
  287. }
  288. NfcCommand seos_worker_listener_inspect_reader(Seos* seos) {
  289. SeosEmulator* seos_emulator = seos->seos_emulator;
  290. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  291. NfcCommand ret = NfcCommandContinue;
  292. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  293. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  294. uint8_t offset = NAD ? 2 : 1;
  295. // + x to skip stuff before APDU
  296. const uint8_t* apdu = rx_data + offset;
  297. if(memcmp(apdu, select_header, sizeof(select_header)) == 0) {
  298. if(memcmp(
  299. apdu + sizeof(select_header) + 1, OPERATION_SELECTOR, sizeof(OPERATION_SELECTOR)) ==
  300. 0) {
  301. uint8_t enableInspection[] = {
  302. 0x6f, 0x08, 0x85, 0x06, 0x02, 0x01, 0x40, 0x02, 0x01, 0x00};
  303. bit_buffer_append_bytes(tx_buffer, enableInspection, sizeof(enableInspection));
  304. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAIDSelected);
  305. } else {
  306. bit_buffer_append_bytes(tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  307. }
  308. } else if(bit_buffer_get_size_bytes(seos_emulator->rx_buffer) > (size_t)(offset + 2)) {
  309. FURI_LOG_I(TAG, "NFC stop; %d bytes", bit_buffer_get_size_bytes(seos_emulator->rx_buffer));
  310. ret = NfcCommandStop;
  311. }
  312. return ret;
  313. }
  314. NfcCommand seos_worker_listener_process_message(Seos* seos) {
  315. SeosEmulator* seos_emulator = seos->seos_emulator;
  316. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  317. NfcCommand ret = NfcCommandContinue;
  318. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  319. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  320. uint8_t offset = NAD ? 2 : 1;
  321. // + x to skip stuff before APDU
  322. const uint8_t* apdu = rx_data + offset;
  323. if(memcmp(apdu, select_header, sizeof(select_header)) == 0) {
  324. seos_emulator->credential->use_hardcoded = false;
  325. if(memcmp(apdu + sizeof(select_header) + 1, standard_seos_aid, sizeof(standard_seos_aid)) ==
  326. 0) {
  327. seos_emulator_select_aid(seos_emulator->tx_buffer);
  328. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAIDSelected);
  329. } else if(
  330. memcmp(
  331. apdu + sizeof(select_header) + 1,
  332. OPERATION_SELECTOR_POST_RESET,
  333. sizeof(OPERATION_SELECTOR_POST_RESET)) == 0) {
  334. FURI_LOG_I(TAG, "OPERATION_SELECTOR_POST_RESET");
  335. bit_buffer_append_bytes(
  336. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  337. } else if(
  338. memcmp(
  339. apdu + sizeof(select_header) + 1,
  340. OPERATION_SELECTOR,
  341. sizeof(OPERATION_SELECTOR)) == 0) {
  342. FURI_LOG_I(TAG, "OPERATION_SELECTOR");
  343. bit_buffer_append_bytes(
  344. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  345. } else if(
  346. memcmp(
  347. apdu + sizeof(select_header) + 1,
  348. MOBILE_SEOS_ADMIN_CARD,
  349. sizeof(MOBILE_SEOS_ADMIN_CARD)) == 0) {
  350. FURI_LOG_I(TAG, "MOBILE_SEOS_ADMIN_CARD");
  351. bit_buffer_append_bytes(
  352. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  353. } else {
  354. seos_log_bitbuffer(TAG, "Reject select", seos_emulator->rx_buffer);
  355. bit_buffer_append_bytes(
  356. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  357. }
  358. } else if(memcmp(apdu, select_adf_header, sizeof(select_adf_header)) == 0) {
  359. // +1 to skip APDU length byte
  360. const uint8_t* oid_list = apdu + sizeof(select_adf_header) + 1;
  361. size_t oid_list_len = apdu[sizeof(select_adf_header)];
  362. if(seos_emulator_select_adf(
  363. oid_list,
  364. oid_list_len,
  365. &seos_emulator->params,
  366. seos_emulator->credential,
  367. seos_emulator->tx_buffer)) {
  368. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventADFMatched);
  369. } else {
  370. FURI_LOG_W(TAG, "Failed to match any ADF OID");
  371. }
  372. } else if(memcmp(apdu, general_authenticate_1, sizeof(general_authenticate_1)) == 0) {
  373. seos_emulator_general_authenticate_1(seos_emulator->tx_buffer, seos_emulator->params);
  374. } else if(memcmp(apdu, general_authenticate_2_header, sizeof(general_authenticate_2_header)) == 0) {
  375. if(!seos_emulator_general_authenticate_2(
  376. apdu,
  377. bit_buffer_get_size_bytes(seos_emulator->rx_buffer),
  378. seos_emulator->credential,
  379. &seos_emulator->params,
  380. seos_emulator->tx_buffer)) {
  381. FURI_LOG_W(TAG, "Failure in General Authenticate 2");
  382. ret = NfcCommandStop;
  383. return ret;
  384. }
  385. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAuthenticated);
  386. // Prepare for future communication
  387. seos_emulator->secure_messaging = secure_messaging_alloc(&seos_emulator->params);
  388. } else if(memcmp(apdu, secure_messaging_header, sizeof(secure_messaging_header)) == 0) {
  389. uint8_t request_sio[] = {0x5c, 0x02, 0xff, 0x00};
  390. if(seos_emulator->secure_messaging) {
  391. FURI_LOG_D(TAG, "Unwrap secure message");
  392. // 0b00 0ccb3fff 16 8508fa8395d30de4e8e097008e085da7edbd833b002d00
  393. // Ignore 2 iso frame bytes
  394. size_t bytes_to_ignore = offset;
  395. BitBuffer* tmp = bit_buffer_alloc(bit_buffer_get_size_bytes(seos_emulator->rx_buffer));
  396. bit_buffer_append_bytes(
  397. tmp,
  398. bit_buffer_get_data(seos_emulator->rx_buffer) + bytes_to_ignore,
  399. bit_buffer_get_size_bytes(seos_emulator->rx_buffer) - bytes_to_ignore);
  400. seos_log_bitbuffer(TAG, "NFC received(wrapped)", tmp);
  401. secure_messaging_unwrap_apdu(seos_emulator->secure_messaging, tmp);
  402. seos_log_bitbuffer(TAG, "NFC received(clear)", tmp);
  403. const uint8_t* message = bit_buffer_get_data(tmp);
  404. if(memcmp(message, request_sio, sizeof(request_sio)) == 0) {
  405. view_dispatcher_send_custom_event(
  406. seos->view_dispatcher, SeosCustomEventSIORequested);
  407. BitBuffer* sio_file = bit_buffer_alloc(128);
  408. bit_buffer_append_bytes(sio_file, message + 2, 2); // fileId
  409. bit_buffer_append_byte(sio_file, seos_emulator->credential->sio_len);
  410. bit_buffer_append_bytes(
  411. sio_file, seos_emulator->credential->sio, seos_emulator->credential->sio_len);
  412. secure_messaging_wrap_rapdu(
  413. seos_emulator->secure_messaging,
  414. (uint8_t*)bit_buffer_get_data(sio_file),
  415. bit_buffer_get_size_bytes(sio_file),
  416. tx_buffer);
  417. bit_buffer_free(sio_file);
  418. }
  419. bit_buffer_free(tmp);
  420. } else {
  421. uint8_t no_sm[] = {0x69, 0x88};
  422. bit_buffer_append_bytes(tx_buffer, no_sm, sizeof(no_sm));
  423. }
  424. } else {
  425. // 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
  426. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventEmulate);
  427. }
  428. return ret;
  429. }
  430. NfcCommand seos_worker_listener_callback(NfcGenericEvent event, void* context) {
  431. furi_assert(context);
  432. furi_assert(event.protocol == NfcProtocolIso14443_4a);
  433. furi_assert(event.event_data);
  434. Seos* seos = context;
  435. SeosEmulator* seos_emulator = seos->seos_emulator;
  436. NfcCommand ret = NfcCommandContinue;
  437. Iso14443_4aListenerEvent* iso14443_4a_event = event.event_data;
  438. Iso14443_3aListener* iso14443_listener = event.instance;
  439. seos_emulator->iso14443_listener = iso14443_listener;
  440. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  441. bit_buffer_reset(tx_buffer);
  442. switch(iso14443_4a_event->type) {
  443. case Iso14443_4aListenerEventTypeReceivedData:
  444. seos_emulator->rx_buffer = iso14443_4a_event->data->buffer;
  445. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  446. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  447. uint8_t offset = NAD ? 2 : 1;
  448. if(bit_buffer_get_size_bytes(iso14443_4a_event->data->buffer) == offset) {
  449. FURI_LOG_I(TAG, "No contents in frame");
  450. break;
  451. }
  452. seos_log_bitbuffer(TAG, "NFC received", seos_emulator->rx_buffer);
  453. // Some ISO14443a framing I need to figure out
  454. bit_buffer_append_bytes(tx_buffer, rx_data, offset);
  455. if(seos->flow_mode == FLOW_CRED) {
  456. ret = seos_worker_listener_process_message(seos);
  457. } else if(seos->flow_mode == FLOW_INSPECT) {
  458. ret = seos_worker_listener_inspect_reader(seos);
  459. }
  460. if(bit_buffer_get_size_bytes(seos_emulator->tx_buffer) >
  461. offset) { // contents belong iso framing
  462. if(memcmp(
  463. FILE_NOT_FOUND,
  464. bit_buffer_get_data(tx_buffer) + bit_buffer_get_size_bytes(tx_buffer) -
  465. sizeof(FILE_NOT_FOUND),
  466. sizeof(FILE_NOT_FOUND)) != 0) {
  467. bit_buffer_append_bytes(tx_buffer, success, sizeof(success));
  468. }
  469. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  470. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  471. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  472. if(error != NfcErrorNone) {
  473. FURI_LOG_W(TAG, "Tx error: %d", error);
  474. break;
  475. }
  476. } else {
  477. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  478. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  479. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  480. if(error != NfcErrorNone) {
  481. FURI_LOG_W(TAG, "Tx error: %d", error);
  482. break;
  483. }
  484. }
  485. break;
  486. case Iso14443_4aListenerEventTypeHalted:
  487. FURI_LOG_I(TAG, "Halted");
  488. break;
  489. case Iso14443_4aListenerEventTypeFieldOff:
  490. FURI_LOG_I(TAG, "Field Off");
  491. break;
  492. }
  493. if(ret == NfcCommandStop) {
  494. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventReaderError);
  495. }
  496. return ret;
  497. }