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