u2f_data.c 14 KB

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