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 #include <AssertMacros.h>
26 #import <Foundation/Foundation.h>
28 #import "CKKSKeychainView.h"
30 #include <Security/SecItemPriv.h>
32 #include <utilities/SecDb.h>
33 #include "keychain/securityd/SecDbItem.h"
34 #include "keychain/securityd/SecItemSchema.h"
38 #import <CloudKit/CloudKit.h>
39 #import "CKKSOutgoingQueueEntry.h"
40 #import "CKKSItemEncrypter.h"
42 #import "keychain/ckks/CloudKitCategories.h"
43 #import "keychain/categories/NSError+UsefulConstructors.h"
46 @implementation CKKSOutgoingQueueEntry
48 - (NSString*)description {
49 return [NSString stringWithFormat: @"<%@(%@): %@ %@ (%@)>",
50 NSStringFromClass([self class]),
51 self.item.zoneID.zoneName,
57 - (instancetype) initWithCKKSItem:(CKKSItem*) item
58 action: (NSString*) action
59 state: (NSString*) state
60 waitUntil: (NSDate*) waitUntil
61 accessGroup: (NSString*) accessgroup
63 if((self = [super init])) {
67 _accessgroup = accessgroup;
68 _waitUntil = waitUntil;
74 - (BOOL)isEqual: (id) object {
75 if(![object isKindOfClass:[CKKSOutgoingQueueEntry class]]) {
79 CKKSOutgoingQueueEntry* obj = (CKKSOutgoingQueueEntry*) object;
81 return ([self.item isEqual: obj.item] &&
82 [self.action isEqual: obj.action] &&
83 [self.state isEqual: obj.state] &&
84 ((self.waitUntil == nil && obj.waitUntil == nil) || (fabs([self.waitUntil timeIntervalSinceDate: obj.waitUntil]) < 1)) &&
85 [self.accessgroup isEqual: obj.accessgroup] &&
90 + (CKKSKey*)keyForItem:(SecDbItemRef)item zoneID:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error
93 ckkserror("ckks-key", zoneID, "Cannot select a key for no item!");
95 *error = [NSError errorWithDomain:CKKSErrorDomain
96 code:CKKSErrorUnexpectedNil
97 description:@"can't pick a key class for an empty item"];
102 CKKSKeyClass* class = nil;
104 NSString* protection = (__bridge NSString*)SecDbItemGetCachedValueWithName(item, kSecAttrAccessible);
105 if([protection isEqualToString: (__bridge NSString*)kSecAttrAccessibleWhenUnlocked]) {
106 class = SecCKKSKeyClassA;
107 } else if([protection isEqualToString: (__bridge NSString*)kSecAttrAccessibleAlwaysPrivate] ||
108 [protection isEqualToString: (__bridge NSString*)kSecAttrAccessibleAfterFirstUnlock]) {
109 class = SecCKKSKeyClassC;
111 NSError* localError = [NSError errorWithDomain:CKKSErrorDomain
112 code:CKKSInvalidKeyClass
113 description:[NSString stringWithFormat:@"can't pick key class for protection %@", protection]];
114 ckkserror("ckks-key", zoneID, "can't pick key class: %@ %@", localError, item);
122 NSError* currentKeyError = nil;
123 CKKSKey* key = [CKKSKey currentKeyForClass:class zoneID:zoneID error:¤tKeyError];
124 if(!key || currentKeyError) {
125 ckkserror("ckks-key", zoneID, "Couldn't find current key for %@: %@", class, currentKeyError);
128 *error = currentKeyError;
133 // and make sure it's unwrapped.
134 NSError* loadedError = nil;
135 if(![key ensureKeyLoaded:&loadedError]) {
136 ckkserror("ckks-key", zoneID, "Couldn't load key(%@): %@", key, loadedError);
138 *error = loadedError;
146 + (instancetype)withItem:(SecDbItemRef)item
147 action:(NSString*)action
148 zoneID:(CKRecordZoneID*)zoneID
149 error:(NSError * __autoreleasing *)error
151 CFErrorRef cferror = NULL;
153 NSString* uuid = nil;
154 NSString* accessgroup = nil;
156 NSInteger newGenerationCount = -1;
158 NSMutableDictionary* objd = nil;
160 ckkserror("ckksitem", zoneID, "Creating a (%@) outgoing queue entry for: %@", action, item);
162 NSError* keyError = nil;
163 key = [self keyForItem:item
166 if(!key || keyError) {
167 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:keyError.code description:@"No key for item" underlying:keyError];
168 ckkserror("ckksitem", zoneID, "no key for item: %@ %@", localerror, item);
175 objd = (__bridge_transfer NSMutableDictionary*) SecDbItemCopyPListWithMask(item, kSecDbSyncFlag, &cferror);
177 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:CFErrorGetCode(cferror) description:@"Couldn't create object plist" underlying:(__bridge_transfer NSError*)cferror];
178 ckkserror("ckksitem", zoneID, "no plist: %@ %@", localerror, item);
185 // Object classes aren't in the item plist, set them specifically
186 [objd setObject: (__bridge NSString*) item->class->name forKey: (__bridge NSString*) kSecClass];
188 uuid = (__bridge_transfer NSString*) CFRetainSafe(SecDbItemGetValue(item, &v10itemuuid, &cferror));
189 if(!uuid || cferror) {
190 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:CKKSNoUUIDOnItem description:@"No UUID for item" underlying:(__bridge_transfer NSError*)cferror];
191 ckkserror("ckksitem", zoneID, "No UUID for item: %@ %@", localerror, item);
197 if([uuid isKindOfClass:[NSNull class]]) {
198 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:CKKSNoUUIDOnItem description:@"UUID not found in object" underlying:nil];
199 ckkserror("ckksitem", zoneID, "couldn't fetch UUID: %@ %@", localerror, item);
206 accessgroup = (__bridge_transfer NSString*) CFRetainSafe(SecDbItemGetValue(item, &v6agrp, &cferror));
207 if(!accessgroup || cferror) {
208 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:CFErrorGetCode(cferror) description:@"accessgroup not found in object" underlying:(__bridge_transfer NSError*)cferror];
209 ckkserror("ckksitem", zoneID, "couldn't fetch access group from item: %@ %@", localerror, item);
215 if([accessgroup isKindOfClass:[NSNull class]]) {
216 // That's okay; this is only used for rate limiting.
217 ckkserror("ckksitem", zoneID, "couldn't fetch accessgroup: %@", item);
218 accessgroup = @"no-group";
221 CKKSMirrorEntry* ckme = [CKKSMirrorEntry tryFromDatabase:uuid zoneID:zoneID error:error];
223 // The action this change should be depends on any existing pending action, if any
224 // Particularly, we need to coalesce (existing action, new action) to:
225 // (add, modify) => add
226 // (add, delete) => no-op
227 // (delete, add) => modify
228 NSString* actualAction = action;
230 NSError* fetchError = nil;
231 CKKSOutgoingQueueEntry* existingOQE = [CKKSOutgoingQueueEntry tryFromDatabase:uuid state:SecCKKSStateNew zoneID:zoneID error:&fetchError];
233 if([existingOQE.action isEqual: SecCKKSActionAdd]) {
234 if([action isEqual:SecCKKSActionModify]) {
235 actualAction = SecCKKSActionAdd;
236 } else if([action isEqual:SecCKKSActionDelete]) {
237 // we're deleting an add. If there's a ckme, keep as a delete
239 // Otherwise, remove from outgoingqueue and don't make a new OQE.
240 [existingOQE deleteFromDatabase:error];
246 if([existingOQE.action isEqual: SecCKKSActionDelete] && [action isEqual:SecCKKSActionAdd]) {
247 actualAction = SecCKKSActionModify;
249 } else if(fetchError) {
250 ckkserror("ckksitem", zoneID, "Unable to fetch an existing OQE due to error: %@", fetchError);
254 if(!ckme && [action isEqualToString:SecCKKSActionDelete]) {
255 CKKSOutgoingQueueEntry* anyExistingOQE = [CKKSOutgoingQueueEntry tryFromDatabase:uuid zoneID:zoneID error:&fetchError];
258 ckkserror("ckksitem", zoneID, "Unable to fetch an existing OQE (any state) due to error: %@", fetchError);
259 } else if(!anyExistingOQE) {
260 // This is a delete for an item which doesn't exist. Therefore, this is a no-op.
261 ckkserror("ckksitem", zoneID, "Asked to delete a record for which we don't have a CKME or any OQE, ignoring: %@", uuid);
267 newGenerationCount = ckme ? ckme.item.generationCount : (NSInteger) 0; // TODO: this is wrong
269 // Is this modification just changing the mdat? As a performance improvement, don't update the item in CK
270 if(ckme && !existingOQE && [actualAction isEqualToString:SecCKKSActionModify]) {
271 NSError* ckmeError = nil;
272 NSMutableDictionary* mirror = [[CKKSItemEncrypter decryptItemToDictionary:ckme.item error:&ckmeError] mutableCopy];
273 NSMutableDictionary* objdCopy = [objd mutableCopy];
276 ckkserror("ckksitem", zoneID, "Unable to decrypt current CKME: %@", ckmeError);
278 mirror[(__bridge id)kSecAttrModificationDate] = nil;
279 mirror[(__bridge id)kSecAttrSHA1] = nil;
280 objdCopy[(__bridge id)kSecAttrModificationDate] = nil;
281 objdCopy[(__bridge id)kSecAttrSHA1] = nil;
283 if([mirror isEqualToDictionary:objdCopy]) {
284 ckksnotice("ckksitem", zoneID, "Update to item only changes mdat; skipping %@", uuid);
290 // Pull out any unencrypted fields
291 NSNumber* pcsServiceIdentifier = objd[(id)kSecAttrPCSPlaintextServiceIdentifier];
292 objd[(id)kSecAttrPCSPlaintextServiceIdentifier] = nil;
294 NSData* pcsPublicKey = objd[(id)kSecAttrPCSPlaintextPublicKey];
295 objd[(id)kSecAttrPCSPlaintextPublicKey] = nil;
297 NSData* pcsPublicIdentity = objd[(id)kSecAttrPCSPlaintextPublicIdentity];
298 objd[(id)kSecAttrPCSPlaintextPublicIdentity] = nil;
300 CKKSItem* baseitem = [[CKKSItem alloc] initWithUUID:uuid
301 parentKeyUUID:key.uuid
306 generationCount:newGenerationCount
307 encver:currentCKKSItemEncryptionVersion
308 plaintextPCSServiceIdentifier:pcsServiceIdentifier
309 plaintextPCSPublicKey:pcsPublicKey
310 plaintextPCSPublicIdentity:pcsPublicIdentity];
313 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:CKKSItemCreationFailure description:@"Couldn't create an item" underlying:nil];
314 ckkserror("ckksitem", zoneID, "couldn't create an item: %@ %@", localerror, item);
321 NSError* encryptionError = nil;
322 CKKSItem* encryptedItem = [CKKSItemEncrypter encryptCKKSItem:baseitem
324 updatingCKKSItem:ckme.item
326 error:&encryptionError];
328 if(!encryptedItem || encryptionError) {
329 NSError* localerror = [NSError errorWithDomain:CKKSErrorDomain code:encryptionError.code description:@"Couldn't encrypt item" underlying:encryptionError];
330 ckkserror("ckksitem", zoneID, "couldn't encrypt item: %@ %@", localerror, item);
337 return [[CKKSOutgoingQueueEntry alloc] initWithCKKSItem:encryptedItem
339 state:SecCKKSStateNew
341 accessGroup:accessgroup];
344 #pragma mark - Property access to underlying CKKSItem
347 return self.item.uuid;
350 -(void)setUuid:(NSString *)uuid {
351 self.item.uuid = uuid;
354 #pragma mark - Database Operations
356 + (instancetype) fromDatabase: (NSString*) uuid state: (NSString*) state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
357 return [self fromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"state": CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
360 + (instancetype) tryFromDatabase: (NSString*) uuid zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
361 return [self tryFromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
364 + (instancetype) tryFromDatabase: (NSString*) uuid state: (NSString*) state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
365 return [self tryFromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"state":CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
368 + (NSArray<CKKSOutgoingQueueEntry*>*) fetch:(ssize_t) n state: (NSString*) state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
369 return [self fetch:n where: @{@"state":CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error:error];
372 + (NSArray<CKKSOutgoingQueueEntry*>*) allInState: (NSString*) state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
373 return [self allWhere: @{@"state":CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error:error];
376 + (NSArray<CKKSOutgoingQueueEntry*>*)allWithUUID:(NSString*)uuid states:(NSArray<NSString*>*)states zoneID:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error
378 return [self allWhere:@{@"UUID": CKKSNilToNSNull(uuid),
379 @"state": [[CKKSSQLWhereIn alloc] initWithValues:states],
380 @"ckzone":CKKSNilToNSNull(zoneID.zoneName)}
385 #pragma mark - CKKSSQLDatabaseObject methods
387 + (NSString*)sqlTable {
388 return @"outgoingqueue";
391 + (NSArray<NSString*>*)sqlColumns {
392 return [[CKKSItem sqlColumns] arrayByAddingObjectsFromArray: @[@"action", @"state", @"waituntil", @"accessgroup"]];
395 - (NSDictionary<NSString*,NSString*>*)whereClauseToFindSelf {
396 return @{@"UUID": self.uuid, @"state": self.state, @"ckzone":self.item.zoneID.zoneName};
399 - (NSDictionary<NSString*,NSString*>*)sqlValues {
400 NSISO8601DateFormatter* dateFormat = [[NSISO8601DateFormatter alloc] init];
402 NSMutableDictionary* values = [[self.item sqlValues] mutableCopy];
403 values[@"action"] = self.action;
404 values[@"state"] = self.state;
405 values[@"waituntil"] = CKKSNilToNSNull(self.waitUntil ? [dateFormat stringFromDate: self.waitUntil] : nil);
406 values[@"accessgroup"] = self.accessgroup;
412 + (instancetype)fromDatabaseRow:(NSDictionary<NSString *, CKKSSQLResult*>*) row {
413 return [[CKKSOutgoingQueueEntry alloc] initWithCKKSItem:[CKKSItem fromDatabaseRow: row]
414 action:row[@"action"].asString
415 state:row[@"state"].asString
416 waitUntil:row[@"waituntil"].asISO8601Date
417 accessGroup:row[@"accessgroup"].asString];
420 + (NSDictionary<NSString*,NSNumber*>*)countsByStateInZone:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
421 NSMutableDictionary* results = [[NSMutableDictionary alloc] init];
423 [CKKSSQLDatabaseObject queryDatabaseTable: [[self class] sqlTable]
424 where: @{@"ckzone": CKKSNilToNSNull(zoneID.zoneName)}
425 columns: @[@"state", @"count(rowid)"]
429 processRow: ^(NSDictionary<NSString*, CKKSSQLResult*>* row) {
430 results[row[@"state"].asString] = row[@"count(rowid)"].asNSNumberInteger;
436 + (NSInteger)countByState:(CKKSItemState *)state zone:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
437 __block NSInteger result = -1;
439 [CKKSSQLDatabaseObject queryDatabaseTable: [[self class] sqlTable]
440 where: @{@"ckzone": CKKSNilToNSNull(zoneID.zoneName), @"state": state }
441 columns: @[@"count(*)"]
445 processRow: ^(NSDictionary<NSString*, CKKSSQLResult*>* row) {
446 result = row[@"count(*)"].asNSInteger;