]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSAccountUpdate.m
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSAccountUpdate.m
1 //
2 // SOSAccountUpdate.c
3 // sec
4 //
5
6 #include "SOSAccountPriv.h"
7 #include "SOSAccountLog.h"
8
9 #include <Security/SecureObjectSync/SOSTransportCircleKVS.h>
10 #include <Security/SecureObjectSync/SOSTransport.h>
11 #include <Security/SecureObjectSync/SOSViews.h>
12 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
13 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
14 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
15 #include <Security/SecureObjectSync/SOSPeerInfoDER.h>
16 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
17 #include <Security/SecureObjectSync/SOSAccountGhost.h>
18
19 #import <Security/SecureObjectSync/SOSAccountTrust.h>
20 #import <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
21 #import <Security/SecureObjectSync/SOSAccountTrustClassic+Expansion.h>
22
23 static void DifferenceAndCall(CFSetRef old_members, CFSetRef new_members, void (^updatedCircle)(CFSetRef additions, CFSetRef removals))
24 {
25 CFMutableSetRef additions = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, new_members);
26 CFMutableSetRef removals = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, old_members);
27
28
29 CFSetForEach(old_members, ^(const void * value) {
30 CFSetRemoveValue(additions, value);
31 });
32
33 CFSetForEach(new_members, ^(const void * value) {
34 CFSetRemoveValue(removals, value);
35 });
36
37 updatedCircle(additions, removals);
38
39 CFReleaseSafe(additions);
40 CFReleaseSafe(removals);
41 }
42 static CFMutableSetRef SOSAccountCopyIntersectedViews(CFSetRef peerViews, CFSetRef myViews) {
43 __block CFMutableSetRef views = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
44 if (peerViews && myViews) CFSetForEach(peerViews, ^(const void *view) {
45 if (CFSetContainsValue(myViews, view)) {
46 CFSetAddValue(views, view);
47 }
48 });
49 return views;
50 }
51
52 static inline bool isSyncing(SOSPeerInfoRef peer, SecKeyRef upub) {
53 if(!SOSPeerInfoApplicationVerify(peer, upub, NULL)) return false;
54 if(SOSPeerInfoIsRetirementTicket(peer)) return false;
55 return true;
56 }
57
58 static bool isBackupSOSRing(SOSRingRef ring)
59 {
60 return isSOSRing(ring) && (kSOSRingBackup == SOSRingGetType(ring));
61 }
62
63 static void SOSAccountAppendPeerMetasForViewBackups(SOSAccount* account, CFSetRef views, CFMutableArrayRef appendTo)
64 {
65 CFMutableDictionaryRef ringToViewTable = NULL;
66
67 if([account getCircleStatus:NULL] != kSOSCCInCircle)
68 return;
69
70 if(!(SOSAccountHasCompletedInitialSync(account))){
71 secnotice("backup", "Haven't finished initial backup syncing, not registering backup metas with engine");
72 return;
73 }
74 if(!SOSPeerInfoV2DictionaryHasData(account.peerInfo, sBackupKeyKey)){
75 secnotice("backup", "No key to backup to, we don't enable individual view backups");
76 return;
77 }
78 ringToViewTable = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
79
80 CFSetForEach(views, ^(const void *value) {
81 CFStringRef viewName = value;
82 if (isString(viewName) && !CFEqualSafe(viewName, kSOSViewKeychainV0)) {
83 CFStringRef ringName = SOSBackupCopyRingNameForView(viewName);
84 viewName = ringName;
85 SOSRingRef ring = [account.trust copyRing:ringName err:NULL];
86 if (ring && isBackupSOSRing(ring)) {
87 CFTypeRef currentValue = (CFTypeRef) CFDictionaryGetValue(ringToViewTable, ring);
88
89 if (isSet(currentValue)) {
90 CFSetAddValue((CFMutableSetRef)currentValue, viewName);
91 } else {
92 CFMutableSetRef viewNameSet = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
93 CFSetAddValue(viewNameSet, viewName);
94
95 CFDictionarySetValue(ringToViewTable, ring, viewNameSet);
96 CFReleaseNull(viewNameSet);
97 }
98 } else {
99 secwarning("View '%@' not being backed up – ring %@:%@ not backup ring.", viewName, ringName, ring);
100 }
101 CFReleaseNull(ringName);
102 CFReleaseNull(ring);
103 }
104 });
105
106 CFDictionaryForEach(ringToViewTable, ^(const void *key, const void *value) {
107 SOSRingRef ring = (SOSRingRef) key;
108 CFSetRef viewNames = asSet(value, NULL);
109 if (isSOSRing(ring) && viewNames) {
110 if (SOSAccountIntersectsWithOutstanding(account, viewNames)) {
111 CFStringSetPerformWithDescription(viewNames, ^(CFStringRef ringViews) {
112 secnotice("engine-notify", "Not ready, no peer meta: R: %@ Vs: %@", SOSRingGetName(ring), ringViews);
113 });
114 } else {
115 bool meta_added = false;
116 CFErrorRef create_error = NULL;
117 SOSBackupSliceKeyBagRef key_bag = NULL;
118 SOSPeerMetaRef newMeta = NULL;
119
120 CFDataRef ring_payload = SOSRingGetPayload(ring, NULL);
121 require_quiet(isData(ring_payload), skip);
122
123 key_bag = SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault, ring_payload, &create_error);
124 require_quiet(key_bag, skip);
125
126 newMeta = SOSPeerMetaCreateWithComponents(SOSRingGetName(ring), viewNames, ring_payload);
127 require_quiet(SecAllocationError(newMeta, &create_error, CFSTR("Didn't make peer meta for: %@"), ring), skip);
128 CFArrayAppendValue(appendTo, newMeta);
129
130 CFStringSetPerformWithDescription(viewNames, ^(CFStringRef ringViews) {
131 secnotice("engine-notify", "Backup peer meta: R: %@ Vs: %@ VD: %@", SOSRingGetName(ring), ringViews, ring_payload);
132 });
133
134 meta_added = true;
135
136 skip:
137 if (!meta_added) {
138 CFStringSetPerformWithDescription(viewNames, ^(CFStringRef ringViews) {
139 secerror("Failed to register backup meta from %@ for views %@. Error (%@)", ring, ringViews, create_error);
140 });
141 }
142 CFReleaseNull(newMeta);
143 CFReleaseNull(key_bag);
144 CFReleaseNull(create_error);
145 }
146 }
147 });
148
149 CFReleaseNull(ringToViewTable);
150 }
151
152 bool SOSAccountSyncingV0(SOSAccount* account) {
153 __block bool syncingV0 = false;
154 SOSAccountForEachCirclePeerExceptMe(account, ^(SOSPeerInfoRef peer) {
155 if (SOSPeerInfoIsEnabledView(peer, kSOSViewKeychainV0)) {
156 syncingV0 = true;
157 }
158 });
159
160 return syncingV0;
161 }
162
163 void SOSAccountNotifyEngines(SOSAccount* account)
164 {
165 SOSAccountTrustClassic *trust = account.trust;
166 SOSFullPeerInfoRef identity = trust.fullPeerInfo;
167 SOSCircleRef circle = trust.trustedCircle;
168
169 SOSPeerInfoRef myPi = SOSFullPeerInfoGetPeerInfo(identity);
170 CFStringRef myPi_id = SOSPeerInfoGetPeerID(myPi);
171 CFMutableArrayRef syncing_peer_metas = NULL;
172 CFMutableArrayRef zombie_peer_metas = NULL;
173 CFErrorRef localError = NULL;
174 SOSPeerMetaRef myMeta = NULL;
175
176 if (myPi_id && isSyncing(myPi, account.accountKey) && SOSCircleHasPeer(circle, myPi, NULL)) {
177 CFMutableSetRef myViews = SOSPeerInfoCopyEnabledViews(myPi);
178
179 // We add V0 views to everyone if we see a V0 peer, or a peer with the view explicity enabled
180 // V2 peers shouldn't be explicity enabling the uber V0 view, though the seeds did.
181 __block bool addV0Views = SOSAccountSyncingV0(account);
182
183 syncing_peer_metas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
184 zombie_peer_metas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
185 SOSAccountForEachCirclePeerExceptMe(account, ^(SOSPeerInfoRef peer) {
186 CFMutableArrayRef arrayToAddTo = isSyncing(peer, account.accountKey) ? syncing_peer_metas : zombie_peer_metas;
187
188 // Compute views each peer is in that we are also in ourselves
189 CFMutableSetRef peerEnabledViews = SOSPeerInfoCopyEnabledViews(peer);
190 CFMutableSetRef views = SOSAccountCopyIntersectedViews(peerEnabledViews, myViews);
191 CFReleaseNull(peerEnabledViews);
192
193 if(addV0Views) {
194 CFSetAddValue(views, kSOSViewKeychainV0);
195 }
196
197 CFStringSetPerformWithDescription(views, ^(CFStringRef viewsDescription) {
198 secnotice("engine-notify", "Meta: %@: %@", SOSPeerInfoGetPeerID(peer), viewsDescription);
199 });
200
201 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(SOSPeerInfoGetPeerID(peer), views, NULL);
202 CFReleaseNull(views);
203
204 CFArrayAppendValue(arrayToAddTo, peerMeta);
205 CFReleaseNull(peerMeta);
206 });
207
208 // We don't make a backup peer meta for the magic V0 peer
209 // Set up all the rest before we munge the set
210 SOSAccountAppendPeerMetasForViewBackups(account, myViews, syncing_peer_metas);
211
212 // If we saw someone else needing V0, we sync V0, too!
213 if (addV0Views) {
214 CFSetAddValue(myViews, kSOSViewKeychainV0);
215 }
216
217 CFStringSetPerformWithDescription(myViews, ^(CFStringRef viewsDescription) {
218 secnotice("engine-notify", "My Meta: %@: %@", myPi_id, viewsDescription);
219 });
220 myMeta = SOSPeerMetaCreateWithComponents(myPi_id, myViews, NULL);
221 CFReleaseSafe(myViews);
222 }
223
224 SOSEngineRef engine = SOSDataSourceFactoryGetEngineForDataSourceName(account.factory, SOSCircleGetName(circle), NULL);
225 if (engine) {
226 SOSEngineCircleChanged(engine, myMeta, syncing_peer_metas, zombie_peer_metas);
227 }
228
229 CFReleaseNull(myMeta);
230 CFReleaseSafe(localError);
231
232 CFReleaseNull(syncing_peer_metas);
233 CFReleaseNull(zombie_peer_metas);
234 }
235
236
237 // Upcoming call to View Changes Here
238 void SOSAccountNotifyOfChange(SOSAccount* account, SOSCircleRef oldCircle, SOSCircleRef newCircle)
239 {
240 account.circle_rings_retirements_need_attention = true;
241
242 CFMutableSetRef old_members = SOSCircleCopyPeers(oldCircle, kCFAllocatorDefault);
243 CFMutableSetRef new_members = SOSCircleCopyPeers(newCircle, kCFAllocatorDefault);
244
245 CFMutableSetRef old_applicants = SOSCircleCopyApplicants(oldCircle, kCFAllocatorDefault);
246 CFMutableSetRef new_applicants = SOSCircleCopyApplicants(newCircle, kCFAllocatorDefault);
247
248 SOSPeerInfoRef me = account.peerInfo;
249 if(me && CFSetContainsValue(new_members, me))
250 SOSAccountSetValue(account, kSOSEscrowRecord, kCFNull, NULL); //removing the escrow records from the account object
251
252 DifferenceAndCall(old_members, new_members, ^(CFSetRef added_members, CFSetRef removed_members) {
253 DifferenceAndCall(old_applicants, new_applicants, ^(CFSetRef added_applicants, CFSetRef removed_applicants) {
254 CFArrayForEach((__bridge CFArrayRef)(account.change_blocks), ^(const void * notificationBlock) {
255 secnotice("updates", "calling change block");
256 ((__bridge SOSAccountCircleMembershipChangeBlock) notificationBlock)(newCircle, added_members, removed_members, added_applicants, removed_applicants);
257 });
258 });
259 });
260
261 CFReleaseNull(old_applicants);
262 CFReleaseNull(new_applicants);
263
264 CFReleaseNull(old_members);
265 CFReleaseNull(new_members);
266 }
267
268 CF_RETURNS_RETAINED
269 CFDictionaryRef SOSAccountHandleRetirementMessages(SOSAccount* account, CFDictionaryRef circle_retirement_messages, CFErrorRef *error) {
270 SOSAccountTrustClassic* trust = account.trust;
271 CFStringRef circle_name = SOSCircleGetName(trust.trustedCircle);
272 CFMutableArrayRef handledRetirementIDs = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
273 // We only handle one circle, look it up:
274
275 if(!trust.trustedCircle) // We don't fail, we intentionally handle nothing.
276 return CFDictionaryCreateForCFTypes(kCFAllocatorDefault, NULL);
277
278 CFDictionaryRef retirement_dictionary = asDictionary(CFDictionaryGetValue(circle_retirement_messages, circle_name), error);
279 if(!retirement_dictionary)
280 return CFDictionaryCreateForCFTypes(kCFAllocatorDefault, NULL);
281
282 CFDictionaryForEach(retirement_dictionary, ^(const void *key, const void *value) {
283 if(isData(value)) {
284 SOSPeerInfoRef pi = SOSPeerInfoCreateFromData(NULL, error, (CFDataRef) value);
285 if(pi && CFEqual(key, SOSPeerInfoGetPeerID(pi)) && SOSPeerInfoInspectRetirementTicket(pi, error)) {
286 [trust.retirees addObject: (__bridge id _Nonnull)(pi)];
287
288 account.circle_rings_retirements_need_attention = true; // Have to handle retirements.
289
290 CFArrayAppendValue(handledRetirementIDs, key);
291 }
292 CFReleaseNull(pi);
293 }
294 });
295
296 // If we are in the retiree list, we somehow got resurrected
297 // clearly we took care of proper departure before so leave
298 // and delcare that we withdrew this time.
299 SOSPeerInfoRef me = account.peerInfo;
300
301 if (me && [trust.retirees containsObject:(__bridge id _Nonnull)(me)]) {
302 SOSAccountPurgeIdentity(account);
303 trust.departureCode = kSOSDiscoveredRetirement;
304 }
305
306 CFDictionaryRef result = (CFArrayGetCount(handledRetirementIDs) == 0) ? CFDictionaryCreateForCFTypes(kCFAllocatorDefault, NULL)
307 : CFDictionaryCreateForCFTypes(kCFAllocatorDefault, circle_name, handledRetirementIDs, NULL);
308
309 CFReleaseNull(handledRetirementIDs);
310 return result;
311 }
312
313 static SOSCircleRef SOSAccountCreateCircleFrom(CFStringRef circleName, CFTypeRef value, CFErrorRef *error) {
314 if (value && !isData(value) && !isNull(value)) {
315 secnotice("circleCreat", "Value provided not appropriate for a circle");
316 CFStringRef description = CFCopyTypeIDDescription(CFGetTypeID(value));
317 SOSCreateErrorWithFormat(kSOSErrorUnexpectedType, NULL, error, NULL,
318 CFSTR("Expected data or NULL got %@"), description);
319 CFReleaseSafe(description);
320 return NULL;
321 }
322
323 SOSCircleRef circle = NULL;
324 if (!value || isNull(value)) {
325 secnotice("circleCreat", "No circle found in data: %@", value);
326 circle = NULL;
327 } else {
328 circle = SOSCircleCreateFromData(NULL, (CFDataRef) value, error);
329 if (circle) {
330 CFStringRef name = SOSCircleGetName(circle);
331 if (!CFEqualSafe(name, circleName)) {
332 secnotice("circleCreat", "Expected circle named %@, got %@", circleName, name);
333 SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL,
334 CFSTR("Expected circle named %@, got %@"), circleName, name);
335 CFReleaseNull(circle);
336 }
337 } else {
338 secnotice("circleCreat", "SOSCircleCreateFromData returned NULL.");
339 }
340 }
341 return circle;
342 }
343
344 bool SOSAccountHandleCircleMessage(SOSAccount* account,
345 CFStringRef circleName, CFDataRef encodedCircleMessage, CFErrorRef *error) {
346 bool success = false;
347 CFErrorRef localError = NULL;
348 SOSCircleRef circle = SOSAccountCreateCircleFrom(circleName, encodedCircleMessage, &localError);
349 if (circle) {
350 success = [account.trust updateCircleFromRemote:account.circle_transport newCircle:circle err:&localError];
351 CFReleaseSafe(circle);
352 } else {
353 secerror("NULL circle found, ignoring ...");
354 success = true; // don't pend this NULL thing.
355 }
356
357 if (!success) {
358 if (isSOSErrorCoded(localError, kSOSErrorIncompatibleCircle)) {
359 secerror("Incompatible circle found, abandoning membership: %@", circleName);
360 }
361
362 if (error) {
363 *error = localError;
364 localError = NULL;
365 }
366
367 }
368
369 CFReleaseNull(localError);
370
371 return success;
372 }
373
374 bool SOSAccountHandleParametersChange(SOSAccount* account, CFDataRef parameters, CFErrorRef *error){
375
376 SecKeyRef newKey = NULL;
377 CFDataRef newParameters = NULL;
378 bool success = false;
379
380 if(SOSAccountRetrieveCloudParameters(account, &newKey, parameters, &newParameters, error)) {
381 debugDumpUserParameters(CFSTR("SOSAccountHandleParametersChange got new user key parameters:"), parameters);
382 secnotice("keygen", "SOSAccountHandleParametersChange got new public key: %@", newKey);
383
384 if (CFEqualSafe(account.accountKey, newKey)) {
385 secnotice("updates", "Got same public key sent our way. Ignoring.");
386 success = true;
387 } else if (CFEqualSafe(account.previousAccountKey, newKey)) {
388 secnotice("updates", "Got previous public key repeated. Ignoring.");
389 success = true;
390 } else {
391 SOSAccountSetUnTrustedUserPublicKey(account, newKey);
392 SOSAccountSetParameters(account, newParameters);
393 newKey = NULL;
394
395 if(SOSAccountRetryUserCredentials(account)) {
396 secnotice("keygen", "Successfully used cached password with new parameters");
397 SOSAccountGenerationSignatureUpdate(account, error);
398 } else {
399 secnotice("keygen", "Got new parameters for public key - could not find or use cached password");
400 SOSAccountPurgePrivateCredential(account);
401 }
402
403 account.circle_rings_retirements_need_attention = true;
404 account.key_interests_need_updating = true;
405
406 success = true;
407 }
408 }
409
410 CFReleaseNull(newKey);
411 CFReleaseNull(newParameters);
412
413 return success;
414 }
415