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 <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 const SecCKKSItemEncryptionVersion currentCKKSItemEncryptionVersion = CKKSItemEncryptionVersion2;
42 NSString* const SecCKKSActionAdd = @"add";
43 NSString* const SecCKKSActionDelete = @"delete";
44 NSString* const SecCKKSActionModify = @"modify";
46 CKKSItemState* const SecCKKSStateNew = (CKKSItemState*) @"new";
47 CKKSItemState* const SecCKKSStateUnauthenticated = (CKKSItemState*) @"unauthenticated";
48 CKKSItemState* const SecCKKSStateInFlight = (CKKSItemState*) @"inflight";
49 CKKSItemState* const SecCKKSStateReencrypt = (CKKSItemState*) @"reencrypt";
50 CKKSItemState* const SecCKKSStateError = (CKKSItemState*) @"error";
51 CKKSItemState* const SecCKKSStateDeleted = (CKKSItemState*) @"deleted";
53 CKKSProcessedState* const SecCKKSProcessedStateLocal = (CKKSProcessedState*) @"local";
54 CKKSProcessedState* const SecCKKSProcessedStateRemote = (CKKSProcessedState*) @"remote";
56 CKKSKeyClass* const SecCKKSKeyClassTLK = (CKKSKeyClass*) @"tlk";
57 CKKSKeyClass* const SecCKKSKeyClassA = (CKKSKeyClass*) @"classA";
58 CKKSKeyClass* const SecCKKSKeyClassC = (CKKSKeyClass*) @"classC";
60 NSString* const SecCKKSContainerName = @"com.apple.security.keychain";
61 bool SecCKKSContainerUsePCS = false;
63 NSString* const SecCKKSSubscriptionID = @"keychain-changes";
64 NSString* const SecCKKSAPSNamedPort = @"com.apple.securityd.aps";
66 NSString* const SecCKRecordItemType = @"item";
67 NSString* const SecCKRecordHostOSVersionKey = @"uploadver";
68 NSString* const SecCKRecordEncryptionVersionKey = @"encver";
69 NSString* const SecCKRecordDataKey = @"data";
70 NSString* const SecCKRecordParentKeyRefKey = @"parentkeyref";
71 NSString* const SecCKRecordWrappedKeyKey = @"wrappedkey";
72 NSString* const SecCKRecordGenerationCountKey = @"gen";
74 NSString* const SecCKRecordPCSServiceIdentifier = @"pcsservice";
75 NSString* const SecCKRecordPCSPublicKey = @"pcspublickey";
76 NSString* const SecCKRecordPCSPublicIdentity = @"pcspublicidentity";
77 NSString* const SecCKRecordServerWasCurrent = @"server_wascurrent";
79 NSString* const SecCKRecordIntermediateKeyType = @"synckey";
80 NSString* const SecCKRecordKeyClassKey = @"class";
82 NSString* const SecCKRecordTLKShareType = @"tlkshare";
83 NSString* const SecCKRecordSenderPeerID = @"sender";
84 NSString* const SecCKRecordReceiverPeerID = @"receiver";
85 NSString* const SecCKRecordReceiverPublicEncryptionKey = @"receiverPublicEncryptionKey";
86 NSString* const SecCKRecordCurve = @"curve";
87 NSString* const SecCKRecordEpoch = @"epoch";
88 NSString* const SecCKRecordPoisoned = @"poisoned";
89 NSString* const SecCKRecordSignature = @"signature";
90 NSString* const SecCKRecordVersion = @"version";
92 NSString* const SecCKRecordCurrentKeyType = @"currentkey";
94 NSString* const SecCKRecordCurrentItemType = @"currentitem";
95 NSString* const SecCKRecordItemRefKey = @"item";
97 NSString* const SecCKRecordDeviceStateType = @"devicestate";
98 NSString* const SecCKRecordCirclePeerID = @"peerid";
99 NSString* const SecCKRecordCircleStatus = @"circle";
100 NSString* const SecCKRecordKeyState = @"keystate";
101 NSString* const SecCKRecordCurrentTLK = @"currentTLK";
102 NSString* const SecCKRecordCurrentClassA = @"currentClassA";
103 NSString* const SecCKRecordCurrentClassC = @"currentClassC";
105 NSString* const SecCKRecordManifestType = @"manifest";
106 NSString* const SecCKRecordManifestDigestValueKey = @"digest_value";
107 NSString* const SecCKRecordManifestGenerationCountKey = @"generation_count";
108 NSString* const SecCKRecordManifestLeafRecordIDsKey = @"leaf_records";
109 NSString* const SecCKRecordManifestPeerManifestRecordIDsKey = @"peer_manifests";
110 NSString* const SecCKRecordManifestCurrentItemsKey = @"current_items";
111 NSString* const SecCKRecordManifestSignaturesKey = @"signatures";
112 NSString* const SecCKRecordManifestSignerIDKey = @"signer_id";
113 NSString* const SecCKRecordManifestSchemaKey = @"schema";
115 NSString* const SecCKRecordManifestLeafType = @"manifest_leaf";
116 NSString* const SecCKRecordManifestLeafDERKey = @"der";
117 NSString* const SecCKRecordManifestLeafDigestKey = @"digest";
119 CKKSZoneKeyState* const SecCKKSZoneKeyStateReady = (CKKSZoneKeyState*) @"ready";
120 CKKSZoneKeyState* const SecCKKSZoneKeyStateError = (CKKSZoneKeyState*) @"error";
121 CKKSZoneKeyState* const SecCKKSZoneKeyStateCancelled = (CKKSZoneKeyState*) @"cancelled";
123 CKKSZoneKeyState* const SecCKKSZoneKeyStateInitializing = (CKKSZoneKeyState*) @"initializing";
124 CKKSZoneKeyState* const SecCKKSZoneKeyStateInitialized = (CKKSZoneKeyState*) @"initialized";
125 CKKSZoneKeyState* const SecCKKSZoneKeyStateFetchComplete = (CKKSZoneKeyState*) @"fetchcomplete";
126 CKKSZoneKeyState* const SecCKKSZoneKeyStateNeedFullRefetch = (CKKSZoneKeyState*) @"needrefetch";
127 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForTLK = (CKKSZoneKeyState*) @"waitfortlk";
128 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForUnlock = (CKKSZoneKeyState*) @"waitforunlock";
129 CKKSZoneKeyState* const SecCKKSZoneKeyStateUnhealthy = (CKKSZoneKeyState*) @"unhealthy";
130 CKKSZoneKeyState* const SecCKKSZoneKeyStateBadCurrentPointers = (CKKSZoneKeyState*) @"badcurrentpointers";
131 CKKSZoneKeyState* const SecCKKSZoneKeyStateNewTLKsFailed = (CKKSZoneKeyState*) @"newtlksfailed";
132 CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKShares = (CKKSZoneKeyState*) @"healtlkshares";
133 CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKSharesFailed = (CKKSZoneKeyState*) @"healtlksharesfailed";
134 CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForFixupOperation = (CKKSZoneKeyState*) @"waitforfixupoperation";
136 NSDictionary<CKKSZoneKeyState*, NSNumber*>* CKKSZoneKeyStateMap(void) {
137 static NSDictionary<CKKSZoneKeyState*, NSNumber*>* map = nil;
138 static dispatch_once_t onceToken;
139 dispatch_once(&onceToken, ^{
141 SecCKKSZoneKeyStateReady: @0U,
142 SecCKKSZoneKeyStateError: @1U,
143 SecCKKSZoneKeyStateCancelled: @2U,
145 SecCKKSZoneKeyStateInitializing: @3U,
146 SecCKKSZoneKeyStateInitialized: @4U,
147 SecCKKSZoneKeyStateFetchComplete: @5U,
148 SecCKKSZoneKeyStateWaitForTLK: @6U,
149 SecCKKSZoneKeyStateWaitForUnlock: @7U,
150 SecCKKSZoneKeyStateUnhealthy: @8U,
151 SecCKKSZoneKeyStateBadCurrentPointers: @9U,
152 SecCKKSZoneKeyStateNewTLKsFailed: @10U,
153 SecCKKSZoneKeyStateNeedFullRefetch: @11U,
154 SecCKKSZoneKeyStateHealTLKShares: @12U,
155 SecCKKSZoneKeyStateHealTLKSharesFailed:@13U,
156 SecCKKSZoneKeyStateWaitForFixupOperation:@14U,
162 NSDictionary<NSNumber*, CKKSZoneKeyState*>* CKKSZoneKeyStateInverseMap(void) {
163 static NSDictionary<NSNumber*, CKKSZoneKeyState*>* backwardMap = nil;
164 static dispatch_once_t onceToken;
165 dispatch_once(&onceToken, ^{
166 NSDictionary<CKKSZoneKeyState*, NSNumber*>* forwardMap = CKKSZoneKeyStateMap();
167 backwardMap = [NSDictionary dictionaryWithObjects:[forwardMap allKeys] forKeys:[forwardMap allValues]];
172 NSNumber* CKKSZoneKeyToNumber(CKKSZoneKeyState* state) {
174 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
176 NSNumber* result = CKKSZoneKeyStateMap()[state];
180 return CKKSZoneKeyStateMap()[SecCKKSZoneKeyStateError];
182 CKKSZoneKeyState* CKKSZoneKeyRecover(NSNumber* stateNumber) {
184 return SecCKKSZoneKeyStateError;
186 CKKSZoneKeyState* result = CKKSZoneKeyStateInverseMap()[stateNumber];
190 return SecCKKSZoneKeyStateError;
193 const NSUInteger SecCKKSItemPaddingBlockSize = 20;
195 NSString* const SecCKKSAggdPropagationDelay = @"com.apple.security.ckks.propagationdelay";
196 NSString* const SecCKKSAggdPrimaryKeyConflict = @"com.apple.security.ckks.pkconflict";
197 NSString* const SecCKKSAggdViewKeyCount = @"com.apple.security.ckks.keycount";
198 NSString* const SecCKKSAggdItemReencryption = @"com.apple.security.ckks.reencrypt";
200 NSString* const SecCKKSUserDefaultsSuite = @"com.apple.security.ckks";
202 NSString* const CKKSErrorDomain = @"CKKSErrorDomain";
203 NSString* const CKKSServerExtensionErrorDomain = @"CKKSServerExtensionErrorDomain";
206 static bool enableCKKS = true;
207 static bool testCKKS = false;
209 bool SecCKKSIsEnabled(void) {
210 if([CKDatabase class] == nil) {
211 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
212 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS (on pain of crash).");
219 bool SecCKKSEnable() {
224 bool SecCKKSDisable() {
229 bool SecCKKSResetSyncing(void) {
230 [CKKSViewManager resetManager: true setTo: nil];
231 return SecCKKSIsEnabled();
234 bool SecCKKSTestsEnabled(void) {
238 bool SecCKKSTestsEnable(void) {
239 if([CKDatabase class] == nil) {
240 // CloudKit is not linked. We cannot bring CKKS up; disable it with prejudice.
241 secerror("CKKS: CloudKit.framework appears to not be linked. Cannot enable CKKS testing.");
250 bool SecCKKSTestsDisable(void) {
255 // Feature flags to twiddle behavior
256 static bool CKKSSyncManifests = false;
257 bool SecCKKSSyncManifests(void) {
258 return CKKSSyncManifests;
260 bool SecCKKSEnableSyncManifests() {
261 CKKSSyncManifests = true;
262 return CKKSSyncManifests;
264 bool SecCKKSSetSyncManifests(bool value) {
265 CKKSSyncManifests = value;
266 return CKKSSyncManifests;
269 static bool CKKSEnforceManifests = false;
270 bool SecCKKSEnforceManifests(void) {
271 return CKKSEnforceManifests;
273 bool SecCKKSEnableEnforceManifests() {
274 CKKSEnforceManifests = true;
275 return CKKSEnforceManifests;
277 bool SecCKKSSetEnforceManifests(bool value) {
278 CKKSEnforceManifests = value;
279 return CKKSEnforceManifests;
282 static bool CKKSShareTLKs = true;
283 bool SecCKKSShareTLKs(void) {
284 static dispatch_once_t onceToken;
285 dispatch_once(&onceToken, ^{
286 // Use the default value as above, or apply the preferences value if it exists
287 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
288 [defaults registerDefaults: @{@"tlksharing": CKKSShareTLKs ? @YES : @NO}];
290 CKKSShareTLKs = !![defaults boolForKey:@"tlksharing"];
291 secnotice("ckksshare", "TLK sharing is %@", CKKSShareTLKs ? @"on" : @"off");
294 return CKKSShareTLKs;
296 bool SecCKKSEnableShareTLKs(void) {
297 return SecCKKSSetShareTLKs(true);
299 bool SecCKKSSetShareTLKs(bool value) {
300 // Call this to do the dispatch_once first
303 CKKSShareTLKs = value;
304 return CKKSShareTLKs;
307 // Feature flags to twiddle behavior for tests
308 static bool CKKSDisableAutomaticUUID = false;
309 bool SecCKKSTestDisableAutomaticUUID(void) {
311 return CKKSDisableAutomaticUUID;
316 void SecCKKSTestSetDisableAutomaticUUID(bool set) {
317 CKKSDisableAutomaticUUID = set;
320 static bool CKKSDisableSOS = false;
321 bool SecCKKSTestDisableSOS(void) {
323 return CKKSDisableSOS;
328 void SecCKKSTestSetDisableSOS(bool set) {
329 CKKSDisableSOS = set;
333 static bool CKKSDisableKeyNotifications = false;
334 bool SecCKKSTestDisableKeyNotifications(void) {
336 return CKKSDisableKeyNotifications;
341 void SecCKKSTestSetDisableKeyNotifications(bool set) {
342 CKKSDisableKeyNotifications = set;
345 void SecCKKSTestResetFlags(void) {
346 SecCKKSTestSetDisableAutomaticUUID(false);
347 SecCKKSTestSetDisableSOS(false);
348 SecCKKSTestSetDisableKeyNotifications(false);
351 XPC_RETURNS_RETAINED xpc_endpoint_t
352 SecServerCreateCKKSEndpoint(void)
354 if (SecCKKSIsEnabled()) {
355 return [[CKKSViewManager manager] xpcControlEndpoint];
361 #else /* NO OCTAGON */
363 bool SecCKKSIsEnabled(void) {
364 secerror("CKKS was disabled at compile time.");
368 bool SecCKKSEnable() {
372 bool SecCKKSDisable() {
376 bool SecCKKSResetSyncing(void) {
377 return SecCKKSIsEnabled();
380 XPC_RETURNS_RETAINED xpc_endpoint_t
381 SecServerCreateCKKSEndpoint(void)
389 void SecCKKSInitialize(SecDbRef db) {
391 CKKSViewManager* manager = [CKKSViewManager manager];
392 [manager initializeZones];
394 SecDbAddNotifyPhaseBlock(db, ^(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
395 SecCKKSNotifyBlock(dbconn, phase, source, changes);
398 [manager.completedSecCKKSInitialize fulfill];
402 void SecCKKSNotifyBlock(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes) {
404 if(phase == kSecDbTransactionDidRollback) {
408 // Ignore our own changes, otherwise we'd infinite-loop.
409 if(source == kSecDbCKKSTransaction) {
410 secinfo("ckks", "Ignoring kSecDbCKKSTransaction notification");
414 CFArrayForEach(changes, ^(CFTypeRef r) {
415 SecDbItemRef deleted = NULL;
416 SecDbItemRef added = NULL;
418 SecDbEventTranslateComponents(r, (CFTypeRef*) &deleted, (CFTypeRef*) &added);
420 if(!added && !deleted) {
421 secerror("CKKS: SecDbEvent gave us garbage: %@", r);
425 [[CKKSViewManager manager] handleKeychainEventDbConnection: dbconn source:source added: added deleted: deleted];
430 void SecCKKS24hrNotification() {
433 [[CKKSViewManager manager] xpc24HrNotification];
438 void CKKSRegisterSyncStatusCallback(CFStringRef cfuuid, SecBoolCFErrorCallback cfcallback) {
440 // Keep plumbing, but transition to NS.
441 SecBoolNSErrorCallback nscallback = ^(bool result, NSError* err) {
442 cfcallback(result, (__bridge CFErrorRef) err);
445 [[CKKSViewManager manager] registerSyncStatusCallback: (__bridge NSString*) cfuuid callback:nscallback];