seos_emulator.c 23 KB

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