2 * Copyright (c) 2018 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 #import "SecDbBackupManager.h"
26 NSString* const KeychainBackupsErrorDomain = @"com.apple.security.keychain.backups";
29 @implementation SecDbBackupWrappedItemKey
30 + (BOOL)supportsSecureCoding {
33 - (void)encodeWithCoder:(nonnull NSCoder *)coder {
34 [coder encodeObject:self.wrappedKey forKey:@"wrappedKey"];
35 [coder encodeObject:self.baguuid forKey:@"baguuid"];
38 - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
39 if (self = [super init]) {
40 _wrappedKey = [coder decodeObjectOfClass:[NSData class] forKey:@"wrappedKey"];
41 _baguuid = [coder decodeObjectOfClass:[NSData class] forKey:@"baguuid"];
47 #if !SECDB_BACKUPS_ENABLED
49 @implementation SecDbBackupManager
51 + (instancetype)manager
56 - (void)verifyBackupIntegrity:(bool)lightweight
57 completion:(void (^)(NSDictionary<NSString*, NSString*>* results, NSError* _Nullable error))completion
59 completion(nil, [NSError errorWithDomain:KeychainBackupsErrorDomain
60 code:SecDbBackupNotSupported
61 userInfo:@{NSLocalizedDescriptionKey : @"platform doesn't do backups"}]);
64 - (SecDbBackupWrappedItemKey* _Nullable)wrapItemKey:(id)key forKeyclass:(keyclass_t)keyclass error:(NSError**)error
71 bool SecDbBackupCreateOrLoadBackupInfrastructure(CFErrorRef* error)
76 #else // SECDB_BACKUPS_ENABLED is true, roll out the code
78 #import "SecDbBackupManager_Internal.h"
79 #include <CommonCrypto/CommonRandom.h>
80 #import <Foundation/NSData_Private.h>
81 #import "SecItemServer.h"
83 #import "keychain/categories/NSError+UsefulConstructors.h"
84 #import "sec_action.h"
85 #import "SecItemServer.h"
86 #include "utilities/der_plist.h"
88 // TODO: fire off metric on outcome
89 bool SecDbBackupCreateOrLoadBackupInfrastructure(CFErrorRef* error)
92 bool ok = [[SecDbBackupManager manager] createOrLoadBackupInfrastructure:&localError];
94 // Translate this to intelligible constant in C, but other errors can be passed along
95 if (localError.code == SecDbBackupKeychainLocked) {
96 *error = CFErrorCreate(kCFAllocatorDefault, kSecErrorDomain, errSecInteractionNotAllowed, NULL);
98 *error = (CFErrorRef)CFBridgingRetain(localError);
104 // Reading from disk is relatively expensive. Keep wrapped key in memory and just delete the unwrapped copy on lock
105 @interface InMemoryKCSK : NSObject
106 @property aks_ref_key_t refKey;
107 @property (nonatomic) NSData* wrappedKey;
108 @property (nonatomic) SFECKeyPair* key;
111 @implementation InMemoryKCSK
119 + (instancetype)kcskWithRefKey:(aks_ref_key_t)refKey wrappedKey:(NSData*)wrappedKey key:(SFECKeyPair*)key
121 InMemoryKCSK* kcsk = [InMemoryKCSK new];
122 kcsk.refKey = refKey;
123 kcsk.wrappedKey = wrappedKey;
130 @interface SecDbBackupManager () {
131 dispatch_queue_t _queue;
132 keybag_handle_t _handle;
133 SecDbBackupBagIdentity* _bagIdentity;
134 NSMutableDictionary<NSNumber*, InMemoryKCSK*>* _cachedKCSKs;
138 @implementation SecDbBackupManager
140 #pragma mark - Misc Helpers
142 - (NSData*)getSHA256OfData:(NSData*)data
144 NSMutableData* digest = [[NSMutableData alloc] initWithLength:CC_SHA512_DIGEST_LENGTH];
145 if (!CC_SHA512(data.bytes, (CC_LONG)data.length, digest.mutableBytes)) {
151 - (void)setBagIdentity:(SecDbBackupBagIdentity *)bagIdentity
153 _bagIdentity = bagIdentity;
156 - (SecDbBackupBagIdentity*)bagIdentity
161 - (bool)fillError:(NSError**)error code:(enum SecDbBackupErrorCode)code underlying:(NSError*)underlying description:(NSString*)format, ... NS_FORMAT_FUNCTION(4, 5)
165 va_start(ap, format);
166 NSString* desc = [[NSString alloc] initWithFormat:format arguments:ap];
169 *error = [NSError errorWithDomain:KeychainBackupsErrorDomain code:code description:desc underlying:underlying];
171 *error = [NSError errorWithDomain:KeychainBackupsErrorDomain code:code description:desc];
175 // analyzer gets upset when a method taking an error** doesn't return a value
179 static SecDbBackupManager* staticManager;
180 + (instancetype)manager
182 static dispatch_once_t onceToken;
183 dispatch_once(&onceToken, ^{
184 staticManager = [SecDbBackupManager new];
186 return staticManager;
189 // Testing only please
193 staticManager = [SecDbBackupManager new];
199 if (!checkV12DevEnabled()) {
202 if (self = [super init]) {
203 _queue = dispatch_queue_create("com.apple.security.secdbbackupmanager", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
204 _handle = bad_keybag_handle;
205 _cachedKCSKs = [NSMutableDictionary new];
210 - (SFECKeyPair*)getECKeyPairFromDERBytes:(void*)bytes length:(size_t)len error:(NSError**)error
212 if (!bytes || len == 0) {
213 [self fillError:error code:SecDbBackupInvalidArgument underlying:nil description:@"Need valid byte buffer to make EC keypair from"];
216 CFTypeRef cftype = NULL;
217 CFErrorRef cferr = NULL;
218 const uint8_t* derp = der_decode_plist(kCFAllocatorDefault, NSPropertyListImmutable, &cftype, &cferr, bytes, bytes + len);
220 if (derp == NULL || derp != (bytes + len) || cftype == NULL) {
221 [self fillError:error code:SecDbBackupMalformedKCSKDataOnDisk underlying:CFBridgingRelease(cferr) description:@"Unable to parse der data"];
225 return [[SFECKeyPair alloc] initWithData:(__bridge_transfer NSData*)cftype
226 specifier:[[SFECKeySpecifier alloc] initWithCurve:SFEllipticCurveNistp384]
230 #pragma mark - Fixup And Verification
232 - (void)verifyBackupIntegrity:(bool)lightweight
233 completion:(void (^)(NSDictionary<NSString*, NSString*>* _Nonnull, NSError * _Nullable))completion
235 NSError* error = nil;
236 completion(@{@"summary" : @"Unimplemented"}, error);
239 #pragma mark - Backup Bag Management
241 // Get the bag's UUID from AKS and the hash from provided data. This must always be the original bag's data
242 - (SecDbBackupBagIdentity*)bagIdentityWithHandle:(keybag_handle_t)handle data:(NSData*)data error:(NSError**)error {
244 secnotice("SecDbBackup", "Getting bag identity");
245 SecDbBackupBagIdentity* identity = [SecDbBackupBagIdentity new];
248 kern_return_t aksResult = aks_get_bag_uuid(handle, uuid);
249 if (aksResult != kAKSReturnSuccess) {
250 [self fillError:error code:SecDbBackupAKSFailure underlying:nil description:@"Unable to get keybag UUID (%d)", aksResult];
253 identity.baguuid = [NSData dataWithBytes:uuid length:16];
255 NSData* digest = [self getSHA256OfData:data];
257 [self fillError:error code:SecDbBackupCryptoFailure underlying:nil description:@"CC_SHA512 returned failure, can't get bag hash"];
260 identity.baghash = digest;
262 secnotice("SecDbBackup", "Obtained bag identity: %@", identity);
267 - (NSData*)createBackupBagSecret:(NSError**)error
269 uint8_t* data = calloc(1, BACKUPBAG_PASSPHRASE_LENGTH);
271 return nil; // Good luck allocating an error message
274 CCRNGStatus rngResult = CCRandomGenerateBytes(data, BACKUPBAG_PASSPHRASE_LENGTH);
275 if (rngResult != kCCSuccess) {
276 [self fillError:error code:SecDbBackupCryptoFailure underlying:nil description:@"Unable to generate random bytes (%d)", rngResult];
280 NSData* secret = [NSData _newZeroingDataWithBytesNoCopy:data length:BACKUPBAG_PASSPHRASE_LENGTH deallocator:NSDataDeallocatorNone];
284 - (keybag_handle_t)onQueueCreateBackupBagWithSecret:(NSData*)secret error:(NSError**)error
286 dispatch_assert_queue(_queue);
288 keybag_handle_t handle = bad_keybag_handle;
289 kern_return_t aksresult = aks_create_bag(secret.bytes, BACKUPBAG_PASSPHRASE_LENGTH, kAppleKeyStoreAsymmetricBackupBag, &handle);
290 if (aksresult != kAKSReturnSuccess) {
291 [self fillError:error code:SecDbBackupAKSFailure underlying:nil description:@"Unable to create keybag (%d)", aksresult];
292 return bad_keybag_handle;
295 // Make secret keys unavailable. Causes pubkeys to be destroyed so reload bag before use
296 aksresult = aks_lock_bag(handle);
297 if (aksresult != kAKSReturnSuccess) { // This would be rather surprising
298 [self fillError:error code:SecDbBackupAKSFailure underlying:nil description:@"Unable to lock keybag (%d)", aksresult];
299 aks_unload_bag(handle);
300 return bad_keybag_handle;
306 - (BOOL)onQueueInTransaction:(SecDbConnectionRef)dbt saveBackupBag:(keybag_handle_t)handle asDefault:(BOOL)asDefault error:(NSError**)error
308 dispatch_assert_queue(_queue);
312 kern_return_t aksResult = aks_save_bag(handle, &buf, &buflen);
313 if (aksResult != kAKSReturnSuccess) {
314 [self fillError:error code:SecDbBackupAKSFailure underlying:nil description:@"Unable to serialize keybag (%d)", aksResult];
317 NSData* bagData = [NSData dataWithBytesNoCopy:buf length:buflen];
319 SecDbBackupBagIdentity* bagIdentity = [self bagIdentityWithHandle:handle data:bagData error:error];
323 SecDbBackupBag* bag = [SecDbBackupBag new];
324 bag.bagIdentity = bagIdentity;
325 bag.keybag = bagData;
327 __block CFErrorRef cfError = NULL;
328 __block bool ok = true;
330 ok &= SecDbPrepare(dbt, CFSTR("UPDATE backupbags SET defaultvalue = 0 WHERE defaultvalue = 1"), &cfError, ^(sqlite3_stmt *stmt) {
331 ok &= SecDbStep(dbt, stmt, &cfError, ^(bool *stop) {
332 // FIXME: Should this be an error? Should something else happen?
333 secwarning("SecDbBackup: Marking existing bag as non-default");
340 ok &= SecDbPrepare(dbt, CFSTR("INSERT INTO backupbags (backupUUID, backupbag, defaultvalue) VALUES (?,?,?)"), &cfError, ^(sqlite3_stmt *stmt) {
341 ok &= SecDbBindObject(stmt, 1, (__bridge CFDataRef)bag.bagIdentity.baguuid, &cfError);
342 ok &= SecDbBindObject(stmt, 2, (__bridge CFDataRef)bag.data, &cfError);
343 ok &= SecDbBindInt(stmt, 3, asDefault ? 1 : 0, &cfError);
344 ok &= SecDbStep(dbt, stmt, &cfError, NULL);
348 secerror("SecDbBackup: unable to save keybag to disk: %@", cfError);
349 [self fillError:error code:SecDbBackupWriteFailure underlying:CFBridgingRelease(cfError) description:@"Unable to save keybag to disk"];
355 - (keybag_handle_t)onQueueLoadBackupBag:(NSUUID*)uuid error:(NSError**)error {
356 dispatch_assert_queue(_queue);
358 secnotice("SecDbBackup", "Attempting to load backup bag from disk");
360 __block CFErrorRef localErr = NULL;
361 __block bool ok = true;
362 __block NSData* readUUID;
363 __block NSData* readBagData;
364 __block unsigned found = 0;
365 ok &= kc_with_dbt_non_item_tables(false, &localErr, ^bool(SecDbConnectionRef dbt) {
366 NSString* sql = [NSString stringWithFormat:@"SELECT backupUUID, backupbag FROM backupbags WHERE %@", uuid ? @"backupUUID = ?" : @"defaultvalue = 1"];
367 ok &= SecDbPrepare(dbt, (__bridge CFStringRef)sql, &localErr, ^(sqlite3_stmt *stmt) {
369 unsigned char uuidbytes[UUIDBYTESLENGTH] = {0};
370 [uuid getUUIDBytes:uuidbytes];
371 ok &= SecDbBindBlob(stmt, 1, uuidbytes, UUIDBYTESLENGTH, SQLITE_TRANSIENT, &localErr);
373 ok &= SecDbStep(dbt, stmt, &localErr, ^(bool *stop) {
374 if (found > 0) { // For uuids this should have violated constraints
375 secerror("Encountered more than one backup bag by %@", uuid ? @"backupUUID" : @"defaultvalue");
378 readUUID = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
379 readBagData = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 1) length:sqlite3_column_bytes(stmt, 1)];
387 secerror("SecDbBackup: Unable to load backup bag from disk: %@", localErr);
388 [self fillError:error code:SecDbBackupWriteFailure underlying:CFBridgingRelease(localErr) description:@"Unable to load backup bag from disk"];
389 return bad_keybag_handle;
393 [self fillError:error code:SecDbBackupNoBackupBagFound underlying:nil description:@"No backup bag found to load from disk"];
394 return bad_keybag_handle;
395 } else if (found > 1) {
396 [self fillError:error
397 code:uuid ? SecDbBackupDuplicateBagFound : SecDbBackupMultipleDefaultBagsFound
399 description:@"More than one backup bag found"];
400 return bad_keybag_handle;
403 if (!readUUID || readUUID.length != UUIDBYTESLENGTH || !readBagData || !readBagData.length) {
404 [self fillError:error code:SecDbBackupMalformedBagDataOnDisk underlying:nil description:@"bags read from disk malformed"];
405 return bad_keybag_handle;
408 SecDbBackupBag* readBag = [[SecDbBackupBag alloc] initWithData:readBagData];
410 [self fillError:error code:SecDbBackupDeserializationFailure underlying:nil description:@"bag from disk does not deserialize"];
411 return bad_keybag_handle;
414 secnotice("SecDbBackup", "Successfully read backup bag from disk; loading and verifying. Read bag ID: %@", readBag.bagIdentity);
416 keybag_handle_t handle = bad_keybag_handle;
417 kern_return_t aksResult = aks_load_bag(readBag.keybag.bytes, (int)readBag.keybag.length, &handle);
418 if (aksResult != kAKSReturnSuccess) {
419 [self fillError:error code:SecDbBackupAKSFailure underlying:nil description:@"Unable to load bag from disk (%d)", aksResult];
420 return bad_keybag_handle;
423 SecDbBackupBagIdentity* loadedID = [self bagIdentityWithHandle:handle data:readBag.keybag error:error];
425 aks_unload_bag(handle);
426 return bad_keybag_handle;
429 if (memcmp(loadedID.baguuid.bytes, readBag.bagIdentity.baguuid.bytes, UUIDBYTESLENGTH) ||
430 memcmp(loadedID.baguuid.bytes, readUUID.bytes, UUIDBYTESLENGTH)) {
431 [self fillError:error code:SecDbBackupUUIDMismatch underlying:nil description:@"Loaded UUID does not match UUIDs on disk"];
432 aks_unload_bag(handle);
433 return bad_keybag_handle;
436 if (memcmp(loadedID.baghash.bytes, readBag.bagIdentity.baghash.bytes, CC_SHA512_DIGEST_LENGTH)) {
437 [self fillError:error code:SecDbBackupDeserializationFailure underlying:nil description:@"Keybag hash does not match its identity's hash"];
438 return bad_keybag_handle;
441 // TODO: verify that bag is still signed, rdar://problem/46702467
443 secnotice("SecDbBackup", "Backup bag loaded and verified.");
445 // Must load readBag's identity because the hash from AKS is unstable.
446 // This is the hash of the original saved bag and is anchored in the KCSKes.
447 _bagIdentity = readBag.bagIdentity;
452 - (BOOL)onQueueReloadDefaultBackupBagWithError:(NSError**)error
454 if (_handle != bad_keybag_handle) {
455 aks_unload_bag(_handle);
458 _handle = [self onQueueLoadBackupBag:nil error:error];
459 return _handle != bad_keybag_handle;
462 #pragma mark - KCSK Management
464 - (SecDbBackupKeyClassSigningKey*)createKCSKForKeyClass:(keyclass_t)class withWrapper:(SFAESKey*)wrapper error:(NSError**)error
467 [self fillError:error code:SecDbBackupInvalidArgument underlying:nil description:@"Need wrapper for KCSK"];
471 SecDbBackupKeyClassSigningKey* kcsk = [SecDbBackupKeyClassSigningKey new];
472 kcsk.keyClass = class;
474 SFECKeyPair* keypair = [[SFECKeyPair alloc] initRandomKeyPairWithSpecifier:[[SFECKeySpecifier alloc] initWithCurve:SFEllipticCurveNistp384]];
475 kcsk.publicKey = [keypair.publicKey.keyData copy];
477 // Create a DER-encoded dictionary of bag identity
480 CFErrorRef cfErr = NULL;
481 NSDictionary* identDict = @{@"baguuid" : _bagIdentity.baguuid, @"baghash" : _bagIdentity.baghash};
482 NSData* identData = (__bridge_transfer NSData*)CFPropertyListCreateDERData(NULL, (__bridge CFDictionaryRef)identDict, &cfErr);
483 aks_operation_optional_params(NULL, 0, identData.bytes, identData.length, NULL, 0, &der_blob, &der_len);
485 // Create ref key with embedded bag identity DER data
486 aks_ref_key_t refkey = NULL;
487 kern_return_t aksResult = aks_ref_key_create(KEYBAG_DEVICE, class, key_type_sym, der_blob, der_len, &refkey);
489 if (aksResult != kAKSReturnSuccess) {
490 [self fillError:error code:SecDbBackupAKSFailure underlying:nil
491 description:@"Unable to create AKS ref key for KCSK class %d: %d", class, aksResult];
495 size_t refkeyblobsize = 0;
496 const uint8_t* refkeyblob = aks_ref_key_get_blob(refkey, &refkeyblobsize);
497 kcsk.aksRefKey = [NSData dataWithBytes:refkeyblob length:refkeyblobsize];
499 size_t wrappedKeyLen = 0;
500 void* wrappedKey = NULL;
501 NSData* keypairAsData = keypair.keyData;
502 aksResult = aks_ref_key_encrypt(refkey, NULL, 0, keypairAsData.bytes, keypairAsData.length, &wrappedKey, &wrappedKeyLen);
503 if (aksResult != kAKSReturnSuccess) {
504 [self fillError:error code:SecDbBackupAKSFailure underlying:nil
505 description:@"Unable to encrypt KCSK class %d with AKS ref key: %d", class, aksResult];
508 aks_ref_key_free(&refkey);
509 kcsk.aksWrappedKey = [NSData dataWithBytesNoCopy:wrappedKey length:wrappedKeyLen];
511 // Also add DER-encoded bag identity here
512 SFAuthenticatedEncryptionOperation* op = [[SFAuthenticatedEncryptionOperation alloc] initWithKeySpecifier:[[SFAESKeySpecifier alloc] initWithBitSize:SFAESKeyBitSize256]];
513 SFAuthenticatedCiphertext* backupwrapped = [op encrypt:keypair.keyData withKey:wrapper additionalAuthenticatedData:identData error:error];
514 kcsk.backupWrappedKey = [NSKeyedArchiver archivedDataWithRootObject:backupwrapped requiringSecureCoding:YES error:error];
515 if (!kcsk.backupWrappedKey) {
522 - (BOOL)inTransaction:(SecDbConnectionRef)dbt writeKCSKToKeychain:(SecDbBackupKeyClassSigningKey*)kcsk error:(NSError**)error
524 __block bool ok = true;
525 __block CFErrorRef cfError = NULL;
526 NSString* sql = @"INSERT INTO backupkeyclasssigningkeys (keyclass, backupUUID, signingkey) VALUES (?, ?, ?)";
527 ok &= SecDbPrepare(dbt, (__bridge CFStringRef)sql, &cfError, ^(sqlite3_stmt *stmt) {
528 ok &= SecDbBindInt(stmt, 1, kcsk.keyClass, &cfError);
529 ok &= SecDbBindObject(stmt, 2, (__bridge CFTypeRef)(self->_bagIdentity.baguuid), &cfError);
530 ok &= SecDbBindObject(stmt, 3, (__bridge CFTypeRef)(kcsk.data), &cfError);
531 ok &= SecDbStep(dbt, stmt, &cfError, NULL);
535 secerror("SecDbBackup: Unable to write KCSK for class %d to keychain: %@", kcsk.keyClass, cfError);
536 [self fillError:error code:SecDbBackupWriteFailure underlying:CFBridgingRelease(cfError) description:@"Unable to write KCSK for class %d to keychain", kcsk.keyClass];
542 - (InMemoryKCSK*)onQueueReadKCSKFromDiskForClass:(keyclass_t)keyclass error:(NSError**)error
544 __block bool ok = true;
545 __block CFErrorRef cfError = NULL;
546 __block NSData* readUUID;
547 __block NSData* readKCSK;
548 NSString* sql = @"SELECT backupUUID, signingkey FROM backupkeyclasssigningkeys WHERE keyclass = ?";
549 ok &= kc_with_dbt_non_item_tables(NO, &cfError, ^bool(SecDbConnectionRef dbt) {
550 ok &= SecDbPrepare(dbt, (__bridge CFStringRef)sql, &cfError, ^(sqlite3_stmt *stmt) {
551 ok &= SecDbBindInt(stmt, 1, keyclass, &cfError);
552 ok &= SecDbStep(dbt, stmt, &cfError, ^(bool *stop) {
553 readUUID = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
554 readKCSK = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 1) length:sqlite3_column_bytes(stmt, 1)];
559 if (!readKCSK || !readUUID) {
560 [self fillError:error code:SecDbBackupNoKCSKFound underlying:nil description:@"KCSK for class %d not on disk", keyclass];
564 SecDbBackupKeyClassSigningKey* kcsk = [[SecDbBackupKeyClassSigningKey alloc] initWithData:readKCSK];
566 [self fillError:error code:SecDbBackupMalformedKCSKDataOnDisk underlying:nil description:@"Retrieved KCSK blob but it didn't become a KCSK"];
570 aks_ref_key_t refkey = NULL;
571 kern_return_t aksResult = aks_ref_key_create_with_blob(KEYBAG_DEVICE, kcsk.aksRefKey.bytes, kcsk.aksRefKey.length, &refkey);
572 if (aksResult != kAKSReturnSuccess) {
573 [self fillError:error
574 code:SecDbBackupAKSFailure
576 description:@"Failed to create refkey from KCSK blob for class %d: %d", keyclass, aksResult];
580 size_t externalDataLen = 0;
581 const uint8_t* externalData = aks_ref_key_get_external_data(refkey, &externalDataLen);
582 NSData* derBagIdent = [NSData dataWithBytes:externalData length:externalDataLen];
583 CFErrorRef cfErr = NULL;
584 NSDictionary* identData = (__bridge_transfer NSDictionary*)CFPropertyListCreateWithDERData(NULL, (__bridge CFDataRef)derBagIdent, 0, NULL, &cfErr);
585 if (!identData || ![_bagIdentity.baghash isEqualToData:identData[@"baghash"]] || ![_bagIdentity.baguuid isEqualToData:identData[@"baguuid"]]) {
586 secerror("SecDbBackup: KCSK ref key embedded bag identity does not match loaded bag. %@ vs %@", identData, _bagIdentity);
587 [self fillError:error code:SecDbBackupMalformedKCSKDataOnDisk underlying:CFBridgingRelease(cfErr) description:@"KCSK ref key embedded bag identity does not match loaded bag."];
591 // AKS refkey claims in its external data to belong to our backup bag. Let's see if the claim holds up: use the key.
592 void* keypairBytes = NULL;
593 size_t keypairLength = 0;
594 aksResult = aks_ref_key_decrypt(refkey, NULL, 0, kcsk.aksWrappedKey.bytes, kcsk.aksWrappedKey.length, &keypairBytes, &keypairLength);
595 if (aksResult == kSKSReturnNoPermission) {
596 [self fillError:error code:SecDbBackupKeychainLocked underlying:nil description:@"Unable to unwrap KCSK private key for class %d. Locked", keyclass];
598 } else if (aksResult != kAKSReturnSuccess) {
599 // Failure could indicate key was corrupted or tampered with
600 [self fillError:error
601 code:SecDbBackupAKSFailure
603 description:@"AKS did not unwrap KCSK private key for class %d: %d", keyclass, aksResult];
607 SFECKeyPair* keypair = [self getECKeyPairFromDERBytes:keypairBytes length:keypairLength error:error];
608 return keypair ? [InMemoryKCSK kcskWithRefKey:refkey wrappedKey:kcsk.aksWrappedKey key:keypair] : nil;
611 - (SFECKeyPair*)onQueueFetchKCSKForKeyclass:(keyclass_t)keyclass error:(NSError**)error
614 assert(_bagIdentity);
615 assert(_handle != bad_keybag_handle);
617 InMemoryKCSK* cached = _cachedKCSKs[@(keyclass)];
623 secnotice("SecDbBackup", "Cached but wrapped KCSK found for class %d, unwrapping", keyclass);
624 void* keybytes = NULL;
626 kern_return_t aksResult = aks_ref_key_decrypt(cached.refKey, NULL, 0, cached.wrappedKey.bytes, cached.wrappedKey.length, &keybytes, &keylen);
627 if (aksResult == kAKSReturnSuccess) {
628 cached.key = [self getECKeyPairFromDERBytes:keybytes length:keylen error:error];
631 secerror("SecDbBackup: Cached KCSK isn't unwrapping key material. This is a bug.");
635 secnotice("SecDbBackup", "No cached KCSK for class %d, reading from disk", keyclass);
636 cached = [self onQueueReadKCSKFromDiskForClass:keyclass error:error];
638 secerror("SecDbBackup: Failed to obtain KCSK for class %d: %@", keyclass, *error);
639 if ((*error).code != SecDbBackupKeychainLocked) {
640 seccritical("SecDbBackup: KCSK unavailable, cannot backup-wrap class %d items. Need to perform recovery.", keyclass);
641 // TODO: We're borked. Need to recover from this.
644 _cachedKCSKs[@(keyclass)] = cached;
648 #pragma mark - Recovery Set Management
650 - (BOOL)onQueueInTransaction:(SecDbConnectionRef)dbt writeRecoverySetToKeychain:(SecDbBackupRecoverySet*)set error:(NSError**)error
652 dispatch_assert_queue(_queue);
653 __block bool ok = true;
654 __block CFErrorRef cfError = NULL;
656 NSString* sql = @"INSERT INTO backuprecoverysets (backupUUID, recoverytype, recoveryset) VALUES (?, ?, ?)";
657 ok &= SecDbPrepare(dbt, (__bridge CFStringRef)sql, &cfError, ^(sqlite3_stmt *stmt) {
658 ok &= SecDbBindObject(stmt, 1, (__bridge CFDataRef)set.bagIdentity.baguuid, &cfError);
659 ok &= SecDbBindObject(stmt, 2, (__bridge CFNumberRef)@(set.recoveryType), &cfError);
660 ok &= SecDbBindObject(stmt, 3, (__bridge CFDataRef)set.data, &cfError);
661 ok &= SecDbStep(dbt, stmt, &cfError, NULL);
665 secerror("SecDbBackup: Unable to write recovery set to keychain: %@", cfError);
666 [self fillError:error code:SecDbBackupWriteFailure underlying:CFBridgingRelease(cfError) description:@"Unable to write recovery set to keychain"];
672 - (SecDbBackupRecoverySet*)onQueueInTransaction:(SecDbConnectionRef)dbt createRecoverySetWithBagSecret:(NSData*)secret
673 forType:(SecDbBackupRecoveryType)type error:(NSError**)error
675 dispatch_assert_queue(_queue);
677 [self fillError:error code:SecDbBackupInvalidArgument underlying:nil description:@"Can't create recovery set without secret"];
681 SecDbBackupRecoverySet* set;
683 case SecDbBackupRecoveryTypeAKS:
684 set = [self inTransaction:dbt createAKSTypeRecoverySetWithBagSecret:secret handle:_handle error:error];
686 case SecDbBackupRecoveryTypeCylon:
687 secerror("SecDbBackup: Cylon recovery type not yet implemented");
688 [self fillError:error code:SecDbBackupUnknownOption underlying:nil description:@"Recovery type Cylon not yet implemented"];
690 case SecDbBackupRecoveryTypeRecoveryKey:
691 secerror("SecDbBackup: RecoveryKey recovery type not yet implemented");
692 [self fillError:error code:SecDbBackupUnknownOption underlying:nil description:@"Recovery type RecoveryKey not yet implemented"];
695 secerror("SecDbBackup: Unknown type %ld", (long)type);
696 [self fillError:error code:SecDbBackupUnknownOption underlying:nil description:@"Recovery type %li unknown", (long)type];
703 - (SecDbBackupRecoverySet*)inTransaction:(SecDbConnectionRef)dbt createAKSTypeRecoverySetWithBagSecret:(NSData*)secret handle:(keybag_handle_t)handle error:(NSError**)error
705 SecDbBackupRecoverySet* set = [SecDbBackupRecoverySet new];
706 set.recoveryType = SecDbBackupRecoveryTypeAKS;
707 set.bagIdentity = _bagIdentity;
709 NSError* cryptoError;
710 SFAESKeySpecifier* specifier = [[SFAESKeySpecifier alloc] initWithBitSize:SFAESKeyBitSize256];
711 SFAESKey* KCSKSecret = [[SFAESKey alloc] initRandomKeyWithSpecifier:specifier error:&cryptoError];
713 [self fillError:error code:SecDbBackupCryptoFailure underlying:cryptoError description:@"Unable to create AKS recovery set"];
717 // We explicitly do NOT want akpu. For the rest, create and write all KCSKs
718 for (NSNumber* class in @[@(key_class_ak), @(key_class_ck), @(key_class_dk), @(key_class_aku), @(key_class_cku), @(key_class_dku)]) {
719 SecDbBackupKeyClassSigningKey* kcsk = [self createKCSKForKeyClass:[class intValue] withWrapper:KCSKSecret error:error];
721 secerror("SecDbBackup: Unable to create KCSK for class %@: %@", class, *error);
724 if (![self inTransaction:dbt writeKCSKToKeychain:kcsk error:error]) {
725 secerror("SecDbBackup: Unable to write KCSK for class %@ to keychain: %@", class, *error);
730 SFAESKey* recoverykey = [[SFAESKey alloc] initRandomKeyWithSpecifier:specifier error:&cryptoError];
732 [self fillError:error code:SecDbBackupCryptoFailure underlying:cryptoError description:@"Unable to create recovery key"];
736 SFAuthenticatedEncryptionOperation* op = [[SFAuthenticatedEncryptionOperation alloc]
737 initWithKeySpecifier:[[SFAESKeySpecifier alloc]
738 initWithBitSize:SFAESKeyBitSize256]];
739 SFAuthenticatedCiphertext* wrappedsecret = [op encrypt:secret withKey:recoverykey error:&cryptoError];
740 if (!wrappedsecret) {
741 secerror("SecDbBackup: Unable to wrap keybag secret: %@", cryptoError);
742 [self fillError:error code:SecDbBackupCryptoFailure underlying:cryptoError description:@"Unable to wrap keybag secret"];
745 set.wrappedBagSecret = [NSKeyedArchiver archivedDataWithRootObject:wrappedsecret requiringSecureCoding:YES error:error];
747 SFAuthenticatedCiphertext* wrappedkcsksecret = [op encrypt:KCSKSecret.keyData withKey:recoverykey error:&cryptoError];
748 if (!wrappedkcsksecret) {
749 secerror("SecDbBackup: Unable to wrap KCSK secret: %@", cryptoError);
750 [self fillError:error code:SecDbBackupCryptoFailure underlying:cryptoError description:@"Unable to wrap KCSK secret"];
753 set.wrappedKCSKSecret = [NSKeyedArchiver archivedDataWithRootObject:wrappedkcsksecret requiringSecureCoding:YES error:error];
755 NSMutableData* wrappedrecoverykey = [[NSMutableData alloc] initWithLength:APPLE_KEYSTORE_MAX_SYM_WRAPPED_KEY_LEN];
756 if (![SecAKSObjCWrappers aksEncryptWithKeybag:KEYBAG_DEVICE keyclass:key_class_aku plaintext:recoverykey.keyData outKeyclass:nil ciphertext:wrappedrecoverykey error:&cryptoError]) {
757 secerror("SecDbBackup: Unable to wrap recovery key to AKS: %@", cryptoError);
758 [self fillError:error code:SecDbBackupAKSFailure underlying:cryptoError description:@"Unable to wrap recovery key to AKS"];
761 set.wrappedRecoveryKey = [wrappedrecoverykey copy];
766 #pragma mark - Backup System Initialization / Maintenance
768 - (BOOL)createOrLoadBackupInfrastructure:(NSError**)error {
770 __block BOOL ok = true;
771 __block NSError* localError;
772 dispatch_sync(_queue, ^{
773 ok = [self onQueueCreateOrLoadBackupInfrastructure:&localError];
782 // TODO: if this creates the infrastructure, kick off a fixup routine
783 // TODO: if not, make sure we actually delete stuff. Nested transactions are not a thing (use checkpointing or delete explicitly)
785 - (BOOL)onQueueCreateOrLoadBackupInfrastructure:(NSError**)error {
786 dispatch_assert_queue(_queue);
788 if (self->_handle != bad_keybag_handle) {
792 self->_handle = [self onQueueLoadBackupBag:nil error:error];
793 if (self->_handle != bad_keybag_handle) {
794 secnotice("SecDbBackup", "Keybag found and loaded");
796 } else if (self->_handle == bad_keybag_handle && (*error).code != SecDbBackupNoBackupBagFound) {
801 __block BOOL ok = YES;
802 __block CFErrorRef cfError = NULL;
803 __block NSError* localError;
804 secnotice("SecDbBackup", "CreateOrLoad: No backup bag found, attempting to create new infrastructure");
805 if (ok && !SecAKSDoWithUserBagLockAssertion(&cfError, ^{
806 ok &= kc_with_dbt_non_item_tables(YES, &cfError, ^bool(SecDbConnectionRef dbt) {
807 ok &= kc_transaction(dbt, &cfError, ^bool{
808 NSData* secret = [self createBackupBagSecret:&localError];
813 self->_handle = [self onQueueCreateBackupBagWithSecret:secret error:&localError];
814 if (self->_handle == bad_keybag_handle) {
817 secnotice("SecDbBackup", "CreateOrLoad: Successfully created backup bag");
819 if (![self onQueueInTransaction:dbt saveBackupBag:self->_handle asDefault:YES error:&localError]) {
822 secnotice("SecDbBackup", "CreateOrLoad: Successfully saved backup bag");
824 if (![self onQueueReloadDefaultBackupBagWithError:&localError]) {
827 secnotice("SecDbBackup", "CreateOrLoad: Successfully reloaded backup bag");
829 SecDbBackupRecoverySet* set = [self onQueueInTransaction:dbt
830 createRecoverySetWithBagSecret:secret
831 forType:SecDbBackupRecoveryTypeAKS
834 secnotice("SecDbBackup", "CreateOrLoad: Successfully created recovery set");
838 if (![self onQueueInTransaction:dbt writeRecoverySetToKeychain:set error:&localError]) {
841 secnotice("SecDbBackup", "CreateOrLoad: Successfully saved recovery set");
847 })) { // could not perform action with lock assertion
848 static dispatch_once_t once;
849 static sec_action_t action;
850 dispatch_once(&once, ^{
851 action = sec_action_create("keybag_locked_during_backup_setup_complaint", 5);
852 sec_action_set_handler(action, ^{
853 secerror("SecDbBackup: Cannot obtain AKS lock assertion so cannot setup backup infrastructure");
856 sec_action_perform(action);
857 [self fillError:&localError code:SecDbBackupKeychainLocked underlying:nil
858 description:@"Unable to initialize backup infrastructure, keychain locked"];
863 self->_bagIdentity = nil;
864 aks_unload_bag(self->_handle);
865 self->_handle = bad_keybag_handle;
869 secnotice("SecDbBackup", "Hurray! Successfully created backup infrastructure");
871 assert(localError || cfError);
873 secerror("SecDbBackup: Could not initialize backup infrastructure: %@", localError);
875 } else if (cfError) {
876 secerror("SecDbBackup: Could not initialize backup infrastructure: %@", cfError);
877 [self fillError:error code:SecDbBackupSetupFailure underlying:CFBridgingRelease(cfError)
878 description:@"Unable to initialize backup infrastructure"];
880 secerror("SecDbBackup: Could not initialize backup infrastructure but have no error");
881 [self fillError:error code:SecDbBackupSetupFailure underlying:nil
882 description:@"Unable to initialize backup infrastructure (not sure why)"];
884 CFReleaseNull(cfError);
890 #pragma mark - Item Encryption
892 - (SecDbBackupWrappedItemKey*)wrapItemKey:(SFAESKey*)key forKeyclass:(keyclass_t)keyclass error:(NSError**)error
895 if (keyclass == key_class_akpu) {
896 secwarning("SecDbBackup: Don't tempt me Frodo!");
897 [self fillError:error code:SecDbBackupInvalidArgument underlying:nil description:@"Do not call wrapItemKey with class akpu"];
901 if (![self createOrLoadBackupInfrastructure:error]) {
902 if ((*error).domain != (__bridge NSString*)kSecErrorDomain || (*error).code != errSecInteractionNotAllowed) {
903 secerror("SecDbBackup: Could not create/load backup infrastructure: %@", *error);
908 __block SecDbBackupWrappedItemKey* backupWrappedKey;
910 __block NSMutableData* wrappedKey = [NSMutableData dataWithLength:APPLE_KEYSTORE_MAX_ASYM_WRAPPED_KEY_LEN];
911 __block NSError* localError;
912 dispatch_sync(_queue, ^{
913 if (![self onQueueCreateOrLoadBackupInfrastructure:&localError]) {
916 if (![SecAKSObjCWrappers aksEncryptWithKeybag:self->_handle
918 plaintext:key.keyData
920 ciphertext:wrappedKey
921 error:&localError]) {
924 SFSignedData* wrappedAndSigned = [self onQueueSignData:wrappedKey withKCSKForKeyclass:keyclass error:&localError];
925 if (!wrappedAndSigned) {
926 if (localError.code != SecDbBackupKeychainLocked) {
927 secerror("SecDbBackup: Unable to sign item for class %d: %@", keyclass, localError);
931 backupWrappedKey = [SecDbBackupWrappedItemKey new];
932 backupWrappedKey.wrappedKey = [NSKeyedArchiver archivedDataWithRootObject:wrappedAndSigned requiringSecureCoding:YES error:&localError];
933 backupWrappedKey.baguuid = self->_bagIdentity.baguuid;
937 secerror("SecDbBackup: Unable to wrap-and-sign item of class %d: %@", keyclass, localError);
942 return backupWrappedKey;
945 - (SFSignedData*)onQueueSignData:(NSMutableData*)data withKCSKForKeyclass:(keyclass_t)keyclass error:(NSError**)error
947 SFECKeyPair* kcsk = [self onQueueFetchKCSKForKeyclass:keyclass error:error];
952 SFEC_X962SigningOperation* op = [[SFEC_X962SigningOperation alloc] initWithKeySpecifier:[[SFECKeySpecifier alloc] initWithCurve:SFEllipticCurveNistp384]];
953 return [op sign:data withKey:kcsk error:error];
956 #pragma mark - Testing Helpers
958 - (keybag_handle_t)createBackupBagWithSecret:(NSData*)secret error:(NSError**)error
961 __block keybag_handle_t handle = bad_keybag_handle;
962 __block NSError* localError;
963 dispatch_sync(_queue, ^{
964 handle = [self onQueueCreateBackupBagWithSecret:secret error:&localError];
968 } else if (handle == bad_keybag_handle) {
969 [self fillError:error code:SecDbBackupTestCodeFailure underlying:nil description:@"Unable to create backup bag, but no reason"];
974 - (BOOL)saveBackupBag:(keybag_handle_t)handle asDefault:(BOOL)asDefault error:(NSError**)error
977 __block bool ok = true;
978 __block NSError* localErr;
979 __block CFErrorRef cfError = NULL;
980 dispatch_sync(_queue, ^{
981 ok &= kc_with_dbt_non_item_tables(YES, &cfError, ^bool(SecDbConnectionRef dbt) {
982 ok &= kc_transaction(dbt, &cfError, ^bool{
983 ok &= [self onQueueInTransaction:dbt saveBackupBag:handle asDefault:asDefault error:&localErr];
992 [self fillError:error code:SecDbBackupTestCodeFailure underlying:CFBridgingRelease(cfError) description:@"Unable to save keybag to disk"];
993 } else if (localErr) {
995 } else if (!localErr) {
996 [self fillError:error code:SecDbBackupTestCodeFailure underlying:nil description:@"Unable to save keybag to disk but who knows why"];
1002 - (keybag_handle_t)loadBackupBag:(NSUUID*)uuid error:(NSError**)error {
1003 __block keybag_handle_t handle = bad_keybag_handle;
1004 __block NSError* localError;
1005 dispatch_sync(_queue, ^{
1006 handle = [self onQueueLoadBackupBag:uuid error:&localError];
1008 if (error && localError) {
1009 *error = localError;
1014 - (SecDbBackupRecoverySet*)createRecoverySetWithBagSecret:(NSData*)secret forType:(SecDbBackupRecoveryType)type error:(NSError**)error
1016 __block SecDbBackupRecoverySet* set;
1017 __block BOOL ok = YES;
1018 __block NSError* localError;
1019 __block CFErrorRef cfError = NULL;
1020 dispatch_sync(_queue, ^{
1021 ok &= kc_with_dbt_non_item_tables(true, &cfError, ^bool(SecDbConnectionRef dbt) {
1022 ok &= kc_transaction(dbt, &cfError, ^bool{
1023 set = [self onQueueInTransaction:dbt createRecoverySetWithBagSecret:secret forType:type error:&localError];
1029 if (error && cfError) {
1030 *error = CFBridgingRelease(cfError);
1031 } else if (error && localError) {
1032 *error = localError;
1034 CFReleaseNull(cfError);
1039 - (SFECKeyPair*)fetchKCSKForKeyclass:(keyclass_t)keyclass error:(NSError**)error
1041 __block SFECKeyPair* keypair;
1042 __block NSError* localError;
1043 dispatch_sync(_queue, ^{
1044 keypair = [self onQueueFetchKCSKForKeyclass:keyclass error:&localError];
1046 if (localError && error) {
1047 *error = localError;
1055 #endif // SECDB_BACKUPS_ENABLED