Debian Codegraph @

src/winhello.c

raw
1 /*
2 * Copyright (c) 2021-2026 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include <sys/types.h>
9
10 #include <stdlib.h>
11 #include <windows.h>
12
13 #include "fido.h"
14 #include "webauthn.h"
15
16 #ifndef NTE_INVALID_PARAMETER
17 #define NTE_INVALID_PARAMETER _HRESULT_TYPEDEF_(0x80090027)
18 #endif
19 #ifndef NTE_NOT_SUPPORTED
20 #define NTE_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x80090029)
21 #endif
22 #ifndef NTE_DEVICE_NOT_FOUND
23 #define NTE_DEVICE_NOT_FOUND _HRESULT_TYPEDEF_(0x80090035)
24 #endif
25 #ifndef NTE_USER_CANCELLED
26 #define NTE_USER_CANCELLED _HRESULT_TYPEDEF_(0x80090036L)
27 #endif
28
29 #define MAXCHARS 128
30 #define MAXCREDS 128
31 #define MAXMSEC 6000 * 1000
32 #define VENDORID 0x045e
33 #define PRODID 0x0001
34
35 struct winhello_assert {
36 WEBAUTHN_CLIENT_DATA cd;
37 WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS opt;
38 WEBAUTHN_ASSERTION *assert;
39 wchar_t *rp_id;
40 wchar_t *appid;
41 };
42
43 struct winhello_cred {
44 WEBAUTHN_RP_ENTITY_INFORMATION rp;
45 WEBAUTHN_USER_ENTITY_INFORMATION user;
46 WEBAUTHN_COSE_CREDENTIAL_PARAMETER alg;
47 WEBAUTHN_COSE_CREDENTIAL_PARAMETERS cose;
48 WEBAUTHN_CLIENT_DATA cd;
49 WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS opt;
50 WEBAUTHN_CREDENTIAL_ATTESTATION *att;
51 wchar_t *rp_id;
52 wchar_t *rp_name;
53 wchar_t *user_name;
54 wchar_t *user_icon;
55 wchar_t *display_name;
56 };
57
58 typedef DWORD WINAPI webauthn_get_api_version_t(void);
59 typedef PCWSTR WINAPI webauthn_strerr_t(HRESULT);
60 typedef HRESULT WINAPI webauthn_get_assert_t(HWND, LPCWSTR,
61 PCWEBAUTHN_CLIENT_DATA,
62 PCWEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS,
63 PWEBAUTHN_ASSERTION *);
64 typedef HRESULT WINAPI webauthn_make_cred_t(HWND,
65 PCWEBAUTHN_RP_ENTITY_INFORMATION,
66 PCWEBAUTHN_USER_ENTITY_INFORMATION,
67 PCWEBAUTHN_COSE_CREDENTIAL_PARAMETERS,
68 PCWEBAUTHN_CLIENT_DATA,
69 PCWEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS,
70 PWEBAUTHN_CREDENTIAL_ATTESTATION *);
71 typedef void WINAPI webauthn_free_assert_t(PWEBAUTHN_ASSERTION);
72 typedef void WINAPI webauthn_free_attest_t(PWEBAUTHN_CREDENTIAL_ATTESTATION);
73
74 static TLS BOOL webauthn_loaded;
75 static TLS HMODULE webauthn_handle;
76 static TLS webauthn_get_api_version_t *webauthn_get_api_version;
77 static TLS webauthn_strerr_t *webauthn_strerr;
78 static TLS webauthn_get_assert_t *webauthn_get_assert;
79 static TLS webauthn_make_cred_t *webauthn_make_cred;
80 static TLS webauthn_free_assert_t *webauthn_free_assert;
81 static TLS webauthn_free_attest_t *webauthn_free_attest;
82
83 static int
84 webauthn_load(void)
85 {
86 DWORD n = 1;
87
88 if (webauthn_loaded || webauthn_handle != NULL) {
89 fido_log_debug("%s: already loaded", __func__);
90 return -1;
91 }
92 if ((webauthn_handle = LoadLibraryEx(TEXT("webauthn.dll"), NULL,
93 LOAD_LIBRARY_SEARCH_SYSTEM32)) == NULL) {
94 fido_log_debug("%s: LoadLibrary", __func__);
95 return -1;
96 }
97
98 if ((webauthn_get_api_version =
99 (webauthn_get_api_version_t *)GetProcAddress(webauthn_handle,
100 "WebAuthNGetApiVersionNumber")) == NULL) {
101 fido_log_debug("%s: WebAuthNGetApiVersionNumber", __func__);
102 /* WebAuthNGetApiVersionNumber might not exist */
103 }
104 if (webauthn_get_api_version != NULL &&
105 (n = webauthn_get_api_version()) < 1) {
106 fido_log_debug("%s: unsupported api %lu", __func__, (u_long)n);
107 goto fail;
108 }
109 fido_log_debug("%s: api version %lu", __func__, (u_long)n);
110 if ((webauthn_strerr =
111 (webauthn_strerr_t *)GetProcAddress(webauthn_handle,
112 "WebAuthNGetErrorName")) == NULL) {
113 fido_log_debug("%s: WebAuthNGetErrorName", __func__);
114 goto fail;
115 }
116 if ((webauthn_get_assert =
117 (webauthn_get_assert_t *)GetProcAddress(webauthn_handle,
118 "WebAuthNAuthenticatorGetAssertion")) == NULL) {
119 fido_log_debug("%s: WebAuthNAuthenticatorGetAssertion",
120 __func__);
121 goto fail;
122 }
123 if ((webauthn_make_cred =
124 (webauthn_make_cred_t *)GetProcAddress(webauthn_handle,
125 "WebAuthNAuthenticatorMakeCredential")) == NULL) {
126 fido_log_debug("%s: WebAuthNAuthenticatorMakeCredential",
127 __func__);
128 goto fail;
129 }
130 if ((webauthn_free_assert =
131 (webauthn_free_assert_t *)GetProcAddress(webauthn_handle,
132 "WebAuthNFreeAssertion")) == NULL) {
133 fido_log_debug("%s: WebAuthNFreeAssertion", __func__);
134 goto fail;
135 }
136 if ((webauthn_free_attest =
137 (webauthn_free_attest_t *)GetProcAddress(webauthn_handle,
138 "WebAuthNFreeCredentialAttestation")) == NULL) {
139 fido_log_debug("%s: WebAuthNFreeCredentialAttestation",
140 __func__);
141 goto fail;
142 }
143
144 webauthn_loaded = true;
145
146 return 0;
147 fail:
148 fido_log_debug("%s: GetProcAddress", __func__);
149 webauthn_get_api_version = NULL;
150 webauthn_strerr = NULL;
151 webauthn_get_assert = NULL;
152 webauthn_make_cred = NULL;
153 webauthn_free_assert = NULL;
154 webauthn_free_attest = NULL;
155 FreeLibrary(webauthn_handle);
156 webauthn_handle = NULL;
157
158 return -1;
159 }
160
161 static wchar_t *
162 to_utf16(const char *utf8)
163 {
164 int nch;
165 wchar_t *utf16;
166
167 if (utf8 == NULL) {
168 fido_log_debug("%s: NULL", __func__);
169 return NULL;
170 }
171 if ((nch = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0)) < 1 ||
172 (size_t)nch > MAXCHARS) {
173 fido_log_debug("%s: MultiByteToWideChar %d", __func__, nch);
174 return NULL;
175 }
176 if ((utf16 = calloc((size_t)nch, sizeof(*utf16))) == NULL) {
177 fido_log_debug("%s: calloc", __func__);
178 return NULL;
179 }
180 if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, nch) != nch) {
181 fido_log_debug("%s: MultiByteToWideChar", __func__);
182 free(utf16);
183 return NULL;
184 }
185
186 return utf16;
187 }
188
189 static int
190 to_fido(HRESULT hr)
191 {
192 switch (hr) {
193 case NTE_NOT_SUPPORTED:
194 return FIDO_ERR_UNSUPPORTED_OPTION;
195 case NTE_INVALID_PARAMETER:
196 return FIDO_ERR_INVALID_PARAMETER;
197 case NTE_TOKEN_KEYSET_STORAGE_FULL:
198 return FIDO_ERR_KEY_STORE_FULL;
199 case NTE_DEVICE_NOT_FOUND:
200 case NTE_NOT_FOUND:
201 return FIDO_ERR_NOT_ALLOWED;
202 case __HRESULT_FROM_WIN32(ERROR_CANCELLED):
203 case NTE_USER_CANCELLED:
204 return FIDO_ERR_OPERATION_DENIED;
205 default:
206 fido_log_debug("%s: hr=0x%lx", __func__, (u_long)hr);
207 return FIDO_ERR_INTERNAL;
208 }
209 }
210
211 static int
212 pack_cd(WEBAUTHN_CLIENT_DATA *out, const fido_blob_t *in)
213 {
214 if (in->ptr == NULL) {
215 fido_log_debug("%s: NULL", __func__);
216 return -1;
217 }
218 if (in->len > ULONG_MAX) {
219 fido_log_debug("%s: in->len=%zu", __func__, in->len);
220 return -1;
221 }
222 out->dwVersion = WEBAUTHN_CLIENT_DATA_CURRENT_VERSION;
223 out->cbClientDataJSON = (DWORD)in->len;
224 out->pbClientDataJSON = in->ptr;
225 out->pwszHashAlgId = WEBAUTHN_HASH_ALGORITHM_SHA_256;
226
227 return 0;
228 }
229
230 static int
231 pack_credlist(WEBAUTHN_CREDENTIALS *out, const fido_blob_array_t *in)
232 {
233 WEBAUTHN_CREDENTIAL *c;
234
235 if (in->len == 0) {
236 return 0; /* nothing to do */
237 }
238 if (in->len > MAXCREDS) {
239 fido_log_debug("%s: in->len=%zu", __func__, in->len);
240 return -1;
241 }
242 if ((out->pCredentials = calloc(in->len, sizeof(*c))) == NULL) {
243 fido_log_debug("%s: calloc", __func__);
244 return -1;
245 }
246 out->cCredentials = (DWORD)in->len;
247 for (size_t i = 0; i < in->len; i++) {
248 if (in->ptr[i].len > ULONG_MAX) {
249 fido_log_debug("%s: %zu", __func__, in->ptr[i].len);
250 return -1;
251 }
252 c = &out->pCredentials[i];
253 c->dwVersion = WEBAUTHN_CREDENTIAL_CURRENT_VERSION;
254 c->cbId = (DWORD)in->ptr[i].len;
255 c->pbId = in->ptr[i].ptr;
256 c->pwszCredentialType = WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY;
257 }
258
259 return 0;
260 }
261
262 static int
263 set_cred_uv(DWORD *out, fido_opt_t uv, const char *pin)
264 {
265 if (pin) {
266 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED;
267 return 0;
268 }
269
270 switch (uv) {
271 case FIDO_OPT_OMIT:
272 case FIDO_OPT_FALSE:
273 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED;
274 break;
275 case FIDO_OPT_TRUE:
276 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED;
277 break;
278 }
279
280 return 0;
281 }
282
283 static int
284 set_assert_uv(DWORD *out, fido_opt_t uv, const char *pin)
285 {
286 if (pin) {
287 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED;
288 return 0;
289 }
290
291 switch (uv) {
292 case FIDO_OPT_OMIT:
293 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_PREFERRED;
294 break;
295 case FIDO_OPT_FALSE:
296 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED;
297 break;
298 case FIDO_OPT_TRUE:
299 *out = WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED;
300 break;
301 }
302
303 return 0;
304 }
305
306 static int
307 pack_rp(wchar_t **id, wchar_t **name, WEBAUTHN_RP_ENTITY_INFORMATION *out,
308 const fido_rp_t *in)
309 {
310 /* keep non-const copies of pwsz* for free() */
311 out->dwVersion = WEBAUTHN_RP_ENTITY_INFORMATION_CURRENT_VERSION;
312 if ((out->pwszId = *id = to_utf16(in->id)) == NULL) {
313 fido_log_debug("%s: id", __func__);
314 return -1;
315 }
316 if (in->name && (out->pwszName = *name = to_utf16(in->name)) == NULL) {
317 fido_log_debug("%s: name", __func__);
318 return -1;
319 }
320 return 0;
321 }
322
323 static int
324 pack_user(wchar_t **name, wchar_t **icon, wchar_t **display_name,
325 WEBAUTHN_USER_ENTITY_INFORMATION *out, const fido_user_t *in)
326 {
327 if (in->id.ptr == NULL || in->id.len > ULONG_MAX) {
328 fido_log_debug("%s: id", __func__);
329 return -1;
330 }
331 out->dwVersion = WEBAUTHN_USER_ENTITY_INFORMATION_CURRENT_VERSION;
332 out->cbId = (DWORD)in->id.len;
333 out->pbId = in->id.ptr;
334 /* keep non-const copies of pwsz* for free() */
335 if (in->name != NULL) {
336 if ((out->pwszName = *name = to_utf16(in->name)) == NULL) {
337 fido_log_debug("%s: name", __func__);
338 return -1;
339 }
340 }
341 if (in->icon != NULL) {
342 if ((out->pwszIcon = *icon = to_utf16(in->icon)) == NULL) {
343 fido_log_debug("%s: icon", __func__);
344 return -1;
345 }
346 }
347 if (in->display_name != NULL) {
348 if ((out->pwszDisplayName = *display_name =
349 to_utf16(in->display_name)) == NULL) {
350 fido_log_debug("%s: display_name", __func__);
351 return -1;
352 }
353 }
354
355 return 0;
356 }
357
358 static int
359 pack_cose(WEBAUTHN_COSE_CREDENTIAL_PARAMETER *alg,
360 WEBAUTHN_COSE_CREDENTIAL_PARAMETERS *cose, int type)
361 {
362 switch (type) {
363 case COSE_ES256:
364 alg->lAlg = WEBAUTHN_COSE_ALGORITHM_ECDSA_P256_WITH_SHA256;
365 break;
366 case COSE_ES384:
367 alg->lAlg = WEBAUTHN_COSE_ALGORITHM_ECDSA_P384_WITH_SHA384;
368 break;
369 case COSE_EDDSA:
370 alg->lAlg = -8; /* XXX */;
371 break;
372 case COSE_RS256:
373 alg->lAlg = WEBAUTHN_COSE_ALGORITHM_RSASSA_PKCS1_V1_5_WITH_SHA256;
374 break;
375 default:
376 fido_log_debug("%s: type %d", __func__, type);
377 return -1;
378 }
379 alg->dwVersion = WEBAUTHN_COSE_CREDENTIAL_PARAMETER_CURRENT_VERSION;
380 alg->pwszCredentialType = WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY;
381 cose->cCredentialParameters = 1;
382 cose->pCredentialParameters = alg;
383
384 return 0;
385 }
386
387 static WEBAUTHN_HMAC_SECRET_SALT *
388 pack_hmac_salt(const fido_blob_t *in)
389 {
390 WEBAUTHN_HMAC_SECRET_SALT *s;
391
392 if (in->ptr == NULL ||
393 (in->len != WEBAUTHN_CTAP_ONE_HMAC_SECRET_LENGTH &&
394 in->len != 2 * WEBAUTHN_CTAP_ONE_HMAC_SECRET_LENGTH)) {
395 fido_log_debug("%s: salt %p/%zu", __func__,
396 (const void *)in->ptr, in->len);
397 return NULL;
398 }
399 if ((s = calloc(1, sizeof(*s))) == NULL) {
400 fido_log_debug("%s: calloc", __func__);
401 return NULL;
402 }
403 if (in->len == WEBAUTHN_CTAP_ONE_HMAC_SECRET_LENGTH) {
404 s->cbFirst = (DWORD)in->len;
405 s->pbFirst = in->ptr;
406 } else { /* 2 * WEBAUTHN_CTAP_ONE_HMAC_SECRET_LENGTH */
407 s->cbFirst = (DWORD)in->len / 2;
408 s->pbFirst = in->ptr;
409 s->cbSecond = (DWORD)in->len / 2;
410 s->pbSecond = in->ptr + in->len / 2;
411 }
412
413 return s;
414 }
415
416 static int
417 pack_cred_ext(WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS *opt,
418 const fido_cred_extin_t *in)
419 {
420 WEBAUTHN_EXTENSIONS *exts = &opt->Extensions;
421 WEBAUTHN_EXTENSION *e;
422 WEBAUTHN_CRED_PROTECT_EXTENSION_IN *p;
423 WEBAUTHN_HMAC_SECRET_SALT *s;
424 BOOL *b;
425 size_t n = 0, i = 0;
426
427 if (in->attr.mask == 0) {
428 return 0; /* nothing to do */
429 }
430 if (in->attr.mask & ~(FIDO_EXT_HMAC_SECRET | FIDO_EXT_CRED_PROTECT |
431 FIDO_EXT_HMAC_SECRET_MC)) {
432 fido_log_debug("%s: mask 0x%x", __func__, in->attr.mask);
433 return -1;
434 }
435 if (in->attr.mask & FIDO_EXT_HMAC_SECRET)
436 n++;
437 if (in->attr.mask & FIDO_EXT_CRED_PROTECT)
438 n++;
439 if ((exts->pExtensions = calloc(n, sizeof(*e))) == NULL) {
440 fido_log_debug("%s: calloc", __func__);
441 return -1;
442 }
443 exts->cExtensions = (DWORD)n;
444 if (in->attr.mask & FIDO_EXT_HMAC_SECRET) {
445 /*
446 * NOTE: webauthn.dll ignores requests to enable hmac-secret
447 * unless a discoverable credential is also requested.
448 */
449 if ((b = calloc(1, sizeof(*b))) == NULL) {
450 fido_log_debug("%s: calloc", __func__);
451 return -1;
452 }
453 *b = true;
454 e = &exts->pExtensions[i];
455 e->pwszExtensionIdentifier =
456 WEBAUTHN_EXTENSIONS_IDENTIFIER_HMAC_SECRET;
457 e->pvExtension = b;
458 e->cbExtension = sizeof(*b);
459 i++;
460 }
461 if (in->attr.mask & FIDO_EXT_CRED_PROTECT) {
462 if ((p = calloc(1, sizeof(*p))) == NULL) {
463 fido_log_debug("%s: calloc", __func__);
464 return -1;
465 }
466 p->dwCredProtect = (DWORD)in->attr.prot;
467 p->bRequireCredProtect = true;
468 e = &exts->pExtensions[i];
469 e->pwszExtensionIdentifier =
470 WEBAUTHN_EXTENSIONS_IDENTIFIER_CRED_PROTECT;
471 e->pvExtension = p;
472 e->cbExtension = sizeof(*p);
473 i++;
474 }
475 if (in->attr.mask & FIDO_EXT_HMAC_SECRET_MC) {
476 if ((s = pack_hmac_salt(&in->hmac_salt)) == NULL) {
477 fido_log_debug("%s: pack_hmac_salt", __func__);
478 return -1;
479 }
480 opt->pPRFGlobalEval = s;
481 opt->dwFlags |= WEBAUTHN_AUTHENTICATOR_HMAC_SECRET_VALUES_FLAG;
482 opt->dwVersion = WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_8;
483 }
484
485 return 0;
486 }
487
488 static int
489 pack_assert_ext(WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS *out,
490 const fido_assert_ext_t *in)
491 {
492 WEBAUTHN_HMAC_SECRET_SALT_VALUES *v;
493 WEBAUTHN_HMAC_SECRET_SALT *s;
494
495 if (in->mask == 0) {
496 return 0; /* nothing to do */
497 }
498 if (in->mask != FIDO_EXT_HMAC_SECRET) {
499 fido_log_debug("%s: mask 0x%x", __func__, in->mask);
500 return -1;
501 }
502 if ((v = calloc(1, sizeof(*v))) == NULL ||
503 (s = pack_hmac_salt(&in->hmac_salt)) == NULL) {
504 free(v);
505 fido_log_debug("%s: pack_hmac_salt", __func__);
506 return -1;
507 }
508 v->pGlobalHmacSalt = s;
509 out->pHmacSecretSaltValues = v;
510 out->dwFlags |= WEBAUTHN_AUTHENTICATOR_HMAC_SECRET_VALUES_FLAG;
511 out->dwVersion = WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_6;
512
513 return 0;
514 }
515
516 static int
517 pack_appid(WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS *opt, const char *id,
518 wchar_t **appid)
519 {
520 if (id == NULL)
521 return 0; /* nothing to do */
522 if ((opt->pbU2fAppId = calloc(1, sizeof(*opt->pbU2fAppId))) == NULL) {
523 fido_log_debug("%s: calloc", __func__);
524 return -1;
525 }
526 if ((*appid = to_utf16(id)) == NULL) {
527 fido_log_debug("%s: to_utf16", __func__);
528 return -1;
529 }
530 fido_log_debug("%s: using %s", __func__, id);
531 opt->pwszU2fAppId = *appid;
532 opt->dwVersion = WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_2;
533
534 return 0;
535 }
536
537 static void
538 unpack_appid(fido_assert_t *assert,
539 const WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS *opt)
540 {
541 if (assert->appid == NULL || opt->pbU2fAppId == NULL)
542 return; /* nothing to do */
543 if (*opt->pbU2fAppId == false) {
544 fido_log_debug("%s: not used", __func__);
545 return;
546 }
547 fido_log_debug("%s: %s -> %s", __func__, assert->rp_id, assert->appid);
548 free(assert->rp_id);
549 assert->rp_id = assert->appid;
550 assert->appid = NULL;
551 }
552
553 static int
554 unpack_assert_authdata(fido_assert_t *assert, const WEBAUTHN_ASSERTION *wa)
555 {
556 int r;
557
558 if ((r = fido_assert_set_authdata_raw(assert, 0, wa->pbAuthenticatorData,
559 wa->cbAuthenticatorData)) != FIDO_OK) {
560 fido_log_debug("%s: fido_assert_set_authdata_raw: %s", __func__,
561 fido_strerr(r));
562 return -1;
563 }
564
565 return 0;
566 }
567
568 static int
569 unpack_assert_sig(fido_assert_t *assert, const WEBAUTHN_ASSERTION *wa)
570 {
571 int r;
572
573 if ((r = fido_assert_set_sig(assert, 0, wa->pbSignature,
574 wa->cbSignature)) != FIDO_OK) {
575 fido_log_debug("%s: fido_assert_set_sig: %s", __func__,
576 fido_strerr(r));
577 return -1;
578 }
579
580 return 0;
581 }
582
583 static int
584 unpack_cred_id(fido_assert_t *assert, const WEBAUTHN_ASSERTION *wa)
585 {
586 if (fido_blob_set(&assert->stmt[0].id, wa->Credential.pbId,
587 wa->Credential.cbId) < 0) {
588 fido_log_debug("%s: fido_blob_set", __func__);
589 return -1;
590 }
591
592 return 0;
593 }
594
595 static int
596 unpack_user_id(fido_assert_t *assert, const WEBAUTHN_ASSERTION *wa)
597 {
598 if (wa->cbUserId == 0)
599 return 0; /* user id absent */
600 if (fido_blob_set(&assert->stmt[0].user.id, wa->pbUserId,
601 wa->cbUserId) < 0) {
602 fido_log_debug("%s: fido_blob_set", __func__);
603 return -1;
604 }
605
606 return 0;
607 }
608
609 static int
610 unpack_hmac_secret(fido_blob_t *blob,
611 const WEBAUTHN_HMAC_SECRET_SALT *pHmacSecret)
612 {
613 if (pHmacSecret == NULL || pHmacSecret->cbFirst == 0 ||
614 pHmacSecret->pbFirst == NULL) {
615 fido_log_debug("%s: hmac-secret absent", __func__);
616 return 0; /* proceed without hmac-secret */
617 }
618 if (!fido_blob_is_empty(blob)) {
619 fido_log_debug("%s: fido_blob_is_empty", __func__);
620 return -1;
621 }
622 if (fido_blob_set(blob, pHmacSecret->pbFirst,
623 pHmacSecret->cbFirst) < 0) {
624 fido_log_debug("%s: fido_blob_set", __func__);
625 return -1;
626 }
627 if (pHmacSecret->cbSecond != 0 && pHmacSecret->pbSecond != NULL &&
628 fido_blob_append(blob, pHmacSecret->pbSecond,
629 pHmacSecret->cbSecond) < 0) {
630 fido_log_debug("%s: fido_blob_append", __func__);
631 return -1;
632 }
633
634 return 0;
635 }
636
637 static int
638 unpack_assert_hmac_secret(fido_assert_t *assert, const WEBAUTHN_ASSERTION *wa)
639 {
640 if (wa->dwVersion < WEBAUTHN_ASSERTION_VERSION_3) {
641 fido_log_debug("%s: dwVersion %u", __func__,
642 (unsigned)wa->dwVersion);
643 return 0; /* proceed without hmac-secret */
644 }
645 return unpack_hmac_secret(&assert->stmt[0].hmac_secret,
646 wa->pHmacSecret);
647 }
648
649 static int
650 unpack_cred_hmac_secret(fido_cred_t *cred,
651 const WEBAUTHN_CREDENTIAL_ATTESTATION *att)
652 {
653 if (att->dwVersion < WEBAUTHN_CREDENTIAL_ATTESTATION_VERSION_7) {
654 fido_log_debug("%s: dwVersion %u", __func__,
655 (unsigned)att->dwVersion);
656 return 0; /* proceed without hmac-secret */
657 }
658 return unpack_hmac_secret(&cred->hmac_secret, att->pHmacSecret);
659 }
660
661 static int
662 translate_fido_assert(struct winhello_assert *ctx, const fido_assert_t *assert,
663 const char *pin, int ms)
664 {
665 WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS *opt;
666
667 /* not supported by webauthn.h */
668 if (assert->up == FIDO_OPT_FALSE) {
669 fido_log_debug("%s: up %d", __func__, assert->up);
670 return FIDO_ERR_UNSUPPORTED_OPTION;
671 }
672 if ((ctx->rp_id = to_utf16(assert->rp_id)) == NULL) {
673 fido_log_debug("%s: rp_id", __func__);
674 return FIDO_ERR_INTERNAL;
675 }
676 if (pack_cd(&ctx->cd, &assert->cd) < 0) {
677 fido_log_debug("%s: pack_cd", __func__);
678 return FIDO_ERR_INTERNAL;
679 }
680 /* options */
681 opt = &ctx->opt;
682 opt->dwVersion = WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_1;
683 opt->dwTimeoutMilliseconds = ms < 0 ? MAXMSEC : (DWORD)ms;
684 if (pack_appid(opt, assert->appid, &ctx->appid) < 0) {
685 fido_log_debug("%s: pack_appid" , __func__);
686 return FIDO_ERR_INTERNAL;
687 }
688 if (pack_credlist(&opt->CredentialList, &assert->allow_list) < 0) {
689 fido_log_debug("%s: pack_credlist", __func__);
690 return FIDO_ERR_INTERNAL;
691 }
692 if (pack_assert_ext(opt, &assert->ext) < 0) {
693 fido_log_debug("%s: pack_assert_ext", __func__);
694 return FIDO_ERR_UNSUPPORTED_EXTENSION;
695 }
696 if (set_assert_uv(&opt->dwUserVerificationRequirement, assert->uv,
697 pin) < 0) {
698 fido_log_debug("%s: set_assert_uv", __func__);
699 return FIDO_ERR_INTERNAL;
700 }
701
702 return FIDO_OK;
703 }
704
705 static int
706 translate_winhello_assert(fido_assert_t *assert,
707 const struct winhello_assert *ctx)
708 {
709 const WEBAUTHN_ASSERTION *wa = ctx->assert;
710 const WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS *opt = &ctx->opt;
711 int r;
712
713 if (assert->stmt_len > 0) {
714 fido_log_debug("%s: stmt_len=%zu", __func__, assert->stmt_len);
715 return FIDO_ERR_INTERNAL;
716 }
717 if ((r = fido_assert_set_count(assert, 1)) != FIDO_OK) {
718 fido_log_debug("%s: fido_assert_set_count: %s", __func__,
719 fido_strerr(r));
720 return FIDO_ERR_INTERNAL;
721 }
722 unpack_appid(assert, opt);
723 if (unpack_assert_authdata(assert, wa) < 0) {
724 fido_log_debug("%s: unpack_assert_authdata", __func__);
725 return FIDO_ERR_INTERNAL;
726 }
727 if (unpack_assert_sig(assert, wa) < 0) {
728 fido_log_debug("%s: unpack_assert_sig", __func__);
729 return FIDO_ERR_INTERNAL;
730 }
731 if (unpack_cred_id(assert, wa) < 0) {
732 fido_log_debug("%s: unpack_cred_id", __func__);
733 return FIDO_ERR_INTERNAL;
734 }
735 if (unpack_user_id(assert, wa) < 0) {
736 fido_log_debug("%s: unpack_user_id", __func__);
737 return FIDO_ERR_INTERNAL;
738 }
739 if (assert->ext.mask & FIDO_EXT_HMAC_SECRET &&
740 unpack_assert_hmac_secret(assert, wa) < 0) {
741 fido_log_debug("%s: unpack_hmac_secret", __func__);
742 return FIDO_ERR_INTERNAL;
743 }
744
745 return FIDO_OK;
746 }
747
748 static int
749 translate_fido_cred(struct winhello_cred *ctx, const fido_cred_t *cred,
750 const char *pin, int ms)
751 {
752 WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS *opt;
753
754 if (pack_rp(&ctx->rp_id, &ctx->rp_name, &ctx->rp, &cred->rp) < 0) {
755 fido_log_debug("%s: pack_rp", __func__);
756 return FIDO_ERR_INTERNAL;
757 }
758 if (pack_user(&ctx->user_name, &ctx->user_icon, &ctx->display_name,
759 &ctx->user, &cred->user) < 0) {
760 fido_log_debug("%s: pack_user", __func__);
761 return FIDO_ERR_INTERNAL;
762 }
763 if (pack_cose(&ctx->alg, &ctx->cose, cred->type) < 0) {
764 fido_log_debug("%s: pack_cose", __func__);
765 return FIDO_ERR_INTERNAL;
766 }
767 if (pack_cd(&ctx->cd, &cred->cd) < 0) {
768 fido_log_debug("%s: pack_cd", __func__);
769 return FIDO_ERR_INTERNAL;
770 }
771 /* options */
772 opt = &ctx->opt;
773 opt->dwVersion = WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_1;
774 opt->dwTimeoutMilliseconds = ms < 0 ? MAXMSEC : (DWORD)ms;
775 opt->dwAttestationConveyancePreference =
776 WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT;
777 if (pack_credlist(&opt->CredentialList, &cred->excl) < 0) {
778 fido_log_debug("%s: pack_credlist", __func__);
779 return FIDO_ERR_INTERNAL;
780 }
781 if (pack_cred_ext(opt, &cred->ext) < 0) {
782 fido_log_debug("%s: pack_cred_ext", __func__);
783 return FIDO_ERR_UNSUPPORTED_EXTENSION;
784 }
785 if (set_cred_uv(&opt->dwUserVerificationRequirement, (cred->ext.attr.mask &
786 FIDO_EXT_CRED_PROTECT) ? FIDO_OPT_TRUE : cred->uv, pin) < 0) {
787 fido_log_debug("%s: set_cred_uv", __func__);
788 return FIDO_ERR_INTERNAL;
789 }
790 if (cred->rk == FIDO_OPT_TRUE) {
791 opt->bRequireResidentKey = true;
792 }
793 if (cred->ea.mode != 0) {
794 if (opt->dwVersion < WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_4) {
795 opt->dwVersion = WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_4;
796 }
797 opt->dwEnterpriseAttestation = (DWORD)cred->ea.mode;
798 }
799
800 return FIDO_OK;
801 }
802
803 static int
804 translate_winhello_cred(fido_cred_t *cred,
805 const WEBAUTHN_CREDENTIAL_ATTESTATION *att)
806 {
807 cbor_item_t *item = NULL;
808 struct cbor_load_result cbor;
809 int r = FIDO_ERR_INTERNAL;
810
811 if (att->pbAttestationObject == NULL) {
812 fido_log_debug("%s: pbAttestationObject", __func__);
813 goto fail;
814 }
815 if ((item = cbor_load(att->pbAttestationObject,
816 att->cbAttestationObject, &cbor)) == NULL) {
817 fido_log_debug("%s: cbor_load", __func__);
818 goto fail;
819 }
820 if (cbor_decode_attobj(item, cred) != 0) {
821 fido_log_debug("%s: cbor_decode_attobj", __func__);
822 goto fail;
823 }
824 if (att->dwVersion >= WEBAUTHN_CREDENTIAL_ATTESTATION_VERSION_4)
825 cred->ea.att = att->bEpAtt;
826
827 if (cred->ext.attr.mask & FIDO_EXT_HMAC_SECRET_MC &&
828 unpack_cred_hmac_secret(cred, att) < 0) {
829 fido_log_debug("%s: unpack_hmac_secret", __func__);
830 return FIDO_ERR_INTERNAL;
831 }
832
833 r = FIDO_OK;
834 fail:
835 if (item != NULL)
836 cbor_decref(&item);
837
838 return r;
839 }
840
841 static int
842 winhello_get_assert(HWND w, struct winhello_assert *ctx)
843 {
844 HRESULT hr;
845 int r = FIDO_OK;
846
847 if ((hr = webauthn_get_assert(w, ctx->rp_id, &ctx->cd, &ctx->opt,
848 &ctx->assert)) != S_OK) {
849 r = to_fido(hr);
850 fido_log_debug("%s: %ls -> %s", __func__, webauthn_strerr(hr),
851 fido_strerr(r));
852 }
853
854 return r;
855 }
856
857 static int
858 winhello_make_cred(HWND w, struct winhello_cred *ctx)
859 {
860 HRESULT hr;
861 int r = FIDO_OK;
862
863 if ((hr = webauthn_make_cred(w, &ctx->rp, &ctx->user, &ctx->cose,
864 &ctx->cd, &ctx->opt, &ctx->att)) != S_OK) {
865 r = to_fido(hr);
866 fido_log_debug("%s: %ls -> %s", __func__, webauthn_strerr(hr),
867 fido_strerr(r));
868 }
869
870 return r;
871 }
872
873 static void
874 winhello_assert_free(struct winhello_assert *ctx)
875 {
876 if (ctx == NULL)
877 return;
878 if (ctx->assert != NULL)
879 webauthn_free_assert(ctx->assert);
880
881 free(ctx->rp_id);
882 free(ctx->appid);
883 free(ctx->opt.CredentialList.pCredentials);
884 if (ctx->opt.pHmacSecretSaltValues != NULL)
885 free(ctx->opt.pHmacSecretSaltValues->pGlobalHmacSalt);
886 free(ctx->opt.pHmacSecretSaltValues);
887 free(ctx);
888 }
889
890 static void
891 winhello_cred_free(struct winhello_cred *ctx)
892 {
893 if (ctx == NULL)
894 return;
895 if (ctx->att != NULL)
896 webauthn_free_attest(ctx->att);
897
898 free(ctx->rp_id);
899 free(ctx->rp_name);
900 free(ctx->user_name);
901 free(ctx->user_icon);
902 free(ctx->display_name);
903 free(ctx->opt.CredentialList.pCredentials);
904 for (size_t i = 0; i < ctx->opt.Extensions.cExtensions; i++) {
905 WEBAUTHN_EXTENSION *e;
906 e = &ctx->opt.Extensions.pExtensions[i];
907 free(e->pvExtension);
908 }
909 free(ctx->opt.Extensions.pExtensions);
910 free(ctx->opt.pPRFGlobalEval);
911 free(ctx);
912 }
913
914 int
915 fido_winhello_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
916 {
917 fido_dev_info_t *di;
918
919 if (ilen == 0) {
920 return FIDO_OK;
921 }
922 if (devlist == NULL) {
923 return FIDO_ERR_INVALID_ARGUMENT;
924 }
925 if (!webauthn_loaded && webauthn_load() < 0) {
926 fido_log_debug("%s: webauthn_load", __func__);
927 return FIDO_OK; /* not an error */
928 }
929
930 di = &devlist[*olen];
931 memset(di, 0, sizeof(*di));
932 di->path = strdup(FIDO_WINHELLO_PATH);
933 di->manufacturer = strdup("Microsoft Corporation");
934 di->product = strdup("Windows Hello");
935 di->vendor_id = VENDORID;
936 di->product_id = PRODID;
937 if (di->path == NULL || di->manufacturer == NULL ||
938 di->product == NULL) {
939 free(di->path);
940 free(di->manufacturer);
941 free(di->product);
942 explicit_bzero(di, sizeof(*di));
943 return FIDO_ERR_INTERNAL;
944 }
945 ++(*olen);
946
947 return FIDO_OK;
948 }
949
950 int
951 fido_winhello_open(fido_dev_t *dev)
952 {
953 if (!webauthn_loaded && webauthn_load() < 0) {
954 fido_log_debug("%s: webauthn_load", __func__);
955 return FIDO_ERR_INTERNAL;
956 }
957 if (dev->flags != 0)
958 return FIDO_ERR_INVALID_ARGUMENT;
959 dev->attr.flags = FIDO_CAP_CBOR | FIDO_CAP_WINK;
960 dev->flags = FIDO_DEV_WINHELLO | FIDO_DEV_CRED_PROT | FIDO_DEV_PIN_SET;
961
962 return FIDO_OK;
963 }
964
965 int
966 fido_winhello_close(fido_dev_t *dev)
967 {
968 memset(dev, 0, sizeof(*dev));
969
970 return FIDO_OK;
971 }
972
973 int
974 fido_winhello_cancel(fido_dev_t *dev)
975 {
976 (void)dev;
977
978 return FIDO_ERR_INTERNAL;
979 }
980
981 int
982 fido_winhello_get_assert(fido_dev_t *dev, fido_assert_t *assert,
983 const char *pin, int ms)
984 {
985 HWND w;
986 struct winhello_assert *ctx;
987 int r = FIDO_ERR_INTERNAL;
988
989 (void)dev;
990
991 fido_assert_reset_rx(assert);
992
993 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
994 fido_log_debug("%s: calloc", __func__);
995 goto fail;
996 }
997 if ((w = GetForegroundWindow()) == NULL) {
998 fido_log_debug("%s: GetForegroundWindow", __func__);
999 if ((w = GetTopWindow(NULL)) == NULL) {
1000 fido_log_debug("%s: GetTopWindow", __func__);
1001 goto fail;
1002 }
1003 }
1004 if ((r = translate_fido_assert(ctx, assert, pin, ms)) != FIDO_OK) {
1005 fido_log_debug("%s: translate_fido_assert", __func__);
1006 goto fail;
1007 }
1008 if ((r = winhello_get_assert(w, ctx)) != FIDO_OK) {
1009 fido_log_debug("%s: winhello_get_assert", __func__);
1010 goto fail;
1011 }
1012 if ((r = translate_winhello_assert(assert, ctx)) != FIDO_OK) {
1013 fido_log_debug("%s: translate_winhello_assert", __func__);
1014 goto fail;
1015 }
1016
1017 fail:
1018 winhello_assert_free(ctx);
1019
1020 return r;
1021 }
1022
1023 int
1024 fido_winhello_get_cbor_info(fido_dev_t *dev, fido_cbor_info_t *ci)
1025 {
1026 const char *v[4] = { "U2F_V2", "FIDO_2_0", "FIDO_2_1_PRE", "FIDO_2_2" };
1027 const char *e[3] = { "credProtect", "hmac-secret", "hmac-secret-mc" };
1028 const char *t[2] = { "nfc", "usb" };
1029 const char *o[4] = { "rk", "up", "uv", "plat" };
1030
1031 (void)dev;
1032
1033 fido_cbor_info_reset(ci);
1034
1035 if (fido_str_array_pack(&ci->versions, v, nitems(v)) < 0 ||
1036 fido_str_array_pack(&ci->extensions, e, nitems(e)) < 0 ||
1037 fido_str_array_pack(&ci->transports, t, nitems(t)) < 0) {
1038 fido_log_debug("%s: fido_str_array_pack", __func__);
1039 return FIDO_ERR_INTERNAL;
1040 }
1041 if ((ci->options.name = calloc(nitems(o), sizeof(char *))) == NULL ||
1042 (ci->options.value = calloc(nitems(o), sizeof(bool))) == NULL) {
1043 fido_log_debug("%s: calloc", __func__);
1044 return FIDO_ERR_INTERNAL;
1045 }
1046 for (size_t i = 0; i < nitems(o); i++) {
1047 if ((ci->options.name[i] = strdup(o[i])) == NULL) {
1048 fido_log_debug("%s: strdup", __func__);
1049 return FIDO_ERR_INTERNAL;
1050 }
1051 ci->options.value[i] = true;
1052 ci->options.len++;
1053 }
1054
1055 return FIDO_OK;
1056 }
1057
1058 int
1059 fido_winhello_make_cred(fido_dev_t *dev, fido_cred_t *cred, const char *pin,
1060 int ms)
1061 {
1062 HWND w;
1063 struct winhello_cred *ctx;
1064 int r = FIDO_ERR_INTERNAL;
1065
1066 (void)dev;
1067
1068 fido_cred_reset_rx(cred);
1069
1070 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
1071 fido_log_debug("%s: calloc", __func__);
1072 goto fail;
1073 }
1074 if ((w = GetForegroundWindow()) == NULL) {
1075 fido_log_debug("%s: GetForegroundWindow", __func__);
1076 if ((w = GetTopWindow(NULL)) == NULL) {
1077 fido_log_debug("%s: GetTopWindow", __func__);
1078 goto fail;
1079 }
1080 }
1081 if ((r = translate_fido_cred(ctx, cred, pin, ms)) != FIDO_OK) {
1082 fido_log_debug("%s: translate_fido_cred", __func__);
1083 goto fail;
1084 }
1085 if ((r = winhello_make_cred(w, ctx)) != FIDO_OK) {
1086 fido_log_debug("%s: winhello_make_cred", __func__);
1087 goto fail;
1088 }
1089 if ((r = translate_winhello_cred(cred, ctx->att)) != FIDO_OK) {
1090 fido_log_debug("%s: translate_winhello_cred", __func__);
1091 goto fail;
1092 }
1093
1094 r = FIDO_OK;
1095 fail:
1096 winhello_cred_free(ctx);
1097
1098 return r;
1099 }