]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSIncomingQueueEntry.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ckks / CKKSIncomingQueueEntry.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 #if OCTAGON
25
26 #include <AssertMacros.h>
27
28 #import <Foundation/Foundation.h>
29
30 #import "CKKSKeychainView.h"
31
32 #include <utilities/SecDb.h>
33 #include <securityd/SecDbItem.h>
34 #include <securityd/SecItemSchema.h>
35
36 #import <CloudKit/CloudKit.h>
37 #import "CKKSIncomingQueueEntry.h"
38 #import "CKKSItemEncrypter.h"
39 #import "CKKSSIV.h"
40
41 @implementation CKKSIncomingQueueEntry
42
43 - (NSString*)description {
44 return [NSString stringWithFormat: @"<%@(%@): %@ %@ (%@)>",
45 NSStringFromClass([self class]),
46 self.item.zoneID.zoneName,
47 self.action,
48 self.item.uuid,
49 self.state];
50 }
51
52 - (instancetype) initWithCKKSItem:(CKKSItem*) item
53 action:(NSString*) action
54 state:(NSString*) state {
55 if(self = [super init]) {
56 _item = item;
57 _action = action;
58 _state = state;
59 }
60
61 return self;
62 }
63
64 #pragma mark - Property access to underlying CKKSItem
65
66 -(NSString*)uuid {
67 return self.item.uuid;
68 }
69
70 -(void)setUuid:(NSString *)uuid {
71 self.item.uuid = uuid;
72 }
73
74 #pragma mark - Database Operations
75
76 + (instancetype) fromDatabase: (NSString*) uuid zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
77 return [self fromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
78 }
79
80 + (instancetype) tryFromDatabase: (NSString*) uuid zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
81 return [self tryFromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
82 }
83
84 + (NSArray<CKKSIncomingQueueEntry*>*)fetch:(ssize_t)n
85 startingAtUUID:(NSString*)uuid
86 state:(NSString*)state
87 zoneID:(CKRecordZoneID*)zoneID
88 error: (NSError * __autoreleasing *) error {
89 NSMutableDictionary* whereDict = [@{@"state": CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} mutableCopy];
90 if(uuid) {
91 whereDict[@"UUID"] = [CKKSSQLWhereObject op:@">" stringValue:uuid];
92 }
93 return [self fetch:n
94 where:whereDict
95 orderBy:@[@"UUID"]
96 error:error];
97 }
98
99
100 #pragma mark - CKKSSQLDatabaseObject methods
101
102 + (NSString*)sqlTable {
103 return @"incomingqueue";
104 }
105
106 + (NSArray<NSString*>*)sqlColumns {
107 return [[CKKSItem sqlColumns] arrayByAddingObjectsFromArray: @[@"action", @"state"]];
108 }
109
110 - (NSDictionary<NSString*,NSString*>*)whereClauseToFindSelf {
111 return [self.item whereClauseToFindSelf];
112 }
113
114 - (NSDictionary<NSString*,NSString*>*)sqlValues {
115 NSMutableDictionary* values = [[self.item sqlValues] mutableCopy];
116 values[@"action"] = self.action;
117 values[@"state"] = self.state;
118 return values;
119 }
120
121
122 + (instancetype)fromDatabaseRow:(NSDictionary<NSString *, CKKSSQLResult*>*) row {
123 return [[CKKSIncomingQueueEntry alloc] initWithCKKSItem: [CKKSItem fromDatabaseRow: row]
124 action:row[@"action"].asString
125 state:row[@"state"].asString];
126 }
127
128 + (NSDictionary<NSString*,NSNumber*>*)countsByStateInZone:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
129 NSMutableDictionary* results = [[NSMutableDictionary alloc] init];
130
131 [CKKSSQLDatabaseObject queryDatabaseTable: [[self class] sqlTable]
132 where: @{@"ckzone": CKKSNilToNSNull(zoneID.zoneName)}
133 columns: @[@"state", @"count(rowid)"]
134 groupBy: @[@"state"]
135 orderBy:nil
136 limit: -1
137 processRow: ^(NSDictionary<NSString*, CKKSSQLResult*>* row) {
138 results[row[@"state"].asString] = row[@"count(rowid)"].asNSNumberInteger;
139 }
140 error: error];
141 return results;
142 }
143
144 + (NSInteger)countByState:(CKKSItemState *)state zone:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
145 __block NSInteger result = -1;
146
147 [CKKSSQLDatabaseObject queryDatabaseTable: [[self class] sqlTable]
148 where: @{@"ckzone": CKKSNilToNSNull(zoneID.zoneName), @"state": state }
149 columns: @[@"count(*)"]
150 groupBy: nil
151 orderBy: nil
152 limit: -1
153 processRow: ^(NSDictionary<NSString*, CKKSSQLResult*>* row) {
154 result = row[@"count(*)"].asNSInteger;
155 }
156 error: error];
157 return result;
158 }
159
160 @end
161
162 #endif