]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKS.h
Security-58286.60.28.tar.gz
[apple/security.git] / keychain / ckks / CKKS.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 #ifndef CKKS_h
25 #define CKKS_h
26
27 #include <dispatch/dispatch.h>
28 #include <ipc/securityd_client.h>
29 #include <utilities/SecCFWrappers.h>
30 #include <utilities/SecDb.h>
31 #include <xpc/xpc.h>
32
33 #ifdef __OBJC__
34 #import <Foundation/Foundation.h>
35 NS_ASSUME_NONNULL_BEGIN
36 #else
37 CF_ASSUME_NONNULL_BEGIN
38 #endif
39
40 #ifdef __OBJC__
41
42 typedef NS_ENUM(NSUInteger, SecCKKSItemEncryptionVersion) {
43 CKKSItemEncryptionVersionNone = 0, // No encryption present
44 CKKSItemEncryptionVersion1 = 1, // Current version, AES-SIV 512, not all fields authenticated
45 CKKSItemEncryptionVersion2 = 2, // Seed3 version, AES-SIV 512, all fields (including unknown fields) authenticated
46 };
47
48 extern const SecCKKSItemEncryptionVersion currentCKKSItemEncryptionVersion;
49
50 /* Queue Actions */
51 extern NSString* const SecCKKSActionAdd;
52 extern NSString* const SecCKKSActionDelete;
53 extern NSString* const SecCKKSActionModify;
54
55 /* Queue States */
56 @protocol SecCKKSItemState <NSObject>
57 @end
58 typedef NSString<SecCKKSItemState> CKKSItemState;
59 extern CKKSItemState* const SecCKKSStateNew;
60 extern CKKSItemState* const SecCKKSStateUnauthenticated;
61 extern CKKSItemState* const SecCKKSStateInFlight;
62 extern CKKSItemState* const SecCKKSStateReencrypt;
63 extern CKKSItemState* const SecCKKSStateError;
64 extern CKKSItemState* const SecCKKSStateDeleted; // meta-state: please delete this item!
65
66 /* Processed States */
67 @protocol SecCKKSProcessedState <NSObject>
68 @end
69 typedef NSString<SecCKKSProcessedState> CKKSProcessedState;
70 extern CKKSProcessedState* const SecCKKSProcessedStateLocal;
71 extern CKKSProcessedState* const SecCKKSProcessedStateRemote;
72
73 /* Key Classes */
74 @protocol SecCKKSKeyClass <NSObject>
75 @end
76 typedef NSString<SecCKKSKeyClass> CKKSKeyClass;
77 extern CKKSKeyClass* const SecCKKSKeyClassTLK;
78 extern CKKSKeyClass* const SecCKKSKeyClassA;
79 extern CKKSKeyClass* const SecCKKSKeyClassC;
80
81 /* Useful CloudKit configuration */
82 extern NSString* const SecCKKSContainerName;
83 extern bool SecCKKSContainerUsePCS;
84 extern NSString* const SecCKKSSubscriptionID;
85 extern NSString* const SecCKKSAPSNamedPort;
86
87 /* Item CKRecords */
88 extern NSString* const SecCKRecordItemType;
89 extern NSString* const SecCKRecordHostOSVersionKey;
90 extern NSString* const SecCKRecordEncryptionVersionKey;
91 extern NSString* const SecCKRecordParentKeyRefKey;
92 extern NSString* const SecCKRecordDataKey;
93 extern NSString* const SecCKRecordWrappedKeyKey;
94 extern NSString* const SecCKRecordGenerationCountKey;
95 extern NSString* const SecCKRecordPCSServiceIdentifier;
96 extern NSString* const SecCKRecordPCSPublicKey;
97 extern NSString* const SecCKRecordPCSPublicIdentity;
98 extern NSString* const SecCKRecordServerWasCurrent;
99
100 /* Intermediate Key CKRecord Keys */
101 extern NSString* const SecCKRecordIntermediateKeyType;
102 extern NSString* const SecCKRecordKeyClassKey;
103 //extern NSString* const SecCKRecordWrappedKeyKey;
104 //extern NSString* const SecCKRecordParentKeyRefKey;
105
106 /* TLK Share CKRecord Keys */
107 // These are a bit special; they can't use the record ID as information without parsing.
108 extern NSString* const SecCKRecordTLKShareType;
109 extern NSString* const SecCKRecordSenderPeerID;
110 extern NSString* const SecCKRecordReceiverPeerID;
111 extern NSString* const SecCKRecordReceiverPublicEncryptionKey;
112 extern NSString* const SecCKRecordCurve;
113 extern NSString* const SecCKRecordEpoch;
114 extern NSString* const SecCKRecordPoisoned;
115 extern NSString* const SecCKRecordSignature;
116 extern NSString* const SecCKRecordVersion;
117 //extern NSString* const SecCKRecordParentKeyRefKey; // reference to the key contained by this record
118 //extern NSString* const SecCKRecordWrappedKeyKey; // key material
119
120 /* Current Key CKRecord Keys */
121 extern NSString* const SecCKRecordCurrentKeyType;
122 // The key class will be the record name.
123 //extern NSString* const SecCKRecordParentKeyRefKey; <-- represent the current key for this key class
124
125 /* Current Item CKRecord Keys */
126 extern NSString* const SecCKRecordCurrentItemType;
127 extern NSString* const SecCKRecordItemRefKey;
128
129
130 /* Device State CKRecord Keys */
131 extern NSString* const SecCKRecordDeviceStateType;
132 extern NSString* const SecCKRecordCirclePeerID;
133 extern NSString* const SecCKRecordCircleStatus;
134 extern NSString* const SecCKRecordKeyState;
135 extern NSString* const SecCKRecordCurrentTLK;
136 extern NSString* const SecCKRecordCurrentClassA;
137 extern NSString* const SecCKRecordCurrentClassC;
138 extern NSString* const SecCKSRecordLastUnlockTime;
139 extern NSString* const SecCKSRecordOSVersionKey; // Similar to SecCKRecordHostOSVersionKey, but better named
140
141 /* Manifest master CKRecord Keys */
142 extern NSString* const SecCKRecordManifestType;
143 extern NSString* const SecCKRecordManifestDigestValueKey;
144 extern NSString* const SecCKRecordManifestGenerationCountKey;
145 extern NSString* const SecCKRecordManifestLeafRecordIDsKey;
146 extern NSString* const SecCKRecordManifestPeerManifestRecordIDsKey;
147 extern NSString* const SecCKRecordManifestCurrentItemsKey;
148 extern NSString* const SecCKRecordManifestSignaturesKey;
149 extern NSString* const SecCKRecordManifestSignerIDKey;
150 extern NSString* const SecCKRecordManifestSchemaKey;
151
152 /* Manifest leaf CKRecord Keys */
153 extern NSString* const SecCKRecordManifestLeafType;
154 extern NSString* const SecCKRecordManifestLeafDERKey;
155 extern NSString* const SecCKRecordManifestLeafDigestKey;
156
157 /* Zone Key Hierarchy States */
158 @protocol SecCKKSZoneKeyState <NSObject>
159 @end
160 typedef NSString<SecCKKSZoneKeyState> CKKSZoneKeyState;
161
162 // CKKS is currently logged out
163 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateLoggedOut;
164
165 // Class has just been created.
166 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateInitializing;
167 // CKKSZone has just informed us that its setup is done (and completed successfully).
168 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateInitialized;
169 // CKKSZone has informed us that zone setup did not work. Try again soon!
170 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateZoneCreationFailed;
171 // Everything is ready and waiting for input.
172 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateReady;
173 // We're presumably ready, but we'd like to do one or two more checks after we unlock.
174 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateReadyPendingUnlock;
175
176 // We're currently refetching the zone
177 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateFetch;
178 // A Fetch has just been completed which includes some new keys to process
179 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateFetchComplete;
180 // We'd really like a full refetch.
181 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateNeedFullRefetch;
182 // We've received a wrapped TLK, but we don't have its contents yet. Wait until they arrive.
183 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForTLK;
184 // We've received a wrapped TLK, but we can't process it until the keybag unlocks. Wait until then.
185 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForUnlock;
186 // Things are unhealthy, but we're not sure entirely why.
187 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateUnhealthy;
188 // Something has gone horribly wrong with the current key pointers.
189 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateBadCurrentPointers;
190 // Something has gone wrong creating new TLKs.
191 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateNewTLKsFailed;
192 // Something isn't quite right with the TLK shares.
193 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKShares;
194 // Something has gone wrong fixing TLK shares.
195 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateHealTLKSharesFailed;
196 // The key hierarchy state machine needs to wait for the fixup operation to complete
197 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateWaitForFixupOperation;
198
199 // CKKS is resetting the remote zone, due to key hierarchy reasons. Will not proceed until the local reset occurs.
200 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateResettingZone;
201 // CKKS is resetting the local data, likely to do a cloudkit reset or a rpc.
202 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateResettingLocalData;
203
204 // Fatal error. Will not proceed unless fixed from outside class.
205 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateError;
206 // This CKKS instance has been cancelled.
207 extern CKKSZoneKeyState* const SecCKKSZoneKeyStateCancelled;
208
209 // If you absolutely need to numberify one of the above constants, here's your maps.
210 NSDictionary<CKKSZoneKeyState*, NSNumber*>* CKKSZoneKeyStateMap(void);
211 NSDictionary<NSNumber*, CKKSZoneKeyState*>* CKKSZoneKeyStateInverseMap(void);
212 NSNumber* CKKSZoneKeyToNumber(CKKSZoneKeyState* state);
213 CKKSZoneKeyState* CKKSZoneKeyRecover(NSNumber* stateNumber);
214
215 // Use this to determine if CKKS believes the current state is "transient": that is, should resolve itself with further local processing
216 // or 'nontransient': further local processing won't progress. Either we're ready, or waiting for the user to unlock, or a remote device to do something.
217 bool CKKSKeyStateTransient(CKKSZoneKeyState* state);
218
219 /* Hide Item Length */
220 extern const NSUInteger SecCKKSItemPaddingBlockSize;
221
222 /* Aggd Keys */
223 extern NSString* const SecCKKSAggdPropagationDelay;
224 extern NSString* const SecCKKSAggdPrimaryKeyConflict;
225 extern NSString* const SecCKKSAggdViewKeyCount;
226 extern NSString* const SecCKKSAggdItemReencryption;
227
228 extern NSString* const SecCKKSUserDefaultsSuite;
229
230 extern NSString* const CKKSErrorDomain;
231 extern NSString* const CKKSServerExtensionErrorDomain;
232
233 /* Queue limits: these should likely be configurable via plist */
234 #define SecCKKSOutgoingQueueItemsAtOnce 100
235 #define SecCKKSIncomingQueueItemsAtOnce 10
236
237 // Utility functions
238 NSString* SecCKKSHostOSVersion(void);
239
240 #endif // OBJ-C
241
242 /* C functions to interact with CKKS */
243 void SecCKKSInitialize(SecDbRef db);
244 void SecCKKSNotifyBlock(SecDbConnectionRef dbconn, SecDbTransactionPhase phase, SecDbTransactionSource source, CFArrayRef changes);
245
246 // Called by XPC approximately every 3 days
247 void SecCKKS24hrNotification(void);
248
249 // Register this callback to receive a call when the item with this UUID next successfully (or unsuccessfully) exits the outgoing queue.
250 void CKKSRegisterSyncStatusCallback(CFStringRef cfuuid, SecBoolCFErrorCallback callback);
251
252 // Tells CKKS that the local keychain was reset, and that it should respond accordingly
253 void SecCKKSPerformLocalResync(void);
254
255 // Returns true if CloudKit keychain syncing should occur
256 bool SecCKKSIsEnabled(void);
257
258 bool SecCKKSEnable(void);
259 bool SecCKKSDisable(void);
260
261 bool SecCKKSResetSyncing(void);
262
263 bool SecCKKSSyncManifests(void);
264 bool SecCKKSEnableSyncManifests(void);
265 bool SecCKKSSetSyncManifests(bool value);
266
267 bool SecCKKSEnforceManifests(void);
268 bool SecCKKSEnableEnforceManifests(void);
269 bool SecCKKSSetEnforceManifests(bool value);
270
271 bool SecCKKSReduceRateLimiting(void);
272 bool SecCKKSSetReduceRateLimiting(bool value);
273
274 // Testing support
275 bool SecCKKSTestsEnabled(void);
276 bool SecCKKSTestsEnable(void);
277 bool SecCKKSTestsDisable(void);
278
279 void SecCKKSTestResetFlags(void);
280 bool SecCKKSTestDisableAutomaticUUID(void);
281 void SecCKKSTestSetDisableAutomaticUUID(bool set);
282
283 bool SecCKKSTestDisableSOS(void);
284 void SecCKKSTestSetDisableSOS(bool set);
285
286 bool SecCKKSTestDisableKeyNotifications(void);
287 void SecCKKSTestSetDisableKeyNotifications(bool set);
288
289 // TODO: handle errors better
290 typedef CF_ENUM(CFIndex, CKKSErrorCode) {
291 CKKSNotInitialized = 9,
292 CKKSNotLoggedIn = 10,
293 CKKSNoSuchView = 11,
294
295 CKKSRemoteItemChangePending = 12,
296 CKKSLocalItemChangePending = 13,
297 CKKSItemChanged = 14,
298 CKKSNoUUIDOnItem = 15,
299 CKKSItemCreationFailure = 16,
300 CKKSInvalidKeyClass = 17,
301 CKKSKeyNotSelfWrapped = 18,
302 CKKSNoTrustedPeer = 19,
303 CKKSDataMismatch = 20,
304 CKKSProtobufFailure = 21,
305 CKKSNoSuchRecord = 22,
306 CKKSMissingTLKShare = 23,
307 CKKSNoPeersAvailable = 24,
308
309 CKKSSplitKeyHierarchy = 32,
310 CKKSOrphanedKey = 33,
311 CKKSInvalidTLK = 34,
312 CKKSNoTrustedTLKShares = 35,
313 CKKSKeyUnknownFormat = 36,
314 CKKSNoSigningKey = 37,
315 CKKSNoEncryptionKey = 38,
316
317 CKKSNotHSA2 = 40,
318 CKKSiCloudGreyMode = 41,
319 };
320
321 typedef CF_ENUM(CFIndex, CKKSResultDescriptionErrorCode) {
322 CKKSResultDescriptionNone = 0,
323 CKKSResultDescriptionPendingKeyReady = 1,
324 CKKSResultDescriptionPendingSuccessfulFetch = 2,
325 CKKSResultDescriptionPendingAccountLoggedIn = 3,
326 CKKSResultDescriptionPendingUnlock = 4,
327 CKKSResultDescriptionPendingBottledPeerModifyRecords = 5,
328 CKKSResultDescriptionPendingBottledPeerFetchRecords = 6,
329
330 CKKSResultDescriptionPendingZoneChangeFetchScheduling = 1000,
331 CKKSResultDescriptionPendingViewChangedScheduling = 1001,
332 CKKSResultDescriptionPendingZoneInitializeScheduling = 1002,
333 CKKSResultDescriptionPendingOutgoingQueueScheduling = 1003,
334 CKKSResultDescriptionPendingKeyHierachyPokeScheduling = 1004,
335 };
336
337 // These errors are returned by the CKKS server extension.
338 // Commented out codes here indicate that we don't currently handle them on the client side.
339 typedef CF_ENUM(CFIndex, CKKSServerExtensionErrorCode) {
340 // Generic Errors
341 //CKKSServerMissingField = 1,
342 //CKKSServerMissingRecord = 2,
343 //CKKSServerUnexpectedFieldType = 3,
344 //CKKSServerUnexpectedRecordType = 4,
345 //CKKSServerUnepxectedRecordID = 5,
346
347 // Chain errors:
348 //CKKSServerMissingCurrentKeyPointer = 6,
349 //CKKSServerMissingCurrentKey = 7,
350 //CKKSServerUnexpectedSyncKeyClassInChain = 8,
351 CKKSServerUnexpectedSyncKeyInChain = 9,
352
353 // Item/Currentitem record errors:
354 //CKKSServerKeyrollingNotAllowed = 10,
355 //CKKSServerInvalidPublicIdentity = 11,
356 //CKKSServerPublicKeyMismatch = 12,
357 //CKKSServerServiceNumberMismatch = 13,
358 //CKKSServerUnknownServiceNumber = 14,
359 //CKKSServerEncverLessThanMinVal = 15,
360 //CKKSServerCannotModifyWasCurrent = 16,
361 //CKKSServerInvalidCurrentItem = 17,
362 };
363
364 #define SecTranslateError(nserrorptr, cferror) \
365 if(nserrorptr) { \
366 *nserrorptr = (__bridge_transfer NSError*)cferror; \
367 } else { \
368 CFReleaseNull(cferror); \
369 }
370
371 // Very similar to the secerror, secnotice, and secinfo macros in debugging.h, but add zoneNames
372 #define ckkserrorwithzonename(scope, zoneName, format, ...) \
373 { \
374 os_log(secLogObjForScope("SecError"), scope "-%@: " format, (zoneName ? zoneName : @"unknown"), ##__VA_ARGS__); \
375 }
376 #define ckksnoticewithzonename(scope, zoneName, format, ...) \
377 { \
378 os_log(secLogObjForCFScope((__bridge CFStringRef)[@(scope "-") stringByAppendingString:(zoneName ? zoneName : @"unknown")]), \
379 format, \
380 ##__VA_ARGS__); \
381 }
382 #define ckksinfowithzonename(scope, zoneName, format, ...) \
383 { \
384 os_log_debug(secLogObjForCFScope((__bridge CFStringRef)[@(scope "-") stringByAppendingString:(zoneName ? zoneName : @"unknown")]), \
385 format, \
386 ##__VA_ARGS__); \
387 }
388
389 #define ckkserror(scope, zoneNameHaver, format, ...) \
390 { \
391 NSString* znh = zoneNameHaver.zoneName; \
392 ckkserrorwithzonename(scope, znh, format, ##__VA_ARGS__) \
393 }
394 #define ckksnotice(scope, zoneNameHaver, format, ...) \
395 { \
396 NSString* znh = zoneNameHaver.zoneName; \
397 ckksnoticewithzonename(scope, znh, format, ##__VA_ARGS__) \
398 }
399 #define ckksinfo(scope, zoneNameHaver, format, ...) \
400 { \
401 NSString* znh = zoneNameHaver.zoneName; \
402 ckksinfowithzonename(scope, znh, format, ##__VA_ARGS__) \
403 }
404
405 #undef ckksdebug
406 #if !defined(NDEBUG)
407 #define ckksdebugwithzonename(scope, zoneName, format, ...) \
408 { \
409 os_log_debug(secLogObjForCFScope((__bridge CFStringRef)[@(scope "-") stringByAppendingString:(zoneName ? zoneName : @"unknown")]), \
410 format, \
411 ##__VA_ARGS__); \
412 }
413 #define ckksdebug(scope, zoneNameHaver, format, ...) \
414 { \
415 NSString* znh = zoneNameHaver.zoneName; \
416 ckksdebugwithzonename(scope, znh, format, ##__VA_ARGS__) \
417 }
418 #else
419 #define ckksdebug(scope, ...) /* nothing */
420 #endif
421
422 #ifdef __OBJC__
423 NS_ASSUME_NONNULL_END
424 #else
425 CF_ASSUME_NONNULL_END
426 #endif
427
428 #endif /* CKKS_h */
429