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 reason:[NSString stringWithFormat:@"%s", getprogname()] reply:^(NSError* error){
76 - (void)rpcResetCloudKit:(NSString*)viewName reason:(NSString *)reason reply:(void(^)(NSError* error))reply {
77 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
79 }] rpcResetCloudKit:viewName reason:reason reply:^(NSError* error){
85 - (void)rpcResync:(NSString*)viewName reply:(void(^)(NSError* error))reply {
86 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
88 }] rpcResync:viewName reply:^(NSError* error){
92 - (void)rpcFetchAndProcessChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
93 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
95 }] rpcFetchAndProcessChanges:viewName reply:^(NSError* error){
99 - (void)rpcFetchAndProcessClassAChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
100 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
102 }] rpcFetchAndProcessClassAChanges:viewName reply:^(NSError* error){
106 - (void)rpcPushOutgoingChanges:(NSString*)viewName reply:(void(^)(NSError* error))reply {
107 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
109 }] rpcPushOutgoingChanges:viewName reply:^(NSError* error){
114 - (void)rpcPerformanceCounters:(void(^)(NSDictionary <NSString *,NSNumber *> *,NSError*))reply {
115 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
117 }] performanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
118 reply(counters, nil);
122 - (void)rpcGetCKDeviceIDWithReply:(void (^)(NSString *))reply {
123 [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
125 }] rpcGetCKDeviceIDWithReply:^(NSString *ckdeviceID) {
130 - (void)rpcTLKMissing:(NSString*)viewName reply:(void(^)(bool missing))reply {
131 [self rpcStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
132 bool missing = false;
134 // Until PCS fixes [<rdar://problem/35103941> PCS: Remove PCS's use of CKKSControlProtocol], we can't add things to the protocol
136 for(NSDictionary* result in results) {
137 NSString* name = result[@"view"];
138 NSString* keystate = result[@"keystate"];
140 if([name isEqualToString:@"global"]) {
141 // this is global status; no view implicated
145 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
154 - (void)rpcKnownBadState:(NSString* _Nullable)viewName reply:(void (^)(CKKSKnownBadState))reply {
155 [self rpcStatus:viewName reply:^(NSArray<NSDictionary*>* results, NSError* blockError) {
156 bool tlkMissing = false;
157 bool waitForUnlock = false;
159 CKKSKnownBadState response = CKKSKnownStatePossiblyGood;
161 // We can now change this hack, but this change needs to be addition-only: <rdar://problem/36356681> CKKS: remove "global" hack from rpcStatus
163 for(NSDictionary* result in results) {
164 NSString* name = result[@"view"];
165 NSString* keystate = result[@"keystate"];
167 if([name isEqualToString:@"global"]) {
168 // this is global status; no view implicated
172 if ([keystate isEqualToString:@"waitfortlk"] || [keystate isEqualToString:@"error"]) {
175 if ([keystate isEqualToString:@"waitforunlock"]) {
176 waitForUnlock = true;
180 response = (tlkMissing ? CKKSKnownStateTLKsMissing :
181 (waitForUnlock ? CKKSKnownStateWaitForUnlock :
182 CKKSKnownStatePossiblyGood));
188 + (CKKSControl*)controlObject:(NSError* __autoreleasing *)error {
190 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydCKKSServiceName) options:0];
192 if (connection == nil) {
194 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
199 NSXPCInterface *interface = CKKSSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(CKKSControlProtocol)]);
200 connection.remoteObjectInterface = interface;
203 CKKSControl* c = [[CKKSControl alloc] initWithConnection:connection];