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_sync_signing_key_ref;
86 CFGiblisWithHashFor(SOSFullPeerInfo);
90 CFStringRef kSOSFullPeerInfoDescriptionKey = CFSTR("SOSFullPeerInfoDescription");
91 CFStringRef kSOSFullPeerInfoSignatureKey = CFSTR("SOSFullPeerInfoSignature");
92 CFStringRef kSOSFullPeerInfoNameKey = CFSTR("SOSFullPeerInfoName");
95 static bool SOSFullPeerInfoUpdate(SOSFullPeerInfoRef peer, CFErrorRef *error, SOSPeerInfoRef (^create_modification)(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error)) {
98 SOSPeerInfoRef newPeer = NULL;
99 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(peer, error);
100 require_quiet(device_key, fail);
102 newPeer = create_modification(peer->peer_info, device_key, error);
103 require_quiet(newPeer, fail);
105 CFTransferRetained(peer->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,
122 CFDataRef backupKey, SecKeyRef signingKey, SecKeyRef octagonSigningKey,
124 return SOSFullPeerInfoCreateWithViews(allocator, gestalt, backupKey, NULL, signingKey, octagonSigningKey, error);
127 SOSFullPeerInfoRef SOSFullPeerInfoCreateWithViews(CFAllocatorRef allocator,
128 CFDictionaryRef gestalt, CFDataRef backupKey, CFSetRef initialViews,
129 SecKeyRef signingKey, SecKeyRef octagonSigningKey, CFErrorRef* error) {
131 SOSFullPeerInfoRef result = NULL;
132 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
134 CFStringRef IDSID = CFSTR("");
135 CFStringRef transportType = SOSTransportMessageTypeIDSV2;
136 CFBooleanRef preferIDS = kCFBooleanFalse;
137 CFBooleanRef preferIDSFragmentation = kCFBooleanTrue;
138 CFBooleanRef preferACKModel = kCFBooleanTrue;
140 fpi->peer_info = SOSPeerInfoCreateWithTransportAndViews(allocator, gestalt, backupKey,
141 IDSID, transportType, preferIDS,
142 preferIDSFragmentation, preferACKModel, initialViews,
143 signingKey, octagonSigningKey, error);
144 require_quiet(fpi->peer_info, exit);
146 OSStatus status = SecKeyCopyPersistentRef(signingKey, &fpi->key_ref);
147 require_quiet(SecError(status, error, CFSTR("Inflating persistent ref")), exit);
149 status = SecKeyCopyPersistentRef(octagonSigningKey, &fpi->octagon_sync_signing_key_ref);
150 require_quiet(SecError(status, error, CFSTR("Inflating octagon persistent ref")), exit);
152 CFTransferRetained(result, fpi);
159 SOSFullPeerInfoRef SOSFullPeerInfoCopyFullPeerInfo(SOSFullPeerInfoRef toCopy) {
160 SOSFullPeerInfoRef retval = NULL;
161 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, kCFAllocatorDefault);
162 SOSPeerInfoRef piToCopy = SOSFullPeerInfoGetPeerInfo(toCopy);
164 require_quiet(piToCopy, errOut);
165 require_quiet(fpi, errOut);
166 fpi->peer_info = SOSPeerInfoCreateCopy(kCFAllocatorDefault, piToCopy, NULL);
167 require_quiet(fpi->peer_info, errOut);
168 fpi->key_ref = toCopy->key_ref;
169 CFTransferRetained(retval, fpi);
176 bool SOSFullPeerInfoUpdateTransportType(SOSFullPeerInfoRef peer, CFStringRef transportType, CFErrorRef* error)
178 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
179 return SOSPeerInfoSetTransportType(kCFAllocatorDefault, peer, transportType, key, error);
183 bool SOSFullPeerInfoUpdateDeviceID(SOSFullPeerInfoRef peer, CFStringRef deviceID, CFErrorRef* error){
184 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
185 return SOSPeerInfoSetDeviceID(kCFAllocatorDefault, peer, deviceID, key, error);
189 bool SOSFullPeerInfoUpdateTransportPreference(SOSFullPeerInfoRef peer, CFBooleanRef preference, CFErrorRef* error){
190 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
191 return SOSPeerInfoSetIDSPreference(kCFAllocatorDefault, peer, preference, key, error);
195 bool SOSFullPeerInfoUpdateTransportFragmentationPreference(SOSFullPeerInfoRef peer, CFBooleanRef preference, CFErrorRef* error){
196 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
197 return SOSPeerInfoSetIDSFragmentationPreference(kCFAllocatorDefault, peer, preference, key, error);
201 bool SOSFullPeerInfoUpdateTransportAckModelPreference(SOSFullPeerInfoRef peer, CFBooleanRef preference, CFErrorRef* error){
202 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
203 return SOSPeerInfoSetIDSACKModelPreference(kCFAllocatorDefault, peer, preference, key, error);
207 CFDataRef SOSPeerInfoCopyData(SOSPeerInfoRef pi, CFErrorRef *error)
209 CFTypeRef vData = NULL;
210 SecKeyRef pubKey = SOSPeerInfoCopyPubKey(pi, error);
211 CFDictionaryRef query = NULL;
212 require_quiet(pubKey, exit);
215 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(pubKey);
217 query = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
218 kSecClass, kSecClassKey,
219 kSecAttrKeyClass, kSecAttrKeyClassPrivate,
220 kSecAttrSynchronizable, kSecAttrSynchronizableAny,
221 kSecAttrApplicationLabel, public_key_hash,
222 kSecReturnData, kCFBooleanTrue,
224 CFReleaseNull(public_key_hash);
226 require_quiet(SecError(SecItemCopyMatching(query, &vData),error ,
227 CFSTR("Error finding persistent ref to key from public: %@"), pubKey), exit);
230 CFReleaseNull(query);
232 secnotice("fpi","no private key found");
233 return (CFDataRef)vData;
236 SOSFullPeerInfoRef SOSFullPeerInfoCreateCloudIdentity(CFAllocatorRef allocator, SOSPeerInfoRef peer, CFErrorRef* error) {
237 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
239 SecKeyRef pubKey = NULL;
241 fpi->peer_info = peer;
242 CFRetainSafe(fpi->peer_info);
243 if (fpi->peer_info == NULL) {
248 pubKey = SOSPeerInfoCopyPubKey(peer, error);
249 require_quiet(pubKey, exit);
251 fpi->key_ref = SecKeyCreatePersistentRefToMatchingPrivateKey(pubKey, error);
253 if (fpi->key_ref == NULL) {
259 CFReleaseNull(pubKey);
264 SOSFullPeerInfoRef SOSFullPeerInfoCreateFromDER(CFAllocatorRef allocator, CFErrorRef* error,
265 const uint8_t** der_p, const uint8_t *der_end) {
266 SOSFullPeerInfoRef fpi = CFTypeAllocate(SOSFullPeerInfo, struct __OpaqueSOSFullPeerInfo, allocator);
268 const uint8_t *sequence_end;
270 *der_p = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, *der_p, der_end);
271 CFReleaseNull(fpi->peer_info);
272 fpi->peer_info = SOSPeerInfoCreateFromDER(allocator, error, der_p, der_end);
273 require_quiet(fpi->peer_info != NULL, fail);
275 *der_p = der_decode_data(allocator, kCFPropertyListImmutable, &fpi->key_ref, error, *der_p, sequence_end);
276 require_quiet(*der_p != NULL, fail);
285 SOSFullPeerInfoRef SOSFullPeerInfoCreateFromData(CFAllocatorRef allocator, CFDataRef fullPeerData, CFErrorRef *error)
287 if(!fullPeerData) return NULL;
288 size_t size = CFDataGetLength(fullPeerData);
289 const uint8_t *der = CFDataGetBytePtr(fullPeerData);
290 SOSFullPeerInfoRef inflated = SOSFullPeerInfoCreateFromDER(allocator, error, &der, der + size);
294 static void SOSFullPeerInfoDestroy(CFTypeRef aObj) {
295 SOSFullPeerInfoRef fpi = (SOSFullPeerInfoRef) aObj;
297 CFReleaseNull(fpi->peer_info);
298 CFReleaseNull(fpi->key_ref);
301 static Boolean SOSFullPeerInfoCompare(CFTypeRef lhs, CFTypeRef rhs) {
302 SOSFullPeerInfoRef lpeer = (SOSFullPeerInfoRef) lhs;
303 SOSFullPeerInfoRef rpeer = (SOSFullPeerInfoRef) rhs;
305 if (!CFEqual(lpeer->peer_info, rpeer->peer_info))
308 if (CFEqual(lpeer->key_ref, rpeer->key_ref))
311 SecKeyRef lpk = SOSFullPeerInfoCopyDeviceKey(lpeer, NULL);
312 SecKeyRef rpk = SOSFullPeerInfoCopyDeviceKey(rpeer, NULL);
314 bool match = lpk && rpk && CFEqual(lpk, rpk);
322 static CFHashCode SOSFullPeerInfoHash(CFTypeRef cf) {
323 SOSFullPeerInfoRef peer = (SOSFullPeerInfoRef) cf;
325 return CFHash(peer->peer_info);
328 static CFStringRef SOSFullPeerInfoCopyFormatDescription(CFTypeRef aObj, CFDictionaryRef formatOptions) {
329 SOSFullPeerInfoRef fpi = (SOSFullPeerInfoRef) aObj;
331 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SOSFullPeerInfo@%p: \"%@\">"), fpi, fpi->peer_info);
334 bool SOSFullPeerInfoUpdateGestalt(SOSFullPeerInfoRef peer, CFDictionaryRef gestalt, CFErrorRef* error)
336 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
337 return SOSPeerInfoCopyWithGestaltUpdate(kCFAllocatorDefault, peer,
338 gestalt, key, error);
342 bool SOSFullPeerInfoUpdateV2Dictionary(SOSFullPeerInfoRef peer, CFDictionaryRef newv2dict, CFErrorRef* error)
344 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
345 return SOSPeerInfoCopyWithV2DictionaryUpdate(kCFAllocatorDefault, peer,
346 newv2dict, key, error);
350 bool SOSFullPeerInfoUpdateBackupKey(SOSFullPeerInfoRef peer, CFDataRef backupKey, CFErrorRef* error)
352 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
353 return SOSPeerInfoCopyWithBackupKeyUpdate(kCFAllocatorDefault, peer, backupKey, key, error);
357 bool SOSFullPeerInfoAddEscrowRecord(SOSFullPeerInfoRef peer, CFStringRef dsid, CFDictionaryRef escrowRecord, CFErrorRef* error)
359 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
360 return SOSPeerInfoCopyWithEscrowRecordUpdate(kCFAllocatorDefault, peer, dsid, escrowRecord, key, error);
364 bool SOSFullPeerInfoReplaceEscrowRecords(SOSFullPeerInfoRef peer, CFDictionaryRef escrowRecords, CFErrorRef* error)
366 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
367 return SOSPeerInfoCopyWithReplacedEscrowRecords(kCFAllocatorDefault, peer, escrowRecords, key, error);
371 SOSViewResultCode SOSFullPeerInfoUpdateViews(SOSFullPeerInfoRef peer, SOSViewActionCode action, CFStringRef viewname, CFErrorRef* error)
373 __block SOSViewResultCode retval = kSOSCCGeneralViewError;
375 secnotice("viewChange", "%s view %@", SOSViewsXlateAction(action), viewname);
377 return SOSFullPeerInfoUpdate(peer, error, ^SOSPeerInfoRef(SOSPeerInfoRef peer, SecKeyRef key, CFErrorRef *error) {
378 return SOSPeerInfoCopyWithViewsChange(kCFAllocatorDefault, peer, action, viewname, &retval, key, error);
379 }) ? retval : kSOSCCGeneralViewError;
382 static CFMutableSetRef SOSFullPeerInfoCopyViewUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
383 CFSetRef enabledViews = SOSPeerInfoCopyEnabledViews(peer->peer_info);
384 CFMutableSetRef newViews = SOSPeerInfoCopyEnabledViews(peer->peer_info);
386 if (isSet(minimumViews)) {
387 CFSetUnion(newViews, minimumViews);
389 if (isSet(excludedViews)) {
390 CFSetSubtract(newViews, excludedViews);
393 if (CFEqualSafe(newViews, enabledViews)) {
394 CFReleaseNull(newViews);
397 CFReleaseNull(enabledViews);
401 static bool SOSFullPeerInfoNeedsViewUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
402 CFSetRef updatedViews = SOSFullPeerInfoCopyViewUpdate(peer, minimumViews, excludedViews);
403 bool needsUpdate = (updatedViews != NULL);
404 CFReleaseNull(updatedViews);
408 static bool sosFullPeerInfoRequiresUpdate(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
410 if(!SOSPeerInfoVersionIsCurrent(peer->peer_info)) return true;
411 if(!SOSPeerInfoSerialNumberIsSet(peer->peer_info)) return true;
412 if(!(SOSPeerInfoV2DictionaryHasString(peer->peer_info, sDeviceID)))return true;
413 if(!(SOSPeerInfoV2DictionaryHasString(peer->peer_info, sTransportType))) return true;
414 if(!(SOSPeerInfoV2DictionaryHasBoolean(peer->peer_info, sPreferIDS))) return true;
415 if(!(SOSPeerInfoV2DictionaryHasBoolean(peer->peer_info, sPreferIDSFragmentation))) return true;
416 if(!(SOSPeerInfoV2DictionaryHasBoolean(peer->peer_info, sPreferIDSACKModel))) return true;
417 if(SOSFullPeerInfoNeedsViewUpdate(peer, minimumViews, excludedViews)) return true;
422 // Returning false indicates we don't need to upgrade.
423 bool SOSFullPeerInfoUpdateToCurrent(SOSFullPeerInfoRef peer, CFSetRef minimumViews, CFSetRef excludedViews) {
424 bool success = false;
426 CFMutableSetRef newViews = NULL;
427 CFErrorRef copyError = NULL;
428 CFErrorRef createError = NULL;
429 SecKeyRef device_key = NULL;
431 require_quiet(sosFullPeerInfoRequiresUpdate(peer, minimumViews, excludedViews), errOut);
433 newViews = SOSFullPeerInfoCopyViewUpdate(peer, minimumViews, excludedViews);
435 device_key = SOSFullPeerInfoCopyDeviceKey(peer, ©Error);
436 require_action_quiet(device_key, errOut,
437 secnotice("upgrade", "SOSFullPeerInfoCopyDeviceKey failed: %@", copyError));
439 SOSPeerInfoRef newPeer = SOSPeerInfoCreateCurrentCopy(kCFAllocatorDefault, peer->peer_info,
440 NULL, NULL, kCFBooleanFalse, kCFBooleanTrue, kCFBooleanTrue, newViews,
441 device_key, &createError);
442 require_action_quiet(newPeer, errOut,
443 secnotice("upgrade", "Peer info v2 create copy failed: %@", createError));
445 CFTransferRetained(peer->peer_info, newPeer);
450 CFReleaseNull(newViews);
451 CFReleaseNull(copyError);
452 CFReleaseNull(createError);
453 CFReleaseNull(device_key);
457 SOSViewResultCode SOSFullPeerInfoViewStatus(SOSFullPeerInfoRef peer, CFStringRef viewname, CFErrorRef *error)
459 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(peer);
460 if(!pi) return kSOSCCGeneralViewError;
461 return SOSPeerInfoViewStatus(pi, viewname, error);
465 SOSSecurityPropertyResultCode SOSFullPeerInfoUpdateSecurityProperty(SOSFullPeerInfoRef peer, SOSViewActionCode action, CFStringRef property, CFErrorRef* error)
467 SOSSecurityPropertyResultCode retval = kSOSCCGeneralSecurityPropertyError;
468 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(peer, error);
469 require_quiet(device_key, fail);
471 SOSPeerInfoRef newPeer = SOSPeerInfoCopyWithSecurityPropertyChange(kCFAllocatorDefault, peer->peer_info, action, property, &retval, device_key, error);
473 require_quiet(newPeer, fail);
475 CFReleaseNull(peer->peer_info);
476 peer->peer_info = newPeer;
480 CFReleaseNull(device_key);
484 SOSSecurityPropertyResultCode SOSFullPeerInfoSecurityPropertyStatus(SOSFullPeerInfoRef peer, CFStringRef property, CFErrorRef *error)
486 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(peer);
487 secnotice("secprop", "have pi %s", (pi)? "true": "false");
488 if(!pi) return kSOSCCGeneralSecurityPropertyError;
489 return SOSPeerInfoSecurityPropertyStatus(pi, property, error);
493 SOSPeerInfoRef SOSFullPeerInfoGetPeerInfo(SOSFullPeerInfoRef fullPeer) {
494 return fullPeer?fullPeer->peer_info:NULL;
497 // MARK: Private Key Retrieval and Existence
499 SecKeyRef SOSFullPeerInfoCopyPubKey(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
500 SecKeyRef retval = NULL;
501 require_quiet(fpi, errOut);
502 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
503 require_quiet(pi, errOut);
504 retval = SOSPeerInfoCopyPubKey(pi, error);
510 static SecKeyRef SOSFullPeerInfoCopyOctagonPubKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
512 SecKeyRef retval = NULL;
513 require_quiet(fpi, errOut);
514 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
515 require_quiet(pi, errOut);
516 retval = SOSPeerInfoCopyOctagonPubKey(pi, error);
522 static SecKeyRef SOSFullPeerInfoCopyMatchingPrivateKey(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
523 SecKeyRef retval = NULL;
525 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
526 require_quiet(pub, exit);
527 retval = SecKeyCopyMatchingPrivateKey(pub, error);
533 static SecKeyRef SOSFullPeerInfoCopyMatchingOctagonPrivateKey(SOSFullPeerInfoRef fpi, CFErrorRef* error)
535 SecKeyRef retval = NULL;
536 SecKeyRef pub = SOSFullPeerInfoCopyOctagonPubKey(fpi, error);
537 require_quiet(pub, exit);
538 retval = SecKeyCopyMatchingPrivateKey(pub, error);
545 static OSStatus SOSFullPeerInfoGetMatchingPrivateKeyStatus(SOSFullPeerInfoRef fpi, CFErrorRef *error) {
546 OSStatus retval = errSecParam;
547 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
548 require_quiet(pub, exit);
549 retval = SecKeyGetMatchingPrivateKeyStatus(pub, error);
556 bool SOSFullPeerInfoValidate(SOSFullPeerInfoRef peer, CFErrorRef* error) {
557 OSStatus result = SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer, error);
558 if(result == errSecSuccess) return true;
562 bool SOSFullPeerInfoPrivKeyExists(SOSFullPeerInfoRef peer) {
563 OSStatus result = SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer, NULL);
564 if(result == errSecItemNotFound || result == errSecParam) return false;
568 bool SOSFullPeerInfoPurgePersistentKey(SOSFullPeerInfoRef fpi, CFErrorRef* error) {
570 CFDictionaryRef privQuery = NULL;
571 CFMutableDictionaryRef query = NULL;
572 CFDictionaryRef octagonPrivQuery = NULL;
573 CFMutableDictionaryRef octagonQuery = NULL;
575 SecKeyRef pub = SOSFullPeerInfoCopyPubKey(fpi, error);
576 SecKeyRef octagonPub = SOSFullPeerInfoCopyOctagonPubKey(fpi, error);
577 require_quiet(pub, fail);
578 require_quiet(octagonPub, fail);
580 privQuery = CreatePrivateKeyMatchingQuery(pub, false);
581 query = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, privQuery);
582 CFDictionaryAddValue(query, kSecUseTombstones, kCFBooleanFalse);
584 result = SecError(SecItemDelete(query), error, CFSTR("Deleting while purging"));
586 // do the same thing to also purge the octagon sync signing key
588 octagonPrivQuery = CreatePrivateKeyMatchingQuery(octagonPub, false);
589 octagonQuery = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, octagonPrivQuery);
590 CFDictionaryAddValue(octagonQuery, kSecUseTombstones, kCFBooleanFalse);
592 result &= SecError(SecItemDelete(octagonQuery), error, CFSTR("Deleting while purging"));
595 CFReleaseNull(privQuery);
596 CFReleaseNull(query);
598 CFReleaseNull(octagonPrivQuery);
599 CFReleaseNull(octagonQuery);
600 CFReleaseNull(octagonPub);
604 SecKeyRef SOSFullPeerInfoCopyDeviceKey(SOSFullPeerInfoRef fullPeer, CFErrorRef* error) {
605 return SOSFullPeerInfoCopyMatchingPrivateKey(fullPeer, error);
608 SecKeyRef SOSFullPeerInfoCopyOctagonSigningKey(SOSFullPeerInfoRef fullPeer, CFErrorRef* error)
610 return SOSFullPeerInfoCopyMatchingOctagonPrivateKey(fullPeer, error);
614 // MARK: Encode and decode
616 size_t SOSFullPeerInfoGetDEREncodedSize(SOSFullPeerInfoRef peer, CFErrorRef *error)
618 size_t peer_size = SOSPeerInfoGetDEREncodedSize(peer->peer_info, error);
622 size_t ref_size = der_sizeof_data(peer->key_ref, error);
626 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE,
627 peer_size + ref_size);
630 uint8_t* SOSFullPeerInfoEncodeToDER(SOSFullPeerInfoRef peer, CFErrorRef* error, const uint8_t* der, uint8_t* der_end)
632 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
633 SOSPeerInfoEncodeToDER(peer->peer_info, error, der,
634 der_encode_data(peer->key_ref, error, der, der_end)));
637 CFDataRef SOSFullPeerInfoCopyEncodedData(SOSFullPeerInfoRef peer, CFAllocatorRef allocator, CFErrorRef *error)
639 size_t size = SOSFullPeerInfoGetDEREncodedSize(peer, error);
642 uint8_t buffer[size];
643 uint8_t* start = SOSFullPeerInfoEncodeToDER(peer, error, buffer, buffer + sizeof(buffer));
644 CFDataRef result = CFDataCreate(kCFAllocatorDefault, start, size);
648 bool SOSFullPeerInfoPromoteToApplication(SOSFullPeerInfoRef fpi, SecKeyRef user_key, CFErrorRef *error)
650 bool success = false;
651 SOSPeerInfoRef old_pi = NULL;
653 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
654 require_quiet(device_key, exit);
656 old_pi = fpi->peer_info;
657 fpi->peer_info = SOSPeerInfoCopyAsApplication(old_pi, user_key, device_key, error);
659 require_action_quiet(fpi->peer_info, exit, fpi->peer_info = old_pi; old_pi = NULL);
664 CFReleaseSafe(old_pi);
665 CFReleaseSafe(device_key);
669 bool SOSFullPeerInfoUpgradeSignatures(SOSFullPeerInfoRef fpi, SecKeyRef user_key, CFErrorRef *error)
671 bool success = false;
672 SOSPeerInfoRef old_pi = NULL;
674 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
675 require_quiet(device_key, exit);
677 old_pi = fpi->peer_info;
678 fpi->peer_info = SOSPeerInfoUpgradeSignatures(NULL, user_key, device_key, old_pi, error);
680 require_action_quiet(fpi->peer_info, exit, fpi->peer_info = old_pi; old_pi = NULL);
685 CFReleaseSafe(old_pi);
686 CFReleaseSafe(device_key);
694 SOSPeerInfoRef SOSFullPeerInfoPromoteToRetiredAndCopy(SOSFullPeerInfoRef fpi, CFErrorRef *error)
696 SOSPeerInfoRef peer_to_free = NULL;
697 SOSPeerInfoRef retired_peer = NULL;
698 SecKeyRef key = SOSFullPeerInfoCopyDeviceKey(fpi, error);
699 require_quiet(key, error_out);
701 retired_peer = SOSPeerInfoCreateRetirementTicket(NULL, key, fpi->peer_info, error);
703 require_quiet(retired_peer, error_out);
705 peer_to_free = fpi->peer_info;
706 fpi->peer_info = retired_peer;
707 CFRetainSafe(fpi->peer_info);
711 CFReleaseNull(peer_to_free);
716 bool SOSFullPeerInfoPing(SOSFullPeerInfoRef peer, CFErrorRef* error) {
718 SecKeyRef device_key = SOSFullPeerInfoCopyDeviceKey(peer, error);
719 require_quiet(device_key, fail);
720 SOSPeerInfoRef newPeer = SOSPeerInfoCopyWithPing(kCFAllocatorDefault, peer->peer_info, device_key, error);
721 require_quiet(newPeer, fail);
723 CFReleaseNull(peer->peer_info);
724 peer->peer_info = newPeer;
728 CFReleaseNull(device_key);