u2f_data.c 12 KB

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