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 OctagonState* peerUnknownState;
23 @property NSOperation* finishedOp;
25 @property (nullable) OctagonFlag* retryFlag;
28 @implementation OTUpdateTPHOperation
29 @synthesize nextState = _nextState;
30 @synthesize intendedState = _intendedState;
32 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
33 intendedState:(OctagonState*)intendedState
34 peerUnknownState:(OctagonState*)peerUnknownState
35 errorState:(OctagonState*)errorState
36 retryFlag:(OctagonFlag* _Nullable)retryFlag
38 if((self = [super init])) {
41 _intendedState = intendedState;
42 _nextState = errorState;
43 _peerUnknownState = peerUnknownState;
45 _retryFlag = retryFlag;
53 self.finishedOp = [NSBlockOperation blockOperationWithBlock:^{
54 // If we errored in some unknown way, ask to try again!
58 if(self.retryFlag == nil) {
59 secerror("octagon: Received an error updating TPH, but no retry flag present.");
63 // Is this a very scary error?
66 OctagonPendingFlag* pendingFlag = nil;
68 if([self.deps.lockStateTracker isLockedError:self.error]) {
69 secnotice("octagon", "Updating trust state failed because locked, retry once unlocked: %@", self.error);
70 self.nextState = OctagonStateWaitForUnlock;
71 pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:self.retryFlag
72 conditions:OctagonPendingConditionsDeviceUnlocked];
75 // more CloudKit errors should trigger a retry here
76 secnotice("octagon", "Error is currently unknown, aborting: %@", self.error);
81 NSTimeInterval baseDelay = SecCKKSTestsEnabled() ? 2 : 30;
82 NSTimeInterval ckDelay = CKRetryAfterSecondsForError(self.error);
83 NSTimeInterval cuttlefishDelay = [self.error cuttlefishRetryAfter];
84 NSTimeInterval delay = MAX(ckDelay, cuttlefishDelay);
89 pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:self.retryFlag
90 delayInSeconds:delay];
92 secnotice("octagon", "Updating trust state no fatal: requesting retry: %@",
94 [self.deps.flagHandler handlePendingFlag:pendingFlag];
98 [self dependOnBeforeGroupFinished:self.finishedOp];
101 [self.deps.cuttlefishXPCWrapper updateWithContainer:self.deps.containerName
102 context:self.deps.contextID
103 deviceName:self.deps.deviceInformationAdapter.deviceName
104 serialNumber:self.deps.deviceInformationAdapter.serialNumber
105 osVersion:self.deps.deviceInformationAdapter.osVersion
108 reply:^(TrustedPeersHelperPeerState* peerState, NSError* error) {
110 if(error || !peerState) {
111 secerror("octagon: update errored: %@", error);
114 // On an error, for now, go back to the intended state
115 // <rdar://problem/50190005> Octagon: handle lock state errors in update()
116 self.nextState = self.intendedState;
117 [self runBeforeGroupFinished:self.finishedOp];
121 secnotice("octagon", "update complete: %@", peerState);
123 if(peerState.identityIsPreapproved) {
124 secnotice("octagon-sos", "Self peer is now preapproved!");
125 [self.deps.flagHandler handleFlag:OctagonFlagEgoPeerPreapproved];
127 if (peerState.memberChanges) {
128 secnotice("octagon", "Member list changed");
129 [self.deps.octagonAdapter sendTrustedPeerSetChangedUpdate];
132 if (peerState.unknownMachineIDsPresent) {
133 secnotice("octagon-authkit", "Unknown machine IDs are present; requesting fetch");
134 [self.deps.flagHandler handleFlag:OctagonFlagFetchAuthKitMachineIDList];
137 if(peerState.peerStatus & TPPeerStatusExcluded) {
138 secnotice("octagon", "Self peer (%@) is excluded; moving to untrusted", peerState.peerID);
139 self.nextState = OctagonStateBecomeUntrusted;
141 } else if(peerState.peerStatus & TPPeerStatusUnknown) {
142 secnotice("octagon", "Self peer (%@) is unknown; moving to '%@''", peerState.peerID, self.peerUnknownState);
143 self.nextState = self.peerUnknownState;
146 self.nextState = self.intendedState;
149 [self runBeforeGroupFinished:self.finishedOp];