2 #import "utilities/debugging.h"
4 #import "keychain/ot/OctagonStateMachine.h"
5 #import "keychain/ot/OTStates.h"
6 #import "keychain/escrowrequest/EscrowRequestController.h"
7 #import "keychain/escrowrequest/EscrowRequestServer.h"
9 #import "keychain/ckks/CKKSLockStateTracker.h"
11 #import "keychain/ot/ObjCImprovements.h"
13 #import "keychain/escrowrequest/operations/EscrowRequestInformCloudServicesOperation.h"
14 #import "keychain/escrowrequest/operations/EscrowRequestPerformEscrowEnrollOperation.h"
16 #import "keychain/escrowrequest/generated_source/SecEscrowPendingRecord.h"
17 #import "keychain/escrowrequest/SecEscrowPendingRecord+KeychainSupport.h"
19 OctagonState* const EscrowRequestStateNothingToDo = (OctagonState*)@"nothing_to_do";
20 OctagonState* const EscrowRequestStateTriggerCloudServices = (OctagonState*)@"trigger_cloudservices";
22 OctagonState* const EscrowRequestStateAttemptEscrowUpload = (OctagonState*)@"trigger_escrow_upload";
23 OctagonState* const EscrowRequestStateWaitForUnlock = (OctagonState*)@"wait_for_unlock";
25 @interface EscrowRequestController ()
26 @property dispatch_queue_t queue;
27 @property CKKSLockStateTracker* lockStateTracker;
28 @property bool haveRecordedDate;
31 @implementation EscrowRequestController
33 - (instancetype)initWithLockStateTracker:(CKKSLockStateTracker*)lockStateTracker
35 if((self = [super init])) {
36 _queue = dispatch_queue_create("EscrowRequestControllerQueue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
37 _lockStateTracker = lockStateTracker;
39 _stateMachine = [[OctagonStateMachine alloc] initWithName:@"escrowrequest"
40 states:[NSSet setWithArray:@[EscrowRequestStateNothingToDo,
41 EscrowRequestStateTriggerCloudServices,
42 EscrowRequestStateAttemptEscrowUpload,
43 EscrowRequestStateWaitForUnlock]]
44 initialState:EscrowRequestStateNothingToDo
47 lockStateTracker:lockStateTracker];
49 _forceIgnoreCloudServicesRateLimiting = false;
55 - (CKKSResultOperation<OctagonStateTransitionOperationProtocol> * _Nullable)_onqueueNextStateMachineTransition:(nonnull OctagonState *)currentState
56 flags:(nonnull OctagonFlags *)flags
57 pendingFlags:(nonnull id<OctagonStateOnqueuePendingFlagHandler>)pendingFlagHandler
59 if([flags _onqueueContains:OctagonFlagEscrowRequestInformCloudServicesOperation]) {
60 [flags _onqueueRemoveFlag:OctagonFlagEscrowRequestInformCloudServicesOperation];
61 return [[EscrowRequestInformCloudServicesOperation alloc] initWithIntendedState:EscrowRequestStateNothingToDo
62 errorState:EscrowRequestStateNothingToDo
63 lockStateTracker:self.lockStateTracker];
66 if([currentState isEqualToString:EscrowRequestStateTriggerCloudServices]) {
67 return [[EscrowRequestInformCloudServicesOperation alloc] initWithIntendedState:EscrowRequestStateNothingToDo
68 errorState:EscrowRequestStateNothingToDo
69 lockStateTracker:self.lockStateTracker];
72 if([currentState isEqualToString:EscrowRequestStateAttemptEscrowUpload]) {
73 return [[EscrowRequestPerformEscrowEnrollOperation alloc] initWithIntendedState:EscrowRequestStateNothingToDo
74 errorState:EscrowRequestStateNothingToDo
75 enforceRateLimiting:true
76 lockStateTracker:self.lockStateTracker];
79 if([currentState isEqualToString:EscrowRequestStateWaitForUnlock]) {
80 secnotice("escrowrequest", "waiting for unlock before continuing with state machine");
81 OctagonStateTransitionOperation* op = [OctagonStateTransitionOperation named:@"wait-for-unlock"
82 entering:EscrowRequestStateNothingToDo];
83 [op addNullableDependency:self.lockStateTracker.unlockDependency];
88 NSArray<SecEscrowPendingRecord*>* records = [SecEscrowPendingRecord loadAllFromKeychain:&error];
90 if([self.lockStateTracker isLockedError:error]) {
91 return [OctagonStateTransitionOperation named:@"wait-for-unlock"
92 entering:EscrowRequestStateWaitForUnlock];
94 secnotice("escrowrequest", "failed to fetch records from keychain, nothing to do: %@", error);
98 // First, do we need to poke CloudServices?
99 for(SecEscrowPendingRecord* record in records) {
100 // Completed records don't need anything.
101 if(record.hasUploadCompleted && record.uploadCompleted) {
105 if (!self.haveRecordedDate) {
106 NSDate *date = [[CKKSAnalytics logger] datePropertyForKey:ESRPendingSince];
108 [[CKKSAnalytics logger] setDateProperty:[NSDate date] forKey:ESRPendingSince];
110 self.haveRecordedDate = true;
113 uint64_t fiveMinutesAgo = ((uint64_t)[[NSDate date] timeIntervalSince1970] * 1000) - (1000*60*5);
115 if(!record.certCached) {
116 if(!self.forceIgnoreCloudServicesRateLimiting && (record.hasLastCloudServicesTriggerTime && record.lastCloudServicesTriggerTime >= fiveMinutesAgo)) {
117 secnotice("escrowrequest", "Request %@ needs to cache a certificate, but that has been attempted recently. Holding off...", record.uuid);
121 secnotice("escrowrequest", "Request %@ needs a cached certififcate", record.uuid);
123 return [OctagonStateTransitionOperation named:@"escrow-request-cache-cert"
124 entering:EscrowRequestStateTriggerCloudServices];
127 if(record.hasSerializedPrerecord) {
128 if([record escrowAttemptedWithinLastSeconds:5*60]) {
129 secnotice("escrowrequest", "Request %@ needs to be stored, but has been attempted recently. Holding off...", record.uuid);
133 secnotice("escrowrequest", "Request %@ needs to be stored!", record.uuid);
135 return [OctagonStateTransitionOperation named:@"escrow-request-attempt-escrow-upload"
136 entering:EscrowRequestStateAttemptEscrowUpload];
144 - (void)triggerEscrowUpdateRPC:(nonnull NSString *)reason
145 reply:(nonnull void (^)(NSError * _Nullable))reply
147 [self.stateMachine startOperation];
149 NSError* error = nil;
150 NSArray<SecEscrowPendingRecord*>* records = [SecEscrowPendingRecord loadAllFromKeychain:&error];
151 if(error && !([error.domain isEqualToString:NSOSStatusErrorDomain] && error.code == errSecItemNotFound)) {
152 secnotice("escrowrequest", "failed to fetch records from keychain: %@", error);
158 secnotice("escrowrequest", "Investigating a new escrow request");
160 BOOL escrowRequestExists = NO;
161 for(SecEscrowPendingRecord* existingRecord in records) {
162 if(existingRecord.uploadCompleted) {
166 if (existingRecord.hasAltDSID) {
170 secnotice("escrowrequest", "Retriggering an existing escrow request: %@", existingRecord);
171 existingRecord.hasCertCached = false;
172 existingRecord.serializedPrerecord = nil;
174 [existingRecord saveToKeychain:&error];
176 secerror("escrowrequest: Unable to save modified request to keychain: %@", error);
181 secnotice("escrowrequest", "Retriggering an existing escrow request complete");
182 escrowRequestExists = YES;
185 if(escrowRequestExists == NO){
186 secnotice("escrowrequest", "Creating a new escrow request");
188 SecEscrowPendingRecord* record = [[SecEscrowPendingRecord alloc] init];
189 record.uuid = [[NSUUID UUID] UUIDString];
190 record.altDSID = nil;
191 record.triggerRequestTime = ((uint64_t)[[NSDate date] timeIntervalSince1970] * 1000);
192 secnotice("escrowrequest", "beginning a new escrow request (%@)", record.uuid);
194 [record saveToKeychain:&error];
197 secerror("escrowrequest: unable to save escrow update request: %@", error);
203 [[CKKSAnalytics logger] setDateProperty:[NSDate date] forKey:ESRPendingSince];
204 self.haveRecordedDate = true;
206 [self.stateMachine handleFlag:OctagonFlagEscrowRequestInformCloudServicesOperation];
211 - (void)storePrerecordsInEscrowRPC:(void (^)(uint64_t count, NSError* _Nullable error))reply
213 EscrowRequestPerformEscrowEnrollOperation* op = [[EscrowRequestPerformEscrowEnrollOperation alloc] initWithIntendedState:EscrowRequestStateNothingToDo
214 errorState:EscrowRequestStateNothingToDo
215 enforceRateLimiting:false
216 lockStateTracker:self.lockStateTracker];
217 [self.stateMachine startOperation];
218 [self.stateMachine doSimpleStateMachineRPC:@"trigger-escrow-store"
220 sourceStates:[NSSet setWithObject:EscrowRequestStateNothingToDo]
221 reply:^(NSError * _Nullable error) {
222 secnotice("escrowrequest", "Uploaded %d records with error %@", (int)op.numberOfRecordsUploaded, error);
223 reply(op.numberOfRecordsUploaded, error);