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 #import "keychain/ckks/CKKS.h"
25 #import "keychain/ckks/CKKSKeychainView.h"
26 #import "keychain/ckks/CKKSCurrentKeyPointer.h"
27 #import "keychain/ckks/CKKSKey.h"
28 #import "keychain/ckks/CKKSOutgoingQueueEntry.h"
29 #import "keychain/ckks/CKKSReencryptOutgoingItemsOperation.h"
30 #import "keychain/ckks/CKKSItemEncrypter.h"
31 #import "keychain/ckks/CloudKitCategories.h"
32 #import "keychain/ckks/CKKSAnalytics.h"
33 #import "keychain/categories/NSError+UsefulConstructors.h"
34 #import "keychain/analytics/CKKSPowerCollection.h"
38 // Note: reencryption is not, strictly speaking, a CloudKit operation. However, we preserve this to pass it back to the outgoing queue operation we'll create
39 @interface CKKSReencryptOutgoingItemsOperation ()
40 @property CKOperationGroup* ckoperationGroup;
43 @implementation CKKSReencryptOutgoingItemsOperation
45 - (instancetype)init {
48 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks ckoperationGroup:(CKOperationGroup*)ckoperationGroup {
49 if(self = [super init]) {
51 _ckoperationGroup = ckoperationGroup;
53 [self addNullableDependency:ckks.keyStateReadyDependency];
54 [self addNullableDependency:ckks.holdReencryptOutgoingItemsOperation];
56 // We also depend on the key hierarchy being reasonable
57 [self addNullableDependency:ckks.keyStateReadyDependency];
64 CKKSKeychainView* ckks = self.ckks;
66 ckkserror("ckksreencrypt", ckks, "no CKKS object");
70 [ckks dispatchSync: ^bool{
72 ckksnotice("ckksreencrypt", ckks, "CKKSReencryptOutgoingItemsOperation cancelled, quitting");
76 ckks.lastReencryptOutgoingItemsOperation = self;
79 bool newItems = false;
81 NSArray<CKKSOutgoingQueueEntry*>* oqes = [CKKSOutgoingQueueEntry allInState: SecCKKSStateReencrypt zoneID:ckks.zoneID error:&error];
83 ckkserror("ckksreencrypt", ckks, "Error fetching oqes from database: %@", error);
88 [CKKSPowerCollection CKKSPowerEvent:kCKKSPowerEventReencryptOutgoing zone:ckks.zoneName count:oqes.count];
90 for(CKKSOutgoingQueueEntry* oqe in oqes) {
91 // If there's already a 'new' item replacing this one, drop the reencryption on the floor
92 CKKSOutgoingQueueEntry* newOQE = [CKKSOutgoingQueueEntry tryFromDatabase:oqe.uuid state:SecCKKSStateNew zoneID:oqe.item.zoneID error:&error];
94 ckkserror("ckksreencrypt", ckks, "Couldn't load 'new' OQE to determine status: %@", error);
100 ckksnotice("ckksreencrypt", ckks, "Have a new OQE superceding %@ (%@), skipping", oqe, newOQE);
101 // Don't use the state transition here, either, since this item isn't really changing states
102 [oqe deleteFromDatabase:&error];
104 ckkserror("ckksreencrypt", ckks, "Couldn't delete reencrypting OQE(%@) from database: %@", oqe, error);
112 ckksnotice("ckksreencrypt", ckks, "Reencrypting item %@", oqe);
114 NSDictionary* item = [CKKSItemEncrypter decryptItemToDictionary: oqe.item error:&error];
116 if ([error.domain isEqualToString:@"securityd"] && error.code == errSecItemNotFound) {
117 ckkserror("ckksreencrypt", ckks, "Couldn't find key in keychain; attempting to poke key hierarchy: %@", error);
118 [ckks.pokeKeyStateMachineScheduler trigger];
120 ckkserror("ckksreencrypt", ckks, "Couldn't decrypt item %@: %@", oqe, error);
127 // Pick a key whose class matches the keyclass that this item
128 CKKSKey* originalKey = [CKKSKey fromDatabase: oqe.item.parentKeyUUID zoneID:ckks.zoneID error:&error];
130 ckkserror("ckksreencrypt", ckks, "Couldn't fetch key (%@) for item %@: %@", oqe.item.parentKeyUUID, oqe, error);
136 CKKSKey* newkey = [CKKSKey currentKeyForClass: originalKey.keyclass zoneID:ckks.zoneID error:&error];
137 [newkey ensureKeyLoaded: &error];
139 ckkserror("ckksreencrypt", ckks, "Couldn't fetch the current key for class %@: %@", originalKey.keyclass, error);
145 CKKSMirrorEntry* ckme = [CKKSMirrorEntry tryFromDatabase:oqe.item.uuid zoneID:ckks.zoneID error:&error];
147 ckkserror("ckksreencrypt", ckks, "Couldn't fetch ckme (%@) for item %@: %@", oqe.item.parentKeyUUID, oqe, error);
153 CKKSItem* encryptedItem = [CKKSItemEncrypter encryptCKKSItem:oqe.item
155 updatingCKKSItem:ckme.item
160 ckkserror("ckksreencrypt", ckks, "Couldn't encrypt under the new key %@: %@", newkey, error);
166 CKKSOutgoingQueueEntry* replacement = [[CKKSOutgoingQueueEntry alloc] initWithCKKSItem:encryptedItem
168 state:SecCKKSStateNew
170 accessGroup:oqe.accessgroup];
172 // Don't use the CKKSKeychainView state change here, since we're doing a wholesale item swap.
173 [oqe deleteFromDatabase:&error];
174 [replacement saveToDatabase:&error];
176 ckkserror("ckksreencrypt", ckks, "Couldn't save newly-encrypted oqe %@: %@", replacement, error);
185 CKKSAnalytics* logger = [CKKSAnalytics logger];
187 [logger logRecoverableError:error forEvent:CKKSEventProcessReencryption inView:ckks withAttributes:nil];
189 [logger logSuccessForEvent:CKKSEventProcessReencryption inView:ckks];
193 [ckks processOutgoingQueue:self.ckoperationGroup];