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@
26 #import "keychain/ckks/CKKSFixups.h"
27 #import "keychain/ckks/CloudKitCategories.h"
28 #import "keychain/ckks/CKKSCurrentItemPointer.h"
29 #import "keychain/ckks/CKKSZoneStateEntry.h"
30 #import "keychain/categories/NSError+UsefulConstructors.h"
32 @implementation CKKSFixups
33 +(CKKSGroupOperation*)fixup:(CKKSFixup)lastfixup for:(CKKSKeychainView*)keychainView
35 if(lastfixup == CKKSCurrentFixupNumber) {
39 CKOperationGroup* fixupCKGroup = [CKOperationGroup CKKSGroupWithName:@"fixup"];
40 CKKSGroupOperation* fixups = [[CKKSGroupOperation alloc] init];
41 fixups.name = @"ckks-fixups";
43 CKKSResultOperation* previousOp = keychainView.holdFixupOperation;
45 if(lastfixup < CKKSFixupRefetchCurrentItemPointers) {
46 CKKSResultOperation* refetch = [[CKKSFixupRefetchAllCurrentItemPointers alloc] initWithCKKSKeychainView:keychainView
47 ckoperationGroup:fixupCKGroup];
48 [refetch addNullableDependency:previousOp];
49 [fixups runBeforeGroupFinished:refetch];
53 if(lastfixup < CKKSFixupFetchTLKShares) {
54 CKKSResultOperation* fetchShares = [[CKKSFixupFetchAllTLKShares alloc] initWithCKKSKeychainView:keychainView
55 ckoperationGroup:fixupCKGroup];
56 [fetchShares addNullableSuccessDependency:previousOp];
57 [fixups runBeforeGroupFinished:fetchShares];
58 previousOp = fetchShares;
61 if(lastfixup < CKKSFixupLocalReload) {
62 CKKSResultOperation* localSync = [[CKKSFixupLocalReloadOperation alloc] initWithCKKSKeychainView:keychainView
63 ckoperationGroup:fixupCKGroup];
64 [localSync addNullableSuccessDependency:previousOp];
65 [fixups runBeforeGroupFinished:localSync];
66 previousOp = localSync;
74 #pragma mark - CKKSFixupRefetchAllCurrentItemPointers
76 @interface CKKSFixupRefetchAllCurrentItemPointers ()
77 @property CKOperationGroup* group;
80 // In <rdar://problem/34916549> CKKS: current item pointer CKRecord resurrection,
81 // We found that some devices could end up with multiple current item pointer records for a given record ID.
82 // This fixup will fetch all CKRecords matching any existing current item pointer records, and then trigger processing.
83 @implementation CKKSFixupRefetchAllCurrentItemPointers
84 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)keychainView
85 ckoperationGroup:(CKOperationGroup *)ckoperationGroup
87 if((self = [super init])) {
89 _group = ckoperationGroup;
94 - (NSString*)description {
95 return [NSString stringWithFormat:@"<CKKSFixup:RefetchAllCurrentItemPointers (%@)>", self.ckks];
98 CKKSKeychainView* ckks = self.ckks;
100 ckkserror("ckksfixup", ckks, "no CKKS object");
101 self.error = [NSError errorWithDomain:@"securityd" code:errSecInternalError userInfo:@{NSLocalizedDescriptionKey: @"no CKKS object"}];
105 [ckks dispatchSyncWithAccountKeys:^bool {
106 NSError* error = nil;
108 NSArray<CKKSCurrentItemPointer*>* cips = [CKKSCurrentItemPointer allInZone: ckks.zoneID error:&error];
110 ckkserror("ckksfixup", ckks, "Couldn't fetch current item pointers: %@", error);
114 NSMutableSet<CKRecordID*>* recordIDs = [NSMutableSet set];
115 for(CKKSCurrentItemPointer* cip in cips) {
116 CKRecordID* recordID = cip.storedCKRecord.recordID;
118 ckksnotice("ckksfixup", ckks, "Re-fetching %@ for %@", recordID, cip);
119 [recordIDs addObject:recordID];
121 ckkserror("ckksfixup", ckks, "No record ID for stored %@", cip);
125 if(recordIDs.count == 0) {
126 ckksnotice("ckksfixup", ckks, "No existing CIPs; fixup complete");
129 __weak __typeof(self) weakSelf = self;
130 NSBlockOperation* doneOp = [NSBlockOperation named:@"fetch-records-operation-complete" withBlock:^{}];
131 id<CKKSFetchRecordsOperation> fetch = [[ckks.fetchRecordsOperationClass alloc] initWithRecordIDs: [recordIDs allObjects]];
132 fetch.fetchRecordsCompletionBlock = ^(NSDictionary<CKRecordID *,CKRecord *> * _Nullable recordsByRecordID, NSError * _Nullable error) {
133 __strong __typeof(self) strongSelf = weakSelf;
134 CKKSKeychainView* strongCKKS = strongSelf.ckks;
136 [strongCKKS dispatchSync:^bool{
138 ckkserror("ckksfixup", strongCKKS, "Finished record fetch with error: %@", error);
140 NSDictionary<CKRecordID*,NSError*>* partialErrors = error.userInfo[CKPartialErrorsByItemIDKey];
141 if([error.domain isEqualToString:CKErrorDomain] && error.code == CKErrorPartialFailure && partialErrors) {
142 // Check if any of these records no longer exist on the server
143 for(CKRecordID* recordID in partialErrors.keyEnumerator) {
144 NSError* recordError = partialErrors[recordID];
145 if(recordError && [recordError.domain isEqualToString:CKErrorDomain] && recordError.code == CKErrorUnknownItem) {
146 ckkserror("ckksfixup", strongCKKS, "CloudKit believes %@ no longer exists", recordID);
147 [strongCKKS _onqueueCKRecordDeleted: recordID recordType:SecCKRecordCurrentItemType resync:true];
149 ckkserror("ckksfixup", strongCKKS, "Unknown error for %@: %@", recordID, error);
150 strongSelf.error = error;
154 strongSelf.error = error;
157 ckksnotice("ckksfixup", strongCKKS, "Finished record fetch successfully");
160 for(CKRecordID* recordID in recordsByRecordID) {
161 CKRecord* record = recordsByRecordID[recordID];
162 ckksnotice("ckksfixup", strongCKKS, "Recieved record %@", record);
163 [self.ckks _onqueueCKRecordChanged:record resync:true];
166 if(!strongSelf.error) {
167 // Now, update the zone state entry to be at this level
168 NSError* localerror = nil;
169 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry fromDatabase:strongCKKS.zoneName error:&localerror];
170 ckse.lastFixup = CKKSFixupRefetchCurrentItemPointers;
171 [ckse saveToDatabase:&localerror];
173 ckkserror("ckksfixup", strongCKKS, "Couldn't save CKKSZoneStateEntry(%@): %@", ckse, localerror);
175 ckksnotice("ckksfixup", strongCKKS, "Updated zone fixup state to CKKSFixupRefetchCurrentItemPointers");
179 [strongSelf runBeforeGroupFinished:doneOp];
183 [ckks.database addOperation: fetch];
184 [self dependOnBeforeGroupFinished:fetch];
185 [self dependOnBeforeGroupFinished:doneOp];
192 #pragma mark - CKKSFixupFetchAllTLKShares
194 @interface CKKSFixupFetchAllTLKShares ()
195 @property CKOperationGroup* group;
198 // In <rdar://problem/34901306> CKKSTLK: TLKShare CloudKit upload/download on TLK change, trust set addition
199 // We introduced TLKShare records.
200 // Older devices will throw them away, so on upgrade, they must refetch them
201 @implementation CKKSFixupFetchAllTLKShares
202 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)keychainView
203 ckoperationGroup:(CKOperationGroup *)ckoperationGroup
205 if((self = [super init])) {
206 _ckks = keychainView;
207 _group = ckoperationGroup;
212 - (NSString*)description {
213 return [NSString stringWithFormat:@"<CKKSFixup:FetchAllTLKShares (%@)>", self.ckks];
216 CKKSKeychainView* ckks = self.ckks;
218 ckkserror("ckksfixup", ckks, "no CKKS object");
219 self.error = [NSError errorWithDomain:@"securityd" code:errSecInternalError userInfo:@{NSLocalizedDescriptionKey: @"no CKKS object"}];
223 [ckks dispatchSyncWithAccountKeys:^bool {
224 __weak __typeof(self) weakSelf = self;
225 NSBlockOperation* doneOp = [NSBlockOperation named:@"fetch-records-operation-complete" withBlock:^{}];
227 NSPredicate *yes = [NSPredicate predicateWithValue:YES];
228 CKQuery *query = [[CKQuery alloc] initWithRecordType:SecCKRecordTLKShareType predicate:yes];
230 id<CKKSQueryOperation> fetch = [[ckks.queryOperationClass alloc] initWithQuery:query];
231 fetch.zoneID = ckks.zoneID;
232 fetch.desiredKeys = nil;
234 fetch.recordFetchedBlock = ^(CKRecord * _Nonnull record) {
235 __strong __typeof(self) strongSelf = weakSelf;
236 CKKSKeychainView* strongCKKS = strongSelf.ckks;
237 [strongCKKS dispatchSync:^bool{
238 ckksnotice("ckksfixup", strongCKKS, "Recieved tlk share record from query: %@", record);
240 [strongCKKS _onqueueCKRecordChanged:record resync:true];
245 fetch.queryCompletionBlock = ^(CKQueryCursor * _Nullable cursor, NSError * _Nullable error) {
246 __strong __typeof(self) strongSelf = weakSelf;
247 CKKSKeychainView* strongCKKS = strongSelf.ckks;
249 [strongCKKS dispatchSync:^bool{
251 ckkserror("ckksfixup", strongCKKS, "Couldn't fetch all TLKShare records: %@", error);
252 strongSelf.error = error;
256 ckksnotice("ckksfixup", strongCKKS, "Successfully fetched TLKShare records (%@)", cursor);
258 NSError* localerror = nil;
259 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry fromDatabase:strongCKKS.zoneName error:&localerror];
260 ckse.lastFixup = CKKSFixupFetchTLKShares;
261 [ckse saveToDatabase:&localerror];
263 ckkserror("ckksfixup", strongCKKS, "Couldn't save CKKSZoneStateEntry(%@): %@", ckse, localerror);
265 ckksnotice("ckksfixup", strongCKKS, "Updated zone fixup state to CKKSFixupFetchTLKShares");
269 [strongSelf runBeforeGroupFinished:doneOp];
272 [ckks.database addOperation: fetch];
273 [self dependOnBeforeGroupFinished:fetch];
274 [self dependOnBeforeGroupFinished:doneOp];
281 #pragma mark - CKKSFixupLocalReloadOperation
283 @interface CKKSFixupLocalReloadOperation ()
284 @property CKOperationGroup* group;
287 // In <rdar://problem/35540228> Server Generated CloudKit "Manatee Identity Lost"
288 // items could be deleted from the local keychain after CKKS believed they were already synced, and therefore wouldn't resync
289 // Perform a local resync operation
290 @implementation CKKSFixupLocalReloadOperation
291 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)keychainView
292 ckoperationGroup:(CKOperationGroup *)ckoperationGroup
294 if((self = [super init])) {
295 _ckks = keychainView;
296 _group = ckoperationGroup;
301 - (NSString*)description {
302 return [NSString stringWithFormat:@"<CKKSFixup:LocalReload (%@)>", self.ckks];
305 CKKSKeychainView* ckks = self.ckks;
306 __weak __typeof(self) weakSelf = self;
308 ckkserror("ckksfixup", ckks, "no CKKS object");
309 self.error = [NSError errorWithDomain:CKKSErrorDomain code:errSecInternalError userInfo:@{NSLocalizedDescriptionKey: @"no CKKS object"}];
313 CKKSResultOperation* reload = [[CKKSReloadAllItemsOperation alloc] initWithCKKSKeychainView:ckks];
314 [self runBeforeGroupFinished:reload];
316 CKKSResultOperation* cleanup = [CKKSResultOperation named:@"local-reload-cleanup" withBlock:^{
317 __strong __typeof(self) strongSelf = weakSelf;
318 __strong __typeof(self.ckks) strongCKKS = strongSelf.ckks;
319 [strongCKKS dispatchSync:^bool{
321 ckkserror("ckksfixup", strongCKKS, "Couldn't perform a reload: %@", reload.error);
322 strongSelf.error = reload.error;
326 ckksnotice("ckksfixup", strongCKKS, "Successfully performed a reload fixup");
328 NSError* localerror = nil;
329 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry fromDatabase:strongCKKS.zoneName error:&localerror];
330 ckse.lastFixup = CKKSFixupLocalReload;
331 [ckse saveToDatabase:&localerror];
333 ckkserror("ckksfixup", strongCKKS, "Couldn't save CKKSZoneStateEntry(%@): %@", ckse, localerror);
335 ckksnotice("ckksfixup", strongCKKS, "Updated zone fixup state to CKKSFixupLocalReload");
340 [cleanup addNullableDependency:reload];
341 [self runBeforeGroupFinished:cleanup];