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/ot/categories/OctagonEscrowRecoverer.h"
52 SOFT_LINK_FRAMEWORK(PrivateFrameworks, KeychainCircle);
53 SOFT_LINK_FRAMEWORK(PrivateFrameworks, CloudServices);
55 #pragma clang diagnostic push
56 #pragma clang diagnostic ignored "-Wstrict-prototypes"
57 SOFT_LINK_CLASS(KeychainCircle, KCPairingChannel);
58 SOFT_LINK_CLASS(KeychainCircle, OTPairingChannel);
59 SOFT_LINK_CLASS(CloudServices, SecureBackup);
60 SOFT_LINK_CONSTANT(CloudServices, kSecureBackupErrorDomain, NSErrorDomain);
62 #pragma clang diagnostic pop
65 OTCliqueCDPContextType OTCliqueCDPContextTypeNone = @"cdpContextTypeNone";
66 OTCliqueCDPContextType OTCliqueCDPContextTypeSignIn = @"cdpContextTypeSignIn";
67 OTCliqueCDPContextType OTCliqueCDPContextTypeRepair = @"cdpContextTypeRepair";
68 OTCliqueCDPContextType OTCliqueCDPContextTypeFinishPasscodeChange = @"cdpContextTypeFinishPasscodeChange";
69 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyGenerate = @"cdpContextTypeRecoveryKeyGenerate";
70 OTCliqueCDPContextType OTCliqueCDPContextTypeRecoveryKeyNew = @"cdpContextTypeRecoveryKeyNew";
71 OTCliqueCDPContextType OTCliqueCDPContextTypeUpdatePasscode = @"cdpContextTypeUpdatePasscode";
73 NSString* OTCliqueStatusToString(CliqueStatus status)
77 return @"CliqueStatusIn";
78 case CliqueStatusNotIn:
79 return @"CliqueStatusNotIn";
80 case CliqueStatusPending:
81 return @"CliqueStatusPending";
82 case CliqueStatusAbsent:
83 return @"CliqueStatusAbsent";
84 case CliqueStatusNoCloudKitAccount:
85 return @"CliqueStatusNoCloudKitAccount";
86 case CliqueStatusError:
87 return @"CliqueStatusError";
90 CliqueStatus OTCliqueStatusFromString(NSString* str)
92 if([str isEqualToString: @"CliqueStatusIn"]) {
93 return CliqueStatusIn;
94 } else if([str isEqualToString: @"CliqueStatusNotIn"]) {
95 return CliqueStatusNotIn;
96 } else if([str isEqualToString: @"CliqueStatusPending"]) {
97 return CliqueStatusPending;
98 } else if([str isEqualToString: @"CliqueStatusAbsent"]) {
99 return CliqueStatusAbsent;
100 } else if([str isEqualToString: @"CliqueStatusNoCloudKitAccount"]) {
101 return CliqueStatusNoCloudKitAccount;
102 } else if([str isEqualToString: @"CliqueStatusError"]) {
103 return CliqueStatusError;
106 return CliqueStatusError;
110 @implementation OTConfigurationContext
111 - (OTControl* _Nullable)makeOTControl:(NSError**)error
114 if (self.otControl) {
115 return self.otControl;
117 return [OTControl controlObject:true error:error];
124 @implementation OTBottleIDs
127 @implementation OTOperationConfiguration
129 - (instancetype)init {
130 if ((self = [super init]) == nil) {
133 _timeoutWaitForCKAccount = 10 * NSEC_PER_SEC;
134 _qualityOfService = NSQualityOfServiceDefault;
135 _discretionaryNetwork = NO;
136 _useCachedAccountStatus = NO;
140 + (BOOL)supportsSecureCoding {
144 - (void)encodeWithCoder:(nonnull NSCoder *)coder {
145 [coder encodeObject:@(_timeoutWaitForCKAccount) forKey:@"timeoutWaitForCKAccount"];
146 [coder encodeObject:@(_qualityOfService) forKey:@"qualityOfService"];
147 [coder encodeObject:@(_discretionaryNetwork) forKey:@"discretionaryNetwork"];
148 [coder encodeObject:@(_useCachedAccountStatus) forKey:@"useCachedAccountStatus"];
151 - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
152 _timeoutWaitForCKAccount = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"timeoutWaitForCKAccount"] unsignedLongLongValue];
153 _qualityOfService = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"qualityOfService"] integerValue];
154 _discretionaryNetwork = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"discretionaryNetwork"] boolValue];
155 _useCachedAccountStatus = [[coder decodeObjectOfClass:[NSNumber class] forKey:@"useCachedAccountStatus"] boolValue];
162 @interface OTClique ()
163 @property (nonatomic, copy) NSString* cliqueMemberIdentifier;
164 @property (nonatomic, strong) OTConfigurationContext *ctx;
165 @property (nonatomic, strong) NSMutableDictionary *defaults;
168 @implementation OTClique
170 + (BOOL)platformSupportsSOS
172 return (OctagonPlatformSupportsSOS() && OctagonIsSOSFeatureEnabled());
175 // defaults write com.apple.security.octagon enable -bool YES
176 -(BOOL)isOctagonPairingEnabled {
177 BOOL nsDefaults = self.defaults[OTDefaultsOctagonEnable] ? [self.defaults[OTDefaultsOctagonEnable] boolValue] : OctagonIsEnabled();
178 secnotice("octagon", "pairing is %@", nsDefaults ? @"on" : @"off");
182 - (void)setPairingDefault:(BOOL)defaults
184 self.defaults[OTDefaultsOctagonEnable] = @(defaults);
187 - (void)removePairingDefault
189 [self.defaults removeObjectForKey:OTDefaultsOctagonEnable];
192 - (instancetype)initWithContextData:(OTConfigurationContext *)ctx error:(NSError * __autoreleasing *)error
197 _ctx = [[OTConfigurationContext alloc]init];
198 _ctx.context = ctx.context ?: OTDefaultContext;
199 _ctx.dsid = [ctx.dsid copy];
200 _ctx.altDSID = [ctx.altDSID copy];
201 _ctx.analytics = ctx.analytics;
202 _ctx.otControl = ctx.otControl;
204 self.defaults = [NSMutableDictionary dictionary];
208 NSAssert(false, @"OTClique is not implemented on this platform");
213 - (NSString* _Nullable)cliqueMemberIdentifier
216 __block NSString* retPeerID = nil;
217 __block bool subTaskSuccess = false;
219 OctagonSignpost fetchEgoPeerSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchEgoPeer);
220 if(OctagonIsEnabled()) {
221 NSError* localError = nil;
222 OTControl* control = [self makeOTControl:&localError];
224 secerror("octagon: Failed to create OTControl: %@", localError);
225 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
229 [control fetchEgoPeerID:nil
230 context:self.ctx.context
231 reply:^(NSString* peerID, NSError* error) {
233 secerror("octagon: Failed to fetch octagon peer ID: %@", error);
237 secnotice("clique", "cliqueMemberIdentifier(octagon) received %@", retPeerID);
240 if([OTClique platformSupportsSOS]) {
241 CFErrorRef error = NULL;
242 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(&error);
243 retPeerID = (NSString*)CFBridgingRelease(CFRetainSafe(SOSPeerInfoGetPeerID(me)));
247 secnotice("clique", "cliqueMemberIdentifier complete: %@", retPeerID);
248 subTaskSuccess = retPeerID ? true : false;
249 OctagonSignpostEnd(fetchEgoPeerSignPost, OctagonSignpostNameFetchEgoPeer, OctagonSignpostNumber1(OctagonSignpostNameFetchEgoPeer), (int)subTaskSuccess);
257 - (OTControl* _Nullable)makeOTControl:(NSError**)error
259 return [self.ctx makeOTControl:error];
262 - (BOOL)establish:(NSError**)error
264 secnotice("clique-establish", "establish started");
265 OctagonSignpost establishSignPost = OctagonSignpostBegin(OctagonSignpostNameEstablish);
266 bool subTaskSuccess = false;
267 OTControl* control = [self makeOTControl:error];
269 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
273 __block BOOL success = NO;
274 __block NSError* localError = nil;
277 [control establish:nil context:self.ctx.context altDSID:self.ctx.altDSID reply:^(NSError * _Nullable operationError) {
279 secnotice("clique-establish", "establish returned an error: %@", operationError);
281 success = !!operationError;
282 localError = operationError;
285 if(localError && error) {
288 secnotice("clique-establish", "establish complete: %@", success ? @"YES" : @"NO");
289 subTaskSuccess = success ? true : false;
290 OctagonSignpostEnd(establishSignPost, OctagonSignpostNameEstablish, OctagonSignpostNumber1(OctagonSignpostNameEstablish), (int)subTaskSuccess);
295 - (BOOL)resetAndEstablish:(CuttlefishResetReason)resetReason error:(NSError**)error
297 secnotice("clique-resetandestablish", "resetAndEstablish started");
298 bool subTaskSuccess = false;
299 OctagonSignpost resetAndEstablishSignPost = OctagonSignpostBegin(OctagonSignpostNameResetAndEstablish);
301 OTControl* control = [self makeOTControl:error];
304 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
308 __block BOOL success = NO;
309 __block NSError* localError = nil;
310 [control resetAndEstablish:nil context:self.ctx.context altDSID:self.ctx.altDSID resetReason:resetReason reply:^(NSError * _Nullable operationError) {
313 secnotice("clique-resetandestablish", "resetAndEstablish returned an error: %@", operationError);
315 success = !!operationError;
316 localError = operationError;
319 if(localError && error) {
323 secnotice("clique-resetandestablish", "establish complete: %@", success ? @"YES" : @"NO");
324 subTaskSuccess = success ? true : false;
325 OctagonSignpostEnd(resetAndEstablishSignPost, OctagonSignpostNameResetAndEstablish, OctagonSignpostNumber1(OctagonSignpostNameResetAndEstablish), (int)subTaskSuccess);
331 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data error:(NSError * __autoreleasing *)error
333 return [OTClique newFriendsWithContextData:data resetReason:CuttlefishResetReasonUserInitiatedReset error:error];
336 + (OTClique*)newFriendsWithContextData:(OTConfigurationContext*)data resetReason:(CuttlefishResetReason)resetReason error:(NSError * __autoreleasing *)error
339 secnotice("clique-newfriends", "makeNewFriends invoked using context: %@, dsid: %@", data.context, data.dsid);
341 bool subTaskSuccess = false;
342 OctagonSignpost performEscrowRecoverySignpost = OctagonSignpostBegin(OctagonSignpostNameMakeNewFriends);
344 OTClique* clique = [[OTClique alloc] initWithContextData:data error:error];
346 if(OctagonIsEnabled()) {
347 NSError* localError = nil;
348 [clique resetAndEstablish:resetReason error:&localError];
351 secnotice("clique-newfriends", "account reset failed: %@", localError);
355 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
358 secnotice("clique-newfriends", "Octagon account reset succeeded");
362 if([OTClique platformSupportsSOS]) {
363 CFErrorRef resetError = NULL;
364 NSData* analyticsData = nil;
366 NSError* encodingError = nil;
367 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:data.analytics requiringSecureCoding:YES error:&encodingError];
370 secnotice("clique-newfriends", "newFriendsWithContextData: unable to serialize analytics: %@", encodingError);
374 result = SOSCCResetToOffering(&resetError);
376 if(!result || resetError){
377 secnotice("clique-newfriends", "newFriendsWithContextData: resetToOffering failed: %@", resetError);
379 *error = CFBridgingRelease(resetError);
381 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
384 secnotice("clique-newfriends", "newFriendsWithContextData: reset the SOS circle");
386 secnotice("clique-newfriends", "newFriendsWithContextData: SOS disabled on this platform");
388 secnotice("clique-newfriends", "makeNewFriends complete");
390 subTaskSuccess = true;
391 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNameMakeNewFriends, OctagonSignpostNumber1(OctagonSignpostNameMakeNewFriends), (int)subTaskSuccess);
397 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
402 + (OTClique* _Nullable)performEscrowRecoveryWithContextData:(OTConfigurationContext*)data
403 escrowArguments:(NSDictionary*)sbdRecoveryArguments
404 error:(NSError**)error
407 OctagonSignpost performEscrowRecoverySignpost = OctagonSignpostBegin(OctagonSignpostNamePerformEscrowRecovery);
408 bool subTaskSuccess = false;
409 NSError* localError = nil;
410 OTClique* clique = [[OTClique alloc] initWithContextData:data
413 if(!clique || localError) {
414 secnotice("clique-recovery", "unable to create otclique: %@", localError);
418 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
422 // Attempt the recovery from sbd
423 secnotice("clique-recovery", "attempting an escrow recovery for context:%@, altdsid:%@", data.context, data.altDSID);
424 id<OctagonEscrowRecovererPrococol> sb = data.sbd ?: [[getSecureBackupClass() alloc] init];
425 NSDictionary* recoveredInformation = nil;
427 OctagonSignpost recoverFromSBDSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformRecoveryFromSBD);
428 NSError* recoverError = [sb recoverWithInfo:sbdRecoveryArguments results:&recoveredInformation];
429 subTaskSuccess = (recoverError == nil) ? true : false;
430 OctagonSignpostEnd(recoverFromSBDSignPost, OctagonSignpostNamePerformRecoveryFromSBD, OctagonSignpostNumber1(OctagonSignpostNamePerformRecoveryFromSBD), (int)subTaskSuccess);
433 secnotice("clique-recovery", "sbd escrow recovery failed: %@", recoverError);
434 if(recoverError.code == 17 /* kSecureBackupRestoringLegacyBackupKeychainError */ && [recoverError.domain isEqualToString:getkSecureBackupErrorDomain()]) { /* XXX */
435 if([OTClique platformSupportsSOS]) {
436 secnotice("clique-recovery", "Can't restore legacy backup with no keybag. Resetting SOS to offering");
437 CFErrorRef blowItAwayError = NULL;
438 bool successfulReset = SOSCCResetToOffering(&blowItAwayError);
439 if(!successfulReset || blowItAwayError) {
440 secerror("clique-recovery: failed to reset to offering:%@", blowItAwayError);
442 secnotice("clique-recovery", "resetting SOS circle successful");
445 secnotice("clique-recovery", "Legacy restore failed on a non-SOS platform");
449 *error = recoverError;
451 subTaskSuccess = false;
452 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
456 if(OctagonPlatformSupportsSOS()) { // Join if the legacy restore is complete now.
457 secnotice("clique-recovery", "attempting joinAfterRestore");
458 [clique joinAfterRestore:&localError];
459 secnotice("clique-recovery", "joinAfterRestore: %@", localError);
463 // look for OT Bottles
464 OTControl* control = [clique makeOTControl:&localError];
466 secnotice("clique-recovery", "unable to create otcontrol: %@", localError);
470 subTaskSuccess = false;
471 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
475 NSString *bottleID = recoveredInformation[@"bottleID"];
476 NSString *isValid = recoveredInformation[@"bottleValid"];
477 NSData *bottledPeerEntropy = recoveredInformation[@"EscrowServiceEscrowData"][@"BottledPeerEntropy"];
478 bool shouldResetOctagon = false;
480 if(bottledPeerEntropy && bottleID && [isValid isEqualToString:@"valid"]){
481 secnotice("clique-recovery", "recovering from bottle: %@", bottleID);
482 __block NSError* restoreBottleError = nil;
484 OctagonSignpost bottleRecoverySignPost = OctagonSignpostBegin(OctagonSignpostNamePerformBottleRecovery);
486 [control restore:OTCKContainerName
487 contextID:data.context
488 bottleSalt:data.altDSID
489 entropy:bottledPeerEntropy
491 reply:^(NSError * _Nullable restoreError) {
493 secnotice("clique-recovery", "restore bottle errored: %@", restoreError);
495 secnotice("clique-recovery", "restoring bottle succeeded");
497 restoreBottleError = restoreError;
500 subTaskSuccess = (restoreBottleError == nil) ? true : false;
501 OctagonSignpostEnd(bottleRecoverySignPost, OctagonSignpostNamePerformBottleRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformBottleRecovery), (int)subTaskSuccess);
503 if(restoreBottleError) {
505 *error = restoreBottleError;
507 subTaskSuccess = false;
508 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
512 shouldResetOctagon = true;
515 if(shouldResetOctagon) {
516 secnotice("clique-recovery", "bottle %@ is not valid, resetting octagon", bottleID);
517 NSError* resetError = nil;
519 OctagonSignpost resetSignPost = OctagonSignpostBegin(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle);
520 [clique resetAndEstablish:CuttlefishResetReasonNoBottleDuringEscrowRecovery error:&resetError];
521 subTaskSuccess = (resetError == nil) ? true : false;
522 OctagonSignpostEnd(resetSignPost, OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle, OctagonSignpostNumber1(OctagonSignpostNamePerformResetAndEstablishAfterFailedBottle), (int)subTaskSuccess);
525 secnotice("clique-recovery", "failed to reset octagon: %@", resetError);
527 secnotice("clique-recovery", "reset octagon succeeded");
531 secnotice("clique-recovery", "recovery complete: %@", clique);
533 subTaskSuccess = clique ? true : false;
534 OctagonSignpostEnd(performEscrowRecoverySignpost, OctagonSignpostNamePerformEscrowRecovery, OctagonSignpostNumber1(OctagonSignpostNamePerformEscrowRecovery), (int)subTaskSuccess);
539 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
546 - (KCPairingChannel *)setupPairingChannelAsInitiator:(KCPairingChannelContext *)ctx
549 return [getKCPairingChannelClass() pairingChannelInitiator:ctx];
555 - (KCPairingChannel * _Nullable)setupPairingChannelAsInitator:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
560 return [self setupPairingChannelAsInitiator:ctx];
563 - (KCPairingChannel *)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx
566 return [getKCPairingChannelClass() pairingChannelAcceptor:ctx];
572 - (KCPairingChannel * _Nullable)setupPairingChannelAsAcceptor:(KCPairingChannelContext *)ctx error:(NSError * __autoreleasing *)error
578 return [self setupPairingChannelAsAcceptor:ctx];
582 - (CliqueStatus)_fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing *)error
585 __block CliqueStatus sosStatus = CliqueStatusError;
586 __block CliqueStatus octagonStatus = CliqueStatusError;
587 bool subTaskSuccess = false;
589 OctagonSignpost fetchCliqueStatusSignPost = OctagonSignpostBegin(OctagonSignpostNameFetchCliqueStatus);
591 // Octagon is supreme.
593 if(OctagonIsEnabled()) {
594 OTControl* control = [self makeOTControl:error];
596 secnotice("clique-status", "cliqueStatus noOTControl");
597 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
599 return CliqueStatusError;
602 __block NSError* localError = nil;
603 [control fetchCliqueStatus:nil context:self.ctx.context configuration:configuration reply:^(CliqueStatus cliqueStatus, NSError * _Nullable fetchError) {
605 octagonStatus = CliqueStatusError;
606 localError = fetchError;
607 secnotice("clique-status", "octagon clique status errored: %@", fetchError);
609 octagonStatus = cliqueStatus;
613 if(OctagonAuthoritativeTrustIsEnabled() || !OctagonPlatformSupportsSOS()) {
614 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) returning %@ (error: %@)",
615 configuration.useCachedAccountStatus ? "" : "non-",
616 self.ctx.context, self.ctx.altDSID,
617 OTCliqueStatusToString(octagonStatus), localError);
618 if (localError && error) {
620 subTaskSuccess = false;
622 subTaskSuccess = true;
624 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
625 return octagonStatus;
629 if([OTClique platformSupportsSOS]) {
630 CFErrorRef circleStatusError = NULL;
631 sosStatus = kSOSCCError;
632 if(configuration.useCachedAccountStatus){
633 sosStatus = SOSCCThisDeviceIsInCircle(&circleStatusError);
635 sosStatus = SOSCCThisDeviceIsInCircleNonCached(&circleStatusError);
637 secnotice("clique-status", "sos clique status is %d (%@)", (int)sosStatus, circleStatusError);
640 *error = (NSError*)CFBridgingRelease(circleStatusError);
642 CFBridgingRelease(circleStatusError);
645 secnotice("clique-status", "cliqueStatus(%{public}scached)(context:%@, altDSID:%@) complete: %@",
646 configuration.useCachedAccountStatus ? "" : "non-",
647 self.ctx.context, self.ctx.altDSID,
648 OTCliqueStatusToString(octagonStatus));
650 subTaskSuccess = true;
651 OctagonSignpostEnd(fetchCliqueStatusSignPost, OctagonSignpostNameFetchCliqueStatus, OctagonSignpostNumber1(OctagonSignpostNameFetchCliqueStatus), (int)subTaskSuccess);
653 return octagonStatus;
656 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
658 return (CliqueStatus)kSOSCCError;
662 // Don't change rules for CoreCDP, and preserve legacy behavior for now
663 // preserve old behavior until CoreCDP can move to -fetchCliqueStatus:error:
664 #define LEGACY_WAITING_BEHAVIOR (TARGET_OS_OSX || TARGET_OS_IOS)
666 - (CliqueStatus)fetchCliqueStatus:(OTOperationConfiguration *)configuration error:(NSError * __autoreleasing * _Nonnull)error
668 return [self _fetchCliqueStatus:configuration error:error];
671 - (CliqueStatus)fetchCliqueStatus:(NSError * __autoreleasing *)error
673 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
674 #if LEGACY_WAITING_BEHAVIOR
675 configuration.timeoutWaitForCKAccount = 0;
677 return [self _fetchCliqueStatus:configuration error:error];
680 - (CliqueStatus)cachedCliqueStatus:(BOOL)usedCached error:(NSError * __autoreleasing *)error
682 OTOperationConfiguration *configuration = [[OTOperationConfiguration alloc] init];
683 #if LEGACY_WAITING_BEHAVIOR
684 configuration.timeoutWaitForCKAccount = 0;
687 configuration.useCachedAccountStatus = YES;
689 return [self _fetchCliqueStatus:configuration error:error];
693 - (BOOL)removeFriendsInClique:(NSArray<NSString*>*)friendIdentifiers error:(NSError * __autoreleasing *)error
696 secnotice("clique-removefriends", "removeFriendsInClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
697 OctagonSignpost removeFriendsSignPost = OctagonSignpostBegin(OctagonSignpostNameRemoveFriendsInClique);
698 bool subTaskSuccess = false;
700 // Annoying: we must sort friendIdentifiers into octagon/sos lists.
701 NSMutableArray<NSString*>* octagonIdentifiers = [NSMutableArray array];
702 NSMutableArray<NSString*>* sosIdentifiers = [NSMutableArray array];
704 for(NSString* friendIdentifier in friendIdentifiers) {
705 if([friendIdentifier hasPrefix:@"SHA256:"]) {
706 [octagonIdentifiers addObject: friendIdentifier];
708 [sosIdentifiers addObject: friendIdentifier];
712 // Ensure that we don't have any peers on the wrong platform
713 if(!OctagonIsEnabled() && octagonIdentifiers.count > 0) {
714 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
715 code:errSecUnimplemented
716 userInfo:@{NSLocalizedDescriptionKey: @"Octagon is disabled; can't distrust any Octagon peers"}];
717 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
721 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
725 if(!OctagonPlatformSupportsSOS() && sosIdentifiers.count > 0) {
726 NSError *localError = [NSError errorWithDomain:NSOSStatusErrorDomain
727 code:errSecUnimplemented
728 userInfo:@{NSLocalizedDescriptionKey: @"SOS is not available on this platform; can't distrust any SOS peers"}];
729 secnotice("clique-removefriends", "removeFriendsInClique failed:%@", localError);
733 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
738 __block NSError* localError = nil;
741 if(OctagonIsEnabled() && octagonIdentifiers.count > 0) {
742 OTControl* control = [self makeOTControl:error];
744 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
748 secnotice("clique-removefriends", "octagon: removing octagon friends: %@", octagonIdentifiers);
749 [control removeFriendsInClique:nil
750 context:self.ctx.context
751 peerIDs:octagonIdentifiers
752 reply:^(NSError* replyError) {
754 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", replyError);
755 localError = replyError;
757 secnotice("clique-removefriends", "octagon: friends removed: %@", octagonIdentifiers);
762 if([OTClique platformSupportsSOS] && sosIdentifiers.count >0) {
763 CFErrorRef removeFriendError = NULL;
764 NSData* analyticsData = nil;
766 secnotice("clique-removefriends", "removing sos friends: %@", sosIdentifiers);
768 if(self.ctx.analytics){
769 NSError* encodingError = nil;
770 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
774 result = SOSCCRemovePeersFromCircleWithAnalytics((__bridge CFArrayRef)friendIdentifiers, (__bridge CFDataRef)analyticsData, &removeFriendError);
776 result = SOSCCRemovePeersFromCircle((__bridge CFArrayRef)friendIdentifiers, &removeFriendError);
779 if(removeFriendError) {
780 secnotice("clique-removefriends", "removeFriendsInClique failed: unable to remove friends: %@", removeFriendError);
781 localError = CFBridgingRelease(removeFriendError);
785 if(error && localError) {
788 secnotice("clique-removefriends", "removeFriendsInClique complete: %d", result);
790 subTaskSuccess = result;
791 OctagonSignpostEnd(removeFriendsSignPost, OctagonSignpostNameRemoveFriendsInClique, OctagonSignpostNumber1(OctagonSignpostNameRemoveFriendsInClique), (int)subTaskSuccess);
793 return result && localError == nil;
799 - (BOOL)leaveClique:(NSError * __autoreleasing *)error
802 secnotice("clique-leaveClique", "leaveClique invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
803 CFErrorRef removeThisDeviceError = NULL;
805 bool subTaskSuccess = false;
807 OctagonSignpost leaveCliqueSignPost = OctagonSignpostBegin(OctagonSignpostNameLeaveClique);
809 if(OctagonIsEnabled()) {
810 OTControl* control = [self makeOTControl:error];
812 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
816 // We only want to issue a "leave" command if we're actively in a clique
817 __block NSError* localError = nil;
818 CliqueStatus currentStatus = [self fetchCliqueStatus:[[OTOperationConfiguration alloc] init]
822 secnotice("clique-leaveClique", "fetching current status errored: %@", localError);
826 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
830 if(currentStatus == CliqueStatusNotIn) {
831 secnotice("clique-leaveClique", "current status is Not In; no need to leave");
832 subTaskSuccess = true;
833 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
836 [control leaveClique:nil context:self.ctx.context reply:^(NSError * _Nullable leaveError) {
838 secnotice("clique-leaveClique", "leaveClique errored: %@", leaveError);
839 localError = leaveError;
841 secnotice("clique-leaveClique", "leaveClique success.");
848 result = !localError;
851 if([OTClique platformSupportsSOS]) {
852 NSData* analyticsData = nil;
854 if(self.ctx.analytics) {
855 NSError* encodingError = nil;
856 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
858 secnotice("clique-leaveClique", "leaveClique unable to archive analytics object: %@", encodingError);
863 result &= SOSCCRemoveThisDeviceFromCircleWithAnalytics((__bridge CFDataRef)analyticsData, &removeThisDeviceError);
865 result &= SOSCCRemoveThisDeviceFromCircle(&removeThisDeviceError);
868 *error = (NSError*)CFBridgingRelease(removeThisDeviceError);
870 CFBridgingRelease(removeThisDeviceError);
873 secnotice("clique-leaveClique", "leaveClique complete: %d", result);
875 subTaskSuccess = result;
876 OctagonSignpostEnd(leaveCliqueSignPost, OctagonSignpostNameLeaveClique, OctagonSignpostNumber1(OctagonSignpostNameLeaveClique), (int)subTaskSuccess);
878 return result ? YES : NO;
884 - (NSDictionary<NSString*,NSString*>* _Nullable)peerDeviceNamesByPeerID:(NSError * __autoreleasing *)error
887 secnotice("clique", "peerDeviceNamesByPeerID invoked using context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
888 OctagonSignpost peerNamesSignPost = OctagonSignpostBegin(OctagonSignpostNamePeerDeviceNamesByPeerID);
889 __block bool subTaskSuccess = false;
890 NSMutableDictionary<NSString*, NSString*>* retPeers = [NSMutableDictionary dictionary];
892 if(OctagonIsEnabled()) {
893 OTControl* control = [self makeOTControl:error];
895 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
899 __block NSError* localError = nil;
900 __block NSDictionary<NSString*, NSString*>* localPeers = nil;
902 [control peerDeviceNamesByPeerID:nil context:OTDefaultContext reply:^(NSDictionary<NSString*,NSString*>* peers, NSError* controlError) {
904 secnotice("clique", "peerDeviceNamesByPeerID errored: %@", controlError);
906 secnotice("clique", "peerDeviceNamesByPeerID succeeded: %@", peers);
908 localError = controlError;
912 if(error && localError) {
916 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
919 [retPeers addEntriesFromDictionary:localPeers];
920 secnotice("clique", "Received %lu Octagon peers", (unsigned long)localPeers.count);
923 if([OTClique platformSupportsSOS]) {
924 CFErrorRef peerErrorRef = NULL;
925 NSMutableDictionary<NSString*,NSString*>* peerMapping = [NSMutableDictionary dictionary];
926 NSArray* arrayOfPeerRefs = CFBridgingRelease(SOSCCCopyPeerPeerInfo(&peerErrorRef));
928 [arrayOfPeerRefs enumerateObjectsUsingBlock:^(id peerRef, NSUInteger idx, BOOL * stop) {
929 SOSPeerInfoRef peer = (__bridge SOSPeerInfoRef)peerRef;
931 [peerMapping setObject:(__bridge NSString*)SOSPeerInfoGetPeerName(peer) forKey:(__bridge NSString*)SOSPeerInfoGetPeerID(peer)];
935 subTaskSuccess = (peerErrorRef == NULL || [retPeers count] == 0) ? true : false;
938 *error = (NSError*)CFBridgingRelease(peerErrorRef);
940 CFBridgingRelease(peerErrorRef);
942 [retPeers addEntriesFromDictionary:peerMapping];
943 secnotice("clique", "Received %lu SOS peers", (unsigned long)peerMapping.count);
946 OctagonSignpostEnd(peerNamesSignPost, OctagonSignpostNamePeerDeviceNamesByPeerID, OctagonSignpostNumber1(OctagonSignpostNamePeerDeviceNamesByPeerID), (int)subTaskSuccess);
953 - (BOOL)joinAfterRestore:(NSError * __autoreleasing *)error
955 secnotice("clique-recovery", "joinAfterRestore for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
956 OctagonSignpost joinAfterRestoreSignPost = OctagonSignpostBegin(OctagonSignpostNameJoinAfterRestore);
957 bool subTaskSuccess = false;
959 if([OTClique platformSupportsSOS]) {
960 CFErrorRef restoreError = NULL;
961 bool res = SOSCCRequestToJoinCircleAfterRestore(&restoreError);
963 *error = (NSError*)CFBridgingRelease(restoreError);
965 CFBridgingRelease(restoreError);
967 secnotice("clique-recovery", "joinAfterRestore complete: %d %@", res, error ? *error : @"no error pointer provided");
969 subTaskSuccess = res;
970 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
972 return res ? YES : NO;
974 secnotice("clique-recovery", "SOS disabled for this platform, returning NO");
976 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
977 code:errSecUnimplemented
978 userInfo:@{NSLocalizedDescriptionKey: @"join after restore unimplemented"}];
980 OctagonSignpostEnd(joinAfterRestoreSignPost, OctagonSignpostNameJoinAfterRestore, OctagonSignpostNumber1(OctagonSignpostNameJoinAfterRestore), (int)subTaskSuccess);
985 - (BOOL)safariPasswordSyncingEnabled:(NSError **)error
987 secnotice("clique-safari", "safariPasswordSyncingEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
988 OctagonSignpost safariSyncingEnabledSignPost = OctagonSignpostBegin(OctagonSignpostNameSafariPasswordSyncingEnabled);
989 bool subTaskSuccess = false;
991 if([OTClique platformSupportsSOS]) {
992 CFErrorRef viewErrorRef = NULL;
994 SOSViewResultCode result = SOSCCView(kSOSViewAutofillPasswords, kSOSCCViewQuery, &viewErrorRef);
995 subTaskSuccess = (viewErrorRef == NULL) ? true : false;
997 BOOL viewMember = result == kSOSCCViewMember;
999 *error = (NSError*)CFBridgingRelease(viewErrorRef);
1001 CFBridgingRelease(viewErrorRef);
1003 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1005 secnotice("clique-safari", "safariPasswordSyncingEnabled complete: %@", viewMember ? @"YES" : @"NO");
1008 secnotice("clique-safari", "SOS disabled for this platform, returning NO");
1010 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1011 code:errSecUnimplemented
1012 userInfo:@{NSLocalizedDescriptionKey: @"safari password syncing enabled unimplemented"}];
1014 OctagonSignpostEnd(safariSyncingEnabledSignPost, OctagonSignpostNameSafariPasswordSyncingEnabled, OctagonSignpostNumber1(OctagonSignpostNameSafariPasswordSyncingEnabled), (int)subTaskSuccess);
1019 - (BOOL)isLastFriend:(NSError **)error
1021 secnotice("clique-isLastFriend", "is last friend");
1025 - (BOOL)waitForInitialSync:(NSError *__autoreleasing*)error
1027 secnotice("clique-legacy", "waitForInitialSync for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1029 OctagonSignpost waitForInitialSyncSignPost = OctagonSignpostBegin(OctagonSignpostNameWaitForInitialSync);
1030 bool subTaskSuccess = false;
1032 if([OTClique platformSupportsSOS]) {
1033 CFErrorRef initialSyncErrorRef = NULL;
1034 bool result = false;
1035 if(self.ctx.analytics){
1036 NSError* encodingError = nil;
1037 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1038 if(!encodingError && analyticsData){
1039 result = SOSCCWaitForInitialSyncWithAnalytics((__bridge CFDataRef)analyticsData, &initialSyncErrorRef);
1041 result = SOSCCWaitForInitialSync(&initialSyncErrorRef);
1044 result = SOSCCWaitForInitialSync(&initialSyncErrorRef);
1047 BOOL initialSyncResult = result ? YES : NO;
1049 *error = (NSError*)CFBridgingRelease(initialSyncErrorRef);
1051 CFBridgingRelease(initialSyncErrorRef);
1053 secnotice("clique-legacy", "waitForInitialSync waited: %d %@", initialSyncResult, error ? *error : @"no error pointer provided");
1055 subTaskSuccess = initialSyncResult ? true : false;
1056 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1058 return initialSyncResult;
1060 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1062 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1063 code:errSecUnimplemented
1064 userInfo:@{NSLocalizedDescriptionKey: @"wait for initial sync unimplemented"}];
1066 OctagonSignpostEnd(waitForInitialSyncSignPost, OctagonSignpostNameWaitForInitialSync, OctagonSignpostNumber1(OctagonSignpostNameWaitForInitialSync), (int)subTaskSuccess);
1071 - (NSArray* _Nullable)copyViewUnawarePeerInfo:(NSError *__autoreleasing*)error
1073 secnotice("clique-legacy", "copyViewUnawarePeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1075 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyViewUnawarePeerInfo);
1076 bool subTaskSuccess = false;
1078 if([OTClique platformSupportsSOS]) {
1079 CFErrorRef copyViewUnawarePeerInfoErrorRef = NULL;
1080 CFArrayRef peerListRef = SOSCCCopyViewUnawarePeerInfo(©ViewUnawarePeerInfoErrorRef);
1082 NSArray* peerList = (peerListRef ? (NSArray*)(CFBridgingRelease(peerListRef)) : nil);
1084 *error = (NSError*)CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1086 CFBridgingRelease(copyViewUnawarePeerInfoErrorRef);
1088 subTaskSuccess = (peerList != nil) ? true : false;
1089 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1093 secnotice("clique-legacy", "SOS disabled for this platform, returning NULL");
1095 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1096 code:errSecUnimplemented
1097 userInfo:@{NSLocalizedDescriptionKey: @"copy view unaware peer info unimplemented"}];
1099 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyViewUnawarePeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyViewUnawarePeerInfo), (int)subTaskSuccess);
1104 - (BOOL)viewSet:(NSSet*)enabledViews disabledViews:(NSSet*)disabledViews
1106 secnotice("clique-legacy", "viewSet for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1107 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameViewSet);
1108 bool subTaskSuccess = false;
1110 if([OTClique platformSupportsSOS]) {
1111 bool result = false;
1112 if(self.ctx.analytics){
1113 NSError* encodingError = nil;
1114 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1115 if(!encodingError && analyticsData){
1116 result = SOSCCViewSetWithAnalytics((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews, (__bridge CFDataRef)analyticsData);
1118 result = SOSCCViewSet((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews);
1121 result = SOSCCViewSet((__bridge CFSetRef)enabledViews, (__bridge CFSetRef)disabledViews);
1124 BOOL viewSetResult = result ? YES : NO;
1125 subTaskSuccess = result;
1126 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1127 return viewSetResult;
1129 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1130 OctagonSignpostEnd(signPost, OctagonSignpostNameViewSet, OctagonSignpostNumber1(OctagonSignpostNameViewSet), (int)subTaskSuccess);
1135 - (BOOL)setUserCredentialsAndDSID:(NSString*)userLabel
1136 password:(NSData*)userPassword
1137 error:(NSError *__autoreleasing*)error
1139 secnotice("clique-legacy", "setUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1140 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetUserCredentialsAndDSID);
1141 bool subTaskSuccess = false;
1143 if([OTClique platformSupportsSOS]) {
1144 CFErrorRef setCredentialsErrorRef = NULL;
1145 bool result = false;
1146 if(self.ctx.analytics){
1147 NSError* encodingError = nil;
1148 NSData* analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1149 if(!encodingError && analyticsData){
1150 result = SOSCCSetUserCredentialsAndDSIDWithAnalytics((__bridge CFStringRef)userLabel,
1151 (__bridge CFDataRef)userPassword,
1152 (__bridge CFStringRef)self.ctx.dsid,
1153 (__bridge CFDataRef)analyticsData,
1154 &setCredentialsErrorRef);
1156 result = SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1157 (__bridge CFDataRef)userPassword,
1158 (__bridge CFStringRef)self.ctx.dsid,
1159 &setCredentialsErrorRef);
1162 result = SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1163 (__bridge CFDataRef)userPassword,
1164 (__bridge CFStringRef)self.ctx.dsid,
1165 &setCredentialsErrorRef);
1168 BOOL setCredentialsResult = result ? YES : NO;
1170 *error = (NSError*)CFBridgingRelease(setCredentialsErrorRef);
1172 CFBridgingRelease(setCredentialsErrorRef);
1174 secnotice("clique-legacy", "setUserCredentialsAndDSID results: %d %@", setCredentialsResult, setCredentialsErrorRef);
1175 subTaskSuccess = result;
1176 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1178 return setCredentialsResult;
1180 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1182 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1183 code:errSecUnimplemented
1184 userInfo:@{NSLocalizedDescriptionKey: @"set user credentials unimplemented"}];
1186 OctagonSignpostEnd(signPost, OctagonSignpostNameSetUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameSetUserCredentialsAndDSID), (int)subTaskSuccess);
1191 - (BOOL)tryUserCredentialsAndDSID:(NSString*)userLabel
1192 password:(NSData*)userPassword
1193 error:(NSError *__autoreleasing*)error
1195 secnotice("clique-legacy", "tryUserCredentialsAndDSID for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1196 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameTryUserCredentialsAndDSID);
1197 bool subTaskSuccess = false;
1199 if([OTClique platformSupportsSOS]) {
1200 CFErrorRef tryCredentialsErrorRef = NULL;
1201 bool result = SOSCCTryUserCredentialsAndDSID((__bridge CFStringRef)userLabel,
1202 (__bridge CFDataRef)userPassword,
1203 (__bridge CFStringRef)self.ctx.dsid,
1204 &tryCredentialsErrorRef);
1206 BOOL tryCredentialsResult = result ? YES : NO;
1208 *error = (NSError*)CFBridgingRelease(tryCredentialsErrorRef);
1210 CFBridgingRelease(tryCredentialsErrorRef);
1212 secnotice("clique-legacy", "tryUserCredentialsAndDSID results: %d %@", tryCredentialsResult, tryCredentialsErrorRef);
1213 subTaskSuccess = result;
1214 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1215 return tryCredentialsResult;
1218 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1220 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1221 code:errSecUnimplemented
1222 userInfo:@{NSLocalizedDescriptionKey: @"try user credentials unimplemented"}];
1224 OctagonSignpostEnd(signPost, OctagonSignpostNameTryUserCredentialsAndDSID, OctagonSignpostNumber1(OctagonSignpostNameTryUserCredentialsAndDSID), (int)subTaskSuccess);
1229 - (NSArray* _Nullable)copyPeerPeerInfo:(NSError *__autoreleasing*)error
1231 secnotice("clique-legacy", "copyPeerPeerInfo for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1232 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameCopyPeerPeerInfo);
1233 bool subTaskSuccess = false;
1235 if([OTClique platformSupportsSOS]) {
1236 CFErrorRef copyPeerErrorRef = NULL;
1237 CFArrayRef result = SOSCCCopyPeerPeerInfo(©PeerErrorRef);
1239 NSArray* peerList = (result ? (NSArray*)(CFBridgingRelease(result)) : nil);
1242 *error = (NSError*)CFBridgingRelease(copyPeerErrorRef);
1244 CFBridgingRelease(copyPeerErrorRef);
1246 secnotice("clique-legacy", "copyPeerPeerInfo results: %@", peerList);
1247 subTaskSuccess = (peerList != nil) ? true : false;
1248 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1251 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1253 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1254 code:errSecUnimplemented
1255 userInfo:@{NSLocalizedDescriptionKey: @"copy peer peer info unimplemented"}];
1257 OctagonSignpostEnd(signPost, OctagonSignpostNameCopyPeerPeerInfo, OctagonSignpostNumber1(OctagonSignpostNameCopyPeerPeerInfo), (int)subTaskSuccess);
1262 - (BOOL)peersHaveViewsEnabled:(NSArray<NSString*>*)viewNames error:(NSError *__autoreleasing*)error
1264 secnotice("clique-legacy", "peersHaveViewsEnabled for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1265 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePeersHaveViewsEnabled);
1266 bool subTaskSuccess = false;
1268 if([OTClique platformSupportsSOS]) {
1269 CFErrorRef viewsEnabledErrorRef = NULL;
1270 BOOL viewsEnabledResult = NO;
1272 CFBooleanRef result = SOSCCPeersHaveViewsEnabled((__bridge CFArrayRef)viewNames, &viewsEnabledErrorRef);
1274 viewsEnabledResult = CFBooleanGetValue(result) ? YES : NO;
1277 *error = (NSError*)CFBridgingRelease(viewsEnabledErrorRef);
1279 CFBridgingRelease(viewsEnabledErrorRef);
1281 secnotice("clique-legacy", "peersHaveViewsEnabled results: %@", viewsEnabledResult ? @"YES" : @"NO");
1282 subTaskSuccess = viewsEnabledResult ? true : false;
1283 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1284 return viewsEnabledResult;
1286 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1288 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1289 code:errSecUnimplemented
1290 userInfo:@{NSLocalizedDescriptionKey: @"peers have views enabled unimplemented"}];
1292 OctagonSignpostEnd(signPost, OctagonSignpostNamePeersHaveViewsEnabled, OctagonSignpostNumber1(OctagonSignpostNamePeersHaveViewsEnabled), (int)subTaskSuccess);
1297 - (BOOL)requestToJoinCircle:(NSError *__autoreleasing*)error
1299 bool result = false;
1300 CFErrorRef joinErrorRef = NULL;
1301 bool subTaskSuccess = false;
1302 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameRequestToJoinCircle);
1305 secnotice("clique-legacy", "requestToJoinCircle for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1307 if(OctagonIsEnabled()) {
1308 // Sometimes, CoreCDP calls this to cause a circle creation to occur.
1309 // So, for Octagon, we might want to request a establish, but not a reset.
1311 // Fetch the current trust status, so we know if we should fire off the establish.
1312 NSError* localError = nil;
1313 CliqueStatus status = [self fetchCliqueStatus: &localError];
1316 secnotice("clique-legacy", "fetching clique status failed: %@", localError);
1318 *error = localError;
1320 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1324 if(status == CliqueStatusAbsent) {
1325 secnotice("clique-legacy", "clique status is %@; beginning an establish", OTCliqueStatusToString(status));
1326 [self establish:&localError];
1330 *error = localError;
1332 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1335 secnotice("clique-legacy", "establish succeeded");
1338 secnotice("clique-legacy", "clique status is %@; performing no Octagon actions", OTCliqueStatusToString(status));
1341 // If we didn't early-exit, and we aren't going to invoke SOS below, we succeeded.
1342 if(!OctagonPlatformSupportsSOS()) {
1343 secnotice("clique-legacy", "requestToJoinCircle results: %d %@", result, joinErrorRef);
1344 subTaskSuccess = true;
1345 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1351 if([OTClique platformSupportsSOS]) {
1352 NSData* analyticsData = nil;
1353 if(self.ctx.analytics){
1354 NSError* encodingError = nil;
1355 analyticsData = [NSKeyedArchiver archivedDataWithRootObject:self.ctx.analytics requiringSecureCoding:YES error:&encodingError];
1359 result = SOSCCRequestToJoinCircleWithAnalytics((__bridge CFDataRef)analyticsData, &joinErrorRef);
1361 result = SOSCCRequestToJoinCircle(&joinErrorRef);
1364 secnotice("clique-legacy", "sos requestToJoinCircle complete: %d %@", result, joinErrorRef);
1368 *error = (NSError*)CFBridgingRelease(joinErrorRef);
1370 CFBridgingRelease(joinErrorRef);
1372 subTaskSuccess = result;
1373 OctagonSignpostEnd(signPost, OctagonSignpostNameRequestToJoinCircle, OctagonSignpostNumber1(OctagonSignpostNameRequestToJoinCircle), (int)subTaskSuccess);
1375 return result ? YES : NO;
1378 - (BOOL)accountUserKeyAvailable
1380 secnotice("clique-legacy", "accountUserKeyAvailable for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1381 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameAccountUserKeyAvailable);
1382 bool subTaskSuccess = false;
1384 if([OTClique platformSupportsSOS]) {
1385 BOOL canAuthenticate = SOSCCCanAuthenticate(NULL) ? YES : NO;
1386 if (canAuthenticate == NO) {
1387 secnotice("clique-legacy", "Security requires credentials...");
1389 subTaskSuccess = canAuthenticate ? true : false;
1390 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1391 return canAuthenticate;
1393 secnotice("clique-legacy", "SOS disabled for this platform, returning NO");
1394 OctagonSignpostEnd(signPost, OctagonSignpostNameAccountUserKeyAvailable, OctagonSignpostNumber1(OctagonSignpostNameAccountUserKeyAvailable), (int)subTaskSuccess);
1399 // MARK: SBD interfaces
1400 + (OTBottleIDs* _Nullable)findOptimalBottleIDsWithContextData:(OTConfigurationContext*)data
1401 error:(NSError**)error
1404 secnotice("clique-findbottle", "finding optimal bottles for context:%@, altdsid:%@", data.context, data.altDSID);
1405 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFindOptimalBottleIDsWithContextData);
1406 bool subTaskSuccess = false;
1408 if(OctagonIsEnabled()) {
1409 __block NSError* localError = nil;
1410 __block NSArray<NSString*>* localViableBottleIDs = nil;
1411 __block NSArray<NSString*>* localPartiallyViableBottleIDs = nil;
1413 OTControl *control = [data makeOTControl:&localError];
1415 secnotice("clique-findbottle", "unable to create otcontrol: %@", localError);
1417 *error = localError;
1419 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1422 [control fetchAllViableBottles:OTCKContainerName
1423 context:data.context
1424 reply:^(NSArray<NSString *> * _Nullable sortedBottleIDs,
1425 NSArray<NSString*> * _Nullable sortedPartialBottleIDs,
1426 NSError * _Nullable fetchError) {
1428 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData errored: %@", fetchError);
1430 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData succeeded: %@, %@", sortedBottleIDs, sortedPartialBottleIDs);
1432 localError = fetchError;
1433 localViableBottleIDs = sortedBottleIDs;
1434 localPartiallyViableBottleIDs = sortedPartialBottleIDs;
1437 if(error && localError) {
1438 *error = localError;
1440 OTBottleIDs* bottleIDs = [[OTBottleIDs alloc] init];
1441 bottleIDs.preferredBottleIDs = localViableBottleIDs;
1442 bottleIDs.partialRecoveryBottleIDs = localPartiallyViableBottleIDs;
1444 secnotice("clique-findbottle", "findOptimalBottleIDsWithContextData complete");
1446 subTaskSuccess = (localError == nil) ? true : false;
1447 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1450 // With octagon off, fail with 'unimplemented'
1452 *error = [NSError errorWithDomain:NSOSStatusErrorDomain
1453 code:errSecUnimplemented
1454 userInfo:@{NSLocalizedDescriptionKey: @"optimal bottle IDs unimplemented"}];
1456 OctagonSignpostEnd(signPost, OctagonSignpostNameFindOptimalBottleIDsWithContextData, OctagonSignpostNumber1(OctagonSignpostNameFindOptimalBottleIDsWithContextData), (int)subTaskSuccess);
1461 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1467 + (OTClique* _Nullable)recoverWithContextData:(OTConfigurationContext*)data
1468 bottleID:(NSString*)bottleID
1469 escrowedEntropy:(NSData*)entropy
1470 error:(NSError**)error
1473 secnotice("octagon", "replaced by performEscrowRecoveryWithContextData:escrowArguments:error: remove call");
1477 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1483 // used by sbd to fill in the escrow record
1484 // TODO: what extra entitlement do you need to call this?
1485 - (void)fetchEscrowContents:(void (^)(NSData* _Nullable entropy,
1486 NSString* _Nullable bottleID,
1487 NSData* _Nullable signingPublicKey,
1488 NSError* _Nullable error))reply
1491 secnotice("clique-fetchescrow", "fetching entropy for bottling for context:%@, altdsid:%@", self.ctx.context, self.ctx.altDSID);
1492 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameFetchEscrowContents);
1493 __block bool subTaskSuccess = false;
1494 if(OctagonIsEnabled()) {
1495 NSError* controlError = nil;
1496 OTControl* control = [self makeOTControl:&controlError];
1498 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1499 reply(nil, nil, nil, controlError);
1502 [control fetchEscrowContents:OTCKContainerName
1503 contextID:self.ctx.context
1504 reply:^(NSData * _Nullable entropy,
1505 NSString * _Nullable bottleID,
1506 NSData * _Nullable signingPublicKey,
1507 NSError * _Nullable error) {
1509 secnotice("clique-fetchescrow", "fetchEscrowContents errored: %@", error);
1511 secnotice("clique-fetchescrow","fetchEscrowContents succeeded");
1513 subTaskSuccess = (error == nil) ? true : false;
1514 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1515 reply (entropy, bottleID, signingPublicKey, error);
1518 // With octagon off, fail with 'unimplemented'
1519 OctagonSignpostEnd(signPost, OctagonSignpostNameFetchEscrowContents, OctagonSignpostNumber1(OctagonSignpostNameFetchEscrowContents), (int)subTaskSuccess);
1520 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain
1521 code:errSecUnimplemented
1522 userInfo:@{NSLocalizedDescriptionKey: @"fetchEscrowRecordContents unimplemented"}]);
1525 reply(nil, nil, nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1529 + (void)setNewRecoveryKeyWithData:(OTConfigurationContext *)ctx
1530 recoveryKey:(NSString*)recoveryKey reply:(nonnull void (^)(SecRecoveryKey *rk, NSError *error))reply
1533 secnotice("octagon-setrecoverykey", "setNewRecoveryKeyWithData invoked for context: %@", ctx.context);
1534 //set the recovery key for SOS
1535 NSError* createRecoveryKeyError = nil;
1536 NSMutableDictionary *userInfo = [NSMutableDictionary new];
1537 NSError* retError = nil;
1538 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameSetNewRecoveryKeyWithData);
1539 __block bool subTaskSuccess = false;
1541 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(recoveryKey, &createRecoveryKeyError);
1543 secerror("octagon-setrecoverykey, SecRKCreateRecoveryKeyWithError() failed: %@", createRecoveryKeyError);
1544 userInfo[NSLocalizedDescriptionKey] = @"SecRKCreateRecoveryKeyWithError() failed";
1545 userInfo[NSUnderlyingErrorKey] = createRecoveryKeyError;
1546 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1547 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1548 reply(nil, retError);
1551 if([OTClique platformSupportsSOS]) {
1552 CFErrorRef registerError = nil;
1553 if (!SecRKRegisterBackupPublicKey(rk, ®isterError)) {
1554 secerror("octagon-setrecoverykey, SecRKRegisterBackupPublicKey() failed: %@", registerError);
1555 NSError *underlyingError = CFBridgingRelease(registerError);
1556 userInfo[NSLocalizedDescriptionKey] = @"SecRKRegisterBackupPublicKey() failed";
1557 userInfo[NSUnderlyingErrorKey] = underlyingError;
1558 retError = [NSError errorWithDomain:getkSecureBackupErrorDomain() code:kSecureBackupInternalError userInfo:userInfo];
1559 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1560 reply(nil,retError);
1563 secnotice("octagon-setrecoverykey", "successfully registered recovery key for SOS");
1567 //set the recovery key for Octagon
1568 if(OctagonRecoveryKeyIsEnabled()) {
1569 NSError* controlError = nil;
1570 OTControl* control = [ctx makeOTControl:&controlError];
1572 secnotice("octagon-setrecoverykey", "failed to fetch OTControl object: %@", controlError);
1573 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1574 reply(nil, controlError);
1577 [control createRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError * createError) {
1579 secerror("octagon-setrecoverykey, failed to create octagon recovery key");
1580 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1581 reply(nil, createError);
1584 secnotice("octagon-setrecoverykey", "successfully set octagon recovery key");
1585 subTaskSuccess = true;
1586 OctagonSignpostEnd(signPost, OctagonSignpostNameSetNewRecoveryKeyWithData, OctagonSignpostNumber1(OctagonSignpostNameSetNewRecoveryKeyWithData), (int)subTaskSuccess);
1593 reply(nil, [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1597 + (void)recoverOctagonUsingData:(OTConfigurationContext *)ctx
1598 recoveryKey:(NSString*)recoveryKey
1599 reply:(void(^)(NSError* _Nullable error))reply
1602 OctagonSignpost signpost = OctagonSignpostBegin(OctagonSignpostNameRecoverOctagonUsingData);
1603 __block bool subTaskSuccess = false;
1605 if(OctagonRecoveryKeyIsEnabled()) {
1606 NSError* controlError = nil;
1607 OTControl* control = [ctx makeOTControl:&controlError];
1609 secnotice("clique-recoverykey", "join using recovery key");
1612 secnotice("clique-recoverykey", "failed to fetch OTControl object: %@", controlError);
1613 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1614 reply(controlError);
1617 [control joinWithRecoveryKey:OTCKContainerName contextID:ctx.context recoveryKey:recoveryKey reply:^(NSError *joinError) {
1619 secnotice("clique-recoverykey", "failed to join using recovery key: %@", joinError);
1620 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1624 secnotice("clique-recoverykey", "successfully joined using recovery key");
1625 subTaskSuccess = true;
1626 OctagonSignpostEnd(signpost, OctagonSignpostNameRecoverOctagonUsingData, OctagonSignpostNumber1(OctagonSignpostNameRecoverOctagonUsingData), (int)subTaskSuccess);
1632 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1636 - (void)performedCDPStateMachineRun:(OTCliqueCDPContextType)type
1637 success:(BOOL)success
1638 error:(NSError * _Nullable)error
1639 reply:(void(^)(NSError* _Nullable error))reply
1642 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNamePerformedCDPStateMachineRun);
1643 NSError* controlError = nil;
1644 __block bool subTaskSuccess = false;
1646 OTControl* control = [self makeOTControl:&controlError];
1648 secnotice("clique-cdp-sm", "octagon, failed to fetch OTControl object: %@", controlError);
1649 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1650 reply(controlError);
1654 [control postCDPFollowupResult:success type:type error:error containerName:OTCKContainerName contextName:OTDefaultContext reply:^(NSError *postError) {
1656 secnotice("clique-cdp-sm", "failed to post %@ result: %@ ", type, postError);
1657 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1662 secnotice("clique-cdp-sm", "posted success: %@", type);
1664 secnotice("clique-cdp-sm", "posted error: %@: %@", type, error);
1666 subTaskSuccess = success ? true : false;
1667 OctagonSignpostEnd(signPost, OctagonSignpostNamePerformedCDPStateMachineRun, OctagonSignpostNumber1(OctagonSignpostNamePerformedCDPStateMachineRun), (int)subTaskSuccess);
1671 reply([NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil]);
1675 - (BOOL)waitForOctagonUpgrade:(NSError** _Nullable)error
1678 OTControl* control = nil;
1679 OctagonSignpost signPost = OctagonSignpostBegin(OctagonSignpostNameWaitForOctagonUpgrade);
1680 __block bool subTaskSuccess = false;
1682 if (!OctagonIsEnabled()) {
1683 secnotice("clique-waitforoctagonupgrade", "cannot upgrade, octagon is not enabled");
1685 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:@{NSLocalizedDescriptionKey: @"Octagon is not enabled"}];
1687 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1692 NSError *controlError = nil;
1693 control = [self makeOTControl:&controlError];
1695 secnotice("clique-waitforoctagonupgrade", "octagon, failed to fetch OTControl object: %@", controlError);
1697 *error = controlError;
1699 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1703 __block BOOL ret = NO;
1704 __block NSError* blockError = nil;
1706 [control waitForOctagonUpgrade:OTCKContainerName context:OTDefaultContext reply:^(NSError *postError) {
1708 secnotice("clique-waitforoctagonupgrade", "error from control: %@", postError);
1709 blockError = postError;
1712 secnotice("clique-waitforoctagonupgrade", "successfully upgraded to octagon");
1717 if (blockError && error) {
1718 *error = blockError;
1720 subTaskSuccess = ret ? true : false;
1721 OctagonSignpostEnd(signPost, OctagonSignpostNameWaitForOctagonUpgrade, OctagonSignpostNumber1(OctagonSignpostNameWaitForOctagonUpgrade), (int)subTaskSuccess);
1725 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecUnimplemented userInfo:nil];
1731 - (void)performedFailureCDPStateMachineRun:(OTCliqueCDPContextType)type
1732 error:(NSError * _Nullable)error
1733 reply:(void(^)(NSError* _Nullable error))reply
1735 [self performedCDPStateMachineRun:type success:NO error:error reply:reply];
1738 - (void)performedSuccessfulCDPStateMachineRun:(OTCliqueCDPContextType)type
1739 reply:(void(^)(NSError* _Nullable error))reply
1741 [self performedCDPStateMachineRun:type success:YES error:nil reply:reply];