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/ot/OTControl.h"
32 #import "keychain/ot/OTControlProtocol.h"
33 #import "keychain/ot/OctagonControlServer.h"
35 #include <security_utilities/debugging.h>
37 @interface OTControl ()
38 @property NSXPCConnection *connection;
41 @implementation OTControl
43 - (instancetype)initWithConnection:(NSXPCConnection*)connection {
44 if(self = [super init]) {
45 _connection = connection;
50 - (void)restore:(NSString *)contextID dsid:(NSString *)dsid secret:(NSData*)secret escrowRecordID:(NSString*)escrowRecordID
51 reply:(void (^)(NSData* signingKeyData, NSData* encryptionKeyData, NSError* _Nullable error))reply
53 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
54 reply(nil, nil, error);
55 }] restore:contextID dsid:dsid secret:secret escrowRecordID:escrowRecordID reply:^(NSData* signingKeyData, NSData* encryptionKeyData, NSError *error) {
56 reply(signingKeyData, encryptionKeyData, error);
61 -(void)reset:(void (^)(BOOL result, NSError* _Nullable error))reply
63 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
65 }] reset:^(BOOL result, NSError * _Nullable error) {
70 - (void)signingKey:(void (^)(NSData* result, NSError* _Nullable error))reply
72 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
74 }] octagonSigningPublicKey:^(NSData *signingKey, NSError * _Nullable error) {
75 reply(signingKey, error);
80 - (void)encryptionKey:(void (^)(NSData* result, NSError* _Nullable error))reply
82 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
84 }] octagonEncryptionPublicKey:^(NSData *encryptionKey, NSError * _Nullable error) {
85 reply(encryptionKey, error);
90 - (void)listOfRecords:(void (^)(NSArray* list, NSError* _Nullable error))reply
92 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
94 }] listOfEligibleBottledPeerRecords:^(NSArray *list, NSError * _Nullable error) {
100 - (void)signIn:(NSString*)dsid reply:(void (^)(BOOL result, NSError * _Nullable error))reply{
101 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
103 }] signIn:dsid reply:^(BOOL result, NSError * _Nullable error) {
104 reply(result, error);
108 - (void)signOut:(void (^)(BOOL result, NSError * _Nullable error))reply
110 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
112 }] signOut:^(BOOL result, NSError * _Nullable error) {
113 reply(result, error);
119 - (void)preflightBottledPeer:(NSString*)contextID
121 reply:(void (^)(NSData* _Nullable entropy,
122 NSString* _Nullable bottleID,
123 NSData* _Nullable signingPublicKey,
124 NSError* _Nullable error))reply
126 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
127 reply(nil, nil, nil, error);
128 }] preflightBottledPeer:contextID dsid:dsid reply:^(NSData* _Nullable entropy,
129 NSString* _Nullable bottleID,
130 NSData* _Nullable signingPublicKey,
131 NSError* _Nullable error) {
132 reply(entropy, bottleID, signingPublicKey, error);
136 - (void)launchBottledPeer:(NSString*)contextID
137 bottleID:(NSString*)bottleID
138 reply:(void (^ _Nullable)(NSError* _Nullable))reply
140 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
142 }] launchBottledPeer:contextID bottleID:bottleID reply:^(NSError * _Nullable error) {
147 - (void)scrubBottledPeer:(NSString*)contextID
148 bottleID:(NSString*)bottleID
149 reply:(void (^ _Nullable)(NSError* _Nullable))reply
151 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
153 }] scrubBottledPeer:contextID bottleID:bottleID reply:reply];
156 + (OTControl*)controlObject:(NSError* __autoreleasing *)error {
158 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydOctagonServiceName) options:0];
160 if (connection == nil) {
162 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
167 NSXPCInterface *interface = OTSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(OTControlProtocol)]);
168 connection.remoteObjectInterface = interface;
171 OTControl* c = [[OTControl alloc] initWithConnection:connection];