2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #import <Foundation/NSXPCConnection_Private.h>
29 #import <Security/SecItemPriv.h>
31 #import "keychain/ckks/CKKSControl.h"
32 #import "keychain/ckks/CKKSControlProtocol.h"
33 #import "keychain/ckks/CKKSControlServer.h"
35 #include <security_utilities/debugging.h>
37 @interface CKKSControl ()
38 @property xpc_endpoint_t endpoint;
39 @property NSXPCConnection *connection;
42 @implementation CKKSControl
44 - (instancetype)initWithConnection:(NSXPCConnection*)connection {
45 if(self = [super init]) {
46 _connection = connection;
51 - (void)rpcStatus:(NSString*)viewName reply:(void(^)(NSArray<NSDictionary*>* result, NSError* error)) reply {
52 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
55 }] rpcStatus:viewName reply:^(NSArray<NSDictionary*>* result, NSError* error){
60 - (void)rpcFastStatus:(NSString*)viewName reply:(void(^)(NSArray<NSDictionary*>* result, NSError* error)) reply {
61 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
64 }] rpcFastStatus:viewName reply:^(NSArray<NSDictionary*>* result, NSError* error){
70 - (void)rpcResetLocal:(NSString*)viewName reply:(void(^)(NSError* error))reply {
71 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
73 }] rpcResetLocal:viewName reply:^(NSError* error){
78 - (void)rpcResetCloudKit:(NSString*)viewName reply:(void(^)(NSError* error))reply {
79 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
81 }] rpcResetCloudKit:viewName reason:[NSString stringWithFormat:@"%s", getprogname()] reply:^(NSError* error){
86 - (void)rpcResetCloudKit:(NSString*)viewName reason:(NSString *)reason reply:(void(^)(NSError* error))reply {
87 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
89 }] rpcResetCloudKit:viewName reason:reason reply:^(NSError* error){
95 - (void)rpcResync:(NSString*)viewName reply:(void(^)(NSError* error))reply {
96 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
98 }] rpcResync:viewName reply:^(NSError* error){
102 - (void)rpcFetchAndProcessChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
103 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
105 }] rpcFetchAndProcessChanges:viewName reply:^(NSError* error){
109 - (void)rpcFetchAndProcessClassAChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
110 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
112 }] rpcFetchAndProcessClassAChanges:viewName reply:^(NSError* error){
116 - (void)rpcPushOutgoingChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
117 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
119 }] rpcPushOutgoingChanges:viewName reply:^(NSError* error){
124 - (void)rpcPerformanceCounters:(void(^)(NSDictionary <NSString *,NSNumber *> *,NSError*))reply {
125 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
127 }] performanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
128 reply(counters, nil);
132 - (void)rpcGetCKDeviceIDWithReply:(void (^)(NSString *))reply {
133 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
135 }] rpcGetCKDeviceIDWithReply:^(NSString *ckdeviceID) {
140 - (void)rpcTLKMissing:(NSString*)viewName reply:(void(^)(bool missing))reply {
141 [self rpcFastStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
142 bool missing = false;
144 for(NSDictionary* result in results) {
145 NSString* name = result[@"view"];
146 NSString* keystate = result[@"keystate"];
148 if([name isEqualToString:@"global"]) {
149 // this is global status; no view implicated
153 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
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;
167 CKKSKnownBadState response = CKKSKnownStatePossiblyGood;
169 for(NSDictionary* result in results) {
170 NSString* name = result[@"view"];
171 NSString* keystate = result[@"keystate"];
173 if([name isEqualToString:@"global"]) {
174 // this is global status; no view implicated
178 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
181 if ([keystate isEqualToString:@"waitforunlock"]) {
182 waitForUnlock = true;
186 response = (tlkMissing ? CKKSKnownStateTLKsMissing :
187 (waitForUnlock ? CKKSKnownStateWaitForUnlock :
188 CKKSKnownStatePossiblyGood));
194 + (CKKSControl*)controlObject:(NSError* __autoreleasing *)error {
196 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydCKKSServiceName) options:0];
198 if (connection == nil) {
200 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
205 NSXPCInterface *interface = CKKSSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(CKKSControlProtocol)]);
206 connection.remoteObjectInterface = interface;
209 CKKSControl* c = [[CKKSControl alloc] initWithConnection:connection];