2 * Copyright (c) 2015-2017 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@
27 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
30 // Needed headers for implementation
31 #include "AssertMacros.h"
32 #include <utilities/SecCFWrappers.h>
33 #include <utilities/SecAKSWrappers.h>
34 #include <utilities/SecBuffer.h>
35 #include <utilities/SecCFError.h>
36 #include <utilities/der_set.h>
37 #include <utilities/der_plist_internal.h>
38 #include <Security/SecRandom.h>
39 #include <Security/SecureObjectSync/SOSPeerInfo.h>
40 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
41 #include <Security/SecureObjectSync/SOSInternal.h>
43 #include <corecrypto/ccec.h>
44 #include <corecrypto/ccsha2.h>
45 #include <corecrypto/ccrng.h>
49 #include "SecRecoveryKey.h"
50 #include "SOSKeyedPubKeyIdentifier.h"
51 #include "SOSInternal.h"
52 #include "SecADWrapper.h"
53 #include "SecCFAllocator.h"
55 CFStringRef bskbRkbgPrefix = CFSTR("RK");
58 // MARK: Type creation
61 CFGiblisFor(SOSBackupSliceKeyBag);
63 const int kAKSBagSecretLength = 32;
65 struct __OpaqueSOSBackupSliceKeyBag {
71 CFDictionaryRef wrapped_keys;
74 static void SOSBackupSliceKeyBagDestroy(CFTypeRef aObj) {
75 SOSBackupSliceKeyBagRef vb = (SOSBackupSliceKeyBagRef) aObj;
77 CFReleaseNull(vb->aks_bag);
78 CFReleaseNull(vb->peers);
79 CFReleaseNull(vb->wrapped_keys);
82 static CFSetRef SOSBackupSliceKeyBagCopyPeerNames(SOSBackupSliceKeyBagRef bksb) {
83 CFMutableSetRef retval = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
84 if(!retval) return NULL;
85 CFSetForEach(bksb->peers, ^(const void *value) {
86 SOSPeerInfoRef pi = (SOSPeerInfoRef) value;
87 CFSetAddValue(retval, SOSPeerInfoGetPeerName(pi));
92 static CFStringRef SOSBackupSliceKeyBagCopyFormatDescription(CFTypeRef aObj, CFDictionaryRef formatOptions) {
93 SOSBackupSliceKeyBagRef vb = (SOSBackupSliceKeyBagRef) aObj;
95 CFMutableStringRef retval = CFStringCreateMutable(kCFAllocatorDefault, 0);
97 CFSetRef peerIDs = SOSBackupSliceKeyBagCopyPeerNames(vb);
98 CFStringSetPerformWithDescription(peerIDs, ^(CFStringRef description) {
99 CFStringAppendFormat(retval, NULL, CFSTR("<SOSBackupSliceKeyBag@%p %ld %@"), vb, vb->peers ? CFSetGetCount(vb->peers) : 0, description);
101 CFReleaseNull(peerIDs);
102 CFStringAppend(retval, CFSTR(">"));
109 // MARK: Encode/Decode
112 const uint8_t* der_decode_BackupSliceKeyBag(CFAllocatorRef allocator,
113 SOSBackupSliceKeyBagRef* BackupSliceKeyBag, CFErrorRef *error,
114 const uint8_t* der, const uint8_t *der_end) {
115 if (der == NULL) return der;
117 const uint8_t *result = NULL;
118 SOSBackupSliceKeyBagRef vb = CFTypeAllocate(SOSBackupSliceKeyBag, struct __OpaqueSOSBackupSliceKeyBag, allocator);
119 require_quiet(SecAllocationError(vb, error, CFSTR("View bag allocation failed")), fail);
121 const uint8_t *sequence_end = NULL;
122 der = ccder_decode_sequence_tl(&sequence_end, der, der_end);
123 require_quiet(sequence_end == der_end, fail);
125 der = der_decode_data(kCFAllocatorDefault, kCFPropertyListImmutable, &vb->aks_bag, error, der, sequence_end);
126 vb->peers = SOSPeerInfoSetCreateFromArrayDER(kCFAllocatorDefault, &kSOSPeerSetCallbacks, error,
128 der = der_decode_dictionary(kCFAllocatorDefault, kCFPropertyListImmutable, &vb->wrapped_keys, error, der, sequence_end);
130 require_quiet(SecRequirementError(der == der_end, error, CFSTR("Extra space in sequence")), fail);
132 if (BackupSliceKeyBag)
133 CFTransferRetained(*BackupSliceKeyBag, vb);
142 size_t der_sizeof_BackupSliceKeyBag(SOSBackupSliceKeyBagRef BackupSliceKeyBag, CFErrorRef *error) {
145 require_quiet(SecRequirementError(BackupSliceKeyBag != NULL, error, CFSTR("Null BackupSliceKeyBag")), fail);
146 require_quiet(BackupSliceKeyBag != NULL, fail); // this is redundant with what happens in SecRequirementError, but the analyzer can't understand that.
147 require_quiet(SecRequirementError(BackupSliceKeyBag->aks_bag != NULL, error, CFSTR("null aks_bag in BackupSliceKeyBag")), fail);
148 require_quiet(BackupSliceKeyBag->aks_bag != NULL, fail); // this is redundant with what happens in SecRequirementError, but the analyzer can't understand that.
150 size_t bag_size = der_sizeof_data(BackupSliceKeyBag->aks_bag, error);
151 require_quiet(bag_size, fail);
153 size_t peers_size = SOSPeerInfoSetGetDEREncodedArraySize(BackupSliceKeyBag->peers, error);
154 require_quiet(peers_size, fail);
156 size_t wrapped_keys_size = der_sizeof_dictionary(BackupSliceKeyBag->wrapped_keys, error);
157 require_quiet(wrapped_keys_size, fail);
159 result = ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, bag_size + peers_size + wrapped_keys_size);
165 uint8_t* der_encode_BackupSliceKeyBag(SOSBackupSliceKeyBagRef set, CFErrorRef *error,
166 const uint8_t *der, uint8_t *der_end) {
167 uint8_t *result = NULL;
168 if (der_end == NULL) return der_end;
170 require_quiet(SecRequirementError(set != NULL, error, CFSTR("Null set passed to encode")), fail);
171 require_quiet(set, fail); // Silence the NULL warning.
173 require_quiet(SecRequirementError(set->aks_bag != NULL, error, CFSTR("Null set passed to encode")), fail);
174 require_quiet(set->aks_bag, fail); // Silence the warning.
176 der_end = ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
177 der_encode_data(set->aks_bag, error, der,
178 SOSPeerInfoSetEncodeToArrayDER(set->peers, error, der,
179 der_encode_dictionary(set->wrapped_keys, error, der, der_end))));
181 require_quiet(der_end == der, fail);
190 SOSBackupSliceKeyBagRef SOSBackupSliceKeyBagCreateFromData(CFAllocatorRef allocator, CFDataRef data, CFErrorRef *error) {
191 SOSBackupSliceKeyBagRef result = NULL;
192 SOSBackupSliceKeyBagRef decodedBag = NULL;
194 const uint8_t *der = CFDataGetBytePtr(data);
195 const uint8_t *der_end = der + CFDataGetLength(data);
197 der = der_decode_BackupSliceKeyBag(allocator, &decodedBag, error, der, der_end);
199 require_quiet(SecRequirementError(der == der_end, error, CFSTR("Didn't consume all data supplied")), fail);
201 CFTransferRetained(result, decodedBag);
204 CFReleaseNull(decodedBag);
209 // MARK: Construction
212 bool SOSBSKBIsGoodBackupPublic(CFDataRef publicKey, CFErrorRef *error) {
215 ccec_pub_ctx_decl_cp(SOSGetBackupKeyCurveParameters(), pub_key);
217 int cc_result = ccec_compact_import_pub(SOSGetBackupKeyCurveParameters(), CFDataGetLength(publicKey), CFDataGetBytePtr(publicKey), pub_key);
219 require_action_quiet(cc_result == 0, exit, SOSErrorCreate(kSOSErrorDecodeFailure, error, NULL, CFSTR("Unable to decode public key: %@"), publicKey));
228 static CFDataRef SOSCopyECWrapped(CFDataRef publicKey, CFDataRef secret, CFErrorRef *error) {
229 CFDataRef result = NULL;
231 ccec_pub_ctx_decl_cp(SOSGetBackupKeyCurveParameters(), pub_key);
233 int cc_result = ccec_compact_import_pub(SOSGetBackupKeyCurveParameters(), CFDataGetLength(publicKey), CFDataGetBytePtr(publicKey), pub_key);
235 require_action_quiet(cc_result == 0, exit, SOSErrorCreate(kSOSErrorDecodeFailure, error, NULL, CFSTR("Unable to decode public key: %@"), publicKey));
237 result = SOSCopyECWrappedData(pub_key, secret, error);
244 static CFDictionaryRef SOSBackupSliceKeyBagCopyWrappedKeys(SOSBackupSliceKeyBagRef vb, CFDataRef secret, CFDictionaryRef additionalKeys, CFErrorRef *error) {
245 CFDictionaryRef result = NULL;
246 CFMutableDictionaryRef wrappedKeys = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
248 __block bool success = true;
249 CFSetForEach(vb->peers, ^(const void *value) {
250 SOSPeerInfoRef pi = (SOSPeerInfoRef) value;
251 if (isSOSPeerInfo(pi)) {
252 CFStringRef id = SOSPeerInfoGetPeerID(pi);
253 CFDataRef backupKey = SOSPeerInfoCopyBackupKey(pi);
256 CFErrorRef wrapError = NULL;
257 CFDataRef wrappedKey = SOSCopyECWrapped(backupKey, secret, &wrapError);
259 CFDictionaryAddValue(wrappedKeys, id, wrappedKey);
260 CFDataPerformWithHexString(backupKey, ^(CFStringRef backupKeyString) {
261 CFDataPerformWithHexString(wrappedKey, ^(CFStringRef wrappedKeyString) {
262 secnotice("bskb", "Add for id: %@, bk: %@, wrapped: %@", id, backupKeyString, wrappedKeyString);
266 CFDataPerformWithHexString(backupKey, ^(CFStringRef backupKeyString) {
267 secnotice("bskb", "Failed at id: %@, bk: %@ error: %@", id, backupKeyString, wrapError);
269 CFErrorPropagate(wrapError, error);
272 CFReleaseNull(wrappedKey);
273 CFReleaseNull(backupKey);
275 secnotice("bskb", "Skipping id %@, no backup key.", id);
281 CFDictionaryForEach(additionalKeys, ^(const void *key, const void *value) {
282 CFStringRef prefix = asString(key, NULL);
283 CFDataRef backupKey = asData(value, NULL);
285 CFDataRef wrappedKey = NULL;
286 CFErrorRef localError = NULL;
287 CFStringRef id = SOSKeyedPubKeyIdentifierCreateWithData(prefix, backupKey);
288 require_quiet(id, done);
290 wrappedKey = SOSCopyECWrapped(backupKey, secret, &localError);
291 require_quiet(wrappedKey, done);
293 CFDictionaryAddValue(wrappedKeys, id, wrappedKey);
297 CFDataPerformWithHexString(backupKey, ^(CFStringRef backupKeyString) {
298 CFDataPerformWithHexString(wrappedKey, ^(CFStringRef wrappedKeyString) {
299 secnotice("bskb", "Add for bk: %@, wrapped: %@", backupKeyString, wrappedKeyString);
303 CFDataPerformWithHexString(backupKey, ^(CFStringRef backupKeyString) {
304 secnotice("bskb", "Failed at bk: %@ error: %@", backupKeyString, localError);
306 CFErrorPropagate(localError, error);
309 CFReleaseNull(wrappedKey);
312 secnotice("bskb", "Skipping %@, not data.", value);
317 CFTransferRetained(result, wrappedKeys);
319 CFReleaseNull(wrappedKeys);
323 static bool SOSBackupSliceKeyBagCreateBackupBag(SOSBackupSliceKeyBagRef vb, CFDictionaryRef/*CFDataRef*/ additionalKeys, CFErrorRef* error) {
324 CFReleaseNull(vb->aks_bag);
326 // Choose a random key.
327 PerformWithBufferAndClear(kAKSBagSecretLength, ^(size_t size, uint8_t *buffer) {
328 CFDataRef secret = NULL;
330 require_quiet(SecError(SecRandomCopyBytes(kSecRandomDefault, size, buffer), error, CFSTR("SecRandom falied!")), fail);
332 secret = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, buffer, size, kCFAllocatorNull);
334 CFAssignRetained(vb->wrapped_keys, SOSBackupSliceKeyBagCopyWrappedKeys(vb, secret, additionalKeys, error));
335 CFAssignRetained(vb->aks_bag, SecAKSCopyBackupBagWithSecret(size, buffer, error));
338 CFReleaseSafe(secret);
341 return vb->aks_bag != NULL;
345 CFDataRef SOSBSKBCopyEncoded(SOSBackupSliceKeyBagRef BackupSliceKeyBag, CFErrorRef* error) {
346 CFDataRef result = NULL;
347 CFMutableDataRef encoded = NULL;
349 size_t encodedSize = der_sizeof_BackupSliceKeyBag(BackupSliceKeyBag, error);
350 require_quiet(encodedSize, fail);
352 encoded = CFDataCreateMutableWithScratch(kCFAllocatorDefault, encodedSize);
353 require_quiet(SecAllocationError(encoded, error, CFSTR("Faild to create scratch")), fail);
355 uint8_t *encode_to = CFDataGetMutableBytePtr(encoded);
356 uint8_t *encode_to_end = encode_to + CFDataGetLength(encoded);
357 require_quiet(encode_to == der_encode_BackupSliceKeyBag(BackupSliceKeyBag, error, encode_to, encode_to_end), fail);
359 CFTransferRetained(result, encoded);
362 CFReleaseSafe(encoded);
366 static CFSetRef SOSBackupSliceKeyBagCreatePeerSet(CFAllocatorRef allocator, CFSetRef peers) {
367 CFMutableSetRef result = CFSetCreateMutableForSOSPeerInfosByID(allocator);
369 CFSetForEach(peers, ^(const void *value) {
370 CFSetAddValue(result, value);
376 SOSBackupSliceKeyBagRef SOSBackupSliceKeyBagCreate(CFAllocatorRef allocator, CFSetRef peers, CFErrorRef* error) {
377 CFMutableDictionaryRef additionalKeys = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
379 SOSBackupSliceKeyBagRef result = SOSBackupSliceKeyBagCreateWithAdditionalKeys(allocator, peers, additionalKeys, NULL);
381 CFReleaseNull(additionalKeys);
386 SOSBackupSliceKeyBagRef SOSBackupSliceKeyBagCreateWithAdditionalKeys(CFAllocatorRef allocator,
387 CFSetRef /*SOSPeerInfoRef*/ peers,
388 CFDictionaryRef /*CFStringRef (prefix) CFDataRef (keydata) */ additionalKeys,
390 SOSBackupSliceKeyBagRef result = NULL;
391 SOSBackupSliceKeyBagRef vb = CFTypeAllocate(SOSBackupSliceKeyBag, struct __OpaqueSOSBackupSliceKeyBag, allocator);
392 require_quiet(SecAllocationError(vb, error, CFSTR("View bag allocation failed")), fail);
394 require_quiet(SecRequirementError(CFSetGetCount(peers) > 0, error, CFSTR("Need peers")), fail);
396 vb->peers = SOSBackupSliceKeyBagCreatePeerSet(allocator, peers);
397 vb->wrapped_keys = CFDictionaryCreateMutableForCFTypes(allocator);
399 require_quiet(SOSBackupSliceKeyBagCreateBackupBag(vb, additionalKeys, error), fail);
401 CFTransferRetained(result, vb);
409 SOSBackupSliceKeyBagRef SOSBackupSliceKeyBagCreateDirect(CFAllocatorRef allocator, CFDataRef aks_bag, CFErrorRef *error)
411 SOSBackupSliceKeyBagRef result = NULL;
412 SOSBackupSliceKeyBagRef vb = CFTypeAllocate(SOSBackupSliceKeyBag, struct __OpaqueSOSBackupSliceKeyBag, allocator);
413 require_quiet(SecAllocationError(vb, error, CFSTR("View bag allocation failed")), fail);
415 require_quiet(SecRequirementError(aks_bag, error, CFSTR("Need aks bag")), fail);
417 vb->aks_bag = CFRetainSafe(aks_bag);
418 vb->peers = CFSetCreateMutableForSOSPeerInfosByID(allocator);
419 vb->wrapped_keys = CFDictionaryCreateMutableForCFTypes(allocator);
421 CFTransferRetained(result, vb);
432 bool SOSBSKBIsDirect(SOSBackupSliceKeyBagRef backupSliceKeyBag)
434 return 0 == CFSetGetCount(backupSliceKeyBag->peers);
437 CFDataRef SOSBSKBCopyAKSBag(SOSBackupSliceKeyBagRef backupSliceKeyBag, CFErrorRef* error)
439 return CFRetainSafe(backupSliceKeyBag->aks_bag);
442 CFSetRef SOSBSKBGetPeers(SOSBackupSliceKeyBagRef backupSliceKeyBag){
443 return backupSliceKeyBag->peers;
446 int SOSBSKBCountPeers(SOSBackupSliceKeyBagRef backupSliceKeyBag) {
447 return (int) CFSetGetCount(backupSliceKeyBag->peers);
450 bool SOSBSKBPeerIsInKeyBag(SOSBackupSliceKeyBagRef backupSliceKeyBag, SOSPeerInfoRef pi) {
451 return CFSetGetValue(backupSliceKeyBag->peers, pi) != NULL;
456 bool SOSBKSBKeyIsInKeyBag(SOSBackupSliceKeyBagRef backupSliceKeyBag, CFDataRef publicKey) {
458 CFStringRef keyID = SOSCopyIDOfDataBuffer(publicKey, NULL);
459 require_quiet(keyID, done);
461 result = CFDictionaryContainsKey(backupSliceKeyBag->wrapped_keys, keyID);
464 CFReleaseSafe(keyID);
468 bool SOSBKSBPrefixedKeyIsInKeyBag(SOSBackupSliceKeyBagRef backupSliceKeyBag, CFStringRef prefix, CFDataRef publicKey) {
470 CFStringRef kpkid = SOSKeyedPubKeyIdentifierCreateWithData(prefix, publicKey);
471 require_quiet(kpkid, done);
473 result = CFDictionaryContainsKey(backupSliceKeyBag->wrapped_keys, kpkid);
476 CFReleaseSafe(kpkid);
483 bskb_keybag_handle_t SOSBSKBLoadLocked(SOSBackupSliceKeyBagRef backupSliceKeyBag,
486 #if !TARGET_HAS_KEYSTORE
487 return bad_keybag_handle;
489 keybag_handle_t result = bad_keybag_handle;
490 keybag_handle_t bag_handle = bad_keybag_handle;
492 require_quiet(SecRequirementError(backupSliceKeyBag->aks_bag, error,
493 CFSTR("No aks bag to load")), exit);
494 require_quiet(SecRequirementError(CFDataGetLength(backupSliceKeyBag->aks_bag) < INT_MAX, error,
495 CFSTR("No aks bag to load")), exit);
497 kern_return_t aks_result;
498 aks_result = aks_load_bag(CFDataGetBytePtr(backupSliceKeyBag->aks_bag),
499 (int) CFDataGetLength(backupSliceKeyBag->aks_bag),
501 require_quiet(SecKernError(aks_result, error,
502 CFSTR("aks_load_bag failed: %d"), aks_result), exit);
505 bag_handle = bad_keybag_handle;
508 if (bag_handle != bad_keybag_handle) {
509 (void) aks_unload_bag(bag_handle);
517 static keybag_handle_t SOSBSKBLoadAndUnlockBagWithSecret(SOSBackupSliceKeyBagRef backupSliceKeyBag,
518 size_t secretSize, const uint8_t *secret,
521 #if !TARGET_HAS_KEYSTORE
522 return bad_keybag_handle;
524 keybag_handle_t result = bad_keybag_handle;
525 keybag_handle_t bag_handle = bad_keybag_handle;
527 require_quiet(SecRequirementError(backupSliceKeyBag->aks_bag, error,
528 CFSTR("No aks bag to load")), exit);
529 require_quiet(SecRequirementError(CFDataGetLength(backupSliceKeyBag->aks_bag) < INT_MAX, error,
530 CFSTR("No aks bag to load")), exit);
531 require_quiet(SecRequirementError(secretSize <= INT_MAX, error,
532 CFSTR("secret too big")), exit);
534 kern_return_t aks_result;
535 aks_result = aks_load_bag(CFDataGetBytePtr(backupSliceKeyBag->aks_bag),
536 (int) CFDataGetLength(backupSliceKeyBag->aks_bag),
538 require_quiet(SecKernError(aks_result, error,
539 CFSTR("aks_load_bag failed: %d"), aks_result), exit);
541 aks_result = aks_unlock_bag(bag_handle, secret, (int) secretSize);
542 require_quiet(SecKernError(aks_result, error,
543 CFSTR("failed to unlock bag: %d"), aks_result), exit);
546 bag_handle = bad_keybag_handle;
549 if (bag_handle != bad_keybag_handle) {
550 (void) aks_unload_bag(bag_handle);
557 keybag_handle_t SOSBSKBLoadAndUnlockWithPeerIDAndSecret(SOSBackupSliceKeyBagRef backupSliceKeyBag,
558 CFStringRef peerID, CFDataRef peerSecret,
561 __block keybag_handle_t result = bad_keybag_handle;
563 CFDataRef lookedUpData = CFDictionaryGetValue(backupSliceKeyBag->wrapped_keys, peerID);
564 require_quiet(SecRequirementError(lookedUpData != NULL, error, CFSTR("%@ has no wrapped key in %@"), peerID, backupSliceKeyBag), exit);
566 require_quiet(SOSPerformWithDeviceBackupFullKey(SOSGetBackupKeyCurveParameters(), peerSecret, error, ^(ccec_full_ctx_t fullKey) {
567 SOSPerformWithUnwrappedData(fullKey, lookedUpData, error, ^(size_t size, uint8_t *buffer) {
568 result = SOSBSKBLoadAndUnlockBagWithSecret(backupSliceKeyBag, size, buffer, error);
577 keybag_handle_t SOSBSKBLoadAndUnlockWithPeerSecret(SOSBackupSliceKeyBagRef backupSliceKeyBag,
578 SOSPeerInfoRef peer, CFDataRef peerSecret,
581 return SOSBSKBLoadAndUnlockWithPeerIDAndSecret(backupSliceKeyBag, SOSPeerInfoGetPeerID(peer), peerSecret, error);
584 keybag_handle_t SOSBSKBLoadAndUnlockWithDirectSecret(SOSBackupSliceKeyBagRef backupSliceKeyBag,
588 keybag_handle_t result = bad_keybag_handle;
589 require_quiet(SecRequirementError(SOSBSKBIsDirect(backupSliceKeyBag), error, CFSTR("Not direct bag")), exit);
591 result = SOSBSKBLoadAndUnlockBagWithSecret(backupSliceKeyBag,
592 CFDataGetLength(secret),
593 CFDataGetBytePtr(secret),
600 #include "SecRecoveryKey.h"
602 static bool SOSPerformWithRecoveryKeyFullKey(CFDataRef wrappingSecret, CFErrorRef *error, void (^operation)(ccec_full_ctx_t fullKey, CFStringRef keyID)) {
605 CFStringRef keyID = NULL;
606 NSError *nserror = NULL;
607 SecRecoveryKey *sRecKey = NULL;
608 NSData* fullKeyBytes = NULL;
609 NSData* pubKeyBytes = NULL;
610 CFStringRef restoreKeySecret = CFStringCreateWithBytes(SecCFAllocatorZeroize(), CFDataGetBytePtr(wrappingSecret), CFDataGetLength(wrappingSecret), kCFStringEncodingUTF8, false);
611 require_action_quiet(restoreKeySecret, errOut, SOSErrorCreate(kSOSErrorDecodeFailure, error, NULL, CFSTR("Unable to create key string from data.")));
613 sRecKey = SecRKCreateRecoveryKeyWithError((__bridge NSString *)(restoreKeySecret), &nserror);
616 *error = (CFErrorRef)CFBridgingRetain(nserror);
620 require_action_quiet(sRecKey, errOut, SOSErrorCreate(kSOSErrorDecodeFailure, error, NULL, CFSTR("Unable to create recovery key from string.")));
622 fullKeyBytes = SecRKCopyBackupFullKey(sRecKey);
623 pubKeyBytes = SecRKCopyBackupPublicKey(sRecKey);
624 require_action_quiet(fullKeyBytes && pubKeyBytes, errOut, SOSErrorCreate(kSOSErrorDecodeFailure, error, NULL, CFSTR("Unable to get recovery key public and private keys.")));
625 keyID = SOSCopyIDOfDataBuffer(((__bridge CFDataRef) pubKeyBytes), error);
626 require_quiet(keyID, errOut);
628 size_t keysize = ccec_compact_import_priv_size(CFDataGetLength((__bridge CFDataRef)fullKeyBytes));
629 ccec_const_cp_t cp = ccec_curve_for_length_lookup(keysize, ccec_cp_256(), ccec_cp_384(), ccec_cp_521());
630 ccec_full_ctx_decl_cp(cp, fullKey);
631 int res = ccec_compact_import_priv(cp, CFDataGetLength((__bridge CFDataRef)fullKeyBytes), CFDataGetBytePtr((__bridge CFDataRef)fullKeyBytes), fullKey);
633 operation(fullKey, keyID);
635 ccec_full_ctx_clear_cp(cp, fullKey);
638 if(!result) SOSErrorCreate(kSOSErrorProcessingFailure, error, NULL, CFSTR("Unable to perform crypto operation from fullKeyBytes."));
641 CFReleaseNull(keyID);
642 CFReleaseNull(restoreKeySecret);
646 bskb_keybag_handle_t SOSBSKBLoadAndUnlockWithWrappingSecret(SOSBackupSliceKeyBagRef backupSliceKeyBag,
647 CFDataRef wrappingSecret,
649 __block keybag_handle_t result = bad_keybag_handle;
651 CFDataRef lookedUpData = SOSBSKBCopyRecoveryKey(backupSliceKeyBag);
652 require_quiet(SecRequirementError(lookedUpData != NULL, error, CFSTR("no recovery key found in %@"), backupSliceKeyBag), errOut);
654 SOSPerformWithRecoveryKeyFullKey(wrappingSecret, error, ^(ccec_full_ctx_t fullKey, CFStringRef keyID) {
655 SOSPerformWithUnwrappedData(fullKey, lookedUpData, error, ^(size_t size, uint8_t *buffer) {
656 result = SOSBSKBLoadAndUnlockBagWithSecret(backupSliceKeyBag, size, buffer, error);
661 CFReleaseSafe(lookedUpData);
665 // fixed for <rdar://problem/30918831> CrashTracer: secdtests at secdtests: SOSBSKBCopyAdditionalKeysWithPrefix
666 static CFDictionaryRef SOSBSKBCopyAdditionalKeysWithPrefix(CFAllocatorRef allocator, SOSBackupSliceKeyBagRef bskb, CFStringRef prefix) {
667 if(!bskb || !bskb->wrapped_keys) return NULL;
668 CFMutableDictionaryRef retval = CFDictionaryCreateMutableForCFTypes(allocator);
669 if(!retval) return NULL;
670 CFDictionaryForEach(bskb->wrapped_keys, ^(const void *key, const void *value) {
671 CFStringRef kpkid = asString(key, NULL);
672 CFDataRef keyData = asData(value, NULL);
673 if(kpkid && keyData && SOSKeyedPubKeyIdentifierIsPrefixed(kpkid)) {
674 CFStringRef idPrefix = SOSKeyedPubKeyIdentifierCopyPrefix(kpkid);
675 if(CFEqualSafe(idPrefix, prefix)) {
676 CFDictionaryAddValue(retval, kpkid, keyData);
678 CFReleaseNull(idPrefix);
684 static bool SOSBSKBHasPrefixedKey(SOSBackupSliceKeyBagRef bskb, CFStringRef prefix) {
685 CFDictionaryRef keyDict = SOSBSKBCopyAdditionalKeysWithPrefix(kCFAllocatorDefault, bskb, prefix);
686 bool haveKeys = keyDict && CFDictionaryGetCount(keyDict) > 0;
687 CFReleaseNull(keyDict);
691 CFDataRef SOSBSKBCopyRecoveryKey(SOSBackupSliceKeyBagRef bskb) {
692 CFDictionaryRef keyDict = SOSBSKBCopyAdditionalKeysWithPrefix(kCFAllocatorDefault, bskb, bskbRkbgPrefix);
693 if(!keyDict) return NULL;
694 if(CFDictionaryGetCount(keyDict) == 1) {
695 __block CFDataRef keyData = NULL;
696 CFDictionaryForEach(keyDict, ^(const void *key, const void *value) {
697 keyData = asData(value, NULL);
699 return CFDataCreateCopy(kCFAllocatorDefault, keyData);
701 CFReleaseNull(keyDict);
705 bool SOSBSKBHasRecoveryKey(SOSBackupSliceKeyBagRef bskb) {
706 if(SOSBSKBHasPrefixedKey(bskb, bskbRkbgPrefix)) return true;
707 // old way for RecoveryKeys
708 int keyCount = (int) CFDictionaryGetCount(bskb->wrapped_keys);
709 int peerCount = SOSBSKBCountPeers(bskb);
710 return !SOSBSKBIsDirect(bskb) && ((keyCount - peerCount) > 0);