]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTModifyUserControllableViewStatusOperation.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / ot / OTModifyUserControllableViewStatusOperation.m
1 /*
2 * Copyright (c) 2020 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if OCTAGON
25
26 #import <TrustedPeers/TrustedPeers.h>
27 #import "keychain/ckks/CKKSAnalytics.h"
28 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
29 #import "keychain/ot/OTModifyUserControllableViewStatusOperation.h"
30 #import "keychain/ot/OTStates.h"
31 #import "keychain/ot/ObjCImprovements.h"
32 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
33
34 @interface OTModifyUserControllableViewStatusOperation ()
35 @property OTOperationDependencies* deps;
36
37 @property OctagonState* peerMissingState;
38
39 @property TPPBPeerStableInfo_UserControllableViewStatus intendedViewStatus;
40 @end
41
42 @implementation OTModifyUserControllableViewStatusOperation
43 @synthesize intendedState = _intendedState;
44 @synthesize nextState = _nextState;
45
46 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
47 intendedViewStatus:(TPPBPeerStableInfo_UserControllableViewStatus)intendedViewStatus
48 intendedState:(OctagonState*)intendedState
49 peerMissingState:(OctagonState*)peerMissingState
50 errorState:(OctagonState*)errorState
51 {
52 if ((self = [super init])) {
53 _deps = dependencies;
54
55 _intendedViewStatus = intendedViewStatus;
56
57 _intendedState = intendedState;
58 _peerMissingState = peerMissingState;
59 _nextState = errorState;
60 }
61 return self;
62 }
63
64 - (void)groupStart
65 {
66
67 if(self.intendedViewStatus == TPPBPeerStableInfo_UserControllableViewStatus_FOLLOWING) {
68 #if TARGET_OS_WATCH || TARGET_OS_TV
69 // Watches and TVs want to be able to set the FOLLOWING state
70 [self performWithStatus:self.intendedViewStatus];
71 #else
72 // For other platforms, we want to determine the actual state by asking
73 WEAKIFY(self);
74
75 // Should we ask SOS? Or Octagon?
76 if(self.deps.sosAdapter.sosEnabled) {
77 NSError* error = nil;
78 BOOL safariViewEnabled = [self.deps.sosAdapter safariViewSyncingEnabled:&error];
79
80 if(error) {
81 secerror("octagon-ckks: Unable to fetch SOS Safari view status: %@", error);
82 self.error = error;
83 return;
84 }
85
86 secnotice("octagon-ckks", "Currently SOS believes the safari view is '%@'", safariViewEnabled ? @"enabled" : @"disabled");
87
88 TPPBPeerStableInfo_UserControllableViewStatus status = safariViewEnabled ?
89 TPPBPeerStableInfo_UserControllableViewStatus_ENABLED :
90 TPPBPeerStableInfo_UserControllableViewStatus_DISABLED;
91
92 [self performWithStatus:status];
93 return;
94 }
95
96 secnotice("octagon-ckks", "Determining peers' user-controllable views policy");
97
98 [self.deps.cuttlefishXPCWrapper fetchCurrentPolicyWithContainer:self.deps.containerName
99 context:self.deps.contextID
100 modelIDOverride:nil
101 reply:^(TPSyncingPolicy* _Nullable syncingPolicy,
102 TPPBPeerStableInfo_UserControllableViewStatus userControllableViewStatusOfPeers,
103 NSError* _Nullable error) {
104 STRONGIFY(self);
105
106 if(error) {
107 secnotice("octagon-ckks", "Determining peers' user-controllable views policy failed: %@", error);
108 self.error = error;
109 return;
110 }
111
112 secnotice("octagon-ckks", "Retrieved peers' user-controllable views policy as: %@",
113 TPPBPeerStableInfo_UserControllableViewStatusAsString(userControllableViewStatusOfPeers));
114
115 [self performWithStatus:userControllableViewStatusOfPeers];
116 return;
117 }];
118 #endif
119
120 } else {
121 [self performWithStatus:self.intendedViewStatus];
122 }
123 }
124
125 - (void)performWithStatus:(TPPBPeerStableInfo_UserControllableViewStatus)intendedViewStatus
126 {
127 WEAKIFY(self);
128
129 secnotice("octagon-ckks", "Setting user-controllable views to %@", TPPBPeerStableInfo_UserControllableViewStatusAsString(self.intendedViewStatus));
130
131 [self.deps.cuttlefishXPCWrapper updateWithContainer:self.deps.containerName
132 context:self.deps.contextID
133 deviceName:nil
134 serialNumber:nil
135 osVersion:nil
136 policyVersion:nil
137 policySecrets:nil
138 syncUserControllableViews:[NSNumber numberWithInt:intendedViewStatus]
139 reply:^(TrustedPeersHelperPeerState* peerState, TPSyncingPolicy* syncingPolicy, NSError* error) {
140 STRONGIFY(self);
141 if(error || !syncingPolicy) {
142 secerror("octagon-ckks: setting user-controllable views status errored: %@", error);
143 self.error = error;
144
145 if([self.deps.lockStateTracker isLockedError:self.error]) {
146 secnotice("octagon-ckks", "Updating user-controllable view status failed because of lock state, will retry once unlocked: %@", self.error);
147 OctagonPendingFlag* pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:OctagonFlagAttemptUserControllableViewStatusUpgrade
148 conditions:OctagonPendingConditionsDeviceUnlocked];
149
150 [self.deps.flagHandler handlePendingFlag:pendingFlag];
151 }
152 if(peerState.peerStatus & (TPPeerStatusExcluded | TPPeerStatusUnknown)) {
153 secnotice("octagon-ckks", "Updating user-controllable view status failed because our self peer is excluded or missing");
154 self.nextState = self.peerMissingState;
155 }
156 return;
157 }
158
159 secnotice("octagon-ckks", "Received syncing policy %@ with view list: %@", syncingPolicy, syncingPolicy.viewList);
160
161 NSError* stateError = nil;
162 [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC * _Nullable(OTAccountMetadataClassC * _Nonnull metadata) {
163 [metadata setTPSyncingPolicy:syncingPolicy];
164 return metadata;
165 } error:&stateError];
166
167 if(stateError) {
168 secerror("octagon: failed to save policy+views: %@", stateError);
169 self.error = stateError;
170 return;
171 }
172
173 [self.deps.viewManager setCurrentSyncingPolicy:syncingPolicy];
174
175 self.nextState = self.intendedState;
176 }];
177 }
178
179 @end
180
181 #endif // OCTAGON