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 <dispatch/dispatch.h>
25 #import <Foundation/Foundation.h>
27 #import <CloudKit/CloudKit.h>
30 #include <utilities/debugging.h>
31 #include "keychain/securityd/SecItemServer.h"
32 #include <Security/SecItemPriv.h>
34 #import <Foundation/Foundation.h>
35 #import "keychain/ckks/CKKS.h"
36 #import "keychain/ckks/CKKSKeychainView.h"
37 #import "keychain/ckks/CKKSViewManager.h"
38 #import "keychain/ckks/CKKSKey.h"
40 #import "keychain/ot/OTManager.h"
42 NSDictionary<CKKSZoneKeyState*, NSNumber*>* CKKSZoneKeyStateMap(void) {
43 static NSDictionary<CKKSZoneKeyState*, NSNumber*>* map = nil;
44 static dispatch_once_t onceToken;
45 dispatch_once(&onceToken, ^{
47 SecCKKSZoneKeyStateReady: @0U,
48 SecCKKSZoneKeyStateError: @1U,
49 SecCKKSZoneKeyStateCancelled: @2U,
51 SecCKKSZoneKeyStateInitializing: @3U,
52 SecCKKSZoneKeyStateInitialized: @4U,
53 SecCKKSZoneKeyStateFetchComplete: @5U,
54 SecCKKSZoneKeyStateWaitForTLK: @6U,
55 SecCKKSZoneKeyStateWaitForUnlock: @7U,
56 SecCKKSZoneKeyStateUnhealthy: @8U,
57 SecCKKSZoneKeyStateBadCurrentPointers: @9U,
58 SecCKKSZoneKeyStateNewTLKsFailed: @10U,
59 SecCKKSZoneKeyStateNeedFullRefetch: @11U,
60 SecCKKSZoneKeyStateHealTLKShares: @12U,
61 SecCKKSZoneKeyStateHealTLKSharesFailed:@13U,
62 SecCKKSZoneKeyStateWaitForFixupOperation:@14U,
63 SecCKKSZoneKeyStateReadyPendingUnlock: @15U,
64 SecCKKSZoneKeyStateFetch: @16U,
65 SecCKKSZoneKeyStateResettingZone: @17U,
66 SecCKKSZoneKeyStateResettingLocalData: @18U,
67 SecCKKSZoneKeyStateLoggedOut: @19U,
68 SecCKKSZoneKeyStateZoneCreationFailed: @20U,
69 SecCKKSZoneKeyStateWaitForTrust: @21U,
70 SecCKKSZoneKeyStateWaitForTLKUpload: @22U,
71 SecCKKSZoneKeyStateWaitForTLKCreation: @23U,
72 SecCKKSZoneKeyStateProcess: @24U,
78 NSDictionary<NSNumber*, CKKSZoneKeyState*>* CKKSZoneKeyStateInverseMap(void) {
79 static NSDictionary<NSNumber*, CKKSZoneKeyState*>* backwardMap = nil;
80 static dispatch_once_t onceToken;
81 dispatch_once(&onceToken, ^{
82 NSDictionary<CKKSZoneKeyState*, NSNumber*>* forwardMap = CKKSZoneKeyStateMap();
83 backwardMap = [NSDictionary dictionaryWithObjects:[forwardMap allKeys] forKeys:[forwardMap allValues]];
88 NSNumber* CKKSZoneKeyToNumber(CKKSZoneKeyState* state) {
90 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
92 NSNumber* result = CKKSZoneKeyStateMap()[state];
96 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
98 CKKSZoneKeyState* CKKSZoneKeyRecover(NSNumber* stateNumber) {
100 return SecCKKSZoneKeyStateError;
102 CKKSZoneKeyState* result = CKKSZoneKeyStateInverseMap()[stateNumber];
106 return SecCKKSZoneKeyStateError;
109 bool CKKSKeyStateTransient(CKKSZoneKeyState* state) {
110 // Easier to compare against a blacklist of end states
111 bool nontransient = [state isEqualToString:SecCKKSZoneKeyStateReady] ||
112 [state isEqualToString:SecCKKSZoneKeyStateReadyPendingUnlock] ||
113 [state isEqualToString:SecCKKSZoneKeyStateWaitForTrust] ||
114 [state isEqualToString:SecCKKSZoneKeyStateWaitForTLK] ||
115 [state isEqualToString:SecCKKSZoneKeyStateWaitForTLKCreation] ||
116 [state isEqualToString:SecCKKSZoneKeyStateWaitForTLKUpload] ||
117 [state isEqualToString:SecCKKSZoneKeyStateWaitForUnlock] ||
118 [state isEqualToString:SecCKKSZoneKeyStateError] ||
119 [state isEqualToString:SecCKKSZoneKeyStateCancelled];
120 return !nontransient;
124 // If you want CKKS to run in your daemon/tests, you must call SecCKKSEnable before bringing up the keychain db
125 static bool enableCKKS = false;
126 static bool testCKKS = false;
128 bool SecCKKSIsEnabled(void) {
129 if([CKDatabase class] == nil) {
130 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
131 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS (on pain of crash).");
138 bool SecCKKSEnable() {
143 bool SecCKKSDisable() {
148 bool SecCKKSResetSyncing(void) {
149 // The function name is a bit of a lie, but it does the thing.
150 [OTManager resetManager:true to:nil];
151 return SecCKKSIsEnabled();
154 bool SecCKKSTestsEnabled(void) {
158 bool SecCKKSTestsEnable(void) {
159 if([CKDatabase class] == nil) {
160 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
161 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS testing.");
170 bool SecCKKSTestsDisable(void) {
175 // Feature flags to twiddle behavior
176 static bool CKKSSyncManifests = false;
177 bool SecCKKSSyncManifests(void) {
178 return CKKSSyncManifests;
180 bool SecCKKSEnableSyncManifests() {
181 CKKSSyncManifests = true;
182 return CKKSSyncManifests;
184 bool SecCKKSSetSyncManifests(bool value) {
185 CKKSSyncManifests = value;
186 return CKKSSyncManifests;
189 static bool CKKSEnforceManifests = false;
190 bool SecCKKSEnforceManifests(void) {
191 return CKKSEnforceManifests;
193 bool SecCKKSEnableEnforceManifests() {
194 CKKSEnforceManifests = true;
195 return CKKSEnforceManifests;
197 bool SecCKKSSetEnforceManifests(bool value) {
198 CKKSEnforceManifests = value;
199 return CKKSEnforceManifests;
202 // defaults write com.apple.security.ckks reduce-rate-limiting YES
203 static bool CKKSReduceRateLimiting = false;
204 bool SecCKKSReduceRateLimiting(void) {
205 static dispatch_once_t onceToken;
206 dispatch_once(&onceToken, ^{
207 // Use the default value as above, or apply the preferences value if it exists
208 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
209 NSString* key = @"reduce-rate-limiting";
210 [defaults registerDefaults: @{key: CKKSReduceRateLimiting ? @YES : @NO}];
212 CKKSReduceRateLimiting = !![defaults boolForKey:@"reduce-rate-limiting"];
213 secnotice("ckks", "reduce-rate-limiting is %@", CKKSReduceRateLimiting ? @"on" : @"off");
216 return CKKSReduceRateLimiting;
219 bool SecCKKSSetReduceRateLimiting(bool value) {
220 (void) SecCKKSReduceRateLimiting(); // Call this once to read the defaults write
221 CKKSReduceRateLimiting = value;
222 secnotice("ckks", "reduce-rate-limiting is now %@", CKKSReduceRateLimiting ? @"on" : @"off");
223 return CKKSReduceRateLimiting;
226 // Here's a mechanism for CKKS feature flags with default values from NSUserDefaults:
227 /*static bool CKKSShareTLKs = true;
228 bool SecCKKSShareTLKs(void) {
230 static dispatch_once_t onceToken;
231 dispatch_once(&onceToken, ^{
232 // Use the default value as above, or apply the preferences value if it exists
233 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
234 [defaults registerDefaults: @{@"tlksharing": CKKSShareTLKs ? @YES : @NO}];
236 CKKSShareTLKs = !![defaults boolForKey:@"tlksharing"];
237 secnotice("ckksshare", "TLK sharing is %@", CKKSShareTLKs ? @"on" : @"off");
240 return CKKSShareTLKs;
243 // Feature flags to twiddle behavior for tests
244 static bool CKKSDisableAutomaticUUID = false;
245 bool SecCKKSTestDisableAutomaticUUID(void) {
247 return CKKSDisableAutomaticUUID;
252 void SecCKKSTestSetDisableAutomaticUUID(bool set) {
253 CKKSDisableAutomaticUUID = set;
256 static bool CKKSDisableSOS = false;
257 bool SecCKKSTestDisableSOS(void) {
259 return CKKSDisableSOS;
264 void SecCKKSTestSetDisableSOS(bool set) {
265 CKKSDisableSOS = set;
269 static bool CKKSDisableKeyNotifications = false;
270 bool SecCKKSTestDisableKeyNotifications(void) {
272 return CKKSDisableKeyNotifications;
277 void SecCKKSTestSetDisableKeyNotifications(bool set) {
278 CKKSDisableKeyNotifications = set;
281 void SecCKKSTestResetFlags(void) {
282 SecCKKSTestSetDisableAutomaticUUID(false);
283 SecCKKSTestSetDisableSOS(false);
284 SecCKKSTestSetDisableKeyNotifications(false);
287 #else /* NO OCTAGON */
289 bool SecCKKSIsEnabled(void) {
290 secerror("CKKS was disabled at compile time.");
294 bool SecCKKSEnable() {
298 bool SecCKKSDisable() {
302 bool SecCKKSResetSyncing(void) {
303 return SecCKKSIsEnabled();
310 void SecCKKSInitialize(SecDbRef db) {
313 CKKSViewManager* manager = [CKKSViewManager manager];
314 [manager createViews];
315 [manager setupAnalytics];
317 SecDbAddNotifyPhaseBlock(db, ^(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
318 SecCKKSNotifyBlock(dbconn, phase, source, changes);
321 [manager.completedSecCKKSInitialize fulfill];
323 if(!SecCKKSTestsEnabled()) {
324 static dispatch_once_t onceToken;
325 dispatch_once(&onceToken, ^{
326 [OctagonAPSReceiver receiverForEnvironment:APSEnvironmentProduction namedDelegatePort:SecCKKSAPSNamedPort apsConnectionClass:[APSConnection class]];
333 void SecCKKSNotifyBlock(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
335 if(phase == kSecDbTransactionDidRollback) {
339 // Ignore our own changes, otherwise we'd infinite-loop.
340 if(source == kSecDbCKKSTransaction) {
341 secinfo("ckks", "Ignoring kSecDbCKKSTransaction notification");
345 CFArrayForEach(changes, ^(CFTypeRef r) {
346 SecDbItemRef deleted = NULL;
347 SecDbItemRef added = NULL;
349 SecDbEventTranslateComponents(r, (CFTypeRef*) &deleted, (CFTypeRef*) &added);
351 if(!added && !deleted) {
352 secerror("CKKS: SecDbEvent gave us garbage: %@", r);
356 [[CKKSViewManager manager] handleKeychainEventDbConnection: dbconn source:source added: added deleted: deleted];
361 void SecCKKS24hrNotification() {
364 [[CKKSViewManager manager] xpc24HrNotification];
369 void CKKSRegisterSyncStatusCallback(CFStringRef cfuuid, SecBoolCFErrorCallback cfcallback) {
371 // Keep plumbing, but transition to NS.
372 SecBoolNSErrorCallback nscallback = ^(bool result, NSError* err) {
373 cfcallback(result, (__bridge CFErrorRef) err);
376 [[CKKSViewManager manager] registerSyncStatusCallback: (__bridge NSString*) cfuuid callback:nscallback];
380 void SecCKKSPerformLocalResync() {
382 if(SecCKKSIsEnabled()) {
383 secnotice("ckks", "Local keychain was reset; performing local resync");
384 [[CKKSViewManager manager] rpcResyncLocal:nil reply:^(NSError *result) {
386 secnotice("ckks", "Local keychain reset resync finished with an error: %@", result);
388 secnotice("ckks", "Local keychain reset resync finished successfully");