]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSZoneStateEntry.m
Security-59306.80.4.tar.gz
[apple/security.git] / keychain / ckks / CKKSZoneStateEntry.m
1 /*
2 * Copyright (c) 2016 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 #include <AssertMacros.h>
25
26 #import <Foundation/Foundation.h>
27 #import <Foundation/NSKeyedArchiver_Private.h>
28
29 #import "CKKSKeychainView.h"
30
31 #include <utilities/SecDb.h>
32 #include "keychain/securityd/SecDbItem.h"
33 #include "keychain/securityd/SecItemSchema.h"
34
35 #if OCTAGON
36
37 #import <CloudKit/CloudKit.h>
38 #import "CKKSZoneStateEntry.h"
39 #import "keychain/ckks/CKKSRateLimiter.h"
40 #import "keychain/ckks/CKKSFixups.h"
41
42
43 @implementation CKKSZoneStateEntry
44
45 - (instancetype)initWithCKZone:(NSString*)ckzone
46 zoneCreated:(bool)ckzonecreated
47 zoneSubscribed:(bool)ckzonesubscribed
48 changeToken:(NSData*)changetoken
49 moreRecordsInCloudKit:(BOOL)moreRecords
50 lastFetch:(NSDate*)lastFetch
51 lastFixup:(CKKSFixup)lastFixup
52 encodedRateLimiter:(NSData*)encodedRateLimiter
53 {
54 if(self = [super init]) {
55 _ckzone = ckzone;
56 _ckzonecreated = ckzonecreated;
57 _ckzonesubscribed = ckzonesubscribed;
58 _encodedChangeToken = changetoken;
59 _moreRecordsInCloudKit = moreRecords;
60 _lastFetchTime = lastFetch;
61 _lastFixup = lastFixup;
62
63 self.encodedRateLimiter = encodedRateLimiter;
64 }
65 return self;
66 }
67
68 - (BOOL)isEqual: (id) object {
69 if(![object isKindOfClass:[CKKSZoneStateEntry class]]) {
70 return NO;
71 }
72
73 CKKSZoneStateEntry* obj = (CKKSZoneStateEntry*) object;
74
75 return ([self.ckzone isEqualToString: obj.ckzone] &&
76 self.ckzonecreated == obj.ckzonecreated &&
77 self.ckzonesubscribed == obj.ckzonesubscribed &&
78 ((self.encodedChangeToken == nil && obj.encodedChangeToken == nil) || [self.encodedChangeToken isEqual: obj.encodedChangeToken]) &&
79 self.moreRecordsInCloudKit == obj.moreRecordsInCloudKit &&
80 ((self.lastFetchTime == nil && obj.lastFetchTime == nil) || [self.lastFetchTime isEqualToDate: obj.lastFetchTime]) &&
81 ((self.rateLimiter == nil && obj.rateLimiter == nil) || [self.rateLimiter isEqual: obj.rateLimiter]) &&
82 self.lastFixup == obj.lastFixup &&
83 true) ? YES : NO;
84 }
85
86 + (instancetype) state: (NSString*) ckzone {
87 NSError* error = nil;
88 CKKSZoneStateEntry* ret = [CKKSZoneStateEntry tryFromDatabase:ckzone error:&error];
89
90 if(error) {
91 secerror("CKKS: error fetching CKState(%@): %@", ckzone, error);
92 }
93
94 if(!ret) {
95 ret = [[CKKSZoneStateEntry alloc] initWithCKZone:ckzone
96 zoneCreated:false
97 zoneSubscribed:false
98 changeToken:nil
99 moreRecordsInCloudKit:NO
100 lastFetch:nil
101 lastFixup:CKKSCurrentFixupNumber
102 encodedRateLimiter:nil];
103 }
104 return ret;
105 }
106
107 - (CKServerChangeToken*) getChangeToken {
108 if(self.encodedChangeToken) {
109 NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:self.encodedChangeToken error:nil];
110 return [unarchiver decodeObjectOfClass:[CKServerChangeToken class] forKey:NSKeyedArchiveRootObjectKey];
111 } else {
112 return nil;
113 }
114 }
115
116 - (void) setChangeToken: (CKServerChangeToken*) token {
117 self.encodedChangeToken = token ? [NSKeyedArchiver archivedDataWithRootObject:token requiringSecureCoding:YES error:nil] : nil;
118 }
119
120 - (NSData*)encodedRateLimiter {
121 if(self.rateLimiter == nil) {
122 return nil;
123 }
124 return [NSKeyedArchiver archivedDataWithRootObject:self.rateLimiter requiringSecureCoding:YES error:nil];
125 }
126
127 - (void)setEncodedRateLimiter:(NSData *)encodedRateLimiter {
128 if(encodedRateLimiter == nil) {
129 self.rateLimiter = nil;
130 return;
131 }
132
133 NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:encodedRateLimiter error:nil];
134 self.rateLimiter = [unarchiver decodeObjectOfClass: [CKKSRateLimiter class] forKey:NSKeyedArchiveRootObjectKey];
135 }
136
137 #pragma mark - Database Operations
138
139 + (instancetype) fromDatabase: (NSString*) ckzone error: (NSError * __autoreleasing *) error {
140 return [self fromDatabaseWhere: @{@"ckzone": CKKSNilToNSNull(ckzone)} error: error];
141 }
142
143 + (instancetype) tryFromDatabase: (NSString*) ckzone error: (NSError * __autoreleasing *) error {
144 return [self tryFromDatabaseWhere: @{@"ckzone": CKKSNilToNSNull(ckzone)} error: error];
145 }
146
147 #pragma mark - CKKSSQLDatabaseObject methods
148
149 + (NSString*) sqlTable {
150 return @"ckstate";
151 }
152
153 + (NSArray<NSString*>*) sqlColumns {
154 return @[@"ckzone", @"ckzonecreated", @"ckzonesubscribed", @"changetoken", @"lastfetch", @"ratelimiter", @"lastFixup", @"morecoming"];
155 }
156
157 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
158 return @{@"ckzone": self.ckzone};
159 }
160
161 - (NSDictionary<NSString*,id>*) sqlValues {
162 NSISO8601DateFormatter* dateFormat = [[NSISO8601DateFormatter alloc] init];
163
164 return @{@"ckzone": self.ckzone,
165 @"ckzonecreated": [NSNumber numberWithBool:self.ckzonecreated],
166 @"ckzonesubscribed": [NSNumber numberWithBool:self.ckzonesubscribed],
167 @"changetoken": CKKSNilToNSNull([self.encodedChangeToken base64EncodedStringWithOptions:0]),
168 @"lastfetch": CKKSNilToNSNull(self.lastFetchTime ? [dateFormat stringFromDate: self.lastFetchTime] : nil),
169 @"ratelimiter": CKKSNilToNSNull([self.encodedRateLimiter base64EncodedStringWithOptions:0]),
170 @"lastFixup": [NSNumber numberWithLong:self.lastFixup],
171 @"morecoming": [NSNumber numberWithBool:self.moreRecordsInCloudKit],
172 };
173 }
174
175 + (instancetype)fromDatabaseRow:(NSDictionary<NSString*, CKKSSQLResult*>*)row {
176 return [[CKKSZoneStateEntry alloc] initWithCKZone:row[@"ckzone"].asString
177 zoneCreated:row[@"ckzonecreated"].asBOOL
178 zoneSubscribed:row[@"ckzonesubscribed"].asBOOL
179 changeToken:row[@"changetoken"].asBase64DecodedData
180 moreRecordsInCloudKit:row[@"morecoming"].asBOOL
181 lastFetch:row[@"lastfetch"].asISO8601Date
182 lastFixup:(CKKSFixup)row[@"lastFixup"].asNSInteger
183 encodedRateLimiter:row[@"ratelimiter"].asBase64DecodedData
184 ];
185 }
186
187 @end
188
189 #endif