2 * Copyright (c) 2017 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@
25 #import <Foundation/Foundation.h>
26 #import <Foundation/NSKeyedArchiver_Private.h>
28 #import "keychain/ot/OTCloudStore.h"
29 #import "keychain/ot/OTCloudStoreState.h"
30 #import "keychain/ckks/CKKSZoneStateEntry.h"
31 #import "keychain/ckks/CKKS.h"
32 #import "keychain/ot/OTDefines.h"
33 #import "keychain/ckks/CKKSReachabilityTracker.h"
34 #import <utilities/debugging.h>
37 NS_ASSUME_NONNULL_BEGIN
39 /* Octagon Trust Local Context Record Constants */
40 static NSString* OTCKRecordContextID = @"contextID";
41 static NSString* OTCKRecordDSID = @"accountDSID";
42 static NSString* OTCKRecordContextName = @"contextName";
43 static NSString* OTCKRecordZoneCreated = @"zoneCreated";
44 static NSString* OTCKRecordSubscribedToChanges = @"subscribedToChanges";
45 static NSString* OTCKRecordChangeToken = @"changeToken";
46 static NSString* OTCKRecordEgoPeerID = @"egoPeerID";
47 static NSString* OTCKRecordEgoPeerCreationDate = @"egoPeerCreationDate";
48 static NSString* OTCKRecordRecoverySigningSPKI = @"recoverySigningSPKI";
49 static NSString* OTCKRecordRecoveryEncryptionSPKI = @"recoveryEncryptionSPKI";
50 static NSString* OTCKRecordBottledPeerTableEntry = @"bottledPeer";
52 /* Octagon Trust Local Peer Record */
53 static NSString* OTCKRecordPeerID = @"peerID";
54 static NSString* OTCKRecordPermanentInfo = @"permanentInfo";
55 static NSString* OTCKRecordStableInfo = @"stableInfo";
56 static NSString* OTCKRecordDynamicInfo = @"dynamicInfo";
57 static NSString* OTCKRecordRecoveryVoucher = @"recoveryVoucher";
58 static NSString* OTCKRecordIsEgoPeer = @"isEgoPeer";
60 /* Octagon Trust BottledPeerSchema */
61 static NSString* OTCKRecordEscrowRecordID = @"escrowRecordID";
62 static NSString* OTCKRecordBottle = @"bottle";
63 static NSString* OTCKRecordSPID = @"spID";
64 static NSString* OTCKRecordEscrowSigningSPKI = @"escrowSigningSPKI";
65 static NSString* OTCKRecordPeerSigningSPKI = @"peerSigningSPKI";
66 static NSString* OTCKRecordSignatureFromEscrow = @"signatureUsingEscrow";
67 static NSString* OTCKRecordSignatureFromPeerKey = @"signatureUsingPeerKey";
68 static NSString* OTCKRecordEncodedRecord = @"encodedRecord";
70 /* Octagon Table Names */
71 static NSString* const contextTable = @"context";
72 static NSString* const peerTable = @"peer";
73 static NSString* const bottledPeerTable = @"bp";
75 /* Octagon Trust Schemas */
76 static NSString* const octagonZoneName = @"OctagonTrustZone";
78 /* Octagon Cloud Kit defines */
79 static NSString* OTCKContainerName = @"com.apple.security.keychain";
80 static NSString* OTCKZoneName = @"OctagonTrust";
81 static NSString* OTCKRecordName = @"bp-";
82 static NSString* OTCKRecordBottledPeerType = @"OTBottledPeer";
84 @interface OTCloudStore ()
85 @property (nonatomic, strong) NSString* dsid;
86 @property (nonatomic, strong) NSString* containerName;
87 @property (nonatomic, strong) CKModifyRecordsOperation* modifyRecordsOperation;
88 @property (nonatomic, strong) CKDatabaseOperation<CKKSFetchRecordZoneChangesOperation>* fetchRecordZoneChangesOperation;
89 @property (nonatomic, strong) NSOperationQueue *operationQueue;
90 @property (nonatomic, strong) OTLocalStore* localStore;
91 @property (nonatomic, strong) CKKSResultOperation* viewSetupOperation;
92 @property (nonatomic, strong) NSError* error;
95 @class CKKSAPSReceiver;
97 @interface OTCloudStore()
99 @property CKDatabaseOperation<CKKSModifyRecordZonesOperation>* zoneCreationOperation;
100 @property CKDatabaseOperation<CKKSModifyRecordZonesOperation>* zoneDeletionOperation;
101 @property CKDatabaseOperation<CKKSModifySubscriptionsOperation>* zoneSubscriptionOperation;
103 @property NSOperation* accountLoggedInDependency;
105 @property NSHashTable<NSOperation*>* accountOperations;
108 @implementation OTCloudStore
110 - (instancetype) initWithContainer:(CKContainer*) container
111 zoneName:(NSString*)zoneName
112 accountTracker:(nullable CKKSCKAccountStateTracker*)accountTracker
113 reachabilityTracker:(nullable CKKSReachabilityTracker*)reachabilityTracker
114 localStore:(OTLocalStore*)localStore
115 contextID:(NSString*)contextID
117 fetchRecordZoneChangesOperationClass:(Class<CKKSFetchRecordZoneChangesOperation>) fetchRecordZoneChangesOperationClass
118 fetchRecordsOperationClass:(Class<CKKSFetchRecordsOperation>)fetchRecordsOperationClass
119 queryOperationClass:(Class<CKKSQueryOperation>)queryOperationClass
120 modifySubscriptionsOperationClass:(Class<CKKSModifySubscriptionsOperation>) modifySubscriptionsOperationClass
121 modifyRecordZonesOperationClass:(Class<CKKSModifyRecordZonesOperation>) modifyRecordZonesOperationClass
122 apsConnectionClass:(Class<CKKSAPSConnection>) apsConnectionClass
123 operationQueue:(nullable NSOperationQueue *)operationQueue
126 self = [super initWithContainer:container
128 accountTracker:accountTracker
129 reachabilityTracker:reachabilityTracker
130 fetchRecordZoneChangesOperationClass:fetchRecordZoneChangesOperationClass
131 fetchRecordsOperationClass:fetchRecordsOperationClass
132 queryOperationClass:queryOperationClass
133 modifySubscriptionsOperationClass:modifySubscriptionsOperationClass
134 modifyRecordZonesOperationClass:modifyRecordZonesOperationClass
135 apsConnectionClass:apsConnectionClass];
138 if (!operationQueue) {
139 operationQueue = [[NSOperationQueue alloc] init];
141 _contextID = [contextID copy];
142 _localStore = localStore;
143 _containerName = OTCKContainerName;
145 _operationQueue = operationQueue;
146 self.queue = dispatch_queue_create([[NSString stringWithFormat:@"OctagonTrustQueue.%@.zone.%@", container.containerIdentifier, zoneName] UTF8String], DISPATCH_QUEUE_SERIAL);
147 [self initializeZone];
153 -(CKKSResultOperation*) otFetchAndProcessUpdates
155 CKKSResultOperation* fetchOp = [CKKSResultOperation named:@"fetch-and-process-updates-watcher" withBlock:^{}];
157 __weak __typeof(self) weakSelf = self;
159 [self dispatchSync: ^bool{
161 OTCloudStoreState* state = [OTCloudStoreState state: self.zoneName];
163 CKFetchRecordZoneChangesOptions* options = [[CKFetchRecordZoneChangesOptions alloc] init];
164 options.previousServerChangeToken = state.changeToken;
166 self.fetchRecordZoneChangesOperation = [[[self.fetchRecordZoneChangesOperationClass class] alloc] initWithRecordZoneIDs:@[self.zoneID] optionsByRecordZoneID:@{self.zoneID : options}];
168 self.fetchRecordZoneChangesOperation.recordChangedBlock = ^(CKRecord *record) {
169 secinfo("octagon", "CloudKit notification: record changed(%@): %@", [record recordType], record);
170 __strong __typeof(weakSelf) strongSelf = weakSelf;
173 secnotice("octagon", "received callback for released object");
174 fetchOp.error = [NSError errorWithDomain:octagonErrorDomain code:OTErrorOTCloudStore userInfo:@{NSLocalizedDescriptionKey: @"received callback for released object"}];
176 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
180 if ([record.recordType isEqualToString:OTCKRecordBottledPeerType]) {
181 NSError* localError = nil;
183 //write to localStore
184 OTBottledPeerRecord *rec = [[OTBottledPeerRecord alloc] init];
185 rec.bottle = record[OTCKRecordBottle];
186 rec.spID = record[OTCKRecordSPID];
187 rec.escrowRecordID = record[OTCKRecordEscrowRecordID];
188 rec.escrowedSigningSPKI = record[OTCKRecordEscrowSigningSPKI];
189 rec.peerSigningSPKI = record[OTCKRecordPeerSigningSPKI];
190 rec.signatureUsingEscrowKey = record[OTCKRecordSignatureFromEscrow];
191 rec.signatureUsingPeerKey = record[OTCKRecordSignatureFromPeerKey];
192 rec.encodedRecord = [strongSelf recordToData:record];
193 rec.launched = @"YES";
194 BOOL result = [strongSelf.localStore insertBottledPeerRecord:rec escrowRecordID:record[OTCKRecordEscrowRecordID] error:&localError];
195 if(!result || localError){
196 secerror("Could not write bottled peer record:%@ to database: %@", record.recordID.recordName, localError);
197 fetchOp.error = localError;
198 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
201 secnotice("octagon", "fetched changes: %@", record);
205 self.fetchRecordZoneChangesOperation.recordWithIDWasDeletedBlock = ^(CKRecordID *RecordID, NSString *recordType) {
206 secinfo("octagon", "CloudKit notification: deleted record(%@): %@", recordType, RecordID);
209 self.fetchRecordZoneChangesOperation.recordZoneChangeTokensUpdatedBlock = ^(CKRecordZoneID *recordZoneID, CKServerChangeToken *serverChangeToken, NSData *clientChangeTokenData) {
210 __strong __typeof(weakSelf) strongSelf = weakSelf;
211 NSError* error = nil;
212 OTCloudStoreState* state = [OTCloudStoreState state: strongSelf.zoneName];
213 secdebug("octagon", "Received a new server change token: %@ %@", serverChangeToken, clientChangeTokenData);
214 state.changeToken = serverChangeToken;
217 secerror("octagon: Couldn't save new server change token: %@", error);
218 fetchOp.error = error;
219 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
223 // Completion blocks don't count for dependencies. Use this intermediate operation hack instead.
224 NSBlockOperation* recordZoneChangesCompletedOperation = [[NSBlockOperation alloc] init];
225 self.fetchRecordZoneChangesOperation.recordZoneFetchCompletionBlock = ^(CKRecordZoneID *recordZoneID, CKServerChangeToken *serverChangeToken, NSData *clientChangeTokenData, BOOL moreComing, NSError * recordZoneError) {
226 __strong __typeof(weakSelf) strongSelf = weakSelf;
228 secnotice("octagon", "received callback for released object");
231 if(recordZoneError) {
232 secerror("octagon: FetchRecordZoneChanges(%@) error: %@", strongSelf.zoneName, recordZoneError);
233 fetchOp.error = recordZoneError;
234 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
237 // TODO: fetch state here
238 if(serverChangeToken) {
239 NSError* error = nil;
240 secdebug("octagon", "Zone change fetch complete: received a new server change token: %@ %@", serverChangeToken, clientChangeTokenData);
241 state.changeToken = serverChangeToken;
243 secerror("octagon: Couldn't save new server change token: %@", error);
244 fetchOp.error = error;
245 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
248 secdebug("octagon", "Record zone fetch complete: changeToken=%@ error=%@", serverChangeToken, recordZoneError);
250 [strongSelf.operationQueue addOperation: recordZoneChangesCompletedOperation];
251 [strongSelf.operationQueue addOperation: fetchOp];
254 self.fetchRecordZoneChangesOperation.fetchRecordZoneChangesCompletionBlock = ^(NSError * _Nullable operationError) {
255 __strong __typeof(weakSelf) strongSelf = weakSelf;
257 secnotice("octagon", "received callback for released object");
258 fetchOp.error = [NSError errorWithDomain:octagonErrorDomain code:OTErrorOTCloudStore userInfo:@{NSLocalizedDescriptionKey: @"received callback for released object"}];
259 fetchOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerFetchRecords;
262 secnotice("octagon", "Record zone changes fetch complete: error=%@", operationError);
266 [self.database addOperation: self.fetchRecordZoneChangesOperation];
272 - (void)notifyZoneChange:(CKRecordZoneNotification* _Nullable)notification
274 secnotice("octagon", "received notify zone change. notification: %@", notification);
276 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-fetch-and-process-changes" withBlock:^{}];
278 [op addSuccessDependency: [self otFetchAndProcessUpdates]];
280 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
281 [self.operationQueue addOperation: op];
283 [op waitUntilFinished];
284 if(op.error != nil) {
285 secerror("octagon: failed to fetch changes error:%@", op.error);
288 secnotice("octagon", "downloaded bottled peer records");
292 -(BOOL) downloadBottledPeerRecord:(NSError**)error
294 secnotice("octagon", "downloadBottledPeerRecord");
296 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-fetch-and-process-changes" withBlock:^{}];
298 [op addSuccessDependency: [self otFetchAndProcessUpdates]];
300 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
301 [self.operationQueue addOperation: op];
303 [op waitUntilFinished];
304 if(op.error != nil) {
305 secerror("octagon: failed to fetch changes error:%@", op.error);
312 secnotice("octagon", "downloaded bottled peer records");
317 - (nullable NSArray*) retrieveListOfEligibleEscrowRecordIDs:(NSError**)error
319 NSError* localError = nil;
321 NSMutableArray* recordIDs = [NSMutableArray array];
323 //fetch any recent changes first before gathering escrow record ids
324 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-fetch-and-process-changes" withBlock:^{}];
326 secnotice("octagon", "Beginning CloudKit fetch");
327 [op addSuccessDependency: [self otFetchAndProcessUpdates]];
329 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
330 [self.operationQueue addOperation: op];
332 [op waitUntilFinished];
333 if(op.error != nil) {
334 secerror("octagon: failed to fetch changes error:%@", op.error);
340 NSArray* localStoreBottledPeerRecords = [self.localStore readAllLocalBottledPeerRecords:&localError];
341 if(!localStoreBottledPeerRecords)
343 secerror("octagon: local store contains no bottled peer entries: %@", localError);
349 for(OTBottledPeerRecord* entry in localStoreBottledPeerRecords){
350 NSString* escrowID = entry.escrowRecordID;
351 if(escrowID && ![recordIDs containsObject:escrowID]){
352 [recordIDs addObject:escrowID];
359 -(CKRecord*) dataToRecord:(NSData*)encodedRecord
361 NSKeyedUnarchiver *coder = [[NSKeyedUnarchiver alloc] initForReadingFromData:encodedRecord error:nil];
362 CKRecord* record = [[CKRecord alloc] initWithCoder:coder];
363 [coder finishDecoding];
367 -(NSData*) recordToData:(CKRecord*)record
369 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
370 [record encodeWithCoder:archiver];
371 [archiver finishEncoding];
373 return archiver.encodedData;
376 -( CKRecord* _Nullable ) CKRecordFromMirror:(CKRecordID*)recordID bpRecord:(OTBottledPeerRecord*)bprecord escrowRecordID:(NSString*)escrowRecordID error:(NSError**)error
378 CKRecord* record = nil;
380 OTBottledPeerRecord* recordFromDB = [self.localStore readLocalBottledPeerRecordWithRecordID:recordID.recordName error:error];
381 if(recordFromDB && recordFromDB.encodedRecord != nil){
382 record = [self dataToRecord:recordFromDB.encodedRecord];
385 record = [[CKRecord alloc] initWithRecordType:OTCKRecordBottledPeerType recordID:recordID];
389 secerror("octagon: failed to create cloud kit record");
391 *error = [NSError errorWithDomain:octagonErrorDomain code:OTErrorOTCloudStore userInfo:@{NSLocalizedDescriptionKey: @"failed to create cloud kit record"}];
395 record[OTCKRecordPeerID] = bprecord.peerID;
396 record[OTCKRecordSPID] = bprecord.spID;
397 record[OTCKRecordEscrowSigningSPKI] = bprecord.escrowedSigningSPKI;
398 record[OTCKRecordPeerSigningSPKI] = bprecord.peerSigningSPKI;
399 record[OTCKRecordEscrowRecordID] = escrowRecordID;
400 record[OTCKRecordBottle] = bprecord.bottle;
401 record[OTCKRecordSignatureFromEscrow] = bprecord.signatureUsingEscrowKey;
402 record[OTCKRecordSignatureFromPeerKey] = bprecord.signatureUsingPeerKey;
407 -(CKKSResultOperation*) modifyRecords:(NSArray<CKRecord *>*) recordsToSave deleteRecordIDs:(NSArray<CKRecordID*>*) recordIDsToDelete
409 __weak __typeof(self) weakSelf = self;
410 CKKSResultOperation* modifyOp = [CKKSResultOperation named:@"modify-records-watcher" withBlock:^{}];
412 [self dispatchSync: ^bool{
413 self.modifyRecordsOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsToSave recordIDsToDelete:recordIDsToDelete];
415 self.modifyRecordsOperation.atomic = YES;
416 self.modifyRecordsOperation.longLived = NO; // The keys are only in memory; mark this explicitly not long-lived
417 self.modifyRecordsOperation.qualityOfService = NSQualityOfServiceUserInitiated; // Currently done during buddy. User is waiting.
418 self.modifyRecordsOperation.savePolicy = CKRecordSaveIfServerRecordUnchanged;
420 self.modifyRecordsOperation.perRecordCompletionBlock = ^(CKRecord *record, NSError * _Nullable error) {
421 // These should all fail or succeed as one. Do the hard work in the records completion block.
423 secnotice("octagon", "Successfully completed upload for %@", record.recordID.recordName);
426 secerror("octagon: error on row: %@ %@", record.recordID.recordName, error);
427 modifyOp.error = error;
428 modifyOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerModifyRecords;
429 [weakSelf.operationQueue addOperation:modifyOp];
432 self.modifyRecordsOperation.modifyRecordsCompletionBlock = ^(NSArray<CKRecord *> *savedRecords, NSArray<CKRecordID *> *deletedRecordIDs, NSError *error) {
433 secnotice("octagon", "Completed trust update");
434 __strong __typeof(weakSelf) strongSelf = weakSelf;
437 modifyOp.error = error;
438 modifyOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerModifyRecords;
439 secerror("octagon: received error from cloudkit: %@", error);
440 if([error.domain isEqualToString:CKErrorDomain] && (error.code == CKErrorPartialFailure)) {
441 NSMutableDictionary<CKRecordID*, NSError*>* failedRecords = error.userInfo[CKPartialErrorsByItemIDKey];
442 ckksnotice("octagon", strongSelf, "failed records %@", failedRecords);
447 secerror("octagon: received callback for released object");
448 modifyOp.error = [NSError errorWithDomain:octagonErrorDomain code:OTErrorOTCloudStore userInfo:@{NSLocalizedDescriptionKey: @"received callback for released object"}];
449 modifyOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerModifyRecords;
450 [strongSelf.operationQueue addOperation:modifyOp];
454 if(savedRecords && [savedRecords count] > 0){
455 for(CKRecord* record in savedRecords){
456 NSError* localError = nil;
457 secnotice("octagon", "saving recordID: %@ changeToken:%@", record.recordID.recordName, record.recordChangeTag);
459 //write to localStore
460 OTBottledPeerRecord *rec = [[OTBottledPeerRecord alloc] init];
461 rec.bottle = record[OTCKRecordBottle];
462 rec.spID = record[OTCKRecordSPID];
463 rec.escrowRecordID = record[OTCKRecordEscrowRecordID];
464 rec.signatureUsingEscrowKey = record[OTCKRecordSignatureFromEscrow];
465 rec.signatureUsingPeerKey = record[OTCKRecordSignatureFromPeerKey];
466 rec.encodedRecord = [strongSelf recordToData:record];
467 rec.launched = @"YES";
468 rec.escrowedSigningSPKI = record[OTCKRecordEscrowSigningSPKI];
469 rec.peerSigningSPKI = record[OTCKRecordPeerSigningSPKI];
471 BOOL result = [strongSelf.localStore insertBottledPeerRecord:rec escrowRecordID:record[OTCKRecordEscrowRecordID] error:&localError];
473 if(!result || localError){
474 secerror("Could not write bottled peer record:%@ to database: %@", record.recordID.recordName, localError);
478 secerror("octagon: could not save to database: %@", localError);
479 modifyOp.error = localError;
480 modifyOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerModifyRecords;
484 else if(deletedRecordIDs && [deletedRecordIDs count] >0){
485 for(CKRecordID* recordID in deletedRecordIDs){
486 secnotice("octagon", "removed recordID: %@", recordID);
487 NSError* localError = nil;
488 BOOL result = [strongSelf.localStore deleteBottledPeer:recordID.recordName error:&localError];
490 secerror("octagon: could not remove record id: %@, error:%@", recordID, localError);
491 modifyOp.error = localError;
492 modifyOp.descriptionErrorCode = CKKSResultDescriptionPendingBottledPeerModifyRecords;
496 [strongSelf.operationQueue addOperation:modifyOp];
501 [self.database addOperation: self.modifyRecordsOperation];
505 - (BOOL) uploadBottledPeerRecord:(OTBottledPeerRecord *)bprecord
506 escrowRecordID:(NSString *)escrowRecordID
507 error:(NSError**)error
509 secnotice("octagon", "sending bottled peer to cloudkit");
512 CKRecordID* recordID = [[CKRecordID alloc] initWithRecordName:bprecord.recordName zoneID:self.zoneID];
513 CKRecord *record = [self CKRecordFromMirror:recordID bpRecord:bprecord escrowRecordID:escrowRecordID error:error];
518 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-modify-changes" withBlock:^{}];
520 secnotice("octagon", "Beginning CloudKit ModifyRecords");
521 [op addSuccessDependency: [self modifyRecords:@[ record ] deleteRecordIDs:@[]]];
523 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
524 [self.operationQueue addOperation: op];
526 [op waitUntilFinished];
527 if(op.error != nil) {
528 secerror("octagon: failed to commit record changes error:%@", op.error);
534 secnotice("octagon", "successfully uploaded record: %@", bprecord.recordName);
538 -(BOOL) removeBottledPeerRecordID:(CKRecordID*)recordID error:(NSError**)error
540 secnotice("octagon", "removing bottled peer from cloudkit");
543 NSMutableArray<CKRecordID*>* recordIDsToRemove = [[NSMutableArray alloc] init];
544 [recordIDsToRemove addObject:recordID];
546 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-modify-changes" withBlock:^{}];
548 secnotice("octagon", "Beginning CloudKit ModifyRecords");
549 [op addSuccessDependency: [self modifyRecords:[NSMutableArray array] deleteRecordIDs:recordIDsToRemove]];
551 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
552 [self.operationQueue addOperation: op];
554 [op waitUntilFinished];
555 if(op.error != nil) {
556 secerror("octagon: ailed to commit record changes error:%@", op.error);
566 - (void)_onqueueHandleCKLogin {
567 if(!SecCKKSIsEnabled()) {
568 ckksnotice("ckks", self, "Skipping CloudKit initialization due to disabled CKKS");
572 dispatch_assert_queue(self.queue);
574 __weak __typeof(self) weakSelf = self;
576 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry state: self.zoneName];
577 [self handleCKLogin:ckse.ckzonecreated zoneSubscribed:ckse.ckzonesubscribed];
579 self.viewSetupOperation = [CKKSResultOperation operationWithBlock: ^{
580 __strong __typeof(weakSelf) strongSelf = weakSelf;
582 ckkserror("ckks", strongSelf, "received callback for released object");
586 __block bool quit = false;
588 [strongSelf dispatchSync: ^bool {
589 ckksnotice("octagon", strongSelf, "Zone setup progress: %@ %d %@ %d %@",
590 [CKKSCKAccountStateTracker stringFromAccountStatus:strongSelf.accountStatus],
591 strongSelf.zoneCreated, strongSelf.zoneCreatedError, strongSelf.zoneSubscribed, strongSelf.zoneSubscribedError);
593 NSError* error = nil;
594 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry state: strongSelf.zoneName];
595 ckse.ckzonecreated = strongSelf.zoneCreated;
596 ckse.ckzonesubscribed = strongSelf.zoneSubscribed;
598 // Although, if the zone subscribed error says there's no zone, mark down that there's no zone
599 if(strongSelf.zoneSubscribedError &&
600 [strongSelf.zoneSubscribedError.domain isEqualToString:CKErrorDomain] && strongSelf.zoneSubscribedError.code == CKErrorPartialFailure) {
601 NSError* subscriptionError = strongSelf.zoneSubscribedError.userInfo[CKPartialErrorsByItemIDKey][strongSelf.zoneID];
602 if(subscriptionError && [subscriptionError.domain isEqualToString:CKErrorDomain] && subscriptionError.code == CKErrorZoneNotFound) {
604 ckkserror("octagon", strongSelf, "zone subscription error appears to say the zone doesn't exist, fixing status: %@", strongSelf.zoneSubscribedError);
605 ckse.ckzonecreated = false;
609 [ckse saveToDatabase: &error];
611 ckkserror("octagon", strongSelf, "couldn't save zone creation status for %@: %@", strongSelf.zoneName, error);
614 if(!strongSelf.zoneCreated || !strongSelf.zoneSubscribed || strongSelf.accountStatus != CKAccountStatusAvailable) {
615 // Something has gone very wrong. Error out and maybe retry.
618 // Note that CKKSZone has probably called [handleLogout]; which means we have a key hierarchy reset queued up. Error here anyway.
619 NSError* realReason = strongSelf.zoneCreatedError ? strongSelf.zoneCreatedError : strongSelf.zoneSubscribedError;
620 strongSelf.viewSetupOperation.error = realReason;
630 ckkserror("octagon", strongSelf, "Quitting setup.");
634 self.viewSetupOperation.name = @"zone-setup";
636 [self.viewSetupOperation addNullableDependency: self.zoneSetupOperation];
637 [self scheduleAccountStatusOperation: self.viewSetupOperation];
640 - (void)handleCKLogin
642 ckksinfo("octagon", self, "received a notification of CK login");
644 __weak __typeof(self) weakSelf = self;
645 CKKSResultOperation* login = [CKKSResultOperation named:@"octagon-login" withBlock:^{
646 __strong __typeof(self) strongSelf = weakSelf;
648 [strongSelf dispatchSync:^bool{
649 strongSelf.accountStatus = CKKSAccountStatusAvailable;
650 [strongSelf _onqueueHandleCKLogin];
655 [self scheduleAccountStatusOperation:login];
658 - (bool)_onqueueResetLocalData: (NSError * __autoreleasing *) error {
659 dispatch_assert_queue(self.queue);
661 NSError* localerror = nil;
662 bool setError = false;
664 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry state: self.zoneName];
665 ckse.ckzonecreated = false;
666 ckse.ckzonesubscribed = false;
667 ckse.changeToken = NULL;
668 [ckse saveToDatabase: &localerror];
670 ckkserror("ckks", self, "couldn't reset zone status for %@: %@", self.zoneName, localerror);
671 if(error && !setError) {
672 *error = localerror; setError = true;
676 BOOL result = [_localStore removeAllBottledPeerRecords:&localerror];
679 secerror("octagon: failed to move all bottled peer entries for context: %@ error: %@", self.contextID, localerror);
681 return (localerror == nil && !setError);
684 -(CKKSResultOperation*) resetOctagonTrustZone:(NSError**)error
686 // On a reset, we should cancel all existing operations
687 [self cancelAllOperations];
688 CKKSResultOperation* reset = [super deleteCloudKitZoneOperation:nil];
689 [self scheduleOperationWithoutDependencies:reset];
691 __weak __typeof(self) weakSelf = self;
692 CKKSGroupOperation* resetFollowUp = [[CKKSGroupOperation alloc] init];
693 resetFollowUp.name = @"cloudkit-reset-follow-up-group";
695 [resetFollowUp runBeforeGroupFinished: [CKKSResultOperation named:@"cloudkit-reset-follow-up" withBlock: ^{
696 __strong __typeof(weakSelf) strongSelf = weakSelf;
698 ckkserror("octagon", strongSelf, "received callback for released object");
703 ckksnotice("octagon", strongSelf, "Successfully deleted zone %@", strongSelf.zoneName);
704 __block NSError* error = nil;
706 [strongSelf dispatchSync: ^bool{
707 [strongSelf _onqueueResetLocalData: &error];
711 // Shouldn't ever happen, since reset is a successDependency
712 ckkserror("ckks", strongSelf, "Couldn't reset zone %@: %@", strongSelf.zoneName, reset.error);
716 [resetFollowUp addSuccessDependency:reset];
717 [self scheduleOperationWithoutDependencies:resetFollowUp];
722 -(BOOL) performReset:(NSError**)error
725 CKKSResultOperation* op = [CKKSResultOperation named:@"cloudkit-reset-zones-waiter" withBlock:^{}];
727 secnotice("octagon", "Beginning CloudKit reset for Octagon Trust");
728 [op addSuccessDependency:[self resetOctagonTrustZone:error]];
730 [op timeout:(SecCKKSTestsEnabled() ? 2*NSEC_PER_SEC : 120*NSEC_PER_SEC)];
731 [self.operationQueue addOperation: op];
733 [op waitUntilFinished];
735 secnotice("octagon", "Completed rpcResetCloudKit");
736 __weak __typeof(self) weakSelf = self;
737 CKKSResultOperation* login = [CKKSResultOperation named:@"octagon-login" withBlock:^{
738 __strong __typeof(self) strongSelf = weakSelf;
740 [strongSelf dispatchSync:^bool{
741 strongSelf.accountStatus = CKKSAccountStatusAvailable;
742 [strongSelf handleCKLogin:false zoneSubscribed:false];
747 [self.operationQueue addOperation:login];
750 secnotice("octagon", "Completed rpcResetCloudKit with error: %@", op.error);
761 NS_ASSUME_NONNULL_END