u2f.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 furi_alloc(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->callback != NULL) U2F->callback(U2fNotifyRegister, U2F->context);
  158. if(U2F->user_present == false) {
  159. memcpy(&buf[0], state_user_missing, 2);
  160. return 2;
  161. }
  162. U2F->user_present = false;
  163. hmac_sha256_context hmac_ctx;
  164. sha256_context sha_ctx;
  165. handle.len = 32 * 2;
  166. // Generate random nonce
  167. furi_hal_random_fill_buf(handle.nonce, 32);
  168. // Generate private key
  169. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  170. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  171. hmac_sha256_update(&hmac_ctx, handle.nonce, 32);
  172. hmac_sha256_finish(&hmac_ctx, U2F->device_key, private);
  173. // Generate private key handle
  174. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  175. hmac_sha256_update(&hmac_ctx, private, 32);
  176. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  177. hmac_sha256_finish(&hmac_ctx, U2F->device_key, handle.hash);
  178. // Generate public key
  179. pub_key.format = 0x04; // Uncompressed point
  180. uECC_compute_public_key(private, pub_key.xy, U2F->p_curve);
  181. // Generate signature
  182. uint8_t reserved_byte = 0;
  183. sha256_start(&sha_ctx);
  184. sha256_update(&sha_ctx, &reserved_byte, 1);
  185. sha256_update(&sha_ctx, req->app_id, 32);
  186. sha256_update(&sha_ctx, req->challenge, 32);
  187. sha256_update(&sha_ctx, handle.hash, handle.len);
  188. sha256_update(&sha_ctx, (uint8_t*)&pub_key, 65);
  189. sha256_finish(&sha_ctx, hash);
  190. uECC_sign(U2F->cert_key, hash, 32, signature, U2F->p_curve);
  191. // Encode response message
  192. resp->reserved = 0x05;
  193. memcpy(&(resp->pub_key), &pub_key, sizeof(U2fPubKey));
  194. memcpy(&(resp->key_handle), &handle, sizeof(U2fKeyHandle));
  195. uint32_t cert_len = u2f_data_cert_load(resp->cert);
  196. uint8_t signature_len = u2f_der_encode_signature(resp->cert + cert_len, signature);
  197. memcpy(resp->cert + cert_len + signature_len, state_no_error, 2);
  198. return (sizeof(U2fRegisterResp) + cert_len + signature_len + 2);
  199. }
  200. static uint16_t u2f_authenticate(U2fData* U2F, uint8_t* buf) {
  201. U2fAuthReq* req = (U2fAuthReq*)buf;
  202. U2fAuthResp* resp = (U2fAuthResp*)buf;
  203. uint8_t priv_key[32];
  204. uint8_t mac_control[32];
  205. hmac_sha256_context hmac_ctx;
  206. sha256_context sha_ctx;
  207. uint8_t flags = 0;
  208. uint8_t hash[32];
  209. uint8_t signature[64];
  210. if(U2F->callback != NULL) U2F->callback(U2fNotifyAuth, U2F->context);
  211. if(U2F->user_present == true) {
  212. flags |= 1;
  213. } else {
  214. if(req->p1 == U2fEnforce) {
  215. memcpy(&buf[0], state_user_missing, 2);
  216. return 2;
  217. }
  218. }
  219. U2F->user_present = false;
  220. // Generate hash
  221. sha256_start(&sha_ctx);
  222. sha256_update(&sha_ctx, req->app_id, 32);
  223. sha256_update(&sha_ctx, &flags, 1);
  224. sha256_update(&sha_ctx, (uint8_t*)&(U2F->counter), 4);
  225. sha256_update(&sha_ctx, req->challenge, 32);
  226. sha256_finish(&sha_ctx, hash);
  227. // Recover private key
  228. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  229. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  230. hmac_sha256_update(&hmac_ctx, req->key_handle.nonce, 32);
  231. hmac_sha256_finish(&hmac_ctx, U2F->device_key, priv_key);
  232. // Generate and verify private key handle
  233. hmac_sha256_init(&hmac_ctx, U2F->device_key);
  234. hmac_sha256_update(&hmac_ctx, priv_key, 32);
  235. hmac_sha256_update(&hmac_ctx, req->app_id, 32);
  236. hmac_sha256_finish(&hmac_ctx, U2F->device_key, mac_control);
  237. if(memcmp(req->key_handle.hash, mac_control, 32) != 0) {
  238. FURI_LOG_W(TAG, "Wrong handle!");
  239. memcpy(&buf[0], state_wrong_data, 2);
  240. return 2;
  241. }
  242. if(req->p1 == U2fCheckOnly) { // Check-only: don't need to send full response
  243. memcpy(&buf[0], state_user_missing, 2);
  244. return 2;
  245. }
  246. uECC_sign(priv_key, hash, 32, signature, U2F->p_curve);
  247. resp->user_present = flags;
  248. resp->counter = U2F->counter;
  249. uint8_t signature_len = u2f_der_encode_signature(resp->signature, signature);
  250. memcpy(resp->signature + signature_len, state_no_error, 2);
  251. FURI_LOG_I(TAG, "Counter: %lu", U2F->counter);
  252. U2F->counter++;
  253. u2f_data_cnt_write(U2F->counter);
  254. return (sizeof(U2fAuthResp) + signature_len + 2);
  255. }
  256. uint16_t u2f_msg_parse(U2fData* U2F, uint8_t* buf, uint16_t len) {
  257. furi_assert(U2F);
  258. if(!U2F->ready) return 0;
  259. if((buf[0] != 0x00) && (len < 5)) return 0;
  260. if(buf[1] == U2F_CMD_REGISTER) { // Register request
  261. return u2f_register(U2F, buf);
  262. } else if(buf[1] == U2F_CMD_AUTHENTICATE) { // Authenticate request
  263. return u2f_authenticate(U2F, buf);
  264. } else if(buf[1] == U2F_CMD_VERSION) { // Get U2F version string
  265. memcpy(&buf[0], ver_str, 6);
  266. memcpy(&buf[6], state_no_error, 2);
  267. return 8;
  268. } else {
  269. memcpy(&buf[0], state_not_supported, 2);
  270. return 2;
  271. }
  272. return 0;
  273. }
  274. void u2f_wink(U2fData* U2F) {
  275. if(U2F->callback != NULL) U2F->callback(U2fNotifyWink, U2F->context);
  276. }