4 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
5 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
7 #import <CloudKit/CloudKit_Private.h>
9 #import "keychain/ckks/CloudKitCategories.h"
11 #import "keychain/ot/ObjCImprovements.h"
13 #import "keychain/ot/OTCuttlefishAccountStateHolder.h"
14 #import "keychain/ot/OTOperationDependencies.h"
15 #import "keychain/ot/OTStates.h"
16 #import "keychain/ot/OTUpdateTPHOperation.h"
18 @interface OTUpdateTPHOperation ()
19 @property OTOperationDependencies* deps;
21 @property NSOperation* finishedOp;
23 @property (nullable) OctagonFlag* retryFlag;
26 @implementation OTUpdateTPHOperation
27 @synthesize nextState = _nextState;
28 @synthesize intendedState = _intendedState;
30 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
31 intendedState:(OctagonState*)intendedState
32 errorState:(OctagonState*)errorState
33 retryFlag:(OctagonFlag* _Nullable)retryFlag
35 if((self = [super init])) {
38 _intendedState = intendedState;
39 _nextState = errorState;
41 _retryFlag = retryFlag;
49 self.finishedOp = [NSBlockOperation blockOperationWithBlock:^{
50 // If we errored in some unknown way, ask to try again!
54 if(self.retryFlag == nil) {
55 secerror("octagon: Received an error updating TPH, but no retry flag present.");
59 // Is this a very scary error?
62 OctagonPendingFlag* pendingFlag = nil;
64 if([self.deps.lockStateTracker isLockedError:self.error]) {
65 secnotice("octagon", "Updating trust state failed because locked, retry once unlocked: %@", self.error);
66 self.nextState = OctagonStateWaitForUnlock;
67 pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:self.retryFlag
68 conditions:OctagonPendingConditionsDeviceUnlocked];
70 } else if ([self.error isCuttlefishError:CuttlefishErrorTransactionalFailure]) {
71 secnotice("octagon", "Transaction failure in cuttlefishm, retrying: %@", self.error);
74 // more CloudKit errors should trigger a retry here
75 secnotice("octagon", "Error is currently unknown, aborting: %@", self.error);
80 NSTimeInterval baseDelay = SecCKKSTestsEnabled() ? 2 : 30;
81 NSTimeInterval delay = CKRetryAfterSecondsForError(self.error) ?: baseDelay;
82 pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:self.retryFlag
83 delayInSeconds:delay];
85 secnotice("octagon", "Updating trust state no fatal: requesting retry: %@",
87 [self.deps.flagHandler handlePendingFlag:pendingFlag];
91 [self dependOnBeforeGroupFinished:self.finishedOp];
94 [[self.deps.cuttlefishXPC remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
96 secerror("octagon: Can't talk with TrustedPeersHelper, update is lost: %@", error);
98 [self runBeforeGroupFinished:self.finishedOp];
100 }] updateWithContainer:self.deps.containerName
101 context:self.deps.contextID
107 reply:^(TrustedPeersHelperPeerState* peerState, NSError* error) {
109 if(error || !peerState) {
110 secerror("octagon: update errored: %@", error);
113 // On an error, for now, go back to the intended state
114 // <rdar://problem/50190005> Octagon: handle lock state errors in update()
115 self.nextState = self.intendedState;
116 [self runBeforeGroupFinished:self.finishedOp];
120 secnotice("octagon", "update complete: %@", peerState);
122 if(peerState.identityIsPreapproved) {
123 secnotice("octagon-sos", "Self peer is now preapproved!");
124 [self.deps.flagHandler handleFlag:OctagonFlagEgoPeerPreapproved];
126 if (peerState.memberChanges) {
127 secnotice("octagon", "Member list changed");
128 [self.deps.octagonAdapter sendTrustedPeerSetChangedUpdate];
131 if (peerState.unknownMachineIDsPresent) {
132 secnotice("octagon-authkit", "Unknown machine IDs are present; requesting fetch");
133 [self.deps.flagHandler handleFlag:OctagonFlagFetchAuthKitMachineIDList];
136 if(peerState.peerStatus & TPPeerStatusExcluded) {
137 secnotice("octagon", "Self peer (%@) is excluded; moving to untrusted", peerState.peerID);
138 self.nextState = OctagonStateBecomeUntrusted;
140 } else if(peerState.peerStatus & TPPeerStatusUnknown) {
141 secnotice("octagon", "Self peer (%@) is unknown; moving to untrusted", peerState.peerID);
142 self.nextState = OctagonStateBecomeUntrusted;
145 self.nextState = self.intendedState;
148 [self runBeforeGroupFinished:self.finishedOp];