2 * Copyright (c) 2012-2014 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@
25 #include <AssertMacros.h>
27 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
28 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
29 #include <Security/SecureObjectSync/SOSPeerInfoDER.h>
30 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
32 #include <Security/SecureObjectSync/SOSCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSPeerInfoDER.h>
37 #include <Security/SecKeyPriv.h>
38 #include <Security/SecItemPriv.h>
39 #include <Security/SecOTR.h>
40 #include <CoreFoundation/CFArray.h>
41 #include <dispatch/dispatch.h>
42 #include <Security/SecFramework.h>
47 #include <utilities/SecCFWrappers.h>
48 #include <utilities/SecCFRelease.h>
50 #include <utilities/der_plist.h>
51 #include <utilities/der_plist_internal.h>
52 #include <corecrypto/ccder.h>
54 #include <CommonCrypto/CommonDigest.h>
55 #include <CommonCrypto/CommonDigestSPI.h>
57 #include <CoreFoundation/CoreFoundation.h>
59 #include "utilities/iOSforOSX.h"
61 #include <AssertMacros.h>
63 #include <utilities/SecCFError.h>
72 extern OSStatus SecKeyCopyPublicBytes(SecKeyRef key, CFDataRef* publicBytes);
73 extern SecKeyRef SecKeyCreatePublicFromPrivate(SecKeyRef privateKey);
78 struct __OpaqueSOSFullPeerInfo {
81 SOSPeerInfoRef peer_info;
83 CFDataRef octagon_peer_signing_key_ref;
84 CFDataRef octagon_peer_encryption_key_ref;
87 CFGiblisWithHashFor(SOSFullPeerInfo);
91 CFStringRef kSOSFullPeerInfoDescriptionKey = CFSTR("SOSFullPeerInfoDescription");
92 CFStringRef kSOSFullPeerInfoSignatureKey = CFSTR("SOSFullPeerInfoSignature");
93 CFStringRef kSOSFullPeerInfoNameKey = CFSTR("SOSFullPeerInfoName");
96 bool SOSFullPeerInfoUpdate(SOSFullPeerInfoRef fullPeerInfo, CFErrorRef *error, SOSPeerInfoRef (^create_modification)(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error)) {
99 SOSPeerInfoRef newPeer = NULL;
100 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(fullPeerInfo, error);
101 require_quiet(device_key, fail);
103 newPeer = create_modification(fullPeerInfo->peer_info, device_key, error);
104 require_quiet(newPeer, fail);
106 CFTransferRetained(fullPeerInfo->peer_info, newPeer);
110 CFReleaseNull(device_key);
111 CFReleaseNull(newPeer);
115 bool SOSFullPeerInfoUpdateToThisPeer(SOSFullPeerInfoRef peer, SOSPeerInfoRef pi, CFErrorRef *error) {
116 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
117 return SOSPeerInfoSign(key, pi, error) ? CFRetainSafe(pi): NULL;
121 SOSFullPeerInfoRef SOSFullPeerInfoCreate(CFAllocatorRef allocator, CFDictionaryRef gestalt,
123 SecKeyRef signingKey,
124 SecKeyRef octagonPeerSigningKey,
125 SecKeyRef octagonPeerEncryptionKey,
127 return SOSFullPeerInfoCreateWithViews(allocator, gestalt, backupKey, NULL, signingKey,
128 octagonPeerSigningKey, octagonPeerEncryptionKey, error);
131 SOSFullPeerInfoRef SOSFullPeerInfoCreateWithViews(CFAllocatorRef allocator,
132 CFDictionaryRef gestalt, CFDataRef backupKey, CFSetRef initialViews,
133 SecKeyRef signingKey,
134 SecKeyRef octagonPeerSigningKey,
135 SecKeyRef octagonPeerEncryptionKey,
138 SOSFullPeerInfoRef result = NULL;
139 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
141 CFStringRef IDSID = CFSTR("");
142 CFStringRef transportType = SOSTransportMessageTypeKVS;
143 CFBooleanRef preferIDS = kCFBooleanFalse;
144 CFBooleanRef preferIDSFragmentation = kCFBooleanTrue;
145 CFBooleanRef preferACKModel = kCFBooleanTrue;
147 fpi->peer_info = SOSPeerInfoCreateWithTransportAndViews(allocator, gestalt, backupKey,
148 IDSID, transportType, preferIDS,
149 preferIDSFragmentation, preferACKModel, initialViews,
150 signingKey, octagonPeerSigningKey, octagonPeerEncryptionKey, error);
151 require_quiet(fpi->peer_info, exit);
153 OSStatus status = SecKeyCopyPersistentRef(signingKey, &fpi->key_ref);
154 require_quiet(SecError(status, error, CFSTR("Inflating persistent ref")), exit);
156 status = SecKeyCopyPersistentRef(octagonPeerSigningKey, &fpi->octagon_peer_signing_key_ref);
157 require_quiet(SecError(status, error, CFSTR("Inflating octagon peer signing persistent ref")), exit);
158 status = SecKeyCopyPersistentRef(octagonPeerSigningKey, &fpi->octagon_peer_encryption_key_ref);
159 require_quiet(SecError(status, error, CFSTR("Inflating octagon peer encryption persistent ref")), exit);
161 CFTransferRetained(result, fpi);
168 SOSFullPeerInfoRef SOSFullPeerInfoCopyFullPeerInfo(SOSFullPeerInfoRef toCopy) {
169 SOSFullPeerInfoRef retval = NULL;
170 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, kCFAllocatorDefault);
171 SOSPeerInfoRef piToCopy = SOSFullPeerInfoGetPeerInfo(toCopy);
173 require_quiet(piToCopy, errOut);
174 require_quiet(fpi, errOut);
175 fpi->peer_info = SOSPeerInfoCreateCopy(kCFAllocatorDefault, piToCopy, NULL);
176 require_quiet(fpi->peer_info, errOut);
177 fpi->key_ref = CFRetainSafe(toCopy->key_ref);
178 CFTransferRetained(retval, fpi);
185 bool SOSFullPeerInfoUpdateOctagonSigningKey(SOSFullPeerInfoRef peer, SecKeyRef octagonSigningKey, CFErrorRef* error){
186 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
187 return SOSPeerInfoSetOctagonSigningKey(kCFAllocatorDefault, peer, octagonSigningKey, key, error);
191 bool SOSFullPeerInfoUpdateOctagonEncryptionKey(SOSFullPeerInfoRef peer, SecKeyRef octagonEncryptionKey, CFErrorRef* error){
192 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
193 return SOSPeerInfoSetOctagonEncryptionKey(kCFAllocatorDefault, peer, octagonEncryptionKey, key, error);
199 CFDataRef SOSPeerInfoCopyData(SOSPeerInfoRef pi, CFErrorRef *error)
201 CFTypeRef vData = NULL;
202 SecKeyRef pubKey = SOSPeerInfoCopyPubKey(pi, error);
203 CFDictionaryRef query = NULL;
204 require_quiet(pubKey, exit);
207 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(pubKey);
209 query = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
210 kSecClass, kSecClassKey,
211 kSecAttrKeyClass, kSecAttrKeyClassPrivate,
212 kSecAttrSynchronizable, kSecAttrSynchronizableAny,
213 kSecAttrApplicationLabel, public_key_hash,
214 kSecReturnData, kCFBooleanTrue,
216 CFReleaseNull(public_key_hash);
218 require_quiet(SecError(SecItemCopyMatching(query, &vData),error ,
219 CFSTR("Error finding persistent ref to key from public: %@"), pubKey), exit);
222 CFReleaseNull(query);
223 CFReleaseNull(pubKey);
225 secnotice("fpi","no private key found");
226 return (CFDataRef)vData;
229 SOSFullPeerInfoRef SOSFullPeerInfoCreateCloudIdentity(CFAllocatorRef allocator, SOSPeerInfoRef peer, CFErrorRef* error) {
230 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
232 SecKeyRef pubKey = NULL;
234 fpi->peer_info = peer;
235 CFRetainSafe(fpi->peer_info);
236 if (fpi->peer_info == NULL) {
241 pubKey = SOSPeerInfoCopyPubKey(peer, error);
242 require_quiet(pubKey, exit);
244 fpi->key_ref = SecKeyCreatePersistentRefToMatchingPrivateKey(pubKey, error);
246 if (fpi->key_ref == NULL) {
252 CFReleaseNull(pubKey);
257 SOSFullPeerInfoRef SOSFullPeerInfoCreateFromDER(CFAllocatorRef allocator, CFErrorRef* error,
258 const uint8_t** der_p, const uint8_t *der_end) {
259 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
261 const uint8_t *sequence_end;
263 *der_p = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, *der_p, der_end);
264 CFReleaseNull(fpi->peer_info);
265 fpi->peer_info = SOSPeerInfoCreateFromDER(allocator, error, der_p, der_end);
266 require_quiet(fpi->peer_info != NULL, fail);
268 *der_p = der_decode_data(allocator, kCFPropertyListImmutable, &fpi->key_ref, error, *der_p, sequence_end);
269 require_quiet(*der_p != NULL, fail);
278 SOSFullPeerInfoRef SOSFullPeerInfoCreateFromData(CFAllocatorRef allocator, CFDataRef fullPeerData, CFErrorRef *error)
280 if(!fullPeerData) return NULL;
281 size_t size = CFDataGetLength(fullPeerData);
282 const uint8_t *der = CFDataGetBytePtr(fullPeerData);
283 SOSFullPeerInfoRef inflated = SOSFullPeerInfoCreateFromDER(allocator, error, &der, der + size);
287 static void SOSFullPeerInfoDestroy(CFTypeRef aObj) {
288 SOSFullPeerInfoRef fpi = (SOSFullPeerInfoRef) aObj;
290 CFReleaseNull(fpi->peer_info);
291 CFReleaseNull(fpi->key_ref);
292 CFReleaseNull(fpi->octagon_peer_signing_key_ref);
293 CFReleaseNull(fpi->octagon_peer_encryption_key_ref);
296 static Boolean SOSFullPeerInfoCompare(CFTypeRef lhs, CFTypeRef rhs) {
297 SOSFullPeerInfoRef lpeer = (SOSFullPeerInfoRef) lhs;
298 SOSFullPeerInfoRef rpeer = (SOSFullPeerInfoRef) rhs;
300 if (!CFEqual(lpeer->peer_info, rpeer->peer_info))
303 if (CFEqual(lpeer->key_ref, rpeer->key_ref))
306 SecKeyRef lpk = SOSFullPeerInfoCopyDeviceKey(lpeer, NULL);
307 SecKeyRef rpk = SOSFullPeerInfoCopyDeviceKey(rpeer, NULL);
309 bool match = lpk && rpk && CFEqual(lpk, rpk);
317 static CFHashCode SOSFullPeerInfoHash(CFTypeRef cf) {
318 SOSFullPeerInfoRef peer = (SOSFullPeerInfoRef) cf;
320 return CFHash(peer->peer_info);
323 static CFStringRef SOSFullPeerInfoCopyFormatDescription(CFTypeRef aObj, CFDictionaryRef formatOptions) {
324 SOSFullPeerInfoRef fpi = (SOSFullPeerInfoRef) aObj;
326 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SOSFullPeerInfo@%p: \"%@\">"), fpi, fpi->peer_info);
329 bool SOSFullPeerInfoUpdateGestalt(SOSFullPeerInfoRef peer, CFDictionaryRef gestalt, CFErrorRef* error)
331 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
332 return SOSPeerInfoCopyWithGestaltUpdate(kCFAllocatorDefault, peer, gestalt, key, error);
336 bool SOSFullPeerInfoUpdateV2Dictionary(SOSFullPeerInfoRef peer, CFDictionaryRef newv2dict, CFErrorRef* error)
338 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
339 return SOSPeerInfoCopyWithV2DictionaryUpdate(kCFAllocatorDefault, peer,
340 newv2dict, key, error);
344 bool SOSFullPeerInfoUpdateBackupKey(SOSFullPeerInfoRef peer, CFDataRef backupKey, CFErrorRef* error)
346 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
347 return SOSPeerInfoCopyWithBackupKeyUpdate(kCFAllocatorDefault, peer, backupKey, key, error);
351 bool SOSFullPeerInfoAddEscrowRecord(SOSFullPeerInfoRef peer, CFStringRef dsid, CFDictionaryRef escrowRecord, CFErrorRef* error)
353 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
354 return SOSPeerInfoCopyWithEscrowRecordUpdate(kCFAllocatorDefault, peer, dsid, escrowRecord, key, error);
358 bool SOSFullPeerInfoReplaceEscrowRecords(SOSFullPeerInfoRef peer, CFDictionaryRef escrowRecords, CFErrorRef* error)
360 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
361 return SOSPeerInfoCopyWithReplacedEscrowRecords(kCFAllocatorDefault, peer, escrowRecords, key, error);
365 SOSViewResultCode SOSFullPeerInfoUpdateViews(SOSFullPeerInfoRef peer, SOSViewActionCode action, CFStringRef viewname, CFErrorRef* error)
367 __block SOSViewResultCode retval = kSOSCCGeneralViewError;
369 secnotice("viewChange", "%s view %@", SOSViewsXlateAction(action), viewname);
371 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
372 return SOSPeerInfoCopyWithViewsChange(kCFAllocatorDefault, peer, action, viewname, &retval, key, error);
373 }) ? retval : kSOSCCGeneralViewError;
376 static CFMutableSetRef SOSFullPeerInfoCopyViewUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
377 CFSetRef enabledViews = SOSPeerInfoCopyEnabledViews(peer->peer_info);
378 CFMutableSetRef newViews = SOSPeerInfoCopyEnabledViews(peer->peer_info);
380 if (isSet(minimumViews)) {
381 CFSetUnion(newViews, minimumViews);
383 if (isSet(excludedViews)) {
384 CFSetSubtract(newViews, excludedViews);
387 if (CFEqualSafe(newViews, enabledViews)) {
388 CFReleaseNull(newViews);
391 CFReleaseNull(enabledViews);
395 static bool SOSFullPeerInfoNeedsViewUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
396 CFSetRef updatedViews = SOSFullPeerInfoCopyViewUpdate(peer, minimumViews, excludedViews);
397 bool needsUpdate = (updatedViews != NULL);
398 CFReleaseNull(updatedViews);
402 static bool sosFullPeerInfoRequiresUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
404 if(!SOSPeerInfoVersionIsCurrent(peer->peer_info)) return true;
405 if(!SOSPeerInfoSerialNumberIsSet(peer->peer_info)) return true;
406 if(SOSFullPeerInfoNeedsViewUpdate(peer, minimumViews, excludedViews)) return true;
411 // Returning false indicates we don't need to upgrade.
412 bool SOSFullPeerInfoUpdateToCurrent(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
413 bool success = false;
415 CFMutableSetRef newViews = NULL;
416 CFErrorRef copyError = NULL;
417 CFErrorRef createError = NULL;
418 SecKeyRef device_key = NULL;
420 require_quiet(sosFullPeerInfoRequiresUpdate(peer, minimumViews, excludedViews), errOut);
422 newViews = SOSFullPeerInfoCopyViewUpdate(peer, minimumViews, excludedViews);
424 device_key = SOSFullPeerInfoCopyDeviceKey(peer, ©Error);
425 require_action_quiet(device_key, errOut,
426 secnotice("upgrade", "SOSFullPeerInfoCopyDeviceKey failed: %@", copyError));
428 SOSPeerInfoRef newPeer = SOSPeerInfoCreateCurrentCopy(kCFAllocatorDefault, peer->peer_info,
429 NULL, NULL, kCFBooleanFalse, kCFBooleanTrue, kCFBooleanTrue, newViews,
430 device_key, &createError);
431 require_action_quiet(newPeer, errOut,
432 secnotice("upgrade", "Peer info v2 create copy failed: %@", createError));
434 CFTransferRetained(peer->peer_info, newPeer);
439 CFReleaseNull(newViews);
440 CFReleaseNull(copyError);
441 CFReleaseNull(createError);
442 CFReleaseNull(device_key);
446 SOSViewResultCode SOSFullPeerInfoViewStatus(SOSFullPeerInfoRef peer, CFStringRef viewname, CFErrorRef *error)
448 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(peer);
449 if(!pi) return kSOSCCGeneralViewError;
450 return SOSPeerInfoViewStatus(pi, viewname, error);
453 SOSPeerInfoRef SOSFullPeerInfoGetPeerInfo(SOSFullPeerInfoRef fullPeer) {
454 return fullPeer?fullPeer->peer_info:NULL;
457 // MARK: Private Key Retrieval and Existence
459 SecKeyRef SOSFullPeerInfoCopyPubKey(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
460 SecKeyRef retval = NULL;
461 require_quiet(fpi, errOut);
462 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
463 require_quiet(pi, errOut);
464 retval = SOSPeerInfoCopyPubKey(pi, error);
470 SecKeyRef SOSFullPeerInfoCopyOctagonPublicSigningKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
472 SecKeyRef retval = NULL;
473 require_quiet(fpi, errOut);
474 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
475 require_quiet(pi, errOut);
476 retval = SOSPeerInfoCopyOctagonSigningPublicKey(pi, error);
482 SecKeyRef SOSFullPeerInfoCopyOctagonPublicEncryptionKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
484 SecKeyRef retval = NULL;
485 require_quiet(fpi, errOut);
486 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
487 require_quiet(pi, errOut);
488 retval = SOSPeerInfoCopyOctagonEncryptionPublicKey(pi, error);
495 static SecKeyRef SOSFullPeerInfoCopyMatchingPrivateKey(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
496 SecKeyRef retval = NULL;
498 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
499 require_quiet(pub, exit);
500 retval = SecKeyCopyMatchingPrivateKey(pub, error);
506 static SecKeyRef SOSFullPeerInfoCopyMatchingOctagonSigningPrivateKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
508 SecKeyRef retval = NULL;
509 SecKeyRef pub = SOSFullPeerInfoCopyOctagonPublicSigningKey(fpi, error);
510 require_quiet(pub, exit);
511 retval = SecKeyCopyMatchingPrivateKey(pub, error);
517 static SecKeyRef SOSFullPeerInfoCopyMatchingOctagonEncryptionPrivateKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
519 SecKeyRef retval = NULL;
520 SecKeyRef pub = SOSFullPeerInfoCopyOctagonPublicEncryptionKey(fpi, error);
521 require_quiet(pub, exit);
522 retval = SecKeyCopyMatchingPrivateKey(pub, error);
530 static OSStatus SOSFullPeerInfoGetMatchingPrivateKeyStatus(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
531 OSStatus retval = errSecParam;
532 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
533 require_quiet(pub, exit);
534 retval = SecKeyGetMatchingPrivateKeyStatus(pub, error);
541 bool SOSFullPeerInfoValidate(SOSFullPeerInfoRef peer, CFErrorRef* error) {
542 OSStatus result = SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer, error);
543 if(result == errSecSuccess) return true;
547 bool SOSFullPeerInfoPrivKeyExists(SOSFullPeerInfoRef peer) {
548 OSStatus result = SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer, NULL);
549 if(result == errSecItemNotFound || result == errSecParam) return false;
553 bool SOSFullPeerInfoPurgePersistentKey(SOSFullPeerInfoRef fpi, CFErrorRef* error) {
555 CFDictionaryRef privQuery = NULL;
556 CFMutableDictionaryRef query = NULL;
557 CFDictionaryRef octagonPrivQuery = NULL;
558 CFMutableDictionaryRef octagonQuery = NULL;
560 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
561 SecKeyRef octagonSigningPub = SOSFullPeerInfoCopyOctagonPublicSigningKey(fpi, error);
562 SecKeyRef octagonEncryptionPub = SOSFullPeerInfoCopyOctagonPublicEncryptionKey(fpi, error);
563 require_quiet(pub, fail);
564 // iCloud Identities doesn't have either signing or encryption key here. Don't fail if they're not present.
566 privQuery = CreatePrivateKeyMatchingQuery(pub, false);
567 query = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, privQuery);
568 CFDictionaryAddValue(query, kSecUseTombstones, kCFBooleanFalse);
570 result = SecError(SecItemDelete(query), error, CFSTR("Deleting while purging"));
572 // do the same thing to also purge the octagon sync signing key
573 if(octagonSigningPub) {
574 octagonPrivQuery = CreatePrivateKeyMatchingQuery(octagonSigningPub, false);
575 octagonQuery = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, octagonPrivQuery);
576 CFDictionaryAddValue(octagonQuery, kSecUseTombstones, kCFBooleanFalse);
578 result &= SecError(SecItemDelete(octagonQuery), error, CFSTR("Deleting signing key while purging"));
581 CFReleaseNull(octagonPrivQuery);
582 CFReleaseNull(octagonQuery);
584 // do the same thing to also purge the octagon encryption key
585 if(octagonEncryptionPub) {
586 octagonPrivQuery = CreatePrivateKeyMatchingQuery(octagonEncryptionPub, false);
587 octagonQuery = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, octagonPrivQuery);
588 CFDictionaryAddValue(octagonQuery, kSecUseTombstones, kCFBooleanFalse);
590 result &= SecError(SecItemDelete(octagonQuery), error, CFSTR("Deleting encryption key while purging"));
594 CFReleaseNull(privQuery);
595 CFReleaseNull(query);
597 CFReleaseNull(octagonPrivQuery);
598 CFReleaseNull(octagonQuery);
599 CFReleaseNull(octagonSigningPub);
600 CFReleaseNull(octagonEncryptionPub);
604 SecKeyRef SOSFullPeerInfoCopyDeviceKey(SOSFullPeerInfoRef fullPeer, CFErrorRef* error)
606 return SOSFullPeerInfoCopyMatchingPrivateKey(fullPeer, error);
609 SecKeyRef SOSFullPeerInfoCopyOctagonSigningKey(SOSFullPeerInfoRef fullPeer, CFErrorRef* error)
611 return SOSFullPeerInfoCopyMatchingOctagonSigningPrivateKey(fullPeer, error);
614 SecKeyRef SOSFullPeerInfoCopyOctagonEncryptionKey(SOSFullPeerInfoRef fullPeer, CFErrorRef* error)
616 return SOSFullPeerInfoCopyMatchingOctagonEncryptionPrivateKey(fullPeer, error);
619 bool SOSFullPeerInfoHaveOctagonKeys(SOSFullPeerInfoRef fullPeer)
621 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fullPeer);
627 SOSPeerInfoHasOctagonSigningPubKey(pi) &&
628 SOSPeerInfoHasOctagonEncryptionPubKey(pi);
633 // MARK: Encode and decode
635 size_t SOSFullPeerInfoGetDEREncodedSize(SOSFullPeerInfoRef peer, CFErrorRef *error)
637 size_t peer_size = SOSPeerInfoGetDEREncodedSize(peer->peer_info, error);
641 size_t ref_size = der_sizeof_data(peer->key_ref, error);
645 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE,
646 peer_size + ref_size);
649 uint8_t* SOSFullPeerInfoEncodeToDER(SOSFullPeerInfoRef peer, CFErrorRef* error, const uint8_t* der, uint8_t* der_end)
651 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
652 SOSPeerInfoEncodeToDER(peer->peer_info, error, der,
653 der_encode_data(peer->key_ref, error, der, der_end)));
656 CFDataRef SOSFullPeerInfoCopyEncodedData(SOSFullPeerInfoRef peer, CFAllocatorRef allocator, CFErrorRef *error)
658 return CFDataCreateWithDER(kCFAllocatorDefault, SOSFullPeerInfoGetDEREncodedSize(peer, error), ^uint8_t*(size_t size, uint8_t *buffer) {
659 return SOSFullPeerInfoEncodeToDER(peer, error, buffer, (uint8_t *) buffer + size);
663 bool SOSFullPeerInfoPromoteToApplication(SOSFullPeerInfoRef fpi, SecKeyRef user_key, CFErrorRef *error)
665 bool success = false;
666 SOSPeerInfoRef old_pi = NULL;
668 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
669 require_quiet(device_key, exit);
671 old_pi = fpi->peer_info;
672 fpi->peer_info = SOSPeerInfoCopyAsApplication(old_pi, user_key, device_key, error);
674 require_action_quiet(fpi->peer_info, exit, fpi->peer_info = old_pi; old_pi = NULL);
679 CFReleaseSafe(old_pi);
680 CFReleaseSafe(device_key);
684 bool SOSFullPeerInfoUpgradeSignatures(SOSFullPeerInfoRef fpi, SecKeyRef user_key, CFErrorRef *error)
686 bool success = false;
687 SOSPeerInfoRef old_pi = NULL;
689 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
690 require_quiet(device_key, exit);
692 old_pi = fpi->peer_info;
693 fpi->peer_info = SOSPeerInfoUpgradeSignatures(NULL, user_key, device_key, old_pi, error);
695 require_action_quiet(fpi->peer_info, exit, fpi->peer_info = old_pi; old_pi = NULL);
700 CFReleaseSafe(old_pi);
701 CFReleaseSafe(device_key);
709 SOSPeerInfoRef SOSFullPeerInfoPromoteToRetiredAndCopy(SOSFullPeerInfoRef fpi, CFErrorRef *error)
711 SOSPeerInfoRef peer_to_free = NULL;
712 SOSPeerInfoRef retired_peer = NULL;
713 SecKeyRef key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
714 require_quiet(key, error_out);
716 retired_peer = SOSPeerInfoCreateRetirementTicket(NULL, key, fpi->peer_info, error);
718 require_quiet(retired_peer, error_out);
720 peer_to_free = fpi->peer_info;
721 fpi->peer_info = retired_peer;
722 CFRetainSafe(fpi->peer_info);
726 CFReleaseNull(peer_to_free);
731 bool SOSFullPeerInfoPing(SOSFullPeerInfoRef peer, CFErrorRef* error) {
733 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(peer, error);
734 require_quiet(device_key, fail);
735 SOSPeerInfoRef newPeer = SOSPeerInfoCopyWithPing(kCFAllocatorDefault, peer->peer_info, device_key, error);
736 require_quiet(newPeer, fail);
738 CFReleaseNull(peer->peer_info);
739 peer->peer_info = newPeer;
743 CFReleaseNull(device_key);