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>
30 #include <Security/SecureObjectSync/SOSCircle.h>
32 #include <Security/SecureObjectSync/SOSInternal.h>
33 #include <Security/SecureObjectSync/SOSAccountPriv.h>
35 #include <Security/SecKeyPriv.h>
36 #include <Security/SecItemPriv.h>
37 #include <Security/SecOTR.h>
38 #include <CoreFoundation/CFArray.h>
39 #include <dispatch/dispatch.h>
40 #include <SOSPeerInfoDER.h>
41 #include <Security/SecFramework.h>
46 #include <utilities/SecCFWrappers.h>
47 #include <utilities/SecCFRelease.h>
49 #include <utilities/der_plist.h>
50 #include <utilities/der_plist_internal.h>
51 #include <corecrypto/ccder.h>
53 #include <CommonCrypto/CommonDigest.h>
54 #include <CommonCrypto/CommonDigestSPI.h>
56 #include <CoreFoundation/CoreFoundation.h>
58 #include "utilities/iOSforOSX.h"
60 #include <AssertMacros.h>
62 #include <utilities/SecCFError.h>
71 extern OSStatus
SecKeyCopyPublicBytes(SecKeyRef key
, CFDataRef
* publicBytes
);
72 extern SecKeyRef
SecKeyCreatePublicFromPrivate(SecKeyRef privateKey
);
77 struct __OpaqueSOSFullPeerInfo
{
80 SOSPeerInfoRef peer_info
;
84 CFGiblisWithHashFor(SOSFullPeerInfo
);
88 CFStringRef kSOSFullPeerInfoDescriptionKey
= CFSTR("SOSFullPeerInfoDescription");
89 CFStringRef kSOSFullPeerInfoSignatureKey
= CFSTR("SOSFullPeerInfoSignature");
90 CFStringRef kSOSFullPeerInfoNameKey
= CFSTR("SOSFullPeerInfoName");
93 static bool SOSFullPeerInfoUpdate(SOSFullPeerInfoRef peer
, CFErrorRef
*error
, SOSPeerInfoRef (^create_modification
)(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
)) {
96 SOSPeerInfoRef newPeer
= NULL
;
97 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, error
);
98 require_quiet(device_key
, fail
);
100 newPeer
= create_modification(peer
->peer_info
, device_key
, error
);
101 require_quiet(newPeer
, fail
);
103 CFTransferRetained(peer
->peer_info
, newPeer
);
108 CFReleaseNull(device_key
);
109 CFReleaseNull(newPeer
);
113 bool SOSFullPeerInfoUpdateToThisPeer(SOSFullPeerInfoRef peer
, SOSPeerInfoRef pi
, CFErrorRef
*error
) {
114 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
115 return SOSPeerInfoSign(key
, pi
, error
) ? CFRetainSafe(pi
): NULL
;
119 SOSFullPeerInfoRef
SOSFullPeerInfoCreate(CFAllocatorRef allocator
, CFDictionaryRef gestalt
,
120 CFDataRef backupKey
, SecKeyRef signingKey
,
122 return SOSFullPeerInfoCreateWithViews(allocator
, gestalt
, backupKey
, NULL
, signingKey
, error
);
125 SOSFullPeerInfoRef
SOSFullPeerInfoCreateWithViews(CFAllocatorRef allocator
,
126 CFDictionaryRef gestalt
, CFDataRef backupKey
, CFSetRef initialViews
,
127 SecKeyRef signingKey
, CFErrorRef
* error
) {
129 SOSFullPeerInfoRef result
= NULL
;
130 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
132 CFStringRef IDSID
= CFSTR("");
133 CFStringRef transportType
=SOSTransportMessageTypeIDSV2
;
134 CFBooleanRef preferIDS
= kCFBooleanFalse
;
135 CFBooleanRef preferIDSFragmentation
= kCFBooleanTrue
;
137 fpi
->peer_info
= SOSPeerInfoCreateWithTransportAndViews(allocator
, gestalt
, backupKey
,
138 IDSID
, transportType
, preferIDS
,
139 preferIDSFragmentation
, initialViews
,
141 require_quiet(fpi
->peer_info
, exit
);
143 OSStatus status
= SecKeyCopyPersistentRef(signingKey
, &fpi
->key_ref
);
144 require_quiet(SecError(status
, error
, CFSTR("Inflating persistent ref")), exit
);
146 CFTransferRetained(result
, fpi
);
153 SOSFullPeerInfoRef
SOSFullPeerInfoCopyFullPeerInfo(SOSFullPeerInfoRef toCopy
) {
154 SOSFullPeerInfoRef retval
= NULL
;
155 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, kCFAllocatorDefault
);
156 SOSPeerInfoRef piToCopy
= SOSFullPeerInfoGetPeerInfo(toCopy
);
158 require_quiet(piToCopy
, errOut
);
159 require_quiet(fpi
, errOut
);
160 fpi
->peer_info
= SOSPeerInfoCreateCopy(kCFAllocatorDefault
, piToCopy
, NULL
);
161 require_quiet(fpi
->peer_info
, errOut
);
162 fpi
->key_ref
= toCopy
->key_ref
;
163 CFTransferRetained(retval
, fpi
);
170 bool SOSFullPeerInfoUpdateTransportType(SOSFullPeerInfoRef peer
, CFStringRef transportType
, CFErrorRef
* error
)
172 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
173 return SOSPeerInfoSetTransportType(kCFAllocatorDefault
, peer
, transportType
, key
, error
);
177 bool SOSFullPeerInfoUpdateDeviceID(SOSFullPeerInfoRef peer
, CFStringRef deviceID
, CFErrorRef
* error
){
178 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
179 return SOSPeerInfoSetDeviceID(kCFAllocatorDefault
, peer
, deviceID
, key
, error
);
183 bool SOSFullPeerInfoUpdateTransportPreference(SOSFullPeerInfoRef peer
, CFBooleanRef preference
, CFErrorRef
* error
){
184 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
185 return SOSPeerInfoSetIDSPreference(kCFAllocatorDefault
, peer
, preference
, key
, error
);
189 bool SOSFullPeerInfoUpdateTransportFragmentationPreference(SOSFullPeerInfoRef peer
, CFBooleanRef preference
, CFErrorRef
* error
){
190 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
191 return SOSPeerInfoSetIDSFragmentationPreference(kCFAllocatorDefault
, peer
, preference
, key
, error
);
196 SOSFullPeerInfoRef
SOSFullPeerInfoCreateCloudIdentity(CFAllocatorRef allocator
, SOSPeerInfoRef peer
, CFErrorRef
* error
) {
197 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
199 SecKeyRef pubKey
= NULL
;
201 fpi
->peer_info
= peer
;
202 CFRetainSafe(fpi
->peer_info
);
203 if (fpi
->peer_info
== NULL
) {
208 pubKey
= SOSPeerInfoCopyPubKey(peer
, error
);
209 require_quiet(pubKey
, exit
);
211 fpi
->key_ref
= SecKeyCreatePersistentRefToMatchingPrivateKey(pubKey
, error
);
213 if (fpi
->key_ref
== NULL
) {
219 CFReleaseNull(pubKey
);
224 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromDER(CFAllocatorRef allocator
, CFErrorRef
* error
,
225 const uint8_t** der_p
, const uint8_t *der_end
) {
226 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
228 const uint8_t *sequence_end
;
230 *der_p
= ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, &sequence_end
, *der_p
, der_end
);
231 CFReleaseNull(fpi
->peer_info
);
232 fpi
->peer_info
= SOSPeerInfoCreateFromDER(allocator
, error
, der_p
, der_end
);
233 require_quiet(fpi
->peer_info
!= NULL
, fail
);
235 *der_p
= der_decode_data(allocator
, kCFPropertyListImmutable
, &fpi
->key_ref
, error
, *der_p
, sequence_end
);
236 require_quiet(*der_p
!= NULL
, fail
);
245 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromData(CFAllocatorRef allocator
, CFDataRef fullPeerData
, CFErrorRef
*error
)
247 if(!fullPeerData
) return NULL
;
248 size_t size
= CFDataGetLength(fullPeerData
);
249 const uint8_t *der
= CFDataGetBytePtr(fullPeerData
);
250 SOSFullPeerInfoRef inflated
= SOSFullPeerInfoCreateFromDER(allocator
, error
, &der
, der
+ size
);
254 static void SOSFullPeerInfoDestroy(CFTypeRef aObj
) {
255 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
257 CFReleaseNull(fpi
->peer_info
);
258 CFReleaseNull(fpi
->key_ref
);
261 static Boolean
SOSFullPeerInfoCompare(CFTypeRef lhs
, CFTypeRef rhs
) {
262 SOSFullPeerInfoRef lpeer
= (SOSFullPeerInfoRef
) lhs
;
263 SOSFullPeerInfoRef rpeer
= (SOSFullPeerInfoRef
) rhs
;
265 if (!CFEqual(lpeer
->peer_info
, rpeer
->peer_info
))
268 if (CFEqual(lpeer
->key_ref
, rpeer
->key_ref
))
271 SecKeyRef lpk
= SOSFullPeerInfoCopyDeviceKey(lpeer
, NULL
);
272 SecKeyRef rpk
= SOSFullPeerInfoCopyDeviceKey(rpeer
, NULL
);
274 bool match
= lpk
&& rpk
&& CFEqual(lpk
, rpk
);
282 static CFHashCode
SOSFullPeerInfoHash(CFTypeRef cf
) {
283 SOSFullPeerInfoRef peer
= (SOSFullPeerInfoRef
) cf
;
285 return CFHash(peer
->peer_info
);
288 static CFStringRef
SOSFullPeerInfoCopyFormatDescription(CFTypeRef aObj
, CFDictionaryRef formatOptions
) {
289 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
291 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SOSFullPeerInfo@%p: \"%@\">"), fpi
, fpi
->peer_info
);
294 bool SOSFullPeerInfoUpdateGestalt(SOSFullPeerInfoRef peer
, CFDictionaryRef gestalt
, CFErrorRef
* error
)
296 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
297 return SOSPeerInfoCopyWithGestaltUpdate(kCFAllocatorDefault
, peer
,
298 gestalt
, key
, error
);
302 bool SOSFullPeerInfoUpdateBackupKey(SOSFullPeerInfoRef peer
, CFDataRef backupKey
, CFErrorRef
* error
)
304 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
305 return SOSPeerInfoCopyWithBackupKeyUpdate(kCFAllocatorDefault
, peer
, backupKey
, key
, error
);
309 bool SOSFullPeerInfoAddEscrowRecord(SOSFullPeerInfoRef peer
, CFStringRef dsid
, CFDictionaryRef escrowRecord
, CFErrorRef
* error
)
311 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
312 return SOSPeerInfoCopyWithEscrowRecordUpdate(kCFAllocatorDefault
, peer
, dsid
, escrowRecord
, key
, error
);
316 bool SOSFullPeerInfoReplaceEscrowRecords(SOSFullPeerInfoRef peer
, CFDictionaryRef escrowRecords
, CFErrorRef
* error
)
318 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
319 return SOSPeerInfoCopyWithReplacedEscrowRecords(kCFAllocatorDefault
, peer
, escrowRecords
, key
, error
);
323 SOSViewResultCode
SOSFullPeerInfoUpdateViews(SOSFullPeerInfoRef peer
, SOSViewActionCode action
, CFStringRef viewname
, CFErrorRef
* error
)
325 __block SOSViewResultCode retval
= kSOSCCGeneralViewError
;
327 secnotice("viewChange", "%s view %@", SOSViewsXlateAction(action
), viewname
);
329 return SOSFullPeerInfoUpdate(peer
, error
, ^SOSPeerInfoRef(SOSPeerInfoRef peer
, SecKeyRef key
, CFErrorRef
*error
) {
330 return SOSPeerInfoCopyWithViewsChange(kCFAllocatorDefault
, peer
, action
, viewname
, &retval
, key
, error
);
331 }) ? retval
: kSOSCCGeneralViewError
;
334 static CFMutableSetRef
SOSFullPeerInfoCopyViewUpdate(SOSFullPeerInfoRef peer
, CFSetRef minimumViews
, CFSetRef excludedViews
) {
335 CFSetRef enabledViews
= SOSPeerInfoCopyEnabledViews(peer
->peer_info
);
336 CFMutableSetRef newViews
= SOSPeerInfoCopyEnabledViews(peer
->peer_info
);
338 if (isSet(minimumViews
)) {
339 CFSetUnion(newViews
, minimumViews
);
341 if (isSet(excludedViews
)) {
342 CFSetSubtract(newViews
, excludedViews
);
345 if (CFEqualSafe(newViews
, enabledViews
)) {
346 CFReleaseNull(newViews
);
349 CFReleaseNull(enabledViews
);
353 static bool SOSFullPeerInfoNeedsViewUpdate(SOSFullPeerInfoRef peer
, CFSetRef minimumViews
, CFSetRef excludedViews
) {
354 CFSetRef updatedViews
= SOSFullPeerInfoCopyViewUpdate(peer
, minimumViews
, excludedViews
);
355 bool needsUpdate
= (updatedViews
!= NULL
);
356 CFReleaseNull(updatedViews
);
360 static bool sosFullPeerInfoRequiresUpdate(SOSFullPeerInfoRef peer
, CFSetRef minimumViews
, CFSetRef excludedViews
) {
362 if(!SOSPeerInfoVersionIsCurrent(peer
->peer_info
)) return true;
363 if(!SOSPeerInfoSerialNumberIsSet(peer
->peer_info
)) return true;
364 if(!(SOSPeerInfoV2DictionaryHasString(peer
->peer_info
, sDeviceID
)))return true;
365 if(!(SOSPeerInfoV2DictionaryHasString(peer
->peer_info
, sTransportType
))) return true;
366 if(!(SOSPeerInfoV2DictionaryHasBoolean(peer
->peer_info
, sPreferIDS
))) return true;
367 if(!(SOSPeerInfoV2DictionaryHasBoolean(peer
->peer_info
, sPreferIDSFragmentation
))) return true;
368 if(SOSFullPeerInfoNeedsViewUpdate(peer
, minimumViews
, excludedViews
)) return true;
373 // Returning false indicates we don't need to upgrade.
374 bool SOSFullPeerInfoUpdateToCurrent(SOSFullPeerInfoRef peer
, CFSetRef minimumViews
, CFSetRef excludedViews
) {
375 bool success
= false;
377 CFMutableSetRef newViews
= NULL
;
378 CFErrorRef copyError
= NULL
;
379 CFErrorRef createError
= NULL
;
380 SecKeyRef device_key
= NULL
;
382 require_quiet(sosFullPeerInfoRequiresUpdate(peer
, minimumViews
, excludedViews
), errOut
);
384 newViews
= SOSFullPeerInfoCopyViewUpdate(peer
, minimumViews
, excludedViews
);
386 device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, ©Error
);
387 require_action_quiet(device_key
, errOut
,
388 secnotice("upgrade", "SOSFullPeerInfoCopyDeviceKey failed: %@", copyError
));
390 SOSPeerInfoRef newPeer
= SOSPeerInfoCreateCurrentCopy(kCFAllocatorDefault
, peer
->peer_info
,
391 NULL
, NULL
, kCFBooleanFalse
, kCFBooleanTrue
, newViews
,
392 device_key
, &createError
);
393 require_action_quiet(newPeer
, errOut
,
394 secnotice("upgrade", "Peer info v2 create copy failed: %@", createError
));
396 CFTransferRetained(peer
->peer_info
, newPeer
);
401 CFReleaseNull(newViews
);
402 CFReleaseNull(copyError
);
403 CFReleaseNull(createError
);
404 CFReleaseNull(device_key
);
408 SOSViewResultCode
SOSFullPeerInfoViewStatus(SOSFullPeerInfoRef peer
, CFStringRef viewname
, CFErrorRef
*error
)
410 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(peer
);
411 if(!pi
) return kSOSCCGeneralViewError
;
412 return SOSPeerInfoViewStatus(pi
, viewname
, error
);
416 SOSSecurityPropertyResultCode
SOSFullPeerInfoUpdateSecurityProperty(SOSFullPeerInfoRef peer
, SOSViewActionCode action
, CFStringRef property
, CFErrorRef
* error
)
418 SOSSecurityPropertyResultCode retval
= kSOSCCGeneralSecurityPropertyError
;
419 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, error
);
420 require_quiet(device_key
, fail
);
422 SOSPeerInfoRef newPeer
= SOSPeerInfoCopyWithSecurityPropertyChange(kCFAllocatorDefault
, peer
->peer_info
, action
, property
, &retval
, device_key
, error
);
424 require_quiet(newPeer
, fail
);
426 CFReleaseNull(peer
->peer_info
);
427 peer
->peer_info
= newPeer
;
431 CFReleaseNull(device_key
);
435 SOSSecurityPropertyResultCode
SOSFullPeerInfoSecurityPropertyStatus(SOSFullPeerInfoRef peer
, CFStringRef property
, CFErrorRef
*error
)
437 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(peer
);
438 secnotice("secprop", "have pi %s", (pi
)? "true": "false");
439 if(!pi
) return kSOSCCGeneralSecurityPropertyError
;
440 return SOSPeerInfoSecurityPropertyStatus(pi
, property
, error
);
444 SOSPeerInfoRef
SOSFullPeerInfoGetPeerInfo(SOSFullPeerInfoRef fullPeer
) {
445 return fullPeer
?fullPeer
->peer_info
:NULL
;
448 // MARK: Private Key Retrieval and Existence
450 static SecKeyRef
SOSFullPeerInfoCopyPubKey(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
) {
451 SecKeyRef retval
= NULL
;
452 require_quiet(fpi
, errOut
);
453 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
454 require_quiet(pi
, errOut
);
455 retval
= SOSPeerInfoCopyPubKey(pi
, error
);
461 static SecKeyRef
SOSFullPeerInfoCopyMatchingPrivateKey(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
) {
462 SecKeyRef retval
= NULL
;
464 SecKeyRef pub
= SOSFullPeerInfoCopyPubKey(fpi
, error
);
465 require_quiet(pub
, exit
);
466 retval
= SecKeyCopyMatchingPrivateKey(pub
, error
);
472 static OSStatus
SOSFullPeerInfoGetMatchingPrivateKeyStatus(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
) {
473 OSStatus retval
= errSecParam
;
474 SecKeyRef pub
= SOSFullPeerInfoCopyPubKey(fpi
, error
);
475 require_quiet(pub
, exit
);
476 retval
= SecKeyGetMatchingPrivateKeyStatus(pub
, error
);
483 bool SOSFullPeerInfoValidate(SOSFullPeerInfoRef peer
, CFErrorRef
* error
) {
484 OSStatus result
= SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer
, error
);
485 if(result
== errSecSuccess
) return true;
489 bool SOSFullPeerInfoPrivKeyExists(SOSFullPeerInfoRef peer
) {
490 OSStatus result
= SOSFullPeerInfoGetMatchingPrivateKeyStatus(peer
, NULL
);
491 if(result
== errSecItemNotFound
|| result
== errSecParam
) return false;
495 bool SOSFullPeerInfoPurgePersistentKey(SOSFullPeerInfoRef fpi
, CFErrorRef
* error
) {
497 CFDictionaryRef privQuery
= NULL
;
498 CFMutableDictionaryRef query
= NULL
;
500 SecKeyRef pub
= SOSFullPeerInfoCopyPubKey(fpi
, error
);
501 require_quiet(pub
, fail
);
503 privQuery
= CreatePrivateKeyMatchingQuery(pub
, false);
504 query
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, privQuery
);
505 CFDictionaryAddValue(query
, kSecUseTombstones
, kCFBooleanFalse
);
507 result
= SecError(SecItemDelete(query
), error
, CFSTR("Deleting while purging"));
510 CFReleaseNull(privQuery
);
511 CFReleaseNull(query
);
516 SecKeyRef
SOSFullPeerInfoCopyDeviceKey(SOSFullPeerInfoRef fullPeer
, CFErrorRef
* error
) {
517 return SOSFullPeerInfoCopyMatchingPrivateKey(fullPeer
, error
);
521 // MARK: Encode and decode
523 size_t SOSFullPeerInfoGetDEREncodedSize(SOSFullPeerInfoRef peer
, CFErrorRef
*error
)
525 size_t peer_size
= SOSPeerInfoGetDEREncodedSize(peer
->peer_info
, error
);
529 size_t ref_size
= der_sizeof_data(peer
->key_ref
, error
);
533 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE
,
534 peer_size
+ ref_size
);
537 uint8_t* SOSFullPeerInfoEncodeToDER(SOSFullPeerInfoRef peer
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
)
539 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, der_end
, der
,
540 SOSPeerInfoEncodeToDER(peer
->peer_info
, error
, der
,
541 der_encode_data(peer
->key_ref
, error
, der
, der_end
)));
544 CFDataRef
SOSFullPeerInfoCopyEncodedData(SOSFullPeerInfoRef peer
, CFAllocatorRef allocator
, CFErrorRef
*error
)
546 size_t size
= SOSFullPeerInfoGetDEREncodedSize(peer
, error
);
549 uint8_t buffer
[size
];
550 uint8_t* start
= SOSFullPeerInfoEncodeToDER(peer
, error
, buffer
, buffer
+ sizeof(buffer
));
551 CFDataRef result
= CFDataCreate(kCFAllocatorDefault
, start
, size
);
555 bool SOSFullPeerInfoPromoteToApplication(SOSFullPeerInfoRef fpi
, SecKeyRef user_key
, CFErrorRef
*error
)
557 bool success
= false;
558 SOSPeerInfoRef old_pi
= NULL
;
560 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
561 require_quiet(device_key
, exit
);
563 old_pi
= fpi
->peer_info
;
564 fpi
->peer_info
= SOSPeerInfoCopyAsApplication(old_pi
, user_key
, device_key
, error
);
566 require_action_quiet(fpi
->peer_info
, exit
, fpi
->peer_info
= old_pi
; old_pi
= NULL
);
571 CFReleaseSafe(old_pi
);
572 CFReleaseSafe(device_key
);
576 bool SOSFullPeerInfoUpgradeSignatures(SOSFullPeerInfoRef fpi
, SecKeyRef user_key
, CFErrorRef
*error
)
578 bool success
= false;
579 SOSPeerInfoRef old_pi
= NULL
;
581 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
582 require_quiet(device_key
, exit
);
584 old_pi
= fpi
->peer_info
;
585 fpi
->peer_info
= SOSPeerInfoUpgradeSignatures(NULL
, user_key
, device_key
, old_pi
, error
);
587 require_action_quiet(fpi
->peer_info
, exit
, fpi
->peer_info
= old_pi
; old_pi
= NULL
);
592 CFReleaseSafe(old_pi
);
593 CFReleaseSafe(device_key
);
601 SOSPeerInfoRef
SOSFullPeerInfoPromoteToRetiredAndCopy(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
)
603 SOSPeerInfoRef peer_to_free
= NULL
;
604 SOSPeerInfoRef retired_peer
= NULL
;
605 SecKeyRef key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
606 require_quiet(key
, error_out
);
608 retired_peer
= SOSPeerInfoCreateRetirementTicket(NULL
, key
, fpi
->peer_info
, error
);
610 require_quiet(retired_peer
, error_out
);
612 peer_to_free
= fpi
->peer_info
;
613 fpi
->peer_info
= retired_peer
;
614 CFRetainSafe(fpi
->peer_info
);
618 CFReleaseNull(peer_to_free
);
623 bool SOSFullPeerInfoPing(SOSFullPeerInfoRef peer
, CFErrorRef
* error
) {
625 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, error
);
626 require_quiet(device_key
, fail
);
627 SOSPeerInfoRef newPeer
= SOSPeerInfoCopyWithPing(kCFAllocatorDefault
, peer
->peer_info
, device_key
, error
);
628 require_quiet(newPeer
, fail
);
630 CFReleaseNull(peer
->peer_info
);
631 peer
->peer_info
= newPeer
;
635 CFReleaseNull(device_key
);