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 <utilities/der_plist.h>
20 #include <utilities/der_plist_internal.h>
21 #include <corecrypto/ccder.h>
23 #include <AssertMacros.h>
28 #include <Security/SecureObjectSync/SOSInternal.h>
30 #include <Security/SecureObjectSync/SOSCircle.h>
31 #include <Security/SecureObjectSync/SOSCircleV2.h>
32 #include <Security/SecureObjectSync/SOSRing.h>
33 #include <Security/SecureObjectSync/SOSRingUtils.h>
34 #include <Security/SecureObjectSync/SOSCloudCircle.h>
35 #include <securityd/SOSCloudCircleServer.h>
36 #include <Security/SecureObjectSync/SOSEngine.h>
37 #include <Security/SecureObjectSync/SOSPeer.h>
38 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
39 #include <Security/SecureObjectSync/SOSPeerInfo.h>
40 #include <Security/SecureObjectSync/SOSPeerInfoInternal.h>
41 #include <Security/SecureObjectSync/SOSUserKeygen.h>
42 #include <Security/SecureObjectSync/SOSTransportCircle.h>
44 #include <utilities/iCloudKeychainTrace.h>
46 #include <Security/SecItemPriv.h>
49 extern const CFStringRef kSOSUnsyncedViewsKey
;
50 extern const CFStringRef kSOSPendingEnableViewsToBeSetKey
;
51 extern const CFStringRef kSOSPendingDisableViewsToBeSetKey
;
52 extern const CFStringRef kSOSRecoveryKey
;
53 extern const CFStringRef kSOSAccountUUID
;
54 extern const CFStringRef kSOSAccountPeerNegotiationTimeouts
;
55 extern const CFStringRef kSOSRecoveryRing
;
56 extern const CFStringRef kSOSEscrowRecord
;
57 extern const CFStringRef kSOSTestV2Settings
;
58 extern const CFStringRef kSOSRateLimitingCounters
;
59 extern const CFStringRef kSOSAccountPeerLastSentTimestamp
;
60 extern const CFStringRef kSOSAccountRenegotiationRetryCount
;
61 extern const CFStringRef kOTRConfigVersion
;
62 extern const CFStringRef kSOSInitialSyncTimeoutV0
;
64 #define kSecServerPeerInfoAvailable "com.apple.security.fpiAvailable"
66 typedef void (^SOSAccountSaveBlock
)(CFDataRef flattenedAccount
, CFErrorRef flattenFailError
);
70 @
class CKKeyParameter
;
71 @
class SOSAccountTrustClassic
;
72 @
class SOSKVSCircleStorageTransport
;
73 @
class SOSCircleStorageTransport
;
74 @
class SOSCKCircleStorage
;
76 @interface SOSAccount
: NSObject
78 @
property (nonatomic
, retain
) NSDictionary
*gestalt
;
79 @
property (nonatomic
, retain
) NSData
*backup_key
;
80 @
property (nonatomic
, retain
) NSString
*deviceID
;
82 @
property (nonatomic
, retain
) SOSAccountTrustClassic
*trust
;
84 @
property (nonatomic
, retain
) dispatch_queue_t queue
;
85 @
property (nonatomic
, retain
) dispatch_source_t user_private_timer
;
86 @
property (nonatomic
) SecKeyRef accountPrivateKey
;
88 @
property (nonatomic
) SOSDataSourceFactoryRef factory
;
90 @
property (nonatomic
, retain
) NSData
*_password_tmp
;
91 @
property (nonatomic
, assign
) BOOL isListeningForSync
;
92 @
property (nonatomic
, assign
) int lock_notification_token
;
93 @
property (nonatomic
, retain
) CKKeyParameter
* key_transport
;
94 @
property (nonatomic
) SOSKVSCircleStorageTransport
* circle_transport
;
95 @
property (nonatomic
, retain
) SOSMessageKVS
* kvs_message_transport
;
96 @
property (nonatomic
, retain
) SOSMessageIDS
* ids_message_transport
;
97 @
property (nonatomic
, retain
) SOSCKCircleStorage
* ck_storage
;
100 @
property (nonatomic
, assign
) BOOL circle_rings_retirements_need_attention
;
101 @
property (nonatomic
, assign
) BOOL engine_peer_state_needs_repair
;
102 @
property (nonatomic
, assign
) BOOL key_interests_need_updating
;
104 @
property (nonatomic
, retain
) NSMutableArray
*change_blocks
;
106 @
property (nonatomic
, retain
) NSMutableDictionary
*waitForInitialSync_blocks
;
107 @
property (nonatomic
, assign
) BOOL isInitialSyncing
;
109 @
property (nonatomic
) NSData
* accountKeyDerivationParamters
;
111 @
property (nonatomic
, assign
) BOOL accountKeyIsTrusted
;
112 @
property (nonatomic
) SecKeyRef accountKey
;
113 @
property (nonatomic
) SecKeyRef previousAccountKey
;
115 @
property (copy
) SOSAccountSaveBlock saveBlock
;
118 // Identity access properties, all delegated to the trust object
119 @
property (readonly
, nonatomic
) BOOL hasPeerInfo
;
120 @
property (readonly
, nonatomic
) SOSPeerInfoRef peerInfo
;
121 @
property (readonly
, nonatomic
) SOSFullPeerInfoRef fullPeerInfo
;
122 @
property (readonly
, nonatomic
) NSString
* peerID
;
126 -(id
) initWithGestalt
:(CFDictionaryRef
)gestalt factory
:(SOSDataSourceFactoryRef
)factory
;
127 - (xpc_endpoint_t
)xpcControlEndpoint
;
129 void SOSAccountAddSyncablePeerBlock(SOSAccount
* a
,
131 SOSAccountSyncablePeersBlock changeBlock
);
133 -(bool) ensureFactoryCircles
;
135 -(void) flattenToSaveBlock
;
137 void SOSAccountSetToNew(SOSAccount
* a
);
139 bool SOSAccountIsMyPeerActive(SOSAccount
* account
, CFErrorRef
* error
);
141 // MARK: In Sync checking
142 typedef bool (^SOSAccountWaitForInitialSyncBlock
)(SOSAccount
* account
);
144 CF_RETURNS_RETAINED CFStringRef
SOSAccountCallWhenInSync(SOSAccount
* account
, SOSAccountWaitForInitialSyncBlock syncBlock
);
145 bool SOSAccountUnregisterCallWhenInSync(SOSAccount
* account
, CFStringRef id
);
147 bool SOSAccountHandleOutOfSyncUpdate(SOSAccount
* account
, CFSetRef oldOOSViews
, CFSetRef newOOSViews
);
149 void SOSAccountEnsureSyncChecking(SOSAccount
* account
);
150 void SOSAccountCancelSyncChecking(SOSAccount
* account
);
152 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
153 void SOSAccountNotifyEngines(SOSAccount
* account
);
154 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
155 bool SOSAccountIsViewOutstanding(SOSAccount
* account
, CFStringRef view
);
156 CFMutableSetRef
SOSAccountCopyIntersectionWithOustanding(SOSAccount
* account
, CFSetRef inSet
);
157 bool SOSAccountIntersectsWithOutstanding(SOSAccount
* account
, CFSetRef views
);
158 bool SOSAccountHasOustandingViews(SOSAccount
* account
);
159 bool SOSAccountHasCompletedInitialSync(SOSAccount
* account
);
160 bool SOSAccountHasCompletedRequiredBackupSync(SOSAccount
* account
);
161 CFMutableSetRef
SOSAccountCopyOutstandingViews(SOSAccount
* account
);
162 bool SOSAccountSyncingV0(SOSAccount
* account
);
167 size_t der_sizeof_fullpeer_or_null(SOSFullPeerInfoRef data
, CFErrorRef
* error
);
169 uint8_t* der_encode_fullpeer_or_null(SOSFullPeerInfoRef data
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
171 const uint8_t* der_decode_fullpeer_or_null(CFAllocatorRef allocator
, SOSFullPeerInfoRef
* data
,
173 const uint8_t* der
, const uint8_t* der_end
);
176 size_t der_sizeof_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
);
178 uint8_t* der_encode_public_bytes(SecKeyRef publicKey
, CFErrorRef
* error
, const uint8_t* der
, uint8_t* der_end
);
180 const uint8_t* der_decode_public_bytes(CFAllocatorRef allocator
, CFIndex algorithmID
, SecKeyRef
* publicKey
, CFErrorRef
* error
, const uint8_t* der
, const uint8_t* der_end
);
184 -(SOSCCStatus
) getCircleStatus
:(CFErrorRef
*) error
;
186 bool SOSAccountHandleCircleMessage(SOSAccount
* account
,
187 CFStringRef circleName
, CFDataRef encodedCircleMessage
, CFErrorRef
*error
);
190 CFDictionaryRef
SOSAccountHandleRetirementMessages(SOSAccount
* account
, CFDictionaryRef circle_retirement_messages
, CFErrorRef
*error
);
192 void SOSAccountRecordRetiredPeersInCircle(SOSAccount
* account
);
194 bool SOSAccountHandleUpdateCircle(SOSAccount
* account
,
195 SOSCircleRef prospective_circle
,
201 bool SOSAccountHasFullPeerInfo(SOSAccount
* account
, CFErrorRef
* error
);
203 bool SOSAccountIsMyPeerInBackupAndCurrentInView(SOSAccount
* account
, CFStringRef viewname
);
204 bool SOSAccountUpdateOurPeerInBackup(SOSAccount
* account
, SOSRingRef oldRing
, CFErrorRef
*error
);
205 bool SOSAccountIsPeerInBackupAndCurrentInView(SOSAccount
* account
, SOSPeerInfoRef testPeer
, CFStringRef viewname
);
206 bool SOSDeleteV0Keybag(CFErrorRef
*error
);
207 void SOSAccountForEachBackupView(SOSAccount
* account
, void (^operation
)(const void *value
));
208 bool SOSAccountUpdatePeerInfo(SOSAccount
* account
, CFStringRef updateDescription
, CFErrorRef
*error
, bool (^update
)(SOSFullPeerInfoRef fpi
, CFErrorRef
*error
));
209 CFStringRef
SOSAccountCreateCompactDescription(SOSAccount
* a
);
211 // Currently permitted backup rings.
212 void SOSAccountForEachBackupRingName(SOSAccount
* account
, void (^operation
)(CFStringRef value
));
213 void SOSAccountForEachRingName(SOSAccount
* account
, void (^operation
)(CFStringRef value
));
216 bool SOSAccountHasCircle(SOSAccount
* account
, CFErrorRef
* error
);
217 SOSCircleRef
SOSAccountEnsureCircle(SOSAccount
* a
, CFStringRef name
, CFErrorRef
*error
);
219 void AppendCircleKeyName(CFMutableArrayRef array
, CFStringRef name
);
221 CFStringRef
SOSInterestListCopyDescription(CFArrayRef interests
);
224 // FullPeerInfos - including Cloud Identity
225 SOSFullPeerInfoRef
CopyCloudKeychainIdentity(SOSPeerInfoRef cloudPeer
, CFErrorRef
*error
);
227 bool SOSAccountIsAccountIdentity(SOSAccount
* account
, SOSPeerInfoRef peer_info
, CFErrorRef
*error
);
228 bool SOSAccountFullPeerInfoVerify(SOSAccount
* account
, SecKeyRef privKey
, CFErrorRef
*error
);
229 SOSPeerInfoRef
GenerateNewCloudIdentityPeerInfo(CFErrorRef
*error
);
232 bool SOSAccountHasPublicKey(SOSAccount
* account
, CFErrorRef
* error
);
233 bool SOSAccountPublishCloudParameters(SOSAccount
* account
, CFErrorRef
* error
);
234 bool SOSAccountRetrieveCloudParameters(SOSAccount
* account
, SecKeyRef
*newKey
,
236 CFDataRef
*newParameters
, CFErrorRef
* error
);
239 void SOSAccountAssertDSID(SOSAccount
* account
, CFStringRef dsid
);
245 SecKeyRef
SOSAccountCopyDeviceKey(SOSAccount
* account
, CFErrorRef
*error
);
246 SecKeyRef
GeneratePermanentFullECKey(int keySize
, CFStringRef name
, CFErrorRef
* error
);
249 void SOSAccountSetLastDepartureReason(SOSAccount
* account
, enum DepartureReason reason
);
250 void SOSAccountSetUserPublicTrustedForTesting(SOSAccount
* account
);
252 void SOSAccountPurgeIdentity(SOSAccount
*);
253 bool sosAccountLeaveCircle(SOSAccount
* account
, SOSCircleRef circle
, CFErrorRef
* error
);
254 bool sosAccountLeaveRing(SOSAccount
* account
, SOSRingRef ring
, CFErrorRef
* error
);
255 bool SOSAccountForEachRing(SOSAccount
* account
, SOSRingRef (^action
)(CFStringRef name
, SOSRingRef ring
));
256 bool SOSAccountUpdateBackUp(SOSAccount
* account
, CFStringRef viewname
, CFErrorRef
*error
);
257 void SOSAccountEnsureRecoveryRing(SOSAccount
* account
);
258 bool SOSAccountEnsureInBackupRings(SOSAccount
* account
);
260 bool SOSAccountEnsurePeerRegistration(SOSAccount
* account
, CFErrorRef
*error
);
262 extern const CFStringRef kSOSUnsyncedViewsKey
;
263 extern const CFStringRef kSOSPendingEnableViewsToBeSetKey
;
264 extern const CFStringRef kSOSPendingDisableViewsToBeSetKey
;
265 extern const CFStringRef kSOSRecoveryKey
;
268 kSOSTransportNone
= 0,
269 kSOSTransportIDS
= 1,
270 kSOSTransportKVS
= 2,
271 kSOSTransportFuture
= 3,
272 kSOSTransportPresent
= 4
275 SOSPeerInfoRef
SOSAccountCopyPeerWithID(SOSAccount
* account
, CFStringRef peerid
, CFErrorRef
*error
);
277 bool SOSAccountSetValue(SOSAccount
* account
, CFStringRef key
, CFTypeRef value
, CFErrorRef
*error
);
278 bool SOSAccountClearValue(SOSAccount
* account
, CFStringRef key
, CFErrorRef
*error
);
279 CFTypeRef
SOSAccountGetValue(SOSAccount
* account
, CFStringRef key
, CFErrorRef
*error
);
281 bool SOSAccountAddEscrowToPeerInfo(SOSAccount
* account
, SOSFullPeerInfoRef myPeer
, CFErrorRef
*error
);
282 bool SOSAccountAddEscrowRecords(SOSAccount
* account
, CFStringRef dsid
, CFDictionaryRef record
, CFErrorRef
*error
);
283 void SOSAccountRemoveRing(SOSAccount
* a
, CFStringRef ringName
);
284 SOSRingRef
SOSAccountCopyRingNamed(SOSAccount
* a
, CFStringRef ringName
, CFErrorRef
*error
);
285 SOSRingRef
SOSAccountRingCreateForName(SOSAccount
* a
, CFStringRef ringName
, CFErrorRef
*error
);
286 bool SOSAccountUpdateRingFromRemote(SOSAccount
* account
, SOSRingRef newRing
, CFErrorRef
*error
);
287 bool SOSAccountUpdateRing(SOSAccount
* account
, SOSRingRef newRing
, CFErrorRef
*error
);
288 bool SOSAccountRemoveBackupPeers(SOSAccount
* account
, CFArrayRef peerIDs
, CFErrorRef
*error
);
289 bool SOSAccountResetRing(SOSAccount
* account
, CFStringRef ringName
, CFErrorRef
*error
);
290 bool SOSAccountCheckPeerAvailability(SOSAccount
* account
, CFErrorRef
*error
);
291 bool SOSAccountUpdateNamedRing(SOSAccount
* account
, CFStringRef ringName
, CFErrorRef
*error
,
292 SOSRingRef (^create
)(CFStringRef ringName
, CFErrorRef
*error
),
293 SOSRingRef (^copyModified
)(SOSRingRef existing
, CFErrorRef
*error
));
296 // MARK: Backup translation functions
299 CFStringRef
SOSBackupCopyRingNameForView(CFStringRef viewName
);
302 // Security tool test/debug functions
304 bool SOSAccountPostDebugScope(SOSAccount
* account
, CFTypeRef scope
, CFErrorRef
*error
);
305 CFDataRef
SOSAccountCopyAccountStateFromKeychain(CFErrorRef
*error
);
306 bool SOSAccountDeleteAccountStateFromKeychain(CFErrorRef
*error
);
307 CFDataRef
SOSAccountCopyEngineStateFromKeychain(CFErrorRef
*error
);
308 bool SOSAccountDeleteEngineStateFromKeychain(CFErrorRef
*error
);
310 bool SOSAccountIsNew(SOSAccount
* account
, CFErrorRef
*error
);
311 bool SOSAccountCheckForAlwaysOnViews(SOSAccount
* account
);
312 // UUID, no setter just getter and ensuring value.
313 void SOSAccountEnsureUUID(SOSAccount
* account
);
314 CFStringRef
SOSAccountCopyUUID(SOSAccount
* account
);
315 const uint8_t* der_decode_cloud_parameters(CFAllocatorRef allocator
,
316 CFIndex algorithmID
, SecKeyRef
* publicKey
,
317 CFDataRef
*parameters
,
319 const uint8_t* der
, const uint8_t* der_end
);
325 CFDataRef
SOSPiggyBackBlobCopyEncodedData(SOSGenCountRef gencount
, SecKeyRef pubKey
, CFDataRef signature
, CFErrorRef
*error
);
328 NSData
*SOSPiggyCreateInitialSyncData(NSArray
<NSData
*> *identities
, NSArray
<NSDictionary
*>* tlks
);
329 NSDictionary
* SOSPiggyCopyInitialSyncData(const uint8_t** der
, const uint8_t *der_end
);
330 NSArray
<NSDictionary
*>* SOSAccountSortTLKS(NSArray
<NSDictionary
*>* tlks
);
333 bool SOSAccountCleanupAllKVSKeys(SOSAccount
* account
, CFErrorRef
* error
);
334 bool SOSAccountPopulateKVSWithBadKeys(SOSAccount
* account
, CFErrorRef
* error
);
338 @interface
SOSAccount (Persistence
)
340 +(instancetype
) accountFromData
: (NSData
*) data
341 factory
: (SOSDataSourceFactoryRef
) factory
342 error
: (NSError
**) error
;
343 +(instancetype
) accountFromDER
: (const uint8_t**) der
344 end
: (const uint8_t*) der_end
345 factory
: (SOSDataSourceFactoryRef
) factory
346 error
: (NSError
**) error
;
348 -(NSData
*) encodedData
: (NSError
**) error
;
351 #endif /* SOSAccount_h */