forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MacOSWebAuthnService.mm
1040 lines (937 loc) · 41.2 KB
/
MacOSWebAuthnService.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#import <AuthenticationServices/AuthenticationServices.h>
#include "MacOSWebAuthnService.h"
#include "CFTypeRefPtr.h"
#include "WebAuthnEnumStrings.h"
#include "WebAuthnResult.h"
#include "WebAuthnTransportIdentifiers.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPrefs_security.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "nsCocoaUtils.h"
#include "nsIWebAuthnPromise.h"
#include "nsThreadUtils.h"
// The documentation for the platform APIs used here can be found at:
// https://developer.apple.com/documentation/authenticationservices/public-private_key_authentication/supporting_passkeys
namespace {
static mozilla::LazyLogModule gMacOSWebAuthnServiceLog("macoswebauthnservice");
} // namespace
namespace mozilla::dom {
class API_AVAILABLE(macos(13.3)) MacOSWebAuthnService;
} // namespace mozilla::dom
// The following ASC* declarations are from the private framework
// AuthenticationServicesCore. The full definitions can be found in WebKit's
// source at Source/WebKit/Platform/spi/Cocoa/AuthenticationServicesCoreSPI.h.
// Overriding ASAuthorizationController's _requestContextWithRequests is
// currently the only way to provide the right information to the macOS
// WebAuthn API (namely, the clientDataHash for requests made to physical
// tokens).
NS_ASSUME_NONNULL_BEGIN
@class ASCPublicKeyCredentialDescriptor;
@interface ASCPublicKeyCredentialDescriptor : NSObject <NSSecureCoding>
- (instancetype)initWithCredentialID:(NSData*)credentialID
transports:
(nullable NSArray<NSString*>*)allowedTransports;
@end
@protocol ASCPublicKeyCredentialCreationOptions
@property(nonatomic, copy) NSData* clientDataHash;
@property(nonatomic, nullable, copy) NSData* challenge;
@property(nonatomic, copy)
NSArray<ASCPublicKeyCredentialDescriptor*>* excludedCredentials;
@end
@protocol ASCPublicKeyCredentialAssertionOptions <NSCopying>
@property(nonatomic, copy) NSData* clientDataHash;
@end
@protocol ASCCredentialRequestContext
@property(nonatomic, nullable, copy) id<ASCPublicKeyCredentialCreationOptions>
platformKeyCredentialCreationOptions;
@property(nonatomic, nullable, copy) id<ASCPublicKeyCredentialAssertionOptions>
platformKeyCredentialAssertionOptions;
@end
@interface ASAuthorizationController (Secrets)
- (id<ASCCredentialRequestContext>)
_requestContextWithRequests:(NSArray<ASAuthorizationRequest*>*)requests
error:(NSError**)outError;
@end
NSArray<NSString*>* TransportsByteToTransportsArray(const uint8_t aTransports)
API_AVAILABLE(macos(13.3)) {
NSMutableArray<NSString*>* transportsNS = [[NSMutableArray alloc] init];
if ((aTransports & MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_USB) ==
MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_USB) {
[transportsNS
addObject:
ASAuthorizationSecurityKeyPublicKeyCredentialDescriptorTransportUSB];
}
if ((aTransports & MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_NFC) ==
MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_NFC) {
[transportsNS
addObject:
ASAuthorizationSecurityKeyPublicKeyCredentialDescriptorTransportNFC];
}
if ((aTransports & MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_BLE) ==
MOZ_WEBAUTHN_AUTHENTICATOR_TRANSPORT_ID_BLE) {
[transportsNS
addObject:
ASAuthorizationSecurityKeyPublicKeyCredentialDescriptorTransportBluetooth];
}
// TODO (bug 1859367): the platform doesn't have a definition for "internal"
// transport. When it does, this code should be updated to handle it.
return transportsNS;
}
NSArray* CredentialListsToCredentialDescriptorArray(
const nsTArray<nsTArray<uint8_t>>& aCredentialList,
const nsTArray<uint8_t>& aCredentialListTransports,
const Class credentialDescriptorClass) API_AVAILABLE(macos(13.3)) {
MOZ_ASSERT(aCredentialList.Length() == aCredentialListTransports.Length());
NSMutableArray* credentials = [[NSMutableArray alloc] init];
for (size_t i = 0; i < aCredentialList.Length(); i++) {
const nsTArray<uint8_t>& credentialId = aCredentialList[i];
const uint8_t& credentialTransports = aCredentialListTransports[i];
NSData* credentialIdNS = [NSData dataWithBytes:credentialId.Elements()
length:credentialId.Length()];
NSArray<NSString*>* credentialTransportsNS =
TransportsByteToTransportsArray(credentialTransports);
NSObject* credential = [[credentialDescriptorClass alloc]
initWithCredentialID:credentialIdNS
transports:credentialTransportsNS];
[credentials addObject:credential];
}
return credentials;
}
// MacOSAuthorizationController is an ASAuthorizationController that overrides
// _requestContextWithRequests so that the implementation can set some options
// that aren't directly settable using the public API.
API_AVAILABLE(macos(13.3))
@interface MacOSAuthorizationController : ASAuthorizationController
@end
@implementation MacOSAuthorizationController {
nsTArray<uint8_t> mClientDataHash;
nsTArray<nsTArray<uint8_t>> mCredentialList;
nsTArray<uint8_t> mCredentialListTransports;
}
- (void)stashClientDataHash:(nsTArray<uint8_t>&&)clientDataHash
andCredentialList:(nsTArray<nsTArray<uint8_t>>&&)credentialList
andCredentialListTransports:(nsTArray<uint8_t>&&)credentialListTransports {
mClientDataHash = std::move(clientDataHash);
mCredentialList = std::move(credentialList);
mCredentialListTransports = std::move(credentialListTransports);
}
- (id<ASCCredentialRequestContext>)
_requestContextWithRequests:(NSArray<ASAuthorizationRequest*>*)requests
error:(NSError**)outError {
id<ASCCredentialRequestContext> context =
[super _requestContextWithRequests:requests error:outError];
id<ASCPublicKeyCredentialCreationOptions> registrationOptions =
context.platformKeyCredentialCreationOptions;
if (registrationOptions) {
registrationOptions.clientDataHash =
[NSData dataWithBytes:mClientDataHash.Elements()
length:mClientDataHash.Length()];
// Unset challenge so that the implementation uses clientDataHash (the API
// returns an error otherwise).
registrationOptions.challenge = nil;
const Class publicKeyCredentialDescriptorClass =
NSClassFromString(@"ASCPublicKeyCredentialDescriptor");
NSArray<ASCPublicKeyCredentialDescriptor*>* excludedCredentials =
CredentialListsToCredentialDescriptorArray(
mCredentialList, mCredentialListTransports,
publicKeyCredentialDescriptorClass);
if ([excludedCredentials count] > 0) {
registrationOptions.excludedCredentials = excludedCredentials;
}
}
id<ASCPublicKeyCredentialAssertionOptions> signOptions =
context.platformKeyCredentialAssertionOptions;
if (signOptions) {
signOptions.clientDataHash =
[NSData dataWithBytes:mClientDataHash.Elements()
length:mClientDataHash.Length()];
context.platformKeyCredentialAssertionOptions =
[signOptions copyWithZone:nil];
}
return context;
}
@end
// MacOSAuthenticatorRequestDelegate is an ASAuthorizationControllerDelegate,
// which can be set as an ASAuthorizationController's delegate to be called
// back when a request for a platform authenticator attestation or assertion
// response completes either with an attestation or assertion
// (didCompleteWithAuthorization) or with an error (didCompleteWithError).
API_AVAILABLE(macos(13.3))
@interface MacOSAuthenticatorRequestDelegate
: NSObject <ASAuthorizationControllerDelegate>
- (void)setCallback:(mozilla::dom::MacOSWebAuthnService*)callback;
@end
// MacOSAuthenticatorPresentationContextProvider is an
// ASAuthorizationControllerPresentationContextProviding, which can be set as
// an ASAuthorizationController's presentationContextProvider, and provides a
// presentation anchor for the ASAuthorizationController. Basically, this
// provides the API a handle to the window that made the request.
API_AVAILABLE(macos(13.3))
@interface MacOSAuthenticatorPresentationContextProvider
: NSObject <ASAuthorizationControllerPresentationContextProviding>
@property(nonatomic, strong) NSWindow* window;
@end
namespace mozilla::dom {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
class API_AVAILABLE(macos(13.3)) MacOSWebAuthnService final
: public nsIWebAuthnService {
public:
MacOSWebAuthnService()
: mTransactionState(Nothing(),
"MacOSWebAuthnService::mTransactionState") {}
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIWEBAUTHNSERVICE
void FinishMakeCredential(const nsTArray<uint8_t>& aRawAttestationObject,
const nsTArray<uint8_t>& aCredentialId,
const nsTArray<nsString>& aTransports,
const Maybe<nsString>& aAuthenticatorAttachment);
void FinishGetAssertion(const nsTArray<uint8_t>& aCredentialId,
const nsTArray<uint8_t>& aSignature,
const nsTArray<uint8_t>& aAuthenticatorData,
const nsTArray<uint8_t>& aUserHandle,
const Maybe<nsString>& aAuthenticatorAttachment);
void ReleasePlatformResources();
void AbortTransaction(nsresult aError);
private:
~MacOSWebAuthnService() = default;
void PerformRequests(NSArray<ASAuthorizationRequest*>* aRequests,
nsTArray<uint8_t>&& aClientDataHash,
nsTArray<nsTArray<uint8_t>>&& aCredentialList,
nsTArray<uint8_t>&& aCredentialListTransports,
uint64_t aBrowsingContextId);
struct TransactionState {
uint64_t transactionId;
uint64_t browsingContextId;
Maybe<RefPtr<nsIWebAuthnSignArgs>> pendingSignArgs;
Maybe<RefPtr<nsIWebAuthnSignPromise>> pendingSignPromise;
};
using TransactionStateMutex = DataMutex<Maybe<TransactionState>>;
void DoGetAssertion(const TransactionStateMutex::AutoLock& aGuard);
TransactionStateMutex mTransactionState;
// Main thread only:
nsCOMPtr<nsIWebAuthnRegisterPromise> mRegisterPromise;
nsCOMPtr<nsIWebAuthnSignPromise> mSignPromise;
MacOSAuthorizationController* mAuthorizationController = nil;
MacOSAuthenticatorRequestDelegate* mRequestDelegate = nil;
MacOSAuthenticatorPresentationContextProvider* mPresentationContextProvider =
nil;
};
#pragma clang diagnostic pop
} // namespace mozilla::dom
nsTArray<uint8_t> NSDataToArray(NSData* data) {
nsTArray<uint8_t> array(reinterpret_cast<const uint8_t*>(data.bytes),
data.length);
return array;
}
@implementation MacOSAuthenticatorRequestDelegate {
RefPtr<mozilla::dom::MacOSWebAuthnService> mCallback;
}
- (void)setCallback:(mozilla::dom::MacOSWebAuthnService*)callback {
mCallback = callback;
}
- (void)authorizationController:(ASAuthorizationController*)controller
didCompleteWithAuthorization:(ASAuthorization*)authorization {
if ([authorization.credential
conformsToProtocol:
@protocol(ASAuthorizationPublicKeyCredentialRegistration)]) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("MacOSAuthenticatorRequestDelegate::didCompleteWithAuthorization: "
"got registration"));
id<ASAuthorizationPublicKeyCredentialRegistration> credential =
(id<ASAuthorizationPublicKeyCredentialRegistration>)
authorization.credential;
nsTArray<uint8_t> rawAttestationObject(
NSDataToArray(credential.rawAttestationObject));
nsTArray<uint8_t> credentialId(NSDataToArray(credential.credentialID));
nsTArray<nsString> transports;
mozilla::Maybe<nsString> authenticatorAttachment;
if ([credential isKindOfClass:
[ASAuthorizationPlatformPublicKeyCredentialRegistration
class]]) {
transports.AppendElement(u"internal"_ns);
#if defined(MAC_OS_VERSION_13_5) && \
MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_13_5
if (__builtin_available(macos 13.5, *)) {
ASAuthorizationPlatformPublicKeyCredentialRegistration*
platformCredential =
(ASAuthorizationPlatformPublicKeyCredentialRegistration*)
credential;
switch (platformCredential.attachment) {
case ASAuthorizationPublicKeyCredentialAttachmentCrossPlatform:
authenticatorAttachment.emplace(u"cross-platform"_ns);
break;
case ASAuthorizationPublicKeyCredentialAttachmentPlatform:
authenticatorAttachment.emplace(u"platform"_ns);
break;
default:
break;
}
}
#endif
}
mCallback->FinishMakeCredential(rawAttestationObject, credentialId,
transports, authenticatorAttachment);
} else if ([authorization.credential
conformsToProtocol:
@protocol(ASAuthorizationPublicKeyCredentialAssertion)]) {
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Debug,
("MacOSAuthenticatorRequestDelegate::didCompleteWithAuthorization: "
"got assertion"));
id<ASAuthorizationPublicKeyCredentialAssertion> credential =
(id<ASAuthorizationPublicKeyCredentialAssertion>)
authorization.credential;
nsTArray<uint8_t> credentialId(NSDataToArray(credential.credentialID));
nsTArray<uint8_t> signature(NSDataToArray(credential.signature));
nsTArray<uint8_t> rawAuthenticatorData(
NSDataToArray(credential.rawAuthenticatorData));
nsTArray<uint8_t> userHandle(NSDataToArray(credential.userID));
mozilla::Maybe<nsString> authenticatorAttachment;
if ([credential
isKindOfClass:[ASAuthorizationPlatformPublicKeyCredentialAssertion
class]]) {
#if defined(MAC_OS_VERSION_13_5) && \
MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_13_5
if (__builtin_available(macos 13.5, *)) {
ASAuthorizationPlatformPublicKeyCredentialAssertion*
platformCredential =
(ASAuthorizationPlatformPublicKeyCredentialAssertion*)
credential;
switch (platformCredential.attachment) {
case ASAuthorizationPublicKeyCredentialAttachmentCrossPlatform:
authenticatorAttachment.emplace(u"cross-platform"_ns);
break;
case ASAuthorizationPublicKeyCredentialAttachmentPlatform:
authenticatorAttachment.emplace(u"platform"_ns);
break;
default:
break;
}
}
#endif
}
mCallback->FinishGetAssertion(credentialId, signature, rawAuthenticatorData,
userHandle, authenticatorAttachment);
} else {
MOZ_LOG(
gMacOSWebAuthnServiceLog, mozilla::LogLevel::Error,
("MacOSAuthenticatorRequestDelegate::didCompleteWithAuthorization: "
"authorization.credential is neither registration nor assertion!"));
MOZ_ASSERT_UNREACHABLE(
"should have ASAuthorizationPublicKeyCredentialRegistration or "
"ASAuthorizationPublicKeyCredentialAssertion");
}
mCallback->ReleasePlatformResources();
mCallback = nullptr;
}
- (void)authorizationController:(ASAuthorizationController*)controller
didCompleteWithError:(NSError*)error {
nsAutoString errorDescription;
nsCocoaUtils::GetStringForNSString(error.localizedDescription,
errorDescription);
MOZ_LOG(gMacOSWebAuthnServiceLog, mozilla::LogLevel::Warning,
("MacOSAuthenticatorRequestDelegate::didCompleteWithError: %ld (%s)",
error.code, NS_ConvertUTF16toUTF8(errorDescription).get()));
nsresult rv;
switch (error.code) {
case ASAuthorizationErrorCanceled:
rv = NS_ERROR_DOM_ABORT_ERR;
break;
case ASAuthorizationErrorFailed:
// The message is right, but it's not about indexeddb.
// See https://webidl.spec.whatwg.org/#constrainterror
rv = NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
break;
case ASAuthorizationErrorUnknown:
rv = NS_ERROR_DOM_UNKNOWN_ERR;
break;
default:
rv = NS_ERROR_DOM_NOT_ALLOWED_ERR;
break;
}
mCallback->AbortTransaction(rv);
mCallback = nullptr;
}
@end
@implementation MacOSAuthenticatorPresentationContextProvider
@synthesize window = window;
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:
(ASAuthorizationController*)controller {
return window;
}
@end
namespace mozilla::dom {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
// Given a browsingContextId, attempts to determine the NSWindow associated
// with that browser.
nsresult BrowsingContextIdToNSWindow(uint64_t browsingContextId,
NSWindow** window) {
*window = nullptr;
RefPtr<BrowsingContext> browsingContext(
BrowsingContext::Get(browsingContextId));
if (!browsingContext) {
return NS_ERROR_NOT_AVAILABLE;
}
CanonicalBrowsingContext* canonicalBrowsingContext =
browsingContext->Canonical();
if (!canonicalBrowsingContext) {
return NS_ERROR_NOT_AVAILABLE;
}
nsCOMPtr<nsIWidget> widget(
canonicalBrowsingContext->GetParentProcessWidgetContaining());
if (!widget) {
return NS_ERROR_NOT_AVAILABLE;
}
*window = static_cast<NSWindow*>(widget->GetNativeData(NS_NATIVE_WINDOW));
return NS_OK;
}
#pragma clang diagnostic pop
already_AddRefed<nsIWebAuthnService> NewMacOSWebAuthnServiceIfAvailable() {
MOZ_ASSERT(XRE_IsParentProcess());
if (!StaticPrefs::security_webauthn_enable_macos_passkeys()) {
MOZ_LOG(
gMacOSWebAuthnServiceLog, LogLevel::Debug,
("macOS platform support for webauthn (passkeys) disabled by pref"));
return nullptr;
}
// This code checks for the entitlement
// 'com.apple.developer.web-browser.public-key-credential', which must be
// true to be able to use the platform APIs.
CFTypeRefPtr<SecTaskRef> entitlementTask(
CFTypeRefPtr<SecTaskRef>::WrapUnderCreateRule(
SecTaskCreateFromSelf(nullptr)));
CFTypeRefPtr<CFBooleanRef> entitlementValue(
CFTypeRefPtr<CFBooleanRef>::WrapUnderCreateRule(
reinterpret_cast<CFBooleanRef>(SecTaskCopyValueForEntitlement(
entitlementTask.get(),
CFSTR("com.apple.developer.web-browser.public-key-credential"),
nullptr))));
if (!entitlementValue || !CFBooleanGetValue(entitlementValue.get())) {
MOZ_LOG(
gMacOSWebAuthnServiceLog, LogLevel::Warning,
("entitlement com.apple.developer.web-browser.public-key-credential "
"not present: platform passkey APIs will not be available"));
return nullptr;
}
nsCOMPtr<nsIWebAuthnService> service(new MacOSWebAuthnService());
return service.forget();
}
void MacOSWebAuthnService::AbortTransaction(nsresult aError) {
MOZ_ASSERT(NS_IsMainThread());
if (mRegisterPromise) {
Unused << mRegisterPromise->Reject(aError);
mRegisterPromise = nullptr;
}
if (mSignPromise) {
Unused << mSignPromise->Reject(aError);
mSignPromise = nullptr;
}
ReleasePlatformResources();
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
NS_IMPL_ISUPPORTS(MacOSWebAuthnService, nsIWebAuthnService)
#pragma clang diagnostic pop
NS_IMETHODIMP
MacOSWebAuthnService::MakeCredential(uint64_t aTransactionId,
uint64_t aBrowsingContextId,
nsIWebAuthnRegisterArgs* aArgs,
nsIWebAuthnRegisterPromise* aPromise) {
Reset();
auto guard = mTransactionState.Lock();
*guard = Some(TransactionState{
aTransactionId,
aBrowsingContextId,
Nothing(),
Nothing(),
});
NS_DispatchToMainThread(NS_NewRunnableFunction(
"MacOSWebAuthnService::MakeCredential",
[self = RefPtr{this}, browsingContextId(aBrowsingContextId),
aArgs = nsCOMPtr{aArgs}, aPromise = nsCOMPtr{aPromise}]() {
self->mRegisterPromise = aPromise;
nsAutoString rpId;
Unused << aArgs->GetRpId(rpId);
NSString* rpIdNS = nsCocoaUtils::ToNSString(rpId);
nsTArray<uint8_t> challenge;
Unused << aArgs->GetChallenge(challenge);
NSData* challengeNS = [NSData dataWithBytes:challenge.Elements()
length:challenge.Length()];
nsTArray<uint8_t> userId;
Unused << aArgs->GetUserId(userId);
NSData* userIdNS = [NSData dataWithBytes:userId.Elements()
length:userId.Length()];
nsAutoString userName;
Unused << aArgs->GetUserName(userName);
NSString* userNameNS = nsCocoaUtils::ToNSString(userName);
nsAutoString userDisplayName;
Unused << aArgs->GetUserName(userDisplayName);
NSString* userDisplayNameNS = nsCocoaUtils::ToNSString(userDisplayName);
nsTArray<int32_t> coseAlgs;
Unused << aArgs->GetCoseAlgs(coseAlgs);
NSMutableArray* credentialParameters = [[NSMutableArray alloc] init];
for (const auto& coseAlg : coseAlgs) {
ASAuthorizationPublicKeyCredentialParameters* credentialParameter =
[[ASAuthorizationPublicKeyCredentialParameters alloc]
initWithAlgorithm:coseAlg];
[credentialParameters addObject:credentialParameter];
}
nsTArray<nsTArray<uint8_t>> excludeList;
Unused << aArgs->GetExcludeList(excludeList);
nsTArray<uint8_t> excludeListTransports;
Unused << aArgs->GetExcludeListTransports(excludeListTransports);
if (excludeList.Length() != excludeListTransports.Length()) {
self->mRegisterPromise->Reject(NS_ERROR_INVALID_ARG);
return;
}
Maybe<ASAuthorizationPublicKeyCredentialUserVerificationPreference>
userVerificationPreference = Nothing();
nsAutoString userVerification;
Unused << aArgs->GetUserVerification(userVerification);
if (userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferenceRequired);
} else if (userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_PREFERRED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferencePreferred);
} else if (
userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferenceDiscouraged);
}
// The API doesn't support attestation for platform passkeys and shows
// no consent UI for non-none attestation for cross-platform devices,
// so this must always be none.
ASAuthorizationPublicKeyCredentialAttestationKind
attestationPreference =
ASAuthorizationPublicKeyCredentialAttestationKindNone;
// Initialize the platform provider with the rpId.
ASAuthorizationPlatformPublicKeyCredentialProvider* platformProvider =
[[ASAuthorizationPlatformPublicKeyCredentialProvider alloc]
initWithRelyingPartyIdentifier:rpIdNS];
// Make a credential registration request with the challenge, userName,
// and userId.
ASAuthorizationPlatformPublicKeyCredentialRegistrationRequest*
platformRegistrationRequest = [platformProvider
createCredentialRegistrationRequestWithChallenge:challengeNS
name:userNameNS
userID:userIdNS];
[platformProvider release];
platformRegistrationRequest.attestationPreference =
attestationPreference;
if (userVerificationPreference.isSome()) {
platformRegistrationRequest.userVerificationPreference =
*userVerificationPreference;
}
// Initialize the cross-platform provider with the rpId.
ASAuthorizationSecurityKeyPublicKeyCredentialProvider*
crossPlatformProvider =
[[ASAuthorizationSecurityKeyPublicKeyCredentialProvider alloc]
initWithRelyingPartyIdentifier:rpIdNS];
// Make a credential registration request with the challenge,
// userDisplayName, userName, and userId.
ASAuthorizationSecurityKeyPublicKeyCredentialRegistrationRequest*
crossPlatformRegistrationRequest = [crossPlatformProvider
createCredentialRegistrationRequestWithChallenge:challengeNS
displayName:
userDisplayNameNS
name:userNameNS
userID:userIdNS];
[crossPlatformProvider release];
crossPlatformRegistrationRequest.attestationPreference =
attestationPreference;
crossPlatformRegistrationRequest.credentialParameters =
credentialParameters;
if (userVerificationPreference.isSome()) {
crossPlatformRegistrationRequest.userVerificationPreference =
*userVerificationPreference;
}
nsTArray<uint8_t> clientDataHash;
nsresult rv = aArgs->GetClientDataHash(clientDataHash);
if (NS_FAILED(rv)) {
self->mRegisterPromise->Reject(rv);
return;
}
nsAutoString authenticatorAttachment;
rv = aArgs->GetAuthenticatorAttachment(authenticatorAttachment);
if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) {
self->mRegisterPromise->Reject(rv);
return;
}
NSMutableArray* requests = [[NSMutableArray alloc] init];
if (authenticatorAttachment.EqualsLiteral(
MOZ_WEBAUTHN_AUTHENTICATOR_ATTACHMENT_PLATFORM)) {
[requests addObject:platformRegistrationRequest];
} else if (authenticatorAttachment.EqualsLiteral(
MOZ_WEBAUTHN_AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM)) {
[requests addObject:crossPlatformRegistrationRequest];
} else {
// Regarding the value of authenticator attachment, according to the
// specification, "client platforms MUST ignore unknown values,
// treating an unknown value as if the member does not exist".
[requests addObject:platformRegistrationRequest];
[requests addObject:crossPlatformRegistrationRequest];
}
self->PerformRequests(
requests, std::move(clientDataHash), std::move(excludeList),
std::move(excludeListTransports), browsingContextId);
}));
return NS_OK;
}
void MacOSWebAuthnService::PerformRequests(
NSArray<ASAuthorizationRequest*>* aRequests,
nsTArray<uint8_t>&& aClientDataHash,
nsTArray<nsTArray<uint8_t>>&& aCredentialList,
nsTArray<uint8_t>&& aCredentialListTransports,
uint64_t aBrowsingContextId) {
MOZ_ASSERT(NS_IsMainThread());
// Create a MacOSAuthorizationController and initialize it with the requests.
MOZ_ASSERT(!mAuthorizationController);
mAuthorizationController = [[MacOSAuthorizationController alloc]
initWithAuthorizationRequests:aRequests];
[mAuthorizationController
stashClientDataHash:std::move(aClientDataHash)
andCredentialList:std::move(aCredentialList)
andCredentialListTransports:std::move(aCredentialListTransports)];
// Set up the delegate to run when the operation completes.
MOZ_ASSERT(!mRequestDelegate);
mRequestDelegate = [[MacOSAuthenticatorRequestDelegate alloc] init];
[mRequestDelegate setCallback:this];
mAuthorizationController.delegate = mRequestDelegate;
// Create a presentation context provider so the API knows which window
// made the request.
NSWindow* window = nullptr;
nsresult rv = BrowsingContextIdToNSWindow(aBrowsingContextId, &window);
if (NS_FAILED(rv)) {
AbortTransaction(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
MOZ_ASSERT(!mPresentationContextProvider);
mPresentationContextProvider =
[[MacOSAuthenticatorPresentationContextProvider alloc] init];
mPresentationContextProvider.window = window;
mAuthorizationController.presentationContextProvider =
mPresentationContextProvider;
// Finally, perform the request.
[mAuthorizationController performRequests];
}
void MacOSWebAuthnService::FinishMakeCredential(
const nsTArray<uint8_t>& aRawAttestationObject,
const nsTArray<uint8_t>& aCredentialId,
const nsTArray<nsString>& aTransports,
const Maybe<nsString>& aAuthenticatorAttachment) {
MOZ_ASSERT(NS_IsMainThread());
if (!mRegisterPromise) {
return;
}
RefPtr<WebAuthnRegisterResult> result(new WebAuthnRegisterResult(
aRawAttestationObject, Nothing(), aCredentialId, aTransports,
aAuthenticatorAttachment));
Unused << mRegisterPromise->Resolve(result);
mRegisterPromise = nullptr;
}
NS_IMETHODIMP
MacOSWebAuthnService::GetAssertion(uint64_t aTransactionId,
uint64_t aBrowsingContextId,
nsIWebAuthnSignArgs* aArgs,
nsIWebAuthnSignPromise* aPromise) {
Reset();
auto guard = mTransactionState.Lock();
*guard = Some(TransactionState{
aTransactionId,
aBrowsingContextId,
Some(RefPtr{aArgs}),
Some(RefPtr{aPromise}),
});
bool conditionallyMediated;
Unused << aArgs->GetConditionallyMediated(&conditionallyMediated);
if (!conditionallyMediated) {
DoGetAssertion(guard);
}
return NS_OK;
}
void MacOSWebAuthnService::DoGetAssertion(
const TransactionStateMutex::AutoLock& aGuard) {
if (aGuard->isNothing() || aGuard->ref().pendingSignArgs.isNothing() ||
aGuard->ref().pendingSignPromise.isNothing()) {
return;
}
// Take the pending Args and Promise to prevent repeated calls to
// DoGetAssertion for this transaction.
RefPtr<nsIWebAuthnSignArgs> aArgs = aGuard->ref().pendingSignArgs.extract();
RefPtr<nsIWebAuthnSignPromise> aPromise =
aGuard->ref().pendingSignPromise.extract();
uint64_t aBrowsingContextId = aGuard->ref().browsingContextId;
NS_DispatchToMainThread(NS_NewRunnableFunction(
"MacOSWebAuthnService::MakeCredential",
[self = RefPtr{this}, browsingContextId(aBrowsingContextId), aArgs,
aPromise]() {
self->mSignPromise = aPromise;
nsAutoString rpId;
Unused << aArgs->GetRpId(rpId);
NSString* rpIdNS = nsCocoaUtils::ToNSString(rpId);
nsTArray<uint8_t> challenge;
Unused << aArgs->GetChallenge(challenge);
NSData* challengeNS = [NSData dataWithBytes:challenge.Elements()
length:challenge.Length()];
nsTArray<nsTArray<uint8_t>> allowList;
Unused << aArgs->GetAllowList(allowList);
nsTArray<uint8_t> allowListTransports;
Unused << aArgs->GetAllowListTransports(allowListTransports);
NSMutableArray* platformAllowedCredentials =
[[NSMutableArray alloc] init];
for (const auto& allowedCredentialId : allowList) {
NSData* allowedCredentialIdNS =
[NSData dataWithBytes:allowedCredentialId.Elements()
length:allowedCredentialId.Length()];
ASAuthorizationPlatformPublicKeyCredentialDescriptor*
allowedCredential =
[[ASAuthorizationPlatformPublicKeyCredentialDescriptor alloc]
initWithCredentialID:allowedCredentialIdNS];
[platformAllowedCredentials addObject:allowedCredential];
}
const Class securityKeyPublicKeyCredentialDescriptorClass =
NSClassFromString(
@"ASAuthorizationSecurityKeyPublicKeyCredentialDescriptor");
NSArray<ASAuthorizationSecurityKeyPublicKeyCredentialDescriptor*>*
crossPlatformAllowedCredentials =
CredentialListsToCredentialDescriptorArray(
allowList, allowListTransports,
securityKeyPublicKeyCredentialDescriptorClass);
Maybe<ASAuthorizationPublicKeyCredentialUserVerificationPreference>
userVerificationPreference = Nothing();
nsAutoString userVerification;
Unused << aArgs->GetUserVerification(userVerification);
if (userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferenceRequired);
} else if (userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_PREFERRED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferencePreferred);
} else if (
userVerification.EqualsLiteral(
MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED)) {
userVerificationPreference.emplace(
ASAuthorizationPublicKeyCredentialUserVerificationPreferenceDiscouraged);
}
// Initialize the platform provider with the rpId.
ASAuthorizationPlatformPublicKeyCredentialProvider* platformProvider =
[[ASAuthorizationPlatformPublicKeyCredentialProvider alloc]
initWithRelyingPartyIdentifier:rpIdNS];
// Make a credential assertion request with the challenge.
ASAuthorizationPlatformPublicKeyCredentialAssertionRequest*
platformAssertionRequest = [platformProvider
createCredentialAssertionRequestWithChallenge:challengeNS];
[platformProvider release];
platformAssertionRequest.allowedCredentials =
platformAllowedCredentials;
if (userVerificationPreference.isSome()) {
platformAssertionRequest.userVerificationPreference =
*userVerificationPreference;
}
// Initialize the cross-platform provider with the rpId.
ASAuthorizationSecurityKeyPublicKeyCredentialProvider*
crossPlatformProvider =
[[ASAuthorizationSecurityKeyPublicKeyCredentialProvider alloc]
initWithRelyingPartyIdentifier:rpIdNS];
// Make a credential assertion request with the challenge.
ASAuthorizationSecurityKeyPublicKeyCredentialAssertionRequest*
crossPlatformAssertionRequest = [crossPlatformProvider
createCredentialAssertionRequestWithChallenge:challengeNS];
[crossPlatformProvider release];
crossPlatformAssertionRequest.allowedCredentials =
crossPlatformAllowedCredentials;
if (userVerificationPreference.isSome()) {
crossPlatformAssertionRequest.userVerificationPreference =
*userVerificationPreference;
}
nsTArray<uint8_t> clientDataHash;
nsresult rv = aArgs->GetClientDataHash(clientDataHash);
if (NS_FAILED(rv)) {
self->mSignPromise->Reject(rv);
return;
}
nsTArray<nsTArray<uint8_t>> unusedCredentialIds;
nsTArray<uint8_t> unusedTransports;
// allowList and allowListTransports won't actually get used.
self->PerformRequests(
@[ platformAssertionRequest, crossPlatformAssertionRequest ],
std::move(clientDataHash), std::move(allowList),
std::move(allowListTransports), browsingContextId);
}));
}
void MacOSWebAuthnService::FinishGetAssertion(
const nsTArray<uint8_t>& aCredentialId, const nsTArray<uint8_t>& aSignature,
const nsTArray<uint8_t>& aAuthenticatorData,
const nsTArray<uint8_t>& aUserHandle,
const Maybe<nsString>& aAuthenticatorAttachment) {
MOZ_ASSERT(NS_IsMainThread());
if (!mSignPromise) {
return;
}
RefPtr<WebAuthnSignResult> result(new WebAuthnSignResult(
aAuthenticatorData, Nothing(), aCredentialId, aSignature, aUserHandle,
aAuthenticatorAttachment));
Unused << mSignPromise->Resolve(result);
mSignPromise = nullptr;
}
void MacOSWebAuthnService::ReleasePlatformResources() {
MOZ_ASSERT(NS_IsMainThread());
[mAuthorizationController release];
mAuthorizationController = nil;
[mRequestDelegate release];
mRequestDelegate = nil;
[mPresentationContextProvider release];
mPresentationContextProvider = nil;
}
NS_IMETHODIMP
MacOSWebAuthnService::Reset() {
auto guard = mTransactionState.Lock();
if (guard->isSome()) {
if (guard->ref().pendingSignPromise.isSome()) {
// This request was never dispatched to the platform API, so
// we need to reject the promise ourselves.
guard->ref().pendingSignPromise.ref()->Reject(
NS_ERROR_DOM_NOT_ALLOWED_ERR);
}
guard->reset();
}
NS_DispatchToMainThread(NS_NewRunnableFunction(
"MacOSWebAuthnService::Cancel", [self = RefPtr{this}] {
// cancel results in the delegate's didCompleteWithError method being
// called, which will release all platform resources.
[self->mAuthorizationController cancel];
}));
return NS_OK;
}
NS_IMETHODIMP
MacOSWebAuthnService::GetIsUVPAA(bool* aAvailable) {
*aAvailable = true;
return NS_OK;
}
NS_IMETHODIMP
MacOSWebAuthnService::Cancel(uint64_t aTransactionId) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::HasPendingConditionalGet(uint64_t aBrowsingContextId,
const nsAString& aOrigin,
uint64_t* aRv) {
auto guard = mTransactionState.Lock();
if (guard->isNothing() ||
guard->ref().browsingContextId != aBrowsingContextId ||
guard->ref().pendingSignArgs.isNothing()) {
*aRv = 0;
return NS_OK;
}
nsString origin;
Unused << guard->ref().pendingSignArgs.ref()->GetOrigin(origin);
if (origin != aOrigin) {
*aRv = 0;
return NS_OK;
}
*aRv = guard->ref().transactionId;
return NS_OK;
}
NS_IMETHODIMP
MacOSWebAuthnService::GetAutoFillEntries(
uint64_t aTransactionId, nsTArray<RefPtr<nsIWebAuthnAutoFillEntry>>& aRv) {
auto guard = mTransactionState.Lock();
if (guard->isNothing() || guard->ref().transactionId != aTransactionId ||
guard->ref().pendingSignArgs.isNothing()) {
return NS_ERROR_NOT_AVAILABLE;
}
// Bug 1865379: discover credentials from the macOS keychain and return them.
aRv.Clear();
return NS_OK;
}
NS_IMETHODIMP
MacOSWebAuthnService::SelectAutoFillEntry(
uint64_t aTransactionId, const nsTArray<uint8_t>& aCredentialId) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::ResumeConditionalGet(uint64_t aTransactionId) {
auto guard = mTransactionState.Lock();
if (guard->isNothing() || guard->ref().transactionId != aTransactionId ||
guard->ref().pendingSignArgs.isNothing()) {
return NS_ERROR_NOT_AVAILABLE;
}
DoGetAssertion(guard);
return NS_OK;
}
NS_IMETHODIMP
MacOSWebAuthnService::PinCallback(uint64_t aTransactionId,
const nsACString& aPin) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::ResumeMakeCredential(uint64_t aTransactionId,
bool aForceNoneAttestation) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::SelectionCallback(uint64_t aTransactionId,
uint64_t aIndex) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::AddVirtualAuthenticator(
const nsACString& protocol, const nsACString& transport,
bool hasResidentKey, bool hasUserVerification, bool isUserConsenting,
bool isUserVerified, uint64_t* _retval) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::RemoveVirtualAuthenticator(uint64_t authenticatorId) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
MacOSWebAuthnService::AddCredential(uint64_t authenticatorId,
const nsACString& credentialId,
bool isResidentCredential,
const nsACString& rpId,
const nsACString& privateKey,