7 #import <Foundation/Foundation.h>
8 #import "SOSAuthKitHelpers.h"
9 #import <utilities/debugging.h>
10 #import "keychain/SecureObjectSync/SOSAccount.h"
11 #import "keychain/SecureObjectSync/SOSAccountPriv.h"
12 #import "keychain/SecureObjectSync/SOSFullPeerInfo.h"
13 #import "keychain/SecureObjectSync/SOSPeerInfoV2.h"
14 #import "keychain/SecureObjectSync/SOSPeerInfoPriv.h"
16 #if !TARGET_OS_BRIDGE && !TARGET_OS_SIMULATOR && __OBJC2__
17 #import <AppleAccount/AppleAccount_Private.h>
18 #import <AuthKit/AuthKit.h>
19 #import <AuthKit/AuthKit_Private.h>
20 #import <SoftLinking/SoftLinking.h>
24 SOFT_LINK_FRAMEWORK(PrivateFrameworks, AuthKit);
25 SOFT_LINK_FRAMEWORK(Frameworks, Accounts);
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wstrict-prototypes"
29 SOFT_LINK_CLASS(AuthKit, AKAccountManager);
30 SOFT_LINK_CLASS(AuthKit, AKAnisetteProvisioningController);
31 SOFT_LINK_CLASS(AuthKit, AKAppleIDAuthenticationController);
32 SOFT_LINK_CLASS(AuthKit, AKDeviceListRequestContext);
33 SOFT_LINK_CLASS(Accounts, ACAccountStore);
34 SOFT_LINK_CONSTANT(AuthKit, AKServiceNameiCloud, const NSString *);
35 #pragma clang diagnostic pop
37 static void *accountsFramework = NULL;
38 static void *appleAccountFramework = NULL;
41 initAccountsFramework(void) {
42 static dispatch_once_t onceToken;
43 dispatch_once(&onceToken, ^{
44 accountsFramework = dlopen("/System/Library/Frameworks/Accounts.framework/Accounts", RTLD_LAZY);
45 appleAccountFramework = dlopen("/System/Library/PrivateFrameworks/AppleAccount.framework/AppleAccount", RTLD_LAZY);
49 @implementation SOSAuthKitHelpers
51 @class SOSAuthKitHelpers;
53 + (NSString *) machineID {
55 NSString *retval = nil;
56 secnotice("sosauthkit", "Entering machineID");
58 AKAnisetteProvisioningController *anisetteController = [getAKAnisetteProvisioningControllerClass() new];
59 if(anisetteController) {
60 AKAnisetteData *anisetteData = [anisetteController anisetteDataWithError:&error];
62 retval = [anisetteData.machineID copy];
64 secnotice("sosauthkit", "machineID is %@", retval);
66 secnotice("sosauthkit", "Failed to get machineID");
69 secnotice("sosauthkit", "can't get mID: %@", error);
72 secnotice("sosauthkit", "can't get controller");
77 static ACAccount *GetPrimaryAccount(void) {
78 ACAccount *primaryAccount;
80 initAccountsFramework();
82 ACAccountStore *store = [getACAccountStoreClass() new];
85 secnotice("sosauthkit", "can't get store");
89 primaryAccount = [store aa_primaryAppleAccount];
91 return primaryAccount;
95 + (void)activeMIDs:(void(^_Nonnull)(NSSet <SOSTrustedDeviceAttributes *> *activeMIDs, NSError *error))complete {
96 ACAccount *primaryAccount;
97 AKDeviceListRequestContext *context;
99 primaryAccount = GetPrimaryAccount();
101 if(!primaryAccount) {
102 secnotice("sosauthkit", "can't get account");
103 complete(NULL, [NSError errorWithDomain:(__bridge NSString *)kSecErrorDomain code:errSecParam userInfo:@{NSLocalizedDescriptionKey : @"no primary account"}]);
107 context = [getAKDeviceListRequestContextClass() new];
108 if (context == NULL) {
109 complete(NULL, [NSError errorWithDomain:(__bridge NSString *)kSecErrorDomain code:errSecParam userInfo:@{NSLocalizedDescriptionKey : @"can't get AKDeviceListRequestContextClass"}]);
112 context.altDSID = primaryAccount.aa_altDSID;
113 context.services = @[ getAKServiceNameiCloud() ];
115 // -[AKAppleIDAuthenticationController fetchDeviceListWithContext:error:] is not exposed, use a semaphore
116 AKAppleIDAuthenticationController *authController = [getAKAppleIDAuthenticationControllerClass() new];
117 if(!authController) {
118 complete(NULL, [NSError errorWithDomain:(__bridge NSString *)kSecErrorDomain code:errSecParam userInfo:@{NSLocalizedDescriptionKey : @"can't get authController"}]);
122 [authController fetchDeviceListWithContext:context completion:^(NSArray<AKRemoteDevice *> *deviceList, NSError *error) {
123 NSMutableSet *mids = [NSMutableSet new];
124 for(AKRemoteDevice *akdev in deviceList) {
125 SOSTrustedDeviceAttributes *newdev = [SOSTrustedDeviceAttributes new];
126 newdev.machineID = akdev.machineId;
127 newdev.serialNumber = akdev.serialNumber;
128 [mids addObject:newdev];
130 if([mids count] == 0) {
131 secnotice("sosauthkit", "found no devices in account");
134 complete(mids, error);
138 + (bool) peerinfoHasMID: (SOSAccount *) account {
139 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(account.fullPeerInfo);
140 if(!pi) return true; // if there's no PI then just say "we don't need one"
141 return SOSPeerInfoV2DictionaryHasString(pi, sMachineIDKey);
146 + (bool) updateMIDInPeerInfo: (SOSAccount *) account {
147 NSString *mid = [SOSAuthKitHelpers machineID];
148 if(!mid) return true;
149 CFErrorRef error = NULL;
150 SOSAccountSetValue(account, sMachineIDKey, (__bridge CFStringRef)mid, &error);
151 bool peerUpdated = SOSAccountUpdatePeerInfoAndPush(account, CFSTR("Add Machine ID"), &error, ^bool(SOSPeerInfoRef pi, CFErrorRef *error) {
152 if(SOSPeerInfoV2DictionaryHasString(pi, sMachineIDKey)) {
155 secnotice("sosauthkit", "Setting PeerInfo MID to %@", mid);
156 SOSPeerInfoV2DictionarySetValue(pi, sMachineIDKey, (__bridge CFStringRef)mid);
160 secnotice("sosauthkit", "Failed to record MID in PeerInfo: %@", error);
162 CFReleaseNull(error);
167 + (bool) accountIsHSA2 {
170 ACAccount *primaryAccount = GetPrimaryAccount();
171 AKAccountManager *manager = [getAKAccountManagerClass() new];
173 if(manager && primaryAccount) {
174 ACAccount *account = [manager authKitAccountWithAltDSID:[manager altDSIDForAccount:primaryAccount]];
175 AKAppleIDSecurityLevel securityLevel = [manager securityLevelForAccount:account];
176 if(securityLevel == AKAppleIDSecurityLevelHSA2) {
179 secnotice("sosauthkit", "Security level is %lu", (unsigned long)securityLevel);
181 secnotice("sosauthkit", "Account %s HSA2", (hsa2) ? "is": "isn't" );
183 secnotice("sosauthkit", "Failed to get manager");
189 -(id) initWithActiveMIDS: (NSSet <SOSTrustedDeviceAttributes *> *) theMidList
191 if ((self = [super init])) {
192 NSMutableSet *MmachineIDs = [[NSMutableSet alloc] init];
193 NSMutableSet *MserialNumbers = [[NSMutableSet alloc] init];
194 _machineIDs = [[NSSet alloc] init];
195 _serialNumbers = [[NSSet alloc] init];
197 if(!theMidList) return nil;
198 _midList = theMidList;
200 for(SOSTrustedDeviceAttributes *dev in _midList) {
202 [MmachineIDs addObject:dev.machineID];
204 if(dev.serialNumber) {
205 [MserialNumbers addObject:dev.serialNumber];
209 _machineIDs = MmachineIDs;
210 _serialNumbers = MserialNumbers;
215 // if the ID passed in is null, the peer doesn't have one, we'll say true - we can't tell from the list
216 - (bool) midIsValidInList: (NSString *) machineId {
217 return (machineId) ? [_machineIDs containsObject:machineId]: true;
220 - (bool) serialIsValidInList: (NSString *) serialNumber {
221 return (serialNumber) ? [_serialNumbers containsObject:serialNumber]: true;
225 return [ _machineIDs count ] > 0;
231 @implementation SOSAuthKitHelpers
233 @class SOSAuthKitHelpers;
235 + (NSString *) machineID {
239 + (void)activeMIDs:(void(^_Nonnull)(NSSet<SOSTrustedDeviceAttributes*> *activeMIDs, NSError *error))complete {
243 + (bool) updateMIDInPeerInfo: (SOSAccount *) account {
247 + (bool) peerinfoHasMID: (SOSAccount *) account {
251 + (bool) accountIsHSA2 {
255 - (id _Nullable) initWithActiveMIDS: (NSSet *_Nullable) theMidList {
259 - (bool) midIsValidInList: (NSString *_Nullable) machineId {
263 - (bool) serialIsValidInList: (NSString *_Nullable) serialNumber {