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/OTConstants.h"
31 #import "keychain/ot/OTDefines.h"
32 #import "keychain/SigninMetrics/OctagonSignPosts.h"
34 #import <utilities/SecCFWrappers.h>
35 #import <utilities/debugging.h>
37 #import "keychain/SecureObjectSync/SOSCloudCircle.h"
38 #import "KeychainCircle/PairingChannel.h"
39 #import <Security/SecBase.h>
41 const NSString* kSecEntitlementPrivateOctagonEscrow = @"com.apple.private.octagon.escrow-content";
44 #import <AuthKit/AuthKit.h>
45 #import <AuthKit/AuthKit_Private.h>
46 #import <SoftLinking/SoftLinking.h>
47 #import <CloudServices/SecureBackup.h>
48 #import <CloudServices/SecureBackupConstants.h>
49 #import "keychain/ot/OTControl.h"
50 #import "keychain/ckks/CKKSControl.h"
51 #import "keychain/ot/categories/OctagonEscrowRecoverer.h"
53 SOFT_LINK_FRAMEWORK(PrivateFrameworks, KeychainCircle);
54 SOFT_LINK_FRAMEWORK(PrivateFrameworks, CloudServices);
56 #pragma clang diagnostic push
57 #pragma clang diagnostic ignored "-Wstrict-prototypes"
58 SOFT_LINK_CLASS(KeychainCircle, KCPairingChannel);
59 SOFT_LINK_CLASS(KeychainCircle, OTPairingChannel);
60 SOFT_LINK_CLASS(CloudServices, SecureBackup);
61 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupErrorDomain, NSErrorDomain);
62 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupAuthenticationAppleID, NSString*);
63 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupAuthenticationPassword, NSString*);
64 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupiCloudDataProtectionDeleteAllRecordsKey, NSString*);
65 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupContainsiCDPDataKey, NSString*);
67 #pragma clang diagnostic pop
70 OTCliqueCDPContextType OTCliqueCDPContextTypeNone = @"cdpContextTypeNone";
71 OTCliqueCDPContextType OTCliqueCDPContextTypeSignIn = @"cdpContextTypeSignIn";
72 OTCliqueCDPContextType OTCliqueCDPContextTypeRepair = @"cdpContextTypeRepair";
73 OTCliqueCDPContextType OTCliqueCDPContextTypeFinishPasscodeChange = @"cdpContextTypeFinishPasscodeChange";
74 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyGenerate = @"cdpContextTypeRecoveryKeyGenerate";
75 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyNew = @"cdpContextTypeRecoveryKeyNew";
76 OTCliqueCDPContextType OTCliqueCDPContextTypeUpdatePasscode = @"cdpContextTypeUpdatePasscode";
78 NSString* OTCliqueStatusToString(CliqueStatus status)
82 return @"CliqueStatusIn";
83 case CliqueStatusNotIn:
84 return @"CliqueStatusNotIn";
85 case CliqueStatusPending:
86 return @"CliqueStatusPending";
87 case CliqueStatusAbsent:
88 return @"CliqueStatusAbsent";
89 case CliqueStatusNoCloudKitAccount:
90 return @"CliqueStatusNoCloudKitAccount";
91 case CliqueStatusError:
92 return @"CliqueStatusError";
95 CliqueStatus OTCliqueStatusFromString(NSString* str)
97 if([str isEqualToString: @"CliqueStatusIn"]) {
98 return CliqueStatusIn;
99 } else if([str isEqualToString: @"CliqueStatusNotIn"]) {
100 return CliqueStatusNotIn;
101 } else if([str isEqualToString: @"CliqueStatusPending"]) {
102 return CliqueStatusPending;
103 } else if([str isEqualToString: @"CliqueStatusAbsent"]) {
104 return CliqueStatusAbsent;
105 } else if([str isEqualToString: @"CliqueStatusNoCloudKitAccount"]) {
106 return CliqueStatusNoCloudKitAccount;
107 } else if([str isEqualToString: @"CliqueStatusError"]) {
108 return CliqueStatusError;
111 return CliqueStatusError;
114 NSString* OTCDPStatusToString(OTCDPStatus status) {
116 case OTCDPStatusUnknown:
118 case OTCDPStatusDisabled:
120 case OTCDPStatusEnabled:
126 @implementation OTConfigurationContext
127 - (OTControl* _Nullable)makeOTControl:(NSError**)error
130 if (self.otControl) {
131 return self.otControl;
133 return [OTControl controlObject:true error:error];
139 - (CKKSControl* _Nullable)makeCKKSControl:(NSError**)error
142 if(self.ckksControl) {
143 return self.ckksControl;
145 return [CKKSControl CKKSControlObject:true error:error];
153 if((self = [super init])) {
154 _context = OTDefaultContext;
160 @implementation OTBottleIDs
163 @implementation OTOperationConfiguration
165 - (instancetype)init {
166 if ((self = [super init]) == nil) {
169 _timeoutWaitForCKAccount = 10 * NSEC_PER_SEC;
170 _qualityOfService = NSQualityOfServiceDefault;
171 _discretionaryNetwork = NO;
172 _useCachedAccountStatus = NO;
176 + (BOOL)supportsSecureCoding {
180 - (void)encodeWithCoder:(nonnull NSCoder *)coder {
181 [coder encodeObject:@(_timeoutWaitForCKAccount) forKey:@"timeoutWaitForCKAccount"];
182 [coder encodeObject:@(_qualityOfService) forKey:@"qualityOfService"];
183 [coder encodeObject:@(_discretionaryNetwork) forKey:@"discretionaryNetwork"];
184 [coder encodeObject:@(_useCachedAccountStatus) forKey:@"useCachedAccountStatus"];
187 - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
188 _timeoutWaitForCKAccount = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"timeoutWaitForCKAccount"] unsignedLongLongValue];
189 _qualityOfService = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"qualityOfService"] integerValue];
190 _discretionaryNetwork = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"discretionaryNetwork"] boolValue];
191 _useCachedAccountStatus = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"useCachedAccountStatus"] boolValue];
198 @interface OTClique ()
199 @property (nonatomic, copy) NSString* cliqueMemberIdentifier;
200 @property (nonatomic, strong) OTConfigurationContext *ctx;
201 @property (nonatomic, strong) NSMutableDictionary *defaults;
204 @implementation OTClique
206 + (BOOL)platformSupportsSOS
208 return (OctagonPlatformSupportsSOS() && OctagonIsSOSFeatureEnabled());
211 // defaults write com.apple.security.octagon enable -bool YES
212 -(BOOL)isOctagonPairingEnabled {
213 BOOL nsDefaults = self.defaults[OTDefaultsOctagonEnable] ? [self.defaults[OTDefaultsOctagonEnable] boolValue] : OctagonIsEnabled();
214 secnotice("octagon", "pairing is %@", nsDefaults ? @"on" : @"off");
218 - (void)setPairingDefault:(BOOL)defaults
220 self.defaults[OTDefaultsOctagonEnable] = @(defaults);
223 - (void)removePairingDefault
225 [self.defaults removeObjectForKey:OTDefaultsOctagonEnable];
228 - (instancetype)initWithContextData:(OTConfigurationContext *)ctx error:(NSError * __autoreleasing *)error
230 return [self initWithContextData:ctx];
233 - (instancetype)initWithContextData:(OTConfigurationContext *)ctx
238 _ctx = [[OTConfigurationContext alloc]init];
239 _ctx.context = ctx.context ?: OTDefaultContext;
240 _ctx.dsid = [ctx.dsid copy];
241 _ctx.altDSID = [ctx.altDSID copy];
242 _ctx.analytics = ctx.analytics;
243 _ctx.otControl = ctx.otControl;
244 _ctx.ckksControl = ctx.ckksControl;
246 self.defaults = [NSMutableDictionary dictionary];
250 NSAssert(false, @"OTClique is not implemented on this platform");
252 // make the build analyzer happy
258 - (NSString* _Nullable)cliqueMemberIdentifier
261 __block NSString* retPeerID = nil;
262 __block bool subTaskSuccess = false;
264 OctagonSignpost fetchEgoPeerSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchEgoPeer);
265 if(OctagonIsEnabled()) {
266 NSError* localError = nil;
267 OTControl* control = [self makeOTControl:&localError];
269 secerror("octagon: Failed to create OTControl: %@", localError);
270 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
274 [control fetchEgoPeerID:nil
275 context:self.ctx.context
276 reply:^(NSString* peerID, NSError* error) {
278 secerror("octagon: Failed to fetch octagon peer ID: %@", error);
282 secnotice("clique", "cliqueMemberIdentifier(octagon) received %@", retPeerID);
285 if([OTClique platformSupportsSOS]) {
286 CFErrorRef error = NULL;
287 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(&error);
288 retPeerID = (NSString*)CFBridgingRelease(CFRetainSafe(SOSPeerInfoGetPeerID(me)));
290 CFBridgingRelease(error);
293 secnotice("clique", "cliqueMemberIdentifier complete: %@", retPeerID);
294 subTaskSuccess = retPeerID ? true : false;
295 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
303 - (OTControl* _Nullable)makeOTControl:(NSError**)error
305 return [self.ctx makeOTControl:error];
308 - (BOOL)establish:(NSError**)error
310 secnotice("clique-establish", "establish started");
311 OctagonSignpost establishSignPost = OctagonSignpostBegin(OctagonSignpostNameEstablish);
312 bool subTaskSuccess = false;
313 OTControl* control = [self makeOTControl:error];
315 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
319 __block BOOL success = NO;
320 __block NSError* localError = nil;
323 [control establish:nil context:self.ctx.context altDSID:self.ctx.altDSID reply:^(NSError * _Nullable operationError) {
325 secnotice("clique-establish", "establish returned an error: %@", operationError);
327 success = operationError == nil;
328 localError = operationError;
331 if(localError && error) {
334 secnotice("clique-establish", "establish complete: %@", success ? @"YES" : @"NO");
335 subTaskSuccess = success ? true : false;
336 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
341 - (BOOL)resetAndEstablish:(CuttlefishResetReason)resetReason error:(NSError**)error
343 secnotice("clique-resetandestablish", "resetAndEstablish started");
344 bool subTaskSuccess = false;
345 OctagonSignpost resetAndEstablishSignPost = OctagonSignpostBegin(OctagonSignpostNameResetAndEstablish);
347 OTControl* control = [self makeOTControl:error];
350 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
354 __block BOOL success = NO;
355 __block NSError* localError = nil;
356 [control resetAndEstablish:nil context:self.ctx.context altDSID:self.ctx.altDSID resetReason:resetReason reply:^(NSError * _Nullable operationError) {
359 secnotice("clique-resetandestablish", "resetAndEstablish returned an error: %@", operationError);
361 success = operationError == nil;
362 localError = operationError;
365 if(localError && error) {
369 secnotice("clique-resetandestablish", "establish complete: %@", success ? @"YES" : @"NO");
370 subTaskSuccess = success ? true : false;
371 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
377 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data error:(NSError * __autoreleasing *)error
379 return [OTClique newFriendsWithContextData:data resetReason:CuttlefishResetReasonUserInitiatedReset error:error];
382 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data resetReason:(CuttlefishResetReason)resetReason error:(NSError * __autoreleasing *)error
385 secnotice("clique-newfriends", "makeNewFriends invoked using context: %@, dsid: %@", data.context, data.dsid);
387 bool subTaskSuccess = false;
388 OctagonSignpost performEscrowRecoverySignpost = OctagonSignpostBegin(OctagonSignpostNameMakeNewFriends);
390 OTClique* clique = [[OTClique alloc] initWithContextData:data];
392 if(OctagonIsEnabled()) {
393 NSError* localError = nil;
394 [clique resetAndEstablish:resetReason error:&localError];
397 secnotice("clique-newfriends", "account reset failed: %@", localError);
401 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
404 secnotice("clique-newfriends", "Octagon account reset succeeded");
408 if([OTClique platformSupportsSOS]) {
409 CFErrorRef resetError = NULL;
410 result = SOSCCResetToOffering(&resetError);
412 if(!result || resetError){
413 secnotice("clique-newfriends", "newFriendsWithContextData: resetToOffering failed: %@", resetError);
415 *error = CFBridgingRelease(resetError);
417 CFBridgingRelease(resetError);
419 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
422 secnotice("clique-newfriends", "newFriendsWithContextData: reset the SOS circle");
424 secnotice("clique-newfriends", "newFriendsWithContextData: SOS disabled on this platform");
426 secnotice("clique-newfriends", "makeNewFriends complete");
428 subTaskSuccess = true;
429 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
435 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
440 + (OTClique* _Nullable)performEscrowRecoveryWithContextData:(OTConfigurationContext*)data
441 escrowArguments:(NSDictionary*)sbdRecoveryArguments
442 error:(NSError**)error
445 OctagonSignpost performEscrowRecoverySignpost = OctagonSignpostBegin(OctagonSignpostNamePerformEscrowRecovery);
446 bool subTaskSuccess = false;
447 NSError* localError = nil;
449 OTClique* clique = [[OTClique alloc] initWithContextData:data];
451 // Attempt the recovery from sbd
452 secnotice("clique-recovery", "attempting an escrow recovery for context:%@, altdsid:%@", data.context, data.altDSID);
453 id<OctagonEscrowRecovererPrococol> sb = data.sbd ?: [[getSecureBackupClass() alloc] init];
454 NSDictionary* recoveredInformation = nil;
456 OctagonSignpost recoverFromSBDSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformRecoveryFromSBD);
457 NSError* recoverError = [sb recoverWithInfo:sbdRecoveryArguments results:&recoveredInformation];
458 subTaskSuccess = (recoverError == nil) ? true : false;
459 OctagonSignpostEnd(recoverFromSBDSignPost, OctagonSignpostNamePerformRecoveryFromSBD, OctagonSignpostNumber1(OctagonSignpostNamePerformRecoveryFromSBD), (int)subTaskSuccess);
462 secnotice("clique-recovery", "sbd escrow recovery failed: %@", recoverError);
463 if(recoverError.code == 17 /* kSecureBackupRestoringLegacyBackupKeychainError */ && [recoverError.domain isEqualToString:getkSecureBackupErrorDomain()]) { /* XXX */
464 if([OTClique platformSupportsSOS]) {
465 secnotice("clique-recovery", "Can't restore legacy backup with no keybag. Resetting SOS to offering");
466 CFErrorRef blowItAwayError = NULL;
467 bool successfulReset = SOSCCResetToOffering(&blowItAwayError);
468 if(!successfulReset || blowItAwayError) {
469 secerror("clique-recovery: failed to reset to offering:%@", blowItAwayError);
471 secnotice("clique-recovery", "resetting SOS circle successful");
473 CFBridgingRelease(blowItAwayError);
475 secnotice("clique-recovery", "Legacy restore failed on a non-SOS platform");
479 *error = recoverError;
481 subTaskSuccess = false;
482 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
486 if(OctagonPlatformSupportsSOS()) { // Join if the legacy restore is complete now.
487 secnotice("clique-recovery", "attempting joinAfterRestore");
488 [clique joinAfterRestore:&localError];
489 secnotice("clique-recovery", "joinAfterRestore: %@", localError);
493 // look for OT Bottles
494 OTControl* control = [clique makeOTControl:&localError];
496 secnotice("clique-recovery", "unable to create otcontrol: %@", localError);
500 subTaskSuccess = false;
501 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
505 NSString *bottleID = recoveredInformation[@"bottleID"];
506 NSString *isValid = recoveredInformation[@"bottleValid"];
507 NSData *bottledPeerEntropy = recoveredInformation[@"EscrowServiceEscrowData"][@"BottledPeerEntropy"];
508 bool shouldResetOctagon = false;
510 if(bottledPeerEntropy && bottleID && [isValid isEqualToString:@"valid"]){
511 secnotice("clique-recovery", "recovering from bottle: %@", bottleID);
512 __block NSError* restoreBottleError = nil;
514 OctagonSignpost bottleRecoverySignPost = OctagonSignpostBegin(OctagonSignpostNamePerformBottleRecovery);
516 [control restore:OTCKContainerName
517 contextID:data.context
518 bottleSalt:data.altDSID
519 entropy:bottledPeerEntropy
521 reply:^(NSError * _Nullable restoreError) {
523 secnotice("clique-recovery", "restore bottle errored: %@", restoreError);
525 secnotice("clique-recovery", "restoring bottle succeeded");
527 restoreBottleError = restoreError;
530 subTaskSuccess = (restoreBottleError == nil) ? true : false;
531 OctagonSignpostEnd(bottleRecoverySignPost, OctagonSignpostNamePerformBottleRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformBottleRecovery), (int)subTaskSuccess);
533 if(restoreBottleError) {
535 *error = restoreBottleError;
537 subTaskSuccess = false;
538 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
542 shouldResetOctagon = true;
545 if(shouldResetOctagon) {
546 secnotice("clique-recovery", "bottle %@ is not valid, resetting octagon", bottleID);
547 NSError* resetError = nil;
549 OctagonSignpost resetSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle);
550 [clique resetAndEstablish:CuttlefishResetReasonNoBottleDuringEscrowRecovery error:&resetError];
551 subTaskSuccess = (resetError == nil) ? true : false;
552 OctagonSignpostEnd(resetSignPost, OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle, OctagonSignpostNumber1(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle), (int)subTaskSuccess);
555 secnotice("clique-recovery", "failed to reset octagon: %@", resetError);
557 secnotice("clique-recovery", "reset octagon succeeded");
561 secnotice("clique-recovery", "recovery complete: %@", clique);
563 subTaskSuccess = clique ? true : false;
564 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
569 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
576 - (KCPairingChannel *)setupPairingChannelAsInitiator:(KCPairingChannelContext *)ctx
579 return [getKCPairingChannelClass() pairingChannelInitiator:ctx];
585 - (KCPairingChannel * _Nullable)setupPairingChannelAsInitator:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
590 return [self setupPairingChannelAsInitiator:ctx];
593 - (KCPairingChannel *)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx
596 return [getKCPairingChannelClass() pairingChannelAcceptor:ctx];
602 - (KCPairingChannel * _Nullable)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
608 return [self setupPairingChannelAsAcceptor:ctx];
612 - (CliqueStatus)_fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing *)error
615 __block CliqueStatus sosStatus = CliqueStatusError;
616 __block CliqueStatus octagonStatus = CliqueStatusError;
617 bool subTaskSuccess = false;
619 OctagonSignpost fetchCliqueStatusSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchCliqueStatus);
621 // Octagon is supreme.
623 if(OctagonIsEnabled()) {
624 OTControl* control = [self makeOTControl:error];
626 secnotice("clique-status", "cliqueStatus noOTControl");
627 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
629 return CliqueStatusError;
632 __block NSError* localError = nil;
633 [control fetchCliqueStatus:nil context:self.ctx.context configuration:configuration reply:^(CliqueStatus cliqueStatus, NSError * _Nullable fetchError) {
635 octagonStatus = CliqueStatusError;
636 localError = fetchError;
637 secnotice("clique-status", "octagon clique status errored: %@", fetchError);
639 octagonStatus = cliqueStatus;
643 if(OctagonAuthoritativeTrustIsEnabled() || !OctagonPlatformSupportsSOS()) {
644 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) returning %@ (error: %@)",
645 configuration.useCachedAccountStatus ? "" : "non-",
646 self.ctx.context, self.ctx.altDSID,
647 OTCliqueStatusToString(octagonStatus), localError);
648 if (localError && error) {
650 subTaskSuccess = false;
652 subTaskSuccess = true;
654 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
655 return octagonStatus;
659 if([OTClique platformSupportsSOS]) {
660 CFErrorRef circleStatusError = NULL;
661 sosStatus = kSOSCCError;
662 if(configuration.useCachedAccountStatus){
663 sosStatus = SOSCCThisDeviceIsInCircle(&circleStatusError);
665 sosStatus = SOSCCThisDeviceIsInCircleNonCached(&circleStatusError);
667 secnotice("clique-status", "sos clique status is %d (%@)", (int)sosStatus, circleStatusError);
670 *error = (NSError*)CFBridgingRelease(circleStatusError);
672 CFBridgingRelease(circleStatusError);
675 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) complete: %@",
676 configuration.useCachedAccountStatus ? "" : "non-",
677 self.ctx.context, self.ctx.altDSID,
678 OTCliqueStatusToString(octagonStatus));
680 subTaskSuccess = true;
681 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
683 return octagonStatus;
686 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
688 return (CliqueStatus)kSOSCCError;
692 // Don't change rules for CoreCDP, and preserve legacy behavior for now
693 // preserve old behavior until CoreCDP can move to -fetchCliqueStatus:error:
694 #define LEGACY_WAITING_BEHAVIOR (TARGET_OS_OSX || TARGET_OS_IOS)
696 - (CliqueStatus)fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing * _Nonnull)error
698 return [self _fetchCliqueStatus:configuration error:error];
701 - (CliqueStatus)fetchCliqueStatus:(NSError * __autoreleasing *)error
703 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
704 #if LEGACY_WAITING_BEHAVIOR
705 configuration.timeoutWaitForCKAccount = 0;
707 return [self _fetchCliqueStatus:configuration error:error];
710 - (CliqueStatus)cachedCliqueStatus:(BOOL)usedCached error:(NSError * __autoreleasing *)error
712 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
713 #if LEGACY_WAITING_BEHAVIOR
714 configuration.timeoutWaitForCKAccount = 0;
717 configuration.useCachedAccountStatus = YES;
719 return [self _fetchCliqueStatus:configuration error:error];
723 - (BOOL)removeFriendsInClique:(NSArray<NSString*>*)friendIdentifiers error:(NSError * __autoreleasing *)error
726 secnotice("clique-removefriends", "removeFriendsInClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
727 OctagonSignpost removeFriendsSignPost = OctagonSignpostBegin(OctagonSignpostNameRemoveFriendsInClique);
728 bool subTaskSuccess = false;
730 // Annoying: we must sort friendIdentifiers into octagon/sos lists.
731 NSMutableArray<NSString*>* octagonIdentifiers = [NSMutableArray array];
732 NSMutableArray<NSString*>* sosIdentifiers = [NSMutableArray array];
734 for(NSString* friendIdentifier in friendIdentifiers) {
735 if([friendIdentifier hasPrefix:@"SHA256:"]) {
736 [octagonIdentifiers addObject: friendIdentifier];
738 [sosIdentifiers addObject: friendIdentifier];
742 // Ensure that we don't have any peers on the wrong platform
743 if(!OctagonIsEnabled() && octagonIdentifiers.count > 0) {
744 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
745 code:errSecUnimplemented
746 userInfo:@{NSLocalizedDescriptionKey: @"Octagon is disabled; can't distrust any Octagon peers"}];
747 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
751 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
755 if(!OctagonPlatformSupportsSOS() && sosIdentifiers.count > 0) {
756 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
757 code:errSecUnimplemented
758 userInfo:@{NSLocalizedDescriptionKey: @"SOS is not available on this platform; can't distrust any SOS peers"}];
759 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
763 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
768 __block NSError* localError = nil;
771 if(OctagonIsEnabled() && octagonIdentifiers.count > 0) {
772 OTControl* control = [self makeOTControl:error];
774 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
778 secnotice("clique-removefriends", "octagon: removing octagon friends: %@", octagonIdentifiers);
779 [control removeFriendsInClique:nil
780 context:self.ctx.context
781 peerIDs:octagonIdentifiers
782 reply:^(NSError* replyError) {
784 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", replyError);
785 localError = replyError;
787 secnotice("clique-removefriends", "octagon: friends removed: %@", octagonIdentifiers);
792 if([OTClique platformSupportsSOS] && sosIdentifiers.count >0) {
793 CFErrorRef removeFriendError = NULL;
794 NSData* analyticsData = nil;
796 secnotice("clique-removefriends", "removing sos friends: %@", sosIdentifiers);
798 if(self.ctx.analytics){
799 NSError* encodingError = nil;
800 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
804 result = SOSCCRemovePeersFromCircleWithAnalytics((__bridge CFArrayRef)friendIdentifiers, (__bridge CFDataRef)analyticsData, &removeFriendError);
806 result = SOSCCRemovePeersFromCircle((__bridge CFArrayRef)friendIdentifiers, &removeFriendError);
809 if(removeFriendError) {
810 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", removeFriendError);
811 localError = CFBridgingRelease(removeFriendError);
815 if(error && localError) {
818 secnotice("clique-removefriends", "removeFriendsInClique complete: %d", result);
820 subTaskSuccess = result;
821 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
823 return result && localError == nil;
829 - (BOOL)leaveClique:(NSError * __autoreleasing *)error
832 secnotice("clique-leaveClique", "leaveClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
833 CFErrorRef removeThisDeviceError = NULL;
835 bool subTaskSuccess = false;
837 OctagonSignpost leaveCliqueSignPost = OctagonSignpostBegin(OctagonSignpostNameLeaveClique);
839 if(OctagonIsEnabled()) {
840 OTControl* control = [self makeOTControl:error];
842 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
846 // We only want to issue a "leave" command if we're actively in a clique
847 __block NSError* localError = nil;
848 CliqueStatus currentStatus = [self fetchCliqueStatus:[[OTOperationConfiguration alloc] init]
852 secnotice("clique-leaveClique", "fetching current status errored: %@", localError);
856 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
860 if(currentStatus == CliqueStatusNotIn) {
861 secnotice("clique-leaveClique", "current status is Not In; no need to leave");
862 subTaskSuccess = true;
863 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
866 [control leaveClique:nil context:self.ctx.context reply:^(NSError * _Nullable leaveError) {
868 secnotice("clique-leaveClique", "leaveClique errored: %@", leaveError);
869 localError = leaveError;
871 secnotice("clique-leaveClique", "leaveClique success.");
878 result = !localError;
881 if([OTClique platformSupportsSOS]) {
882 NSData* analyticsData = nil;
884 if(self.ctx.analytics) {
885 NSError* encodingError = nil;
886 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
888 secnotice("clique-leaveClique", "leaveClique unable to archive analytics object: %@", encodingError);
893 result &= SOSCCRemoveThisDeviceFromCircleWithAnalytics((__bridge CFDataRef)analyticsData, &removeThisDeviceError);
895 result &= SOSCCRemoveThisDeviceFromCircle(&removeThisDeviceError);
898 *error = (NSError*)CFBridgingRelease(removeThisDeviceError);
900 CFBridgingRelease(removeThisDeviceError);
903 secnotice("clique-leaveClique", "leaveClique complete: %d", result);
905 subTaskSuccess = result;
906 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
908 return result ? YES : NO;
914 - (NSDictionary<NSString*,NSString*>* _Nullable)peerDeviceNamesByPeerID:(NSError * __autoreleasing *)error
917 secnotice("clique", "peerDeviceNamesByPeerID invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
918 OctagonSignpost peerNamesSignPost = OctagonSignpostBegin(OctagonSignpostNamePeerDeviceNamesByPeerID);
919 __block bool subTaskSuccess = false;
920 NSMutableDictionary<NSString*, NSString*>* retPeers = [NSMutableDictionary dictionary];
922 if(OctagonIsEnabled()) {
923 OTControl* control = [self makeOTControl:error];
925 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
929 __block NSError* localError = nil;
930 __block NSDictionary<NSString*, NSString*>* localPeers = nil;
932 [control peerDeviceNamesByPeerID:nil context:OTDefaultContext reply:^(NSDictionary<NSString*,NSString*>* peers, NSError* controlError) {
934 secnotice("clique", "peerDeviceNamesByPeerID errored: %@", controlError);
936 secnotice("clique", "peerDeviceNamesByPeerID succeeded: %@", peers);
938 localError = controlError;
942 if(error && localError) {
946 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
949 [retPeers addEntriesFromDictionary:localPeers];
950 secnotice("clique", "Received %lu Octagon peers", (unsigned long)localPeers.count);
953 if([OTClique platformSupportsSOS]) {
954 CFErrorRef peerErrorRef = NULL;
955 NSMutableDictionary<NSString*,NSString*>* peerMapping = [NSMutableDictionary dictionary];
956 NSArray* arrayOfPeerRefs = CFBridgingRelease(SOSCCCopyPeerPeerInfo(&peerErrorRef));
958 [arrayOfPeerRefs enumerateObjectsUsingBlock:^(id peerRef, NSUInteger idx, BOOL * stop) {
959 SOSPeerInfoRef peer = (__bridge SOSPeerInfoRef)peerRef;
961 [peerMapping setObject:(__bridge NSString*)SOSPeerInfoGetPeerName(peer) forKey:(__bridge NSString*)SOSPeerInfoGetPeerID(peer)];
965 subTaskSuccess = (peerErrorRef == NULL || [retPeers count] == 0) ? true : false;
968 *error = (NSError*)CFBridgingRelease(peerErrorRef);
970 CFBridgingRelease(peerErrorRef);
972 [retPeers addEntriesFromDictionary:peerMapping];
973 secnotice("clique", "Received %lu SOS peers", (unsigned long)peerMapping.count);
976 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
983 - (BOOL)joinAfterRestore:(NSError * __autoreleasing *)error
985 secnotice("clique-recovery", "joinAfterRestore for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
986 OctagonSignpost joinAfterRestoreSignPost = OctagonSignpostBegin(OctagonSignpostNameJoinAfterRestore);
987 bool subTaskSuccess = false;
989 if([OTClique platformSupportsSOS]) {
990 CFErrorRef restoreError = NULL;
991 bool res = SOSCCRequestToJoinCircleAfterRestore(&restoreError);
993 *error = (NSError*)CFBridgingRelease(restoreError);
995 CFBridgingRelease(restoreError);
997 secnotice("clique-recovery", "joinAfterRestore complete: %d %@", res, error ? *error : @"no error pointer provided");
999 subTaskSuccess = res;
1000 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
1002 return res ? YES : NO;
1004 secnotice("clique-recovery", "SOS disabled for this platform, returning NO");
1006 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1007 code:errSecUnimplemented
1008 userInfo:@{NSLocalizedDescriptionKey: @"join after restore unimplemented"}];
1010 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
1015 - (BOOL)safariPasswordSyncingEnabled:(NSError **)error
1017 secnotice("clique-safari", "safariPasswordSyncingEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1018 OctagonSignpost safariSyncingEnabledSignPost = OctagonSignpostBegin(OctagonSignpostNameSafariPasswordSyncingEnabled);
1019 bool subTaskSuccess = false;
1021 if([OTClique platformSupportsSOS]) {
1022 CFErrorRef viewErrorRef = NULL;
1024 SOSViewResultCode result = SOSCCView(kSOSViewAutofillPasswords, kSOSCCViewQuery, &viewErrorRef);
1025 subTaskSuccess = (viewErrorRef == NULL) ? true : false;
1027 BOOL viewMember = result == kSOSCCViewMember;
1029 *error = (NSError*)CFBridgingRelease(viewErrorRef);
1031 CFBridgingRelease(viewErrorRef);
1033 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1035 secnotice("clique-safari", "safariPasswordSyncingEnabled complete: %@", viewMember ? @"YES" : @"NO");
1038 secnotice("clique-safari", "SOS disabled for this platform, returning NO");
1040 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1041 code:errSecUnimplemented
1042 userInfo:@{NSLocalizedDescriptionKey: @"safari password syncing enabled unimplemented"}];
1044 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1049 - (BOOL)isLastFriend:(NSError **)error
1051 secnotice("clique-isLastFriend", "is last friend");
1055 - (BOOL)waitForInitialSync:(NSError *__autoreleasing*)error
1057 secnotice("clique-legacy", "waitForInitialSync for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1059 OctagonSignpost waitForInitialSyncSignPost = OctagonSignpostBegin(OctagonSignpostNameWaitForInitialSync);
1060 bool subTaskSuccess = false;
1062 if([OTClique platformSupportsSOS]) {
1063 CFErrorRef initialSyncErrorRef = NULL;
1064 bool result = false;
1065 if(self.ctx.analytics){
1066 NSError* encodingError = nil;
1067 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1068 if(!encodingError && analyticsData){
1069 result = SOSCCWaitForInitialSyncWithAnalytics((__bridge CFDataRef)analyticsData, &initialSyncErrorRef);
1071 result = SOSCCWaitForInitialSync(&initialSyncErrorRef);
1074 result = SOSCCWaitForInitialSync(&initialSyncErrorRef);
1077 BOOL initialSyncResult = result ? YES : NO;
1079 *error = (NSError*)CFBridgingRelease(initialSyncErrorRef);
1081 CFBridgingRelease(initialSyncErrorRef);
1083 secnotice("clique-legacy", "waitForInitialSync waited: %d %@", initialSyncResult, error ? *error : @"no error pointer provided");
1085 subTaskSuccess = initialSyncResult ? true : false;
1086 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1088 return initialSyncResult;
1090 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1092 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1093 code:errSecUnimplemented
1094 userInfo:@{NSLocalizedDescriptionKey: @"wait for initial sync unimplemented"}];
1096 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1101 - (NSArray* _Nullable)copyViewUnawarePeerInfo:(NSError *__autoreleasing*)error
1103 secnotice("clique-legacy", "copyViewUnawarePeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1105 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyViewUnawarePeerInfo);
1106 bool subTaskSuccess = false;
1108 if([OTClique platformSupportsSOS]) {
1109 CFErrorRef copyViewUnawarePeerInfoErrorRef = NULL;
1110 CFArrayRef peerListRef = SOSCCCopyViewUnawarePeerInfo(©ViewUnawarePeerInfoErrorRef);
1112 NSArray* peerList = (peerListRef ? (NSArray*)(CFBridgingRelease(peerListRef)) : nil);
1114 *error = (NSError*)CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1116 CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1118 subTaskSuccess = (peerList != nil) ? true : false;
1119 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1123 secnotice("clique-legacy", "SOS disabled for this platform, returning NULL");
1125 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1126 code:errSecUnimplemented
1127 userInfo:@{NSLocalizedDescriptionKey: @"copy view unaware peer info unimplemented"}];
1129 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1134 - (BOOL)viewSet:(NSSet*)enabledViews disabledViews:(NSSet*)disabledViews
1136 secnotice("clique-legacy", "viewSet for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1137 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameViewSet);
1138 bool subTaskSuccess = false;
1140 if([OTClique platformSupportsSOS]) {
1141 bool result = false;
1142 if(self.ctx.analytics){
1143 NSError* encodingError = nil;
1144 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1145 if(!encodingError && analyticsData){
1146 result = SOSCCViewSetWithAnalytics((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews, (__bridge CFDataRef)analyticsData);
1148 result = SOSCCViewSet((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews);
1151 result = SOSCCViewSet((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews);
1154 BOOL viewSetResult = result ? YES : NO;
1155 subTaskSuccess = result;
1156 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1157 return viewSetResult;
1159 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1160 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1165 - (BOOL)setUserCredentialsAndDSID:(NSString*)userLabel
1166 password:(NSData*)userPassword
1167 error:(NSError *__autoreleasing*)error
1169 secnotice("clique-legacy", "setUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1170 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetUserCredentialsAndDSID);
1171 bool subTaskSuccess = false;
1173 if([OTClique platformSupportsSOS]) {
1174 CFErrorRef setCredentialsErrorRef = NULL;
1175 bool result = false;
1176 if(self.ctx.analytics){
1177 NSError* encodingError = nil;
1178 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1179 if(!encodingError && analyticsData){
1180 result = SOSCCSetUserCredentialsAndDSIDWithAnalytics((__bridge CFStringRef)userLabel,
1181 (__bridge CFDataRef)userPassword,
1182 (__bridge CFStringRef)self.ctx.dsid,
1183 (__bridge CFDataRef)analyticsData,
1184 &setCredentialsErrorRef);
1186 result = SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1187 (__bridge CFDataRef)userPassword,
1188 (__bridge CFStringRef)self.ctx.dsid,
1189 &setCredentialsErrorRef);
1192 result = SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1193 (__bridge CFDataRef)userPassword,
1194 (__bridge CFStringRef)self.ctx.dsid,
1195 &setCredentialsErrorRef);
1198 BOOL setCredentialsResult = result ? YES : NO;
1199 secnotice("clique-legacy", "setUserCredentialsAndDSID results: %d %@", setCredentialsResult, setCredentialsErrorRef);
1201 *error = (NSError*)CFBridgingRelease(setCredentialsErrorRef);
1203 CFBridgingRelease(setCredentialsErrorRef);
1205 subTaskSuccess = result;
1206 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1208 return setCredentialsResult;
1210 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1212 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1213 code:errSecUnimplemented
1214 userInfo:@{NSLocalizedDescriptionKey: @"set user credentials unimplemented"}];
1216 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1221 - (BOOL)tryUserCredentialsAndDSID:(NSString*)userLabel
1222 password:(NSData*)userPassword
1223 error:(NSError *__autoreleasing*)error
1225 secnotice("clique-legacy", "tryUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1226 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameTryUserCredentialsAndDSID);
1227 bool subTaskSuccess = false;
1229 if([OTClique platformSupportsSOS]) {
1230 CFErrorRef tryCredentialsErrorRef = NULL;
1231 bool result = SOSCCTryUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1232 (__bridge CFDataRef)userPassword,
1233 (__bridge CFStringRef)self.ctx.dsid,
1234 &tryCredentialsErrorRef);
1236 BOOL tryCredentialsResult = result ? YES : NO;
1237 secnotice("clique-legacy", "tryUserCredentialsAndDSID results: %d %@", tryCredentialsResult, tryCredentialsErrorRef);
1239 *error = (NSError*)CFBridgingRelease(tryCredentialsErrorRef);
1241 CFBridgingRelease(tryCredentialsErrorRef);
1243 subTaskSuccess = result;
1244 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1245 return tryCredentialsResult;
1248 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1250 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1251 code:errSecUnimplemented
1252 userInfo:@{NSLocalizedDescriptionKey: @"try user credentials unimplemented"}];
1254 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1259 - (NSArray* _Nullable)copyPeerPeerInfo:(NSError *__autoreleasing*)error
1261 secnotice("clique-legacy", "copyPeerPeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1262 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyPeerPeerInfo);
1263 bool subTaskSuccess = false;
1265 if([OTClique platformSupportsSOS]) {
1266 CFErrorRef copyPeerErrorRef = NULL;
1267 CFArrayRef result = SOSCCCopyPeerPeerInfo(©PeerErrorRef);
1269 NSArray* peerList = (result ? (NSArray*)(CFBridgingRelease(result)) : nil);
1271 secnotice("clique-legacy", "copyPeerPeerInfo results: %@ (%@)", peerList, copyPeerErrorRef);
1273 *error = (NSError*)CFBridgingRelease(copyPeerErrorRef);
1275 CFBridgingRelease(copyPeerErrorRef);
1277 subTaskSuccess = (peerList != nil) ? true : false;
1278 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1281 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1283 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1284 code:errSecUnimplemented
1285 userInfo:@{NSLocalizedDescriptionKey: @"copy peer peer info unimplemented"}];
1287 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1292 - (BOOL)peersHaveViewsEnabled:(NSArray<NSString*>*)viewNames error:(NSError *__autoreleasing*)error
1294 secnotice("clique-legacy", "peersHaveViewsEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1295 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePeersHaveViewsEnabled);
1296 bool subTaskSuccess = false;
1298 if([OTClique platformSupportsSOS]) {
1299 CFErrorRef viewsEnabledErrorRef = NULL;
1300 BOOL viewsEnabledResult = NO;
1302 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef)viewNames, &viewsEnabledErrorRef);
1304 viewsEnabledResult = CFBooleanGetValue(result) ? YES : NO;
1306 secnotice("clique-legacy", "peersHaveViewsEnabled results: %@ (%@)", viewsEnabledResult ? @"YES" : @"NO",
1307 viewsEnabledErrorRef);
1309 *error = (NSError*)CFBridgingRelease(viewsEnabledErrorRef);
1311 CFBridgingRelease(viewsEnabledErrorRef);
1313 subTaskSuccess = viewsEnabledResult ? true : false;
1314 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1315 return viewsEnabledResult;
1317 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1319 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1320 code:errSecUnimplemented
1321 userInfo:@{NSLocalizedDescriptionKey: @"peers have views enabled unimplemented"}];
1323 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1328 - (BOOL)requestToJoinCircle:(NSError *__autoreleasing*)error
1330 bool result = false;
1331 bool subTaskSuccess = false;
1332 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameRequestToJoinCircle);
1335 secnotice("clique-legacy", "requestToJoinCircle for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1337 if(OctagonIsEnabled()) {
1338 // Sometimes, CoreCDP calls this to cause a circle creation to occur.
1339 // So, for Octagon, we might want to request a establish, but not a reset.
1341 // Fetch the current trust status, so we know if we should fire off the establish.
1342 NSError* localError = nil;
1343 CliqueStatus status = [self fetchCliqueStatus: &localError];
1346 secnotice("clique-legacy", "fetching clique status failed: %@", localError);
1348 *error = localError;
1350 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1354 if(status == CliqueStatusAbsent) {
1355 secnotice("clique-legacy", "clique status is %@; beginning an establish", OTCliqueStatusToString(status));
1356 [self establish:&localError];
1360 *error = localError;
1362 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1365 secnotice("clique-legacy", "establish succeeded");
1368 secnotice("clique-legacy", "clique status is %@; performing no Octagon actions", OTCliqueStatusToString(status));
1371 // If we didn't early-exit, and we aren't going to invoke SOS below, we succeeded.
1372 if(!OctagonPlatformSupportsSOS()) {
1373 secnotice("clique-legacy", "requestToJoinCircle platform does not support SOS");
1374 subTaskSuccess = true;
1375 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1381 if([OTClique platformSupportsSOS]) {
1382 NSData* analyticsData = nil;
1383 CFErrorRef joinErrorRef = NULL;
1384 if(self.ctx.analytics){
1385 NSError* encodingError = nil;
1386 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1390 result = SOSCCRequestToJoinCircleWithAnalytics((__bridge CFDataRef)analyticsData, &joinErrorRef);
1392 result = SOSCCRequestToJoinCircle(&joinErrorRef);
1395 secnotice("clique-legacy", "sos requestToJoinCircle complete: %d %@", result, joinErrorRef);
1397 *error = (NSError*)CFBridgingRelease(joinErrorRef);
1399 CFBridgingRelease(joinErrorRef);
1403 subTaskSuccess = result;
1404 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1406 return result ? YES : NO;
1409 - (BOOL)accountUserKeyAvailable
1411 secnotice("clique-legacy", "accountUserKeyAvailable for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1412 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameAccountUserKeyAvailable);
1413 bool subTaskSuccess = false;
1415 if([OTClique platformSupportsSOS]) {
1416 BOOL canAuthenticate = SOSCCCanAuthenticate(NULL) ? YES : NO;
1417 if (canAuthenticate == NO) {
1418 secnotice("clique-legacy", "Security requires credentials...");
1420 subTaskSuccess = canAuthenticate ? true : false;
1421 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1422 return canAuthenticate;
1424 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1425 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1430 // MARK: SBD interfaces
1431 + (OTBottleIDs* _Nullable)findOptimalBottleIDsWithContextData:(OTConfigurationContext*)data
1432 error:(NSError**)error
1435 secnotice("clique-findbottle", "finding optimal bottles for context:%@, altdsid:%@", data.context, data.altDSID);
1436 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFindOptimalBottleIDsWithContextData);
1437 bool subTaskSuccess = false;
1439 if(OctagonIsEnabled()) {
1440 __block NSError* localError = nil;
1441 __block NSArray<NSString*>* localViableBottleIDs = nil;
1442 __block NSArray<NSString*>* localPartiallyViableBottleIDs = nil;
1444 OTControl *control = [data makeOTControl:&localError];
1446 secnotice("clique-findbottle", "unable to create otcontrol: %@", localError);
1448 *error = localError;
1450 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1453 [control fetchAllViableBottles:OTCKContainerName
1454 context:data.context
1455 reply:^(NSArray<NSString *> * _Nullable sortedBottleIDs,
1456 NSArray<NSString*> * _Nullable sortedPartialBottleIDs,
1457 NSError * _Nullable fetchError) {
1459 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData errored: %@", fetchError);
1461 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData succeeded: %@, %@", sortedBottleIDs, sortedPartialBottleIDs);
1463 localError = fetchError;
1464 localViableBottleIDs = sortedBottleIDs;
1465 localPartiallyViableBottleIDs = sortedPartialBottleIDs;
1468 if(error && localError) {
1469 *error = localError;
1471 OTBottleIDs* bottleIDs = [[OTBottleIDs alloc] init];
1472 bottleIDs.preferredBottleIDs = localViableBottleIDs;
1473 bottleIDs.partialRecoveryBottleIDs = localPartiallyViableBottleIDs;
1475 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData complete");
1477 subTaskSuccess = (localError == nil) ? true : false;
1478 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1481 // With octagon off, fail with 'unimplemented'
1483 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1484 code:errSecUnimplemented
1485 userInfo:@{NSLocalizedDescriptionKey: @"optimal bottle IDs unimplemented"}];
1487 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1492 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1498 + (OTClique* _Nullable)recoverWithContextData:(OTConfigurationContext*)data
1499 bottleID:(NSString*)bottleID
1500 escrowedEntropy:(NSData*)entropy
1501 error:(NSError**)error
1504 secnotice("octagon", "replaced by performEscrowRecoveryWithContextData:escrowArguments:error: remove call");
1508 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1514 // used by sbd to fill in the escrow record
1515 // TODO: what extra entitlement do you need to call this?
1516 - (void)fetchEscrowContents:(void (^)(NSData* _Nullable entropy,
1517 NSString* _Nullable bottleID,
1518 NSData* _Nullable signingPublicKey,
1519 NSError* _Nullable error))reply
1522 secnotice("clique-fetchescrow", "fetching entropy for bottling for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1523 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFetchEscrowContents);
1524 __block bool subTaskSuccess = false;
1525 if(OctagonIsEnabled()) {
1526 NSError* controlError = nil;
1527 OTControl* control = [self makeOTControl:&controlError];
1529 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1530 reply(nil, nil, nil, controlError);
1533 [control fetchEscrowContents:OTCKContainerName
1534 contextID:self.ctx.context
1535 reply:^(NSData * _Nullable entropy,
1536 NSString * _Nullable bottleID,
1537 NSData * _Nullable signingPublicKey,
1538 NSError * _Nullable error) {
1540 secnotice("clique-fetchescrow", "fetchEscrowContents errored: %@", error);
1542 secnotice("clique-fetchescrow","fetchEscrowContents succeeded");
1544 subTaskSuccess = (error == nil) ? true : false;
1545 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1546 reply (entropy, bottleID, signingPublicKey, error);
1549 // With octagon off, fail with 'unimplemented'
1550 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1551 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain
1552 code:errSecUnimplemented
1553 userInfo:@{NSLocalizedDescriptionKey: @"fetchEscrowRecordContents unimplemented"}]);
1556 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1560 + (void)setNewRecoveryKeyWithData:(OTConfigurationContext *)ctx
1561 recoveryKey:(NSString*)recoveryKey reply:(nonnull void (^)(SecRecoveryKey *rk, NSError *error))reply
1564 secnotice("octagon-setrecoverykey", "setNewRecoveryKeyWithData invoked for context: %@", ctx.context);
1565 //set the recovery key for SOS
1566 NSError* createRecoveryKeyError = nil;
1567 NSMutableDictionary *userInfo = [NSMutableDictionary new];
1568 NSError* retError = nil;
1569 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetNewRecoveryKeyWithData);
1570 __block bool subTaskSuccess = false;
1572 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(recoveryKey, &createRecoveryKeyError);
1574 secerror("octagon-setrecoverykey, SecRKCreateRecoveryKeyWithError() failed: %@", createRecoveryKeyError);
1575 userInfo[NSLocalizedDescriptionKey] = @"SecRKCreateRecoveryKeyWithError() failed";
1576 userInfo[NSUnderlyingErrorKey] = createRecoveryKeyError;
1577 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1578 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1579 reply(nil, retError);
1582 if([OTClique platformSupportsSOS]) {
1583 CFErrorRef registerError = nil;
1584 if (!SecRKRegisterBackupPublicKey(rk, ®isterError)) {
1585 secerror("octagon-setrecoverykey, SecRKRegisterBackupPublicKey() failed: %@", registerError);
1586 NSError *underlyingError = CFBridgingRelease(registerError);
1587 userInfo[NSLocalizedDescriptionKey] = @"SecRKRegisterBackupPublicKey() failed";
1588 userInfo[NSUnderlyingErrorKey] = underlyingError;
1589 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1590 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1591 reply(nil,retError);
1594 secnotice("octagon-setrecoverykey", "successfully registered recovery key for SOS");
1598 //set the recovery key for Octagon
1599 if(OctagonRecoveryKeyIsEnabled()) {
1600 NSError* controlError = nil;
1601 OTControl* control = [ctx makeOTControl:&controlError];
1603 secnotice("octagon-setrecoverykey", "failed to fetch OTControl object: %@", controlError);
1604 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1605 reply(nil, controlError);
1608 [control createRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError * createError) {
1610 secerror("octagon-setrecoverykey, failed to create octagon recovery key");
1611 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1612 reply(nil, createError);
1615 secnotice("octagon-setrecoverykey", "successfully set octagon recovery key");
1616 subTaskSuccess = true;
1617 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1624 reply(nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1628 + (void)recoverOctagonUsingData:(OTConfigurationContext *)ctx
1629 recoveryKey:(NSString*)recoveryKey
1630 reply:(void(^)(NSError* _Nullable error))reply
1633 OctagonSignpost signpost = OctagonSignpostBegin(OctagonSignpostNameRecoverOctagonUsingData);
1634 __block bool subTaskSuccess = false;
1636 if(OctagonRecoveryKeyIsEnabled()) {
1637 NSError* controlError = nil;
1638 OTControl* control = [ctx makeOTControl:&controlError];
1640 secnotice("clique-recoverykey", "join using recovery key");
1643 secnotice("clique-recoverykey", "failed to fetch OTControl object: %@", controlError);
1644 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1645 reply(controlError);
1648 [control joinWithRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError *joinError) {
1650 secnotice("clique-recoverykey", "failed to join using recovery key: %@", joinError);
1651 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1655 secnotice("clique-recoverykey", "successfully joined using recovery key");
1656 subTaskSuccess = true;
1657 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1663 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1667 - (void)performedCDPStateMachineRun:(OTCliqueCDPContextType)type
1668 success:(BOOL)success
1669 error:(NSError * _Nullable)error
1670 reply:(void(^)(NSError* _Nullable error))reply
1673 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePerformedCDPStateMachineRun);
1674 NSError* controlError = nil;
1675 __block bool subTaskSuccess = false;
1677 OTControl* control = [self makeOTControl:&controlError];
1679 secnotice("clique-cdp-sm", "octagon, failed to fetch OTControl object: %@", controlError);
1680 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1681 reply(controlError);
1685 [control postCDPFollowupResult:success type:type error:error containerName:OTCKContainerName contextName:OTDefaultContext reply:^(NSError *postError) {
1687 secnotice("clique-cdp-sm", "failed to post %@ result: %@ ", type, postError);
1688 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1693 secnotice("clique-cdp-sm", "posted success: %@", type);
1695 secnotice("clique-cdp-sm", "posted error: %@: %@", type, error);
1697 subTaskSuccess = success ? true : false;
1698 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1702 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1706 - (BOOL)waitForOctagonUpgrade:(NSError** _Nullable)error
1709 OTControl* control = nil;
1710 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameWaitForOctagonUpgrade);
1711 __block bool subTaskSuccess = false;
1713 if (!OctagonIsEnabled()) {
1714 secnotice("clique-waitforoctagonupgrade", "cannot upgrade, octagon is not enabled");
1716 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:@{NSLocalizedDescriptionKey: @"Octagon is not enabled"}];
1718 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1723 NSError *controlError = nil;
1724 control = [self makeOTControl:&controlError];
1726 secnotice("clique-waitforoctagonupgrade", "octagon, failed to fetch OTControl object: %@", controlError);
1728 *error = controlError;
1730 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1734 __block BOOL ret = NO;
1735 __block NSError* blockError = nil;
1737 [control waitForOctagonUpgrade:OTCKContainerName context:OTDefaultContext reply:^(NSError *postError) {
1739 secnotice("clique-waitforoctagonupgrade", "error from control: %@", postError);
1740 blockError = postError;
1743 secnotice("clique-waitforoctagonupgrade", "successfully upgraded to octagon");
1748 if (blockError && error) {
1749 *error = blockError;
1751 subTaskSuccess = ret ? true : false;
1752 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1756 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1762 - (void)performedFailureCDPStateMachineRun:(OTCliqueCDPContextType)type
1763 error:(NSError * _Nullable)error
1764 reply:(void(^)(NSError* _Nullable error))reply
1766 [self performedCDPStateMachineRun:type success:NO error:error reply:reply];
1769 - (void)performedSuccessfulCDPStateMachineRun:(OTCliqueCDPContextType)type
1770 reply:(void(^)(NSError* _Nullable error))reply
1772 [self performedCDPStateMachineRun:type success:YES error:nil reply:reply];
1775 + (BOOL)setCDPEnabled:(OTConfigurationContext*)arguments
1776 error:(NSError* __autoreleasing*)error
1779 NSError *controlError = nil;
1780 OTControl* control = [arguments makeOTControl:&controlError];
1782 secerror("octagon-setcdpenabled: failed to fetch OTControl object: %@", controlError);
1784 *error = controlError;
1789 __block NSError* reterror = nil;
1791 [control setCDPEnabled:nil
1792 contextID:arguments.context
1793 reply:^(NSError * _Nullable resultError) {
1795 secnotice("octagon-setcdpenabled", "failed to set CDP bit: %@", resultError);
1796 reterror = resultError;
1798 secnotice("octagon-setcdpenabled", "successfully set CDP bit");
1802 if(reterror && error) {
1806 return (reterror == nil);
1809 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1815 + (OTCDPStatus)getCDPStatus:(OTConfigurationContext*)arguments
1816 error:(NSError* __autoreleasing *)error
1819 NSError *controlError = nil;
1820 OTControl* control = [arguments makeOTControl:&controlError];
1822 secerror("octagon-cdp-status: failed to fetch OTControl object: %@", controlError);
1824 *error = controlError;
1826 return OTCDPStatusUnknown;
1829 __block NSError* reterror = nil;
1830 __block OTCDPStatus retcdpstatus = OTCDPStatusUnknown;
1832 [control getCDPStatus:nil
1833 contextID:arguments.context
1834 reply:^(OTCDPStatus status, NSError * _Nullable resultError) {
1836 secnotice("octagon-cdp-status", "failed to fetch CDP status: %@", resultError);
1837 reterror = resultError;
1840 secnotice("octagon-cdp-status", "successfully fetched CDP status as %@", OTCDPStatusToString(status));
1841 retcdpstatus = status;
1845 if(reterror && error) {
1849 return retcdpstatus;
1852 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1854 return OTCDPStatusDisabled;
1858 + (OTClique* _Nullable)resetProtectedData:(OTConfigurationContext*)data error:(NSError**)error
1861 NSError *controlError = nil;
1862 OTControl *control = [data makeOTControl:&controlError];
1864 secerror("clique-reset-protected-data: unable to create otcontrol: %@", controlError);
1866 *error = controlError;
1871 __block NSError* localError = nil;
1873 //delete all records
1874 id<OctagonEscrowRecovererPrococol> sb = data.sbd ?: [[getSecureBackupClass() alloc] init];
1876 NSDictionary* deletionInformation = @{ getkSecureBackupAuthenticationAppleID() : data.authenticationAppleID,
1877 getkSecureBackupAuthenticationPassword() : data.passwordEquivalentToken,
1878 getkSecureBackupiCloudDataProtectionDeleteAllRecordsKey() : @YES,
1879 getkSecureBackupContainsiCDPDataKey() : @YES};
1881 NSError* sbError = [sb disableWithInfo:deletionInformation];
1883 secerror("clique-reset-protected-data: secure backup escrow record deletion failed: %@", sbError);
1889 secnotice("clique-reset-protected-data", "sbd disableWithInfo succeeded");
1893 if(OctagonPlatformSupportsSOS()) {
1895 CFErrorRef sosError = NULL;
1896 bool resetSuccess = SOSCCResetToOffering(&sosError);
1898 if(sosError || !resetSuccess) {
1899 secerror("clique-reset-protected-data: sos reset failed: %@", sosError);
1901 *error = (NSError*)CFBridgingRelease(sosError);
1905 secnotice("clique-reset-protected-data", "sos reset succeeded");
1908 secnotice("clique-reset-protected-data", "platform does not support sos");
1912 OTClique* clique = [[OTClique alloc] initWithContextData:data];
1913 if(OctagonIsEnabled()) {
1914 [clique resetAndEstablish:CuttlefishResetReasonUserInitiatedReset error:&localError];
1917 secerror("clique-reset-protected-data: account reset failed: %@", localError);
1919 *error = localError;
1923 secnotice("clique-reset-protected-data", "Octagon account reset succeeded");
1928 CKKSControl* ckksControl = [data makeCKKSControl:&localError];
1929 [ckksControl rpcResetCloudKit:nil reason:@"clique-reset-protected-data" reply:^(NSError* resetError){
1930 localError = resetError;
1934 secerror("clique-reset-protected-data: ckks cloudkit reset failed: %@", localError);
1936 *error = localError;
1940 secnotice("clique-reset-protected-data", "ckks cloudkit reset succeeded");
1946 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];