2 // KeychainSyncAccountNotification.m
6 #import "KeychainSyncAccountNotification.h"
7 #import <Accounts/Accounts.h>
8 #import <Accounts/Accounts_Private.h>
9 #import <AppleAccount/ACAccount+AppleAccount.h>
10 #import <AccountsDaemon/ACDAccountStore.h>
11 #import <Security/SecureObjectSync/SOSCloudCircle.h>
12 #import <AuthKit/AuthKit.h>
13 #import <AuthKit/AuthKit_Private.h>
15 #import <keychain/ot/OTControl.h>
16 #include <utilities/SecCFRelease.h>
18 #import "utilities/debugging.h"
21 @implementation KeychainSyncAccountNotification
23 - (bool)accountIsPrimary:(ACAccount *)account
25 return [account aa_isAccountClass:AAAccountClassPrimary];
28 // this is where we initialize SOS and OT for account sign-in
29 // in the future we may bring this logic over to KeychainDataclassOwner and delete KeychainSyncAccountNotification, but accounts people say that's a change that today would require coordination across multiple teams
30 // was asked to file this radar for accounts: <rdar://problem/40176124> Invoke DataclassOwner when enabling or signing into an account
31 - (void)account:(ACAccount *)account didChangeWithType:(ACAccountChangeType)changeType inStore:(ACDAccountStore *)store oldAccount:(ACAccount *)oldAccount {
33 if((changeType == kACAccountChangeTypeAdded || changeType == kACAccountChangeTypeModified) &&
34 [account.accountType.identifier isEqualToString: ACAccountTypeIdentifierAppleAccount] &&
35 [self accountIsPrimary:account]) {
38 if(OctagonIsEnabled()){
39 NSString* altDSID = [account aa_altDSID];
40 secnotice("octagon-account", "Received an primary Apple account modification (altDSID %@)", altDSID);
42 __block NSError* error = nil;
44 // Use asynchronous XPC here for speed and just hope it works
45 OTControl* otcontrol = [OTControl controlObject:false error:&error];
47 if (nil == otcontrol) {
48 secerror("octagon-account: Failed to get OTControl: %@", error.localizedDescription);
50 [otcontrol signIn:altDSID container:nil context:OTDefaultContext reply:^(NSError * _Nullable signedInError) {
51 // take a retain on otcontrol so it won't invalidate the connection
55 secerror("octagon-account: error signing in: %s", [[signedInError description] UTF8String]);
57 secnotice("octagon-account", "account now signed in for octagon operation");
62 secerror("Octagon not enabled; not signing in");
67 // If there is any change to any AuthKit account's security level, notify octagon
70 if([account.accountType.identifier isEqualToString: ACAccountTypeIdentifierIDMS]) {
71 NSString* altDSID = [account aa_altDSID];;
72 secnotice("octagon-authkit", "Received an IDMS account modification (altDSID: %@)", altDSID);
74 AKAccountManager *manager = [AKAccountManager sharedInstance];
76 AKAppleIDSecurityLevel oldSecurityLevel = [manager securityLevelForAccount:oldAccount];
77 AKAppleIDSecurityLevel newSecurityLevel = [manager securityLevelForAccount:account];
79 if(oldSecurityLevel != newSecurityLevel) {
80 secnotice("octagon-authkit", "IDMS security level has now moved to %ld for altDSID %@", (unsigned long)newSecurityLevel, altDSID);
82 __block NSError* error = nil;
83 // Use an asynchronous otcontrol for Speed But Not Necessarily Correctness
84 OTControl* otcontrol = [OTControl controlObject:false error:&error];
85 if(!otcontrol || error) {
86 secerror("octagon-authkit: Failed to get OTControl: %@", error);
88 [otcontrol notifyIDMSTrustLevelChangeForContainer:nil context:OTDefaultContext reply:^(NSError * _Nullable idmsError) {
89 // take a retain on otcontrol so it won't invalidate the connection
93 secerror("octagon-authkit: error with idms trust level change in: %s", [[idmsError description] UTF8String]);
95 secnotice("octagon-authkit", "informed octagon of IDMS trust level change");
101 secnotice("octagon-authkit", "No change to IDMS security level (%lu) for altDSID %@", (unsigned long)newSecurityLevel, altDSID);
106 if ((changeType == kACAccountChangeTypeDeleted) && [oldAccount.accountType.identifier isEqualToString:ACAccountTypeIdentifierAppleAccount]) {
107 NSString* altDSID = [oldAccount aa_altDSID];
108 secnotice("octagon-account", "Received an Apple account deletion (altDSID %@)", altDSID);
110 NSString *accountIdentifier = oldAccount.identifier;
111 NSString *username = oldAccount.username;
113 if(accountIdentifier != NULL && username !=NULL) {
114 if ([self accountIsPrimary:oldAccount]) {
115 CFErrorRef removalError = NULL;
117 secinfo("accounts", "Performing SOS circle credential removal for account %@: %@", accountIdentifier, username);
119 if (!SOSCCLoggedOutOfAccount(&removalError)) {
120 secerror("Account %@ could not leave the SOS circle: %@", accountIdentifier, removalError);
124 if(OctagonIsEnabled()){
125 __block NSError* error = nil;
127 // Use an asynchronous control for Speed
128 OTControl* otcontrol = [OTControl controlObject:false error:&error];
130 if (nil == otcontrol) {
131 secerror("octagon-account: Failed to get OTControl: %@", error.localizedDescription);
133 [otcontrol signOut:nil context:OTDefaultContext reply:^(NSError * _Nullable signedInError) {
134 // take a retain on otcontrol so it won't invalidate the connection
138 secerror("octagon-account: error signing out: %s", [[signedInError description] UTF8String]);
140 secnotice("octagon-account", "signed out of octagon trust");
145 secerror("Octagon not enabled; not signing out");