]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSControl.m
Security-58286.230.21.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)rpcFastStatus:(NSString*)viewName reply:(void(^)(NSArray<NSDictionary*>* result, NSError* error)) reply {
61 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
62 reply(nil, error);
63
64 }] rpcFastStatus:viewName reply:^(NSArray<NSDictionary*>* result, NSError* error){
65 reply(result, error);
66 }];
67 }
68
69
70 - (void)rpcResetLocal:(NSString*)viewName reply:(void(^)(NSError* error))reply {
71 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
72 reply(error);
73 }] rpcResetLocal:viewName reply:^(NSError* error){
74 reply(error);
75 }];
76 }
77
78 - (void)rpcResetCloudKit:(NSString*)viewName reply:(void(^)(NSError* error))reply {
79 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
80 reply(error);
81 }] rpcResetCloudKit:viewName reason:[NSString stringWithFormat:@"%s", getprogname()] reply:^(NSError* error){
82 reply(error);
83 }];
84 }
85
86 - (void)rpcResetCloudKit:(NSString*)viewName reason:(NSString *)reason reply:(void(^)(NSError* error))reply {
87 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
88 reply(error);
89 }] rpcResetCloudKit:viewName reason:reason reply:^(NSError* error){
90 reply(error);
91 }];
92 }
93
94
95 - (void)rpcResync:(NSString*)viewName reply:(void(^)(NSError* error))reply {
96 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
97 reply(error);
98 }] rpcResync:viewName reply:^(NSError* error){
99 reply(error);
100 }];
101 }
102 - (void)rpcFetchAndProcessChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
103 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
104 reply(error);
105 }] rpcFetchAndProcessChanges:viewName reply:^(NSError* error){
106 reply(error);
107 }];
108 }
109 - (void)rpcFetchAndProcessClassAChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
110 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
111 reply(error);
112 }] rpcFetchAndProcessClassAChanges:viewName reply:^(NSError* error){
113 reply(error);
114 }];
115 }
116 - (void)rpcPushOutgoingChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
117 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
118 reply(error);
119 }] rpcPushOutgoingChanges:viewName reply:^(NSError* error){
120 reply(error);
121 }];
122 }
123
124 - (void)rpcPerformanceCounters:(void(^)(NSDictionary <NSString *,NSNumber *> *,NSError*))reply {
125 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
126 reply(nil, error);
127 }] performanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
128 reply(counters, nil);
129 }];
130 }
131
132 - (void)rpcGetCKDeviceIDWithReply:(void (^)(NSString *))reply {
133 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
134 reply(nil);
135 }] rpcGetCKDeviceIDWithReply:^(NSString *ckdeviceID) {
136 reply(ckdeviceID);
137 }];
138 }
139
140 - (void)rpcTLKMissing:(NSString*)viewName reply:(void(^)(bool missing))reply {
141 [self rpcFastStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
142 bool missing = false;
143
144 for(NSDictionary* result in results) {
145 NSString* name = result[@"view"];
146 NSString* keystate = result[@"keystate"];
147
148 if([name isEqualToString:@"global"]) {
149 // this is global status; no view implicated
150 continue;
151 }
152
153 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
154 missing = true;
155 }
156 }
157
158 reply(missing);
159 }];
160 }
161
162 - (void)rpcKnownBadState:(NSString* _Nullable)viewName reply:(void (^)(CKKSKnownBadState))reply {
163 [self rpcFastStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
164 bool tlkMissing = false;
165 bool waitForUnlock = false;
166
167 CKKSKnownBadState response = CKKSKnownStatePossiblyGood;
168
169 for(NSDictionary* result in results) {
170 NSString* name = result[@"view"];
171 NSString* keystate = result[@"keystate"];
172
173 if([name isEqualToString:@"global"]) {
174 // this is global status; no view implicated
175 continue;
176 }
177
178 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
179 tlkMissing = true;
180 }
181 if ([keystate isEqualToString:@"waitforunlock"]) {
182 waitForUnlock = true;
183 }
184 }
185
186 response = (tlkMissing ? CKKSKnownStateTLKsMissing :
187 (waitForUnlock ? CKKSKnownStateWaitForUnlock :
188 CKKSKnownStatePossiblyGood));
189
190 reply(response);
191 }];
192 }
193
194 + (CKKSControl*)controlObject:(NSError* __autoreleasing *)error {
195
196 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydCKKSServiceName) options:0];
197
198 if (connection == nil) {
199 if(error) {
200 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
201 }
202 return nil;
203 }
204
205 NSXPCInterface *interface = CKKSSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(CKKSControlProtocol)]);
206 connection.remoteObjectInterface = interface;
207 [connection resume];
208
209 CKKSControl* c = [[CKKSControl alloc] initWithConnection:connection];
210 return c;
211 }
212
213 @end
214
215 #endif // __OBJC2__