u2f.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <furi.h>
  2. #include "u2f.h"
  3. #include "u2f_hid.h"
  4. #include "u2f_data.h"
  5. #include <furi_hal.h>
  6. #include <furi_hal_random.h>
  7. #include <littlefs/lfs_util.h> // for lfs_tobe32
  8. #include "toolbox/sha256.h"
  9. #include "toolbox/hmac_sha256.h"
  10. #include "micro-ecc/uECC.h"
  11. #define TAG "U2F"
  12. #define WORKER_TAG TAG "Worker"
  13. #define U2F_CMD_REGISTER 0x01
  14. #define U2F_CMD_AUTHENTICATE 0x02
  15. #define U2F_CMD_VERSION 0x03
  16. typedef enum {
  17. U2fCheckOnly = 0x07, // "check-only" - only check key handle, don't send auth response
  18. U2fEnforce =
  19. 0x03, // "enforce-user-presence-and-sign" - send auth response only if user is present
  20. U2fDontEnforce =
  21. 0x08, // "dont-enforce-user-presence-and-sign" - send auth response even if user is missing
  22. } U2fAuthMode;
  23. typedef struct {
  24. uint8_t format;
  25. uint8_t xy[64];
  26. } __attribute__((packed)) U2fPubKey;
  27. typedef struct {
  28. uint8_t len;
  29. uint8_t hash[32];
  30. uint8_t nonce[32];
  31. } __attribute__((packed)) U2fKeyHandle;
  32. typedef struct {
  33. uint8_t cla;
  34. uint8_t ins;
  35. uint8_t p1;
  36. uint8_t p2;
  37. uint8_t len[3];
  38. uint8_t challenge[32];
  39. uint8_t app_id[32];
  40. } __attribute__((packed)) U2fRegisterReq;
  41. typedef struct {
  42. uint8_t reserved;
  43. U2fPubKey pub_key;
  44. U2fKeyHandle key_handle;
  45. uint8_t cert[];
  46. } __attribute__((packed)) U2fRegisterResp;
  47. typedef struct {
  48. uint8_t cla;
  49. uint8_t ins;
  50. uint8_t p1;
  51. uint8_t p2;
  52. uint8_t len[3];
  53. uint8_t challenge[32];
  54. uint8_t app_id[32];
  55. U2fKeyHandle key_handle;
  56. } __attribute__((packed)) U2fAuthReq;
  57. typedef struct {
  58. uint8_t user_present;
  59. uint32_t counter;
  60. uint8_t signature[];
  61. } __attribute__((packed)) U2fAuthResp;
  62. static const uint8_t ver_str[] = {"U2F_V2"};
  63. static const uint8_t state_no_error[] = {0x90, 0x00};
  64. static const uint8_t state_not_supported[] = {0x6D, 0x00};
  65. static const uint8_t state_user_missing[] = {0x69, 0x85};
  66. static const uint8_t state_wrong_data[] = {0x6A, 0x80};
  67. struct U2fData {
  68. uint8_t device_key[32];
  69. uint8_t cert_key[32];
  70. uint32_t counter;
  71. const struct uECC_Curve_t* p_curve;
  72. bool ready;
  73. bool user_present;
  74. U2fEvtCallback callback;
  75. void* context;
  76. };
  77. static int u2f_uecc_random(uint8_t* dest, unsigned size) {
  78. furi_hal_random_fill_buf(dest, size);
  79. return 1;
  80. }
  81. U2fData* u2f_alloc() {
  82. return malloc(sizeof(U2fData));
  83. }
  84. void u2f_free(U2fData* U2F) {
  85. furi_assert(U2F);
  86. free(U2F);
  87. }
  88. bool u2f_init(U2fData* U2F) {
  89. furi_assert(U2F);
  90. if(u2f_data_cert_check() == false) {
  91. FURI_LOG_E(TAG, "Certificate load error");
  92. return false;
  93. }
  94. if(u2f_data_cert_key_load(U2F->cert_key) == false) {
  95. FURI_LOG_E(TAG, "Certificate key load error");
  96. return false;
  97. }
  98. if(u2f_data_key_load(U2F->device_key) == false) {
  99. FURI_LOG_W(TAG, "Key loading error, generating new");
  100. if(u2f_data_key_generate(U2F->device_key) == false) {
  101. FURI_LOG_E(TAG, "Key write failed");
  102. return false;
  103. }
  104. }
  105. if(u2f_data_cnt_read(&U2F->counter) == false) {
  106. FURI_LOG_W(TAG, "Counter loading error, resetting counter");
  107. U2F->counter = 0;
  108. if(u2f_data_cnt_write(0) == false) {
  109. FURI_LOG_E(TAG, "Counter write failed");
  110. return false;
  111. }
  112. }
  113. U2F->p_curve = uECC_secp256r1();
  114. uECC_set_rng(u2f_uecc_random);
  115. U2F->ready = true;
  116. return true;
  117. }
  118. void u2f_set_event_callback(U2fData* U2F, U2fEvtCallback callback, void* context) {
  119. furi_assert(U2F);
  120. furi_assert(callback);
  121. U2F->callback = callback;
  122. U2F->context = context;
  123. }
  124. void u2f_confirm_user_present(U2fData* U2F) {
  125. U2F->user_present = true;
  126. }
  127. static uint8_t u2f_der_encode_int(uint8_t* der, uint8_t* val, uint8_t val_len) {
  128. der[0] = 0x02; // Integer
  129. uint8_t len = 2;
  130. // Omit leading zeros
  131. while(val[0] == 0 && val_len > 0) {
  132. ++val;
  133. --val_len;
  134. }
  135. // Check if integer is negative
  136. if(val[0] > 0x7f) der[len++] = 0;
  137. memcpy(der + len, val, val_len);
  138. len += val_len;
  139. der[1] = len - 2;
  140. return len;
  141. }
  142. static uint8_t u2f_der_encode_signature(uint8_t* der, uint8_t* sig) {
  143. der[0] = 0x30;
  144. uint8_t len = 2;
  145. len += u2f_der_encode_int(der + len, sig, 32);
  146. len += u2f_der_encode_int(der + len, sig + 32, 32);
  147. der[1] = len - 2;
  148. return len;
  149. }
  150. static uint16_t u2f_register(U2fData* U2F, uint8_t* buf) {
  151. U2fRegisterReq* req = (U2fRegisterReq*)buf;
  152. U2fRegisterResp* resp = (U2fRegisterResp*)buf;
  153. U2fKeyHandle handle;
  154. uint8_t private[32];
  155. U2fPubKey pub_key;
  156. uint8_t hash[32];
  157. uint8_t signature[64];
  158. if(u2f_data_check(false) == false) {
  159. U2F->ready = false;
  160. if(U2F->callback != NULL) U2F->callback(U2fNotifyError, U2F->context);
  161. memcpy(&buf[0], state_not_supported, 2);
  162. return 2;
  163. }
  164. if(U2F->callback != NULL) U2F->callback(U2fNotifyRegister, U2F->context);
  165. if(U2F->user_present == false) {
  166. memcpy(&buf[0], state_user_missing, 2);
  167. return 2;
  168. }
  169. U2F->user_present = false;
  170. hmac_sha256_context hmac_ctx;
  171. sha256_context sha_ctx;
  172. handle.len = 32 * 2;
  173. // Generate random nonce
  174. furi_hal_random_fill_buf(handle.nonce, 32);
  175. // Generate private key
  176. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  177. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  178. hmac_sha256_update(&hmac_ctx, handle.nonce, 32);
  179. hmac_sha256_finish(&hmac_ctx, U2F->device_key, private);
  180. // Generate private key handle
  181. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  182. hmac_sha256_update(&hmac_ctx, private, 32);
  183. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  184. hmac_sha256_finish(&hmac_ctx, U2F->device_key, handle.hash);
  185. // Generate public key
  186. pub_key.format = 0x04; // Uncompressed point
  187. uECC_compute_public_key(private, pub_key.xy, U2F->p_curve);
  188. // Generate signature
  189. uint8_t reserved_byte = 0;
  190. sha256_start(&sha_ctx);
  191. sha256_update(&sha_ctx, &reserved_byte, 1);
  192. sha256_update(&sha_ctx, req->app_id, 32);
  193. sha256_update(&sha_ctx, req->challenge, 32);
  194. sha256_update(&sha_ctx, handle.hash, handle.len);
  195. sha256_update(&sha_ctx, (uint8_t*)&pub_key, 65);
  196. sha256_finish(&sha_ctx, hash);
  197. uECC_sign(U2F->cert_key, hash, 32, signature, U2F->p_curve);
  198. // Encode response message
  199. resp->reserved = 0x05;
  200. memcpy(&(resp->pub_key), &pub_key, sizeof(U2fPubKey));
  201. memcpy(&(resp->key_handle), &handle, sizeof(U2fKeyHandle));
  202. uint32_t cert_len = u2f_data_cert_load(resp->cert);
  203. uint8_t signature_len = u2f_der_encode_signature(resp->cert + cert_len, signature);
  204. memcpy(resp->cert + cert_len + signature_len, state_no_error, 2);
  205. return (sizeof(U2fRegisterResp) + cert_len + signature_len + 2);
  206. }
  207. static uint16_t u2f_authenticate(U2fData* U2F, uint8_t* buf) {
  208. U2fAuthReq* req = (U2fAuthReq*)buf;
  209. U2fAuthResp* resp = (U2fAuthResp*)buf;
  210. uint8_t priv_key[32];
  211. uint8_t mac_control[32];
  212. hmac_sha256_context hmac_ctx;
  213. sha256_context sha_ctx;
  214. uint8_t flags = 0;
  215. uint8_t hash[32];
  216. uint8_t signature[64];
  217. uint32_t be_u2f_counter;
  218. if(u2f_data_check(false) == false) {
  219. U2F->ready = false;
  220. if(U2F->callback != NULL) U2F->callback(U2fNotifyError, U2F->context);
  221. memcpy(&buf[0], state_not_supported, 2);
  222. return 2;
  223. }
  224. if(U2F->callback != NULL) U2F->callback(U2fNotifyAuth, U2F->context);
  225. if(U2F->user_present == true) {
  226. flags |= 1;
  227. } else {
  228. if(req->p1 == U2fEnforce) {
  229. memcpy(&buf[0], state_user_missing, 2);
  230. return 2;
  231. }
  232. }
  233. U2F->user_present = false;
  234. // The 4 byte counter is represented in big endian. Increment it before use
  235. be_u2f_counter = lfs_tobe32(U2F->counter + 1);
  236. // Generate hash
  237. sha256_start(&sha_ctx);
  238. sha256_update(&sha_ctx, req->app_id, 32);
  239. sha256_update(&sha_ctx, &flags, 1);
  240. sha256_update(&sha_ctx, (uint8_t*)&(be_u2f_counter), 4);
  241. sha256_update(&sha_ctx, req->challenge, 32);
  242. sha256_finish(&sha_ctx, hash);
  243. // Recover private key
  244. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  245. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  246. hmac_sha256_update(&hmac_ctx, req->key_handle.nonce, 32);
  247. hmac_sha256_finish(&hmac_ctx, U2F->device_key, priv_key);
  248. // Generate and verify private key handle
  249. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  250. hmac_sha256_update(&hmac_ctx, priv_key, 32);
  251. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  252. hmac_sha256_finish(&hmac_ctx, U2F->device_key, mac_control);
  253. if(memcmp(req->key_handle.hash, mac_control, 32) != 0) {
  254. FURI_LOG_W(TAG, "Wrong handle!");
  255. memcpy(&buf[0], state_wrong_data, 2);
  256. return 2;
  257. }
  258. if(req->p1 == U2fCheckOnly) { // Check-only: don't need to send full response
  259. memcpy(&buf[0], state_user_missing, 2);
  260. return 2;
  261. }
  262. uECC_sign(priv_key, hash, 32, signature, U2F->p_curve);
  263. resp->user_present = flags;
  264. resp->counter = be_u2f_counter;
  265. uint8_t signature_len = u2f_der_encode_signature(resp->signature, signature);
  266. memcpy(resp->signature + signature_len, state_no_error, 2);
  267. U2F->counter++;
  268. FURI_LOG_D(TAG, "Counter: %lu", U2F->counter);
  269. u2f_data_cnt_write(U2F->counter);
  270. if(U2F->callback != NULL) U2F->callback(U2fNotifyAuthSuccess, U2F->context);
  271. return (sizeof(U2fAuthResp) + signature_len + 2);
  272. }
  273. uint16_t u2f_msg_parse(U2fData* U2F, uint8_t* buf, uint16_t len) {
  274. furi_assert(U2F);
  275. if(!U2F->ready) return 0;
  276. if((buf[0] != 0x00) && (len < 5)) return 0;
  277. if(buf[1] == U2F_CMD_REGISTER) { // Register request
  278. return u2f_register(U2F, buf);
  279. } else if(buf[1] == U2F_CMD_AUTHENTICATE) { // Authenticate request
  280. return u2f_authenticate(U2F, buf);
  281. } else if(buf[1] == U2F_CMD_VERSION) { // Get U2F version string
  282. memcpy(&buf[0], ver_str, 6);
  283. memcpy(&buf[6], state_no_error, 2);
  284. return 8;
  285. } else {
  286. memcpy(&buf[0], state_not_supported, 2);
  287. return 2;
  288. }
  289. return 0;
  290. }
  291. void u2f_wink(U2fData* U2F) {
  292. if(U2F->callback != NULL) U2F->callback(U2fNotifyWink, U2F->context);
  293. }
  294. void u2f_set_state(U2fData* U2F, uint8_t state) {
  295. if(state == 0) {
  296. if(U2F->callback != NULL) U2F->callback(U2fNotifyDisconnect, U2F->context);
  297. } else {
  298. if(U2F->callback != NULL) U2F->callback(U2fNotifyConnect, U2F->context);
  299. }
  300. U2F->user_present = false;
  301. }