5 #import <TargetConditionals.h>
6 #import <Security/SecKey.h>
7 #import <Security/SecKeyPriv.h>
8 #import <CloudKit/CloudKit_Private.h>
10 #import "keychain/ot/ObjCImprovements.h"
11 #import "keychain/ot/OTSOSUpdatePreapprovalsOperation.h"
12 #import "keychain/ot/OTOperationDependencies.h"
13 #import "keychain/ot/OTCuttlefishAccountStateHolder.h"
14 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
15 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
16 #import "keychain/ckks/CKKSAnalytics.h"
17 #import "keychain/ckks/CloudKitCategories.h"
19 @interface OTSOSUpdatePreapprovalsOperation ()
20 @property OTOperationDependencies* deps;
22 // Since we're making callback based async calls, use this operation trick to hold off the ending of this operation
23 @property NSOperation* finishedOp;
26 @implementation OTSOSUpdatePreapprovalsOperation
27 @synthesize nextState = _nextState;
28 @synthesize intendedState = _intendedState;
30 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
31 intendedState:(OctagonState*)intendedState
32 sosNotPresentState:(OctagonState*)sosNotPresentState
33 errorState:(OctagonState*)errorState
35 if((self = [super init])) {
38 _intendedState = intendedState;
39 _sosNotPresentState = sosNotPresentState;
40 _nextState = errorState;
49 if(!self.deps.sosAdapter.sosEnabled) {
50 secnotice("octagon-sos", "SOS not enabled on this platform?");
54 self.finishedOp = [NSBlockOperation blockOperationWithBlock:^{
55 // If we errored in some unknown way, ask to try again!
59 // Is this a very scary error?
62 NSTimeInterval ckDelay = CKRetryAfterSecondsForError(self.error);
63 NSTimeInterval cuttlefishDelay = [self.error cuttlefishRetryAfter];
64 NSTimeInterval delay = MAX(ckDelay, cuttlefishDelay);
69 if([self.error isCuttlefishError:CuttlefishErrorResultGraphNotFullyReachable]) {
70 secnotice("octagon-sos", "SOS update preapproval error is 'result graph not reachable'; retrying is useless: %@", self.error);
74 if([self.error.domain isEqualToString:TrustedPeersHelperErrorDomain] && self.error.code == TrustedPeersHelperErrorNoPreparedIdentity) {
75 secnotice("octagon-sos", "SOS update preapproval error is 'no prepared identity'; retrying immediately is useless: %@", self.error);
80 secnotice("octagon-sos", "SOS update preapproval error is not fatal: requesting retry in %0.2fs: %@", delay, self.error);
81 [self.deps.flagHandler handlePendingFlag:[[OctagonPendingFlag alloc] initWithFlag:OctagonFlagAttemptSOSUpdatePreapprovals
82 delayInSeconds:delay]];
86 [self dependOnBeforeGroupFinished:self.finishedOp];
88 NSError* sosPreapprovalError = nil;
89 NSArray<NSData*>* publicSigningSPKIs = [OTSOSAdapterHelpers peerPublicSigningKeySPKIsForCircle:self.deps.sosAdapter error:&sosPreapprovalError];
91 if(!publicSigningSPKIs || sosPreapprovalError) {
92 secerror("octagon-sos: Can't fetch trusted peers; stopping preapproved key update: %@", sosPreapprovalError);
93 self.error = sosPreapprovalError;
94 self.nextState = self.sosNotPresentState;
95 [self runBeforeGroupFinished:self.finishedOp];
99 secnotice("octagon-sos", "Updating SOS preapproved keys to %@", publicSigningSPKIs);
101 [self.deps.cuttlefishXPCWrapper setPreapprovedKeysWithContainer:self.deps.containerName
102 context:self.deps.contextID
103 preapprovedKeys:publicSigningSPKIs
104 reply:^(TrustedPeersHelperPeerState* _Nullable peerState, NSError* error) {
107 secerror("octagon-sos: unable to update preapproved keys: %@", error);
110 secnotice("octagon-sos", "Updated SOS preapproved keys");
112 if (peerState.memberChanges) {
113 secnotice("octagon", "Member list changed");
114 [self.deps.octagonAdapter sendTrustedPeerSetChangedUpdate];
117 self.nextState = self.intendedState;
119 [self runBeforeGroupFinished:self.finishedOp];