u2f_data.c 14 KB

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