u2f.c 10 KB

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