u2f_data.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include <furi.h>
  2. #include "u2f_hid.h"
  3. #include <furi_hal.h>
  4. #include <storage/storage.h>
  5. #include <furi_hal_random.h>
  6. #include <flipper_file.h>
  7. #define TAG "U2F"
  8. #define U2F_DATA_FOLDER "/any/u2f/"
  9. #define U2F_CERT_FILE U2F_DATA_FOLDER "assets/cert.der"
  10. #define U2F_CERT_KEY_FILE U2F_DATA_FOLDER "assets/cert_key.u2f"
  11. #define U2F_KEY_FILE U2F_DATA_FOLDER "key.u2f"
  12. #define U2F_CNT_FILE U2F_DATA_FOLDER "cnt.u2f"
  13. #define U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_FACTORY 2
  14. #define U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE 11
  15. #define U2F_CERT_STOCK 0 // Stock certificate, private key is encrypted with factory key
  16. #define U2F_CERT_USER 1 // User certificate, private key is encrypted with unique key
  17. #define U2F_CERT_USER_UNENCRYPTED \
  18. 2 // Unencrypted user certificate, will be encrypted after first load
  19. #define U2F_CERT_KEY_FILE_TYPE "Flipper U2F Certificate Key File"
  20. #define U2F_CERT_KEY_VERSION 1
  21. #define U2F_DEVICE_KEY_FILE_TYPE "Flipper U2F Device Key File"
  22. #define U2F_DEVICE_KEY_VERSION 1
  23. #define U2F_COUNTER_FILE_TYPE "Flipper U2F Counter File"
  24. #define U2F_COUNTER_VERSION 1
  25. #define U2F_COUNTER_CONTROL_VAL 0xAA5500FF
  26. typedef struct {
  27. uint32_t counter;
  28. uint8_t random_salt[24];
  29. uint32_t control;
  30. } __attribute__((packed)) U2fCounterData;
  31. bool u2f_data_check(bool cert_only) {
  32. bool state = false;
  33. Storage* fs_api = furi_record_open("storage");
  34. File* file = storage_file_alloc(fs_api);
  35. do {
  36. if(!storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  37. storage_file_close(file);
  38. if(!storage_file_open(file, U2F_CERT_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  39. if(cert_only) {
  40. state = true;
  41. break;
  42. }
  43. storage_file_close(file);
  44. if(!storage_file_open(file, U2F_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  45. storage_file_close(file);
  46. if(!storage_file_open(file, U2F_CNT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  47. state = true;
  48. } while(0);
  49. storage_file_close(file);
  50. storage_file_free(file);
  51. furi_record_close("storage");
  52. return state;
  53. }
  54. bool u2f_data_cert_check() {
  55. bool state = false;
  56. Storage* fs_api = furi_record_open("storage");
  57. File* file = storage_file_alloc(fs_api);
  58. uint8_t file_buf[8];
  59. if(storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
  60. do {
  61. // Read header to check certificate size
  62. size_t file_size = storage_file_size(file);
  63. size_t len_cur = storage_file_read(file, file_buf, 4);
  64. if(len_cur != 4) break;
  65. if(file_buf[0] != 0x30) {
  66. FURI_LOG_E(TAG, "Wrong certificate header");
  67. break;
  68. }
  69. size_t temp_len = ((file_buf[2] << 8) | (file_buf[3])) + 4;
  70. if(temp_len != file_size) {
  71. FURI_LOG_E(TAG, "Wrong certificate length");
  72. break;
  73. }
  74. state = true;
  75. } while(0);
  76. }
  77. storage_file_close(file);
  78. storage_file_free(file);
  79. furi_record_close("storage");
  80. return state;
  81. }
  82. uint32_t u2f_data_cert_load(uint8_t* cert) {
  83. furi_assert(cert);
  84. Storage* fs_api = furi_record_open("storage");
  85. File* file = storage_file_alloc(fs_api);
  86. uint32_t file_size = 0;
  87. uint32_t len_cur = 0;
  88. if(storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
  89. file_size = storage_file_size(file);
  90. len_cur = storage_file_read(file, cert, file_size);
  91. if(len_cur != file_size) len_cur = 0;
  92. }
  93. storage_file_close(file);
  94. storage_file_free(file);
  95. furi_record_close("storage");
  96. return len_cur;
  97. }
  98. static bool u2f_data_cert_key_encrypt(uint8_t* cert_key) {
  99. furi_assert(cert_key);
  100. bool state = false;
  101. uint8_t iv[16];
  102. uint8_t key[48];
  103. uint32_t cert_type = U2F_CERT_USER;
  104. FURI_LOG_I(TAG, "Encrypting user cert key");
  105. // Generate random IV
  106. furi_hal_random_fill_buf(iv, 16);
  107. if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
  108. FURI_LOG_E(TAG, "Unable to load encryption key");
  109. return false;
  110. }
  111. if(!furi_hal_crypto_encrypt(cert_key, key, 32)) {
  112. FURI_LOG_E(TAG, "Encryption failed");
  113. return false;
  114. }
  115. furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
  116. Storage* storage = furi_record_open("storage");
  117. FlipperFile* flipper_file = flipper_file_alloc(storage);
  118. if(flipper_file_open_always(flipper_file, U2F_CERT_KEY_FILE)) {
  119. do {
  120. if(!flipper_file_write_header_cstr(
  121. flipper_file, U2F_CERT_KEY_FILE_TYPE, U2F_CERT_KEY_VERSION))
  122. break;
  123. if(!flipper_file_write_uint32(flipper_file, "Type", &cert_type, 1)) break;
  124. if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) break;
  125. if(!flipper_file_write_hex(flipper_file, "Data", key, 48)) break;
  126. state = true;
  127. } while(0);
  128. }
  129. flipper_file_close(flipper_file);
  130. flipper_file_free(flipper_file);
  131. furi_record_close("storage");
  132. return state;
  133. }
  134. bool u2f_data_cert_key_load(uint8_t* cert_key) {
  135. furi_assert(cert_key);
  136. bool state = false;
  137. uint8_t iv[16];
  138. uint8_t key[48];
  139. uint32_t cert_type = 0;
  140. uint8_t key_slot = 0;
  141. uint32_t version = 0;
  142. // Check if unique key exists in secure eclave and generate it if missing
  143. if(!furi_hal_crypto_verify_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE)) return false;
  144. string_t filetype;
  145. string_init(filetype);
  146. Storage* storage = furi_record_open("storage");
  147. FlipperFile* flipper_file = flipper_file_alloc(storage);
  148. if(flipper_file_open_existing(flipper_file, U2F_CERT_KEY_FILE)) {
  149. do {
  150. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  151. FURI_LOG_E(TAG, "Missing or incorrect header");
  152. break;
  153. }
  154. if(strcmp(string_get_cstr(filetype), U2F_CERT_KEY_FILE_TYPE) != 0 ||
  155. version != U2F_CERT_KEY_VERSION) {
  156. FURI_LOG_E(TAG, "Type or version mismatch");
  157. break;
  158. }
  159. if(!flipper_file_read_uint32(flipper_file, "Type", &cert_type, 1)) {
  160. FURI_LOG_E(TAG, "Missing cert type");
  161. break;
  162. }
  163. if(cert_type == U2F_CERT_STOCK) {
  164. key_slot = U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_FACTORY;
  165. } else if(cert_type == U2F_CERT_USER) {
  166. key_slot = U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE;
  167. } else if(cert_type == U2F_CERT_USER_UNENCRYPTED) {
  168. key_slot = 0;
  169. } else {
  170. FURI_LOG_E(TAG, "Unknown cert type");
  171. break;
  172. }
  173. if(key_slot != 0) {
  174. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  175. FURI_LOG_E(TAG, "Missing IV");
  176. break;
  177. }
  178. if(!flipper_file_read_hex(flipper_file, "Data", key, 48)) {
  179. FURI_LOG_E(TAG, "Missing data");
  180. break;
  181. }
  182. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  183. FURI_LOG_E(TAG, "Unable to load encryption key");
  184. break;
  185. }
  186. memset(cert_key, 0, 32);
  187. if(!furi_hal_crypto_decrypt(key, cert_key, 32)) {
  188. memset(cert_key, 0, 32);
  189. FURI_LOG_E(TAG, "Decryption failed");
  190. break;
  191. }
  192. furi_hal_crypto_store_unload_key(key_slot);
  193. } else {
  194. if(!flipper_file_read_hex(flipper_file, "Data", cert_key, 32)) {
  195. FURI_LOG_E(TAG, "Missing data");
  196. break;
  197. }
  198. }
  199. state = true;
  200. } while(0);
  201. }
  202. flipper_file_close(flipper_file);
  203. flipper_file_free(flipper_file);
  204. furi_record_close("storage");
  205. string_clear(filetype);
  206. if(cert_type == U2F_CERT_USER_UNENCRYPTED) {
  207. return u2f_data_cert_key_encrypt(cert_key);
  208. }
  209. return state;
  210. }
  211. bool u2f_data_key_load(uint8_t* device_key) {
  212. furi_assert(device_key);
  213. bool state = false;
  214. uint8_t iv[16];
  215. uint8_t key[48];
  216. uint32_t version = 0;
  217. string_t filetype;
  218. string_init(filetype);
  219. Storage* storage = furi_record_open("storage");
  220. FlipperFile* flipper_file = flipper_file_alloc(storage);
  221. if(flipper_file_open_existing(flipper_file, U2F_KEY_FILE)) {
  222. do {
  223. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  224. FURI_LOG_E(TAG, "Missing or incorrect header");
  225. break;
  226. }
  227. if(strcmp(string_get_cstr(filetype), U2F_DEVICE_KEY_FILE_TYPE) != 0 ||
  228. version != U2F_DEVICE_KEY_VERSION) {
  229. FURI_LOG_E(TAG, "Type or version mismatch");
  230. break;
  231. }
  232. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  233. FURI_LOG_E(TAG, "Missing IV");
  234. break;
  235. }
  236. if(!flipper_file_read_hex(flipper_file, "Data", key, 48)) {
  237. FURI_LOG_E(TAG, "Missing data");
  238. break;
  239. }
  240. if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
  241. FURI_LOG_E(TAG, "Unable to load encryption key");
  242. break;
  243. }
  244. memset(device_key, 0, 32);
  245. if(!furi_hal_crypto_decrypt(key, device_key, 32)) {
  246. memset(device_key, 0, 32);
  247. FURI_LOG_E(TAG, "Decryption failed");
  248. break;
  249. }
  250. furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
  251. state = true;
  252. } while(0);
  253. }
  254. flipper_file_close(flipper_file);
  255. flipper_file_free(flipper_file);
  256. furi_record_close("storage");
  257. string_clear(filetype);
  258. return state;
  259. }
  260. bool u2f_data_key_generate(uint8_t* device_key) {
  261. furi_assert(device_key);
  262. bool state = false;
  263. uint8_t iv[16];
  264. uint8_t key[32];
  265. uint8_t key_encrypted[48];
  266. // Generate random IV and key
  267. furi_hal_random_fill_buf(iv, 16);
  268. furi_hal_random_fill_buf(key, 32);
  269. if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
  270. FURI_LOG_E(TAG, "Unable to load encryption key");
  271. return false;
  272. }
  273. if(!furi_hal_crypto_encrypt(key, key_encrypted, 32)) {
  274. FURI_LOG_E(TAG, "Encryption failed");
  275. return false;
  276. }
  277. furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
  278. Storage* storage = furi_record_open("storage");
  279. FlipperFile* flipper_file = flipper_file_alloc(storage);
  280. if(flipper_file_open_always(flipper_file, U2F_KEY_FILE)) {
  281. do {
  282. if(!flipper_file_write_header_cstr(
  283. flipper_file, U2F_DEVICE_KEY_FILE_TYPE, U2F_DEVICE_KEY_VERSION))
  284. break;
  285. if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) break;
  286. if(!flipper_file_write_hex(flipper_file, "Data", key_encrypted, 48)) break;
  287. state = true;
  288. memcpy(device_key, key, 32);
  289. } while(0);
  290. }
  291. flipper_file_close(flipper_file);
  292. flipper_file_free(flipper_file);
  293. furi_record_close("storage");
  294. return state;
  295. }
  296. bool u2f_data_cnt_read(uint32_t* cnt_val) {
  297. furi_assert(cnt_val);
  298. bool state = false;
  299. uint8_t iv[16];
  300. U2fCounterData cnt;
  301. uint8_t cnt_encr[48];
  302. uint32_t version = 0;
  303. string_t filetype;
  304. string_init(filetype);
  305. Storage* storage = furi_record_open("storage");
  306. FlipperFile* flipper_file = flipper_file_alloc(storage);
  307. if(flipper_file_open_existing(flipper_file, U2F_CNT_FILE)) {
  308. do {
  309. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  310. FURI_LOG_E(TAG, "Missing or incorrect header");
  311. break;
  312. }
  313. if(strcmp(string_get_cstr(filetype), U2F_COUNTER_FILE_TYPE) != 0 ||
  314. version != U2F_COUNTER_VERSION) {
  315. FURI_LOG_E(TAG, "Type or version mismatch");
  316. break;
  317. }
  318. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  319. FURI_LOG_E(TAG, "Missing IV");
  320. break;
  321. }
  322. if(!flipper_file_read_hex(flipper_file, "Data", cnt_encr, 48)) {
  323. FURI_LOG_E(TAG, "Missing data");
  324. break;
  325. }
  326. if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
  327. FURI_LOG_E(TAG, "Unable to load encryption key");
  328. break;
  329. }
  330. memset(&cnt, 0, 32);
  331. if(!furi_hal_crypto_decrypt(cnt_encr, (uint8_t*)&cnt, 32)) {
  332. memset(&cnt, 0, 32);
  333. FURI_LOG_E(TAG, "Decryption failed");
  334. break;
  335. }
  336. furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
  337. if(cnt.control == U2F_COUNTER_CONTROL_VAL) {
  338. *cnt_val = cnt.counter;
  339. state = true;
  340. }
  341. } while(0);
  342. }
  343. flipper_file_close(flipper_file);
  344. flipper_file_free(flipper_file);
  345. furi_record_close("storage");
  346. string_clear(filetype);
  347. return state;
  348. }
  349. bool u2f_data_cnt_write(uint32_t cnt_val) {
  350. bool state = false;
  351. uint8_t iv[16];
  352. U2fCounterData cnt;
  353. uint8_t cnt_encr[48];
  354. // Generate random IV and key
  355. furi_hal_random_fill_buf(iv, 16);
  356. furi_hal_random_fill_buf(cnt.random_salt, 24);
  357. cnt.control = U2F_COUNTER_CONTROL_VAL;
  358. cnt.counter = cnt_val;
  359. if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
  360. FURI_LOG_E(TAG, "Unable to load encryption key");
  361. return false;
  362. }
  363. if(!furi_hal_crypto_encrypt((uint8_t*)&cnt, cnt_encr, 32)) {
  364. FURI_LOG_E(TAG, "Encryption failed");
  365. return false;
  366. }
  367. furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
  368. Storage* storage = furi_record_open("storage");
  369. FlipperFile* flipper_file = flipper_file_alloc(storage);
  370. if(flipper_file_open_always(flipper_file, U2F_CNT_FILE)) {
  371. do {
  372. if(!flipper_file_write_header_cstr(
  373. flipper_file, U2F_COUNTER_FILE_TYPE, U2F_COUNTER_VERSION))
  374. break;
  375. if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) break;
  376. if(!flipper_file_write_hex(flipper_file, "Data", cnt_encr, 48)) break;
  377. state = true;
  378. } while(0);
  379. }
  380. flipper_file_close(flipper_file);
  381. flipper_file_free(flipper_file);
  382. furi_record_close("storage");
  383. return state;
  384. }