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 <SecureObjectSync/SOSFullPeerInfo.h>
28 #include <SecureObjectSync/SOSCircle.h>
30 #include <SecureObjectSync/SOSInternal.h>
32 #include <Security/SecKeyPriv.h>
33 #include <Security/SecItemPriv.h>
34 #include <Security/SecOTR.h>
35 #include <CoreFoundation/CFArray.h>
36 #include <dispatch/dispatch.h>
41 #include <utilities/SecCFWrappers.h>
42 #include <utilities/SecCFRelease.h>
44 #include <utilities/der_plist.h>
45 #include <utilities/der_plist_internal.h>
46 #include <corecrypto/ccder.h>
48 #include <CommonCrypto/CommonDigest.h>
49 #include <CommonCrypto/CommonDigestSPI.h>
51 #include <CoreFoundation/CoreFoundation.h>
53 #include "utilities/iOSforOSX.h"
55 #include <AssertMacros.h>
57 #include <utilities/SecCFError.h>
66 extern OSStatus
SecKeyCopyPublicBytes(SecKeyRef key
, CFDataRef
* publicBytes
);
67 extern SecKeyRef
SecKeyCreatePublicFromPrivate(SecKeyRef privateKey
);
72 struct __OpaqueSOSFullPeerInfo
{
75 SOSPeerInfoRef peer_info
;
79 CFGiblisWithHashFor(SOSFullPeerInfo
);
82 static CFStringRef sPublicKeyKey
= CFSTR("PublicSigningKey");
83 static CFStringRef sNameKey
= CFSTR("DeviceName");
84 static CFStringRef sVersionKey
= CFSTR("ConflictVersion");
86 CFStringRef kSOSFullPeerInfoDescriptionKey
= CFSTR("SOSFullPeerInfoDescription");
87 CFStringRef kSOSFullPeerInfoSignatureKey
= CFSTR("SOSFullPeerInfoSignature");
88 CFStringRef kSOSFullPeerInfoNameKey
= CFSTR("SOSFullPeerInfoName");
90 SOSFullPeerInfoRef
SOSFullPeerInfoCreate(CFAllocatorRef allocator
, CFDictionaryRef gestalt
, SecKeyRef signingKey
, CFErrorRef
* error
) {
91 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
93 fpi
->peer_info
= SOSPeerInfoCreate(allocator
, gestalt
, signingKey
, error
);
94 if (fpi
->peer_info
== NULL
) {
99 OSStatus result
= SecKeyCopyPersistentRef(signingKey
, &fpi
->key_ref
);
101 if (result
!= errSecSuccess
) {
112 SOSFullPeerInfoRef
SOSFullPeerInfoCreateCloudIdentity(CFAllocatorRef allocator
, SOSPeerInfoRef peer
, CFErrorRef
* error
) {
113 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
115 SecKeyRef pubKey
= NULL
;
117 fpi
->peer_info
= peer
;
118 CFRetainSafe(fpi
->peer_info
);
119 if (fpi
->peer_info
== NULL
) {
124 pubKey
= SOSPeerInfoCopyPubKey(peer
);
126 fpi
->key_ref
= SecKeyCreatePersistentRefToMatchingPrivateKey(pubKey
, error
);
128 if (fpi
->key_ref
== NULL
) {
134 CFReleaseNull(pubKey
);
139 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromDER(CFAllocatorRef allocator
, CFErrorRef
* error
,
140 const uint8_t** der_p
, const uint8_t *der_end
) {
141 SOSFullPeerInfoRef fpi
= CFTypeAllocate(SOSFullPeerInfo
, struct __OpaqueSOSFullPeerInfo
, allocator
);
143 const uint8_t *sequence_end
;
145 *der_p
= ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, &sequence_end
, *der_p
, der_end
);
147 fpi
->peer_info
= SOSPeerInfoCreateFromDER(allocator
, error
, der_p
, der_end
);
148 require_quiet(fpi
->peer_info
!= NULL
, fail
);
150 *der_p
= der_decode_data(allocator
, kCFPropertyListImmutable
, &fpi
->key_ref
, error
, *der_p
, sequence_end
);
151 require_quiet(*der_p
!= NULL
, fail
);
160 SOSFullPeerInfoRef
SOSFullPeerInfoCreateFromData(CFAllocatorRef allocator
, CFDataRef fullPeerData
, CFErrorRef
*error
)
162 size_t size
= CFDataGetLength(fullPeerData
);
163 const uint8_t *der
= CFDataGetBytePtr(fullPeerData
);
164 SOSFullPeerInfoRef inflated
= SOSFullPeerInfoCreateFromDER(allocator
, error
, &der
, der
+ size
);
168 static void SOSFullPeerInfoDestroy(CFTypeRef aObj
) {
169 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
171 CFReleaseNull(fpi
->peer_info
);
172 CFReleaseNull(fpi
->key_ref
);
175 static Boolean
SOSFullPeerInfoCompare(CFTypeRef lhs
, CFTypeRef rhs
) {
176 SOSFullPeerInfoRef lpeer
= (SOSFullPeerInfoRef
) lhs
;
177 SOSFullPeerInfoRef rpeer
= (SOSFullPeerInfoRef
) rhs
;
179 if (!CFEqual(lpeer
->peer_info
, rpeer
->peer_info
))
182 if (CFEqual(lpeer
->key_ref
, rpeer
->key_ref
))
185 SecKeyRef lpk
= SOSFullPeerInfoCopyDeviceKey(lpeer
, NULL
);
186 SecKeyRef rpk
= SOSFullPeerInfoCopyDeviceKey(rpeer
, NULL
);
188 bool match
= lpk
&& rpk
&& CFEqual(lpk
, rpk
);
196 static CFHashCode
SOSFullPeerInfoHash(CFTypeRef cf
) {
197 SOSFullPeerInfoRef peer
= (SOSFullPeerInfoRef
) cf
;
199 return CFHash(peer
->peer_info
);
202 static CFStringRef
SOSFullPeerInfoCopyFormatDescription(CFTypeRef aObj
, CFDictionaryRef formatOptions
) {
203 SOSFullPeerInfoRef fpi
= (SOSFullPeerInfoRef
) aObj
;
205 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SOSFullPeerInfo@%p: \"%@\">"), fpi
, fpi
->peer_info
);
208 bool SOSFullPeerInfoUpdateGestalt(SOSFullPeerInfoRef peer
, CFDictionaryRef gestalt
, CFErrorRef
* error
)
210 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(peer
, error
);
211 require_quiet(device_key
, fail
);
213 SOSPeerInfoRef newPeer
= SOSPeerInfoCopyWithGestaltUpdate(kCFAllocatorDefault
, peer
->peer_info
,
214 gestalt
, device_key
, error
);
216 require_quiet(newPeer
, fail
);
218 CFReleaseNull(peer
->peer_info
);
219 peer
->peer_info
= newPeer
;
222 CFReleaseNull(device_key
);
226 CFReleaseNull(device_key
);
231 bool SOSFullPeerInfoValidate(SOSFullPeerInfoRef peer
, CFErrorRef
* error
) {
232 SecKeyRef device_key
= NULL
;
233 OSStatus result
= SecKeyFindWithPersistentRef(peer
->key_ref
, &device_key
);
234 CFReleaseNull(device_key
);
235 if(result
== errSecSuccess
) return true;
239 bool SOSFullPeerInfoPurgePersistentKey(SOSFullPeerInfoRef fullPeer
, CFErrorRef
* error
) {
240 CFDictionaryRef query
= CFDictionaryCreateForCFTypes(kCFAllocatorDefault
,
241 kSecValuePersistentRef
, fullPeer
->key_ref
,
242 kSecUseTombstones
, kCFBooleanFalse
,
244 SecItemDelete(query
);
245 CFReleaseNull(query
);
246 CFReleaseNull(fullPeer
->key_ref
);
251 SOSPeerInfoRef
SOSFullPeerInfoGetPeerInfo(SOSFullPeerInfoRef fullPeer
)
253 return fullPeer
?fullPeer
->peer_info
:NULL
;
256 SecKeyRef
SOSFullPeerInfoCopyDeviceKey(SOSFullPeerInfoRef fullPeer
, CFErrorRef
* error
)
258 SecKeyRef device_key
= NULL
;
260 require(fullPeer
&& fullPeer
->key_ref
, fail
);
262 OSStatus result
= SecKeyFindWithPersistentRef(fullPeer
->key_ref
, &device_key
);
264 require_action_quiet(result
== errSecSuccess
, fail
, SecError(result
, error
, CFSTR("Finding Persistent Ref")));
269 CFReleaseNull(device_key
);
274 // MARK: Encode and decode
276 size_t SOSFullPeerInfoGetDEREncodedSize(SOSFullPeerInfoRef peer
, CFErrorRef
*error
)
278 size_t peer_size
= SOSPeerInfoGetDEREncodedSize(peer
->peer_info
, error
);
282 size_t ref_size
= der_sizeof_data(peer
->key_ref
, error
);
286 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE
,
287 peer_size
+ ref_size
);
290 uint8_t* SOSFullPeerInfoEncodeToDER(SOSFullPeerInfoRef peer
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
)
292 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, der_end
, der
,
293 SOSPeerInfoEncodeToDER(peer
->peer_info
, error
, der
,
294 der_encode_data(peer
->key_ref
, error
, der
, der_end
)));
297 CFDataRef
SOSFullPeerInfoCopyEncodedData(SOSFullPeerInfoRef peer
, CFAllocatorRef allocator
, CFErrorRef
*error
)
299 size_t size
= SOSFullPeerInfoGetDEREncodedSize(peer
, error
);
302 uint8_t buffer
[size
];
303 uint8_t* start
= SOSFullPeerInfoEncodeToDER(peer
, error
, buffer
, buffer
+ sizeof(buffer
));
304 CFDataRef result
= CFDataCreate(kCFAllocatorDefault
, start
, size
);
308 bool SOSFullPeerInfoPromoteToApplication(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
= SOSPeerInfoCopyAsApplication(old_pi
, user_key
, device_key
, 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
);
329 bool SOSFullPeerInfoUpgradeSignatures(SOSFullPeerInfoRef fpi
, SecKeyRef user_key
, CFErrorRef
*error
)
331 bool success
= false;
332 SOSPeerInfoRef old_pi
= NULL
;
334 SecKeyRef device_key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
335 require_quiet(device_key
, exit
);
337 old_pi
= fpi
->peer_info
;
338 fpi
->peer_info
= SOSPeerInfoUpgradeSignatures(NULL
, user_key
, device_key
, old_pi
, error
);
340 require_action_quiet(fpi
->peer_info
, exit
, fpi
->peer_info
= old_pi
; old_pi
= NULL
);
345 CFReleaseSafe(old_pi
);
346 CFReleaseSafe(device_key
);
354 SOSPeerInfoRef
SOSFullPeerInfoPromoteToRetiredAndCopy(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
)
356 SOSPeerInfoRef peer_to_free
= NULL
;
357 SOSPeerInfoRef retired_peer
= NULL
;
358 SecKeyRef key
= SOSFullPeerInfoCopyDeviceKey(fpi
, error
);
359 require_quiet(key
, error_out
);
361 retired_peer
= SOSPeerInfoCreateRetirementTicket(NULL
, key
, fpi
->peer_info
, error
);
363 require_quiet(retired_peer
, error_out
);
365 peer_to_free
= fpi
->peer_info
;
366 fpi
->peer_info
= retired_peer
;
367 CFRetainSafe(fpi
->peer_info
);
371 CFReleaseNull(peer_to_free
);