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@
25 #import <Foundation/Foundation.h>
29 #include "keychain/securityd/SecDbItem.h"
30 #import "keychain/ckks/CKKS.h"
31 #import "keychain/ckks/OctagonAPSReceiver.h"
32 #import "keychain/ckks/CKKSAccountStateTracker.h"
33 #import "keychain/ckks/CKKSCloudKitClassDependencies.h"
34 #import "keychain/ckks/CKKSCondition.h"
35 #import "keychain/ckks/CKKSControlProtocol.h"
36 #import "keychain/ckks/CKKSLockStateTracker.h"
37 #import "keychain/ckks/CKKSReachabilityTracker.h"
38 #import "keychain/ckks/CKKSNotifier.h"
39 #import "keychain/ckks/CKKSPeer.h"
40 #import "keychain/ckks/CKKSRateLimiter.h"
41 #import "keychain/ckks/CloudKitDependencies.h"
42 #import "keychain/ckks/CKKSZoneChangeFetcher.h"
43 #import "keychain/ckks/CKKSZoneModifier.h"
45 #import "keychain/ot/OTSOSAdapter.h"
46 #import "keychain/ot/OTDefines.h"
48 NS_ASSUME_NONNULL_BEGIN
50 @
class CKKSKeychainView
, CKKSRateLimiter
, TPPolicy
;
52 @interface CKKSViewManager
: NSObject
<CKKSControlProtocol
>
54 @property CKContainer
* container
;
55 @property CKKSAccountStateTracker
* accountTracker
;
56 @property CKKSLockStateTracker
* lockStateTracker
;
57 @property CKKSReachabilityTracker
*reachabilityTracker
;
58 @property CKKSZoneChangeFetcher
* zoneChangeFetcher
;
59 @property CKKSZoneModifier
* zoneModifier
;
61 // Signaled when SecCKKSInitialize is complete, as it's async and likes to fire after tests are complete
62 @property CKKSCondition
* completedSecCKKSInitialize
;
64 @property CKKSRateLimiter
* globalRateLimiter
;
66 @property id
<OTSOSAdapter
> sosPeerAdapter
;
68 @
property (readonly
, nullable
) TPPolicy
* policy
;
70 @
property (readonly
) NSMutableDictionary
<NSString
*, CKKSKeychainView
*>* views
;
72 - (instancetype
)initWithContainer
:(CKContainer
*)container
73 sosAdapter
:(id
<OTSOSAdapter
> _Nullable
)sosAdapter
74 accountStateTracker
:(CKKSAccountStateTracker
*)accountTracker
75 lockStateTracker
:(CKKSLockStateTracker
*)lockStateTracker
76 cloudKitClassDependencies
:(CKKSCloudKitClassDependencies
*)cloudKitClassDependencies
;
78 - (CKKSKeychainView
*)findView
:(NSString
*)viewName
;
79 - (CKKSKeychainView
*)findOrCreateView
:(NSString
*)viewName
;
80 - (void)setView
:(CKKSKeychainView
*)obj
;
81 - (void)clearView
:(NSString
*)viewName
;
83 - (NSSet
<CKKSKeychainView
*>*)currentViews
;
85 - (NSDictionary
<NSString
*, NSString
*>*)activeTLKs
;
87 - (void)setupAnalytics
;
89 - (NSString
* _Nullable
)viewNameForItem
:(SecDbItemRef
)item
;
91 - (void)handleKeychainEventDbConnection
:(SecDbConnectionRef
)dbconn
92 source
:(SecDbTransactionSource
)txionSource
93 added
:(SecDbItemRef _Nullable
)added
94 deleted
:(SecDbItemRef _Nullable
)deleted
;
96 - (void)setCurrentItemForAccessGroup
:(NSData
* _Nonnull
)newItemPersistentRef
97 hash
:(NSData
*)newItemSHA1
98 accessGroup
:(NSString
*)accessGroup
99 identifier
:(NSString
*)identifier
100 viewHint
:(NSString
*)viewHint
101 replacing
:(NSData
* _Nullable
)oldCurrentItemPersistentRef
102 hash
:(NSData
* _Nullable
)oldItemSHA1
103 complete
:(void (^)(NSError
* operror
))complete
;
105 - (void)getCurrentItemForAccessGroup
:(NSString
*)accessGroup
106 identifier
:(NSString
*)identifier
107 viewHint
:(NSString
*)viewHint
108 fetchCloudValue
:(bool)fetchCloudValue
109 complete
:(void (^)(NSString
* uuid
, NSError
* operror
))complete
;
111 - (void)registerSyncStatusCallback
:(NSString
*)uuid callback
:(SecBoolNSErrorCallback
)callback
;
113 // Cancels pending operations owned by this view manager
114 - (void)cancelPendingOperations
;
116 + (instancetype
)manager
;
118 // Called by XPC every 24 hours
119 - (void)xpc24HrNotification
;
121 // Returns the current set of views
122 - (NSSet
<NSString
*>*)viewList
;
124 - (NSSet
<NSString
*>*)defaultViewList
;
126 // Call this to set the syncing views+policy that this manager will use.
127 // If beginCloudKitOperationOfAllViews has previously been called, then any new views created
128 // as a result of this call will begin CK operation.
129 - (void)setSyncingViews
:(NSSet
<NSString
*>* _Nullable
)viewNames sortingPolicy
:(TPPolicy
* _Nullable
)policy
;
131 - (void)clearAllViews
;
133 // Create all views, but don't begin CK/network operations
134 // Remove as part of <rdar://problem/57768740> CKKS: ensure we collect keychain changes made before policy is loaded from disk
137 // Call this to begin CK operation of all views
138 // This bit will be 'sticky', in that any new views created with also begin cloudkit operation immediately.
139 // (clearAllViews will reset this bit.)
140 - (void)beginCloudKitOperationOfAllViews
;
142 // Notify sbd to re-backup.
143 - (void)notifyNewTLKsInKeychain
;
144 - (void)syncBackupAndNotifyAboutSync
;
146 // allow user blocking operation to block on trust status trying to sort it-self out the
147 // first time after launch, only waits the the initial call
148 - (BOOL
)waitForTrustReady
;
150 // Helper function to make CK containers
151 + (CKContainer
*)makeCKContainer
:(NSString
*)containerName
154 // Checks featureflags to return whether we should use policy-based views, or use the hardcoded list
155 - (BOOL
)useCKKSViewsFromPolicy
;
157 // Interfaces to examine sync callbacks
158 - (SecBoolNSErrorCallback _Nullable
)claimCallbackForUUID
:(NSString
*)uuid
;
159 - (NSSet
<NSString
*>*)pendingCallbackUUIDs
;
160 + (void)callSyncCallbackWithErrorNoAccount
:(SecBoolNSErrorCallback
)syncCallback
;
163 @interface
CKKSViewManager (Testing
)
164 - (void)setOverrideCKKSViewsFromPolicy
:(BOOL
)value
;
165 - (void)resetSyncingPolicy
;
168 - (CKKSKeychainView
*)restartZone
:(NSString
*)viewName
;
169 - (void)haltZone
:(NSString
*)viewName
;
171 // If set, any set passed to setSyncingViews will be intersected with this set
172 - (void)setSyncingViewsAllowList
:(NSSet
<NSString
*>* _Nullable
)viewNames
;
174 NS_ASSUME_NONNULL_END
177 @interface CKKSViewManager
: NSObject