]> git.saurik.com Git - apple/security.git/blob - OSX/sec/ipc/client_endpoint.m
Security-58286.60.28.tar.gz
[apple/security.git] / OSX / sec / ipc / client_endpoint.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Foundation/Foundation.h>
25 #import <Foundation/NSXPCConnection.h>
26 #import <Foundation/NSXPCConnection_Private.h>
27 #import <objc/runtime.h>
28 #import <utilities/debugging.h>
29
30 #include <ipc/securityd_client.h>
31
32 @implementation SecuritydXPCClient
33 @synthesize connection = _connection;
34
35 - (instancetype) init
36 {
37 if ((self = [super init])) {
38 NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(SecuritydXPCProtocol)];
39
40 self.connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydGeneralServiceName) options:0];
41 if (self.connection == NULL) {
42 return NULL;
43 }
44
45 self.connection.remoteObjectInterface = interface;
46 [SecuritydXPCClient configureSecuritydXPCProtocol: self.connection.remoteObjectInterface];
47
48 [self.connection resume];
49 }
50
51 return self;
52 }
53
54 +(void)configureSecuritydXPCProtocol: (NSXPCInterface*) interface {
55 NSXPCInterface *rpcCallbackInterface = [NSXPCInterface interfaceWithProtocol: @protocol(SecuritydXPCCallbackProtocol)];
56 [interface setInterface:rpcCallbackInterface
57 forSelector:@selector(SecItemAddAndNotifyOnSync:syncCallback:complete:)
58 argumentIndex:1
59 ofReply:0];
60
61 #if OCTAGON
62 static NSMutableSet *errClasses;
63 static dispatch_once_t onceToken;
64 dispatch_once(&onceToken, ^{
65 // By finding classes by strings at runtime, we'll only get the CloudKit helpers if you link CloudKit
66 // Plus, we don't have to weak-link cloudkit from Security.framework
67
68 errClasses = [[NSMutableSet alloc] init];
69 char *classes[] = {
70 "NSError",
71 "NSArray",
72 "NSString",
73 "NSNumber",
74 "NSData",
75 "NSDate",
76 "CKReference",
77 "CKAsset",
78 "CLLocation",
79 "CKPackage",
80 "CKArchivedAnchoredPackage",
81 "CKPrettyError",
82 "CKRecordID",
83 "NSURL",
84 };
85
86 for (unsigned n = 0; n < sizeof(classes)/sizeof(classes[0]); n++) {
87 Class cls = objc_getClass(classes[n]);
88 if (cls) {
89 [errClasses addObject:cls];
90 }
91 }
92 });
93
94 @try {
95 [rpcCallbackInterface setClasses:errClasses forSelector:@selector(callCallback:error:) argumentIndex:1 ofReply:NO];
96
97 [interface setClasses:errClasses forSelector:@selector(SecItemAddAndNotifyOnSync:
98 syncCallback:
99 complete:) argumentIndex:2 ofReply:YES];
100 [interface setClasses:errClasses forSelector:@selector(secItemFetchCurrentItemAcrossAllDevices:
101 identifier:
102 viewHint:
103 fetchCloudValue:
104 complete:) argumentIndex:1 ofReply:YES];
105 [interface setClasses:errClasses forSelector:@selector(secItemDigest:
106 accessGroup:
107 complete:) argumentIndex:1 ofReply:YES];
108 [interface setClasses:errClasses forSelector:@selector(secItemSetCurrentItemAcrossAllDevices:
109 newCurrentItemHash:
110 accessGroup:
111 identifier:
112 viewHint:
113 oldCurrentItemReference:
114 oldCurrentItemHash:
115 complete:) argumentIndex:0 ofReply:YES];
116 }
117 @catch(NSException* e) {
118 secerror("Could not configure SecuritydXPCProtocol: %@", e);
119 #if DEBUG
120 @throw e;
121 #endif // DEBUG
122 }
123 #endif // OCTAGON
124 }
125 @end
126
127 @implementation SecuritydXPCCallback
128 @synthesize callback = _callback;
129
130 -(instancetype)initWithCallback: (SecBoolNSErrorCallback) callback {
131 if((self = [super init])) {
132 _callback = callback;
133 }
134 return self;
135 }
136
137 - (void)callCallback: (bool) result error:(NSError*) error {
138 self.callback(result, error);
139 }
140 @end
141
142 id<SecuritydXPCProtocol> SecuritydXPCProxyObject(void (^rpcErrorHandler)(NSError *))
143 {
144 if (gSecurityd && gSecurityd->secd_xpc_server) {
145 return (__bridge id<SecuritydXPCProtocol>)gSecurityd->secd_xpc_server;
146 }
147
148 static SecuritydXPCClient* rpc;
149 static dispatch_once_t onceToken;
150
151 dispatch_once(&onceToken, ^{
152 rpc = [[SecuritydXPCClient alloc] init];
153 });
154
155 if (rpc == NULL) {
156 rpcErrorHandler([NSError errorWithDomain:@"securityd" code:-1 userInfo:@{ NSLocalizedDescriptionKey : @"Could not create SecuritydXPCClient" }]);
157 return NULL;
158 } else {
159 return [rpc.connection remoteObjectProxyWithErrorHandler: rpcErrorHandler];
160 }
161 }
162