]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSControl.m
Security-58286.51.6.tar.gz
[apple/security.git] / keychain / ckks / CKKSControl.m
1 /*
2 * Copyright (c) 2017 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 __OBJC2__
25
26 #import <Foundation/NSXPCConnection_Private.h>
27 #import <xpc/xpc.h>
28
29 #import <Security/SecItemPriv.h>
30
31 #import "keychain/ckks/CKKSControl.h"
32 #import "keychain/ckks/CKKSControlProtocol.h"
33 #import "keychain/ckks/CKKSControlServer.h"
34
35 #include <security_utilities/debugging.h>
36
37 @interface CKKSControl ()
38 @property xpc_endpoint_t endpoint;
39 @property NSXPCConnection *connection;
40 @end
41
42 @implementation CKKSControl
43
44 - (instancetype)initWithConnection:(NSXPCConnection*)connection {
45 if(self = [super init]) {
46 _connection = connection;
47 }
48 return self;
49 }
50
51 - (void)rpcStatus:(NSString*)viewName reply:(void(^)(NSArray<NSDictionary*>* result, NSError* error)) reply {
52 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
53 reply(nil, error);
54
55 }] rpcStatus:viewName reply:^(NSArray<NSDictionary*>* result, NSError* error){
56 reply(result, error);
57 }];
58 }
59
60 - (void)rpcResetLocal:(NSString*)viewName reply:(void(^)(NSError* error))reply {
61 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
62 reply(error);
63 }] rpcResetLocal:viewName reply:^(NSError* error){
64 reply(error);
65 }];
66 }
67
68 - (void)rpcResetCloudKit:(NSString*)viewName reply:(void(^)(NSError* error))reply {
69 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
70 reply(error);
71 }] rpcResetCloudKit:viewName reply:^(NSError* error){
72 reply(error);
73 }];
74 }
75
76 - (void)rpcResync:(NSString*)viewName reply:(void(^)(NSError* error))reply {
77 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
78 reply(error);
79 }] rpcResync:viewName reply:^(NSError* error){
80 reply(error);
81 }];
82 }
83 - (void)rpcFetchAndProcessChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
84 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
85 reply(error);
86 }] rpcFetchAndProcessChanges:viewName reply:^(NSError* error){
87 reply(error);
88 }];
89 }
90 - (void)rpcFetchAndProcessClassAChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
91 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
92 reply(error);
93 }] rpcFetchAndProcessClassAChanges:viewName reply:^(NSError* error){
94 reply(error);
95 }];
96 }
97 - (void)rpcPushOutgoingChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
98 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
99 reply(error);
100 }] rpcPushOutgoingChanges:viewName reply:^(NSError* error){
101 reply(error);
102 }];
103 }
104
105 - (void)rpcPerformanceCounters:(void(^)(NSDictionary <NSString *,NSNumber *> *,NSError*))reply {
106 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
107 reply(nil, error);
108 }] performanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
109 reply(counters, nil);
110 }];
111 }
112
113 - (void)rpcGetCKDeviceIDWithReply:(void (^)(NSString *))reply {
114 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
115 reply(nil);
116 }] rpcGetCKDeviceIDWithReply:^(NSString *ckdeviceID) {
117 reply(ckdeviceID);
118 }];
119 }
120
121 - (void)rpcTLKMissing:(NSString*)viewName reply:(void(^)(bool missing))reply {
122 [self rpcStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
123 bool missing = false;
124
125 // Until PCS fixes [<rdar://problem/35103941> PCS: Remove PCS's use of CKKSControlProtocol], we can't add things to the protocol
126 // Use this hack
127 for(NSDictionary* result in results) {
128 NSString* name = result[@"view"];
129 NSString* keystate = result[@"keystate"];
130
131 if([name isEqualToString:@"global"]) {
132 // this is global status; no view implicated
133 continue;
134 }
135
136 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
137 missing = true;
138 }
139 }
140
141 reply(missing);
142 }];
143 }
144
145 - (void)rpcKnownBadState:(NSString* _Nullable)viewName reply:(void (^)(CKKSKnownBadState))reply {
146 [self rpcStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
147 bool tlkMissing = false;
148 bool waitForUnlock = false;
149
150 CKKSKnownBadState response = CKKSKnownStatePossiblyGood;
151
152 // We can now change this hack, but this change needs to be addition-only: <rdar://problem/36356681> CKKS: remove "global" hack from rpcStatus
153 // Use this hack
154 for(NSDictionary* result in results) {
155 NSString* name = result[@"view"];
156 NSString* keystate = result[@"keystate"];
157
158 if([name isEqualToString:@"global"]) {
159 // this is global status; no view implicated
160 continue;
161 }
162
163 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
164 tlkMissing = true;
165 }
166 if ([keystate isEqualToString:@"waitforunlock"]) {
167 waitForUnlock = true;
168 }
169 }
170
171 response = (tlkMissing ? CKKSKnownStateTLKsMissing :
172 (waitForUnlock ? CKKSKnownStateWaitForUnlock :
173 CKKSKnownStatePossiblyGood));
174
175 reply(response);
176 }];
177 }
178
179 + (CKKSControl*)controlObject:(NSError* __autoreleasing *)error {
180
181 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydCKKSServiceName) options:0];
182
183 if (connection == nil) {
184 if(error) {
185 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
186 }
187 return nil;
188 }
189
190 NSXPCInterface *interface = CKKSSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(CKKSControlProtocol)]);
191 connection.remoteObjectInterface = interface;
192 [connection resume];
193
194 CKKSControl* c = [[CKKSControl alloc] initWithConnection:connection];
195 return c;
196 }
197
198 @end
199
200 #endif // __OBJC2__