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>
26 #include <sys/sysctl.h>
28 #import <CloudKit/CloudKit.h>
31 #include <utilities/debugging.h>
32 #include <securityd/SecItemServer.h>
33 #include <Security/SecItemPriv.h>
35 #import <Foundation/Foundation.h>
36 #import "keychain/ckks/CKKS.h"
37 #import "keychain/ckks/CKKSKeychainView.h"
38 #import "keychain/ckks/CKKSViewManager.h"
39 #import "keychain/ckks/CKKSKey.h"
41 #import "keychain/ot/OTManager.h"
42 const SecCKKSItemEncryptionVersion currentCKKSItemEncryptionVersion = CKKSItemEncryptionVersion2;
44 NSString* const SecCKKSActionAdd = @"add";
45 NSString* const SecCKKSActionDelete = @"delete";
46 NSString* const SecCKKSActionModify = @"modify";
48 CKKSItemState* const SecCKKSStateNew = (CKKSItemState*) @"new";
49 CKKSItemState* const SecCKKSStateUnauthenticated = (CKKSItemState*) @"unauthenticated";
50 CKKSItemState* const SecCKKSStateInFlight = (CKKSItemState*) @"inflight";
51 CKKSItemState* const SecCKKSStateReencrypt = (CKKSItemState*) @"reencrypt";
52 CKKSItemState* const SecCKKSStateError = (CKKSItemState*) @"error";
53 CKKSItemState* const SecCKKSStateDeleted = (CKKSItemState*) @"deleted";
55 CKKSProcessedState* const SecCKKSProcessedStateLocal = (CKKSProcessedState*) @"local";
56 CKKSProcessedState* const SecCKKSProcessedStateRemote = (CKKSProcessedState*) @"remote";
58 CKKSKeyClass* const SecCKKSKeyClassTLK = (CKKSKeyClass*) @"tlk";
59 CKKSKeyClass* const SecCKKSKeyClassA = (CKKSKeyClass*) @"classA";
60 CKKSKeyClass* const SecCKKSKeyClassC = (CKKSKeyClass*) @"classC";
62 NSString* const SecCKKSContainerName = @"com.apple.security.keychain";
63 bool SecCKKSContainerUsePCS = false;
65 NSString* const SecCKKSSubscriptionID = @"keychain-changes";
66 NSString* const SecCKKSAPSNamedPort = @"com.apple.securityd.aps";
68 NSString* const SecCKRecordItemType = @"item";
69 NSString* const SecCKRecordHostOSVersionKey = @"uploadver";
70 NSString* const SecCKRecordEncryptionVersionKey = @"encver";
71 NSString* const SecCKRecordDataKey = @"data";
72 NSString* const SecCKRecordParentKeyRefKey = @"parentkeyref";
73 NSString* const SecCKRecordWrappedKeyKey = @"wrappedkey";
74 NSString* const SecCKRecordGenerationCountKey = @"gen";
76 NSString* const SecCKRecordPCSServiceIdentifier = @"pcsservice";
77 NSString* const SecCKRecordPCSPublicKey = @"pcspublickey";
78 NSString* const SecCKRecordPCSPublicIdentity = @"pcspublicidentity";
79 NSString* const SecCKRecordServerWasCurrent = @"server_wascurrent";
81 NSString* const SecCKRecordIntermediateKeyType = @"synckey";
82 NSString* const SecCKRecordKeyClassKey = @"class";
84 NSString* const SecCKRecordTLKShareType = @"tlkshare";
85 NSString* const SecCKRecordSenderPeerID = @"sender";
86 NSString* const SecCKRecordReceiverPeerID = @"receiver";
87 NSString* const SecCKRecordReceiverPublicEncryptionKey = @"receiverPublicEncryptionKey";
88 NSString* const SecCKRecordCurve = @"curve";
89 NSString* const SecCKRecordEpoch = @"epoch";
90 NSString* const SecCKRecordPoisoned = @"poisoned";
91 NSString* const SecCKRecordSignature = @"signature";
92 NSString* const SecCKRecordVersion = @"version";
94 NSString* const SecCKRecordCurrentKeyType = @"currentkey";
96 NSString* const SecCKRecordCurrentItemType = @"currentitem";
97 NSString* const SecCKRecordItemRefKey = @"item";
99 NSString* const SecCKRecordDeviceStateType = @"devicestate";
100 NSString* const SecCKRecordCirclePeerID = @"peerid";
101 NSString* const SecCKRecordCircleStatus = @"circle";
102 NSString* const SecCKRecordKeyState = @"keystate";
103 NSString* const SecCKRecordCurrentTLK = @"currentTLK";
104 NSString* const SecCKRecordCurrentClassA = @"currentClassA";
105 NSString* const SecCKRecordCurrentClassC = @"currentClassC";
106 NSString* const SecCKSRecordLastUnlockTime = @"lastunlock";
107 NSString* const SecCKSRecordOSVersionKey = @"osver";
109 NSString* const SecCKRecordManifestType = @"manifest";
110 NSString* const SecCKRecordManifestDigestValueKey = @"digest_value";
111 NSString* const SecCKRecordManifestGenerationCountKey = @"generation_count";
112 NSString* const SecCKRecordManifestLeafRecordIDsKey = @"leaf_records";
113 NSString* const SecCKRecordManifestPeerManifestRecordIDsKey = @"peer_manifests";
114 NSString* const SecCKRecordManifestCurrentItemsKey = @"current_items";
115 NSString* const SecCKRecordManifestSignaturesKey = @"signatures";
116 NSString* const SecCKRecordManifestSignerIDKey = @"signer_id";
117 NSString* const SecCKRecordManifestSchemaKey = @"schema";
119 NSString* const SecCKRecordManifestLeafType = @"manifest_leaf";
120 NSString* const SecCKRecordManifestLeafDERKey = @"der";
121 NSString* const SecCKRecordManifestLeafDigestKey = @"digest";
123 CKKSZoneKeyState* const SecCKKSZoneKeyStateReady = (CKKSZoneKeyState*) @"ready";
124 CKKSZoneKeyState* const SecCKKSZoneKeyStateReadyPendingUnlock = (CKKSZoneKeyState*) @"readypendingunlock";
125 CKKSZoneKeyState* const SecCKKSZoneKeyStateError = (CKKSZoneKeyState*) @"error";
126 CKKSZoneKeyState* const SecCKKSZoneKeyStateCancelled = (CKKSZoneKeyState*) @"cancelled";
128 CKKSZoneKeyState* const SecCKKSZoneKeyStateInitializing = (CKKSZoneKeyState*) @"initializing";
129 CKKSZoneKeyState* const SecCKKSZoneKeyStateInitialized = (CKKSZoneKeyState*) @"initialized";
130 CKKSZoneKeyState* const SecCKKSZoneKeyStateFetch = (CKKSZoneKeyState*) @"fetching";
131 CKKSZoneKeyState* const SecCKKSZoneKeyStateFetchComplete = (CKKSZoneKeyState*) @"fetchcomplete";
132 CKKSZoneKeyState* const SecCKKSZoneKeyStateNeedFullRefetch = (CKKSZoneKeyState*) @"needrefetch";
133 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForTLK = (CKKSZoneKeyState*) @"waitfortlk";
134 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForUnlock = (CKKSZoneKeyState*) @"waitforunlock";
135 CKKSZoneKeyState* const SecCKKSZoneKeyStateUnhealthy = (CKKSZoneKeyState*) @"unhealthy";
136 CKKSZoneKeyState* const SecCKKSZoneKeyStateBadCurrentPointers = (CKKSZoneKeyState*) @"badcurrentpointers";
137 CKKSZoneKeyState* const SecCKKSZoneKeyStateNewTLKsFailed = (CKKSZoneKeyState*) @"newtlksfailed";
138 CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKShares = (CKKSZoneKeyState*) @"healtlkshares";
139 CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKSharesFailed = (CKKSZoneKeyState*) @"healtlksharesfailed";
140 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForFixupOperation = (CKKSZoneKeyState*) @"waitforfixupoperation";
141 CKKSZoneKeyState* const SecCKKSZoneKeyStateResettingZone = (CKKSZoneKeyState*) @"resetzone";
142 CKKSZoneKeyState* const SecCKKSZoneKeyStateResettingLocalData = (CKKSZoneKeyState*) @"resetlocal";
143 CKKSZoneKeyState* const SecCKKSZoneKeyStateLoggedOut = (CKKSZoneKeyState*) @"loggedout";
144 CKKSZoneKeyState* const SecCKKSZoneKeyStateZoneCreationFailed = (CKKSZoneKeyState*) @"zonecreationfailed";
146 NSDictionary<CKKSZoneKeyState*, NSNumber*>* CKKSZoneKeyStateMap(void) {
147 static NSDictionary<CKKSZoneKeyState*, NSNumber*>* map = nil;
148 static dispatch_once_t onceToken;
149 dispatch_once(&onceToken, ^{
151 SecCKKSZoneKeyStateReady: @0U,
152 SecCKKSZoneKeyStateError: @1U,
153 SecCKKSZoneKeyStateCancelled: @2U,
155 SecCKKSZoneKeyStateInitializing: @3U,
156 SecCKKSZoneKeyStateInitialized: @4U,
157 SecCKKSZoneKeyStateFetchComplete: @5U,
158 SecCKKSZoneKeyStateWaitForTLK: @6U,
159 SecCKKSZoneKeyStateWaitForUnlock: @7U,
160 SecCKKSZoneKeyStateUnhealthy: @8U,
161 SecCKKSZoneKeyStateBadCurrentPointers: @9U,
162 SecCKKSZoneKeyStateNewTLKsFailed: @10U,
163 SecCKKSZoneKeyStateNeedFullRefetch: @11U,
164 SecCKKSZoneKeyStateHealTLKShares: @12U,
165 SecCKKSZoneKeyStateHealTLKSharesFailed:@13U,
166 SecCKKSZoneKeyStateWaitForFixupOperation:@14U,
167 SecCKKSZoneKeyStateReadyPendingUnlock: @15U,
168 SecCKKSZoneKeyStateFetch: @16U,
169 SecCKKSZoneKeyStateResettingZone: @17U,
170 SecCKKSZoneKeyStateResettingLocalData: @18U,
171 SecCKKSZoneKeyStateLoggedOut: @19U,
172 SecCKKSZoneKeyStateZoneCreationFailed: @20U,
178 NSDictionary<NSNumber*, CKKSZoneKeyState*>* CKKSZoneKeyStateInverseMap(void) {
179 static NSDictionary<NSNumber*, CKKSZoneKeyState*>* backwardMap = nil;
180 static dispatch_once_t onceToken;
181 dispatch_once(&onceToken, ^{
182 NSDictionary<CKKSZoneKeyState*, NSNumber*>* forwardMap = CKKSZoneKeyStateMap();
183 backwardMap = [NSDictionary dictionaryWithObjects:[forwardMap allKeys] forKeys:[forwardMap allValues]];
188 NSNumber* CKKSZoneKeyToNumber(CKKSZoneKeyState* state) {
190 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
192 NSNumber* result = CKKSZoneKeyStateMap()[state];
196 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
198 CKKSZoneKeyState* CKKSZoneKeyRecover(NSNumber* stateNumber) {
200 return SecCKKSZoneKeyStateError;
202 CKKSZoneKeyState* result = CKKSZoneKeyStateInverseMap()[stateNumber];
206 return SecCKKSZoneKeyStateError;
209 bool CKKSKeyStateTransient(CKKSZoneKeyState* state) {
210 // Easier to compare against a blacklist of end states
211 bool nontransient = [state isEqualToString:SecCKKSZoneKeyStateReady] ||
212 [state isEqualToString:SecCKKSZoneKeyStateReadyPendingUnlock] ||
213 [state isEqualToString:SecCKKSZoneKeyStateWaitForTLK] ||
214 [state isEqualToString:SecCKKSZoneKeyStateWaitForUnlock] ||
215 [state isEqualToString:SecCKKSZoneKeyStateError] ||
216 [state isEqualToString:SecCKKSZoneKeyStateCancelled];
217 return !nontransient;
220 const NSUInteger SecCKKSItemPaddingBlockSize = 20;
222 NSString* const SecCKKSAggdPropagationDelay = @"com.apple.security.ckks.propagationdelay";
223 NSString* const SecCKKSAggdPrimaryKeyConflict = @"com.apple.security.ckks.pkconflict";
224 NSString* const SecCKKSAggdViewKeyCount = @"com.apple.security.ckks.keycount";
225 NSString* const SecCKKSAggdItemReencryption = @"com.apple.security.ckks.reencrypt";
227 NSString* const SecCKKSUserDefaultsSuite = @"com.apple.security.ckks";
229 NSString* const CKKSErrorDomain = @"CKKSErrorDomain";
230 NSString* const CKKSServerExtensionErrorDomain = @"CKKSServerExtensionErrorDomain";
233 static bool enableCKKS = true;
234 static bool testCKKS = false;
236 bool SecCKKSIsEnabled(void) {
237 if([CKDatabase class] == nil) {
238 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
239 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS (on pain of crash).");
246 bool SecCKKSEnable() {
251 bool SecCKKSDisable() {
256 bool SecCKKSResetSyncing(void) {
257 [CKKSViewManager resetManager: true setTo: nil];
258 return SecCKKSIsEnabled();
261 bool SecCKKSTestsEnabled(void) {
265 bool SecCKKSTestsEnable(void) {
266 if([CKDatabase class] == nil) {
267 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
268 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS testing.");
277 bool SecCKKSTestsDisable(void) {
282 // Feature flags to twiddle behavior
283 static bool CKKSSyncManifests = false;
284 bool SecCKKSSyncManifests(void) {
285 return CKKSSyncManifests;
287 bool SecCKKSEnableSyncManifests() {
288 CKKSSyncManifests = true;
289 return CKKSSyncManifests;
291 bool SecCKKSSetSyncManifests(bool value) {
292 CKKSSyncManifests = value;
293 return CKKSSyncManifests;
296 static bool CKKSEnforceManifests = false;
297 bool SecCKKSEnforceManifests(void) {
298 return CKKSEnforceManifests;
300 bool SecCKKSEnableEnforceManifests() {
301 CKKSEnforceManifests = true;
302 return CKKSEnforceManifests;
304 bool SecCKKSSetEnforceManifests(bool value) {
305 CKKSEnforceManifests = value;
306 return CKKSEnforceManifests;
309 // defaults write com.apple.security.ckks reduce-rate-limiting YES
310 static bool CKKSReduceRateLimiting = false;
311 bool SecCKKSReduceRateLimiting(void) {
312 static dispatch_once_t onceToken;
313 dispatch_once(&onceToken, ^{
314 // Use the default value as above, or apply the preferences value if it exists
315 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
316 NSString* key = @"reduce-rate-limiting";
317 [defaults registerDefaults: @{key: CKKSReduceRateLimiting ? @YES : @NO}];
319 CKKSReduceRateLimiting = !![defaults boolForKey:@"reduce-rate-limiting"];
320 secnotice("ckks", "reduce-rate-limiting is %@", CKKSReduceRateLimiting ? @"on" : @"off");
323 return CKKSReduceRateLimiting;
326 bool SecCKKSSetReduceRateLimiting(bool value) {
327 (void) SecCKKSReduceRateLimiting(); // Call this once to read the defaults write
328 CKKSReduceRateLimiting = value;
329 secnotice("ckks", "reduce-rate-limiting is now %@", CKKSReduceRateLimiting ? @"on" : @"off");
330 return CKKSReduceRateLimiting;
333 // Here's a mechanism for CKKS feature flags with default values from NSUserDefaults:
334 /*static bool CKKSShareTLKs = true;
335 bool SecCKKSShareTLKs(void) {
337 static dispatch_once_t onceToken;
338 dispatch_once(&onceToken, ^{
339 // Use the default value as above, or apply the preferences value if it exists
340 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
341 [defaults registerDefaults: @{@"tlksharing": CKKSShareTLKs ? @YES : @NO}];
343 CKKSShareTLKs = !![defaults boolForKey:@"tlksharing"];
344 secnotice("ckksshare", "TLK sharing is %@", CKKSShareTLKs ? @"on" : @"off");
347 return CKKSShareTLKs;
350 // Feature flags to twiddle behavior for tests
351 static bool CKKSDisableAutomaticUUID = false;
352 bool SecCKKSTestDisableAutomaticUUID(void) {
354 return CKKSDisableAutomaticUUID;
359 void SecCKKSTestSetDisableAutomaticUUID(bool set) {
360 CKKSDisableAutomaticUUID = set;
363 static bool CKKSDisableSOS = false;
364 bool SecCKKSTestDisableSOS(void) {
366 return CKKSDisableSOS;
371 void SecCKKSTestSetDisableSOS(bool set) {
372 CKKSDisableSOS = set;
376 static bool CKKSDisableKeyNotifications = false;
377 bool SecCKKSTestDisableKeyNotifications(void) {
379 return CKKSDisableKeyNotifications;
384 void SecCKKSTestSetDisableKeyNotifications(bool set) {
385 CKKSDisableKeyNotifications = set;
388 void SecCKKSTestResetFlags(void) {
389 SecCKKSTestSetDisableAutomaticUUID(false);
390 SecCKKSTestSetDisableSOS(false);
391 SecCKKSTestSetDisableKeyNotifications(false);
394 #else /* NO OCTAGON */
396 bool SecCKKSIsEnabled(void) {
397 secerror("CKKS was disabled at compile time.");
401 bool SecCKKSEnable() {
405 bool SecCKKSDisable() {
409 bool SecCKKSResetSyncing(void) {
410 return SecCKKSIsEnabled();
417 void SecCKKSInitialize(SecDbRef db) {
420 CKKSViewManager* manager = [CKKSViewManager manager];
421 [manager initializeZones];
423 SecDbAddNotifyPhaseBlock(db, ^(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
424 SecCKKSNotifyBlock(dbconn, phase, source, changes);
427 [manager.completedSecCKKSInitialize fulfill];
432 void SecCKKSNotifyBlock(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
434 if(phase == kSecDbTransactionDidRollback) {
438 // Ignore our own changes, otherwise we'd infinite-loop.
439 if(source == kSecDbCKKSTransaction) {
440 secinfo("ckks", "Ignoring kSecDbCKKSTransaction notification");
444 CFArrayForEach(changes, ^(CFTypeRef r) {
445 SecDbItemRef deleted = NULL;
446 SecDbItemRef added = NULL;
448 SecDbEventTranslateComponents(r, (CFTypeRef*) &deleted, (CFTypeRef*) &added);
450 if(!added && !deleted) {
451 secerror("CKKS: SecDbEvent gave us garbage: %@", r);
455 [[CKKSViewManager manager] handleKeychainEventDbConnection: dbconn source:source added: added deleted: deleted];
460 void SecCKKS24hrNotification() {
463 [[CKKSViewManager manager] xpc24HrNotification];
468 void CKKSRegisterSyncStatusCallback(CFStringRef cfuuid, SecBoolCFErrorCallback cfcallback) {
470 // Keep plumbing, but transition to NS.
471 SecBoolNSErrorCallback nscallback = ^(bool result, NSError* err) {
472 cfcallback(result, (__bridge CFErrorRef) err);
475 [[CKKSViewManager manager] registerSyncStatusCallback: (__bridge NSString*) cfuuid callback:nscallback];
479 void SecCKKSPerformLocalResync() {
481 secnotice("ckks", "Local keychain was reset; performing local resync");
482 [[CKKSViewManager manager] rpcResyncLocal:nil reply:^(NSError *result) {
484 secnotice("ckks", "Local keychain reset resync finished with an error: %@", result);
486 secnotice("ckks", "Local keychain reset resync finished successfully");
492 NSString* SecCKKSHostOSVersion()
495 // Use complicated macro magic to get the string value passed in as preprocessor define PLATFORM.
496 #define PLATFORM_VALUE(f) #f
497 #define PLATFORM_OBJCSTR(f) @PLATFORM_VALUE(f)
498 NSString* platform = (PLATFORM_OBJCSTR(PLATFORM));
499 #undef PLATFORM_OBJCSTR
500 #undef PLATFORM_VALUE
502 NSString* platform = "unknown";
503 #warning No PLATFORM defined; why?
506 NSString* osversion = nil;
508 // If we can get the build information from sysctl, use it.
510 size_t releasesize = sizeof(release);
511 bool haveSysctlInfo = true;
512 haveSysctlInfo &= (0 == sysctlbyname("kern.osrelease", release, &releasesize, NULL, 0));
515 size_t versionsize = sizeof(version);
516 haveSysctlInfo &= (0 == sysctlbyname("kern.osversion", version, &versionsize, NULL, 0));
519 // Null-terminate for extra safety
520 release[sizeof(release)-1] = '\0';
521 version[sizeof(version)-1] = '\0';
522 osversion = [NSString stringWithFormat:@"%s (%s)", release, version];
526 // Otherwise, use the not-really-supported fallback.
527 osversion = [[NSProcessInfo processInfo] operatingSystemVersionString];
529 // subtly improve osversion (but it's okay if that does nothing)
530 osversion = [osversion stringByReplacingOccurrencesOfString:@"Version" withString:@""];
533 return [NSString stringWithFormat:@"%@ %@", platform, osversion];