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 #include <CoreFoundation/CoreFoundation.h>
35 #include <Foundation/Foundation.h>
36 #include "keychain/SecureObjectSync/SOSAccountPriv.h"
37 #include "keychain/SecureObjectSync/SOSCircle.h"
38 #include "keychain/SecureObjectSync/SOSFullPeerInfo.h"
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
41 #include "keychain/SecureObjectSync/SOSTransportCircle.h"
42 #include "keychain/SecureObjectSync/SOSRing.h"
43 #include "keychain/SecureObjectSync/SOSRecoveryKeyBag.h"
44 #import "keychain/SecureObjectSync/SOSAccountTransaction.h"
45 #include <dispatch/dispatch.h>
47 extern NSString
* const kSOSIdentityStatusCompleteIdentity
;
48 extern NSString
* const kSOSIdentityStatusKeyOnly
;
49 extern NSString
* const kSOSIdentityStatusPeerOnly
;
55 #define RETIREMENT_FINALIZATION_SECONDS (24*60*60)
57 typedef void (^SOSAccountCircleMembershipChangeBlock
)(SOSAccount
* account
,
58 SOSCircleRef new_circle
,
59 CFSetRef added_peers
, CFSetRef removed_peers
,
60 CFSetRef added_applicants
, CFSetRef removed_applicants
);
62 CFTypeID
SOSAccountGetTypeID(void);
64 SOSAccount
* SOSAccountCreate(CFAllocatorRef allocator
,
65 CFDictionaryRef gestalt
,
66 SOSDataSourceFactoryRef factory
);
69 // MARK: Credential management
72 SecKeyRef
SOSAccountGetTrustedPublicCredential(SOSAccount
* account
, CFErrorRef
* error
);
74 SecKeyRef
SOSAccountGetPrivateCredential(SOSAccount
* account
, CFErrorRef
* error
);
75 CFDataRef
SOSAccountGetCachedPassword(SOSAccount
* account
, CFErrorRef
* error
);
76 void SOSAccountStashAccountKey(SOSAccount
* account
);
77 SecKeyRef
SOSAccountCopyStashedUserPrivateKey(SOSAccount
* account
, CFErrorRef
*error
);
79 void SOSAccountSetParameters(SOSAccount
* account
, CFDataRef parameters
);
81 void SOSAccountPurgePrivateCredential(SOSAccount
* account
);
83 void SOSAccountRestartPrivateCredentialTimer(SOSAccount
* account
);
85 bool SOSAccountTryUserCredentials(SOSAccount
* account
,
86 CFStringRef user_account
, CFDataRef user_password
,
89 bool SOSAccountTryUserPrivateKey(SOSAccount
* account
, SecKeyRef user_private
, CFErrorRef
*error
);
91 bool SOSAccountValidateAccountCredential(SOSAccount
* account
, SecKeyRef accountPrivateKey
, CFErrorRef
*error
);
92 bool SOSAccountAssertStashedAccountCredential(SOSAccount
* account
, CFErrorRef
*error
);
93 bool SOSAccountAssertUserCredentials(SOSAccount
* account
,
94 CFStringRef user_account
, CFDataRef user_password
,
97 bool SOSAccountRetryUserCredentials(SOSAccount
* account
);
98 void SOSAccountSetUnTrustedUserPublicKey(SOSAccount
* account
, SecKeyRef publicKey
);
100 bool SOSAccountGenerationSignatureUpdate(SOSAccount
* account
, CFErrorRef
*error
);
103 // MARK: Circle management
106 bool SOSAccountUpdateCircle(SOSAccount
* account
, SOSCircleRef circle
, CFErrorRef
*error
);
107 void SOSTransportEachMessage(SOSAccount
* account
, CFDictionaryRef updates
, CFErrorRef
*error
);
110 CFStringRef
SOSAccountGetSOSCCStatusString(SOSCCStatus status
);
111 SOSCCStatus
SOSAccountGetSOSCCStatusFromString(CFStringRef status
);
112 bool SOSAccountJoinCircles(SOSAccountTransaction
* aTxn
, CFErrorRef
* error
);
113 bool SOSAccountJoinCirclesAfterRestore(SOSAccountTransaction
* aTxn
, CFErrorRef
* error
);
114 bool SOSAccountRemovePeersFromCircle(SOSAccount
* account
, CFArrayRef peers
, CFErrorRef
* error
);
115 bool SOSAccountBail(SOSAccount
* account
, uint64_t limit_in_seconds
, CFErrorRef
* error
);
116 bool SOSAccountAcceptApplicants(SOSAccount
* account
, CFArrayRef applicants
, CFErrorRef
* error
);
117 bool SOSAccountRejectApplicants(SOSAccount
* account
, CFArrayRef applicants
, CFErrorRef
* error
);
119 bool SOSValidateUserPublic(SOSAccount
* account
, CFErrorRef
* error
);
121 void SOSAccountForEachCirclePeerExceptMe(SOSAccount
* account
, void (^action
)(SOSPeerInfoRef peer
));
123 CFArrayRef
SOSAccountCopyApplicants(SOSAccount
* account
, CFErrorRef
*error
);
124 CFArrayRef
SOSAccountCopyGeneration(SOSAccount
* account
, CFErrorRef
*error
);
125 CFArrayRef
SOSAccountCopyValidPeers(SOSAccount
* account
, CFErrorRef
*error
);
126 CFArrayRef
SOSAccountCopyPeersToListenTo(SOSAccount
* account
, CFErrorRef
*error
);
127 CFArrayRef
SOSAccountCopyNotValidPeers(SOSAccount
* account
, CFErrorRef
*error
);
128 CFArrayRef
SOSAccountCopyRetired(SOSAccount
* account
, CFErrorRef
*error
);
129 CFArrayRef
SOSAccountCopyViewUnaware(SOSAccount
* account
, CFErrorRef
*error
);
130 CFArrayRef
SOSAccountCopyPeers(SOSAccount
* account
, CFErrorRef
*error
);
131 CFArrayRef
SOSAccountCopyActivePeers(SOSAccount
* account
, CFErrorRef
*error
);
132 CFArrayRef CF_RETURNS_RETAINED
SOSAccountCopyActiveValidPeers(SOSAccount
* account
, CFErrorRef
*error
);
133 CFArrayRef
SOSAccountCopyConcurringPeers(SOSAccount
* account
, CFErrorRef
*error
);
135 bool SOSAccountIsAccountIdentity(SOSAccount
* account
, SOSPeerInfoRef peer_info
, CFErrorRef
*error
);
137 enum DepartureReason
SOSAccountGetLastDepartureReason(SOSAccount
* account
, CFErrorRef
* error
);
140 // MARK: iCloud Identity
142 bool SOSAccountRemoveIncompleteiCloudIdentities(SOSAccount
* account
, SOSCircleRef circle
, SecKeyRef privKey
, CFErrorRef
*error
);
145 // MARK: Change blocks
147 void SOSAccountAddChangeBlock(SOSAccount
* a
, SOSAccountCircleMembershipChangeBlock changeBlock
);
148 void SOSAccountRemoveChangeBlock(SOSAccount
* a
, SOSAccountCircleMembershipChangeBlock changeBlock
);
152 // MARK: Local device gestalt change.
154 CFDictionaryRef
SOSAccountCopyGestalt(SOSAccount
* account
);
156 CFDictionaryRef
SOSAccountCopyV2Dictionary(SOSAccount
* account
);
158 void SOSAccountPendDisableViewSet(SOSAccount
* account
, CFSetRef disabledViews
);
160 void SOSAccountUpdateOutOfSyncViews(SOSAccountTransaction
* aTxn
, CFSetRef viewsInSync
);
161 void SOSAccountPeerGotInSync(SOSAccountTransaction
* aTxn
, CFStringRef peerID
, CFSetRef views
);
163 bool SOSAccountHandleParametersChange(SOSAccount
* account
, CFDataRef updates
, CFErrorRef
*error
);
166 // MARK: Local device key access from account object - can call without lock without endangering peerinfo.
168 SecKeyRef
SOSAccountCopyDevicePrivateKey(SOSAccount
* account
, CFErrorRef
*error
);
169 SecKeyRef
SOSAccountCopyDevicePublicKey(SOSAccount
* account
, CFErrorRef
*error
);
172 // MARK: Requests for syncing later
174 bool SOSAccountRequestSyncWithAllPeers(SOSAccountTransaction
* txn
, CFErrorRef
*error
);
175 CF_RETURNS_RETAINED CFMutableSetRef
SOSAccountSyncWithPeers(SOSAccountTransaction
* txn
, CFSetRef
/* CFStringRef */ peerIDs
, CFErrorRef
*error
);
176 CFSetRef
SOSAccountSyncWithPeersOverKVS(SOSAccountTransaction
* txn
, CFSetRef peers
);
177 bool SOSAccountInflateTransports(SOSAccount
* account
, CFStringRef circleName
, CFErrorRef
*error
);
180 SOSAccountTriggerSyncWithBackupPeer(CFStringRef peer
);
183 // MARK: Outgoing/Sync functions
186 bool SOSAccountSyncWithKVSPeerWithMessage(SOSAccountTransaction
* txn
, CFStringRef peerid
, CFDataRef message
, CFErrorRef
*error
);
188 CF_RETURNS_RETAINED CFSetRef
SOSAccountProcessSyncWithPeers(SOSAccountTransaction
* txn
, CFSetRef
/* CFStringRef */ peers
, CFSetRef
/* CFStringRef */ backupPeers
, CFErrorRef
*error
);
189 CF_RETURNS_RETAINED CFSetRef
SOSAccountCopyBackupPeersAndForceSync(SOSAccountTransaction
* txn
, CFErrorRef
*error
);
192 // MARK: Cleanup functions
195 bool SOSAccountScanForRetired(SOSAccount
* account
, SOSCircleRef circle
, CFErrorRef
*error
);
196 CF_RETURNS_RETAINED SOSCircleRef
SOSAccountCloneCircleWithRetirement(SOSAccount
* account
, SOSCircleRef starting_circle
, CFErrorRef
*error
);
199 // MARK: Backup functions
202 bool SOSAccountIsBackupRingEmpty(SOSAccount
* account
, CFStringRef viewName
);
203 bool SOSAccountNewBKSBForView(SOSAccount
* account
, CFStringRef viewName
, CFErrorRef
*error
);
205 void SOSAccountProcessBackupRings(SOSAccount
* account
);
206 bool SOSAccountValidateBackupRingForView(SOSAccount
* account
, CFStringRef viewName
, CFErrorRef
*error
);
207 bool SOSAccountSetBackupPublicKey(SOSAccountTransaction
* aTxn
, CFDataRef backupKey
, CFErrorRef
*error
);
208 bool SOSAccountRemoveBackupPublickey(SOSAccountTransaction
* aTxn
, CFErrorRef
*error
);
209 bool SOSAccountBackupUpdateBackupPublicKey(SOSAccount
*account
, CFDataRef backupKey
);
210 bool SOSAccountSetBSKBagForAllSlices(SOSAccount
* account
, CFDataRef backupSlice
, bool setupV0Only
, CFErrorRef
*error
);
212 CF_RETURNS_RETAINED SOSBackupSliceKeyBagRef
SOSAccountBackupSliceKeyBagForView(SOSAccount
* account
, CFStringRef viewName
, CFErrorRef
* error
);
215 // MARK: Recovery Public Key Functions
217 bool SOSAccountRegisterRecoveryPublicKey(SOSAccountTransaction
* txn
, CFDataRef recovery_key
, CFErrorRef
*error
);
218 CFDataRef
SOSAccountCopyRecoveryPublicKey(SOSAccountTransaction
* txn
, CFErrorRef
*error
);
219 bool SOSAccountClearRecoveryPublicKey(SOSAccountTransaction
* txn
, CFDataRef recovery_key
, CFErrorRef
*error
);
222 // Internal calls that sets or clears Recovery Keys for the Account Object Provided by Clients
223 bool SOSAccountSetRecoveryKey(SOSAccount
* account
, CFDataRef pubData
, CFErrorRef
*error
);
224 bool SOSAccountRemoveRecoveryKey(SOSAccount
* account
, CFErrorRef
*error
);
227 CFDataRef
SOSAccountCopyRecoveryPublic(CFAllocatorRef allocator
, SOSAccount
* account
, CFErrorRef
*error
);
228 bool SOSAccountRecoveryKeyIsInBackupAndCurrentInView(SOSAccount
* account
, CFStringRef viewname
);
229 bool SOSAccountSetRecoveryKeyBagEntry(CFAllocatorRef allocator
, SOSAccount
* account
, SOSRecoveryKeyBagRef rkbg
, CFErrorRef
*error
);
230 SOSRecoveryKeyBagRef
SOSAccountCopyRecoveryKeyBagEntry(CFAllocatorRef allocator
, SOSAccount
* account
, CFErrorRef
*error
);
231 void SOSAccountEnsureRecoveryRing(SOSAccount
* account
);
234 // MARK: Private functions
237 dispatch_queue_t
SOSAccountGetQueue(SOSAccount
* account
);
239 typedef bool (^SOSAccountSendBlock
)(CFStringRef key
, CFDataRef message
, CFErrorRef
*error
);
242 // MARK: Utility functions
245 CFStringRef
SOSInterestListCopyDescription(CFArrayRef interests
);
248 // MARK: HSA2 Piggyback Support Functions
250 SOSPeerInfoRef
SOSAccountCopyApplication(SOSAccount
* account
, CFErrorRef
*);
251 CFDataRef
SOSAccountCopyCircleJoiningBlob(SOSAccount
* account
, SOSPeerInfoRef applicant
, CFErrorRef
*error
);
252 bool SOSAccountJoinWithCircleJoiningBlob(SOSAccount
* account
, CFDataRef joiningBlob
, PiggyBackProtocolVersion version
, CFErrorRef
*error
);
253 CFDataRef
SOSAccountCopyInitialSyncData(SOSAccount
* account
, SOSInitialSyncFlags flags
, CFErrorRef
*error
);
256 // MARK: Initial-Sync
258 CFMutableSetRef
SOSAccountCopyUnsyncedInitialViews(SOSAccount
* account
);
261 // MARK: State Logging
263 void SOSAccountLogState(SOSAccount
* account
);
264 void SOSAccountLogViewState(SOSAccount
* account
);
265 void SOSAccountConsiderLoggingEngineState(SOSAccountTransaction
* txn
);
268 // MARK: Checking other peer views
271 CFBooleanRef
SOSAccountPeersHaveViewsEnabled(SOSAccount
* account
, CFArrayRef viewNames
, CFErrorRef
*error
);
273 void SOSAccountSetTestSerialNumber(SOSAccount
* account
, CFStringRef serial
);
274 SOSViewResultCode
SOSAccountVirtualV0Behavior(SOSAccount
* account
, SOSViewActionCode actionCode
);
277 bool SOSAccountIsPeerRetired(SOSAccount
* account
, CFSetRef peers
);
278 void SOSAccountNotifyOfChange(SOSAccount
* account
, SOSCircleRef oldCircle
, SOSCircleRef newCircle
);
282 // MARK: Syncing status functions
284 bool SOSAccountMessageFromPeerIsPending(SOSAccountTransaction
* txn
, SOSPeerInfoRef peer
, CFErrorRef
*error
);
285 bool SOSAccountSendToPeerIsPending(SOSAccountTransaction
* txn
, SOSPeerInfoRef peer
, CFErrorRef
*error
);
290 void SOSAccountResetOTRNegotiationCoder(SOSAccount
* account
, CFStringRef peerid
);
291 void SOSAccountTimerFiredSendNextMessage(SOSAccountTransaction
* txn
, NSString
* peerid
, NSString
* accessGroup
);
293 NSArray
<NSDictionary
*>* SOSAccountGetAllTLKs(void);
294 NSArray
<NSDictionary
*>* SOSAccountGetSelectedTLKs(void);
296 CF_RETURNS_RETAINED CFMutableArrayRef
SOSAccountCopyiCloudIdentities(SOSAccount
* account
);
298 bool SOSAccountEvaluateKeysAndCircle(SOSAccountTransaction
*txn
, CFErrorRef
*block_error
);
302 #endif /* !_SOSACCOUNT_H_ */