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 ()
42 @implementation CKKSReencryptOutgoingItemsOperation
43 @synthesize nextState = _nextState;
44 @synthesize intendedState = _intendedState;
46 - (instancetype)init {
49 - (instancetype)initWithDependencies:(CKKSOperationDependencies*)dependencies
50 ckks:(CKKSKeychainView*)ckks
51 intendedState:(OctagonState*)intendedState
52 errorState:(OctagonState*)errorState
54 if(self = [super init]) {
58 _nextState = errorState;
59 _intendedState = intendedState;
61 [self addNullableDependency:ckks.keyStateReadyDependency];
62 [self addNullableDependency:ckks.holdReencryptOutgoingItemsOperation];
64 // We also depend on the key hierarchy being reasonable
65 [self addNullableDependency:ckks.keyStateReadyDependency];
72 CKKSKeychainView* ckks = self.ckks;
74 [self.deps.databaseProvider dispatchSyncWithSQLTransaction:^CKKSDatabaseTransactionResult{
76 ckksnotice("ckksreencrypt", self.deps.zoneID, "CKKSReencryptOutgoingItemsOperation cancelled, quitting");
77 return CKKSDatabaseTransactionRollback;
80 ckks.lastReencryptOutgoingItemsOperation = self;
83 bool newItems = false;
85 NSArray<CKKSOutgoingQueueEntry*>* oqes = [CKKSOutgoingQueueEntry allInState:SecCKKSStateReencrypt zoneID:self.deps.zoneID error:&error];
87 ckkserror("ckksreencrypt", self.deps.zoneID, "Error fetching oqes from database: %@", error);
89 return CKKSDatabaseTransactionRollback;
92 [CKKSPowerCollection CKKSPowerEvent:kCKKSPowerEventReencryptOutgoing zone:self.deps.zoneID.zoneName count:oqes.count];
94 for(CKKSOutgoingQueueEntry* oqe in oqes) {
95 // If there's already a 'new' item replacing this one, drop the reencryption on the floor
96 CKKSOutgoingQueueEntry* newOQE = [CKKSOutgoingQueueEntry tryFromDatabase:oqe.uuid state:SecCKKSStateNew zoneID:oqe.item.zoneID error:&error];
98 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't load 'new' OQE to determine status: %@", error);
104 ckksnotice("ckksreencrypt", self.deps.zoneID, "Have a new OQE superceding %@ (%@), skipping", oqe, newOQE);
105 // Don't use the state transition here, either, since this item isn't really changing states
106 [oqe deleteFromDatabase:&error];
108 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't delete reencrypting OQE(%@) from database: %@", oqe, error);
116 ckksnotice("ckksreencrypt", self.deps.zoneID, "Reencrypting item %@", oqe);
118 NSDictionary* item = [CKKSItemEncrypter decryptItemToDictionary: oqe.item error:&error];
120 if ([error.domain isEqualToString:@"securityd"] && error.code == errSecItemNotFound) {
121 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't find key in keychain; asking for reset: %@", error);
122 [ckks _onqueuePokeKeyStateMachine];
123 self.nextState = SecCKKSZoneKeyStateUnhealthy;
125 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't decrypt item %@: %@", oqe, error);
132 // Pick a key whose class matches the keyclass that this item
133 CKKSKey* originalKey = [CKKSKey fromDatabase: oqe.item.parentKeyUUID zoneID:self.deps.zoneID error:&error];
135 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't fetch key (%@) for item %@: %@", oqe.item.parentKeyUUID, oqe, error);
141 CKKSKey* newkey = [CKKSKey currentKeyForClass: originalKey.keyclass zoneID:self.deps.zoneID error:&error];
142 [newkey ensureKeyLoaded: &error];
144 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't fetch the current key for class %@: %@", originalKey.keyclass, error);
150 CKKSMirrorEntry* ckme = [CKKSMirrorEntry tryFromDatabase:oqe.item.uuid zoneID:self.deps.zoneID error:&error];
152 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't fetch ckme (%@) for item %@: %@", oqe.item.parentKeyUUID, oqe, error);
158 CKKSItem* encryptedItem = [CKKSItemEncrypter encryptCKKSItem:oqe.item
160 updatingCKKSItem:ckme.item
165 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't encrypt under the new key %@: %@", newkey, error);
171 CKKSOutgoingQueueEntry* replacement = [[CKKSOutgoingQueueEntry alloc] initWithCKKSItem:encryptedItem
173 state:SecCKKSStateNew
175 accessGroup:oqe.accessgroup];
177 // Don't use the CKKSKeychainView state change here, since we're doing a wholesale item swap.
178 [oqe deleteFromDatabase:&error];
179 [replacement saveToDatabase:&error];
181 ckkserror("ckksreencrypt", self.deps.zoneID, "Couldn't save newly-encrypted oqe %@: %@", replacement, error);
190 CKKSAnalytics* logger = [CKKSAnalytics logger];
192 [logger logRecoverableError:error forEvent:CKKSEventProcessReencryption zoneName:self.deps.zoneID.zoneName withAttributes:nil];
194 [logger logSuccessForEvent:CKKSEventProcessReencryption zoneName:self.deps.zoneID.zoneName ];
198 [ckks processOutgoingQueue:self.deps.ckoperationGroup];
200 self.nextState = self.intendedState;