6 #ifndef sec_SOSAccountPriv_h
7 #define sec_SOSAccountPriv_h
9 #include "SOSAccount.h"
11 #include <CoreFoundation/CoreFoundation.h>
12 #include <CoreFoundation/CFRuntime.h>
13 #include <utilities/SecCFWrappers.h>
14 #include <utilities/SecCFError.h>
15 #include <utilities/SecAKSWrappers.h>
18 #include <Security/SecKeyPriv.h>
20 #include <utilities/der_plist.h>
21 #include <utilities/der_plist_internal.h>
22 #include <corecrypto/ccder.h>
24 #include <AssertMacros.h>
29 #include <SecureObjectSync/SOSInternal.h>
30 #include <SecureObjectSync/SOSCircle.h>
31 #include <SecureObjectSync/SOSCloudCircle.h>
32 #include <securityd/SOSCloudCircleServer.h>
33 #include <SecureObjectSync/SOSEngine.h>
34 #include <SecureObjectSync/SOSPeer.h>
35 #include <SecureObjectSync/SOSFullPeerInfo.h>
36 #include <SecureObjectSync/SOSPeerInfo.h>
37 #include <SecureObjectSync/SOSPeerInfoInternal.h>
38 #include <SecureObjectSync/SOSUserKeygen.h>
39 #include <utilities/iCloudKeychainTrace.h>
41 #include <Security/SecItemPriv.h>
43 struct __OpaqueSOSAccount
{
46 dispatch_queue_t queue
;
48 CFDictionaryRef gestalt
;
50 CFMutableDictionaryRef circle_identities
;
51 CFMutableDictionaryRef circles
;
52 CFMutableDictionaryRef retired_peers
;
54 bool user_public_trusted
;
55 CFDataRef user_key_parameters
;
56 SecKeyRef user_public
;
57 SecKeyRef previous_public
;
58 enum DepartureReason departure_code
;
60 // Non-persistent data
62 SOSDataSourceFactoryRef factory
;
63 SecKeyRef _user_private
;
64 dispatch_source_t user_private_timer
;
65 int lock_notification_token
;
67 SOSTransportKeyParameterRef key_transport
;
68 CFMutableDictionaryRef circle_transports
;
69 CFMutableDictionaryRef message_transports
;
72 CFMutableArrayRef change_blocks
;
75 SOSAccountRef
SOSAccountCreateBasic(CFAllocatorRef allocator
,
76 CFDictionaryRef gestalt
,
77 SOSDataSourceFactoryRef factory
);
79 bool SOSAccountEnsureFactoryCircles(SOSAccountRef a
);
81 void SOSAccountSetToNew(SOSAccountRef a
);
83 void SOSAccountForEachKnownCircle(SOSAccountRef account
,
84 void (^handle_incompatible
)(CFStringRef name
),
85 void (^handle_no_peer
)(SOSCircleRef circle
),
86 void (^handle_peer
)(SOSCircleRef circle
, SOSFullPeerInfoRef full_peer
));
88 bool SOSAccountIsMyPeerActiveInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
89 bool SOSAccountIsMyPeerActiveInCircleNamed(SOSAccountRef account
, CFStringRef circle_name
, CFErrorRef
* error
);
94 size_t der_sizeof_data_or_null(CFDataRef data
, CFErrorRef
* error
);
96 uint8_t* der_encode_data_or_null(CFDataRef data
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
98 const uint8_t* der_decode_data_or_null(CFAllocatorRef allocator
, CFDataRef
* data
,
100 const uint8_t* der
, const uint8_t* der_end
);
102 size_t der_sizeof_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
);
104 uint8_t* der_encode_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
106 const uint8_t* der_decode_public_bytes(CFAllocatorRef allocator
, CFIndex algorithmID
, SecKeyRef
* publicKey
, CFErrorRef
* error
, const uint8_t* der
, const uint8_t* der_end
);
108 const uint8_t* ccder_decode_bool(bool* boolean
, const uint8_t* der
, const uint8_t *der_end
);
110 size_t ccder_sizeof_bool(bool value __unused
, CFErrorRef
*error
);
112 uint8_t* ccder_encode_bool(bool value
, const uint8_t *der
, uint8_t *der_end
);
117 SOSAccountRef
SOSAccountCreateFromDER_V1(CFAllocatorRef allocator
,
118 SOSDataSourceFactoryRef factory
,
120 const uint8_t** der_p
, const uint8_t *der_end
);
122 SOSAccountRef
SOSAccountCreateFromDER_V2(CFAllocatorRef allocator
,
123 SOSDataSourceFactoryRef factory
,
125 const uint8_t** der_p
, const uint8_t *der_end
);
127 SOSAccountRef
SOSAccountCreateFromDER_V3(CFAllocatorRef allocator
,
128 SOSDataSourceFactoryRef factory
,
130 const uint8_t** der_p
, const uint8_t *der_end
);
132 SOSAccountRef
SOSAccountCreateFromDER(CFAllocatorRef allocator
,
133 SOSDataSourceFactoryRef factory
,
135 const uint8_t** der_p
, const uint8_t *der_end
);
137 SOSAccountRef
SOSAccountCreateFromData(CFAllocatorRef allocator
, CFDataRef circleData
,
138 SOSDataSourceFactoryRef factory
,
141 size_t SOSAccountGetDEREncodedSize(SOSAccountRef account
, CFErrorRef
*error
);
143 uint8_t* SOSAccountEncodeToDER(SOSAccountRef account
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
145 size_t SOSAccountGetDEREncodedSize_V3(SOSAccountRef account
, CFErrorRef
*error
);
147 uint8_t* SOSAccountEncodeToDER_V3(SOSAccountRef account
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
149 size_t SOSAccountGetDEREncodedSize_V2(SOSAccountRef account
, CFErrorRef
*error
);
151 uint8_t* SOSAccountEncodeToDER_V2(SOSAccountRef account
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
153 size_t SOSAccountGetDEREncodedSize_V1(SOSAccountRef account
, CFErrorRef
*error
);
155 uint8_t* SOSAccountEncodeToDER_V1(SOSAccountRef account
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
157 CFDataRef
SOSAccountCopyEncodedData(SOSAccountRef account
, CFAllocatorRef allocator
, CFErrorRef
*error
);
161 bool SOSAccountHandleCircleMessage(SOSAccountRef account
,
162 CFStringRef circleName
, CFDataRef encodedCircleMessage
, CFErrorRef
*error
);
164 void SOSAccountRecordRetiredPeerInCircleNamed(SOSAccountRef account
, CFStringRef circleName
, SOSPeerInfoRef retiree
);
167 bool SOSAccountHandleUpdateCircle(SOSAccountRef account
,
168 SOSCircleRef prospective_circle
,
174 void SOSAccountForEachKnownCircle(SOSAccountRef account
,
175 void (^handle_incompatible
)(CFStringRef name
),
176 void (^handle_no_peer
)(SOSCircleRef circle
),
177 void (^handle_peer
)(SOSCircleRef circle
, SOSFullPeerInfoRef full_peer
));
179 int SOSAccountCountCircles(SOSAccountRef a
);
181 SOSFullPeerInfoRef
SOSAccountMakeMyFullPeerInCircleNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
183 bool SOSAccountDestroyCirclePeerInfoNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
* error
);
185 bool SOSAccountDestroyCirclePeerInfo(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
187 SOSFullPeerInfoRef
SOSAccountGetMyFullPeerInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
189 SOSPeerInfoRef
SOSAccountGetMyPeerInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
191 SOSPeerInfoRef
SOSAccountGetMyPeerInCircleNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
193 bool SOSAccountIsActivePeerInCircleNamed(SOSAccountRef account
, CFStringRef circle_name
, CFStringRef peerid
, CFErrorRef
* error
);
195 bool SOSAccountIsMyPeerActiveInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
197 SOSCircleRef
SOSAccountFindCircle(SOSAccountRef a
, CFStringRef name
, CFErrorRef
*error
);
199 SOSCircleRef
SOSAccountEnsureCircle(SOSAccountRef a
, CFStringRef name
, CFErrorRef
*error
);
201 bool SOSAccountUpdateCircleFromRemote(SOSAccountRef account
, SOSCircleRef newCircle
, CFErrorRef
*error
);
203 bool SOSAccountUpdateCircle(SOSAccountRef account
, SOSCircleRef newCircle
, CFErrorRef
*error
);
205 bool SOSAccountModifyCircle(SOSAccountRef account
,
206 CFStringRef circleName
,
208 bool (^action
)(SOSCircleRef circle
));
210 SOSFullPeerInfoRef
SOSAccountGetMyFullPeerInCircleNamedIfPresent(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
212 void AppendCircleKeyName(CFMutableArrayRef array
, CFStringRef name
);
214 CFStringRef
SOSInterestListCopyDescription(CFArrayRef interests
);
217 // Peers and PeerInfos
218 bool SOSAccountDestroyCirclePeerInfoNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
* error
);
220 bool SOSAccountDestroyCirclePeerInfo(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
222 SOSPeerInfoRef
SOSAccountGetMyPeerInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
224 SOSPeerInfoRef
SOSAccountGetMyPeerInCircleNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
226 bool SOSAccountIsActivePeerInCircleNamed(SOSAccountRef account
, CFStringRef circle_name
, CFStringRef peerid
, CFErrorRef
* error
);
228 bool SOSAccountIsMyPeerActiveInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
230 // FullPeerInfos - including Cloud Identity
231 SOSFullPeerInfoRef
CopyCloudKeychainIdentity(SOSPeerInfoRef cloudPeer
, CFErrorRef
*error
);
233 SOSFullPeerInfoRef
SOSAccountGetMyFullPeerInCircleNamedIfPresent(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
235 bool SOSAccountIsAccountIdentity(SOSAccountRef account
, SOSPeerInfoRef peer_info
, CFErrorRef
*error
);
237 SOSFullPeerInfoRef
SOSAccountMakeMyFullPeerInCircleNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
);
239 SOSFullPeerInfoRef
SOSAccountGetMyFullPeerInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
241 SOSPeerInfoRef
GenerateNewCloudIdentityPeerInfo(CFErrorRef
*error
);
244 bool SOSAccountHasPublicKey(SOSAccountRef account
, CFErrorRef
* error
);
245 void SOSAccountSetPreviousPublic(SOSAccountRef account
);
246 bool SOSAccountPublishCloudParameters(SOSAccountRef account
, CFErrorRef
* error
);
247 bool SOSAccountRetrieveCloudParameters(SOSAccountRef account
, SecKeyRef
*newKey
,
249 CFDataRef
*newParameters
, CFErrorRef
* error
);
252 void SOSAccountSetUserPublicTrustedForTesting(SOSAccountRef account
);
253 CFDictionaryRef
SOSAccountGetMessageTransports(SOSAccountRef account
);
257 static inline void CFArrayAppendValueIfNot(CFMutableArrayRef array
, CFTypeRef value
, CFTypeRef excludedValue
)
259 if (!CFEqualSafe(value
, excludedValue
))
260 CFArrayAppendValue(array
, value
);
263 static inline CFMutableDictionaryRef
CFDictionaryEnsureCFDictionaryAndGetCurrentValue(CFMutableDictionaryRef dict
, CFTypeRef key
)
265 CFMutableDictionaryRef result
= (CFMutableDictionaryRef
) CFDictionaryGetValue(dict
, key
);
267 if (!isDictionary(result
)) {
268 result
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
269 CFDictionarySetValue(dict
, key
, result
);
270 CFReleaseSafe(result
);
276 static inline CFMutableArrayRef
CFDictionaryEnsureCFArrayAndGetCurrentValue(CFMutableDictionaryRef dict
, CFTypeRef key
)
278 CFMutableArrayRef result
= (CFMutableArrayRef
) CFDictionaryGetValue(dict
, key
);
280 if (!isArray(result
)) {
281 result
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
282 CFDictionarySetValue(dict
, key
, result
);
283 CFReleaseSafe(result
);
289 bool sosAccountLeaveCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
);
291 bool SOSAccountEnsurePeerRegistration(SOSAccountRef account
, CFErrorRef
*error
);