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>
38 #import <SecurityFoundation/SFKey.h>
41 @interface OTControl ()
42 @property NSXPCConnection *connection;
45 @implementation OTControl
47 - (instancetype)initWithConnection:(NSXPCConnection*)connection {
48 if(self = [super init]) {
49 _connection = connection;
54 - (void)restore:(NSString *)contextID dsid:(NSString *)dsid secret:(NSData*)secret escrowRecordID:(NSString*)escrowRecordID
55 reply:(void (^)(NSData* signingKeyData, NSData* encryptionKeyData, NSError* _Nullable error))reply
57 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
58 reply(nil, nil, error);
59 }] restore:contextID dsid:dsid secret:secret escrowRecordID:escrowRecordID reply:^(NSData* signingKeyData, NSData* encryptionKeyData, NSError *error) {
60 reply(signingKeyData, encryptionKeyData, error);
65 -(void)reset:(void (^)(BOOL result, NSError* _Nullable error))reply
67 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
69 }] reset:^(BOOL result, NSError * _Nullable error) {
74 - (void)signingKey:(void (^)(NSData* result, NSError* _Nullable error))reply
76 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
78 }] octagonSigningPublicKey:^(NSData *signingKey, NSError * _Nullable error) {
79 reply(signingKey, error);
84 - (void)encryptionKey:(void (^)(NSData* result, NSError* _Nullable error))reply
86 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
88 }] octagonEncryptionPublicKey:^(NSData *encryptionKey, NSError * _Nullable error) {
89 reply(encryptionKey, error);
94 - (void)listOfRecords:(void (^)(NSArray* list, NSError* _Nullable error))reply
96 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
98 }] listOfEligibleBottledPeerRecords:^(NSArray *list, NSError * _Nullable error) {
104 - (void)signIn:(NSString*)dsid reply:(void (^)(BOOL result, NSError * _Nullable error))reply{
105 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
107 }] signIn:dsid reply:^(BOOL result, NSError * _Nullable error) {
108 reply(result, error);
112 - (void)signOut:(void (^)(BOOL result, NSError * _Nullable error))reply
114 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
116 }] signOut:^(BOOL result, NSError * _Nullable error) {
117 reply(result, error);
123 - (void)preflightBottledPeer:(NSString*)contextID
125 reply:(void (^)(NSData* _Nullable entropy,
126 NSString* _Nullable bottleID,
127 NSData* _Nullable signingPublicKey,
128 NSError* _Nullable error))reply
130 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
131 reply(nil, nil, nil, error);
132 }] preflightBottledPeer:contextID dsid:dsid reply:^(NSData* _Nullable entropy,
133 NSString* _Nullable bottleID,
134 NSData* _Nullable signingPublicKey,
135 NSError* _Nullable error) {
136 reply(entropy, bottleID, signingPublicKey, error);
140 - (void)launchBottledPeer:(NSString*)contextID
141 bottleID:(NSString*)bottleID
142 reply:(void (^ _Nullable)(NSError* _Nullable))reply
144 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
146 }] launchBottledPeer:contextID bottleID:bottleID reply:^(NSError * _Nullable error) {
151 - (void)scrubBottledPeer:(NSString*)contextID
152 bottleID:(NSString*)bottleID
153 reply:(void (^ _Nullable)(NSError* _Nullable))reply
155 [[self.connection remoteObjectProxyWithErrorHandler: ^(NSError* error) {
157 }] scrubBottledPeer:contextID bottleID:bottleID reply:reply];
160 + (OTControl*)controlObject:(NSError* __autoreleasing *)error {
162 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydOctagonServiceName) options:0];
164 if (connection == nil) {
166 *error = [NSError errorWithDomain:@"securityd" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Couldn't create connection (no reason given)"}];
171 NSXPCInterface *interface = OTSetupControlProtocol([NSXPCInterface interfaceWithProtocol:@protocol(OTControlProtocol)]);
172 connection.remoteObjectInterface = interface;
175 OTControl* c = [[OTControl alloc] initWithConnection:connection];