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 @implementation CKKSCurrentKeyPointer
30 - (instancetype)initForClass:(CKKSKeyClass*)keyclass
31 currentKeyUUID:(NSString*)currentKeyUUID
32 zoneID:(CKRecordZoneID*)zoneID
33 encodedCKRecord: (NSData*) encodedrecord
35 if(self = [super initWithCKRecordType: SecCKRecordCurrentKeyType encodedCKRecord:encodedrecord zoneID:zoneID]) {
37 _currentKeyUUID = currentKeyUUID;
39 if(self.currentKeyUUID == nil) {
40 secerror("ckkscurrentkey: created a CKKSCurrentKey with a nil currentKeyUUID. Why?");
46 - (NSString*)description {
47 return [NSString stringWithFormat:@"<CKKSCurrentKeyPointer(%@) %@: %@>", self.zoneID.zoneName, self.keyclass, self.currentKeyUUID];
50 - (instancetype)copyWithZone:(NSZone*)zone {
51 CKKSCurrentKeyPointer* copy = [super copyWithZone:zone];
52 copy.keyclass = [self.keyclass copyWithZone:zone];
53 copy.currentKeyUUID = [self.currentKeyUUID copyWithZone:zone];
56 - (BOOL)isEqual: (id) object {
57 if(![object isKindOfClass:[CKKSCurrentKeyPointer class]]) {
61 CKKSCurrentKeyPointer* obj = (CKKSCurrentKeyPointer*) object;
63 return ([self.zoneID isEqual: obj.zoneID] &&
64 ((self.currentKeyUUID == nil && obj.currentKeyUUID == nil) || [self.currentKeyUUID isEqual: obj.currentKeyUUID]) &&
65 ((self.keyclass == nil && obj.keyclass == nil) || [self.keyclass isEqual:obj.keyclass]) &&
69 #pragma mark - CKKSCKRecordHolder methods
71 - (NSString*) CKRecordName {
75 - (CKRecord*) updateCKRecord: (CKRecord*) record zoneID: (CKRecordZoneID*) zoneID {
76 // The record name should already match keyclass...
77 if(![record.recordID.recordName isEqualToString: self.keyclass]) {
79 exceptionWithName:@"WrongCKRecordNameException"
80 reason:[NSString stringWithFormat: @"CKRecord name (%@) was not %@", record.recordID.recordName, self.keyclass]
84 // Set the parent reference
85 record[SecCKRecordParentKeyRefKey] = [[CKReference alloc] initWithRecordID: [[CKRecordID alloc] initWithRecordName: self.currentKeyUUID zoneID: zoneID] action: CKReferenceActionNone];
89 - (bool) matchesCKRecord: (CKRecord*) record {
90 if(![record.recordType isEqualToString: SecCKRecordCurrentKeyType]) {
94 if(![record.recordID.recordName isEqualToString: self.keyclass]) {
98 if(![[record[SecCKRecordParentKeyRefKey] recordID].recordName isEqualToString: self.currentKeyUUID]) {
105 - (void) setFromCKRecord: (CKRecord*) record {
106 if(![record.recordType isEqualToString: SecCKRecordCurrentKeyType]) {
108 exceptionWithName:@"WrongCKRecordTypeException"
109 reason:[NSString stringWithFormat: @"CKRecordType (%@) was not %@", record.recordType, SecCKRecordCurrentKeyType]
113 [self setStoredCKRecord:record];
115 // TODO: verify this is a real keyclass
116 self.keyclass = (CKKSKeyClass*) record.recordID.recordName;
117 self.currentKeyUUID = [record[SecCKRecordParentKeyRefKey] recordID].recordName;
119 if(self.currentKeyUUID == nil) {
120 secerror("ckkscurrentkey: No current key UUID in record! How/why? %@", record);
124 #pragma mark - Load from database
126 + (instancetype) fromDatabase: (CKKSKeyClass*) keyclass zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
127 return [self fromDatabaseWhere: @{@"keyclass": keyclass, @"ckzone":zoneID.zoneName} error: error];
130 + (instancetype) tryFromDatabase: (CKKSKeyClass*) keyclass zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
131 return [self tryFromDatabaseWhere: @{@"keyclass": keyclass, @"ckzone":zoneID.zoneName} error: error];
134 + (instancetype) forKeyClass: (CKKSKeyClass*) keyclass withKeyUUID: (NSString*) keyUUID zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
135 NSError* localerror = nil;
136 CKKSCurrentKeyPointer* current = [self tryFromDatabase: keyclass zoneID:zoneID error: &localerror];
145 current.currentKeyUUID = keyUUID;
149 return [[CKKSCurrentKeyPointer alloc] initForClass: keyclass currentKeyUUID: keyUUID zoneID:zoneID encodedCKRecord:nil];
152 + (NSArray<CKKSCurrentKeyPointer*>*)all:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
153 return [self allWhere:@{@"ckzone":zoneID.zoneName} error:error];
156 + (bool) deleteAll:(CKRecordZoneID*) zoneID error: (NSError * __autoreleasing *) error {
157 bool ok = [CKKSSQLDatabaseObject deleteFromTable:[self sqlTable] where: @{@"ckzone":zoneID.zoneName} connection:nil error: error];
160 secdebug("ckksitem", "Deleted all %@", self);
162 secdebug("ckksitem", "Couldn't delete all %@: %@", self, error ? *error : @"unknown");
167 #pragma mark - CKKSSQLDatabaseObject methods
169 + (NSString*) sqlTable {
170 return @"currentkeys";
173 + (NSArray<NSString*>*) sqlColumns {
174 return @[@"keyclass", @"currentKeyUUID", @"ckzone", @"ckrecord"];
177 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
178 return @{@"keyclass": self.keyclass, @"ckzone":self.zoneID.zoneName};
181 - (NSDictionary<NSString*,NSString*>*) sqlValues {
182 return @{@"keyclass": self.keyclass,
183 @"currentKeyUUID": CKKSNilToNSNull(self.currentKeyUUID),
184 @"ckzone": CKKSNilToNSNull(self.zoneID.zoneName),
185 @"ckrecord": CKKSNilToNSNull([self.encodedCKRecord base64EncodedStringWithOptions:0]),
189 + (instancetype) fromDatabaseRow: (NSDictionary*) row {
190 return [[CKKSCurrentKeyPointer alloc] initForClass: row[@"keyclass"]
191 currentKeyUUID: [row[@"currentKeyUUID"] isEqual: [NSNull null]] ? nil : row[@"currentKeyUUID"]
192 zoneID: [[CKRecordZoneID alloc] initWithZoneName: row[@"ckzone"] ownerName:CKCurrentUserDefaultName]
193 encodedCKRecord: [[NSData alloc] initWithBase64EncodedString: row[@"ckrecord"] options:0]];
198 @implementation CKKSCurrentKeySet
199 -(instancetype)init {
200 if((self = [super init])) {
205 -(instancetype)initForZone:(CKRecordZoneID*)zoneID {
206 if((self = [super init])) {
207 NSError* error = nil;
208 _currentTLKPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassTLK zoneID:zoneID error:&error];
209 _currentClassAPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassA zoneID:zoneID error:&error];
210 _currentClassCPointer = [CKKSCurrentKeyPointer tryFromDatabase: SecCKKSKeyClassC zoneID:zoneID error:&error];
212 _tlk = _currentTLKPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:_currentTLKPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
213 _classA = _currentClassAPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:_currentClassAPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
214 _classC = _currentClassCPointer.currentKeyUUID ? [CKKSKey tryFromDatabase:_currentClassCPointer.currentKeyUUID zoneID:zoneID error:&error] : nil;
216 _tlkShares = [CKKSTLKShare allForUUID:_currentTLKPointer.currentKeyUUID zoneID:zoneID error:&error];
224 -(NSString*)description {
226 return [NSString stringWithFormat:@"<CKKSCurrentKeySet: %@:%@ %@:%@ %@:%@ %@>",
227 self.currentTLKPointer.currentKeyUUID, self.tlk,
228 self.currentClassAPointer.currentKeyUUID, self.classA,
229 self.currentClassCPointer.currentKeyUUID, self.classC,
233 return [NSString stringWithFormat:@"<CKKSCurrentKeySet: %@:%@ %@:%@ %@:%@>",
234 self.currentTLKPointer.currentKeyUUID, self.tlk,
235 self.currentClassAPointer.currentKeyUUID, self.classA,
236 self.currentClassCPointer.currentKeyUUID, self.classC];
239 - (instancetype)copyWithZone:(NSZone*)zone {
240 CKKSCurrentKeySet* copy = [[[self class] alloc] init];
241 copy.currentTLKPointer = [self.currentTLKPointer copyWithZone:zone];
242 copy.currentClassAPointer = [self.currentClassAPointer copyWithZone:zone];
243 copy.currentClassCPointer = [self.currentClassCPointer copyWithZone:zone];
244 copy.tlk = [self.tlk copyWithZone:zone];
245 copy.classA = [self.classA copyWithZone:zone];
246 copy.classC = [self.classC copyWithZone:zone];
248 copy.error = [self.error copyWithZone:zone];