2 * Copyright (c) 2007-2009,2012-2015 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@
24 #include <TargetConditionals.h>
26 // client.c is from the iOS world and must compile with an iOS view of the headers
27 #ifndef SEC_IOS_ON_OSX
28 #define SEC_IOS_ON_OSX 1
29 #endif // SEC_IOS_ON_OSX
32 #ifndef SECITEM_SHIM_OSX
33 #define SECITEM_SHIM_OSX 1
34 #endif // SECITEM_SHIM_OSX
35 #endif // TARGET_OS_OSX
38 #include <sys/queue.h>
40 #include <vproc_priv.h>
42 #include <xpc/private.h>
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <Security/SecItem.h>
46 #include <Security/SecBasePriv.h>
47 #include <Security/SecInternal.h>
48 #include <Security/SecuritydXPC.h>
49 #include <Security/SecTask.h>
50 #include <Security/SecItemPriv.h>
52 #include <utilities/debugging.h>
53 #include <utilities/SecCFError.h>
54 #include <utilities/SecXPCError.h>
55 #include <utilities/SecCFWrappers.h>
56 #include <utilities/SecDispatchRelease.h>
57 #include <utilities/SecDb.h> // TODO Fixme this gets us SecError().
58 #include <utilities/SecAKSWrappers.h>
59 #include <ipc/securityd_client.h>
61 struct securityd
*gSecurityd
;
62 struct trustd
*gTrustd
;
68 /* Hardcoded Access Groups for the server itself */
69 static CFArrayRef
SecServerCopyAccessGroups(void) {
70 return CFArrayCreateForCFTypes(kCFAllocatorDefault
,
74 CFSTR("lockdown-identities"),
75 CFSTR("123456.test.group"),
76 CFSTR("123456.test.group2"),
80 CFSTR("com.apple.security.sos"),
81 CFSTR("com.apple.security.ckks"),
82 CFSTR("com.apple.security.sos-usercredential"),
83 CFSTR("com.apple.sbd"),
84 CFSTR("com.apple.lakitu"),
85 kSecAttrAccessGroupToken
,
89 static SecurityClient gClient
;
93 SecSecuritySetMusrMode(bool mode
, uid_t uid
, int activeUser
)
95 gClient
.inMultiUser
= mode
;
97 gClient
.activeUser
= activeUser
;
102 SecSecurityClientGet(void)
104 static dispatch_once_t onceToken
;
105 dispatch_once(&onceToken
, ^{
107 gClient
.accessGroups
= SecServerCopyAccessGroups();
108 gClient
.allowSystemKeychain
= true;
109 gClient
.allowSyncBubbleKeychain
= true;
110 gClient
.isNetworkExtension
= false;
112 gClient
.inMultiUser
= false;
113 gClient
.activeUser
= 501;
119 CFArrayRef
SecAccessGroupsGetCurrent(void) {
120 SecurityClient
*client
= SecSecurityClientGet();
121 assert(client
&& client
->accessGroups
);
122 return client
->accessGroups
;
126 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups
);
127 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups
) {
128 // Not thread safe at all, but OK because it is meant to be used only by tests.
129 gClient
.accessGroups
= accessGroups
;
132 #if !TARGET_OS_IPHONE
133 static bool securityd_in_system_context(void) {
134 static bool runningInSystemContext
;
135 static dispatch_once_t onceToken
;
136 dispatch_once(&onceToken
, ^{
137 runningInSystemContext
= (getuid() == 0);
138 if (!runningInSystemContext
) {
140 if (vproc_swap_string(NULL
, VPROC_GSK_MGR_NAME
, NULL
, &manager
) == NULL
) {
141 runningInSystemContext
= (!strcmp(manager
, VPROCMGR_SESSION_SYSTEM
) ||
142 !strcmp(manager
, VPROCMGR_SESSION_LOGINWINDOW
));
147 return runningInSystemContext
;
151 static const char *securityd_service_name(void) {
152 return kSecuritydXPCServiceName
;
155 static uid_t target_uid
= -1;
158 _SecSetSecuritydTargetUID(uid_t uid
)
164 static xpc_connection_t
securityd_create_connection(const char *name
, uint64_t flags
) {
165 const char *serviceName
= name
;
167 serviceName
= securityd_service_name();
169 xpc_connection_t connection
;
170 connection
= xpc_connection_create_mach_service(serviceName
, NULL
, flags
);
171 xpc_connection_set_event_handler(connection
, ^(xpc_object_t event
) {
172 const char *description
= xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
);
173 secnotice("xpc", "got event: %s", description
);
175 if (target_uid
!= (uid_t
)-1) {
176 xpc_connection_set_target_uid(connection
, target_uid
);
178 xpc_connection_resume(connection
);
182 static xpc_connection_t sSecuritydConnection
;
183 static xpc_connection_t sTrustdConnection
;
185 static xpc_connection_t
securityd_connection(void) {
186 static dispatch_once_t once
;
187 dispatch_once(&once
, ^{
188 sSecuritydConnection
= securityd_create_connection(kSecuritydXPCServiceName
, 0);
190 return sSecuritydConnection
;
193 static xpc_connection_t
trustd_connection(void) {
194 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
195 static dispatch_once_t once
;
196 dispatch_once(&once
, ^{
197 bool sysCtx
= securityd_in_system_context();
198 uint64_t flags
= (sysCtx
) ? XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
: 0;
199 const char *serviceName
= (sysCtx
) ? kTrustdXPCServiceName
: kTrustdAgentXPCServiceName
;
200 sTrustdConnection
= securityd_create_connection(serviceName
, flags
);
202 return sTrustdConnection
;
204 static dispatch_once_t once
;
205 dispatch_once(&once
, ^{
206 sTrustdConnection
= securityd_create_connection(kTrustdXPCServiceName
, 0);
208 return sTrustdConnection
;
212 static bool is_trust_operation(enum SecXPCOperation op
) {
214 case sec_trust_store_contains_id
:
215 case sec_trust_store_set_trust_settings_id
:
216 case sec_trust_store_remove_certificate_id
:
217 case sec_trust_evaluate_id
:
218 case sec_trust_store_copy_all_id
:
219 case sec_trust_store_copy_usage_constraints_id
:
220 case sec_ocsp_cache_flush_id
:
221 case sec_ota_pki_trust_store_version_id
:
222 case kSecXPCOpOTAGetEscrowCertificates
:
223 case kSecXPCOpOTAPKIGetNewAsset
:
224 case kSecXPCOpTLSAnaltyicsReport
:
232 static xpc_connection_t
securityd_connection_for_operation(enum SecXPCOperation op
) {
233 bool isTrustOp
= is_trust_operation(op
);
234 #if SECTRUST_VERBOSE_DEBUG
236 bool sysCtx
= securityd_in_system_context();
237 const char *serviceName
= (sysCtx
) ? kTrustdXPCServiceName
: kTrustdAgentXPCServiceName
;
238 syslog(LOG_ERR
, "Will connect to: %s (op=%d)",
239 (isTrustOp
) ? serviceName
: kSecuritydXPCServiceName
, (int)op
);
242 return (isTrustOp
) ? trustd_connection() : securityd_connection();
245 // NOTE: This is not thread safe, but this SPI is for testing only.
246 void SecServerSetMachServiceName(const char *name
) {
247 // Make sure sSecXPCServer.queue exists.
250 xpc_connection_t oldConection
= sTrustdConnection
;
251 sTrustdConnection
= securityd_create_connection(name
, 0);
253 xpc_release(oldConection
);
258 securityd_message_with_reply_sync(xpc_object_t message
, CFErrorRef
*error
)
260 xpc_object_t reply
= NULL
;
261 uint64_t operation
= xpc_dictionary_get_uint64(message
, kSecXPCKeyOperation
);
262 xpc_connection_t connection
= securityd_connection_for_operation((enum SecXPCOperation
)operation
);
264 const int max_tries
= 4; // Per <rdar://problem/17829836> N61/12A342: Audio Playback... for why this needs to be at least 3, so we made it 4.
266 unsigned int tries_left
= max_tries
;
268 if (reply
) xpc_release(reply
);
269 reply
= xpc_connection_send_message_with_reply_sync(connection
, message
);
270 } while (reply
== XPC_ERROR_CONNECTION_INTERRUPTED
&& --tries_left
> 0);
272 if (xpc_get_type(reply
) == XPC_TYPE_ERROR
) {
274 if (reply
== XPC_ERROR_CONNECTION_INTERRUPTED
|| reply
== XPC_ERROR_CONNECTION_INVALID
) {
275 code
= kSecXPCErrorConnectionFailed
;
276 seccritical("Failed to talk to %s after %d attempts.",
277 (is_trust_operation((enum SecXPCOperation
)operation
)) ? "trustd" :
284 } else if (reply
== XPC_ERROR_TERMINATION_IMMINENT
)
285 code
= kSecXPCErrorUnknown
;
287 code
= kSecXPCErrorUnknown
;
289 char *conn_desc
= xpc_copy_description(connection
);
290 const char *description
= xpc_dictionary_get_string(reply
, XPC_ERROR_KEY_DESCRIPTION
);
291 SecCFCreateErrorWithFormat(code
, sSecXPCErrorDomain
, NULL
, error
, NULL
, CFSTR("%s: %s"), conn_desc
, description
);
302 securityd_create_message(enum SecXPCOperation op
, CFErrorRef
* error
)
304 xpc_object_t message
= xpc_dictionary_create(NULL
, NULL
, 0);
306 xpc_dictionary_set_uint64(message
, kSecXPCKeyOperation
, op
);
308 SecCFCreateError(kSecXPCErrorConnectionFailed
, sSecXPCErrorDomain
,
309 CFSTR("xpc_dictionary_create returned NULL"), NULL
, error
);
314 // Return true if there is no error in message, return false and set *error if there is.
315 bool securityd_message_no_error(xpc_object_t message
, CFErrorRef
*error
) {
316 xpc_object_t xpc_error
= NULL
;
320 xpc_error
= xpc_dictionary_get_value(message
, kSecXPCKeyError
);
321 if (xpc_error
== NULL
)
324 CFErrorRef localError
= SecCreateCFErrorWithXPCObject(xpc_error
);
327 secdebug("xpc", "Talking to securityd failed with error: %@", localError
);
330 uint64_t operation
= xpc_dictionary_get_uint64(message
, kSecXPCKeyOperation
);
332 secdebug("xpc", "Talking to %s failed with error: %@",
333 (is_trust_operation((enum SecXPCOperation
)operation
)) ? "trustd" : "secd", localError
);
339 CFReleaseSafe(localError
);
344 bool securityd_send_sync_and_do(enum SecXPCOperation op
, CFErrorRef
*error
,
345 bool (^add_to_message
)(xpc_object_t message
, CFErrorRef
* error
),
346 bool (^handle_response
)(xpc_object_t _Nonnull response
, CFErrorRef
* error
)) {
347 xpc_object_t message
= securityd_create_message(op
, error
);
350 if (!add_to_message
|| add_to_message(message
, error
)) {
351 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
353 if (securityd_message_no_error(response
, error
)) {
354 ok
= (!handle_response
|| handle_response(response
, error
));
356 xpc_release(response
);
359 xpc_release(message
);
367 _SecSecuritydCopyWhoAmI(CFErrorRef
*error
)
369 CFDictionaryRef reply
= NULL
;
370 xpc_object_t message
= securityd_create_message(kSecXPCOpWhoAmI
, error
);
372 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
374 reply
= _CFXPCCreateCFObjectFromXPCObject(response
);
375 xpc_release(response
);
377 secerror("Securityd failed getting whoamid with error: %@",
378 error
? *error
: NULL
);
380 xpc_release(message
);
386 _SecSyncBubbleTransfer(CFArrayRef services
, uid_t uid
, CFErrorRef
*error
)
388 xpc_object_t message
;
391 message
= securityd_create_message(kSecXPCOpTransmogrifyToSyncBubble
, error
);
393 xpc_dictionary_set_int64(message
, "uid", uid
);
394 if (SecXPCDictionarySetPList(message
, "services", services
, error
)) {
395 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
397 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
399 securityd_message_no_error(response
, error
);
400 xpc_release(response
);
402 xpc_release(message
);
409 _SecSystemKeychainTransfer(CFErrorRef
*error
)
411 xpc_object_t message
;
414 message
= securityd_create_message(kSecXPCOpTransmogrifyToSystemKeychain
, error
);
416 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
418 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
420 securityd_message_no_error(response
, error
);
421 xpc_release(response
);
423 xpc_release(message
);
429 _SecSyncDeleteUserViews(uid_t uid
, CFErrorRef
*error
)
431 xpc_object_t message
;
434 message
= securityd_create_message(kSecXPCOpDeleteUserView
, error
);
436 xpc_dictionary_set_int64(message
, "uid", uid
);
438 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
440 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
442 securityd_message_no_error(response
, error
);
443 xpc_release(response
);
445 xpc_release(message
);
450 XPC_RETURNS_RETAINED xpc_endpoint_t
451 _SecSecuritydCopyEndpoint(enum SecXPCOperation op
, CFErrorRef
*error
)
453 xpc_endpoint_t endpoint
= NULL
;
454 xpc_object_t message
= securityd_create_message(op
, error
);
456 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
458 endpoint
= xpc_dictionary_get_value(response
, kSecXPCKeyEndpoint
);
460 if(xpc_get_type(endpoint
) != XPC_TYPE_ENDPOINT
) {
461 secerror("endpoint was not an endpoint");
464 xpc_retain(endpoint
);
467 secerror("endpoint was null");
469 xpc_release(response
);
471 secerror("Securityd failed getting endpoint with error: %@", error
? *error
: NULL
);
473 xpc_release(message
);
479 XPC_RETURNS_RETAINED xpc_endpoint_t
480 _SecSecuritydCopyCKKSEndpoint(CFErrorRef
*error
)
485 XPC_RETURNS_RETAINED xpc_endpoint_t
486 _SecSecuritydCopyKeychainControlEndpoint(CFErrorRef
* error
)
488 return _SecSecuritydCopyEndpoint(kSecXPCOpKeychainControlEndpoint
, error
);