]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSAccountPriv.h
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSAccountPriv.h
1 //
2 // SOSAccountPriv.h
3 // Security
4 //
5
6 #ifndef SOSAccountPriv_h
7 #define SOSAccountPriv_h
8
9 #import <Foundation/Foundation.h>
10
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>
16
17 #include <Security/SecKeyPriv.h>
18
19 #include <Security/der_plist.h>
20 #include <utilities/der_plist_internal.h>
21 #include <corecrypto/ccder.h>
22
23 #include <AssertMacros.h>
24 #include <assert.h>
25
26 #import <notify.h>
27
28 #include "keychain/SecureObjectSync/SOSInternal.h"
29
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 <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>
40
41 #include "keychain/SecureObjectSync/SOSPeerInfoInternal.h"
42 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
43 #include "keychain/SecureObjectSync/SOSTransportCircle.h"
44
45 #include <utilities/iCloudKeychainTrace.h>
46
47 #include <Security/SecItemPriv.h>
48
49
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;
64
65 typedef void (^SOSAccountSaveBlock)(CFDataRef flattenedAccount, CFErrorRef flattenFailError);
66
67 @class SOSMessageKVS;
68 @class CKKeyParameter;
69 @class SOSAccountTrustClassic;
70 @class SOSKVSCircleStorageTransport;
71 @class SOSCircleStorageTransport;
72 @class SOSCKCircleStorage;
73
74 @interface SOSAccount : NSObject <SOSControlProtocol>
75
76 @property (nonatomic, retain) NSDictionary *gestalt;
77 @property (nonatomic, retain) NSData *backup_key;
78 @property (nonatomic, retain) NSString *deviceID;
79
80 @property (nonatomic, retain) SOSAccountTrustClassic *trust;
81
82 @property (nonatomic, retain) dispatch_queue_t queue;
83 @property (nonatomic, retain) dispatch_source_t user_private_timer;
84 @property (nonatomic) SecKeyRef accountPrivateKey;
85
86 @property (nonatomic) SOSDataSourceFactoryRef factory;
87
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;
95
96
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;
100
101 @property (nonatomic, retain) NSMutableArray *change_blocks;
102
103 @property (nonatomic, retain) NSMutableDictionary *waitForInitialSync_blocks;
104
105 @property (nonatomic, retain) NSData* accountKeyDerivationParamters;
106
107 @property (nonatomic, assign) BOOL accountKeyIsTrusted;
108 @property (nonatomic) SecKeyRef accountKey;
109 @property (nonatomic) SecKeyRef previousAccountKey;
110
111 @property (copy) SOSAccountSaveBlock saveBlock;
112
113
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;
119
120 @property (nonatomic, assign) BOOL notifyCircleChangeOnExit;
121 @property (nonatomic, assign) BOOL notifyViewChangeOnExit;
122 @property (nonatomic, assign) BOOL notifyBackupOnExit;
123
124 @property (nonatomic, retain) NSUserDefaults* settings;
125
126
127
128 -(id) init;
129 -(id) initWithGestalt:(CFDictionaryRef)gestalt factory:(SOSDataSourceFactoryRef)factory;
130
131 void SOSAccountAddSyncablePeerBlock(SOSAccount* a,
132 CFStringRef ds_name,
133 SOSAccountSyncablePeersBlock changeBlock);
134
135 -(bool) ensureFactoryCircles;
136 -(void) ensureOctagonPeerKeys;
137
138 -(void) flattenToSaveBlock;
139
140 -(void) ghostBustSchedule;
141 + (SOSAccountGhostBustingOptions) ghostBustGetRampSettings;
142 - (bool) ghostBustCheckDate;
143
144
145 void SOSAccountSetToNew(SOSAccount* a);
146
147 bool SOSAccountIsMyPeerActive(SOSAccount* account, CFErrorRef* error);
148
149 // MARK: In Sync checking
150 typedef bool (^SOSAccountWaitForInitialSyncBlock)(SOSAccount* account);
151
152 CF_RETURNS_RETAINED CFStringRef SOSAccountCallWhenInSync(SOSAccount* account, SOSAccountWaitForInitialSyncBlock syncBlock);
153 bool SOSAccountUnregisterCallWhenInSync(SOSAccount* account, CFStringRef id);
154
155 bool SOSAccountHandleOutOfSyncUpdate(SOSAccount* account, CFSetRef oldOOSViews, CFSetRef newOOSViews);
156
157 void SOSAccountEnsureSyncChecking(SOSAccount* account);
158 void SOSAccountCancelSyncChecking(SOSAccount* account);
159 void SOSAccountInitializeInitialSync(SOSAccount* account);
160 CFMutableSetRef SOSAccountCopyOutstandingViews(SOSAccount* account);
161 CFSetRef SOSAccountCopyEnabledViews(SOSAccount* account);
162 void SOSAccountNotifyEngines(SOSAccount* account);
163 CFMutableSetRef SOSAccountCopyOutstandingViews(SOSAccount* account);
164 bool SOSAccountIsViewOutstanding(SOSAccount* account, CFStringRef view);
165 CFMutableSetRef SOSAccountCopyIntersectionWithOustanding(SOSAccount* account, CFSetRef inSet);
166 bool SOSAccountIntersectsWithOutstanding(SOSAccount* account, CFSetRef views);
167 bool SOSAccountHasOustandingViews(SOSAccount* account);
168 bool SOSAccountHasCompletedInitialSync(SOSAccount* account);
169 bool SOSAccountHasCompletedRequiredBackupSync(SOSAccount* account);
170 CFMutableSetRef SOSAccountCopyOutstandingViews(SOSAccount* account);
171 bool SOSAccountSyncingV0(SOSAccount* account);
172
173 // MARK: DER Stuff
174
175
176 size_t der_sizeof_fullpeer_or_null(SOSFullPeerInfoRef data, CFErrorRef* error);
177
178 uint8_t* der_encode_fullpeer_or_null(SOSFullPeerInfoRef data, CFErrorRef* error, const uint8_t* der, uint8_t* der_end);
179
180 const uint8_t* der_decode_fullpeer_or_null(CFAllocatorRef allocator, SOSFullPeerInfoRef* data,
181 CFErrorRef* error,
182 const uint8_t* der, const uint8_t* der_end);
183
184
185 size_t der_sizeof_public_bytes(SecKeyRef publicKey, CFErrorRef* error);
186
187 uint8_t* der_encode_public_bytes(SecKeyRef publicKey, CFErrorRef* error, const uint8_t* der, uint8_t* der_end);
188
189 const uint8_t* der_decode_public_bytes(CFAllocatorRef allocator, CFIndex algorithmID, SecKeyRef* publicKey, CFErrorRef* error, const uint8_t* der, const uint8_t* der_end);
190
191
192 // Update
193 -(SOSCCStatus) getCircleStatus:(CFErrorRef*) error;
194 -(bool) isInCircle:(CFErrorRef *)error;
195
196 bool SOSAccountHandleCircleMessage(SOSAccount* account,
197 CFStringRef circleName, CFDataRef encodedCircleMessage, CFErrorRef *error);
198
199 CF_RETURNS_RETAINED
200 CFDictionaryRef SOSAccountHandleRetirementMessages(SOSAccount* account, CFDictionaryRef circle_retirement_messages, CFErrorRef *error);
201
202 void SOSAccountRecordRetiredPeersInCircle(SOSAccount* account);
203
204 bool SOSAccountHandleUpdateCircle(SOSAccount* account,
205 SOSCircleRef prospective_circle,
206 bool writeUpdate,
207 CFErrorRef *error);
208
209
210 // My Peer
211 bool SOSAccountHasFullPeerInfo(SOSAccount* account, CFErrorRef* error);
212
213 bool SOSAccountIsMyPeerInBackupAndCurrentInView(SOSAccount* account, CFStringRef viewname);
214 bool SOSAccountUpdateOurPeerInBackup(SOSAccount* account, SOSRingRef oldRing, CFErrorRef *error);
215 bool SOSAccountIsPeerInBackupAndCurrentInView(SOSAccount* account, SOSPeerInfoRef testPeer, CFStringRef viewname);
216 bool SOSDeleteV0Keybag(CFErrorRef *error);
217 bool SOSAccountUpdatePeerInfo(SOSAccount* account, CFStringRef updateDescription, CFErrorRef *error, bool (^update)(SOSFullPeerInfoRef fpi, CFErrorRef *error));
218 bool SOSAccountUpdatePeerInfoAndPush(SOSAccount* account, CFStringRef updateDescription, CFErrorRef *error,
219 bool (^update)(SOSPeerInfoRef pi, CFErrorRef *error));
220
221 // Currently permitted backup rings.
222 void SOSAccountForEachBackupRingName(SOSAccount* account, void (^operation)(CFStringRef value));
223 void SOSAccountForEachRingName(SOSAccount* account, void (^operation)(CFStringRef value));
224 void SOSAccountForEachBackupView(SOSAccount* account, void (^operation)(const void *value));
225 SOSRingRef SOSAccountCreateBackupRingForView(SOSAccount* account, CFStringRef ringBackupViewName, CFErrorRef *error);
226
227
228 // My Circle
229 bool SOSAccountHasCircle(SOSAccount* account, CFErrorRef* error);
230 SOSCircleRef CF_RETURNS_RETAINED SOSAccountEnsureCircle(SOSAccount* a, CFStringRef name, CFErrorRef *error);
231
232 void AppendCircleKeyName(CFMutableArrayRef array, CFStringRef name);
233
234 CFStringRef SOSInterestListCopyDescription(CFArrayRef interests);
235
236
237 // FullPeerInfos - including Cloud Identity
238 SOSFullPeerInfoRef CopyCloudKeychainIdentity(SOSPeerInfoRef cloudPeer, CFErrorRef *error);
239
240 bool SOSAccountIsAccountIdentity(SOSAccount* account, SOSPeerInfoRef peer_info, CFErrorRef *error);
241 bool SOSAccountFullPeerInfoVerify(SOSAccount* account, SecKeyRef privKey, CFErrorRef *error);
242 CF_RETURNS_RETAINED SOSPeerInfoRef GenerateNewCloudIdentityPeerInfo(CFErrorRef *error);
243
244 // Credentials
245 bool SOSAccountHasPublicKey(SOSAccount* account, CFErrorRef* error);
246 bool SOSAccountPublishCloudParameters(SOSAccount* account, CFErrorRef* error);
247 bool SOSAccountRetrieveCloudParameters(SOSAccount* account, SecKeyRef *newKey,
248 CFDataRef derparms,
249 CFDataRef *newParameters, CFErrorRef* error);
250
251 //DSID
252 void SOSAccountAssertDSID(SOSAccount* account, CFStringRef dsid);
253
254 //
255 // Key extraction
256 //
257
258 SecKeyRef SOSAccountCopyDeviceKey(SOSAccount* account, CFErrorRef *error);
259 SecKeyRef CF_RETURNS_RETAINED GeneratePermanentFullECKey(int keySize, CFStringRef name, CFErrorRef* error);
260
261 // Testing
262 void SOSAccountSetLastDepartureReason(SOSAccount* account, enum DepartureReason reason);
263 void SOSAccountSetUserPublicTrustedForTesting(SOSAccount* account);
264
265 void SOSAccountPurgeIdentity(SOSAccount*);
266 bool sosAccountLeaveCircle(SOSAccount* account, SOSCircleRef circle, CFErrorRef* error);
267 bool sosAccountLeaveCircleWithAnalytics(SOSAccount* account, SOSCircleRef circle, NSData* parentData, CFErrorRef* error);
268
269 bool sosAccountLeaveRing(SOSAccount* account, SOSRingRef ring, CFErrorRef* error);
270 bool SOSAccountForEachRing(SOSAccount* account, SOSRingRef (^action)(CFStringRef name, SOSRingRef ring));
271 bool SOSAccountUpdateBackUp(SOSAccount* account, CFStringRef viewname, CFErrorRef *error);
272 void SOSAccountEnsureRecoveryRing(SOSAccount* account);
273 bool SOSAccountEnsureInBackupRings(SOSAccount* account);
274
275 bool SOSAccountEnsurePeerRegistration(SOSAccount* account, CFErrorRef *error);
276
277 extern const CFStringRef kSOSUnsyncedViewsKey;
278 extern const CFStringRef kSOSPendingEnableViewsToBeSetKey;
279 extern const CFStringRef kSOSPendingDisableViewsToBeSetKey;
280 extern const CFStringRef kSOSRecoveryKey;
281
282 typedef enum{
283 kSOSTransportNone = 0,
284 kSOSTransportIDS = 1,
285 kSOSTransportKVS = 2,
286 kSOSTransportFuture = 3,
287 kSOSTransportPresent = 4
288 }TransportType;
289
290 SOSPeerInfoRef SOSAccountCopyPeerWithID(SOSAccount* account, CFStringRef peerid, CFErrorRef *error);
291
292 bool SOSAccountSetValue(SOSAccount* account, CFStringRef key, CFTypeRef value, CFErrorRef *error);
293 bool SOSAccountClearValue(SOSAccount* account, CFStringRef key, CFErrorRef *error);
294 CFTypeRef SOSAccountGetValue(SOSAccount* account, CFStringRef key, CFErrorRef *error);
295
296 bool SOSAccountAddEscrowToPeerInfo(SOSAccount* account, SOSFullPeerInfoRef myPeer, CFErrorRef *error);
297 bool SOSAccountAddEscrowRecords(SOSAccount* account, CFStringRef dsid, CFDictionaryRef record, CFErrorRef *error);
298 void SOSAccountRemoveRing(SOSAccount* a, CFStringRef ringName);
299 SOSRingRef SOSAccountCopyRingNamed(SOSAccount* a, CFStringRef ringName, CFErrorRef *error);
300 SOSRingRef SOSAccountRingCreateForName(SOSAccount* a, CFStringRef ringName, CFErrorRef *error);
301 bool SOSAccountUpdateRingFromRemote(SOSAccount* account, SOSRingRef newRing, CFErrorRef *error);
302 bool SOSAccountUpdateRing(SOSAccount* account, SOSRingRef newRing, CFErrorRef *error);
303 bool SOSAccountRemoveBackupPeers(SOSAccount* account, CFArrayRef peerIDs, CFErrorRef *error);
304 bool SOSAccountResetRing(SOSAccount* account, CFStringRef ringName, CFErrorRef *error);
305 bool SOSAccountCheckPeerAvailability(SOSAccount* account, 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));
309
310 //
311 // MARK: Backup translation functions
312 //
313
314 CFStringRef SOSBackupCopyRingNameForView(CFStringRef viewName);
315 bool SOSAccountUpdateBackupRing(SOSAccount* account, CFStringRef viewName, CFErrorRef *error,
316 SOSRingRef (^modify)(SOSRingRef existing, CFErrorRef *error));
317 //
318 // Security tool test/debug functions
319 //
320 bool SOSAccountPostDebugScope(SOSAccount* account, CFTypeRef scope, CFErrorRef *error);
321 CFDataRef SOSAccountCopyAccountStateFromKeychain(CFErrorRef *error);
322 bool SOSAccountDeleteAccountStateFromKeychain(CFErrorRef *error);
323 CFDataRef SOSAccountCopyEngineStateFromKeychain(CFErrorRef *error);
324 bool SOSAccountDeleteEngineStateFromKeychain(CFErrorRef *error);
325
326 bool SOSAccountIsNew(SOSAccount* account, CFErrorRef *error);
327 bool SOSAccountCheckForAlwaysOnViews(SOSAccount* account);
328 // UUID, no setter just getter and ensuring value.
329 void SOSAccountEnsureUUID(SOSAccount* account);
330 CFStringRef CF_RETURNS_RETAINED SOSAccountCopyUUID(SOSAccount* account);
331 const uint8_t* der_decode_cloud_parameters(CFAllocatorRef allocator,
332 CFIndex algorithmID, SecKeyRef* publicKey,
333 CFDataRef *parameters,
334 CFErrorRef* error,
335 const uint8_t* der, const uint8_t* der_end);
336
337 /*
338 * HSA2/piggybacking
339 */
340
341 CFDataRef SOSPiggyBackBlobCopyEncodedData(SOSGenCountRef gencount, SecKeyRef pubKey, CFDataRef signature, CFErrorRef *error);
342
343 #if __OBJC__
344 NSData *SOSPiggyCreateInitialSyncData(NSArray<NSData*> *identities, NSArray<NSDictionary *>* tlks);
345 NSDictionary * SOSPiggyCopyInitialSyncData(const uint8_t** der, const uint8_t *der_end);
346 NSArray<NSDictionary*>* SOSAccountSortTLKS(NSArray<NSDictionary*>* tlks);
347 #endif
348
349 bool SOSAccountCleanupAllKVSKeys(SOSAccount* account, CFErrorRef* error);
350 bool SOSAccountPopulateKVSWithBadKeys(SOSAccount* account, CFErrorRef* error);
351
352 @end
353
354 @interface SOSAccount (Persistence)
355
356 +(instancetype) accountFromData: (NSData*) data
357 factory: (SOSDataSourceFactoryRef) factory
358 error: (NSError**) error;
359 +(instancetype) accountFromDER: (const uint8_t**) der
360 end: (const uint8_t*) der_end
361 factory: (SOSDataSourceFactoryRef) factory
362 error: (NSError**) error;
363
364 -(NSData*) encodedData: (NSError**) error;
365
366
367 @end
368
369 #endif /* SOSAccount_h */