2 * Copyright (c) 2016 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@
24 #include <AssertMacros.h>
26 #import <Foundation/Foundation.h>
29 #import "CloudKitDependencies.h"
30 #import "keychain/ckks/CKKSAccountStateTracker.h"
31 #import "keychain/ckks/CloudKitCategories.h"
32 #import "keychain/categories/NSError+UsefulConstructors.h"
33 #import <CloudKit/CloudKit.h>
34 #import <CloudKit/CloudKit_Private.h>
36 #import "keychain/ot/ObjCImprovements.h"
38 #import "CKKSKeychainView.h"
41 #include <utilities/debugging.h>
45 @property CKDatabaseOperation<CKKSModifyRecordZonesOperation>* zoneCreationOperation;
46 @property CKDatabaseOperation<CKKSModifyRecordZonesOperation>* zoneDeletionOperation;
47 @property CKDatabaseOperation<CKKSModifySubscriptionsOperation>* zoneSubscriptionOperation;
49 @property NSOperationQueue* operationQueue;
50 @property CKKSResultOperation* accountLoggedInDependency;
52 @property NSHashTable<NSOperation*>* accountOperations;
55 @property bool halted;
58 @implementation CKKSZone
60 - (instancetype)initWithContainer:(CKContainer*)container
61 zoneName:(NSString*)zoneName
62 accountTracker:(CKKSAccountStateTracker*)accountTracker
63 reachabilityTracker:(CKKSReachabilityTracker*)reachabilityTracker
64 zoneModifier:(CKKSZoneModifier*)zoneModifier
65 cloudKitClassDependencies:(CKKSCloudKitClassDependencies*)cloudKitClassDependencies
67 if(self = [super init]) {
68 _container = container;
70 _accountTracker = accountTracker;
71 _reachabilityTracker = reachabilityTracker;
73 _zoneModifier = zoneModifier;
77 _database = [_container privateCloudDatabase];
78 _zone = [[CKRecordZone alloc] initWithZoneID: [[CKRecordZoneID alloc] initWithZoneName:zoneName ownerName:CKCurrentUserDefaultName]];
80 _accountStatus = CKKSAccountStatusUnknown;
82 _accountLoggedInDependency = [self createAccountLoggedInDependency:@"CloudKit account logged in."];
84 _accountOperations = [NSHashTable weakObjectsHashTable];
86 _cloudKitClassDependencies = cloudKitClassDependencies;
88 _queue = dispatch_queue_create([[NSString stringWithFormat:@"CKKSQueue.%@.zone.%@", container.containerIdentifier, zoneName] UTF8String], DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
89 _operationQueue = [[NSOperationQueue alloc] init];
94 - (CKKSResultOperation*)createAccountLoggedInDependency:(NSString*)message {
96 CKKSResultOperation* accountLoggedInDependency = [CKKSResultOperation named:@"account-logged-in-dependency" withBlock:^{
98 ckksnotice("ckkszone", self, "%@", message);
100 accountLoggedInDependency.descriptionErrorCode = CKKSResultDescriptionPendingAccountLoggedIn;
101 return accountLoggedInDependency;
104 - (void)beginCloudKitOperation {
105 [self.accountTracker registerForNotificationsOfCloudKitAccountStatusChange:self];
109 self.zoneCreated = false;
110 self.zoneSubscribed = false;
111 self.zoneCreatedError = nil;
112 self.zoneSubscribedError = nil;
114 self.zoneCreationOperation = nil;
115 self.zoneSubscriptionOperation = nil;
116 self.zoneDeletionOperation = nil;
119 - (CKRecordZoneID*)zoneID {
120 return [self.zone zoneID];
123 - (CKKSAccountStatus)accountStatusFromCKAccountInfo:(CKAccountInfo*)info
126 return CKKSAccountStatusUnknown;
128 if(info.accountStatus == CKAccountStatusAvailable &&
129 info.hasValidCredentials) {
130 return CKKSAccountStatusAvailable;
132 return CKKSAccountStatusNoAccount;
137 - (void)cloudkitAccountStateChange:(CKAccountInfo* _Nullable)oldAccountInfo to:(CKAccountInfo*)currentAccountInfo
139 ckksnotice("ckkszone", self, "%@ Received notification of CloudKit account status change, moving from %@ to %@",
140 self.zoneID.zoneName,
144 // Filter for device2device encryption and cloudkit grey mode
145 CKKSAccountStatus oldStatus = [self accountStatusFromCKAccountInfo:oldAccountInfo];
146 CKKSAccountStatus currentStatus = [self accountStatusFromCKAccountInfo:currentAccountInfo];
148 if(oldStatus == currentStatus) {
149 ckksnotice("ckkszone", self, "Computed status of new CK account info is same as old status: %@", [CKKSAccountStateTracker stringFromAccountStatus:currentStatus]);
153 switch(currentStatus) {
154 case CKKSAccountStatusAvailable: {
155 ckksnotice("ckkszone", self, "Logged into iCloud.");
156 [self handleCKLogin];
158 if(self.accountLoggedInDependency) {
159 [self.operationQueue addOperation:self.accountLoggedInDependency];
160 self.accountLoggedInDependency = nil;
165 case CKKSAccountStatusNoAccount: {
166 ckksnotice("ckkszone", self, "Logging out of iCloud. Shutting down.");
168 if(!self.accountLoggedInDependency) {
169 self.accountLoggedInDependency = [self createAccountLoggedInDependency:@"CloudKit account logged in again."];
172 [self handleCKLogout];
176 case CKKSAccountStatusUnknown: {
177 // We really don't expect to receive this as a notification, but, okay!
178 ckksnotice("ckkszone", self, "Account status has become undetermined. Pausing for %@", self.zoneID.zoneName);
180 if(!self.accountLoggedInDependency) {
181 self.accountLoggedInDependency = [self createAccountLoggedInDependency:@"CloudKit account logged in again."];
184 [self handleCKLogout];
190 - (CKKSResultOperation*)handleCKLogin:(bool)zoneCreated zoneSubscribed:(bool)zoneSubscribed {
191 if(!SecCKKSIsEnabled()) {
192 ckksinfo("ckkszone", self, "Skipping CloudKit registration due to disabled CKKS");
196 // If we've already started set up and that hasn't finished, complain
197 if([self.zoneSetupOperation isPending] || [self.zoneSetupOperation isExecuting]) {
198 ckksnotice("ckkszone", self, "Asked to handleCKLogin, but zoneSetupOperation appears to not be complete? %@ Continuing anyway", self.zoneSetupOperation);
201 self.zoneSetupOperation = [[CKKSGroupOperation alloc] init];
202 self.zoneSetupOperation.name = [NSString stringWithFormat:@"zone-setup-operation-%@", self.zoneName];
204 self.zoneCreated = zoneCreated;
205 self.zoneSubscribed = zoneSubscribed;
207 ckksnotice("ckkszone", self, "Setting up zone %@", self.zoneName);
211 // First, check the account status. If it's sufficient, add the necessary CloudKit operations to this operation
212 __weak CKKSGroupOperation* weakZoneSetupOperation = self.zoneSetupOperation;
213 [self.zoneSetupOperation runBeforeGroupFinished:[CKKSResultOperation named:[NSString stringWithFormat:@"zone-setup-%@", self.zoneName] withBlock:^{
215 __strong __typeof(self.zoneSetupOperation) zoneSetupOperation = weakZoneSetupOperation;
216 if(!self || !zoneSetupOperation) {
217 ckkserror("ckkszone", self, "received callback for released object");
221 if(self.accountStatus != CKKSAccountStatusAvailable) {
222 ckkserror("ckkszone", self, "Zone doesn't believe it's logged in; quitting setup");
226 NSBlockOperation* setupCompleteOperation = [NSBlockOperation blockOperationWithBlock:^{
229 secerror("ckkszone: received callback for released object");
233 ckksnotice("ckkszone", self, "%@: Setup complete", self.zoneName);
235 setupCompleteOperation.name = @"zone-setup-complete-operation";
237 // We have an account, so fetch the push environment and bring up APS
238 [self.container serverPreferredPushEnvironmentWithCompletionHandler: ^(NSString *apsPushEnvString, NSError *error) {
241 secerror("ckkszone: received callback for released object");
245 if(error || (apsPushEnvString == nil)) {
246 ckkserror("ckkszone", self, "Received error fetching preferred push environment (%@). Keychain syncing is highly degraded: %@", apsPushEnvString, error);
248 OctagonAPSReceiver* aps = [OctagonAPSReceiver receiverForEnvironment:apsPushEnvString
249 namedDelegatePort:SecCKKSAPSNamedPort
250 apsConnectionClass:self.cloudKitClassDependencies.apsConnectionClass];
251 [aps registerReceiver:self forZoneID:self.zoneID];
255 if(!zoneCreated || !zoneSubscribed) {
256 ckksnotice("ckkszone", self, "Asking to create and subscribe to CloudKit zone '%@'", self.zoneName);
257 CKKSZoneModifyOperations* zoneOps = [self.zoneModifier createZone:self.zone];
259 CKKSResultOperation* handleModificationsOperation = [CKKSResultOperation named:@"handle-modification" withBlock:^{
261 if([zoneOps.savedRecordZones containsObject:self.zone]) {
262 ckksnotice("ckkszone", self, "Successfully created '%@'", self.zoneName);
263 self.zoneCreated = true;
265 ckksnotice("ckkszone", self, "Failed to create '%@'", self.zoneName);
266 self.zoneCreatedError = zoneOps.zoneModificationOperation.error;
269 bool createdSubscription = false;
270 for(CKSubscription* subscription in zoneOps.savedSubscriptions) {
271 if([subscription.zoneID isEqual:self.zoneID]) {
272 createdSubscription = true;
277 if(createdSubscription) {
278 ckksnotice("ckkszone", self, "Successfully subscribed '%@'", self.zoneName);
279 self.zoneSubscribed = true;
281 ckksnotice("ckkszone", self, "Failed to subscribe to '%@'", self.zoneName);
282 self.zoneSubscribedError = zoneOps.zoneSubscriptionOperation.error;
285 [setupCompleteOperation addDependency:zoneOps.zoneModificationOperation];
286 [handleModificationsOperation addDependency:zoneOps.zoneModificationOperation];
287 [handleModificationsOperation addDependency:zoneOps.zoneSubscriptionOperation];
288 [zoneSetupOperation runBeforeGroupFinished:handleModificationsOperation];
290 ckksnotice("ckkszone", self, "no need to create or subscribe to the zone '%@'", self.zoneName);
293 [self.zoneSetupOperation runBeforeGroupFinished:setupCompleteOperation];
296 [self scheduleAccountStatusOperation:self.zoneSetupOperation];
297 return self.zoneSetupOperation;
301 - (CKKSResultOperation*)deleteCloudKitZoneOperation:(CKOperationGroup* _Nullable)ckoperationGroup {
302 if(!SecCKKSIsEnabled()) {
303 ckksnotice("ckkszone", self, "Skipping CloudKit reset due to disabled CKKS");
309 // We want to delete this zone and this subscription from CloudKit.
311 // Step 1: cancel setup operations (if they exist)
312 [self.accountLoggedInDependency cancel];
313 [self.zoneSetupOperation cancel];
314 [self.zoneCreationOperation cancel];
315 [self.zoneSubscriptionOperation cancel];
317 // Step 2: Try to delete the zone
319 CKKSZoneModifyOperations* zoneOps = [self.zoneModifier deleteZone:self.zoneID];
321 CKKSResultOperation* afterModification = [CKKSResultOperation named:@"after-modification" withBlockTakingSelf:^(CKKSResultOperation * _Nonnull op) {
324 bool fatalError = false;
326 NSError* operationError = zoneOps.zoneModificationOperation.error;
327 bool removed = [zoneOps.deletedRecordZoneIDs containsObject:self.zoneID];
329 if(!removed && operationError) {
330 // Okay, but if this error is either 'ZoneNotFound' or 'UserDeletedZone', that's fine by us: the zone is deleted.
331 NSDictionary* partialErrors = operationError.userInfo[CKPartialErrorsByItemIDKey];
332 if([operationError.domain isEqualToString:CKErrorDomain] && operationError.code == CKErrorPartialFailure && partialErrors) {
333 for(CKRecordZoneID* errorZoneID in partialErrors.allKeys) {
334 NSError* errorZone = partialErrors[errorZoneID];
336 if(errorZone && [errorZone.domain isEqualToString:CKErrorDomain] &&
337 (errorZone.code == CKErrorZoneNotFound || errorZone.code == CKErrorUserDeletedZone)) {
338 ckksnotice("ckkszone", self, "Attempted to delete zone %@, but it's already missing. This is okay: %@", errorZoneID, errorZone);
348 ckksnotice("ckkszone", self, "deletion of record zone %@ completed with error: %@", self.zoneID, operationError);
351 op.error = operationError;
354 ckksnotice("ckkszone", self, "deletion of record zone %@ completed successfully", self.zoneID);
358 [afterModification addDependency:zoneOps.zoneModificationOperation];
359 return afterModification;
362 - (void)notifyZoneChange: (CKRecordZoneNotification*) notification {
363 ckksnotice("ckkszone", self, "received a notification for CK zone change, ignoring");
366 - (void)handleCKLogin {
367 ckksinfo("ckkszone", self, "received a notification of CK login");
368 self.accountStatus = CKKSAccountStatusAvailable;
371 - (void)handleCKLogout {
372 ckksinfo("ckkszone", self, "received a notification of CK logout");
373 self.accountStatus = CKKSAccountStatusNoAccount;
377 - (bool)scheduleOperation: (NSOperation*) op {
379 ckkserror("ckkszone", self, "attempted to schedule an operation on a halted zone, ignoring");
383 [op addNullableDependency:self.accountLoggedInDependency];
385 [self.operationQueue addOperation: op];
389 - (void)cancelAllOperations {
390 [self.operationQueue cancelAllOperations];
393 - (void)waitUntilAllOperationsAreFinished {
394 [self.operationQueue waitUntilAllOperationsAreFinished];
397 - (void)waitForOperationsOfClass:(Class) operationClass {
398 NSArray* operations = [self.operationQueue.operations copy];
399 for(NSOperation* op in operations) {
400 if([op isKindOfClass:operationClass]) {
401 [op waitUntilFinished];
406 - (bool)scheduleAccountStatusOperation: (NSOperation*) op {
408 ckkserror("ckkszone", self, "attempted to schedule an account operation on a halted zone, ignoring");
412 // Always succeed. But, account status operations should always proceed in-order.
413 [op linearDependencies:self.accountOperations];
414 [self.operationQueue addOperation: op];
418 // to be used rarely, if at all
419 - (bool)scheduleOperationWithoutDependencies:(NSOperation*)op {
421 ckkserror("ckkszone", self, "attempted to schedule an non-dependent operation on a halted zone, ignoring");
425 [self.operationQueue addOperation: op];
429 - (void) dispatchSync: (bool (^)(void)) block {
430 // important enough to block this thread.
431 __block bool ok = false;
432 dispatch_sync(self.queue, ^{
434 ckkserror("ckkszone", self, "CKKSZone not dispatchSyncing a block (due to being halted)");
440 ckkserror("ckkszone", self, "CKKSZone block returned false");
446 // Synchronously set the 'halted' bit
447 dispatch_sync(self.queue, ^{
451 // Bring all operations down, too
452 [self cancelAllOperations];
454 // And now, wait for all operations that are running
455 for(NSOperation* op in self.operationQueue.operations) {
457 [op waitUntilFinished];