5 // Created by Mitch Adler on 10/26/12.
9 #include <AssertMacros.h>
11 #include <SecureObjectSync/SOSFullPeerInfo.h>
12 #include <SecureObjectSync/SOSCircle.h>
14 #include <SecureObjectSync/SOSInternal.h>
16 #include <Security/SecKeyPriv.h>
17 #include <Security/SecItemPriv.h>
18 #include <Security/SecOTR.h>
19 #include <CoreFoundation/CFArray.h>
20 #include <dispatch/dispatch.h>
25 #include <utilities/SecCFWrappers.h>
26 #include <utilities/SecCFRelease.h>
28 #include <utilities/der_plist.h>
29 #include <utilities/der_plist_internal.h>
30 #include <corecrypto/ccder.h>
32 #include <CommonCrypto/CommonDigest.h>
33 #include <CommonCrypto/CommonDigestSPI.h>
35 #include <CoreFoundation/CoreFoundation.h>
37 #include "utilities/iOSforOSX.h"
39 #include <AssertMacros.h>
41 #include <utilities/SecCFError.h>
50 extern OSStatus
SecKeyCopyPublicBytes(SecKeyRef key
, CFDataRef
* publicBytes
);
51 extern SecKeyRef
SecKeyCreatePublicFromPrivate(SecKeyRef privateKey
);
56 struct __OpaqueSOSFullPeerInfo
{
59 SOSPeerInfoRef peer_info
;
63 CFGiblisWithHashFor(SOSFullPeerInfo
);
66 static CFStringRef sPublicKeyKey
= CFSTR("PublicSigningKey");
67 static CFStringRef sNameKey
= CFSTR("DeviceName");
68 static CFStringRef sVersionKey
= CFSTR("ConflictVersion");
70 CFStringRef kSOSFullPeerInfoDescriptionKey
= CFSTR("SOSFullPeerInfoDescription");
71 CFStringRef kSOSFullPeerInfoSignatureKey
= CFSTR("SOSFullPeerInfoSignature");
72 CFStringRef kSOSFullPeerInfoNameKey
= CFSTR("SOSFullPeerInfoName");
74 SOSFullPeerInfoRef
SOSFullPeerInfoCreate(CFAllocatorRef allocator
, CFDictionaryRef gestalt
, SecKeyRef signingKey
, CFErrorRef
* error
) {
75 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
77 fpi
->peer_info
= SOSPeerInfoCreate(allocator
, gestalt
, signingKey
, error
);
78 if (fpi
->peer_info
== NULL
) {
83 OSStatus result
= SecKeyCopyPersistentRef(signingKey
, &fpi
->key_ref
);
85 if (result
!= errSecSuccess
) {
96 SOSFullPeerInfoRef
SOSFullPeerInfoCreateCloudIdentity(CFAllocatorRef allocator
, SOSPeerInfoRef peer
, CFErrorRef
* error
) {
97 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
99 SecKeyRef pubKey
= NULL
;
101 fpi
->peer_info
= peer
;
102 CFRetainSafe(fpi
->peer_info
);
103 if (fpi
->peer_info
== NULL
) {
108 pubKey
= SOSPeerInfoCopyPubKey(peer
);
110 fpi
->key_ref
= SecKeyCreatePersistentRefToMatchingPrivateKey(pubKey
, error
);
112 if (fpi
->key_ref
== NULL
) {
118 CFReleaseNull(pubKey
);
123 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromDER(CFAllocatorRef allocator
, CFErrorRef
* error
,
124 const uint8_t** der_p
, const uint8_t *der_end
) {
125 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
127 const uint8_t *sequence_end
;
129 *der_p
= ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, &sequence_end
, *der_p
, der_end
);
131 fpi
->peer_info
= SOSPeerInfoCreateFromDER(allocator
, error
, der_p
, der_end
);
132 require_quiet(fpi
->peer_info
!= NULL
, fail
);
134 *der_p
= der_decode_data(allocator
, kCFPropertyListImmutable
, &fpi
->key_ref
, error
, *der_p
, sequence_end
);
135 require_quiet(*der_p
!= NULL
, fail
);
144 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromData(CFAllocatorRef allocator
, CFDataRef fullPeerData
, CFErrorRef
*error
)
146 size_t size
= CFDataGetLength(fullPeerData
);
147 const uint8_t *der
= CFDataGetBytePtr(fullPeerData
);
148 SOSFullPeerInfoRef inflated
= SOSFullPeerInfoCreateFromDER(allocator
, error
, &der
, der
+ size
);
152 static void SOSFullPeerInfoDestroy(CFTypeRef aObj
) {
153 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
155 CFReleaseNull(fpi
->peer_info
);
156 CFReleaseNull(fpi
->key_ref
);
159 static Boolean
SOSFullPeerInfoCompare(CFTypeRef lhs
, CFTypeRef rhs
) {
160 SOSFullPeerInfoRef lpeer
= (SOSFullPeerInfoRef
) lhs
;
161 SOSFullPeerInfoRef rpeer
= (SOSFullPeerInfoRef
) rhs
;
163 if (!CFEqual(lpeer
->peer_info
, rpeer
->peer_info
))
166 if (CFEqual(lpeer
->key_ref
, rpeer
->key_ref
))
169 SecKeyRef lpk
= SOSFullPeerInfoCopyDeviceKey(lpeer
, NULL
);
170 SecKeyRef rpk
= SOSFullPeerInfoCopyDeviceKey(rpeer
, NULL
);
172 bool match
= lpk
&& rpk
&& CFEqual(lpk
, rpk
);
180 static CFHashCode
SOSFullPeerInfoHash(CFTypeRef cf
) {
181 SOSFullPeerInfoRef peer
= (SOSFullPeerInfoRef
) cf
;
183 return CFHash(peer
->peer_info
);
186 static CFStringRef
SOSFullPeerInfoCopyDescription(CFTypeRef aObj
) {
187 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
189 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SOSFullPeerInfo@%p: \"%@\">"), fpi
, fpi
->peer_info
);
192 bool SOSFullPeerInfoUpdateGestalt(SOSFullPeerInfoRef peer
, CFDictionaryRef gestalt
, CFErrorRef
* error
)
194 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, error
);
195 require_quiet(device_key
, fail
);
197 SOSPeerInfoRef newPeer
= SOSPeerInfoCopyWithGestaltUpdate(kCFAllocatorDefault
, peer
->peer_info
,
198 gestalt
, device_key
, error
);
200 require_quiet(newPeer
, fail
);
202 CFReleaseNull(peer
->peer_info
);
203 peer
->peer_info
= newPeer
;
206 CFReleaseNull(device_key
);
210 CFReleaseNull(device_key
);
215 bool SOSFullPeerInfoValidate(SOSFullPeerInfoRef peer
, CFErrorRef
* error
) {
219 bool SOSFullPeerInfoPurgePersistentKey(SOSFullPeerInfoRef fullPeer
, CFErrorRef
* error
) {
220 CFDictionaryRef query
= CFDictionaryCreateForCFTypes(kCFAllocatorDefault
,
221 kSecValuePersistentRef
, fullPeer
->key_ref
,
222 kSecUseTombstones
, kCFBooleanFalse
,
224 SecItemDelete(query
);
225 CFReleaseNull(query
);
230 SOSPeerInfoRef
SOSFullPeerInfoGetPeerInfo(SOSFullPeerInfoRef fullPeer
)
232 return fullPeer
?fullPeer
->peer_info
:NULL
;
235 SecKeyRef
SOSFullPeerInfoCopyDeviceKey(SOSFullPeerInfoRef fullPeer
, CFErrorRef
* error
)
237 SecKeyRef device_key
= NULL
;
239 require(fullPeer
->key_ref
, fail
);
241 OSStatus result
= SecKeyFindWithPersistentRef(fullPeer
->key_ref
, &device_key
);
243 require_action_quiet(result
== errSecSuccess
, fail
, SecError(result
, error
, CFSTR("Finding Persistent Ref")));
248 CFReleaseNull(device_key
);
253 // MARK: Encode and decode
255 size_t SOSFullPeerInfoGetDEREncodedSize(SOSFullPeerInfoRef peer
, CFErrorRef
*error
)
257 size_t peer_size
= SOSPeerInfoGetDEREncodedSize(peer
->peer_info
, error
);
261 size_t ref_size
= der_sizeof_data(peer
->key_ref
, error
);
265 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE
,
266 peer_size
+ ref_size
);
269 uint8_t* SOSFullPeerInfoEncodeToDER(SOSFullPeerInfoRef peer
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
)
271 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, der_end
, der
,
272 SOSPeerInfoEncodeToDER(peer
->peer_info
, error
, der
,
273 der_encode_data(peer
->key_ref
, error
, der
, der_end
)));
276 CFDataRef
SOSFullPeerInfoCopyEncodedData(SOSFullPeerInfoRef peer
, CFAllocatorRef allocator
, CFErrorRef
*error
)
278 size_t size
= SOSFullPeerInfoGetDEREncodedSize(peer
, error
);
281 uint8_t buffer
[size
];
282 uint8_t* start
= SOSFullPeerInfoEncodeToDER(peer
, error
, buffer
, buffer
+ sizeof(buffer
));
283 CFDataRef result
= CFDataCreate(kCFAllocatorDefault
, start
, size
);
287 bool SOSFullPeerInfoPromoteToApplication(SOSFullPeerInfoRef fpi
, SecKeyRef user_key
, CFErrorRef
*error
)
289 bool success
= false;
290 SOSPeerInfoRef old_pi
= NULL
;
292 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
293 require_quiet(device_key
, exit
);
295 old_pi
= fpi
->peer_info
;
296 fpi
->peer_info
= SOSPeerInfoCopyAsApplication(old_pi
, user_key
, device_key
, error
);
298 require_action_quiet(fpi
->peer_info
, exit
, fpi
->peer_info
= old_pi
; old_pi
= NULL
);
303 CFReleaseSafe(old_pi
);
304 CFReleaseSafe(device_key
);
308 bool SOSFullPeerInfoUpgradeSignatures(SOSFullPeerInfoRef fpi
, SecKeyRef user_key
, CFErrorRef
*error
)
310 bool success
= false;
311 SOSPeerInfoRef old_pi
= NULL
;
313 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
314 require_quiet(device_key
, exit
);
316 old_pi
= fpi
->peer_info
;
317 fpi
->peer_info
= SOSPeerInfoUpgradeSignatures(NULL
, user_key
, device_key
, old_pi
, error
);
319 require_action_quiet(fpi
->peer_info
, exit
, fpi
->peer_info
= old_pi
; old_pi
= NULL
);
324 CFReleaseSafe(old_pi
);
325 CFReleaseSafe(device_key
);
333 SOSPeerInfoRef
SOSFullPeerInfoPromoteToRetiredAndCopy(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
)
335 SOSPeerInfoRef peer_to_free
= NULL
;
336 SOSPeerInfoRef retired_peer
= NULL
;
337 SecKeyRef key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
338 require_quiet(key
, error_out
);
340 retired_peer
= SOSPeerInfoCreateRetirementTicket(NULL
, key
, fpi
->peer_info
, error
);
342 require_quiet(retired_peer
, error_out
);
344 peer_to_free
= fpi
->peer_info
;
345 fpi
->peer_info
= retired_peer
;
346 CFRetainSafe(fpi
->peer_info
);
350 CFReleaseNull(peer_to_free
);