]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSCurrentItemPointer.m
Security-58286.70.7.tar.gz
[apple/security.git] / keychain / ckks / CKKSCurrentItemPointer.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "keychain/ckks/CKKSCurrentItemPointer.h"
25
26 #if OCTAGON
27
28 @implementation CKKSCurrentItemPointer
29
30 - (instancetype)initForIdentifier:(NSString*)identifier
31 currentItemUUID:(NSString*)currentItemUUID
32 state:(CKKSProcessedState*)state
33 zoneID:(CKRecordZoneID*)zoneID
34 encodedCKRecord: (NSData*) encodedrecord
35 {
36 if(self = [super initWithCKRecordType: SecCKRecordCurrentItemType encodedCKRecord:encodedrecord zoneID:zoneID]) {
37 _state = state;
38 _identifier = identifier;
39 _currentItemUUID = currentItemUUID;
40 }
41 return self;
42 }
43
44 - (NSString*)description {
45 return [NSString stringWithFormat:@"<CKKSCurrentItemPointer(%@) %@: %@>", self.zoneID.zoneName, self.identifier, self.currentItemUUID];
46 }
47
48 #pragma mark - CKKSCKRecordHolder methods
49
50 - (NSString*) CKRecordName {
51 return self.identifier;
52 }
53
54 - (CKRecord*)updateCKRecord: (CKRecord*) record zoneID: (CKRecordZoneID*) zoneID {
55 // The record name should already match identifier...
56 if(![record.recordID.recordName isEqualToString: self.identifier]) {
57 @throw [NSException
58 exceptionWithName:@"WrongCKRecordNameException"
59 reason:[NSString stringWithFormat: @"CKRecord name (%@) was not %@", record.recordID.recordName, self.identifier]
60 userInfo:nil];
61 }
62
63 // Set the parent reference
64 record[SecCKRecordItemRefKey] = [[CKReference alloc] initWithRecordID: [[CKRecordID alloc] initWithRecordName: self.currentItemUUID zoneID: zoneID]
65 action: CKReferenceActionNone];
66 return record;
67 }
68
69 - (bool)matchesCKRecord: (CKRecord*) record {
70 if(![record.recordType isEqualToString: SecCKRecordCurrentItemType]) {
71 return false;
72 }
73
74 if(![record.recordID.recordName isEqualToString: self.identifier]) {
75 return false;
76 }
77
78 if(![[record[SecCKRecordItemRefKey] recordID].recordName isEqualToString: self.currentItemUUID]) {
79 return false;
80 }
81
82 return true;
83 }
84
85 - (void)setFromCKRecord: (CKRecord*) record {
86 if(![record.recordType isEqualToString: SecCKRecordCurrentItemType]) {
87 @throw [NSException
88 exceptionWithName:@"WrongCKRecordTypeException"
89 reason:[NSString stringWithFormat: @"CKRecordType (%@) was not %@", record.recordType, SecCKRecordCurrentItemType]
90 userInfo:nil];
91 }
92
93 [self setStoredCKRecord:record];
94
95 self.identifier = (CKKSKeyClass*) record.recordID.recordName;
96 self.currentItemUUID = [record[SecCKRecordItemRefKey] recordID].recordName;
97 }
98
99 #pragma mark - Load from database
100
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];
103 }
104
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];
107 }
108
109 + (NSArray<CKKSCurrentItemPointer*>*)remoteItemPointers: (CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
110 return [self allWhere: @{@"state": SecCKKSProcessedStateRemote, @"ckzone":zoneID.zoneName} error:error];
111 }
112
113 + (NSArray<CKKSCurrentItemPointer*>*)allInZone:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error {
114 return [self allWhere: @{@"ckzone":zoneID.zoneName} error:error];
115 }
116
117 + (bool)deleteAll:(CKRecordZoneID*)zoneID error:(NSError * __autoreleasing *)error {
118 bool ok = [CKKSSQLDatabaseObject deleteFromTable:[self sqlTable] where: @{@"ckzone":zoneID.zoneName} connection:nil error: error];
119
120 if(ok) {
121 secdebug("ckksitem", "Deleted all %@", self);
122 } else {
123 secdebug("ckksitem", "Couldn't delete all %@: %@", self, error ? *error : @"unknown");
124 }
125 return ok;
126 }
127
128 #pragma mark - CKKSSQLDatabaseObject methods
129
130 + (NSString*)sqlTable {
131 return @"currentitems";
132 }
133
134 + (NSArray<NSString*>*)sqlColumns {
135 return @[@"identifier", @"currentItemUUID", @"state", @"ckzone", @"ckrecord"];
136 }
137
138 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
139 return @{@"identifier": self.identifier, @"ckzone":self.zoneID.zoneName, @"state":self.state};
140 }
141
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]),
148 };
149 }
150
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]];
157 }
158
159 @end
160
161 #endif // OCTAGON
162