]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSKeychainView.h
Security-59306.140.5.tar.gz
[apple/security.git] / keychain / ckks / CKKSKeychainView.h
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 #import <Foundation/Foundation.h>
26 #include <dispatch/dispatch.h>
27
28 #import "keychain/analytics/CKKSLaunchSequence.h"
29 #import "keychain/ckks/OctagonAPSReceiver.h"
30 #import "keychain/ckks/CKKSLockStateTracker.h"
31 #import "keychain/ckks/CKKSReachabilityTracker.h"
32 #import "keychain/ckks/CloudKitDependencies.h"
33
34 #include "keychain/securityd/SecDbItem.h"
35 #include <utilities/SecDb.h>
36
37 #import "keychain/ckks/CKKS.h"
38 #import "keychain/ckks/CKKSFetchAllRecordZoneChangesOperation.h"
39 #import "keychain/ckks/CKKSGroupOperation.h"
40 #import "keychain/ckks/CKKSIncomingQueueOperation.h"
41 #import "keychain/ckks/CKKSNearFutureScheduler.h"
42 #import "keychain/ckks/CKKSNewTLKOperation.h"
43 #import "keychain/ckks/CKKSNotifier.h"
44 #import "keychain/ckks/CKKSOutgoingQueueOperation.h"
45 #import "keychain/ckks/CKKSPeer.h"
46 #import "keychain/ckks/CKKSPeerProvider.h"
47 #import "keychain/ckks/CKKSProcessReceivedKeysOperation.h"
48 #import "keychain/ckks/CKKSReencryptOutgoingItemsOperation.h"
49 #import "keychain/ckks/CKKSScanLocalItemsOperation.h"
50 #import "keychain/ckks/CKKSTLKShareRecord.h"
51 #import "keychain/ckks/CKKSUpdateDeviceStateOperation.h"
52 #import "keychain/ckks/CKKSZone.h"
53 #import "keychain/ckks/CKKSZoneModifier.h"
54 #import "keychain/ckks/CKKSZoneChangeFetcher.h"
55 #import "keychain/ckks/CKKSSynchronizeOperation.h"
56 #import "keychain/ckks/CKKSLocalSynchronizeOperation.h"
57 #import "keychain/ckks/CKKSProvideKeySetOperation.h"
58
59 #include "CKKS.h"
60
61 NS_ASSUME_NONNULL_BEGIN
62
63 @class CKKSKey;
64 @class CKKSAESSIVKey;
65 @class CKKSSynchronizeOperation;
66 @class CKKSRateLimiter;
67 @class CKKSManifest;
68 @class CKKSEgoManifest;
69 @class CKKSOutgoingQueueEntry;
70 @class CKKSZoneChangeFetcher;
71 @class CKKSCurrentKeySet;
72
73 @interface CKKSKeychainView : CKKSZone <CKKSZoneUpdateReceiver,
74 CKKSChangeFetcherClient,
75 CKKSPeerUpdateListener>
76 {
77 CKKSZoneKeyState* _keyHierarchyState;
78 }
79
80 @property CKKSCondition* loggedIn;
81 @property CKKSCondition* loggedOut;
82 @property CKKSCondition* accountStateKnown;
83
84 @property CKKSAccountStatus trustStatus;
85 @property (nullable) CKKSResultOperation* trustDependency;
86
87 @property (nullable) CKKSLaunchSequence *launch;
88
89 @property CKKSLockStateTracker* lockStateTracker;
90
91 @property CKKSZoneKeyState* keyHierarchyState;
92 @property (nullable) NSError* keyHierarchyError;
93 @property (nullable) CKOperationGroup* keyHierarchyOperationGroup;
94 @property (nullable) NSOperation* keyStateMachineOperation;
95
96 // If the key hierarchy isn't coming together, it might be because we're out of sync with cloudkit.
97 // Use this to track if we've completed a full refetch, so fix-up operations can be done.
98 @property bool keyStateMachineRefetched;
99
100 // Set this to request a key state refetch (tests only)
101 @property bool keyStateFullRefetchRequested;
102
103 @property (nullable) CKKSEgoManifest* egoManifest;
104 @property (nullable) CKKSManifest* latestManifest;
105 @property (nullable) CKKSResultOperation* keyStateReadyDependency;
106
107 // Wait for the key state to become 'nontransient': no pending operation is expected to advance it (at least until intervention)
108 @property (nullable) CKKSResultOperation* keyStateNonTransientDependency;
109
110 // True if we believe there's any items in the keychain which haven't been brought up in CKKS yet
111 @property bool droppedItems;
112
113 @property (readonly) NSString* lastActiveTLKUUID;
114
115 // Full of condition variables, if you'd like to try to wait until the key hierarchy is in some state
116 @property NSMutableDictionary<CKKSZoneKeyState*, CKKSCondition*>* keyHierarchyConditions;
117
118 @property CKKSZoneChangeFetcher* zoneChangeFetcher;
119
120 @property (weak) CKKSNearFutureScheduler* savedTLKNotifier;
121
122 @property (nullable) CKKSNearFutureScheduler* suggestTLKUpload;
123
124
125 /* Used for debugging: just what happened last time we ran this? */
126 @property CKKSIncomingQueueOperation* lastIncomingQueueOperation;
127 @property CKKSNewTLKOperation* lastNewTLKOperation;
128 @property CKKSOutgoingQueueOperation* lastOutgoingQueueOperation;
129 @property CKKSProcessReceivedKeysOperation* lastProcessReceivedKeysOperation;
130 @property CKKSReencryptOutgoingItemsOperation* lastReencryptOutgoingItemsOperation;
131 @property CKKSScanLocalItemsOperation* lastScanLocalItemsOperation;
132 @property CKKSSynchronizeOperation* lastSynchronizeOperation;
133 @property CKKSResultOperation* lastFixupOperation;
134
135 /* Used for testing: pause operation types by adding operations here */
136 @property NSOperation* holdReencryptOutgoingItemsOperation;
137 @property NSOperation* holdOutgoingQueueOperation;
138 @property NSOperation* holdIncomingQueueOperation;
139 @property NSOperation* holdLocalSynchronizeOperation;
140 @property CKKSResultOperation* holdFixupOperation;
141
142 /* Trigger this to tell the whole machine that this view has changed */
143 @property CKKSNearFutureScheduler* notifyViewChangedScheduler;
144
145 /* Trigger this to tell the whole machine that this view is more ready then before */
146 @property CKKSNearFutureScheduler* notifyViewReadyScheduler;
147
148 /* trigger this to request key state machine poking */
149 @property CKKSNearFutureScheduler* pokeKeyStateMachineScheduler;
150
151 // The current list of peer providers. If empty, CKKS will consider itself untrusted, and halt operation
152 @property (readonly) NSArray<id<CKKSPeerProvider>>* currentPeerProviders;
153
154 // These are available when you're in a dispatchSyncWithAccountKeys call, but at no other time
155 // These must be pre-fetched before you get on the CKKS queue, otherwise we end up with CKKS<->SQLite<->SOSAccountQueue deadlocks
156
157 // They will be in a parallel array with currentPeerProviders above
158 @property (readonly) NSArray<CKKSPeerProviderState*>* currentTrustStates;
159
160 - (instancetype)initWithContainer:(CKContainer*)container
161 zoneName:(NSString*)zoneName
162 accountTracker:(CKKSAccountStateTracker*)accountTracker
163 lockStateTracker:(CKKSLockStateTracker*)lockStateTracker
164 reachabilityTracker:(CKKSReachabilityTracker*)reachabilityTracker
165 changeFetcher:(CKKSZoneChangeFetcher*)fetcher
166 zoneModifier:(CKKSZoneModifier*)zoneModifier
167 savedTLKNotifier:(CKKSNearFutureScheduler*)savedTLKNotifier
168 cloudKitClassDependencies:(CKKSCloudKitClassDependencies*)cloudKitClassDependencies;
169
170 /* Trust state management */
171 - (void)beginTrustedOperation:(NSArray<id<CKKSPeerProvider>>*)peerProviders
172 suggestTLKUpload:(CKKSNearFutureScheduler*)suggestTLKUpload;
173 - (void)endTrustedOperation;
174
175 /* Synchronous operations */
176
177 - (void)handleKeychainEventDbConnection:(SecDbConnectionRef)dbconn
178 source:(SecDbTransactionSource)txionSource
179 added:(SecDbItemRef _Nullable)added
180 deleted:(SecDbItemRef _Nullable)deleted
181 rateLimiter:(CKKSRateLimiter*)rateLimiter;
182
183 - (void)setCurrentItemForAccessGroup:(NSData*)newItemPersistentRef
184 hash:(NSData*)newItemSHA1
185 accessGroup:(NSString*)accessGroup
186 identifier:(NSString*)identifier
187 replacing:(NSData* _Nullable)oldCurrentItemPersistentRef
188 hash:(NSData* _Nullable)oldItemSHA1
189 complete:(void (^)(NSError* operror))complete;
190
191 - (void)getCurrentItemForAccessGroup:(NSString*)accessGroup
192 identifier:(NSString*)identifier
193 fetchCloudValue:(bool)fetchCloudValue
194 complete:(void (^)(NSString* uuid, NSError* operror))complete;
195
196 - (bool)outgoingQueueEmpty:(NSError* __autoreleasing*)error;
197
198 - (CKKSResultOperation<CKKSKeySetProviderOperationProtocol>*)findKeySet;
199 - (void)receiveTLKUploadRecords:(NSArray<CKRecord*>*)records;
200
201 - (CKKSResultOperation*)waitForFetchAndIncomingQueueProcessing;
202 - (void)waitForKeyHierarchyReadiness;
203 - (void)cancelAllOperations;
204
205 - (CKKSKey* _Nullable)keyForItem:(SecDbItemRef)item error:(NSError* __autoreleasing*)error;
206
207 - (bool)_onqueueWithAccountKeysCheckTLK:(CKKSKey*)proposedTLK error:(NSError* __autoreleasing*)error;
208
209 - (BOOL)otherDevicesReportHavingTLKs:(CKKSCurrentKeySet*)keyset;
210
211 /* Asynchronous kickoffs */
212
213 - (CKKSOutgoingQueueOperation*)processOutgoingQueue:(CKOperationGroup* _Nullable)ckoperationGroup;
214 - (CKKSOutgoingQueueOperation*)processOutgoingQueueAfter:(CKKSResultOperation* _Nullable)after
215 ckoperationGroup:(CKOperationGroup* _Nullable)ckoperationGroup;
216 - (CKKSOutgoingQueueOperation*)processOutgoingQueueAfter:(CKKSResultOperation*)after
217 requiredDelay:(uint64_t)requiredDelay
218 ckoperationGroup:(CKOperationGroup*)ckoperationGroup;
219
220 - (CKKSIncomingQueueOperation*)processIncomingQueue:(bool)failOnClassA;
221 - (CKKSIncomingQueueOperation*)processIncomingQueue:(bool)failOnClassA after:(CKKSResultOperation* _Nullable)after;
222
223 - (CKKSScanLocalItemsOperation*)scanLocalItems:(NSString*)name;
224
225 // Schedules a process queueoperation to happen after the next device unlock. This may be Immediately, if the device is unlocked.
226 - (void)processIncomingQueueAfterNextUnlock;
227
228 // This operation will complete directly after the next ProcessIncomingQueue, and should supply that IQO's result. Used mainly for testing; otherwise you'd just kick off a IQO directly.
229 - (CKKSResultOperation*)resultsOfNextProcessIncomingQueueOperation;
230
231 // Schedules an operation to update this device's state record in CloudKit
232 // If rateLimit is true, the operation will abort if it's updated the record in the past 3 days
233 - (CKKSUpdateDeviceStateOperation*)updateDeviceState:(bool)rateLimit
234 waitForKeyHierarchyInitialization:(uint64_t)timeout
235 ckoperationGroup:(CKOperationGroup* _Nullable)ckoperationGroup;
236
237 - (CKKSSynchronizeOperation*)resyncWithCloud;
238 - (CKKSLocalSynchronizeOperation*)resyncLocal;
239
240 - (CKKSResultOperation*)fetchAndProcessCKChanges:(CKKSFetchBecause*)because;
241
242 - (CKKSResultOperation*)resetLocalData;
243 - (CKKSResultOperation*)resetCloudKitZone:(CKOperationGroup*)operationGroup;
244
245 // Call this to tell the key state machine that you think some new data has arrived that it is interested in
246 - (void)keyStateMachineRequestProcess;
247
248 // For our serial queue to work with how handleKeychainEventDbConnection is called from the main thread,
249 // every block on our queue must have a SecDBConnectionRef available to it before it begins on the queue.
250 // Use these helper methods to make sure those exist.
251 - (void)dispatchSync:(bool (^)(void))block;
252 - (void)dispatchSyncWithAccountKeys:(bool (^)(void))block;
253
254 /* Synchronous operations which must be called from inside a dispatchAsyncWithAccountKeys or dispatchSync block */
255
256 // Call this to request the key hierarchy state machine to fetch new updates
257 - (void)_onqueueKeyStateMachineRequestFetch;
258
259 // Call this to request the key hierarchy state machine to reprocess
260 - (void)_onqueueKeyStateMachineRequestProcess;
261
262 // Call this from a key hierarchy operation to move the state machine, and record the results of the last move.
263 - (void)_onqueueAdvanceKeyStateMachineToState:(CKKSZoneKeyState* _Nullable)state withError:(NSError* _Nullable)error;
264
265 // Since we might have people interested in the state transitions of objects, please do those transitions via these methods
266 - (bool)_onqueueChangeOutgoingQueueEntry:(CKKSOutgoingQueueEntry*)oqe
267 toState:(NSString*)state
268 error:(NSError* __autoreleasing*)error;
269 - (bool)_onqueueErrorOutgoingQueueEntry:(CKKSOutgoingQueueEntry*)oqe
270 itemError:(NSError*)itemError
271 error:(NSError* __autoreleasing*)error;
272
273 // Call this if you've done a write and received an error. It'll pull out any new records returned as CKErrorServerRecordChanged and pretend we received them in a fetch
274 //
275 // Note that you need to tell this function the records you wanted to save, so it can determine which record failed from its CKRecordID.
276 // I don't know why CKRecordIDs don't have record types, either.
277 - (bool)_onqueueCKWriteFailed:(NSError*)ckerror attemptedRecordsChanged:(NSDictionary<CKRecordID*, CKRecord*>*)savedRecords;
278
279 - (bool)_onqueueCKRecordChanged:(CKRecord*)record resync:(bool)resync;
280 - (bool)_onqueueCKRecordDeleted:(CKRecordID*)recordID recordType:(NSString*)recordType resync:(bool)resync;
281
282 // For this key, who doesn't yet have a CKKSTLKShare for it, shared to their current Octagon keys?
283 // Note that we really want a record sharing the TLK to ourselves, so this function might return
284 // a non-empty set even if all peers have the TLK: it wants us to make a record for ourself.
285 // If you pass in a non-empty set in afterUploading, those records will be included in the calculation.
286 - (NSSet<id<CKKSPeer>>*)_onqueueFindPeers:(CKKSPeerProviderState*)trustState
287 missingShare:(CKKSKey*)key
288 afterUploading:(NSSet<CKKSTLKShareRecord*>* _Nullable)newShares
289 error:(NSError* __autoreleasing*)error;
290
291 - (BOOL)_onqueueAreNewSharesSufficient:(NSSet<CKKSTLKShareRecord*>*)newShares
292 currentTLK:(CKKSKey*)key
293 error:(NSError* __autoreleasing*)error;
294
295 // For this key, share it to all trusted peers who don't have it yet
296 - (NSSet<CKKSTLKShareRecord*>* _Nullable)_onqueueCreateMissingKeyShares:(CKKSKey*)key error:(NSError* __autoreleasing*)error;
297
298 - (bool)_onqueueUpdateLatestManifestWithError:(NSError**)error;
299
300 - (CKKSDeviceStateEntry* _Nullable)_onqueueCurrentDeviceStateEntry:(NSError* __autoreleasing*)error;
301
302 // Please don't use these unless you're an Operation in this package
303 @property NSHashTable<CKKSIncomingQueueOperation*>* incomingQueueOperations;
304 @property NSHashTable<CKKSOutgoingQueueOperation*>* outgoingQueueOperations;
305
306 @property NSHashTable<CKKSScanLocalItemsOperation*>* scanLocalItemsOperations;
307 @property CKKSScanLocalItemsOperation* initialScanOperation;
308
309 // Returns the current state of this view, fastStatus is the same, but as name promise, no expensive calculations
310 - (NSDictionary<NSString*, NSString*>*)status;
311 - (NSDictionary<NSString*, NSString*>*)fastStatus;
312 @end
313
314 NS_ASSUME_NONNULL_END
315 #else // !OCTAGON
316 #import <Foundation/Foundation.h>
317 @interface CKKSKeychainView : NSObject
318 {
319 NSString* _containerName;
320 }
321 @end
322 #endif // OCTAGON