2 // SOSAccountTrustClassicExpansion.m
7 #import <Foundation/Foundation.h>
8 #import "Security/SecureObjectSync/SOSAccount.h"
9 #import "Security/SecureObjectSync/SOSAccountTrustClassic.h"
10 #import "Security/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
11 #import "Security/SecureObjectSync/SOSAccountTrustClassic+Retirement.h"
12 #import "Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
13 #import "Security/SecureObjectSync/SOSViews.h"
14 #import "Security/SecureObjectSync/SOSPeerInfoV2.h"
15 #import "Security/SecureObjectSync/SOSTransportCircleKVS.h"
17 @implementation SOSAccountTrustClassic (Expansion)
28 static const char *concordstring[] = {
29 "kSOSConcordanceTrusted",
30 "kSOSConcordanceGenOld", // kSOSErrorReplay
31 "kSOSConcordanceNoUserSig", // kSOSErrorBadSignature
32 "kSOSConcordanceNoUserKey", // kSOSErrorNoKey
33 "kSOSConcordanceNoPeer", // kSOSErrorPeerNotFound
34 "kSOSConcordanceBadUserSig", // kSOSErrorBadSignature
35 "kSOSConcordanceBadPeerSig", // kSOSErrorBadSignature
36 "kSOSConcordanceNoPeerSig",
37 "kSOSConcordanceWeSigned",
40 static const char * __unused actionstring[] = {
41 "accept", "countersign", "leave", "revert", "modify", "ignore",
44 static NSString* kSOSRingKey = @"trusted_rings";
47 // Generic Calls to Expansion Dictionary
49 -(CFTypeRef) getValueFromExpansion:(CFStringRef)key err:(CFErrorRef*)error
51 if (!self.expansion) {
54 return (__bridge CFTypeRef)([self.expansion objectForKey:(__bridge NSString*)key]);
57 -(bool) ensureExpansion:(CFErrorRef *)error
59 if (!self.expansion) {
60 self.expansion = [NSMutableDictionary dictionary];
63 return SecAllocationError((__bridge CFTypeRef)(self.expansion), error, CFSTR("Can't Alloc Account Expansion dictionary"));
66 -(bool) clearValueFromExpansion:(CFStringRef) key err:(CFErrorRef *)error
68 bool success = [self ensureExpansion:error];
70 require_quiet(success, errOut);
72 [self.expansion removeObjectForKey: (__bridge NSString*)(key)];
77 -(bool) setValueInExpansion:(CFStringRef) key value:(CFTypeRef) value err:(CFErrorRef *)error {
78 if (value == NULL) return [self clearValueFromExpansion:key err:error];
80 bool success = [self ensureExpansion:error];
81 require_quiet(success, errOut);
83 [self.expansion setObject:(__bridge id _Nonnull)(value) forKey:(__bridge NSString*)key];
89 -(bool) valueSetContainsValue:(CFStringRef) key value:(CFTypeRef) value
91 CFSetRef foundSet = asSet([self getValueFromExpansion:key err:NULL], NULL);
92 return foundSet && CFSetContainsValue(foundSet, value);
95 -(void) valueUnionWith:(CFStringRef) key valuesToUnion:(CFSetRef) valuesToUnion
97 CFMutableSetRef unionedSet = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, valuesToUnion);
98 CFSetRef foundSet = asSet([self getValueFromExpansion:key err:NULL], NULL);
100 CFSetUnion(unionedSet, foundSet);
102 [self setValueInExpansion:key value:unionedSet err:NULL];
103 CFReleaseNull(unionedSet);
106 -(void) valueSubtractFrom:(CFStringRef) key valuesToSubtract:(CFSetRef) valuesToSubtract
108 CFSetRef foundSet = asSet([self getValueFromExpansion:key err:NULL], NULL);
110 CFMutableSetRef subtractedSet = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, foundSet);
111 CFSetSubtract(subtractedSet, valuesToSubtract);
112 [self setValueInExpansion:key value:subtractedSet err:NULL];
113 CFReleaseNull(subtractedSet);
118 -(void) pendEnableViewSet:(CFSetRef) enabledViews
120 if(CFSetGetValue(enabledViews, kSOSViewKeychainV0) != NULL) secnotice("viewChange", "Warning, attempting to Add KeychainV0");
122 [self valueUnionWith:kSOSPendingEnableViewsToBeSetKey valuesToUnion:enabledViews];
123 [self valueSubtractFrom:kSOSPendingDisableViewsToBeSetKey valuesToSubtract:enabledViews];
127 -(bool) updateV2Dictionary:(SOSAccount*)account v2:(CFDictionaryRef) newV2Dict
129 if(!newV2Dict) return true;
131 [self setValueInExpansion:kSOSTestV2Settings value:newV2Dict err:NULL];
133 if (self.trustedCircle && self.fullPeerInfo
134 && SOSFullPeerInfoUpdateV2Dictionary(self.fullPeerInfo, newV2Dict, NULL)) {
135 [self modifyCircle:account.circle_transport err:NULL action:^(SOSCircleRef circle_to_change) {
136 secnotice("circleChange", "Calling SOSCircleUpdatePeerInfo for gestalt change");
137 return SOSCircleUpdatePeerInfo(circle_to_change, account.peerInfo);
147 -(bool) forEachRing:(RingNameBlock)block
150 __block bool changed = false;
151 __block CFStringRef ringname = NULL;
152 __block CFDataRef ringder = NULL;
153 __block SOSRingRef ring = NULL;
154 __block SOSRingRef newring = NULL;
155 __block CFDataRef newringder = NULL;
157 CFMutableDictionaryRef rings = [self getRings:NULL];
158 CFMutableDictionaryRef ringscopy = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
160 CFReleaseNull(ringscopy);
164 CFReleaseNull(ringscopy);
167 CFDictionaryForEach(rings, ^(const void *key, const void *value) {
168 ringname = (CFStringRef) key;
169 ringder = CFDataCreateCopy(kCFAllocatorDefault, (CFDataRef) value);
170 CFDictionaryAddValue(ringscopy, key, ringder);
171 ring = SOSRingCreateFromData(NULL, ringder);
172 newring = block(ringname, ring);
174 newringder = SOSRingCopyEncodedData(newring, NULL);
175 CFDictionaryReplaceValue(ringscopy, key, newringder);
176 CFReleaseNull(newringder);
180 CFReleaseNull(ringder);
181 CFReleaseNull(newring);
184 [self setRings:ringscopy];
188 CFReleaseNull(ringscopy);
192 -(bool) resetAllRings:(SOSAccount*)account err:(CFErrorRef *)error
194 __block bool retval = true;
195 CFMutableSetRef ringList = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
197 CFReleaseNull(ringList);
201 [self forEachRing: ^SOSRingRef(CFStringRef name, SOSRingRef ring) {
202 CFSetAddValue(ringList, name);
203 return NULL; // just using this to grab names.
206 CFSetForEach(ringList, ^(const void *value) {
207 CFStringRef ringName = (CFStringRef) value;
208 retval = retval && [self resetRing:account ringName:ringName err:error];
211 CFReleaseNull(ringList);
215 -(bool) resetAccountToEmpty:(SOSAccount*)account transport: (SOSCircleStorageTransport*)circleTransport err:(CFErrorRef*) error
218 __block bool result = true;
220 result &= [self resetAllRings:account err:error];
222 self.fullPeerInfo = nil;
224 self.departureCode = kSOSWithdrewMembership;
225 secnotice("resetToEmpty", "Reset Circle to empty by client request");
227 result &= [self modifyCircle:circleTransport err:error action:^bool(SOSCircleRef circle) {
228 result = SOSCircleResetToEmpty(circle, error);
233 secerror("error: %@", error ? *error : NULL);
238 -(void) setRings:(CFMutableDictionaryRef) newrings
240 [self.expansion setObject:(__bridge NSMutableDictionary*)newrings forKey:(kSOSRingKey)];
243 -(bool) checkForRings:(CFErrorRef*)error
245 __block bool retval = true;
246 CFMutableDictionaryRef rings = [self getRings:NULL];
247 if(rings && isDictionary(rings)) {
248 [self forEachRing:^SOSRingRef(CFStringRef ringname, SOSRingRef ring) {
250 if(!SOSRingIsStable(ring)) {
252 secnotice("ring", "Ring %@ not stable", ringname);
258 SOSCreateError(kSOSErrorNotReady, CFSTR("Rings not present"), NULL, error);
264 -(bool) setRing:(SOSRingRef) addRing ringName:(CFStringRef) ringName err:(CFErrorRef*)error
266 require_quiet(addRing, errOut);
267 CFMutableDictionaryRef rings = [self getRings:NULL];
268 require_action_quiet(rings, errOut, SOSCreateError(kSOSErrorNoRing, CFSTR("No Rings found"), NULL, error));
269 CFDataRef ringder = SOSRingCopyEncodedData(addRing, error);
270 require_quiet(ringder, errOut);
271 CFDictionarySetValue(rings, ringName, ringder);
272 CFReleaseNull(ringder);
278 static bool SOSAccountBackupSliceKeyBagNeedsFix(SOSAccount* account, SOSBackupSliceKeyBagRef bskb) {
280 if (SOSBSKBIsDirect(bskb) || account.backup_key == NULL)
283 CFSetRef peers = SOSBSKBGetPeers(bskb);
285 /* first scan for retired peers, and kick'em out!*/
286 SOSAccountIsPeerRetired(account, peers);
288 bool needsFix = true;
290 SOSPeerInfoRef myPeer = account.peerInfo;
292 SOSPeerInfoRef meInBag = (SOSPeerInfoRef) CFSetGetValue(peers, myPeer);
293 CFDataRef myBK = SOSPeerInfoCopyBackupKey(myPeer);
294 CFDataRef meInBagBK = SOSPeerInfoCopyBackupKey(meInBag);
295 needsFix = !(meInBag && CFEqualSafe(myBK,
298 CFReleaseNull(meInBagBK);
301 CFDataRef rkbg = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, account, NULL);
302 if(rkbg) needsFix |= !SOSBKSBPrefixedKeyIsInKeyBag(bskb, bskbRkbgPrefix, rkbg);
303 else needsFix |= SOSBSKBHasRecoveryKey(bskb); // if we don't have a recovery key - the bskb shouldn't
309 -(bool) handleUpdateRing:(SOSAccount*)account prospectiveRing:(SOSRingRef)prospectiveRing transport:(SOSKVSCircleStorageTransport*)circleTransport userPublicKey:(SecKeyRef)userPublic writeUpdate:(bool)writeUpdate err:(CFErrorRef *)error
312 bool haveOldRing = true;
314 const char * __unused localRemote = writeUpdate ? "local": "remote";
315 SOSFullPeerInfoRef fpi = self.fullPeerInfo;
316 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
317 CFStringRef peerID = SOSPeerInfoGetPeerID(pi);
318 bool peerActive = (fpi && pi && peerID && [self isInCircle:NULL]);
319 SOSRingRef newRing = NULL;
320 SOSRingRef oldRing = NULL;
322 secdebug("ringSigning", "start:[%s] %@", localRemote, prospectiveRing);
324 require_quiet(SOSAccountHasPublicKey(account, error), errOut);
326 require_action_quiet(prospectiveRing, errOut,
327 SOSCreateError(kSOSErrorIncompatibleCircle, CFSTR("No Ring to work with"), NULL, error));
329 require_action_quiet(SOSRingIsStable(prospectiveRing), errOut, SOSCreateError(kSOSErrorIncompatibleCircle, CFSTR("You give rings a bad name"), NULL, error));
331 // We should at least have a sane ring system in the account object
332 require_quiet([self checkForRings:error], errOut);
334 CFStringRef ringName = SOSRingGetName(prospectiveRing);
335 oldRing = [self copyRing:ringName err:NULL];
337 newRing = CFRetainSafe(prospectiveRing); // TODO: SOSAccountCloneRingWithRetirement(account, prospectiveRing, error);
339 ringAction_t ringAction = ignore;
341 bool userTrustedoldRing = true;
343 CFSetRef peers = SOSCircleCopyPeers(self.trustedCircle, kCFAllocatorDefault);
345 SecKeyRef oldKey = userPublic;
348 oldRing = CFRetainSafe(newRing);
351 SOSConcordanceStatus concstat = SOSRingConcordanceTrust(fpi, peers, oldRing, newRing, oldKey, userPublic, peerID, error);
352 CFReleaseNull(peers);
354 CFStringRef concStr = NULL;
356 case kSOSConcordanceTrusted:
357 ringAction = countersign;
358 concStr = CFSTR("Trusted");
360 case kSOSConcordanceGenOld:
361 ringAction = userTrustedoldRing ? revert : ignore;
362 concStr = CFSTR("Generation Old");
364 case kSOSConcordanceBadUserSig:
365 case kSOSConcordanceBadPeerSig:
366 ringAction = userTrustedoldRing ? revert : accept;
367 concStr = CFSTR("Bad Signature");
369 case kSOSConcordanceNoUserSig:
370 ringAction = userTrustedoldRing ? revert : accept;
371 concStr = CFSTR("No User Signature");
373 case kSOSConcordanceNoPeerSig:
374 ringAction = accept; // We might like this one eventually but don't countersign.
375 concStr = CFSTR("No trusted peer signature");
376 secnotice("signing", "##### No trusted peer signature found, accepting hoping for concordance later %@", newRing);
378 case kSOSConcordanceNoPeer:
380 concStr = CFSTR("No trusted peer left");
382 case kSOSConcordanceNoUserKey:
383 secerror("##### No User Public Key Available, this shouldn't ever happen!!!");
387 case kSOSConcordanceMissingMe:
388 case kSOSConcordanceImNotWorthy:
390 concStr = CFSTR("Incorrect membership for me");
392 case kSOSConcordanceInvalidMembership:
393 ringAction = userTrustedoldRing ? revert : ignore;
394 concStr = CFSTR("Invalid Ring Membership");
397 secerror("##### Bad Error Return from ConcordanceTrust");
404 secdebug("ringSigning", "Decided on action [%s] based on concordance state [%s] and [%s] circle.", actionstring[ringAction], concordstring[concstat], userTrustedoldRing ? "trusted" : "untrusted");
406 SOSRingRef ringToPush = NULL;
407 bool iWasInOldRing = peerID && SOSRingHasPeerID(oldRing, peerID);
408 bool iAmInNewRing = peerID && SOSRingHasPeerID(newRing, peerID);
409 bool ringIsBackup = SOSRingGetType(newRing) == kSOSRingBackup;
410 bool ringIsRecovery = SOSRingGetType(newRing) == kSOSRingRecovery;
412 if (ringIsBackup && peerActive) {
413 if (ringAction == accept || ringAction == countersign) {
414 CFErrorRef localError = NULL;
415 SOSBackupSliceKeyBagRef bskb = SOSRingCopyBackupSliceKeyBag(newRing, &localError);
418 secnotice("ringSigning", "Backup ring with no backup slice keybag (%@)", localError);
419 } else if (SOSAccountBackupSliceKeyBagNeedsFix(account, bskb)) {
422 CFReleaseSafe(localError);
426 if (ringAction == modify) {
427 CFErrorRef updateError = NULL;
428 [self setRing:newRing ringName:ringName err:error];
430 if(SOSAccountUpdateOurPeerInBackup(account, newRing, &updateError)) {
431 secdebug("signing", "Modified backup ring to include us");
433 secerror("Could not add ourselves to the backup: (%@)", updateError);
435 CFReleaseSafe(updateError);
437 // Fall through to normal modify handling.
441 if (ringIsRecovery && peerActive && (ringAction == modify)) {
442 [self setRing:newRing ringName:ringName err:error];
446 if (ringAction == modify) {
450 if (ringAction == leave) {
452 if ([self leaveRing:circleTransport ring:newRing err:error]){
453 ringToPush = newRing;
455 secdebug("ringSigning", "Can't leave ring %@", oldRing);
460 // We are not in this ring, but we need to update account with it, since we got it from cloud
465 if (ringAction == countersign) {
467 if (SOSRingPeerTrusted(newRing, fpi, NULL)) {
468 secdebug("ringSigning", "Already concur with: %@", newRing);
470 CFErrorRef signingError = NULL;
472 if (fpi && SOSRingConcordanceSign(newRing, fpi, &signingError)) {
473 ringToPush = newRing;
475 secerror("Failed to concordance sign, error: %@ Old: %@ New: %@", signingError, oldRing, newRing);
478 CFReleaseSafe(signingError);
481 secdebug("ringSigning", "Not countersigning, not in ring: %@", newRing);
486 if (ringAction == accept) {
487 if (iWasInOldRing && !iAmInNewRing) {
489 // Don't destroy evidence of other code determining reason for leaving.
490 //if(!SOSAccountHasLeft(account)) account.departure_code = kSOSMembershipRevoked;
491 // TODO: LeaveReason for rings
494 if (pi && SOSRingHasRejection(newRing, peerID)) {
495 // TODO: ReasonForLeaving for rings
496 SOSRingRemoveRejection(newRing, peerID);
499 [self setRing:newRing ringName:ringName err:error];
501 if (pi && account.accountKeyIsTrusted
502 && SOSRingHasApplicant(oldRing, peerID)
503 && SOSRingCountPeers(newRing) > 0
504 && !iAmInNewRing && !SOSRingHasApplicant(newRing, peerID)) {
505 // We weren't rejected (above would have set me to NULL.
506 // We were applying and we weren't accepted.
507 // Our application is declared lost, let us reapply.
509 if (SOSRingApply(newRing, userPublic, fpi, NULL))
510 if(peerActive) writeUpdate = true;
513 if (pi && SOSRingHasPeerID(oldRing, peerID)) {
514 [self cleanupRetirementTickets:account circle:self.trustedCircle time:RETIREMENT_FINALIZATION_SECONDS err:NULL];
518 account.circle_rings_retirements_need_attention = true;
521 ringToPush = newRing;
522 account.key_interests_need_updating = true;
526 * In the revert section we'll guard the KVS idea of circles by rejecting "bad" new rings
527 * and pushing our current view of the ring (oldRing). We'll only do this if we actually
528 * are a member of oldRing - never for an empty ring.
531 if (ringAction == revert) {
532 if(haveOldRing && peerActive && SOSRingHasPeerID(oldRing, peerID)) {
533 secdebug("ringSigning", "%@, Rejecting: %@ re-publishing %@", concStr, newRing, oldRing);
534 ringToPush = oldRing;
536 secdebug("ringSigning", "%@, Rejecting: %@ Have no old circle - would reset", concStr, newRing);
541 if (ringToPush != NULL) {
542 secdebug("ringSigning", "Pushing:[%s] %@", localRemote, ringToPush);
543 CFDataRef ringData = SOSRingCopyEncodedData(ringToPush, error);
545 success &= [circleTransport kvsRingPostRing:SOSRingGetName(ringToPush) ring:ringData err:error];
549 CFReleaseNull(ringData);
551 CFReleaseNull(oldRing);
552 CFReleaseNull(newRing);
555 CFReleaseNull(oldRing);
556 CFReleaseNull(newRing);
561 -(SOSRingRef) copyRing:(CFStringRef)ringName err:(CFErrorRef *)error
563 CFMutableDictionaryRef rings = [self getRings:error];
564 require_action_quiet(rings, errOut, SOSCreateError(kSOSErrorNoRing, CFSTR("No Rings found"), NULL, error));
565 CFTypeRef ringder = CFDictionaryGetValue(rings, ringName);
566 require_action_quiet(ringder, errOut, SOSCreateError(kSOSErrorNoRing, CFSTR("No Ring found"), NULL, error));
567 SOSRingRef ring = SOSRingCreateFromData(NULL, ringder);
568 return (SOSRingRef) ring;
574 -(CFMutableDictionaryRef) getRings:(CFErrorRef *)error
576 CFMutableDictionaryRef rings = (__bridge CFMutableDictionaryRef) [self.expansion objectForKey:kSOSRingKey];
578 [self addRingDictionary];
579 rings = [self getRings:error];
585 -(bool) resetRing:(SOSAccount*)account ringName:(CFStringRef) ringName err:(CFErrorRef *)error
589 SOSRingRef ring = [self copyRing:ringName err:error];
590 SOSRingRef newring = SOSRingCreate(ringName, NULL, SOSRingGetType(ring), error);
591 SOSRingGenerationCreateWithBaseline(newring, ring);
592 SOSBackupRingSetViews(newring, self.fullPeerInfo, SOSBackupRingGetViews(ring, NULL), error);
593 require_quiet(newring, errOut);
595 retval = SOSAccountUpdateRing(account, newring, error);
597 CFReleaseNull(newring);
601 -(bool) leaveRing:(SOSKVSCircleStorageTransport*)circle_transport ring:(SOSRingRef) ring err:(CFErrorRef*) error
603 SOSFullPeerInfoRef fpi = self.fullPeerInfo;
604 if(!fpi) return false;
605 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
606 CFStringRef peerID = SOSPeerInfoGetPeerID(pi);
608 CFErrorRef localError = NULL;
611 bool writeRing = false;
612 bool writePeerInfo = false;
614 if(SOSRingHasPeerID(ring, peerID)) {
615 writePeerInfo = true;
618 if(writePeerInfo || writeRing) {
619 SOSRingWithdraw(ring, NULL, fpi, error);
623 CFDataRef ring_data = SOSRingCopyEncodedData(ring, error);
626 [circle_transport kvsRingPostRing:SOSRingGetName(ring) ring:ring_data err:NULL];
628 CFReleaseNull(ring_data);
631 CFReleaseNull(localError);