seos_emulator.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #include "seos_emulator_i.h"
  2. #define TAG "SeosEmulator"
  3. #define NAD_MASK 0x08
  4. #define SEADER_PATH "/ext/apps_data/seader"
  5. #define SEADER_APP_EXTENSION ".credential"
  6. static uint8_t select_header[] = {0x00, 0xa4, 0x04, 0x00};
  7. static uint8_t standard_seos_aid[] = {0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  8. static uint8_t MOBILE_SEOS_ADMIN_CARD[] =
  9. {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x2d, 0x00, 0x01, 0x01};
  10. static uint8_t OPERATION_SELECTOR[] = {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x2f, 0x00, 0x01, 0x01};
  11. static uint8_t OPERATION_SELECTOR_POST_RESET[] =
  12. {0xa0, 0x00, 0x00, 0x03, 0x82, 0x00, 0x31, 0x00, 0x01, 0x01};
  13. static uint8_t SEOS_APPLET_FCI[] =
  14. {0x6F, 0x0C, 0x84, 0x0A, 0xA0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  15. static uint8_t FILE_NOT_FOUND[] = {0x6A, 0x82};
  16. static uint8_t success[] = {0x90, 0x00};
  17. static uint8_t select_adf_header[] = {0x80, 0xa5, 0x04, 0x00};
  18. static uint8_t general_authenticate_1[] =
  19. {0x00, 0x87, 0x00, 0x01, 0x04, 0x7c, 0x02, 0x81, 0x00, 0x00};
  20. static uint8_t general_authenticate_1_response_header[] = {0x7c, 0x0a, 0x81, 0x08};
  21. static uint8_t general_authenticate_2_header[] = {0x00, 0x87, 0x00, 0x01};
  22. static uint8_t secure_messaging_header[] = {0x0c, 0xcb, 0x3f, 0xff};
  23. static uint8_t empty[16] =
  24. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  25. SeosEmulator* seos_emulator_alloc(SeosCredential* credential) {
  26. SeosEmulator* seos_emulator = malloc(sizeof(SeosEmulator));
  27. memset(seos_emulator, 0, sizeof(SeosEmulator));
  28. // Using DES for greater compatibilty
  29. seos_emulator->params.cipher = TWO_KEY_3DES_CBC_MODE;
  30. seos_emulator->params.hash = SHA1;
  31. memset(seos_emulator->params.rndICC, 0x0d, sizeof(seos_emulator->params.rndICC));
  32. memset(seos_emulator->params.rNonce, 0x0c, sizeof(seos_emulator->params.rNonce));
  33. seos_emulator->credential = credential;
  34. seos_emulator->secure_messaging = NULL;
  35. seos_emulator->storage = furi_record_open(RECORD_STORAGE);
  36. seos_emulator->dialogs = furi_record_open(RECORD_DIALOGS);
  37. seos_emulator->load_path = furi_string_alloc();
  38. seos_emulator->tx_buffer = bit_buffer_alloc(SEOS_WORKER_MAX_BUFFER_SIZE);
  39. return seos_emulator;
  40. }
  41. void seos_emulator_free(SeosEmulator* seos_emulator) {
  42. furi_assert(seos_emulator);
  43. if(seos_emulator->secure_messaging) {
  44. secure_messaging_free(seos_emulator->secure_messaging);
  45. }
  46. furi_record_close(RECORD_STORAGE);
  47. furi_record_close(RECORD_DIALOGS);
  48. furi_string_free(seos_emulator->load_path);
  49. bit_buffer_free(seos_emulator->tx_buffer);
  50. free(seos_emulator);
  51. }
  52. void seos_emulator_set_loading_callback(
  53. SeosEmulator* seos_emulator,
  54. SeosLoadingCallback callback,
  55. void* context) {
  56. furi_assert(seos_emulator);
  57. seos_emulator->loading_cb = callback;
  58. seos_emulator->loading_cb_ctx = context;
  59. }
  60. static bool
  61. seos_emulator_file_load(SeosEmulator* seos_emulator, FuriString* path, bool show_dialog) {
  62. bool parsed = false;
  63. FlipperFormat* file = flipper_format_file_alloc(seos_emulator->storage);
  64. FuriString* temp_str;
  65. temp_str = furi_string_alloc();
  66. bool deprecated_version = false;
  67. if(seos_emulator->loading_cb) {
  68. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, true);
  69. }
  70. memset(
  71. seos_emulator->credential->diversifier, 0, sizeof(seos_emulator->credential->diversifier));
  72. memset(seos_emulator->credential->sio, 0, sizeof(seos_emulator->credential->sio));
  73. do {
  74. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  75. // Read and verify file header
  76. uint32_t version = 0;
  77. if(!flipper_format_read_header(file, temp_str, &version)) break;
  78. if(furi_string_cmp_str(temp_str, seos_file_header) || (version != seos_file_version)) {
  79. deprecated_version = true;
  80. break;
  81. }
  82. if(!flipper_format_read_uint32(
  83. file,
  84. "Diversifier Length",
  85. (uint32_t*)&(seos_emulator->credential->diversifier_len),
  86. 1))
  87. break;
  88. if(!flipper_format_read_hex(
  89. file,
  90. "Diversifier",
  91. seos_emulator->credential->diversifier,
  92. seos_emulator->credential->diversifier_len))
  93. break;
  94. if(!flipper_format_read_uint32(
  95. file, "SIO Length", (uint32_t*)&(seos_emulator->credential->sio_len), 1))
  96. break;
  97. if(!flipper_format_read_hex(
  98. file, "SIO", seos_emulator->credential->sio, seos_emulator->credential->sio_len))
  99. break;
  100. // optional
  101. memset(
  102. seos_emulator->credential->priv_key, 0, sizeof(seos_emulator->credential->priv_key));
  103. memset(
  104. seos_emulator->credential->auth_key, 0, sizeof(seos_emulator->credential->auth_key));
  105. memset(
  106. seos_emulator->credential->adf_response,
  107. 0,
  108. sizeof(seos_emulator->credential->adf_response));
  109. flipper_format_read_hex(
  110. file,
  111. "Priv Key",
  112. seos_emulator->credential->priv_key,
  113. sizeof(seos_emulator->credential->priv_key));
  114. flipper_format_read_hex(
  115. file,
  116. "Auth Key",
  117. seos_emulator->credential->auth_key,
  118. sizeof(seos_emulator->credential->auth_key));
  119. if(memcmp(seos_emulator->credential->priv_key, empty, sizeof(empty)) != 0) {
  120. FURI_LOG_I(TAG, "+ Priv Key");
  121. }
  122. if(memcmp(seos_emulator->credential->priv_key, empty, sizeof(empty)) != 0) {
  123. FURI_LOG_I(TAG, "+ Auth Key");
  124. }
  125. flipper_format_read_hex(
  126. file,
  127. "ADF Response",
  128. seos_emulator->credential->adf_response,
  129. sizeof(seos_emulator->credential->adf_response));
  130. parsed = true;
  131. } while(false);
  132. if(seos_emulator->loading_cb) {
  133. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, false);
  134. }
  135. if((!parsed) && (show_dialog)) {
  136. if(deprecated_version) {
  137. dialog_message_show_storage_error(seos_emulator->dialogs, "File format deprecated");
  138. } else {
  139. dialog_message_show_storage_error(seos_emulator->dialogs, "Can not parse\nfile");
  140. }
  141. }
  142. furi_string_free(temp_str);
  143. flipper_format_free(file);
  144. return parsed;
  145. }
  146. static bool seos_emulator_file_load_seader(
  147. SeosEmulator* seos_emulator,
  148. FuriString* path,
  149. bool show_dialog) {
  150. bool parsed = false;
  151. FlipperFormat* file = flipper_format_file_alloc(seos_emulator->storage);
  152. FuriString* reason = furi_string_alloc_set("Couldn't load file");
  153. FuriString* temp_str;
  154. temp_str = furi_string_alloc();
  155. const char* seader_file_header = "Flipper Seader Credential";
  156. const uint32_t seader_file_version = 1;
  157. if(seos_emulator->loading_cb) {
  158. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, true);
  159. }
  160. memset(
  161. seos_emulator->credential->diversifier, 0, sizeof(seos_emulator->credential->diversifier));
  162. memset(seos_emulator->credential->sio, 0, sizeof(seos_emulator->credential->sio));
  163. do {
  164. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  165. // Read and verify file header
  166. uint32_t version = 0;
  167. if(!flipper_format_read_header(file, temp_str, &version)) break;
  168. if(furi_string_cmp_str(temp_str, seader_file_header) || (version != seader_file_version)) {
  169. furi_string_printf(reason, "Deprecated file format");
  170. break;
  171. }
  172. // Don't forget, order of keys is important
  173. if(!flipper_format_key_exist(file, "SIO")) {
  174. furi_string_printf(reason, "Missing SIO");
  175. break;
  176. }
  177. seos_emulator->credential->sio_len = 64; // Seader SIO size
  178. // We can't check the return status because it will be false if less than the requested length of bytes was read.
  179. flipper_format_read_hex(
  180. file, "SIO", seos_emulator->credential->sio, seos_emulator->credential->sio_len);
  181. seos_emulator->credential->sio_len = seos_emulator->credential->sio[1] +
  182. 4; // 2 for type and length, 2 for null after SIO data
  183. // -------------
  184. if(!flipper_format_key_exist(file, "Diversifier")) {
  185. furi_string_printf(reason, "Missing Diversifier");
  186. break;
  187. }
  188. seos_emulator->credential->diversifier_len = 8; //Seader diversifier size
  189. flipper_format_read_hex(
  190. file,
  191. "Diversifier",
  192. seos_emulator->credential->diversifier,
  193. seos_emulator->credential->diversifier_len);
  194. uint8_t* end = memchr(seos_emulator->credential->diversifier, 0, 8);
  195. if(end) {
  196. seos_emulator->credential->diversifier_len =
  197. end - seos_emulator->credential->diversifier;
  198. } // Returns NULL if char cannot be found
  199. SeosCredential* cred = seos_emulator->credential;
  200. char display[128 * 2 + 1];
  201. memset(display, 0, sizeof(display));
  202. for(uint8_t i = 0; i < cred->sio_len; i++) {
  203. snprintf(display + (i * 2), sizeof(display), "%02x", cred->sio[i]);
  204. }
  205. FURI_LOG_D(TAG, "SIO: %s", display);
  206. memset(display, 0, sizeof(display));
  207. for(uint8_t i = 0; i < cred->diversifier_len; i++) {
  208. snprintf(display + (i * 2), sizeof(display), "%02x", cred->diversifier[i]);
  209. }
  210. FURI_LOG_D(TAG, "Diversifier: %s", display);
  211. parsed = true;
  212. } while(false);
  213. if(seos_emulator->loading_cb) {
  214. seos_emulator->loading_cb(seos_emulator->loading_cb_ctx, false);
  215. }
  216. if((!parsed) && (show_dialog)) {
  217. dialog_message_show_storage_error(seos_emulator->dialogs, furi_string_get_cstr(reason));
  218. }
  219. furi_string_free(reason);
  220. furi_string_free(temp_str);
  221. flipper_format_free(file);
  222. return parsed;
  223. }
  224. bool seos_emulator_file_select_seader(SeosEmulator* seos_emulator) {
  225. furi_assert(seos_emulator);
  226. bool res = false;
  227. FuriString* app_folder = furi_string_alloc_set(SEADER_PATH);
  228. DialogsFileBrowserOptions browser_options;
  229. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  230. browser_options.base_path = SEADER_PATH;
  231. res = dialog_file_browser_show(
  232. seos_emulator->dialogs, seos_emulator->load_path, app_folder, &browser_options);
  233. furi_string_free(app_folder);
  234. if(res) {
  235. FuriString* filename;
  236. filename = furi_string_alloc();
  237. path_extract_filename(seos_emulator->load_path, filename, true);
  238. strncpy(seos_emulator->name, furi_string_get_cstr(filename), SEOS_FILE_NAME_MAX_LENGTH);
  239. res = seos_emulator_file_load_seader(seos_emulator, seos_emulator->load_path, true);
  240. furi_string_free(filename);
  241. }
  242. return res;
  243. }
  244. bool seos_emulator_file_select_seos(SeosEmulator* seos_emulator) {
  245. furi_assert(seos_emulator);
  246. bool res = false;
  247. FuriString* seos_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  248. DialogsFileBrowserOptions browser_options;
  249. dialog_file_browser_set_basic_options(&browser_options, SEOS_APP_EXTENSION, &I_Nfc_10px);
  250. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  251. res = dialog_file_browser_show(
  252. seos_emulator->dialogs, seos_emulator->load_path, seos_app_folder, &browser_options);
  253. furi_string_free(seos_app_folder);
  254. if(res) {
  255. FuriString* filename;
  256. filename = furi_string_alloc();
  257. path_extract_filename(seos_emulator->load_path, filename, true);
  258. strncpy(seos_emulator->name, furi_string_get_cstr(filename), SEOS_FILE_NAME_MAX_LENGTH);
  259. res = seos_emulator_file_load(seos_emulator, seos_emulator->load_path, true);
  260. furi_string_free(filename);
  261. }
  262. return res;
  263. }
  264. bool seos_emulator_file_select(SeosEmulator* seos_emulator) {
  265. if(seos_emulator->load_type == SeosLoadSeos) {
  266. return seos_emulator_file_select_seos(seos_emulator);
  267. } else if(seos_emulator->load_type == SeosLoadSeader) {
  268. return seos_emulator_file_select_seader(seos_emulator);
  269. }
  270. return false;
  271. }
  272. bool seos_emulator_delete(SeosEmulator* seos_emulator, bool use_load_path) {
  273. furi_assert(seos_emulator);
  274. bool deleted = false;
  275. FuriString* file_path;
  276. file_path = furi_string_alloc();
  277. do {
  278. // Delete original file
  279. if(use_load_path && !furi_string_empty(seos_emulator->load_path)) {
  280. furi_string_set(file_path, seos_emulator->load_path);
  281. } else {
  282. furi_string_printf(
  283. file_path, APP_DATA_PATH("%s%s"), seos_emulator->name, SEOS_APP_EXTENSION);
  284. }
  285. if(!storage_simply_remove(seos_emulator->storage, furi_string_get_cstr(file_path))) break;
  286. deleted = true;
  287. } while(0);
  288. if(!deleted) {
  289. dialog_message_show_storage_error(seos_emulator->dialogs, "Can not remove file");
  290. }
  291. furi_string_free(file_path);
  292. return deleted;
  293. }
  294. void seos_emulator_select_aid(BitBuffer* tx_buffer) {
  295. FURI_LOG_D(TAG, "Select AID");
  296. bit_buffer_append_bytes(tx_buffer, SEOS_APPLET_FCI, sizeof(SEOS_APPLET_FCI));
  297. }
  298. void seos_emulator_general_authenticate_1(BitBuffer* tx_buffer, AuthParameters params) {
  299. bit_buffer_append_bytes(
  300. tx_buffer,
  301. general_authenticate_1_response_header,
  302. sizeof(general_authenticate_1_response_header));
  303. bit_buffer_append_bytes(tx_buffer, params.rndICC, sizeof(params.rndICC));
  304. }
  305. // 0a00
  306. // 00870001 2c7c 2a82 28 bbb4e9156136f27f687e2967865dfe812e33c95ddcf9294a4340d26da3e76db0220d1163c591e5b8 00
  307. bool seos_emulator_general_authenticate_2(
  308. const uint8_t* buffer,
  309. size_t buffer_len,
  310. SeosCredential* credential,
  311. AuthParameters* params,
  312. BitBuffer* tx_buffer) {
  313. FURI_LOG_D(TAG, "seos_emulator_general_authenticate_2");
  314. UNUSED(buffer_len);
  315. uint8_t* rx_data = (uint8_t*)buffer;
  316. uint8_t* cryptogram = rx_data + sizeof(general_authenticate_2_header) + 5;
  317. size_t encrypted_len = 32;
  318. uint8_t* mac = cryptogram + encrypted_len;
  319. params->key_no = rx_data[3];
  320. if(memcmp(credential->priv_key, empty, sizeof(empty)) == 0) {
  321. seos_worker_diversify_key(
  322. SEOS_ADF1_READ,
  323. credential->diversifier,
  324. credential->diversifier_len,
  325. SEOS_ADF_OID,
  326. SEOS_ADF_OID_LEN,
  327. params->cipher,
  328. params->hash,
  329. params->key_no,
  330. true,
  331. params->priv_key);
  332. } else {
  333. memcpy(params->priv_key, credential->priv_key, sizeof(params->priv_key));
  334. }
  335. if(memcmp(credential->auth_key, empty, sizeof(empty)) == 0) {
  336. seos_worker_diversify_key(
  337. SEOS_ADF1_READ,
  338. credential->diversifier,
  339. credential->diversifier_len,
  340. SEOS_ADF_OID,
  341. SEOS_ADF_OID_LEN,
  342. params->cipher,
  343. params->hash,
  344. params->key_no,
  345. false,
  346. params->auth_key);
  347. } else {
  348. memcpy(params->auth_key, credential->auth_key, sizeof(params->auth_key));
  349. }
  350. uint8_t cmac[16];
  351. if(params->cipher == AES_128_CBC) {
  352. aes_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  353. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  354. des_cmac(params->auth_key, sizeof(params->auth_key), cryptogram, encrypted_len, cmac);
  355. } else {
  356. FURI_LOG_W(TAG, "Cipher not matched");
  357. return false;
  358. }
  359. if(memcmp(cmac, mac, SEOS_WORKER_CMAC_SIZE) != 0) {
  360. FURI_LOG_W(TAG, "Incorrect cryptogram mac %02x... vs %02x...", cmac[0], mac[0]);
  361. return false;
  362. }
  363. uint8_t clear[32];
  364. if(params->cipher == AES_128_CBC) {
  365. seos_worker_aes_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  366. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  367. seos_worker_des_decrypt(params->priv_key, encrypted_len, cryptogram, clear);
  368. } else {
  369. FURI_LOG_W(TAG, "Cipher not matched");
  370. }
  371. size_t index = 0;
  372. memcpy(params->UID, clear + index, sizeof(params->UID));
  373. index += sizeof(params->UID);
  374. if(memcmp(clear + index, params->rndICC, sizeof(params->rndICC)) != 0) {
  375. FURI_LOG_W(TAG, "Incorrect rndICC returned");
  376. return false;
  377. }
  378. index += sizeof(params->rndICC);
  379. memcpy(params->cNonce, clear + index, sizeof(params->cNonce));
  380. index += sizeof(params->cNonce);
  381. // Construct response
  382. uint8_t response_header[] = {0x7c, 0x2a, 0x82, 0x28};
  383. memset(clear, 0, sizeof(clear));
  384. memset(cmac, 0, sizeof(cmac));
  385. index = 0;
  386. memcpy(clear + index, params->rndICC, sizeof(params->rndICC));
  387. index += sizeof(params->rndICC);
  388. memcpy(clear + index, params->UID, sizeof(params->UID));
  389. index += sizeof(params->UID);
  390. memcpy(clear + index, params->rNonce, sizeof(params->rNonce));
  391. index += sizeof(params->rNonce);
  392. uint8_t encrypted[32];
  393. if(params->cipher == AES_128_CBC) {
  394. seos_worker_aes_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  395. aes_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  396. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  397. seos_worker_des_encrypt(params->priv_key, sizeof(clear), clear, encrypted);
  398. des_cmac(params->auth_key, sizeof(params->auth_key), encrypted, sizeof(encrypted), cmac);
  399. } else {
  400. FURI_LOG_W(TAG, "Cipher not matched");
  401. }
  402. bit_buffer_append_bytes(tx_buffer, response_header, sizeof(response_header));
  403. bit_buffer_append_bytes(tx_buffer, encrypted, sizeof(encrypted));
  404. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  405. return true;
  406. }
  407. void seos_emulator_des_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  408. // Synethic IV
  409. /// random bytes
  410. uint8_t rnd[4] = {0, 0, 0, 0};
  411. uint8_t cmac[8] = {0};
  412. /// cmac
  413. des_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  414. uint8_t iv[8];
  415. memcpy(iv + 0, rnd, sizeof(rnd));
  416. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  417. // Copy IV to buffer because mbedtls_des3_crypt_cbc mutates it
  418. memcpy(buffer + 0, iv, sizeof(iv));
  419. uint8_t clear[0x30];
  420. memset(clear, 0, sizeof(clear));
  421. size_t index = 0;
  422. // OID
  423. clear[index++] = 0x06;
  424. clear[index++] = SEOS_ADF_OID_LEN, memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  425. index += SEOS_ADF_OID_LEN;
  426. // diversifier
  427. clear[index++] = 0xcf;
  428. clear[index++] = credential->diversifier_len;
  429. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  430. index += credential->diversifier_len;
  431. mbedtls_des3_context ctx;
  432. mbedtls_des3_init(&ctx);
  433. mbedtls_des3_set2key_enc(&ctx, SEOS_ADF1_PRIV_ENC);
  434. mbedtls_des3_crypt_cbc(
  435. &ctx, MBEDTLS_DES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  436. mbedtls_des3_free(&ctx);
  437. }
  438. void seos_emulator_aes_adf_payload(SeosCredential* credential, uint8_t* buffer) {
  439. // Synethic IV
  440. /// random bytes
  441. uint8_t rnd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  442. uint8_t cmac[16] = {0};
  443. /// cmac
  444. aes_cmac(SEOS_ADF1_PRIV_MAC, sizeof(SEOS_ADF1_PRIV_MAC), rnd, sizeof(rnd), cmac);
  445. uint8_t iv[16];
  446. memcpy(iv + 0, rnd, sizeof(rnd));
  447. memcpy(iv + sizeof(rnd), cmac, sizeof(iv) - sizeof(rnd));
  448. // Copy IV to buffer because mbedtls_aes_crypt_cbc mutates it
  449. memcpy(buffer + 0, iv, sizeof(iv));
  450. uint8_t clear[0x30];
  451. memset(clear, 0, sizeof(clear));
  452. size_t index = 0;
  453. // OID
  454. clear[index++] = 0x06;
  455. clear[index++] = SEOS_ADF_OID_LEN;
  456. memcpy(clear + index, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  457. index += SEOS_ADF_OID_LEN;
  458. // diversifier
  459. clear[index++] = 0xcf;
  460. clear[index++] = credential->diversifier_len;
  461. memcpy(clear + index, credential->diversifier, credential->diversifier_len);
  462. index += credential->diversifier_len;
  463. mbedtls_aes_context ctx;
  464. mbedtls_aes_init(&ctx);
  465. mbedtls_aes_setkey_enc(&ctx, SEOS_ADF1_PRIV_ENC, sizeof(SEOS_ADF1_PRIV_ENC) * 8);
  466. mbedtls_aes_crypt_cbc(
  467. &ctx, MBEDTLS_AES_ENCRYPT, sizeof(clear), iv, clear, buffer + sizeof(iv));
  468. mbedtls_aes_free(&ctx);
  469. }
  470. void seos_emulator_select_adf(
  471. AuthParameters* params,
  472. SeosCredential* credential,
  473. BitBuffer* tx_buffer) {
  474. FURI_LOG_D(TAG, "Select ADF");
  475. // Shortcut if the credential file contained the hardcoded response
  476. if(credential->adf_response[2] != 0x00 && credential->adf_response[2] == params->cipher) {
  477. FURI_LOG_I(TAG, "Using hardcoded ADF Response");
  478. bit_buffer_append_bytes(
  479. tx_buffer, credential->adf_response, sizeof(credential->adf_response));
  480. seos_log_bitbuffer(TAG, "Select ADF (0xcd02...)", tx_buffer);
  481. return;
  482. }
  483. size_t prefix_len = bit_buffer_get_size_bytes(tx_buffer);
  484. size_t des_cryptogram_length = 56;
  485. size_t aes_cryptogram_length = 64;
  486. uint8_t header[] = {0xcd, 0x02, params->cipher, params->hash};
  487. bit_buffer_append_bytes(tx_buffer, header, sizeof(header));
  488. // cryptogram
  489. // 06112b0601040181e438010102011801010202 cf 07 3d4c010c71cfa7 e2d0b41a00cc5e494c8d52b6e562592399fe614a
  490. uint8_t buffer[64];
  491. uint8_t cmac[16];
  492. memset(buffer, 0, sizeof(buffer));
  493. if(params->cipher == AES_128_CBC) {
  494. uint8_t cryptogram_prefix[] = {0x85, aes_cryptogram_length};
  495. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  496. seos_emulator_aes_adf_payload(credential, buffer);
  497. bit_buffer_append_bytes(tx_buffer, buffer, aes_cryptogram_length);
  498. aes_cmac(
  499. SEOS_ADF1_PRIV_MAC,
  500. sizeof(SEOS_ADF1_PRIV_MAC),
  501. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  502. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  503. cmac);
  504. } else if(params->cipher == TWO_KEY_3DES_CBC_MODE) {
  505. uint8_t cryptogram_prefix[] = {0x85, des_cryptogram_length};
  506. bit_buffer_append_bytes(tx_buffer, cryptogram_prefix, sizeof(cryptogram_prefix));
  507. seos_emulator_des_adf_payload(credential, buffer);
  508. bit_buffer_append_bytes(tx_buffer, buffer, des_cryptogram_length);
  509. // +2 / -2 is to ignore iso14a framing
  510. des_cmac(
  511. SEOS_ADF1_PRIV_MAC,
  512. sizeof(SEOS_ADF1_PRIV_MAC),
  513. (uint8_t*)bit_buffer_get_data(tx_buffer) + prefix_len,
  514. bit_buffer_get_size_bytes(tx_buffer) - prefix_len,
  515. cmac);
  516. }
  517. uint8_t cmac_prefix[] = {0x8e, 0x08};
  518. bit_buffer_append_bytes(tx_buffer, cmac_prefix, sizeof(cmac_prefix));
  519. bit_buffer_append_bytes(tx_buffer, cmac, SEOS_WORKER_CMAC_SIZE);
  520. seos_log_bitbuffer(TAG, "Select ADF (0xcd02...)", tx_buffer);
  521. }
  522. NfcCommand seos_worker_listener_inspect_reader(Seos* seos) {
  523. SeosEmulator* seos_emulator = seos->seos_emulator;
  524. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  525. NfcCommand ret = NfcCommandContinue;
  526. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  527. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  528. uint8_t offset = NAD ? 2 : 1;
  529. // + x to skip stuff before APDU
  530. const uint8_t* apdu = rx_data + offset;
  531. if(memcmp(apdu, select_header, sizeof(select_header)) == 0) {
  532. if(memcmp(
  533. apdu + sizeof(select_header) + 1, OPERATION_SELECTOR, sizeof(OPERATION_SELECTOR)) ==
  534. 0) {
  535. uint8_t enableInspection[] = {
  536. 0x6f, 0x08, 0x85, 0x06, 0x02, 0x01, 0x40, 0x02, 0x01, 0x00};
  537. bit_buffer_append_bytes(tx_buffer, enableInspection, sizeof(enableInspection));
  538. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAIDSelected);
  539. } else {
  540. bit_buffer_append_bytes(tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  541. }
  542. } else if(bit_buffer_get_size_bytes(seos_emulator->rx_buffer) > (size_t)(offset + 2)) {
  543. FURI_LOG_I(TAG, "NFC stop; %d bytes", bit_buffer_get_size_bytes(seos_emulator->rx_buffer));
  544. ret = NfcCommandStop;
  545. }
  546. return ret;
  547. }
  548. NfcCommand seos_worker_listener_process_message(Seos* seos) {
  549. SeosEmulator* seos_emulator = seos->seos_emulator;
  550. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  551. NfcCommand ret = NfcCommandContinue;
  552. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  553. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  554. uint8_t offset = NAD ? 2 : 1;
  555. // + x to skip stuff before APDU
  556. const uint8_t* apdu = rx_data + offset;
  557. if(memcmp(apdu, select_header, sizeof(select_header)) == 0) {
  558. if(memcmp(apdu + sizeof(select_header) + 1, standard_seos_aid, sizeof(standard_seos_aid)) ==
  559. 0) {
  560. seos_emulator_select_aid(seos_emulator->tx_buffer);
  561. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAIDSelected);
  562. } else if(
  563. memcmp(
  564. apdu + sizeof(select_header) + 1,
  565. OPERATION_SELECTOR_POST_RESET,
  566. sizeof(OPERATION_SELECTOR_POST_RESET)) == 0) {
  567. FURI_LOG_I(TAG, "OPERATION_SELECTOR_POST_RESET");
  568. bit_buffer_append_bytes(
  569. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  570. } else if(
  571. memcmp(
  572. apdu + sizeof(select_header) + 1,
  573. OPERATION_SELECTOR,
  574. sizeof(OPERATION_SELECTOR)) == 0) {
  575. FURI_LOG_I(TAG, "OPERATION_SELECTOR");
  576. bit_buffer_append_bytes(
  577. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  578. } else if(
  579. memcmp(
  580. apdu + sizeof(select_header) + 1,
  581. MOBILE_SEOS_ADMIN_CARD,
  582. sizeof(MOBILE_SEOS_ADMIN_CARD)) == 0) {
  583. FURI_LOG_I(TAG, "MOBILE_SEOS_ADMIN_CARD");
  584. bit_buffer_append_bytes(
  585. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  586. } else {
  587. seos_log_bitbuffer(TAG, "Reject select", seos_emulator->rx_buffer);
  588. bit_buffer_append_bytes(
  589. seos_emulator->tx_buffer, (uint8_t*)FILE_NOT_FOUND, sizeof(FILE_NOT_FOUND));
  590. }
  591. } else if(memcmp(apdu, select_adf_header, sizeof(select_adf_header)) == 0) {
  592. // is our adf in the list?
  593. // +1 to skip APDU length byte
  594. void* p = memmem(
  595. apdu + sizeof(select_adf_header) + 1,
  596. apdu[sizeof(select_adf_header)],
  597. SEOS_ADF_OID,
  598. SEOS_ADF_OID_LEN);
  599. if(p) {
  600. BitBuffer* tmp = bit_buffer_alloc(SEOS_ADF_OID_LEN);
  601. bit_buffer_append_bytes(tmp, p, SEOS_ADF_OID_LEN);
  602. seos_log_bitbuffer(TAG, "Matched ADF", tmp);
  603. bit_buffer_free(tmp);
  604. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventADFMatched);
  605. seos_emulator_select_adf(
  606. &seos_emulator->params, seos_emulator->credential, seos_emulator->tx_buffer);
  607. } else {
  608. FURI_LOG_W(TAG, "Failed to match any ADF OID");
  609. }
  610. } else if(memcmp(apdu, general_authenticate_1, sizeof(general_authenticate_1)) == 0) {
  611. seos_emulator_general_authenticate_1(seos_emulator->tx_buffer, seos_emulator->params);
  612. } else if(memcmp(apdu, general_authenticate_2_header, sizeof(general_authenticate_2_header)) == 0) {
  613. if(!seos_emulator_general_authenticate_2(
  614. apdu,
  615. bit_buffer_get_size_bytes(seos_emulator->rx_buffer),
  616. seos_emulator->credential,
  617. &seos_emulator->params,
  618. seos_emulator->tx_buffer)) {
  619. FURI_LOG_W(TAG, "Failure in General Authenticate 2");
  620. ret = NfcCommandStop;
  621. return ret;
  622. }
  623. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventAuthenticated);
  624. // Prepare for future communication
  625. seos_emulator->secure_messaging = secure_messaging_alloc(&seos_emulator->params);
  626. } else if(memcmp(apdu, secure_messaging_header, sizeof(secure_messaging_header)) == 0) {
  627. uint8_t request_sio[] = {0x5c, 0x02, 0xff, 0x00};
  628. if(seos_emulator->secure_messaging) {
  629. FURI_LOG_D(TAG, "Unwrap secure message");
  630. // 0b00 0ccb3fff 16 8508fa8395d30de4e8e097008e085da7edbd833b002d00
  631. // Ignore 2 iso frame bytes
  632. size_t bytes_to_ignore = offset;
  633. BitBuffer* tmp = bit_buffer_alloc(bit_buffer_get_size_bytes(seos_emulator->rx_buffer));
  634. bit_buffer_append_bytes(
  635. tmp,
  636. bit_buffer_get_data(seos_emulator->rx_buffer) + bytes_to_ignore,
  637. bit_buffer_get_size_bytes(seos_emulator->rx_buffer) - bytes_to_ignore);
  638. seos_log_bitbuffer(TAG, "NFC received(wrapped)", tmp);
  639. secure_messaging_unwrap_apdu(seos_emulator->secure_messaging, tmp);
  640. seos_log_bitbuffer(TAG, "NFC received(clear)", tmp);
  641. const uint8_t* message = bit_buffer_get_data(tmp);
  642. if(memcmp(message, request_sio, sizeof(request_sio)) == 0) {
  643. view_dispatcher_send_custom_event(
  644. seos->view_dispatcher, SeosCustomEventSIORequested);
  645. BitBuffer* sio_file = bit_buffer_alloc(128);
  646. bit_buffer_append_bytes(sio_file, message + 2, 2); // fileId
  647. bit_buffer_append_byte(sio_file, seos_emulator->credential->sio_len);
  648. bit_buffer_append_bytes(
  649. sio_file, seos_emulator->credential->sio, seos_emulator->credential->sio_len);
  650. secure_messaging_wrap_rapdu(
  651. seos_emulator->secure_messaging,
  652. (uint8_t*)bit_buffer_get_data(sio_file),
  653. bit_buffer_get_size_bytes(sio_file),
  654. tx_buffer);
  655. bit_buffer_free(sio_file);
  656. }
  657. bit_buffer_free(tmp);
  658. } else {
  659. uint8_t no_sm[] = {0x69, 0x88};
  660. bit_buffer_append_bytes(tx_buffer, no_sm, sizeof(no_sm));
  661. }
  662. } else {
  663. // 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
  664. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventEmulate);
  665. }
  666. return ret;
  667. }
  668. NfcCommand seos_worker_listener_callback(NfcGenericEvent event, void* context) {
  669. furi_assert(context);
  670. furi_assert(event.protocol == NfcProtocolIso14443_4a);
  671. furi_assert(event.event_data);
  672. Seos* seos = context;
  673. SeosEmulator* seos_emulator = seos->seos_emulator;
  674. NfcCommand ret = NfcCommandContinue;
  675. Iso14443_4aListenerEvent* iso14443_4a_event = event.event_data;
  676. Iso14443_3aListener* iso14443_listener = event.instance;
  677. seos_emulator->iso14443_listener = iso14443_listener;
  678. BitBuffer* tx_buffer = seos_emulator->tx_buffer;
  679. bit_buffer_reset(tx_buffer);
  680. switch(iso14443_4a_event->type) {
  681. case Iso14443_4aListenerEventTypeReceivedData:
  682. seos_emulator->rx_buffer = iso14443_4a_event->data->buffer;
  683. const uint8_t* rx_data = bit_buffer_get_data(seos_emulator->rx_buffer);
  684. bool NAD = (rx_data[0] & NAD_MASK) == NAD_MASK;
  685. uint8_t offset = NAD ? 2 : 1;
  686. if(bit_buffer_get_size_bytes(iso14443_4a_event->data->buffer) == offset) {
  687. FURI_LOG_I(TAG, "No contents in frame");
  688. break;
  689. }
  690. seos_log_bitbuffer(TAG, "NFC received", seos_emulator->rx_buffer);
  691. // Some ISO14443a framing I need to figure out
  692. bit_buffer_append_bytes(tx_buffer, rx_data, offset);
  693. if(seos->flow_mode == FLOW_CRED) {
  694. ret = seos_worker_listener_process_message(seos);
  695. } else if(seos->flow_mode == FLOW_INSPECT) {
  696. ret = seos_worker_listener_inspect_reader(seos);
  697. }
  698. if(bit_buffer_get_size_bytes(seos_emulator->tx_buffer) >
  699. offset) { // contents belong iso framing
  700. if(memcmp(
  701. FILE_NOT_FOUND,
  702. bit_buffer_get_data(tx_buffer) + bit_buffer_get_size_bytes(tx_buffer) -
  703. sizeof(FILE_NOT_FOUND),
  704. sizeof(FILE_NOT_FOUND)) != 0) {
  705. bit_buffer_append_bytes(tx_buffer, success, sizeof(success));
  706. }
  707. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  708. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  709. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  710. if(error != NfcErrorNone) {
  711. FURI_LOG_W(TAG, "Tx error: %d", error);
  712. break;
  713. }
  714. } else {
  715. iso14443_crc_append(Iso14443CrcTypeA, tx_buffer);
  716. seos_log_bitbuffer(TAG, "NFC transmit", seos_emulator->tx_buffer);
  717. NfcError error = nfc_listener_tx((Nfc*)iso14443_listener, tx_buffer);
  718. if(error != NfcErrorNone) {
  719. FURI_LOG_W(TAG, "Tx error: %d", error);
  720. break;
  721. }
  722. }
  723. break;
  724. case Iso14443_4aListenerEventTypeHalted:
  725. FURI_LOG_I(TAG, "Halted");
  726. break;
  727. case Iso14443_4aListenerEventTypeFieldOff:
  728. FURI_LOG_I(TAG, "Field Off");
  729. break;
  730. }
  731. if(ret == NfcCommandStop) {
  732. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventReaderError);
  733. }
  734. return ret;
  735. }