6 #ifndef SOSAccountPriv_h
7 #define SOSAccountPriv_h
9 #import <Foundation/Foundation.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>
17 #include <Security/SecKeyPriv.h>
19 #include <Security/der_plist.h>
20 #include <utilities/der_plist_internal.h>
21 #include <corecrypto/ccder.h>
23 #include <AssertMacros.h>
28 #include "keychain/SecureObjectSync/SOSInternal.h"
30 #include "keychain/SecureObjectSync/SOSCircle.h"
31 #include "keychain/SecureObjectSync/SOSCircleV2.h"
32 #include "keychain/SecureObjectSync/SOSRing.h"
33 #include "keychain/SecureObjectSync/SOSRingUtils.h"
34 #include <Security/SecureObjectSync/SOSCloudCircle.h>
35 #include "keychain/securityd/SOSCloudCircleServer.h"
36 #include "keychain/SecureObjectSync/SOSEngine.h"
37 #include "keychain/SecureObjectSync/SOSPeer.h"
38 #include "keychain/SecureObjectSync/SOSFullPeerInfo.h"
39 #include <Security/SecureObjectSync/SOSPeerInfo.h>
41 #include "keychain/SecureObjectSync/SOSPeerInfoInternal.h"
42 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
43 #include "keychain/SecureObjectSync/SOSTransportCircle.h"
45 #include <utilities/iCloudKeychainTrace.h>
47 #include <Security/SecItemPriv.h>
50 extern const CFStringRef kSOSUnsyncedViewsKey
;
51 extern const CFStringRef kSOSPendingEnableViewsToBeSetKey
;
52 extern const CFStringRef kSOSPendingDisableViewsToBeSetKey
;
53 extern const CFStringRef kSOSRecoveryKey
;
54 extern const CFStringRef kSOSAccountUUID
;
55 extern const CFStringRef kSOSAccountPeerNegotiationTimeouts
;
56 extern const CFStringRef kSOSRecoveryRing
;
57 extern const CFStringRef kSOSEscrowRecord
;
58 extern const CFStringRef kSOSAccountName
;
59 extern const CFStringRef kSOSTestV2Settings
;
60 extern const CFStringRef kSOSRateLimitingCounters
;
61 extern const CFStringRef kSOSAccountPeerLastSentTimestamp
;
62 extern const CFStringRef kSOSAccountRenegotiationRetryCount
;
63 extern const CFStringRef kSOSInitialSyncTimeoutV0
;
65 typedef void (^SOSAccountSaveBlock
)(CFDataRef flattenedAccount
, CFErrorRef flattenFailError
);
68 @
class CKKeyParameter
;
69 @
class SOSAccountTrustClassic
;
70 @
class SOSKVSCircleStorageTransport
;
71 @
class SOSCircleStorageTransport
;
72 @
class SOSCKCircleStorage
;
74 @interface SOSAccount
: NSObject
<SOSControlProtocol
>
76 @
property (nonatomic
, retain
) NSDictionary
*gestalt
;
77 @
property (nonatomic
, retain
) NSData
*backup_key
;
78 @
property (nonatomic
, retain
) NSString
*deviceID
;
80 @
property (nonatomic
, retain
) SOSAccountTrustClassic
*trust
;
82 @
property (nonatomic
, retain
) dispatch_queue_t queue
;
83 @
property (nonatomic
, retain
) dispatch_source_t user_private_timer
;
84 @
property (nonatomic
) SecKeyRef accountPrivateKey
;
86 @
property (nonatomic
) SOSDataSourceFactoryRef factory
;
88 @
property (nonatomic
, retain
) NSData
*_password_tmp
;
89 @
property (nonatomic
, assign
) BOOL isListeningForSync
;
90 @
property (nonatomic
, assign
) int lock_notification_token
;
91 @
property (nonatomic
, retain
) CKKeyParameter
* key_transport
;
92 @
property (nonatomic
, retain
) SOSKVSCircleStorageTransport
* circle_transport
;
93 @
property (nonatomic
, retain
) SOSMessageKVS
* kvs_message_transport
;
94 @
property (nonatomic
, retain
) SOSCKCircleStorage
* ck_storage
;
97 @
property (nonatomic
, assign
) BOOL circle_rings_retirements_need_attention
;
98 @
property (nonatomic
, assign
) BOOL engine_peer_state_needs_repair
;
99 @
property (nonatomic
, assign
) BOOL key_interests_need_updating
;
101 @
property (nonatomic
, retain
) NSMutableArray
*change_blocks
;
103 @
property (nonatomic
, retain
) NSMutableDictionary
*waitForInitialSync_blocks
;
105 @
property (nonatomic
, retain
) NSData
* accountKeyDerivationParamters
;
107 @
property (nonatomic
, assign
) BOOL accountKeyIsTrusted
;
108 @
property (nonatomic
) SecKeyRef accountKey
;
109 @
property (nonatomic
) SecKeyRef previousAccountKey
;
111 @
property (copy
) SOSAccountSaveBlock saveBlock
;
114 // Identity access properties, all delegated to the trust object
115 @
property (readonly
, nonatomic
) BOOL hasPeerInfo
;
116 @
property (readonly
, nonatomic
) SOSPeerInfoRef peerInfo
;
117 @
property (readonly
, nonatomic
) SOSFullPeerInfoRef fullPeerInfo
;
118 @
property (readonly
, nonatomic
) NSString
* peerID
;
120 @
property (nonatomic
, assign
) BOOL notifyCircleChangeOnExit
;
121 @
property (nonatomic
, assign
) BOOL notifyViewChangeOnExit
;
122 @
property (nonatomic
, assign
) BOOL notifyBackupOnExit
;
124 @
property (nonatomic
, retain
) NSUserDefaults
* settings
;
128 -(id
) init NS_UNAVAILABLE
;
129 -(id
) initWithGestalt
:(CFDictionaryRef
)gestalt factory
:(SOSDataSourceFactoryRef
)factory
;
131 //- (void)startStateMachine;
133 void SOSAccountAddSyncablePeerBlock(SOSAccount
* a
,
135 SOSAccountSyncablePeersBlock changeBlock
);
137 -(bool) ensureFactoryCircles
;
138 -(void) ensureOctagonPeerKeys
;
140 -(void) flattenToSaveBlock
;
142 -(void) ghostBustSchedule
;
143 + (SOSAccountGhostBustingOptions
) ghostBustGetRampSettings
;
144 - (bool) ghostBustCheckDate
;
147 - (void)triggerBackupForPeers
:(NSArray
<NSString
*>*)backupPeer
;
151 void SOSAccountSetToNew(SOSAccount
* a
);
153 bool SOSAccountIsMyPeerActive(SOSAccount
* account
, CFErrorRef
* error
);
155 // MARK: In Sync checking
156 typedef bool (^SOSAccountWaitForInitialSyncBlock
)(SOSAccount
* account
);
158 CF_RETURNS_RETAINED CFStringRef
SOSAccountCallWhenInSync(SOSAccount
* account
, SOSAccountWaitForInitialSyncBlock syncBlock
);
159 bool SOSAccountUnregisterCallWhenInSync(SOSAccount
* account
, CFStringRef id
);
161 bool SOSAccountHandleOutOfSyncUpdate(SOSAccount
* account
, CFSetRef oldOOSViews
, CFSetRef newOOSViews
);
163 void SOSAccountEnsureSyncChecking(SOSAccount
* account
);
164 void SOSAccountCancelSyncChecking(SOSAccount
* account
);
165 void SOSAccountInitializeInitialSync(SOSAccount
* account
);
166 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
167 CFSetRef
SOSAccountCopyEnabledViews(SOSAccount
* account
);
168 void SOSAccountNotifyEngines(SOSAccount
* account
);
169 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
170 bool SOSAccountIsViewOutstanding(SOSAccount
* account
, CFStringRef view
);
171 CFMutableSetRef
SOSAccountCopyIntersectionWithOustanding(SOSAccount
* account
, CFSetRef inSet
);
172 bool SOSAccountIntersectsWithOutstanding(SOSAccount
* account
, CFSetRef views
);
173 bool SOSAccountHasOustandingViews(SOSAccount
* account
);
174 bool SOSAccountHasCompletedInitialSync(SOSAccount
* account
);
175 bool SOSAccountHasCompletedRequiredBackupSync(SOSAccount
* account
);
176 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
177 bool SOSAccountSyncingV0(SOSAccount
* account
);
182 size_t der_sizeof_fullpeer_or_null(SOSFullPeerInfoRef data
, CFErrorRef
* error
);
184 uint8_t* der_encode_fullpeer_or_null(SOSFullPeerInfoRef data
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
186 const uint8_t* der_decode_fullpeer_or_null(CFAllocatorRef allocator
, SOSFullPeerInfoRef
* data
,
188 const uint8_t* der
, const uint8_t* der_end
);
191 size_t der_sizeof_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
);
193 uint8_t* der_encode_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
195 const uint8_t* der_decode_public_bytes(CFAllocatorRef allocator
, CFIndex algorithmID
, SecKeyRef
* publicKey
, CFErrorRef
* error
, const uint8_t* der
, const uint8_t* der_end
);
199 -(SOSCCStatus
) getCircleStatus
:(CFErrorRef
*) error
;
200 -(bool) isInCircle
:(CFErrorRef
*)error
;
202 bool SOSAccountHandleCircleMessage(SOSAccount
* account
,
203 CFStringRef circleName
, CFDataRef encodedCircleMessage
, CFErrorRef
*error
);
206 CFDictionaryRef
SOSAccountHandleRetirementMessages(SOSAccount
* account
, CFDictionaryRef circle_retirement_messages
, CFErrorRef
*error
);
208 void SOSAccountRecordRetiredPeersInCircle(SOSAccount
* account
);
210 bool SOSAccountHandleUpdateCircle(SOSAccount
* account
,
211 SOSCircleRef prospective_circle
,
217 bool SOSAccountHasFullPeerInfo(SOSAccount
* account
, CFErrorRef
* error
);
219 bool SOSAccountIsMyPeerInBackupAndCurrentInView(SOSAccount
* account
, CFStringRef viewname
);
220 bool SOSAccountUpdateOurPeerInBackup(SOSAccount
* account
, SOSRingRef oldRing
, CFErrorRef
*error
);
221 bool SOSAccountIsPeerInBackupAndCurrentInView(SOSAccount
* account
, SOSPeerInfoRef testPeer
, CFStringRef viewname
);
222 bool SOSDeleteV0Keybag(CFErrorRef
*error
);
223 bool SOSAccountUpdatePeerInfo(SOSAccount
* account
, CFStringRef updateDescription
, CFErrorRef
*error
, bool (^update
)(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
));
224 bool SOSAccountUpdatePeerInfoAndPush(SOSAccount
* account
, CFStringRef updateDescription
, CFErrorRef
*error
,
225 bool (^update
)(SOSPeerInfoRef pi
, CFErrorRef
*error
));
227 // Currently permitted backup rings.
228 void SOSAccountForEachBackupRingName(SOSAccount
* account
, void (^operation
)(CFStringRef value
));
229 void SOSAccountForEachRingName(SOSAccount
* account
, void (^operation
)(CFStringRef value
));
230 void SOSAccountForEachBackupView(SOSAccount
* account
, void (^operation
)(const void *value
));
231 SOSRingRef
SOSAccountCreateBackupRingForView(SOSAccount
* account
, CFStringRef ringBackupViewName
, CFErrorRef
*error
);
235 bool SOSAccountHasCircle(SOSAccount
* account
, CFErrorRef
* error
);
236 SOSCircleRef CF_RETURNS_RETAINED
SOSAccountEnsureCircle(SOSAccount
* a
, CFStringRef name
, CFErrorRef
*error
);
238 void AppendCircleKeyName(CFMutableArrayRef array
, CFStringRef name
);
240 CFStringRef
SOSInterestListCopyDescription(CFArrayRef interests
);
243 // FullPeerInfos - including Cloud Identity
244 SOSFullPeerInfoRef
CopyCloudKeychainIdentity(SOSPeerInfoRef cloudPeer
, CFErrorRef
*error
);
246 bool SOSAccountIsAccountIdentity(SOSAccount
* account
, SOSPeerInfoRef peer_info
, CFErrorRef
*error
);
247 bool SOSAccountFullPeerInfoVerify(SOSAccount
* account
, SecKeyRef privKey
, CFErrorRef
*error
);
248 CF_RETURNS_RETAINED SOSPeerInfoRef
GenerateNewCloudIdentityPeerInfo(CFErrorRef
*error
);
251 bool SOSAccountHasPublicKey(SOSAccount
* account
, CFErrorRef
* error
);
252 bool SOSAccountPublishCloudParameters(SOSAccount
* account
, CFErrorRef
* error
);
253 bool SOSAccountRetrieveCloudParameters(SOSAccount
* account
, SecKeyRef
*newKey
,
255 CFDataRef
*newParameters
, CFErrorRef
* error
);
258 void SOSAccountAssertDSID(SOSAccount
* account
, CFStringRef dsid
);
264 SecKeyRef
SOSAccountCopyDeviceKey(SOSAccount
* account
, CFErrorRef
*error
);
265 SecKeyRef CF_RETURNS_RETAINED
GeneratePermanentFullECKey(int keySize
, CFStringRef name
, CFErrorRef
* error
);
268 void SOSAccountSetLastDepartureReason(SOSAccount
* account
, enum DepartureReason reason
);
269 void SOSAccountSetUserPublicTrustedForTesting(SOSAccount
* account
);
271 void SOSAccountPurgeIdentity(SOSAccount
*);
272 bool sosAccountLeaveCircle(SOSAccount
* account
, SOSCircleRef circle
, NSData
* parentData
, CFErrorRef
* error
);
274 bool SOSAccountForEachRing(SOSAccount
* account
, SOSRingRef (^action
)(CFStringRef name
, SOSRingRef ring
));
275 bool SOSAccountUpdateBackUp(SOSAccount
* account
, CFStringRef viewname
, CFErrorRef
*error
);
276 void SOSAccountEnsureRecoveryRing(SOSAccount
* account
);
277 bool SOSAccountEnsureInBackupRings(SOSAccount
* account
);
279 bool SOSAccountEnsurePeerRegistration(SOSAccount
* account
, CFErrorRef
*error
);
281 extern const CFStringRef kSOSUnsyncedViewsKey
;
282 extern const CFStringRef kSOSPendingEnableViewsToBeSetKey
;
283 extern const CFStringRef kSOSPendingDisableViewsToBeSetKey
;
284 extern const CFStringRef kSOSRecoveryKey
;
287 kSOSTransportNone
= 0,
288 kSOSTransportIDS
= 1,
289 kSOSTransportKVS
= 2,
290 kSOSTransportFuture
= 3,
291 kSOSTransportPresent
= 4
294 SOSPeerInfoRef
SOSAccountCopyPeerWithID(SOSAccount
* account
, CFStringRef peerid
, CFErrorRef
*error
);
296 bool SOSAccountSetValue(SOSAccount
* account
, CFStringRef key
, CFTypeRef value
, CFErrorRef
*error
);
297 bool SOSAccountClearValue(SOSAccount
* account
, CFStringRef key
, CFErrorRef
*error
);
298 CFTypeRef
SOSAccountGetValue(SOSAccount
* account
, CFStringRef key
, CFErrorRef
*error
);
300 bool SOSAccountAddEscrowToPeerInfo(SOSAccount
* account
, SOSFullPeerInfoRef myPeer
, CFErrorRef
*error
);
301 void SOSAccountRemoveRing(SOSAccount
* a
, CFStringRef ringName
);
302 SOSRingRef
SOSAccountCopyRingNamed(SOSAccount
* a
, CFStringRef ringName
, CFErrorRef
*error
);
303 bool SOSAccountUpdateRingFromRemote(SOSAccount
* account
, SOSRingRef newRing
, CFErrorRef
*error
);
304 bool SOSAccountUpdateRing(SOSAccount
* account
, SOSRingRef newRing
, CFErrorRef
*error
);
305 bool SOSAccountRemoveBackupPeers(SOSAccount
* account
, CFArrayRef peerIDs
, CFErrorRef
*error
);
306 bool SOSAccountUpdateNamedRing(SOSAccount
* account
, CFStringRef ringName
, CFErrorRef
*error
,
307 SOSRingRef (^create
)(CFStringRef ringName
, CFErrorRef
*error
),
308 SOSRingRef (^copyModified
)(SOSRingRef existing
, CFErrorRef
*error
));
311 // MARK: Backup translation functions
314 CFStringRef
SOSBackupCopyRingNameForView(CFStringRef viewName
);
315 bool SOSAccountUpdateBackupRing(SOSAccount
* account
, CFStringRef viewName
, CFErrorRef
*error
,
316 SOSRingRef (^modify
)(SOSRingRef existing
, CFErrorRef
*error
));
318 // Security tool test/debug functions
320 bool SOSAccountPostDebugScope(SOSAccount
* account
, CFTypeRef scope
, CFErrorRef
*error
);
322 bool SOSAccountCheckForAlwaysOnViews(SOSAccount
* account
);
323 // UUID, no setter just getter and ensuring value.
324 void SOSAccountEnsureUUID(SOSAccount
* account
);
325 CFStringRef CF_RETURNS_RETAINED
SOSAccountCopyUUID(SOSAccount
* account
);
326 const uint8_t* der_decode_cloud_parameters(CFAllocatorRef allocator
,
327 CFIndex algorithmID
, SecKeyRef
* publicKey
,
328 CFDataRef
*parameters
,
330 const uint8_t* der
, const uint8_t* der_end
);
336 CFDataRef
SOSPiggyBackBlobCopyEncodedData(SOSGenCountRef gencount
, SecKeyRef pubKey
, CFDataRef signature
, CFErrorRef
*error
);
339 NSData
*SOSPiggyCreateInitialSyncData(NSArray
<NSData
*> *identities
, NSArray
<NSDictionary
*>* tlks
);
340 NSDictionary
* SOSPiggyCopyInitialSyncData(const uint8_t** der
, const uint8_t *der_end
);
341 NSArray
<NSDictionary
*>* SOSAccountSortTLKS(NSArray
<NSDictionary
*>* tlks
);
344 bool SOSAccountCleanupAllKVSKeys(SOSAccount
* account
, CFErrorRef
* error
);
348 @interface
SOSAccount (Persistence
)
350 +(instancetype
) accountFromData
: (NSData
*) data
351 factory
: (SOSDataSourceFactoryRef
) factory
352 error
: (NSError
**) error
;
353 +(instancetype
) accountFromDER
: (const uint8_t**) der
354 end
: (const uint8_t*) der_end
355 factory
: (SOSDataSourceFactoryRef
) factory
356 error
: (NSError
**) error
;
358 -(NSData
*) encodedData
: (NSError
**) error
;
363 #endif /* SOSAccount_h */