6 #import "KeychainSettings.h"
7 #import <Security/Security.h>
8 #import <Security/OTControl.h>
9 #import <Security/CKKSControl.h>
11 #import <AppleAccount/AppleAccount.h>
12 #import <AppleAccount/AppleAccount_Private.h>
13 #import <AuthKit/AuthKit.h>
14 #import <AuthKit/AuthKit_Private.h>
15 #import <AuthKit/AKDeviceListDeltaMessagePayload.h>
16 #import <Foundation/NSDistributedNotificationCenter.h>
18 #import <AppleAccount/ACAccount+AppleAccount.h>
19 #import <Accounts/ACAccountStore.h>
20 #import <UIKit/UIKit.h>
21 #import <utilities/debugging.h>
23 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
26 @interface KeychainSettings ()
27 @property (strong) OTControl* control;
28 @property (strong) NSDictionary* status;
29 @property (strong) NSString *statusError;
31 + (OTControl *)sharedOTControl;
32 + (CKKSControl *)sharedCKKSControl;
35 @implementation KeychainSettings
38 if ((self = [super init]) != nil) {
39 [self updateCircleStatus];
44 + (OTControl *)sharedOTControl
46 static OTControl *control;
47 static dispatch_once_t onceToken;
48 dispatch_once(&onceToken, ^{
51 control = [OTControl controlObject:true error:&error];
52 if(error || !control) {
53 os_log(OS_LOG_DEFAULT, "no OTControl, failed: %@", error);
59 + (CKKSControl *)sharedCKKSControl
61 static CKKSControl *control;
62 static dispatch_once_t onceToken;
63 dispatch_once(&onceToken, ^{
66 // Use a synchronous control object
67 control = [CKKSControl CKKSControlObject:true error:&error];
68 if(error || !control) {
69 os_log(OS_LOG_DEFAULT, "no CKKSControl, failed: %@", error);
77 - (void)updateCircleStatus
79 [[KeychainSettings sharedOTControl] status:NULL
80 context:OTDefaultContext
81 reply:^(NSDictionary* result, NSError* _Nullable error) {
82 @synchronized (self) {
83 self.statusError = nil;
86 self.statusError = [error description];
89 self.statusError = nil;
95 - (NSArray *)specifiers {
97 _specifiers = [self loadSpecifiersFromPlistName:@"KeychainSettings" target:self];
102 - (NSString *)octagonStatusString:(NSString *)key
104 __block id status = nil;
105 @synchronized (self) {
107 id value = self.status[key];
108 if ([value isKindOfClass:[NSString class]]) {
110 } else if ([value isKindOfClass:[NSNumber class]]) {
111 NSNumber *number = value;
112 status = [number stringValue];
114 status = [key description];
117 if (status == nil && self.statusError) {
118 status = self.statusError;
128 - (NSNumber *)octagonStatusNumber:(NSString *)key
130 __block NSNumber *status = nil;
131 @synchronized (self) {
132 NSNumber *value = self.status[key];
133 if ([value isKindOfClass:[NSNumber class]]) {
142 - (NSString *)octagonStateMachine:(PSSpecifier *)specifier
144 return [self octagonStatusString:@"state"];
147 - (NSString *)prettyifyProtobufString:(NSString *)string
149 return [string.capitalizedString stringByReplacingOccurrencesOfString:@"_" withString:@" "];
152 - (NSString *)octagonTrustState:(PSSpecifier *)specifier
154 return [self prettyifyProtobufString:OTAccountMetadataClassC_TrustStateAsString([[self octagonStatusNumber:@"memoizedTrustState"] intValue])];
157 - (NSString *)octagonAccountState:(PSSpecifier *)specifier
159 return [self prettyifyProtobufString:OTAccountMetadataClassC_AccountStateAsString([[self octagonStatusNumber:@"memoizedAccountState"] intValue])];
162 - (NSString *)ckksAggregateStatus:(PSSpecifier *)specifier
164 __block NSString *status = NULL;
165 __block bool foundNonReady = false;
166 __block bool foundOne = false;
168 void (^replyBlock)(NSArray<NSDictionary *> * _Nullable result, NSError * _Nullable error) = ^(NSArray<NSDictionary *>* result, NSError* _Nullable error){
170 status = [NSString stringWithFormat:@"error: %@", error];
172 for(NSDictionary* view in result) {
173 NSString* viewName = view[@"view"];
174 if (viewName == NULL || [viewName isEqualToString:@"global"]) {
178 NSString *viewStatus = view[@"keystate"];
180 if (![viewStatus isKindOfClass:[NSString class]] ||
181 !([viewStatus isEqualToString:@"ready"] || [viewStatus isEqualToString:@"readypendingunlock"])) {
182 foundNonReady = true;
187 [[KeychainSettings sharedCKKSControl] rpcFastStatus:NULL reply: replyBlock];
190 /* something already provided status */
191 } else if (foundNonReady) {
192 status = @"not ready";
193 } else if (foundOne) {
196 status = @"no status";
202 - (NSString* _Nullable)primaryiCloudAccountAltDSID
204 ACAccountStore *store = [[ACAccountStore alloc] init];
205 ACAccount* primaryAccount = [store aa_primaryAppleAccount];
206 if(!primaryAccount) {
210 return [primaryAccount aa_altDSID];
213 - (void) resetOctagon:(PSSpecifier *)specifier
215 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
217 [[KeychainSettings sharedOTControl] resetAndEstablish:nil
218 context:OTDefaultContext
219 altDSID:[self primaryiCloudAccountAltDSID]
220 resetReason:CuttlefishResetReasonUserInitiatedReset
221 reply:^(NSError * _Nullable error) {
225 dispatch_semaphore_signal(sema);
228 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60)) != 0) {
229 secerror("timed out attempting to reset octagon");
236 @implementation KeychainSettingsOctagonPeers
238 - (void)addPeerIDs:(NSArray<NSString*>*)peerIDs
239 peers:(NSMutableDictionary<NSString*, NSDictionary*>*)peers
240 toSpecifiers:(NSMutableArray*)specifiers
242 NSMutableArray<NSDictionary*>* selectedPeers = [NSMutableArray array];
244 for (NSString *peerID in peerIDs) {
245 NSDictionary *peer = peers[peerID];
247 [selectedPeers addObject:peer];
251 [selectedPeers sortUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
252 return [obj1[@"stableInfo"][@"device_name"] compare: obj2[@"stableInfo"][@"device_name"]];
255 for (NSDictionary *peer in selectedPeers) {
256 PSSpecifier* spec = [PSSpecifier preferenceSpecifierNamed:peer[@"stableInfo"][@"device_name"] target:self set:nil get:nil detail:nil cell:PSTitleValueCell edit:nil];
258 [specifiers addObject:spec];
263 - (PSSpecifier *)groupSpecifier:(NSString *)name
265 return [PSSpecifier preferenceSpecifierNamed:name target:self set:nil get:nil detail:nil cell:PSGroupCell edit:nil];
268 - (NSArray *)specifiers {
271 NSMutableArray* specifiers = [NSMutableArray array];
273 [specifiers addObjectsFromArray: [self loadSpecifiersFromPlistName:@"KeychainSettingsOctagonPeers" target:self]];
275 void (^replyBlock)(NSDictionary* result, NSError* _Nullable error) = ^(NSDictionary* result, NSError* _Nullable error) {
276 NSDictionary* contextDump = result[@"contextDump"];
278 // Make it easy to find peer information
279 NSMutableDictionary<NSString*, NSDictionary*>* peers = [NSMutableDictionary dictionary];
280 NSMutableArray<NSString*>* allPeerIDs = [NSMutableArray array];
281 for(NSDictionary* peerInformation in contextDump[@"peers"]) {
282 NSString* peerID = peerInformation[@"peerID"];
284 peers[peerID] = peerInformation;
285 [allPeerIDs addObject:peerID];
289 NSDictionary* egoInformation = contextDump[@"self"];
290 NSString* egoPeerID = egoInformation[@"peerID"];
291 NSDictionary* egoDynamicInfo = egoInformation[@"dynamicInfo"];
293 if(egoPeerID && egoInformation && egoDynamicInfo) {
295 peers[egoPeerID] = egoInformation;
297 [specifiers addObject:[self groupSpecifier:@"Me"]];
298 [self addPeerIDs:@[egoPeerID] peers:peers toSpecifiers:specifiers];
300 NSArray<NSString*>* included = egoDynamicInfo[@"included"];
301 [specifiers addObject:[self groupSpecifier:@"Included"]];
302 [self addPeerIDs:included peers:peers toSpecifiers:specifiers];
303 [peers removeObjectsForKeys:included];
305 NSArray<NSString*>* excluded = egoDynamicInfo[@"excluded"];
306 [specifiers addObject:[self groupSpecifier:@"Excluded"]];
307 [self addPeerIDs:excluded peers:peers toSpecifiers:specifiers];
308 [peers removeObjectsForKeys:excluded];
311 [specifiers addObject:[self groupSpecifier:@"Me (untrusted)"]];
315 [specifiers addObject:[self groupSpecifier:@"Other peers"]];
316 [self addPeerIDs:peers.allKeys peers:peers toSpecifiers:specifiers];
321 [[KeychainSettings sharedOTControl] status:NULL context:OTDefaultContext reply:replyBlock];
323 _specifiers = specifiers;
331 @implementation KeychainSettingsCKKSViews
333 - (NSArray *)specifiers {
336 NSMutableArray* specifiers = [NSMutableArray array];
338 [specifiers addObjectsFromArray: [self loadSpecifiersFromPlistName:@"KeychainSettingsCKKSViews" target:self]];
340 void (^replyBlock)(NSArray<NSDictionary *> * _Nullable result, NSError * _Nullable error) = ^(NSArray<NSDictionary *>* result, NSError* _Nullable error){
342 NSMutableArray<NSDictionary*>* views = [NSMutableArray array];
343 for(NSDictionary* view in result) {
344 NSString* viewName = view[@"view"];
345 if (viewName == NULL || [viewName isEqualToString:@"global"]) {
349 [views addObject:view];
352 [views sortUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
353 return [obj1[@"view"] compare: obj2[@"view"]];
356 for (NSDictionary *view in views) {
357 NSString *description = [NSString stringWithFormat:@"%@ - %@", view[@"view"], view[@"keystate"]];
358 PSSpecifier* spec = [PSSpecifier preferenceSpecifierNamed:description target:self set:nil get:nil detail:nil cell:PSTitleValueCell edit:nil];
360 [specifiers addObject:spec];
363 [[KeychainSettings sharedCKKSControl] rpcFastStatus:NULL reply: replyBlock];
365 _specifiers = specifiers;