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