2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import <TargetConditionals.h>
27 #import <Foundation/Foundation.h>
29 #import "keychain/ot/OTClique.h"
30 #import "keychain/ot/OTClique+Private.h"
31 #import "keychain/ot/OTConstants.h"
32 #import "keychain/ot/OTDefines.h"
33 #import "keychain/SigninMetrics/OctagonSignPosts.h"
35 #import <utilities/SecCFWrappers.h>
36 #import <utilities/debugging.h>
38 #import "keychain/SecureObjectSync/SOSCloudCircle.h"
39 #import "KeychainCircle/PairingChannel.h"
40 #import <Security/SecBase.h>
42 const NSString* kSecEntitlementPrivateOctagonEscrow = @"com.apple.private.octagon.escrow-content";
45 #import <AuthKit/AuthKit.h>
46 #import <AuthKit/AuthKit_Private.h>
47 #import <SoftLinking/SoftLinking.h>
48 #import <CloudServices/SecureBackup.h>
49 #import <CloudServices/SecureBackupConstants.h>
50 #import "keychain/ot/OTControl.h"
51 #import "keychain/ckks/CKKSControl.h"
52 #import "keychain/ot/categories/OctagonEscrowRecoverer.h"
54 SOFT_LINK_FRAMEWORK(PrivateFrameworks, KeychainCircle);
55 SOFT_LINK_OPTIONAL_FRAMEWORK(PrivateFrameworks, CloudServices);
57 #pragma clang diagnostic push
58 #pragma clang diagnostic ignored "-Wstrict-prototypes"
59 SOFT_LINK_CLASS(KeychainCircle, KCPairingChannel);
60 SOFT_LINK_CLASS(KeychainCircle, OTPairingChannel);
61 SOFT_LINK_CLASS(CloudServices, SecureBackup);
62 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupErrorDomain, NSErrorDomain);
63 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupAuthenticationAppleID, NSString*);
64 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupAuthenticationPassword, NSString*);
65 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupiCloudDataProtectionDeleteAllRecordsKey, NSString*);
66 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupContainsiCDPDataKey, NSString*);
68 #pragma clang diagnostic pop
71 OTCliqueCDPContextType OTCliqueCDPContextTypeNone = @"cdpContextTypeNone";
72 OTCliqueCDPContextType OTCliqueCDPContextTypeSignIn = @"cdpContextTypeSignIn";
73 OTCliqueCDPContextType OTCliqueCDPContextTypeRepair = @"cdpContextTypeRepair";
74 OTCliqueCDPContextType OTCliqueCDPContextTypeFinishPasscodeChange = @"cdpContextTypeFinishPasscodeChange";
75 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyGenerate = @"cdpContextTypeRecoveryKeyGenerate";
76 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyNew = @"cdpContextTypeRecoveryKeyNew";
77 OTCliqueCDPContextType OTCliqueCDPContextTypeUpdatePasscode = @"cdpContextTypeUpdatePasscode";
79 NSString* OTCliqueStatusToString(CliqueStatus status)
83 return @"CliqueStatusIn";
84 case CliqueStatusNotIn:
85 return @"CliqueStatusNotIn";
86 case CliqueStatusPending:
87 return @"CliqueStatusPending";
88 case CliqueStatusAbsent:
89 return @"CliqueStatusAbsent";
90 case CliqueStatusNoCloudKitAccount:
91 return @"CliqueStatusNoCloudKitAccount";
92 case CliqueStatusError:
93 return @"CliqueStatusError";
96 CliqueStatus OTCliqueStatusFromString(NSString* str)
98 if([str isEqualToString: @"CliqueStatusIn"]) {
99 return CliqueStatusIn;
100 } else if([str isEqualToString: @"CliqueStatusNotIn"]) {
101 return CliqueStatusNotIn;
102 } else if([str isEqualToString: @"CliqueStatusPending"]) {
103 return CliqueStatusPending;
104 } else if([str isEqualToString: @"CliqueStatusAbsent"]) {
105 return CliqueStatusAbsent;
106 } else if([str isEqualToString: @"CliqueStatusNoCloudKitAccount"]) {
107 return CliqueStatusNoCloudKitAccount;
108 } else if([str isEqualToString: @"CliqueStatusError"]) {
109 return CliqueStatusError;
112 return CliqueStatusError;
115 NSString* OTCDPStatusToString(OTCDPStatus status) {
117 case OTCDPStatusUnknown:
119 case OTCDPStatusDisabled:
121 case OTCDPStatusEnabled:
127 @implementation OTConfigurationContext
129 - (OTControl* _Nullable)makeOTControl:(NSError**)error
132 if (self.otControl) {
133 return self.otControl;
135 return [OTControl controlObject:true error:error];
141 - (CKKSControl* _Nullable)makeCKKSControl:(NSError**)error
144 if(self.ckksControl) {
145 return self.ckksControl;
147 return [CKKSControl CKKSControlObject:true error:error];
155 if((self = [super init])) {
156 _context = OTDefaultContext;
162 @implementation OTBottleIDs
165 @implementation OTOperationConfiguration
167 - (instancetype)init {
168 if ((self = [super init]) == nil) {
171 _timeoutWaitForCKAccount = 10 * NSEC_PER_SEC;
172 _qualityOfService = NSQualityOfServiceDefault;
173 _discretionaryNetwork = NO;
174 _useCachedAccountStatus = NO;
178 + (BOOL)supportsSecureCoding {
182 - (void)encodeWithCoder:(nonnull NSCoder *)coder {
183 [coder encodeObject:@(_timeoutWaitForCKAccount) forKey:@"timeoutWaitForCKAccount"];
184 [coder encodeObject:@(_qualityOfService) forKey:@"qualityOfService"];
185 [coder encodeObject:@(_discretionaryNetwork) forKey:@"discretionaryNetwork"];
186 [coder encodeObject:@(_useCachedAccountStatus) forKey:@"useCachedAccountStatus"];
189 - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
190 _timeoutWaitForCKAccount = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"timeoutWaitForCKAccount"] unsignedLongLongValue];
191 _qualityOfService = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"qualityOfService"] integerValue];
192 _discretionaryNetwork = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"discretionaryNetwork"] boolValue];
193 _useCachedAccountStatus = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"useCachedAccountStatus"] boolValue];
200 @interface OTClique ()
201 @property (nonatomic, copy) NSString* cliqueMemberIdentifier;
202 @property (nonatomic, strong) OTConfigurationContext *ctx;
203 @property (nonatomic, strong) NSMutableDictionary *defaults;
206 @implementation OTClique
208 + (BOOL)platformSupportsSOS
210 return (OctagonPlatformSupportsSOS() && OctagonIsSOSFeatureEnabled());
213 // defaults write com.apple.security.octagon enable -bool YES
214 -(BOOL)isOctagonPairingEnabled {
215 BOOL nsDefaults = self.defaults[OTDefaultsOctagonEnable] ? [self.defaults[OTDefaultsOctagonEnable] boolValue] : OctagonIsEnabled();
216 secnotice("octagon", "pairing is %@", nsDefaults ? @"on" : @"off");
220 - (void)setPairingDefault:(BOOL)defaults
222 self.defaults[OTDefaultsOctagonEnable] = @(defaults);
225 - (void)removePairingDefault
227 [self.defaults removeObjectForKey:OTDefaultsOctagonEnable];
230 - (instancetype)initWithContextData:(OTConfigurationContext *)ctx
233 if ((self = [super init])) {
234 _ctx = [[OTConfigurationContext alloc]init];
235 _ctx.context = ctx.context ?: OTDefaultContext;
236 _ctx.dsid = [ctx.dsid copy];
237 _ctx.altDSID = [ctx.altDSID copy];
238 _ctx.otControl = ctx.otControl;
239 _ctx.ckksControl = ctx.ckksControl;
240 _ctx.overrideEscrowCache = ctx.overrideEscrowCache;
242 self.defaults = [NSMutableDictionary dictionary];
246 NSAssert(false, @"OTClique is not implemented on this platform");
248 // make the build analyzer happy
249 if ((self = [super init])) {
255 - (NSString* _Nullable)cliqueMemberIdentifier
258 __block NSString* retPeerID = nil;
259 __block bool subTaskSuccess = false;
261 OctagonSignpost fetchEgoPeerSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchEgoPeer);
262 if(OctagonIsEnabled()) {
263 NSError* localError = nil;
264 OTControl* control = [self makeOTControl:&localError];
266 secerror("octagon: Failed to create OTControl: %@", localError);
267 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
271 [control fetchEgoPeerID:nil
272 context:self.ctx.context
273 reply:^(NSString* peerID, NSError* error) {
275 secerror("octagon: Failed to fetch octagon peer ID: %@", error);
279 secnotice("clique", "cliqueMemberIdentifier(octagon) received %@", retPeerID);
282 if([OTClique platformSupportsSOS]) {
283 CFErrorRef error = NULL;
284 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(&error);
285 retPeerID = (NSString*)CFBridgingRelease(CFRetainSafe(SOSPeerInfoGetPeerID(me)));
287 CFBridgingRelease(error);
290 secnotice("clique", "cliqueMemberIdentifier complete: %@", retPeerID);
291 subTaskSuccess = retPeerID ? true : false;
292 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
300 - (OTControl* _Nullable)makeOTControl:(NSError**)error
302 return [self.ctx makeOTControl:error];
305 - (BOOL)establish:(NSError**)error
307 secnotice("clique-establish", "establish started");
308 OctagonSignpost establishSignPost = OctagonSignpostBegin(OctagonSignpostNameEstablish);
309 bool subTaskSuccess = false;
310 OTControl* control = [self makeOTControl:error];
312 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
316 __block BOOL success = NO;
317 __block NSError* localError = nil;
320 [control establish:nil context:self.ctx.context altDSID:self.ctx.altDSID reply:^(NSError * _Nullable operationError) {
322 secnotice("clique-establish", "establish returned an error: %@", operationError);
324 success = operationError == nil;
325 localError = operationError;
328 if(localError && error) {
331 secnotice("clique-establish", "establish complete: %@", success ? @"YES" : @"NO");
332 subTaskSuccess = success ? true : false;
333 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
338 - (BOOL)resetAndEstablish:(CuttlefishResetReason)resetReason error:(NSError**)error
340 secnotice("clique-resetandestablish", "resetAndEstablish started");
341 bool subTaskSuccess = false;
342 OctagonSignpost resetAndEstablishSignPost = OctagonSignpostBegin(OctagonSignpostNameResetAndEstablish);
344 OTControl* control = [self makeOTControl:error];
347 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
351 __block BOOL success = NO;
352 __block NSError* localError = nil;
353 [control resetAndEstablish:nil context:self.ctx.context altDSID:self.ctx.altDSID resetReason:resetReason reply:^(NSError * _Nullable operationError) {
356 secnotice("clique-resetandestablish", "resetAndEstablish returned an error: %@", operationError);
358 success = operationError == nil;
359 localError = operationError;
362 if(localError && error) {
366 secnotice("clique-resetandestablish", "establish complete: %@", success ? @"YES" : @"NO");
367 subTaskSuccess = success ? true : false;
368 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
374 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data error:(NSError * __autoreleasing *)error
376 return [OTClique newFriendsWithContextData:data resetReason:CuttlefishResetReasonUserInitiatedReset error:error];
379 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data resetReason:(CuttlefishResetReason)resetReason error:(NSError * __autoreleasing *)error
382 secnotice("clique-newfriends", "makeNewFriends invoked using context: %@, dsid: %@", data.context, data.dsid);
384 bool subTaskSuccess = false;
385 OctagonSignpost newFriendsSignpost = OctagonSignpostBegin(OctagonSignpostNameMakeNewFriends);
387 OTClique* clique = [[OTClique alloc] initWithContextData:data];
389 if(OctagonIsEnabled()) {
390 NSError* localError = nil;
391 [clique resetAndEstablish:resetReason error:&localError];
394 secnotice("clique-newfriends", "account reset failed: %@", localError);
398 OctagonSignpostEnd(newFriendsSignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
401 secnotice("clique-newfriends", "Octagon account reset succeeded");
405 if([OTClique platformSupportsSOS]) {
406 CFErrorRef resetError = NULL;
407 result = SOSCCResetToOffering(&resetError);
409 if(!result || resetError){
410 secnotice("clique-newfriends", "newFriendsWithContextData: resetToOffering failed: %@", resetError);
412 *error = CFBridgingRelease(resetError);
414 CFBridgingRelease(resetError);
416 OctagonSignpostEnd(newFriendsSignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
419 secnotice("clique-newfriends", "newFriendsWithContextData: reset the SOS circle");
421 secnotice("clique-newfriends", "newFriendsWithContextData: SOS disabled on this platform");
423 secnotice("clique-newfriends", "makeNewFriends complete");
425 subTaskSuccess = true;
426 OctagonSignpostEnd(newFriendsSignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
432 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
437 + (BOOL)isCloudServicesAvailable
440 if (isCloudServicesAvailable()) {
444 static dispatch_once_t onceToken;
445 dispatch_once(&onceToken, ^{
446 secnotice("octagon", "CloudServices is unavailable on this platform");
454 + (OTClique* _Nullable)performEscrowRecoveryWithContextData:(OTConfigurationContext*)data
455 escrowArguments:(NSDictionary*)sbdRecoveryArguments
456 error:(NSError**)error
459 if ([OTClique isCloudServicesAvailable] == NO) {
461 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
466 OctagonSignpost performEscrowRecoverySignpost = OctagonSignpostBegin(OctagonSignpostNamePerformEscrowRecovery);
467 bool subTaskSuccess = false;
468 NSError* localError = nil;
470 OTClique* clique = [[OTClique alloc] initWithContextData:data];
472 // Attempt the recovery from sbd
473 secnotice("clique-recovery", "attempting an escrow recovery for context:%@, altdsid:%@", data.context, data.altDSID);
474 id<OctagonEscrowRecovererPrococol> sb = data.sbd ?: [[getSecureBackupClass() alloc] init];
475 NSDictionary* recoveredInformation = nil;
477 OctagonSignpost recoverFromSBDSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformRecoveryFromSBD);
478 NSError* recoverError = [sb recoverWithInfo:sbdRecoveryArguments results:&recoveredInformation];
479 subTaskSuccess = (recoverError == nil) ? true : false;
480 OctagonSignpostEnd(recoverFromSBDSignPost, OctagonSignpostNamePerformRecoveryFromSBD, OctagonSignpostNumber1(OctagonSignpostNamePerformRecoveryFromSBD), (int)subTaskSuccess);
483 secnotice("clique-recovery", "sbd escrow recovery failed: %@", recoverError);
484 if(recoverError.code == 17 /* kSecureBackupRestoringLegacyBackupKeychainError */ && [recoverError.domain isEqualToString:getkSecureBackupErrorDomain()]) { /* XXX */
485 if([OTClique platformSupportsSOS]) {
486 secnotice("clique-recovery", "Can't restore legacy backup with no keybag. Resetting SOS to offering");
487 CFErrorRef blowItAwayError = NULL;
488 bool successfulReset = SOSCCResetToOffering(&blowItAwayError);
489 if(!successfulReset || blowItAwayError) {
490 secerror("clique-recovery: failed to reset to offering:%@", blowItAwayError);
492 secnotice("clique-recovery", "resetting SOS circle successful");
494 CFBridgingRelease(blowItAwayError);
496 secnotice("clique-recovery", "Legacy restore failed on a non-SOS platform");
500 *error = recoverError;
502 subTaskSuccess = false;
503 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
507 if(OctagonPlatformSupportsSOS()) { // Join if the legacy restore is complete now.
508 secnotice("clique-recovery", "attempting joinAfterRestore");
509 [clique joinAfterRestore:&localError];
510 secnotice("clique-recovery", "joinAfterRestore: %@", localError);
514 // look for OT Bottles
515 OTControl* control = [clique makeOTControl:&localError];
517 secnotice("clique-recovery", "unable to create otcontrol: %@", localError);
521 subTaskSuccess = false;
522 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
526 NSString *bottleID = recoveredInformation[@"bottleID"];
527 NSString *isValid = recoveredInformation[@"bottleValid"];
528 NSData *bottledPeerEntropy = recoveredInformation[@"EscrowServiceEscrowData"][@"BottledPeerEntropy"];
529 bool shouldResetOctagon = false;
531 if(bottledPeerEntropy && bottleID && [isValid isEqualToString:@"valid"]){
532 secnotice("clique-recovery", "recovering from bottle: %@", bottleID);
533 __block NSError* restoreBottleError = nil;
535 OctagonSignpost bottleRecoverySignPost = OctagonSignpostBegin(OctagonSignpostNamePerformOctagonJoin);
537 [control restore:OTCKContainerName
538 contextID:data.context
539 bottleSalt:data.altDSID
540 entropy:bottledPeerEntropy
542 reply:^(NSError * _Nullable restoreError) {
544 secnotice("clique-recovery", "restore bottle errored: %@", restoreError);
546 secnotice("clique-recovery", "restoring bottle succeeded");
548 restoreBottleError = restoreError;
551 subTaskSuccess = (restoreBottleError == nil) ? true : false;
552 OctagonSignpostEnd(bottleRecoverySignPost, OctagonSignpostNamePerformOctagonJoin, OctagonSignpostNumber1(OctagonSignpostNamePerformOctagonJoin), (int)subTaskSuccess);
554 if(restoreBottleError) {
556 *error = restoreBottleError;
558 subTaskSuccess = false;
559 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
563 shouldResetOctagon = true;
566 if(shouldResetOctagon) {
567 secnotice("clique-recovery", "bottle %@ is not valid, resetting octagon", bottleID);
568 NSError* resetError = nil;
570 OctagonSignpost resetSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle);
571 [clique resetAndEstablish:CuttlefishResetReasonNoBottleDuringEscrowRecovery error:&resetError];
572 subTaskSuccess = (resetError == nil) ? true : false;
573 OctagonSignpostEnd(resetSignPost, OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle, OctagonSignpostNumber1(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle), (int)subTaskSuccess);
576 secnotice("clique-recovery", "failed to reset octagon: %@", resetError);
580 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
583 secnotice("clique-recovery", "reset octagon succeeded");
587 secnotice("clique-recovery", "recovery complete: %@", clique);
589 subTaskSuccess = clique ? true : false;
590 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
595 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
602 - (KCPairingChannel *)setupPairingChannelAsInitiator:(KCPairingChannelContext *)ctx
605 return [getKCPairingChannelClass() pairingChannelInitiator:ctx];
611 - (KCPairingChannel * _Nullable)setupPairingChannelAsInitator:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
616 return [self setupPairingChannelAsInitiator:ctx];
619 - (KCPairingChannel *)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx
622 return [getKCPairingChannelClass() pairingChannelAcceptor:ctx];
628 - (KCPairingChannel * _Nullable)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
634 return [self setupPairingChannelAsAcceptor:ctx];
638 - (CliqueStatus)_fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing *)error
641 __block CliqueStatus sosStatus = CliqueStatusError;
642 __block CliqueStatus octagonStatus = CliqueStatusError;
643 bool subTaskSuccess = false;
645 OctagonSignpost fetchCliqueStatusSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchCliqueStatus);
647 // Octagon is supreme.
649 if(OctagonIsEnabled()) {
650 OTControl* control = [self makeOTControl:error];
652 secnotice("clique-status", "cliqueStatus noOTControl");
653 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
655 return CliqueStatusError;
658 __block NSError* localError = nil;
659 [control fetchCliqueStatus:nil context:self.ctx.context configuration:configuration reply:^(CliqueStatus cliqueStatus, NSError * _Nullable fetchError) {
661 octagonStatus = CliqueStatusError;
662 localError = fetchError;
663 secnotice("clique-status", "octagon clique status errored: %@", fetchError);
665 octagonStatus = cliqueStatus;
669 if(OctagonAuthoritativeTrustIsEnabled() || !OctagonPlatformSupportsSOS()) {
670 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) returning %@ (error: %@)",
671 configuration.useCachedAccountStatus ? "" : "non-",
672 self.ctx.context, self.ctx.altDSID,
673 OTCliqueStatusToString(octagonStatus), localError);
674 if (localError && error) {
676 subTaskSuccess = false;
678 subTaskSuccess = true;
680 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
681 return octagonStatus;
685 if([OTClique platformSupportsSOS]) {
686 CFErrorRef circleStatusError = NULL;
687 sosStatus = kSOSCCError;
688 if(configuration.useCachedAccountStatus){
689 sosStatus = SOSCCThisDeviceIsInCircle(&circleStatusError);
691 sosStatus = SOSCCThisDeviceIsInCircleNonCached(&circleStatusError);
693 secnotice("clique-status", "sos clique status is %d (%@)", (int)sosStatus, circleStatusError);
696 *error = (NSError*)CFBridgingRelease(circleStatusError);
698 CFBridgingRelease(circleStatusError);
701 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) complete: %@",
702 configuration.useCachedAccountStatus ? "" : "non-",
703 self.ctx.context, self.ctx.altDSID,
704 OTCliqueStatusToString(octagonStatus));
706 subTaskSuccess = true;
707 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
709 return octagonStatus;
712 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
714 return (CliqueStatus)kSOSCCError;
718 // Don't change rules for CoreCDP, and preserve legacy behavior for now
719 // preserve old behavior until CoreCDP can move to -fetchCliqueStatus:error:
720 #define LEGACY_WAITING_BEHAVIOR (TARGET_OS_OSX || TARGET_OS_IOS)
722 - (CliqueStatus)fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing * _Nonnull)error
724 return [self _fetchCliqueStatus:configuration error:error];
727 - (CliqueStatus)fetchCliqueStatus:(NSError * __autoreleasing *)error
729 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
730 #if LEGACY_WAITING_BEHAVIOR
731 configuration.timeoutWaitForCKAccount = 0;
733 return [self _fetchCliqueStatus:configuration error:error];
736 - (CliqueStatus)cachedCliqueStatus:(BOOL)usedCached error:(NSError * __autoreleasing *)error
738 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
739 #if LEGACY_WAITING_BEHAVIOR
740 configuration.timeoutWaitForCKAccount = 0;
743 configuration.useCachedAccountStatus = YES;
745 return [self _fetchCliqueStatus:configuration error:error];
749 - (BOOL)removeFriendsInClique:(NSArray<NSString*>*)friendIdentifiers error:(NSError * __autoreleasing *)error
752 secnotice("clique-removefriends", "removeFriendsInClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
753 OctagonSignpost removeFriendsSignPost = OctagonSignpostBegin(OctagonSignpostNameRemoveFriendsInClique);
754 bool subTaskSuccess = false;
756 // Annoying: we must sort friendIdentifiers into octagon/sos lists.
757 NSMutableArray<NSString*>* octagonIdentifiers = [NSMutableArray array];
758 NSMutableArray<NSString*>* sosIdentifiers = [NSMutableArray array];
760 for(NSString* friendIdentifier in friendIdentifiers) {
761 if([friendIdentifier hasPrefix:@"SHA256:"]) {
762 [octagonIdentifiers addObject: friendIdentifier];
764 [sosIdentifiers addObject: friendIdentifier];
768 // Ensure that we don't have any peers on the wrong platform
769 if(!OctagonIsEnabled() && octagonIdentifiers.count > 0) {
770 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
771 code:errSecUnimplemented
772 userInfo:@{NSLocalizedDescriptionKey: @"Octagon is disabled; can't distrust any Octagon peers"}];
773 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
777 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
781 if(!OctagonPlatformSupportsSOS() && sosIdentifiers.count > 0) {
782 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
783 code:errSecUnimplemented
784 userInfo:@{NSLocalizedDescriptionKey: @"SOS is not available on this platform; can't distrust any SOS peers"}];
785 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
789 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
794 __block NSError* localError = nil;
797 if(OctagonIsEnabled() && octagonIdentifiers.count > 0) {
798 OTControl* control = [self makeOTControl:error];
800 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
804 secnotice("clique-removefriends", "octagon: removing octagon friends: %@", octagonIdentifiers);
805 [control removeFriendsInClique:nil
806 context:self.ctx.context
807 peerIDs:octagonIdentifiers
808 reply:^(NSError* replyError) {
810 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", replyError);
811 localError = replyError;
813 secnotice("clique-removefriends", "octagon: friends removed: %@", octagonIdentifiers);
818 if([OTClique platformSupportsSOS] && sosIdentifiers.count >0) {
819 CFErrorRef removeFriendError = NULL;
821 secnotice("clique-removefriends", "removing sos friends: %@", sosIdentifiers);
822 result = SOSCCRemovePeersFromCircle((__bridge CFArrayRef)friendIdentifiers, &removeFriendError);
824 if(removeFriendError) {
825 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", removeFriendError);
826 localError = CFBridgingRelease(removeFriendError);
830 if(error && localError) {
833 secnotice("clique-removefriends", "removeFriendsInClique complete: %d", result);
835 subTaskSuccess = result;
836 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
838 return result && localError == nil;
844 - (BOOL)leaveClique:(NSError * __autoreleasing *)error
847 secnotice("clique-leaveClique", "leaveClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
848 CFErrorRef removeThisDeviceError = NULL;
850 bool subTaskSuccess = false;
852 OctagonSignpost leaveCliqueSignPost = OctagonSignpostBegin(OctagonSignpostNameLeaveClique);
854 if(OctagonIsEnabled()) {
855 OTControl* control = [self makeOTControl:error];
857 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
861 // We only want to issue a "leave" command if we're actively in a clique
862 __block NSError* localError = nil;
863 CliqueStatus currentStatus = [self fetchCliqueStatus:[[OTOperationConfiguration alloc] init]
867 secnotice("clique-leaveClique", "fetching current status errored: %@", localError);
871 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
875 if(currentStatus == CliqueStatusNotIn) {
876 secnotice("clique-leaveClique", "current status is Not In; no need to leave");
877 subTaskSuccess = true;
878 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
881 [control leaveClique:nil context:self.ctx.context reply:^(NSError * _Nullable leaveError) {
883 secnotice("clique-leaveClique", "leaveClique errored: %@", leaveError);
884 localError = leaveError;
886 secnotice("clique-leaveClique", "leaveClique success.");
893 result = !localError;
896 if([OTClique platformSupportsSOS]) {
897 result &= SOSCCRemoveThisDeviceFromCircle(&removeThisDeviceError);
899 *error = (NSError*)CFBridgingRelease(removeThisDeviceError);
901 CFBridgingRelease(removeThisDeviceError);
904 secnotice("clique-leaveClique", "leaveClique complete: %d", result);
906 subTaskSuccess = result;
907 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
909 return result ? YES : NO;
915 - (NSDictionary<NSString*,NSString*>* _Nullable)peerDeviceNamesByPeerID:(NSError * __autoreleasing *)error
918 secnotice("clique", "peerDeviceNamesByPeerID invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
919 OctagonSignpost peerNamesSignPost = OctagonSignpostBegin(OctagonSignpostNamePeerDeviceNamesByPeerID);
920 __block bool subTaskSuccess = false;
921 NSMutableDictionary<NSString*, NSString*>* retPeers = [NSMutableDictionary dictionary];
923 if(OctagonIsEnabled()) {
924 OTControl* control = [self makeOTControl:error];
926 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
930 __block NSError* localError = nil;
931 __block NSDictionary<NSString*, NSString*>* localPeers = nil;
933 [control peerDeviceNamesByPeerID:nil context:OTDefaultContext reply:^(NSDictionary<NSString*,NSString*>* peers, NSError* controlError) {
935 secnotice("clique", "peerDeviceNamesByPeerID errored: %@", controlError);
937 secnotice("clique", "peerDeviceNamesByPeerID succeeded: %@", peers);
939 localError = controlError;
943 if(error && localError) {
947 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
950 [retPeers addEntriesFromDictionary:localPeers];
951 secnotice("clique", "Received %lu Octagon peers", (unsigned long)localPeers.count);
954 if([OTClique platformSupportsSOS]) {
955 CFErrorRef peerErrorRef = NULL;
956 NSMutableDictionary<NSString*,NSString*>* peerMapping = [NSMutableDictionary dictionary];
957 NSArray* arrayOfPeerRefs = CFBridgingRelease(SOSCCCopyPeerPeerInfo(&peerErrorRef));
959 [arrayOfPeerRefs enumerateObjectsUsingBlock:^(id peerRef, NSUInteger idx, BOOL * stop) {
960 SOSPeerInfoRef peer = (__bridge SOSPeerInfoRef)peerRef;
962 [peerMapping setObject:(__bridge NSString*)SOSPeerInfoGetPeerName(peer) forKey:(__bridge NSString*)SOSPeerInfoGetPeerID(peer)];
966 subTaskSuccess = (peerErrorRef == NULL || [retPeers count] == 0) ? true : false;
969 *error = (NSError*)CFBridgingRelease(peerErrorRef);
971 CFBridgingRelease(peerErrorRef);
973 [retPeers addEntriesFromDictionary:peerMapping];
974 secnotice("clique", "Received %lu SOS peers", (unsigned long)peerMapping.count);
977 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
984 - (BOOL)joinAfterRestore:(NSError * __autoreleasing *)error
986 secnotice("clique-recovery", "joinAfterRestore for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
987 OctagonSignpost joinAfterRestoreSignPost = OctagonSignpostBegin(OctagonSignpostNameJoinAfterRestore);
988 bool subTaskSuccess = false;
990 if([OTClique platformSupportsSOS]) {
991 CFErrorRef restoreError = NULL;
992 bool res = SOSCCRequestToJoinCircleAfterRestore(&restoreError);
994 *error = (NSError*)CFBridgingRelease(restoreError);
996 CFBridgingRelease(restoreError);
998 secnotice("clique-recovery", "joinAfterRestore complete: %d %@", res, error ? *error : @"no error pointer provided");
1000 subTaskSuccess = res;
1001 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
1003 return res ? YES : NO;
1005 secnotice("clique-recovery", "SOS disabled for this platform, returning NO");
1007 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1008 code:errSecUnimplemented
1009 userInfo:@{NSLocalizedDescriptionKey: @"join after restore unimplemented"}];
1011 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
1016 - (BOOL)safariPasswordSyncingEnabled:(NSError **)error
1018 return [self fetchUserControllableViewsSyncingEnabled:error];
1021 - (BOOL)sosSafariPasswordSyncingEnabled:(NSError **)error
1023 secnotice("clique-safari", "safariPasswordSyncingEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1024 OctagonSignpost safariSyncingEnabledSignPost = OctagonSignpostBegin(OctagonSignpostNameSafariPasswordSyncingEnabled);
1025 bool subTaskSuccess = false;
1027 if([OTClique platformSupportsSOS]) {
1028 CFErrorRef viewErrorRef = NULL;
1030 SOSViewResultCode result = SOSCCView(kSOSViewAutofillPasswords, kSOSCCViewQuery, &viewErrorRef);
1031 subTaskSuccess = (viewErrorRef == NULL) ? true : false;
1033 BOOL viewMember = result == kSOSCCViewMember;
1035 *error = (NSError*)CFBridgingRelease(viewErrorRef);
1037 CFBridgingRelease(viewErrorRef);
1039 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1041 secnotice("clique-safari", "safariPasswordSyncingEnabled complete: %@", viewMember ? @"YES" : @"NO");
1044 secnotice("clique-safari", "SOS disabled for this platform, returning NO");
1046 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1047 code:errSecUnimplemented
1048 userInfo:@{NSLocalizedDescriptionKey: @"safari password syncing enabled unimplemented"}];
1050 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1055 - (BOOL)setUserControllableViewsSyncStatus:(BOOL)enabled
1056 error:(NSError* __autoreleasing *)error
1058 if([OTClique platformSupportsSOS]) {
1059 // We either enable or disable this list of SOS views, taken from CDP.
1060 NSSet<NSString*>* sosViews = [NSSet setWithObjects:(__bridge id)kSOSViewWiFi,
1061 (__bridge id)kSOSViewAutofillPasswords,
1062 (__bridge id)kSOSViewSafariCreditCards,
1063 (__bridge id)kSOSViewOtherSyncable,
1066 BOOL sosResult = NO;
1068 sosResult = [self sosViewSet:sosViews disabledViews:[NSSet set]];
1070 sosResult = [self sosViewSet:[NSSet set] disabledViews:sosViews];
1073 if(sosResult == NO) {
1074 secnotice("clique-user-sync", "SOS view setting failed, but no error returned");
1076 *error = [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain
1077 code:kSOSErrorNotReady
1079 NSLocalizedDescriptionKey: @"SOS reported failure, but no error given"
1087 if(OctagonIsEnabled()) {
1088 return [self setOctagonUserControllableViewsSyncEnabled:enabled
1095 - (BOOL)setOctagonUserControllableViewsSyncEnabled:(BOOL)enabled
1096 error:(NSError* __autoreleasing *)error
1099 OTControl* control = [self makeOTControl:error];
1104 __block NSError* localError = nil;
1106 secnotice("clique-user-sync", "setting user-controllable-sync status to %@", enabled ? @"enabled" : @"paused");
1108 [control setUserControllableViewsSyncStatus:self.ctx.containerName
1109 contextID:self.ctx.context
1111 reply:^(BOOL nowSyncing, NSError* _Nullable fetchError) {
1113 secnotice("clique-user-sync", "setting user-controllable-sync status errored: %@", fetchError);
1114 localError = fetchError;
1116 secnotice("clique-user-sync", "setting user-controllable-sync status succeeded, now : %@", nowSyncing ? @"enabled" : @"paused");
1120 if(error && localError) {
1121 *error = localError;
1124 return localError == nil;
1127 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1128 code:errSecUnimplemented
1129 userInfo:@{NSLocalizedDescriptionKey: @"setOctagonUserControllableViewSync unimplemented on this platform"}];
1135 - (BOOL)fetchUserControllableViewsSyncingEnabled:(NSError* __autoreleasing *)error
1137 if(!OctagonIsEnabled() && ![OTClique platformSupportsSOS]) {
1138 secnotice("clique-user-sync", "No syncing platforms enabled; views are not syncing");
1142 __block BOOL octagonSyncing = NO;
1144 if(OctagonIsEnabled()) {
1145 OTControl* control = [self makeOTControl:error];
1150 __block NSError* localError = nil;
1151 [control fetchUserControllableViewsSyncStatus:self.ctx.containerName
1152 contextID:self.ctx.context
1153 reply:^(BOOL nowSyncing, NSError* _Nullable fetchError) {
1155 secnotice("clique-user-sync", "fetching user-controllable-sync status errored: %@", fetchError);
1157 secnotice("clique-user-sync", "fetched user-controllable-sync status as : %@", nowSyncing ? @"enabled" : @"paused");
1159 octagonSyncing = nowSyncing;
1160 localError = fetchError;
1165 *error = localError;
1167 return octagonSyncing;
1172 BOOL sosSafariEnabled = NO;
1173 if([OTClique platformSupportsSOS]) {
1174 NSError* localError = nil;
1175 sosSafariEnabled = [self sosSafariPasswordSyncingEnabled:&localError];
1179 *error = localError;
1181 return sosSafariEnabled;
1185 if(OctagonIsEnabled() && [OTClique platformSupportsSOS]) {
1186 // Return the OR of this value, so that the UI checkbox will be on if any syncing is occurring
1187 return octagonSyncing || sosSafariEnabled;
1189 } else if(OctagonIsEnabled() && ![OTClique platformSupportsSOS]) {
1190 return octagonSyncing;
1192 } else if(!OctagonIsEnabled()&& [OTClique platformSupportsSOS]) {
1193 return sosSafariEnabled;
1196 // Should be impossible, due to the check above. So:
1198 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1199 code:errSecUnimplemented
1200 userInfo:@{NSLocalizedDescriptionKey: @"fetchUserControllableViewsSyncingEnabled has no meaning if all sync systems are not on this platform"}];
1207 - (BOOL)isLastFriend:(NSError **)error
1209 secnotice("clique-isLastFriend", "is last friend");
1213 - (BOOL)waitForInitialSync:(NSError *__autoreleasing*)error
1215 secnotice("clique-legacy", "waitForInitialSync for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1217 OctagonSignpost waitForInitialSyncSignPost = OctagonSignpostBegin(OctagonSignpostNameWaitForInitialSync);
1218 bool subTaskSuccess = false;
1220 if([OTClique platformSupportsSOS]) {
1221 CFErrorRef initialSyncErrorRef = NULL;
1222 BOOL initialSyncResult = SOSCCWaitForInitialSync(&initialSyncErrorRef) ? YES : NO;
1225 *error = (NSError*)CFBridgingRelease(initialSyncErrorRef);
1227 CFBridgingRelease(initialSyncErrorRef);
1229 secnotice("clique-legacy", "waitForInitialSync waited: %d %@", initialSyncResult, error ? *error : @"no error pointer provided");
1231 subTaskSuccess = initialSyncResult ? true : false;
1232 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1234 return initialSyncResult;
1236 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1238 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1239 code:errSecUnimplemented
1240 userInfo:@{NSLocalizedDescriptionKey: @"wait for initial sync unimplemented"}];
1242 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1247 - (NSArray* _Nullable)copyViewUnawarePeerInfo:(NSError *__autoreleasing*)error
1249 secnotice("clique-legacy", "copyViewUnawarePeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1251 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyViewUnawarePeerInfo);
1252 bool subTaskSuccess = false;
1254 if([OTClique platformSupportsSOS]) {
1255 CFErrorRef copyViewUnawarePeerInfoErrorRef = NULL;
1256 CFArrayRef peerListRef = SOSCCCopyViewUnawarePeerInfo(©ViewUnawarePeerInfoErrorRef);
1258 NSArray* peerList = (peerListRef ? (NSArray*)(CFBridgingRelease(peerListRef)) : nil);
1260 *error = (NSError*)CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1262 CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1264 subTaskSuccess = (peerList != nil) ? true : false;
1265 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1269 secnotice("clique-legacy", "SOS disabled for this platform, returning NULL");
1271 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1272 code:errSecUnimplemented
1273 userInfo:@{NSLocalizedDescriptionKey: @"copy view unaware peer info unimplemented"}];
1275 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1280 - (BOOL)viewSet:(NSSet*)enabledViews disabledViews:(NSSet*)disabledViews
1282 if(OctagonIsEnabled()) {
1283 bool enableUserViews = [enabledViews containsObject:(__bridge NSString*)kSOSViewAutofillPasswords];
1284 NSError* octagonError = nil;
1285 // This function should log its success or failure
1286 BOOL result = [self setOctagonUserControllableViewsSyncEnabled:enableUserViews error:&octagonError];
1292 return [self sosViewSet:enabledViews disabledViews:disabledViews];
1295 - (BOOL)sosViewSet:(NSSet*)enabledViews disabledViews:(NSSet*)disabledViews
1297 secnotice("clique-legacy", "viewSet for context:%@, altdsid:%@ enable:%@, disable:%@",
1298 self.ctx.context, self.ctx.altDSID, enabledViews, disabledViews);
1299 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameViewSet);
1300 bool subTaskSuccess = false;
1302 if([OTClique platformSupportsSOS]) {
1303 bool result = SOSCCViewSet((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews);
1305 BOOL viewSetResult = result ? YES : NO;
1306 subTaskSuccess = result;
1307 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1308 return viewSetResult;
1310 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1311 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1316 - (BOOL)setUserCredentialsAndDSID:(NSString*)userLabel
1317 password:(NSData*)userPassword
1318 error:(NSError *__autoreleasing*)error
1320 secnotice("clique-legacy", "setUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1321 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetUserCredentialsAndDSID);
1322 bool subTaskSuccess = false;
1324 if([OTClique platformSupportsSOS]) {
1325 CFErrorRef setCredentialsErrorRef = NULL;
1326 bool result = SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1327 (__bridge CFDataRef)userPassword,
1328 (__bridge CFStringRef)self.ctx.dsid,
1329 &setCredentialsErrorRef);
1331 BOOL setCredentialsResult = result ? YES : NO;
1332 secnotice("clique-legacy", "setUserCredentialsAndDSID results: %d %@", setCredentialsResult, setCredentialsErrorRef);
1334 *error = (NSError*)CFBridgingRelease(setCredentialsErrorRef);
1336 CFBridgingRelease(setCredentialsErrorRef);
1338 subTaskSuccess = result;
1339 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1341 return setCredentialsResult;
1343 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1345 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1346 code:errSecUnimplemented
1347 userInfo:@{NSLocalizedDescriptionKey: @"set user credentials unimplemented"}];
1349 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1354 - (BOOL)tryUserCredentialsAndDSID:(NSString*)userLabel
1355 password:(NSData*)userPassword
1356 error:(NSError *__autoreleasing*)error
1358 secnotice("clique-legacy", "tryUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1359 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameTryUserCredentialsAndDSID);
1360 bool subTaskSuccess = false;
1362 if([OTClique platformSupportsSOS]) {
1363 CFErrorRef tryCredentialsErrorRef = NULL;
1364 bool result = SOSCCTryUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1365 (__bridge CFDataRef)userPassword,
1366 (__bridge CFStringRef)self.ctx.dsid,
1367 &tryCredentialsErrorRef);
1369 BOOL tryCredentialsResult = result ? YES : NO;
1370 secnotice("clique-legacy", "tryUserCredentialsAndDSID results: %d %@", tryCredentialsResult, tryCredentialsErrorRef);
1372 *error = (NSError*)CFBridgingRelease(tryCredentialsErrorRef);
1374 CFBridgingRelease(tryCredentialsErrorRef);
1376 subTaskSuccess = result;
1377 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1378 return tryCredentialsResult;
1381 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1383 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1384 code:errSecUnimplemented
1385 userInfo:@{NSLocalizedDescriptionKey: @"try user credentials unimplemented"}];
1387 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1392 - (NSArray* _Nullable)copyPeerPeerInfo:(NSError *__autoreleasing*)error
1394 secnotice("clique-legacy", "copyPeerPeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1395 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyPeerPeerInfo);
1396 bool subTaskSuccess = false;
1398 if([OTClique platformSupportsSOS]) {
1399 CFErrorRef copyPeerErrorRef = NULL;
1400 CFArrayRef result = SOSCCCopyPeerPeerInfo(©PeerErrorRef);
1402 NSArray* peerList = (result ? (NSArray*)(CFBridgingRelease(result)) : nil);
1404 secnotice("clique-legacy", "copyPeerPeerInfo results: %@ (%@)", peerList, copyPeerErrorRef);
1406 *error = (NSError*)CFBridgingRelease(copyPeerErrorRef);
1408 CFBridgingRelease(copyPeerErrorRef);
1410 subTaskSuccess = (peerList != nil) ? true : false;
1411 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1414 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1416 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1417 code:errSecUnimplemented
1418 userInfo:@{NSLocalizedDescriptionKey: @"copy peer peer info unimplemented"}];
1420 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1425 - (BOOL)peersHaveViewsEnabled:(NSArray<NSString*>*)viewNames error:(NSError *__autoreleasing*)error
1427 secnotice("clique-legacy", "peersHaveViewsEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1428 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePeersHaveViewsEnabled);
1429 bool subTaskSuccess = false;
1431 if([OTClique platformSupportsSOS]) {
1432 CFErrorRef viewsEnabledErrorRef = NULL;
1433 BOOL viewsEnabledResult = NO;
1435 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef)viewNames, &viewsEnabledErrorRef);
1437 viewsEnabledResult = CFBooleanGetValue(result) ? YES : NO;
1439 secnotice("clique-legacy", "peersHaveViewsEnabled results: %@ (%@)", viewsEnabledResult ? @"YES" : @"NO",
1440 viewsEnabledErrorRef);
1442 *error = (NSError*)CFBridgingRelease(viewsEnabledErrorRef);
1444 CFBridgingRelease(viewsEnabledErrorRef);
1446 subTaskSuccess = viewsEnabledResult ? true : false;
1447 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1448 return viewsEnabledResult;
1450 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1452 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1453 code:errSecUnimplemented
1454 userInfo:@{NSLocalizedDescriptionKey: @"peers have views enabled unimplemented"}];
1456 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1461 - (BOOL)requestToJoinCircle:(NSError *__autoreleasing*)error
1463 bool result = false;
1464 bool subTaskSuccess = false;
1465 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameRequestToJoinCircle);
1468 secnotice("clique-legacy", "requestToJoinCircle for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1470 if(OctagonIsEnabled()) {
1471 // Sometimes, CoreCDP calls this to cause a circle creation to occur.
1472 // So, for Octagon, we might want to request a establish, but not a reset.
1474 // Fetch the current trust status, so we know if we should fire off the establish.
1475 NSError* localError = nil;
1476 CliqueStatus status = [self fetchCliqueStatus: &localError];
1479 secnotice("clique-legacy", "fetching clique status failed: %@", localError);
1481 *error = localError;
1483 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1487 if(status == CliqueStatusAbsent) {
1488 secnotice("clique-legacy", "clique status is %@; beginning an establish", OTCliqueStatusToString(status));
1489 [self establish:&localError];
1493 *error = localError;
1495 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1498 secnotice("clique-legacy", "establish succeeded");
1501 secnotice("clique-legacy", "clique status is %@; performing no Octagon actions", OTCliqueStatusToString(status));
1504 // If we didn't early-exit, and we aren't going to invoke SOS below, we succeeded.
1505 if(!OctagonPlatformSupportsSOS()) {
1506 secnotice("clique-legacy", "requestToJoinCircle platform does not support SOS");
1507 subTaskSuccess = true;
1508 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1514 if([OTClique platformSupportsSOS]) {
1515 CFErrorRef joinErrorRef = NULL;
1516 result = SOSCCRequestToJoinCircle(&joinErrorRef);
1517 secnotice("clique-legacy", "sos requestToJoinCircle complete: %d %@", result, joinErrorRef);
1519 *error = (NSError*)CFBridgingRelease(joinErrorRef);
1521 CFBridgingRelease(joinErrorRef);
1525 subTaskSuccess = result;
1526 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1528 return result ? YES : NO;
1531 - (BOOL)accountUserKeyAvailable
1533 secnotice("clique-legacy", "accountUserKeyAvailable for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1534 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameAccountUserKeyAvailable);
1535 bool subTaskSuccess = false;
1537 if([OTClique platformSupportsSOS]) {
1538 BOOL canAuthenticate = SOSCCCanAuthenticate(NULL) ? YES : NO;
1539 if (canAuthenticate == NO) {
1540 secnotice("clique-legacy", "Security requires credentials...");
1542 subTaskSuccess = canAuthenticate ? true : false;
1543 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1544 return canAuthenticate;
1546 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1547 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1552 + (NSArray<NSData*>* _Nullable)fetchEscrowRecordsInternal:(OTConfigurationContext*)configurationContext
1553 error:(NSError**)error
1556 secnotice("clique-fetchrecords", "fetching escrow records for context:%@, altdsid:%@", configurationContext.context, configurationContext.altDSID);
1558 if(OctagonIsEnabled()) {
1559 __block NSError* localError = nil;
1560 __block NSArray<NSData*>* localRecords = nil;
1562 OTControl *control = [configurationContext makeOTControl:&localError];
1564 secnotice("clique-fetchrecords", "unable to create otcontrol: %@", localError);
1566 *error = localError;
1570 [control fetchEscrowRecords:OTCKContainerName
1571 contextID:configurationContext.context
1572 forceFetch:configurationContext.overrideEscrowCache
1573 reply:^(NSArray<NSData *> * _Nullable records,
1574 NSError * _Nullable fetchError) {
1576 secnotice("clique-fetchrecords", "fetchEscrowRecords errored: %@", fetchError);
1578 secnotice("clique-fetchrecords", "fetchEscrowRecords succeeded: %@", records);
1580 localError = fetchError;
1581 localRecords = records;
1584 if(error && localError) {
1585 *error = localError;
1588 secnotice("clique-fetchrecords", "fetchEscrowRecords complete");
1590 return localRecords;
1592 // With octagon off, fail with 'unimplemented'
1594 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1595 code:errSecUnimplemented
1596 userInfo:@{NSLocalizedDescriptionKey: @"fetching escrow records unimplemented"}];
1602 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1608 // MARK: SBD interfaces
1609 + (OTBottleIDs* _Nullable)findOptimalBottleIDsWithContextData:(OTConfigurationContext*)data
1610 error:(NSError**)error
1613 secnotice("clique-findbottle", "finding optimal bottles for context:%@, altdsid:%@", data.context, data.altDSID);
1614 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFindOptimalBottleIDsWithContextData);
1615 bool subTaskSuccess = false;
1617 if(OctagonIsEnabled()) {
1618 __block NSError* localError = nil;
1619 __block NSArray<NSString*>* localViableBottleIDs = nil;
1620 __block NSArray<NSString*>* localPartiallyViableBottleIDs = nil;
1622 OTControl *control = [data makeOTControl:&localError];
1624 secnotice("clique-findbottle", "unable to create otcontrol: %@", localError);
1626 *error = localError;
1628 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1631 [control fetchAllViableBottles:OTCKContainerName
1632 context:data.context
1633 reply:^(NSArray<NSString *> * _Nullable sortedBottleIDs,
1634 NSArray<NSString*> * _Nullable sortedPartialBottleIDs,
1635 NSError * _Nullable fetchError) {
1637 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData errored: %@", fetchError);
1639 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData succeeded: %@, %@", sortedBottleIDs, sortedPartialBottleIDs);
1641 localError = fetchError;
1642 localViableBottleIDs = sortedBottleIDs;
1643 localPartiallyViableBottleIDs = sortedPartialBottleIDs;
1646 if(error && localError) {
1647 *error = localError;
1649 OTBottleIDs* bottleIDs = [[OTBottleIDs alloc] init];
1650 bottleIDs.preferredBottleIDs = localViableBottleIDs;
1651 bottleIDs.partialRecoveryBottleIDs = localPartiallyViableBottleIDs;
1653 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData complete");
1655 subTaskSuccess = (localError == nil) ? true : false;
1656 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1659 // With octagon off, fail with 'unimplemented'
1661 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1662 code:errSecUnimplemented
1663 userInfo:@{NSLocalizedDescriptionKey: @"optimal bottle IDs unimplemented"}];
1665 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1670 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1676 + (OTClique* _Nullable)recoverWithContextData:(OTConfigurationContext*)data
1677 bottleID:(NSString*)bottleID
1678 escrowedEntropy:(NSData*)entropy
1679 error:(NSError**)error
1682 secnotice("octagon", "replaced by performEscrowRecoveryWithContextData:escrowArguments:error: remove call");
1686 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1692 // used by sbd to fill in the escrow record
1693 // TODO: what extra entitlement do you need to call this?
1694 - (void)fetchEscrowContents:(void (^)(NSData* _Nullable entropy,
1695 NSString* _Nullable bottleID,
1696 NSData* _Nullable signingPublicKey,
1697 NSError* _Nullable error))reply
1700 secnotice("clique-fetchescrow", "fetching entropy for bottling for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1701 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFetchEscrowContents);
1702 __block bool subTaskSuccess = false;
1703 if(OctagonIsEnabled()) {
1704 NSError* controlError = nil;
1705 OTControl* control = [self makeOTControl:&controlError];
1707 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1708 reply(nil, nil, nil, controlError);
1711 [control fetchEscrowContents:OTCKContainerName
1712 contextID:self.ctx.context
1713 reply:^(NSData * _Nullable entropy,
1714 NSString * _Nullable bottleID,
1715 NSData * _Nullable signingPublicKey,
1716 NSError * _Nullable error) {
1718 secnotice("clique-fetchescrow", "fetchEscrowContents errored: %@", error);
1720 secnotice("clique-fetchescrow","fetchEscrowContents succeeded");
1722 subTaskSuccess = (error == nil) ? true : false;
1723 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1724 reply (entropy, bottleID, signingPublicKey, error);
1727 // With octagon off, fail with 'unimplemented'
1728 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1729 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain
1730 code:errSecUnimplemented
1731 userInfo:@{NSLocalizedDescriptionKey: @"fetchEscrowRecordContents unimplemented"}]);
1734 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1738 + (void)setNewRecoveryKeyWithData:(OTConfigurationContext *)ctx
1739 recoveryKey:(NSString*)recoveryKey reply:(nonnull void (^)(SecRecoveryKey *rk, NSError *error))reply
1742 secnotice("octagon-setrecoverykey", "setNewRecoveryKeyWithData invoked for context: %@", ctx.context);
1743 //set the recovery key for SOS
1744 NSError* createRecoveryKeyError = nil;
1745 NSMutableDictionary *userInfo = [NSMutableDictionary new];
1746 NSError* retError = nil;
1747 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetNewRecoveryKeyWithData);
1748 __block bool subTaskSuccess = false;
1750 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(recoveryKey, &createRecoveryKeyError);
1752 secerror("octagon-setrecoverykey, SecRKCreateRecoveryKeyWithError() failed: %@", createRecoveryKeyError);
1753 userInfo[NSLocalizedDescriptionKey] = @"SecRKCreateRecoveryKeyWithError() failed";
1754 userInfo[NSUnderlyingErrorKey] = createRecoveryKeyError;
1755 if ([OTClique isCloudServicesAvailable] == NO) {
1756 retError = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:userInfo];
1758 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1760 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1761 reply(nil, retError);
1764 if([OTClique platformSupportsSOS]) {
1765 CFErrorRef registerError = nil;
1766 if (!SecRKRegisterBackupPublicKey(rk, ®isterError)) {
1767 secerror("octagon-setrecoverykey, SecRKRegisterBackupPublicKey() failed: %@", registerError);
1768 NSError *underlyingError = CFBridgingRelease(registerError);
1769 userInfo[NSLocalizedDescriptionKey] = @"SecRKRegisterBackupPublicKey() failed";
1770 userInfo[NSUnderlyingErrorKey] = underlyingError;
1771 if ([OTClique isCloudServicesAvailable] == NO) {
1772 retError = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:userInfo];
1774 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1776 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1777 reply(nil,retError);
1780 secnotice("octagon-setrecoverykey", "successfully registered recovery key for SOS");
1784 //set the recovery key for Octagon
1785 if(OctagonRecoveryKeyIsEnabled()) {
1786 NSError* controlError = nil;
1787 OTControl* control = [ctx makeOTControl:&controlError];
1789 secnotice("octagon-setrecoverykey", "failed to fetch OTControl object: %@", controlError);
1790 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1791 reply(nil, controlError);
1794 [control createRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError * createError) {
1796 secerror("octagon-setrecoverykey, failed to create octagon recovery key");
1797 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1798 reply(nil, createError);
1801 secnotice("octagon-setrecoverykey", "successfully set octagon recovery key");
1802 subTaskSuccess = true;
1803 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1810 reply(nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1814 + (void)recoverOctagonUsingData:(OTConfigurationContext *)ctx
1815 recoveryKey:(NSString*)recoveryKey
1816 reply:(void(^)(NSError* _Nullable error))reply
1819 OctagonSignpost signpost = OctagonSignpostBegin(OctagonSignpostNameRecoverOctagonUsingData);
1820 __block bool subTaskSuccess = false;
1822 if(OctagonRecoveryKeyIsEnabled()) {
1823 NSError* controlError = nil;
1824 OTControl* control = [ctx makeOTControl:&controlError];
1826 secnotice("clique-recoverykey", "join using recovery key");
1829 secnotice("clique-recoverykey", "failed to fetch OTControl object: %@", controlError);
1830 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1831 reply(controlError);
1834 [control joinWithRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError *joinError) {
1836 secnotice("clique-recoverykey", "failed to join using recovery key: %@", joinError);
1837 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1841 secnotice("clique-recoverykey", "successfully joined using recovery key");
1842 subTaskSuccess = true;
1843 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1849 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1853 - (void)performedCDPStateMachineRun:(OTCliqueCDPContextType)type
1854 success:(BOOL)success
1855 error:(NSError * _Nullable)error
1856 reply:(void(^)(NSError* _Nullable error))reply
1859 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePerformedCDPStateMachineRun);
1860 NSError* controlError = nil;
1861 __block bool subTaskSuccess = false;
1863 OTControl* control = [self makeOTControl:&controlError];
1865 secnotice("clique-cdp-sm", "octagon, failed to fetch OTControl object: %@", controlError);
1866 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1867 reply(controlError);
1871 [control postCDPFollowupResult:success type:type error:error containerName:OTCKContainerName contextName:OTDefaultContext reply:^(NSError *postError) {
1873 secnotice("clique-cdp-sm", "failed to post %@ result: %@ ", type, postError);
1874 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1879 secnotice("clique-cdp-sm", "posted success: %@", type);
1881 secnotice("clique-cdp-sm", "posted error: %@: %@", type, error);
1883 subTaskSuccess = success ? true : false;
1884 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1888 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1892 - (BOOL)waitForOctagonUpgrade:(NSError** _Nullable)error
1895 OTControl* control = nil;
1896 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameWaitForOctagonUpgrade);
1897 __block bool subTaskSuccess = false;
1899 if (!OctagonIsEnabled()) {
1900 secnotice("clique-waitforoctagonupgrade", "cannot upgrade, octagon is not enabled");
1902 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:@{NSLocalizedDescriptionKey: @"Octagon is not enabled"}];
1904 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1909 NSError *controlError = nil;
1910 control = [self makeOTControl:&controlError];
1912 secnotice("clique-waitforoctagonupgrade", "octagon, failed to fetch OTControl object: %@", controlError);
1914 *error = controlError;
1916 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1920 __block BOOL ret = NO;
1921 __block NSError* blockError = nil;
1923 [control waitForOctagonUpgrade:OTCKContainerName context:OTDefaultContext reply:^(NSError *postError) {
1925 secnotice("clique-waitforoctagonupgrade", "error from control: %@", postError);
1926 blockError = postError;
1929 secnotice("clique-waitforoctagonupgrade", "successfully upgraded to octagon");
1934 if (blockError && error) {
1935 *error = blockError;
1937 subTaskSuccess = ret ? true : false;
1938 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1942 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1948 - (void)performedFailureCDPStateMachineRun:(OTCliqueCDPContextType)type
1949 error:(NSError * _Nullable)error
1950 reply:(void(^)(NSError* _Nullable error))reply
1952 [self performedCDPStateMachineRun:type success:NO error:error reply:reply];
1955 - (void)performedSuccessfulCDPStateMachineRun:(OTCliqueCDPContextType)type
1956 reply:(void(^)(NSError* _Nullable error))reply
1958 [self performedCDPStateMachineRun:type success:YES error:nil reply:reply];
1961 + (BOOL)setCDPEnabled:(OTConfigurationContext*)arguments
1962 error:(NSError* __autoreleasing*)error
1965 NSError *controlError = nil;
1966 OTControl* control = [arguments makeOTControl:&controlError];
1968 secerror("octagon-setcdpenabled: failed to fetch OTControl object: %@", controlError);
1970 *error = controlError;
1975 __block NSError* reterror = nil;
1977 [control setCDPEnabled:nil
1978 contextID:arguments.context
1979 reply:^(NSError * _Nullable resultError) {
1981 secnotice("octagon-setcdpenabled", "failed to set CDP bit: %@", resultError);
1982 reterror = resultError;
1984 secnotice("octagon-setcdpenabled", "successfully set CDP bit");
1988 if(reterror && error) {
1992 return (reterror == nil);
1995 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
2001 + (OTCDPStatus)getCDPStatus:(OTConfigurationContext*)arguments
2002 error:(NSError* __autoreleasing *)error
2005 NSError *controlError = nil;
2006 OTControl* control = [arguments makeOTControl:&controlError];
2008 secerror("octagon-cdp-status: failed to fetch OTControl object: %@", controlError);
2010 *error = controlError;
2012 return OTCDPStatusUnknown;
2015 __block NSError* reterror = nil;
2016 __block OTCDPStatus retcdpstatus = OTCDPStatusUnknown;
2018 [control getCDPStatus:nil
2019 contextID:arguments.context
2020 reply:^(OTCDPStatus status, NSError * _Nullable resultError) {
2022 secnotice("octagon-cdp-status", "failed to fetch CDP status: %@", resultError);
2023 reterror = resultError;
2026 secnotice("octagon-cdp-status", "successfully fetched CDP status as %@", OTCDPStatusToString(status));
2027 retcdpstatus = status;
2031 if(reterror && error) {
2035 return retcdpstatus;
2038 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
2040 return OTCDPStatusDisabled;
2044 + (OTClique* _Nullable)resetProtectedData:(OTConfigurationContext*)data error:(NSError**)error
2047 if ([OTClique isCloudServicesAvailable] == NO) {
2049 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
2053 NSError *controlError = nil;
2054 OTControl *control = [data makeOTControl:&controlError];
2056 secerror("clique-reset-protected-data: unable to create otcontrol: %@", controlError);
2058 *error = controlError;
2063 __block NSError* localError = nil;
2065 //delete all records
2066 id<OctagonEscrowRecovererPrococol> sb = data.sbd ?: [[getSecureBackupClass() alloc] init];
2068 NSDictionary* deletionInformation = @{ getkSecureBackupAuthenticationAppleID() : data.authenticationAppleID,
2069 getkSecureBackupAuthenticationPassword() : data.passwordEquivalentToken,
2070 getkSecureBackupiCloudDataProtectionDeleteAllRecordsKey() : @YES,
2071 getkSecureBackupContainsiCDPDataKey() : @YES};
2073 NSError* sbError = [sb disableWithInfo:deletionInformation];
2075 secerror("clique-reset-protected-data: secure backup escrow record deletion failed: %@", sbError);
2081 secnotice("clique-reset-protected-data", "sbd disableWithInfo succeeded");
2085 if(OctagonPlatformSupportsSOS()) {
2087 CFErrorRef sosError = NULL;
2088 bool resetSuccess = SOSCCResetToOffering(&sosError);
2090 if(sosError || !resetSuccess) {
2091 secerror("clique-reset-protected-data: sos reset failed: %@", sosError);
2093 *error = (NSError*)CFBridgingRelease(sosError);
2097 secnotice("clique-reset-protected-data", "sos reset succeeded");
2100 secnotice("clique-reset-protected-data", "platform does not support sos");
2104 OTClique* clique = [[OTClique alloc] initWithContextData:data];
2105 if(OctagonIsEnabled()) {
2106 [clique resetAndEstablish:CuttlefishResetReasonUserInitiatedReset error:&localError];
2109 secerror("clique-reset-protected-data: account reset failed: %@", localError);
2111 *error = localError;
2115 secnotice("clique-reset-protected-data", "Octagon account reset succeeded");
2122 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];