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)rpcResetLocal:(NSString*)viewName reply:(void(^)(NSError* error))reply {
61 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
63 }] rpcResetLocal:viewName reply:^(NSError* error){
68 - (void)rpcResetCloudKit:(NSString*)viewName reply:(void(^)(NSError* error))reply {
69 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
71 }] rpcResetCloudKit:viewName reply:^(NSError* error){
76 - (void)rpcResync:(NSString*)viewName reply:(void(^)(NSError* error))reply {
77 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
79 }] rpcResync:viewName reply:^(NSError* error){
83 - (void)rpcFetchAndProcessChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
84 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
86 }] rpcFetchAndProcessChanges:viewName reply:^(NSError* error){
90 - (void)rpcFetchAndProcessClassAChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
91 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
93 }] rpcFetchAndProcessClassAChanges:viewName reply:^(NSError* error){
97 - (void)rpcPushOutgoingChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
98 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
100 }] rpcPushOutgoingChanges:viewName reply:^(NSError* error){
105 - (void)rpcPerformanceCounters:(void(^)(NSDictionary <NSString *,NSNumber *> *,NSError*))reply {
106 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
108 }] performanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
109 reply(counters, nil);
113 - (void)rpcGetCKDeviceIDWithReply:(void (^)(NSString *))reply {
114 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
116 }] rpcGetCKDeviceIDWithReply:^(NSString *ckdeviceID) {
121 - (void)rpcTLKMissing:(NSString*)viewName reply:(void(^)(bool missing))reply {
122 [self rpcStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
123 bool missing = false;
125 // Until PCS fixes [<rdar://problem/35103941> PCS: Remove PCS's use of CKKSControlProtocol], we can't add things to the protocol
127 for(NSDictionary* result in results) {
128 NSString* name = result[@"view"];
129 NSString* keystate = result[@"keystate"];
131 if([name isEqualToString:@"global"]) {
132 // this is global status; no view implicated
136 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
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;
150 CKKSKnownBadState response = CKKSKnownStatePossiblyGood;
152 // We can now change this hack, but this change needs to be addition-only: <rdar://problem/36356681> CKKS: remove "global" hack from rpcStatus
154 for(NSDictionary* result in results) {
155 NSString* name = result[@"view"];
156 NSString* keystate = result[@"keystate"];
158 if([name isEqualToString:@"global"]) {
159 // this is global status; no view implicated
163 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
166 if ([keystate isEqualToString:@"waitforunlock"]) {
167 waitForUnlock = true;
171 response = (tlkMissing ? CKKSKnownStateTLKsMissing :
172 (waitForUnlock ? CKKSKnownStateWaitForUnlock :
173 CKKSKnownStatePossiblyGood));
179 + (CKKSControl*)controlObject:(NSError* __autoreleasing *)error {
181 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydCKKSServiceName) options:0];
183 if (connection == nil) {
185 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
190 NSXPCInterface *interface = CKKSSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(CKKSControlProtocol)]);
191 connection.remoteObjectInterface = interface;
194 CKKSControl* c = [[CKKSControl alloc] initWithConnection:connection];