]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSSynchronizeOperation.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ckks / CKKSSynchronizeOperation.m
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "CKKSKeychainView.h"
25 #import "CKKSGroupOperation.h"
26 #import "CKKSSynchronizeOperation.h"
27 #import "CKKSFetchAllRecordZoneChangesOperation.h"
28 #import "CKKSScanLocalItemsOperation.h"
29 #import "keychain/ckks/CloudKitCategories.h"
30 #import "keychain/categories/NSError+UsefulConstructors.h"
31 #import "keychain/ot/ObjCImprovements.h"
32
33 #if OCTAGON
34
35 @interface CKKSSynchronizeOperation ()
36 @property int32_t restartCount;
37 @end
38
39 @implementation CKKSSynchronizeOperation
40
41 - (instancetype)init {
42 return nil;
43 }
44 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks {
45 if(self = [super init]) {
46 _ckks = ckks;
47 _restartCount = 0;
48 }
49 return self;
50 }
51
52 - (void)groupStart {
53 WEAKIFY(self);
54
55 /*
56 * Synchronizations (or resynchronizations) are complicated beasts. We will:
57 *
58 * 1. Finish processing the outgoing queue. You can't be in-sync with cloudkit if you have an update that hasn't propagated.
59 * 2. Kick off a normal CloudKit fetch.
60 * 3. Process the incoming queue as normal.
61 * (Note that this requires the keybag to be unlocked.)
62 *
63 * So far, this is normal operation. Now:
64 *
65 * 4. Start another CloudKit fetch, giving it the nil change tag. This fetches all objects in CloudKit.
66 * 4a. Compare those objects against our local mirror. If any discrepancies, this is a bug.
67 * 4b. All conflicts: CloudKit data wins.
68 * 4c. All items we have that CloudKit doesn't: delete locally.
69 *
70 * 5. Process the incoming queue again. This should be empty.
71 * 6. Scan the local keychain for items which exist locally but are not in CloudKit. Upload them.
72 * 7. If there are any such items in 6, restart the sync.
73 */
74
75 // Promote to strong reference
76 CKKSKeychainView* ckks = self.ckks;
77
78 // Synchronous, on some thread. Get back on the CKKS queue for SQL thread-safety.
79 [ckks dispatchSync: ^bool{
80 if(self.cancelled) {
81 ckksnotice("ckksresync", ckks, "CKKSSynchronizeOperation cancelled, quitting");
82 return false;
83 }
84
85 ckks.lastSynchronizeOperation = self;
86
87 uint32_t steps = 5;
88
89 ckksnotice("ckksresync", ckks, "Beginning resynchronize (attempt %u)", self.restartCount);
90
91 CKOperationGroup* operationGroup = [CKOperationGroup CKKSGroupWithName:@"ckks-resync"];
92
93 // Step 1
94 CKKSFetchAllRecordZoneChangesOperation* fetchOp = [[CKKSFetchAllRecordZoneChangesOperation alloc] initWithContainer:ckks.container
95 fetchClass:ckks.cloudKitClassDependencies.fetchRecordZoneChangesOperationClass
96 clients:@[ckks]
97 fetchReasons:[NSSet setWithObject:CKKSFetchBecauseResync]
98 apnsPushes:nil
99 forceResync:true
100 ckoperationGroup:operationGroup];
101 fetchOp.name = [NSString stringWithFormat: @"resync-step%u-fetch", self.restartCount * steps + 1];
102 [self runBeforeGroupFinished: fetchOp];
103
104 // Step 2
105 CKKSIncomingQueueOperation* incomingOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
106 incomingOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 2];
107 [incomingOp addSuccessDependency:fetchOp];
108 [self runBeforeGroupFinished:incomingOp];
109
110 // Step 3
111 CKKSScanLocalItemsOperation* scan = [[CKKSScanLocalItemsOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
112 scan.name = [NSString stringWithFormat: @"resync-step%u-scan", self.restartCount * steps + 3];
113 [scan addSuccessDependency: incomingOp];
114 [self runBeforeGroupFinished: scan];
115
116 // Step 4
117 CKKSOutgoingQueueOperation* outgoingOp = [ckks processOutgoingQueue: operationGroup];
118 outgoingOp.name = [NSString stringWithFormat: @"resync-step%u-outgoing", self.restartCount * steps + 4];
119 [self dependOnBeforeGroupFinished:outgoingOp];
120
121 // Step 5:
122 CKKSResultOperation* restart = [[CKKSResultOperation alloc] init];
123 restart.name = [NSString stringWithFormat: @"resync-step%u-consider-restart", self.restartCount * steps + 5];
124 [restart addExecutionBlock:^{
125 STRONGIFY(self);
126 if(!self) {
127 secerror("ckksresync: received callback for released object");
128 return;
129 }
130
131 if(scan.recordsFound > 0) {
132 if(self.restartCount >= 3) {
133 // we've restarted too many times. Fail and stop.
134 ckkserror("ckksresync", ckks, "restarted synchronization too often; Failing");
135 self.error = [NSError errorWithDomain:@"securityd"
136 code:2
137 userInfo:@{NSLocalizedDescriptionKey: @"resynchronization restarted too many times; churn in database?"}];
138 } else {
139 // restart the sync operation.
140 self.restartCount += 1;
141 ckkserror("ckksresync", ckks, "restarting synchronization operation due to new local items");
142 [self groupStart];
143 }
144 }
145 }];
146
147 [restart addSuccessDependency: outgoingOp];
148 [self runBeforeGroupFinished: restart];
149
150 return true;
151 }];
152 }
153
154 @end;
155
156 #endif