2 * Copyright (c) 2016 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 #include <AssertMacros.h>
26 #import <Foundation/Foundation.h>
27 #import <Foundation/NSKeyedArchiver_Private.h>
29 #import "CKKSKeychainView.h"
31 #include <utilities/SecDb.h>
32 #include <securityd/SecDbItem.h>
33 #include <securityd/SecItemSchema.h>
37 #import <CloudKit/CloudKit.h>
38 #import "CKKSZoneStateEntry.h"
39 #import "keychain/ckks/CKKSRateLimiter.h"
40 #import "keychain/ckks/CKKSFixups.h"
43 @implementation CKKSZoneStateEntry
45 - (instancetype)initWithCKZone:(NSString*)ckzone
46 zoneCreated:(bool)ckzonecreated
47 zoneSubscribed:(bool)ckzonesubscribed
48 changeToken:(NSData*)changetoken
49 lastFetch:(NSDate*)lastFetch
50 lastFixup:(CKKSFixup)lastFixup
51 encodedRateLimiter:(NSData*)encodedRateLimiter
53 if(self = [super init]) {
55 _ckzonecreated = ckzonecreated;
56 _ckzonesubscribed = ckzonesubscribed;
57 _encodedChangeToken = changetoken;
58 _lastFetchTime = lastFetch;
59 _lastFixup = lastFixup;
61 self.encodedRateLimiter = encodedRateLimiter;
66 - (BOOL)isEqual: (id) object {
67 if(![object isKindOfClass:[CKKSZoneStateEntry class]]) {
71 CKKSZoneStateEntry* obj = (CKKSZoneStateEntry*) object;
73 return ([self.ckzone isEqualToString: obj.ckzone] &&
74 self.ckzonecreated == obj.ckzonecreated &&
75 self.ckzonesubscribed == obj.ckzonesubscribed &&
76 ((self.encodedChangeToken == nil && obj.encodedChangeToken == nil) || [self.encodedChangeToken isEqual: obj.encodedChangeToken]) &&
77 ((self.lastFetchTime == nil && obj.lastFetchTime == nil) || [self.lastFetchTime isEqualToDate: obj.lastFetchTime]) &&
78 ((self.rateLimiter == nil && obj.rateLimiter == nil) || [self.rateLimiter isEqual: obj.rateLimiter]) &&
79 self.lastFixup == obj.lastFixup &&
83 + (instancetype) state: (NSString*) ckzone {
85 CKKSZoneStateEntry* ret = [CKKSZoneStateEntry tryFromDatabase:ckzone error:&error];
88 secerror("CKKS: error fetching CKState(%@): %@", ckzone, error);
92 ret = [[CKKSZoneStateEntry alloc] initWithCKZone:ckzone
97 lastFixup:CKKSCurrentFixupNumber
98 encodedRateLimiter:nil];
103 - (CKServerChangeToken*) getChangeToken {
104 if(self.encodedChangeToken) {
105 NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:self.encodedChangeToken error:nil];
106 return [unarchiver decodeObjectOfClass:[CKServerChangeToken class] forKey:NSKeyedArchiveRootObjectKey];
112 - (void) setChangeToken: (CKServerChangeToken*) token {
113 self.encodedChangeToken = token ? [NSKeyedArchiver archivedDataWithRootObject:token requiringSecureCoding:YES error:nil] : nil;
116 - (NSData*)encodedRateLimiter {
117 if(self.rateLimiter == nil) {
120 return [NSKeyedArchiver archivedDataWithRootObject:self.rateLimiter requiringSecureCoding:YES error:nil];
123 - (void)setEncodedRateLimiter:(NSData *)encodedRateLimiter {
124 if(encodedRateLimiter == nil) {
125 self.rateLimiter = nil;
129 NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:encodedRateLimiter error:nil];
130 self.rateLimiter = [unarchiver decodeObjectOfClass: [CKKSRateLimiter class] forKey:NSKeyedArchiveRootObjectKey];
133 #pragma mark - Database Operations
135 + (instancetype) fromDatabase: (NSString*) ckzone error: (NSError * __autoreleasing *) error {
136 return [self fromDatabaseWhere: @{@"ckzone": CKKSNilToNSNull(ckzone)} error: error];
139 + (instancetype) tryFromDatabase: (NSString*) ckzone error: (NSError * __autoreleasing *) error {
140 return [self tryFromDatabaseWhere: @{@"ckzone": CKKSNilToNSNull(ckzone)} error: error];
143 #pragma mark - CKKSSQLDatabaseObject methods
145 + (NSString*) sqlTable {
149 + (NSArray<NSString*>*) sqlColumns {
150 return @[@"ckzone", @"ckzonecreated", @"ckzonesubscribed", @"changetoken", @"lastfetch", @"ratelimiter", @"lastFixup"];
153 - (NSDictionary<NSString*,NSString*>*) whereClauseToFindSelf {
154 return @{@"ckzone": self.ckzone};
157 - (NSDictionary<NSString*,id>*) sqlValues {
158 NSISO8601DateFormatter* dateFormat = [[NSISO8601DateFormatter alloc] init];
160 return @{@"ckzone": self.ckzone,
161 @"ckzonecreated": [NSNumber numberWithBool:self.ckzonecreated],
162 @"ckzonesubscribed": [NSNumber numberWithBool:self.ckzonesubscribed],
163 @"changetoken": CKKSNilToNSNull([self.encodedChangeToken base64EncodedStringWithOptions:0]),
164 @"lastfetch": CKKSNilToNSNull(self.lastFetchTime ? [dateFormat stringFromDate: self.lastFetchTime] : nil),
165 @"ratelimiter": CKKSNilToNSNull([self.encodedRateLimiter base64EncodedStringWithOptions:0]),
166 @"lastFixup": [NSNumber numberWithLong:self.lastFixup],
170 + (instancetype)fromDatabaseRow:(NSDictionary<NSString*, CKKSSQLResult*>*)row {
171 return [[CKKSZoneStateEntry alloc] initWithCKZone:row[@"ckzone"].asString
172 zoneCreated:row[@"ckzonecreated"].asBOOL
173 zoneSubscribed:row[@"ckzonesubscribed"].asBOOL
174 changeToken:row[@"changetoken"].asBase64DecodedData
175 lastFetch:row[@"lastfetch"].asISO8601Date
176 lastFixup:(CKKSFixup)row[@"lastFixup"].asNSInteger
177 encodedRateLimiter:row[@"ratelimiter"].asBase64DecodedData