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@
24 #import "CKKSCurrentKeyPointer.h"
28 #import "keychain/categories/NSError+UsefulConstructors.h"
30 @implementation CKKSCurrentKeyPointer
32 - (instancetype)initForClass:(CKKSKeyClass*)keyclass
33 currentKeyUUID:(NSString*)currentKeyUUID
34 zoneID:(CKRecordZoneID*)zoneID
35 encodedCKRecord: (NSData*) encodedrecord
37 if(self = [super initWithCKRecordType: SecCKRecordCurrentKeyType encodedCKRecord:encodedrecord zoneID:zoneID]) {
39 _currentKeyUUID = currentKeyUUID;
41 if(self.currentKeyUUID == nil) {
42 ckkserror_global("currentkey", "created a CKKSCurrentKey with a nil currentKeyUUID. Why?");
48 - (NSString*)description {
49 return [NSString stringWithFormat:@"<CKKSCurrentKeyPointer(%@) %@: %@>", self.zoneID.zoneName, self.keyclass, self.currentKeyUUID];
52 - (instancetype)copyWithZone:(NSZone*)zone {
53 CKKSCurrentKeyPointer* copy = [super copyWithZone:zone];
54 copy.keyclass = [self.keyclass copyWithZone:zone];
55 copy.currentKeyUUID = [self.currentKeyUUID copyWithZone:zone];
58 - (BOOL)isEqual: (id) object {
59 if(![object isKindOfClass:[CKKSCurrentKeyPointer class]]) {
63 CKKSCurrentKeyPointer* obj = (CKKSCurrentKeyPointer*) object;
65 return ([self.zoneID isEqual: obj.zoneID] &&
66 ((self.currentKeyUUID == nil && obj.currentKeyUUID == nil) || [self.currentKeyUUID isEqual: obj.currentKeyUUID]) &&
67 ((self.keyclass == nil && obj.keyclass == nil) || [self.keyclass isEqual:obj.keyclass]) &&
71 #pragma mark - CKKSCKRecordHolder methods
73 - (NSString*) CKRecordName {
77 - (CKRecord*) updateCKRecord: (CKRecord*) record zoneID: (CKRecordZoneID*) zoneID {
78 if(![record.recordType isEqualToString: SecCKRecordCurrentKeyType]) {
80 exceptionWithName:@"WrongCKRecordTypeException"
81 reason:[NSString stringWithFormat: @"CKRecordType (%@) was not %@", record.recordType, SecCKRecordCurrentKeyType]
85 // The record name should already match keyclass...
86 if(![record.recordID.recordName isEqualToString: self.keyclass]) {
88 exceptionWithName:@"WrongCKRecordNameException"
89 reason:[NSString stringWithFormat: @"CKRecord name (%@) was not %@", record.recordID.recordName, self.keyclass]
93 // Set the parent reference
94 record[SecCKRecordParentKeyRefKey] = [[CKReference alloc] initWithRecordID: [[CKRecordID alloc] initWithRecordName: self.currentKeyUUID zoneID: zoneID] action: CKReferenceActionNone];
98 - (bool) matchesCKRecord: (CKRecord*) record {
99 if(![record.recordType isEqualToString: SecCKRecordCurrentKeyType]) {
103 if(![record.recordID.recordName isEqualToString: self.keyclass]) {
107 if(![[record[SecCKRecordParentKeyRefKey] recordID].recordName isEqualToString: self.currentKeyUUID]) {
114 - (void) setFromCKRecord: (CKRecord*) record {
115 if(![record.recordType isEqualToString: SecCKRecordCurrentKeyType]) {
117 exceptionWithName:@"WrongCKRecordTypeException"
118 reason:[NSString stringWithFormat: @"CKRecordType (%@) was not %@", record.recordType, SecCKRecordCurrentKeyType]
122 [self setStoredCKRecord:record];
124 // TODO: verify this is a real keyclass
125 self.keyclass = (CKKSKeyClass*) record.recordID.recordName;
126 self.currentKeyUUID = [record[SecCKRecordParentKeyRefKey] recordID].recordName;
128 if(self.currentKeyUUID == nil) {
129 ckkserror_global("currentkey", "No current key UUID in record! How/why? %@", record);
133 #pragma mark - Load from database
135 + (instancetype) fromDatabase: (CKKSKeyClass*) keyclass zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
136 return [self fromDatabaseWhere: @{@"keyclass": keyclass, @"ckzone":zoneID.zoneName} error: error];
139 + (instancetype) tryFromDatabase: (CKKSKeyClass*) keyclass zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
140 return [self tryFromDatabaseWhere: @{@"keyclass": keyclass, @"ckzone":zoneID.zoneName} error: error];
143 + (instancetype) forKeyClass: (CKKSKeyClass*) keyclass withKeyUUID: (NSString*) keyUUID zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
144 NSError* localerror = nil;
145 CKKSCurrentKeyPointer* current = [self tryFromDatabase: keyclass zoneID:zoneID error: &localerror];
154 current.currentKeyUUID = keyUUID;
158 return [[CKKSCurrentKeyPointer alloc] initForClass: keyclass currentKeyUUID: keyUUID zoneID:zoneID encodedCKRecord:nil];
161 + (NSArray<CKKSCurrentKeyPointer*>*)all:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
162 return [self allWhere:@{@"ckzone":zoneID.zoneName} error:error];
165 + (bool) deleteAll:(CKRecordZoneID*) zoneID error: (NSError * __autoreleasing *) error {
166 bool ok = [CKKSSQLDatabaseObject deleteFromTable:[self sqlTable] where: @{@"ckzone":zoneID.zoneName} connection:nil error: error];
169 secdebug("ckksitem", "Deleted all %@", self);
171 secdebug("ckksitem", "Couldn't delete all %@: %@", self, error ? *error : @"unknown");
176 #pragma mark - CKKSSQLDatabaseObject methods
178 + (NSString*) sqlTable {
179 return @"currentkeys";
182 + (NSArray<NSString*>*) sqlColumns {
183 return @[@"keyclass", @"currentKeyUUID", @"ckzone", @"ckrecord"];
186 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
187 return @{@"keyclass": self.keyclass, @"ckzone":self.zoneID.zoneName};
190 - (NSDictionary<NSString*,NSString*>*) sqlValues {
191 return @{@"keyclass": self.keyclass,
192 @"currentKeyUUID": CKKSNilToNSNull(self.currentKeyUUID),
193 @"ckzone": CKKSNilToNSNull(self.zoneID.zoneName),
194 @"ckrecord": CKKSNilToNSNull([self.encodedCKRecord base64EncodedStringWithOptions:0]),
198 + (instancetype)fromDatabaseRow:(NSDictionary<NSString*, CKKSSQLResult*>*)row {
199 return [[CKKSCurrentKeyPointer alloc] initForClass:(CKKSKeyClass*)row[@"keyclass"].asString
200 currentKeyUUID:row[@"currentKeyUUID"].asString
201 zoneID:[[CKRecordZoneID alloc] initWithZoneName:row[@"ckzone"].asString ownerName:CKCurrentUserDefaultName]
202 encodedCKRecord:row[@"ckrecord"].asBase64DecodedData];
207 @implementation CKKSCurrentKeySet
208 -(instancetype)initForZoneName:(NSString*)zoneName {
209 if((self = [super init])) {
210 _viewName = zoneName;
216 + (CKKSCurrentKeySet*)loadForZone:(CKRecordZoneID*)zoneID
218 CKKSCurrentKeySet* set = [[CKKSCurrentKeySet alloc] initForZoneName:zoneID.zoneName];
219 NSError* error = nil;
221 set.currentTLKPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassTLK zoneID:zoneID error:&error];
222 set.currentClassAPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassA zoneID:zoneID error:&error];
223 set.currentClassCPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassC zoneID:zoneID error:&error];
225 set.tlk = set.currentTLKPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:set.currentTLKPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
226 set.classA = set.currentClassAPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:set.currentClassAPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
227 set.classC = set.currentClassCPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:set.currentClassCPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
229 set.tlkShares = [CKKSTLKShareRecord allForUUID:set.currentTLKPointer.currentKeyUUID zoneID:zoneID error:&error];
230 set.pendingTLKShares = nil;
239 -(NSString*)description {
241 return [NSString stringWithFormat:@"<CKKSCurrentKeySet(%@): %@:%@ %@:%@ %@:%@ new:%d %@>",
243 self.currentTLKPointer.currentKeyUUID, self.tlk,
244 self.currentClassAPointer.currentKeyUUID, self.classA,
245 self.currentClassCPointer.currentKeyUUID, self.classC,
250 return [NSString stringWithFormat:@"<CKKSCurrentKeySet(%@): %@:%@ %@:%@ %@:%@ new:%d>",
252 self.currentTLKPointer.currentKeyUUID, self.tlk,
253 self.currentClassAPointer.currentKeyUUID, self.classA,
254 self.currentClassCPointer.currentKeyUUID, self.classC,
258 - (instancetype)copyWithZone:(NSZone*)zone {
259 CKKSCurrentKeySet* copy = [[[self class] alloc] init];
260 copy.currentTLKPointer = [self.currentTLKPointer copyWithZone:zone];
261 copy.currentClassAPointer = [self.currentClassAPointer copyWithZone:zone];
262 copy.currentClassCPointer = [self.currentClassCPointer copyWithZone:zone];
263 copy.tlk = [self.tlk copyWithZone:zone];
264 copy.classA = [self.classA copyWithZone:zone];
265 copy.classC = [self.classC copyWithZone:zone];
266 copy.proposed = self.proposed;
268 copy.error = [self.error copyWithZone:zone];
272 - (CKKSKeychainBackedKeySet* _Nullable)asKeychainBackedSet:(NSError**)error
274 if(!self.tlk.keycore ||
275 !self.classA.keycore ||
276 !self.classC.keycore) {
278 *error = [NSError errorWithDomain:CKKSErrorDomain
280 description:@"unable to make keychain backed set; key is missing"];
285 return [[CKKSKeychainBackedKeySet alloc] initWithTLK:self.tlk.keycore
286 classA:self.classA.keycore
287 classC:self.classC.keycore
288 newUpload:self.proposed];