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 "keychain/ckks/CKKSCurrentItemPointer.h"
28 @implementation CKKSCurrentItemPointer
30 - (instancetype)initForIdentifier:(NSString*)identifier
31 currentItemUUID:(NSString*)currentItemUUID
32 state:(CKKSProcessedState*)state
33 zoneID:(CKRecordZoneID*)zoneID
34 encodedCKRecord: (NSData*) encodedrecord
36 if(self = [super initWithCKRecordType: SecCKRecordCurrentItemType encodedCKRecord:encodedrecord zoneID:zoneID]) {
38 _identifier = identifier;
39 _currentItemUUID = currentItemUUID;
44 - (NSString*)description {
45 return [NSString stringWithFormat:@"<CKKSCurrentItemPointer(%@) %@: %@>", self.zoneID.zoneName, self.identifier, self.currentItemUUID];
48 #pragma mark - CKKSCKRecordHolder methods
50 - (NSString*) CKRecordName {
51 return self.identifier;
54 - (CKRecord*)updateCKRecord: (CKRecord*) record zoneID: (CKRecordZoneID*) zoneID {
55 // The record name should already match identifier...
56 if(![record.recordID.recordName isEqualToString: self.identifier]) {
58 exceptionWithName:@"WrongCKRecordNameException"
59 reason:[NSString stringWithFormat: @"CKRecord name (%@) was not %@", record.recordID.recordName, self.identifier]
63 // Set the parent reference
64 record[SecCKRecordItemRefKey] = [[CKReference alloc] initWithRecordID: [[CKRecordID alloc] initWithRecordName: self.currentItemUUID zoneID: zoneID]
65 action: CKReferenceActionNone];
69 - (bool)matchesCKRecord: (CKRecord*) record {
70 if(![record.recordType isEqualToString: SecCKRecordCurrentItemType]) {
74 if(![record.recordID.recordName isEqualToString: self.identifier]) {
78 if(![[record[SecCKRecordItemRefKey] recordID].recordName isEqualToString: self.currentItemUUID]) {
85 - (void)setFromCKRecord: (CKRecord*) record {
86 if(![record.recordType isEqualToString: SecCKRecordCurrentItemType]) {
88 exceptionWithName:@"WrongCKRecordTypeException"
89 reason:[NSString stringWithFormat: @"CKRecordType (%@) was not %@", record.recordType, SecCKRecordCurrentItemType]
93 [self setStoredCKRecord:record];
95 self.identifier = (CKKSKeyClass*) record.recordID.recordName;
96 self.currentItemUUID = [record[SecCKRecordItemRefKey] recordID].recordName;
99 #pragma mark - Load from database
101 + (instancetype)fromDatabase:(NSString*)identifier state:(CKKSProcessedState*)state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
102 return [self fromDatabaseWhere: @{@"identifier":identifier, @"state":state, @"ckzone":zoneID.zoneName} error: error];
105 + (instancetype)tryFromDatabase:(NSString*)identifier state:(CKKSProcessedState*)state zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
106 return [self tryFromDatabaseWhere: @{@"identifier":identifier, @"state":state, @"ckzone":zoneID.zoneName} error: error];
109 + (NSArray<CKKSCurrentItemPointer*>*)remoteItemPointers: (CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
110 return [self allWhere: @{@"state": SecCKKSProcessedStateRemote, @"ckzone":zoneID.zoneName} error:error];
113 + (NSArray<CKKSCurrentItemPointer*>*)allInZone:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error {
114 return [self allWhere: @{@"ckzone":zoneID.zoneName} error:error];
117 + (bool)deleteAll:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error {
118 bool ok = [CKKSSQLDatabaseObject deleteFromTable:[self sqlTable] where: @{@"ckzone":zoneID.zoneName} connection:nil error: error];
121 secdebug("ckksitem", "Deleted all %@", self);
123 secdebug("ckksitem", "Couldn't delete all %@: %@", self, error ? *error : @"unknown");
128 #pragma mark - CKKSSQLDatabaseObject methods
130 + (NSString*)sqlTable {
131 return @"currentitems";
134 + (NSArray<NSString*>*)sqlColumns {
135 return @[@"identifier", @"currentItemUUID", @"state", @"ckzone", @"ckrecord"];
138 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
139 return @{@"identifier": self.identifier, @"ckzone":self.zoneID.zoneName, @"state":self.state};
142 - (NSDictionary<NSString*,NSString*>*)sqlValues {
143 return @{@"identifier": self.identifier,
144 @"currentItemUUID": CKKSNilToNSNull(self.currentItemUUID),
145 @"state": CKKSNilToNSNull(self.state),
146 @"ckzone": CKKSNilToNSNull(self.zoneID.zoneName),
147 @"ckrecord": CKKSNilToNSNull([self.encodedCKRecord base64EncodedStringWithOptions:0]),
151 + (instancetype)fromDatabaseRow: (NSDictionary*) row {
152 return [[CKKSCurrentItemPointer alloc] initForIdentifier:row[@"identifier"]
153 currentItemUUID:CKKSNSNullToNil(row[@"currentItemUUID"])
154 state:CKKSNSNullToNil(row[@"state"])
155 zoneID:[[CKRecordZoneID alloc] initWithZoneName: row[@"ckzone"] ownerName:CKCurrentUserDefaultName]
156 encodedCKRecord:[[NSData alloc] initWithBase64EncodedString: row[@"ckrecord"] options:0]];