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@
27 The functions provided in SOSCircle.h provide an interface to a
28 secure object syncing circle for a single class
31 #ifndef _SOSACCOUNT_H_
32 #define _SOSACCOUNT_H_
34 /* Forward declarations of SOS types. */
35 typedef struct __OpaqueSOSAccount
*SOSAccountRef
;
36 typedef struct __OpaqueSOSRecoveryKeyBag
*SOSRecoveryKeyBagRef
;
38 #include <CoreFoundation/CoreFoundation.h>
40 #include <Security/SecureObjectSync/SOSAccountTransaction.h>
41 #include <Security/SecureObjectSync/SOSCircle.h>
42 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
44 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
45 #include <Security/SecureObjectSync/SOSTransportKeyParameter.h>
46 #include <Security/SecureObjectSync/SOSTransportCircle.h>
47 #include <Security/SecureObjectSync/SOSTransportMessage.h>
48 #include <Security/SecureObjectSync/SOSRing.h>
49 #include <Security/SecureObjectSync/SOSPeerInfoSecurityProperties.h>
50 #include <Security/SecureObjectSync/SOSRecoveryKeyBag.h>
52 #include <dispatch/dispatch.h>
56 #define RETIREMENT_FINALIZATION_SECONDS (24*60*60)
59 typedef void (^SOSAccountCircleMembershipChangeBlock
)(SOSCircleRef new_circle
,
60 CFSetRef added_peers
, CFSetRef removed_peers
,
61 CFSetRef added_applicants
, CFSetRef removed_applicants
);
62 typedef void (^SOSAccountSyncablePeersBlock
)(CFArrayRef trustedPeers
, CFArrayRef addedPeers
, CFArrayRef removedPeers
);
63 typedef bool (^SOSAccountWaitForInitialSyncBlock
)(SOSAccountRef account
);
64 typedef void (^SOSAccountSaveBlock
)(CFDataRef flattenedAccount
, CFErrorRef flattenFailError
);
66 SOSAccountRef
SOSAccountCreate(CFAllocatorRef allocator
,
67 CFDictionaryRef gestalt
,
68 SOSDataSourceFactoryRef factory
);
69 SOSAccountRef
SOSAccountCreateBasic(CFAllocatorRef allocator
,
70 CFDictionaryRef gestalt
,
71 SOSDataSourceFactoryRef factory
);
74 CFTypeID
SOSAccountGetTypeID(void);
77 // MARK: Persistent Encode decode
80 SOSAccountRef
SOSAccountCreateFromDER(CFAllocatorRef allocator
, SOSDataSourceFactoryRef factory
,
82 const uint8_t** der_p
, const uint8_t *der_end
);
83 SOSAccountRef
SOSAccountCreateFromData(CFAllocatorRef allocator
, CFDataRef circleData
,
84 SOSDataSourceFactoryRef factory
,
87 size_t SOSAccountGetDEREncodedSize(SOSAccountRef cir
, CFErrorRef
*error
);
88 uint8_t* SOSAccountEncodeToDER(SOSAccountRef cir
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
89 size_t SOSAccountGetDEREncodedSize_V3(SOSAccountRef cir
, CFErrorRef
*error
);
90 uint8_t* SOSAccountEncodeToDER_V3(SOSAccountRef cir
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
91 CFDataRef
SOSAccountCopyEncodedData(SOSAccountRef circle
, CFAllocatorRef allocator
, CFErrorRef
*error
);
94 CFStringRef
SOSAccountCopyDeviceID(SOSAccountRef account
, CFErrorRef
*error
);
95 bool SOSAccountSetMyDSID(SOSAccountTransactionRef txn
, CFStringRef IDS
, CFErrorRef
* errror
);
96 bool SOSAccountSendIDSTestMessage(SOSAccountRef account
, CFStringRef message
, CFErrorRef
*error
);
97 bool SOSAccountStartPingTest(SOSAccountRef account
, CFStringRef message
, CFErrorRef
*error
);
98 bool SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(SOSAccountRef account
, CFErrorRef
*error
);
101 // MARK: Credential management
104 SecKeyRef
SOSAccountGetTrustedPublicCredential(SOSAccountRef account
, CFErrorRef
* error
);
106 SecKeyRef
SOSAccountGetPrivateCredential(SOSAccountRef account
, CFErrorRef
* error
);
107 CFDataRef
SOSAccountGetCachedPassword(SOSAccountRef account
, CFErrorRef
* error
);
109 void SOSAccountSetParameters(SOSAccountRef account
, CFDataRef parameters
);
111 void SOSAccountPurgePrivateCredential(SOSAccountRef account
);
113 bool SOSAccountTryUserCredentials(SOSAccountRef account
,
114 CFStringRef user_account
, CFDataRef user_password
,
117 bool SOSAccountAssertUserCredentials(SOSAccountRef account
,
118 CFStringRef user_account
, CFDataRef user_password
,
121 bool SOSAccountRetryUserCredentials(SOSAccountRef account
);
122 void SOSAccountSetUnTrustedUserPublicKey(SOSAccountRef account
, SecKeyRef publicKey
);
124 bool SOSAccountGenerationSignatureUpdate(SOSAccountRef account
, CFErrorRef
*error
);
127 // MARK: Circle management
130 bool SOSAccountUpdateCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
*error
);
131 void SOSTransportEachMessage(SOSAccountRef account
, CFDictionaryRef updates
, CFErrorRef
*error
);
134 SOSCCStatus
SOSAccountGetCircleStatus(SOSAccountRef account
, CFErrorRef
* error
);
135 CFStringRef
SOSAccountGetSOSCCStatusString(SOSCCStatus status
);
136 bool SOSAccountIsInCircle(SOSAccountRef account
, CFErrorRef
*error
);
137 bool SOSAccountJoinCircles(SOSAccountTransactionRef aTxn
, CFErrorRef
* error
);
138 bool SOSAccountJoinCirclesAfterRestore(SOSAccountTransactionRef aTxn
, CFErrorRef
* error
);
139 bool SOSAccountLeaveCircle(SOSAccountRef account
,CFErrorRef
* error
);
140 bool SOSAccountRemovePeersFromCircle(SOSAccountRef account
, CFArrayRef peers
, CFErrorRef
* error
);
141 bool SOSAccountBail(SOSAccountRef account
, uint64_t limit_in_seconds
, CFErrorRef
* error
);
142 bool SOSAccountAcceptApplicants(SOSAccountRef account
, CFArrayRef applicants
, CFErrorRef
* error
);
143 bool SOSAccountRejectApplicants(SOSAccountRef account
, CFArrayRef applicants
, CFErrorRef
* error
);
145 bool SOSAccountResetToOffering(SOSAccountTransactionRef aTxn
, CFErrorRef
* error
);
146 bool SOSAccountResetToEmpty(SOSAccountRef account
, CFErrorRef
* error
);
147 bool SOSValidateUserPublic(SOSAccountRef account
, CFErrorRef
* error
);
149 void SOSAccountForEachCirclePeerExceptMe(SOSAccountRef account
, void (^action
)(SOSPeerInfoRef peer
));
151 CFArrayRef
SOSAccountCopyApplicants(SOSAccountRef account
, CFErrorRef
*error
);
152 CFArrayRef
SOSAccountCopyGeneration(SOSAccountRef account
, CFErrorRef
*error
);
153 CFArrayRef
SOSAccountCopyValidPeers(SOSAccountRef account
, CFErrorRef
*error
);
154 CFArrayRef
SOSAccountCopyPeersToListenTo(SOSAccountRef account
, CFErrorRef
*error
);
155 CFArrayRef
SOSAccountCopyNotValidPeers(SOSAccountRef account
, CFErrorRef
*error
);
156 CFArrayRef
SOSAccountCopyRetired(SOSAccountRef account
, CFErrorRef
*error
);
157 CFArrayRef
SOSAccountCopyViewUnaware(SOSAccountRef account
, CFErrorRef
*error
);
158 CFArrayRef
SOSAccountCopyPeers(SOSAccountRef account
, CFErrorRef
*error
);
159 CFArrayRef
SOSAccountCopyActivePeers(SOSAccountRef account
, CFErrorRef
*error
);
160 CFArrayRef
SOSAccountCopyActiveValidPeers(SOSAccountRef account
, CFErrorRef
*error
);
161 CFArrayRef
SOSAccountCopyConcurringPeers(SOSAccountRef account
, CFErrorRef
*error
);
163 SOSFullPeerInfoRef
SOSAccountCopyAccountIdentityPeerInfo(SOSAccountRef account
, CFAllocatorRef allocator
, CFErrorRef
* error
);
164 bool SOSAccountIsAccountIdentity(SOSAccountRef account
, SOSPeerInfoRef peer_info
, CFErrorRef
*error
);
166 enum DepartureReason
SOSAccountGetLastDepartureReason(SOSAccountRef account
, CFErrorRef
* error
);
169 // MARK: iCloud Identity
171 bool SOSAccountAddiCloudIdentity(SOSAccountRef account
, SOSCircleRef circle
, SecKeyRef user_key
, CFErrorRef
*error
);
172 bool SOSAccountRemoveIncompleteiCloudIdentities(SOSAccountRef account
, SOSCircleRef circle
, SecKeyRef privKey
, CFErrorRef
*error
);
178 void SOSAccountSetSaveBlock(SOSAccountRef account
, SOSAccountSaveBlock saveBlock
);
179 void SOSAccountFlattenToSaveBlock(SOSAccountRef account
);
182 // MARK: Change blocks
184 void SOSAccountAddChangeBlock(SOSAccountRef a
, SOSAccountCircleMembershipChangeBlock changeBlock
);
185 void SOSAccountRemoveChangeBlock(SOSAccountRef a
, SOSAccountCircleMembershipChangeBlock changeBlock
);
187 void SOSAccountAddSyncablePeerBlock(SOSAccountRef a
,
189 SOSAccountSyncablePeersBlock changeBlock
);
192 // MARK: Local device gestalt change.
194 bool SOSAccountUpdateGestalt(SOSAccountRef account
, CFDictionaryRef new_gestalt
);
196 CFDictionaryRef
SOSAccountCopyGestalt(SOSAccountRef account
);
198 CFDictionaryRef
SOSAccountCopyV2Dictionary(SOSAccountRef account
);
200 bool SOSAccountUpdateV2Dictionary(SOSAccountRef account
, CFDictionaryRef newV2Dict
);
202 bool SOSAccountUpdateFullPeerInfo(SOSAccountRef account
, CFSetRef minimumViews
, CFSetRef excludedViews
);
204 SOSViewResultCode
SOSAccountUpdateView(SOSAccountRef account
, CFStringRef viewname
, SOSViewActionCode actionCode
, CFErrorRef
*error
);
206 SOSViewResultCode
SOSAccountViewStatus(SOSAccountRef account
, CFStringRef viewname
, CFErrorRef
*error
);
208 bool SOSAccountUpdateViewSets(SOSAccountRef account
, CFSetRef enabledViews
, CFSetRef disabledViews
);
210 void SOSAccountPendEnableViewSet(SOSAccountRef account
, CFSetRef enabledViews
);
211 void SOSAccountPendDisableViewSet(SOSAccountRef account
, CFSetRef disabledViews
);
214 SOSSecurityPropertyResultCode
SOSAccountUpdateSecurityProperty(SOSAccountRef account
, CFStringRef property
, SOSSecurityPropertyActionCode actionCode
, CFErrorRef
*error
);
216 SOSSecurityPropertyResultCode
SOSAccountSecurityPropertyStatus(SOSAccountRef account
, CFStringRef property
, CFErrorRef
*error
);
219 bool SOSAccountHandleParametersChange(SOSAccountRef account
, CFDataRef updates
, CFErrorRef
*error
);
222 // MARK: Requests for syncing later
224 bool SOSAccountRequestSyncWithAllPeers(SOSAccountTransactionRef txn
, CFErrorRef
*error
);
227 // MARK: Outgoing/Sync functions
230 bool SOSAccountSyncWithKVSPeerWithMessage(SOSAccountTransactionRef txn
, CFStringRef peerid
, CFDataRef message
, CFErrorRef
*error
);
231 bool SOSAccountClearPeerMessageKey(SOSAccountTransactionRef txn
, CFStringRef peerID
, CFErrorRef
*error
);
233 CF_RETURNS_RETAINED CFSetRef
SOSAccountProcessSyncWithPeers(SOSAccountTransactionRef txn
, CFSetRef
/* CFStringRef */ peers
, CFSetRef
/* CFStringRef */ backupPeers
, CFErrorRef
*error
);
235 bool SOSAccountSendIKSPSyncList(SOSAccountRef account
, CFErrorRef
*error
);
236 bool SOSAccountSyncWithKVSUsingIDSID(SOSAccountRef account
, CFStringRef deviceID
, CFErrorRef
*error
);
240 // MARK: Cleanup functions
242 bool SOSAccountCleanupAfterPeer(SOSAccountRef account
, size_t seconds
, SOSCircleRef circle
,
243 SOSPeerInfoRef cleanupPeer
, CFErrorRef
* error
);
245 bool SOSAccountCleanupRetirementTickets(SOSAccountRef account
, size_t seconds
, CFErrorRef
* error
);
247 bool SOSAccountScanForRetired(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
*error
);
249 SOSCircleRef
SOSAccountCloneCircleWithRetirement(SOSAccountRef account
, SOSCircleRef starting_circle
, CFErrorRef
*error
);
251 bool SOSAccountPostDebugScope(SOSAccountRef account
, CFTypeRef scope
, CFErrorRef
*error
);
254 // MARK: Version incompatibility Functions
256 CFStringRef
SOSAccountCopyIncompatibilityInfo(SOSAccountRef account
, CFErrorRef
* error
);
259 // MARK: Backup functions
262 bool SOSAccountIsBackupRingEmpty(SOSAccountRef account
, CFStringRef viewName
);
263 bool SOSAccountNewBKSBForView(SOSAccountRef account
, CFStringRef viewName
, CFErrorRef
*error
);
265 bool SOSAccountSetBackupPublicKey(SOSAccountTransactionRef aTxn
, CFDataRef backupKey
, CFErrorRef
*error
);
266 bool SOSAccountRemoveBackupPublickey(SOSAccountTransactionRef aTxn
, CFErrorRef
*error
);
267 bool SOSAccountSetBSKBagForAllSlices(SOSAccountRef account
, CFDataRef backupSlice
, bool setupV0Only
, CFErrorRef
*error
);
269 SOSBackupSliceKeyBagRef
SOSAccountBackupSliceKeyBagForView(SOSAccountRef account
, CFStringRef viewName
, CFErrorRef
* error
);
271 bool SOSAccountIsLastBackupPeer(SOSAccountRef account
, CFErrorRef
*error
);
275 // MARK: Recovery Public Key Functions
277 bool SOSAccountRegisterRecoveryPublicKey(SOSAccountTransactionRef txn
, CFDataRef recovery_key
, CFErrorRef
*error
);
278 CFDataRef
SOSAccountCopyRecoveryPublicKey(SOSAccountTransactionRef txn
, CFErrorRef
*error
);
279 bool SOSAccountClearRecoveryPublicKey(SOSAccountTransactionRef txn
, CFDataRef recovery_key
, CFErrorRef
*error
);
280 bool SOSAccountSetRecoveryKey(SOSAccountRef account
, CFDataRef pubData
, CFErrorRef
*error
);
281 bool SOSAccountRemoveRecoveryKey(SOSAccountRef account
, CFErrorRef
*error
);
282 SOSRecoveryKeyBagRef
SOSAccountCopyRecoveryKeyBag(CFAllocatorRef allocator
, SOSAccountRef account
, CFErrorRef
*error
);
283 CFDataRef
SOSAccountCopyRecoveryPublic(CFAllocatorRef allocator
, SOSAccountRef account
, CFErrorRef
*error
);
284 bool SOSAccountRecoveryKeyIsInBackupAndCurrentInView(SOSAccountRef account
, CFStringRef viewname
);
285 bool SOSAccountSetRecoveryKeyBagEntry(CFAllocatorRef allocator
, SOSAccountRef account
, SOSRecoveryKeyBagRef rkbg
, CFErrorRef
*error
);
286 SOSRecoveryKeyBagRef
SOSAccountCopyRecoveryKeyBagEntry(CFAllocatorRef allocator
, SOSAccountRef account
, CFErrorRef
*error
);
287 void SOSAccountEnsureRecoveryRing(SOSAccountRef account
);
290 // MARK: Private functions
293 dispatch_queue_t
SOSAccountGetQueue(SOSAccountRef account
);
295 typedef bool (^SOSAccountSendBlock
)(CFStringRef key
, CFDataRef message
, CFErrorRef
*error
);
298 // MARK: Utility functions
301 CFStringRef
SOSAccountCreateCompactDescription(SOSAccountRef a
);
302 CFStringRef
SOSInterestListCopyDescription(CFArrayRef interests
);
305 // MARK: View Funcitons
308 // Use these to tell the engine what views are common to myPeer and other circle peers
309 CFArrayRef
SOSCreateActiveViewIntersectionArrayForPeerID(SOSAccountRef account
, CFStringRef peerID
);
310 CFDictionaryRef
SOSViewsCreateActiveViewMatrixDictionary(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
*error
);
312 const uint8_t* der_decode_cloud_parameters(CFAllocatorRef allocator
,
313 CFIndex algorithmID
, SecKeyRef
* publicKey
,
314 CFDataRef
*parameters
,
316 const uint8_t* der
, const uint8_t* der_end
);
318 /* CFSet <-> XPC functions */
319 CFSetRef
CreateCFSetRefFromXPCObject(xpc_object_t xpcSetDER
, CFErrorRef
* error
);
320 xpc_object_t
CreateXPCObjectWithCFSetRef(CFSetRef setref
, CFErrorRef
*error
);
324 // MARK: HSA2 Piggyback Support Functions
327 SOSPeerInfoRef
SOSAccountCopyApplication(SOSAccountRef account
, CFErrorRef
*);
328 CFDataRef
SOSAccountCopyCircleJoiningBlob(SOSAccountRef account
, SOSPeerInfoRef applicant
, CFErrorRef
*error
);
329 bool SOSAccountJoinWithCircleJoiningBlob(SOSAccountRef account
, CFDataRef joiningBlob
, CFErrorRef
*error
);
332 // MARK: Initial-Sync
334 bool SOSAccountHasCompletedInitialSync(SOSAccountRef account
);
335 CFMutableSetRef
SOSAccountCopyUnsyncedInitialViews(SOSAccountRef account
);
336 bool SOSAccountHasCompletedRequiredBackupSync(SOSAccountRef account
);
339 // MARK: State Logging
341 void SOSAccountLogState(SOSAccountRef account
);
342 void SOSAccountLogViewState(SOSAccountRef account
);
345 // MARK: Checking other peer views
348 CFBooleanRef
SOSAccountPeersHaveViewsEnabled(SOSAccountRef account
, CFArrayRef viewNames
, CFErrorRef
*error
);
350 void SOSAccountSetTestSerialNumber(SOSAccountRef account
, CFStringRef serial
);
354 // MARK: Syncing status functions
356 bool SOSAccountMessageFromPeerIsPending(SOSAccountTransactionRef txn
, SOSPeerInfoRef peer
, CFErrorRef
*error
);
357 bool SOSAccountSendToPeerIsPending(SOSAccountTransactionRef txn
, SOSPeerInfoRef peer
, CFErrorRef
*error
);
361 #endif /* !_SOSACCOUNT_H_ */