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 #include "server_security_helpers.h"
63 struct securityd
*gSecurityd
;
64 struct trustd
*gTrustd
;
70 /* Hardcoded Access Groups for the server itself */
71 static CFArrayRef
SecServerCopyAccessGroups(void) {
72 return CFArrayCreateForCFTypes(kCFAllocatorDefault
,
76 CFSTR("lockdown-identities"),
77 CFSTR("123456.test.group"),
78 CFSTR("123456.test.group2"),
79 CFSTR("com.apple.cfnetwork"),
80 CFSTR("com.apple.bluetooth"),
83 CFSTR("com.apple.security.sos"),
84 CFSTR("com.apple.security.ckks"),
85 CFSTR("com.apple.security.sos-usercredential"),
86 CFSTR("com.apple.sbd"),
87 CFSTR("com.apple.lakitu"),
88 CFSTR("com.apple.security.securityd"),
89 kSecAttrAccessGroupToken
,
93 static SecurityClient gClient
;
97 SecSecuritySetMusrMode(bool mode
, uid_t uid
, int activeUser
)
99 gClient
.inMultiUser
= mode
;
101 gClient
.activeUser
= activeUser
;
106 SecSecurityClientGet(void)
108 static dispatch_once_t onceToken
;
109 dispatch_once(&onceToken
, ^{
111 gClient
.accessGroups
= SecServerCopyAccessGroups();
112 gClient
.allowSystemKeychain
= true;
113 gClient
.allowSyncBubbleKeychain
= true;
114 gClient
.isNetworkExtension
= false;
116 gClient
.inMultiUser
= false;
117 gClient
.activeUser
= 501;
123 CFArrayRef
SecAccessGroupsGetCurrent(void) {
124 SecurityClient
*client
= SecSecurityClientGet();
125 assert(client
&& client
->accessGroups
);
126 return client
->accessGroups
;
130 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups
) {
131 // Not thread safe at all, but OK because it is meant to be used only by tests.
132 gClient
.accessGroups
= accessGroups
;
135 #if !TARGET_OS_IPHONE
136 static bool securityd_in_system_context(void) {
137 static bool runningInSystemContext
;
138 static dispatch_once_t onceToken
;
139 dispatch_once(&onceToken
, ^{
140 runningInSystemContext
= (getuid() == 0);
141 if (!runningInSystemContext
) {
143 if (vproc_swap_string(NULL
, VPROC_GSK_MGR_NAME
, NULL
, &manager
) == NULL
) {
144 runningInSystemContext
= (!strcmp(manager
, VPROCMGR_SESSION_SYSTEM
) ||
145 !strcmp(manager
, VPROCMGR_SESSION_LOGINWINDOW
));
150 return runningInSystemContext
;
154 static const char *securityd_service_name(void) {
155 return kSecuritydXPCServiceName
;
158 #define SECURITY_TARGET_UID_UNSET ((uid_t)-1)
159 static uid_t securityd_target_uid
= SECURITY_TARGET_UID_UNSET
;
162 _SecSetSecuritydTargetUID(uid_t uid
)
164 securityd_target_uid
= uid
;
168 static xpc_connection_t
securityd_create_connection(const char *name
, uid_t target_uid
, uint64_t flags
) {
169 const char *serviceName
= name
;
171 serviceName
= securityd_service_name();
173 xpc_connection_t connection
;
174 connection
= xpc_connection_create_mach_service(serviceName
, NULL
, flags
);
175 xpc_connection_set_event_handler(connection
, ^(xpc_object_t event
) {
176 const char *description
= xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
);
177 secnotice("xpc", "got event: %s", description
);
179 if (target_uid
!= SECURITY_TARGET_UID_UNSET
) {
180 xpc_connection_set_target_uid(connection
, target_uid
);
182 xpc_connection_resume(connection
);
186 static xpc_connection_t sSecuritydConnection
;
187 static xpc_connection_t sTrustdConnection
;
189 static xpc_connection_t
securityd_connection(void) {
190 static dispatch_once_t once
;
191 dispatch_once(&once
, ^{
192 sSecuritydConnection
= securityd_create_connection(kSecuritydXPCServiceName
, securityd_target_uid
, 0);
194 return sSecuritydConnection
;
197 static xpc_connection_t
trustd_connection(void) {
198 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
199 static dispatch_once_t once
;
200 dispatch_once(&once
, ^{
201 bool sysCtx
= securityd_in_system_context();
202 uint64_t flags
= (sysCtx
) ? XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
: 0;
203 const char *serviceName
= (sysCtx
) ? kTrustdXPCServiceName
: kTrustdAgentXPCServiceName
;
204 sTrustdConnection
= securityd_create_connection(serviceName
, SECURITY_TARGET_UID_UNSET
, flags
);
206 return sTrustdConnection
;
208 static dispatch_once_t once
;
209 dispatch_once(&once
, ^{
210 sTrustdConnection
= securityd_create_connection(kTrustdXPCServiceName
, SECURITY_TARGET_UID_UNSET
, 0);
212 return sTrustdConnection
;
216 static bool is_trust_operation(enum SecXPCOperation op
) {
218 case sec_trust_store_contains_id
:
219 case sec_trust_store_set_trust_settings_id
:
220 case sec_trust_store_remove_certificate_id
:
221 case sec_trust_evaluate_id
:
222 case sec_trust_store_copy_all_id
:
223 case sec_trust_store_copy_usage_constraints_id
:
224 case sec_ocsp_cache_flush_id
:
225 case sec_ota_pki_trust_store_version_id
:
226 case sec_ota_pki_asset_version_id
:
227 case kSecXPCOpOTAGetEscrowCertificates
:
228 case kSecXPCOpOTAPKIGetNewAsset
:
229 case kSecXPCOpNetworkingAnalyticsReport
:
230 case kSecXPCOpSetCTExceptions
:
231 case kSecXPCOpCopyCTExceptions
:
239 static xpc_connection_t
securityd_connection_for_operation(enum SecXPCOperation op
) {
240 bool isTrustOp
= is_trust_operation(op
);
241 #if SECTRUST_VERBOSE_DEBUG
243 bool sysCtx
= securityd_in_system_context();
244 const char *serviceName
= (sysCtx
) ? kTrustdXPCServiceName
: kTrustdAgentXPCServiceName
;
245 syslog(LOG_ERR
, "Will connect to: %s (op=%d)",
246 (isTrustOp
) ? serviceName
: kSecuritydXPCServiceName
, (int)op
);
249 return (isTrustOp
) ? trustd_connection() : securityd_connection();
252 // NOTE: This is not thread safe, but this SPI is for testing only.
253 void SecServerSetTrustdMachServiceName(const char *name
) {
254 // Make sure sSecXPCServer.queue exists.
257 xpc_connection_t oldConection
= sTrustdConnection
;
258 sTrustdConnection
= securityd_create_connection(name
, SECURITY_TARGET_UID_UNSET
, 0);
260 xpc_release(oldConection
);
264 #define SECURITYD_MAX_XPC_TRIES 4
265 // Per <rdar://problem/17829836> N61/12A342: Audio Playback... for why this needs to be at least 3, so we made it 4.
268 _securityd_process_message_reply(xpc_object_t
*reply
,
270 xpc_connection_t connection
,
273 if (xpc_get_type(*reply
) != XPC_TYPE_ERROR
) {
277 if (*reply
== XPC_ERROR_CONNECTION_INTERRUPTED
|| *reply
== XPC_ERROR_CONNECTION_INVALID
) {
278 code
= kSecXPCErrorConnectionFailed
;
279 seccritical("Failed to talk to %s after %d attempts.",
280 (is_trust_operation((enum SecXPCOperation
)operation
)) ? "trustd" :
286 SECURITYD_MAX_XPC_TRIES
);
287 } else if (*reply
== XPC_ERROR_TERMINATION_IMMINENT
) {
288 code
= kSecXPCErrorUnknown
;
290 code
= kSecXPCErrorUnknown
;
293 char *conn_desc
= xpc_copy_description(connection
);
294 const char *description
= xpc_dictionary_get_string(*reply
, XPC_ERROR_KEY_DESCRIPTION
);
295 SecCFCreateErrorWithFormat(code
, sSecXPCErrorDomain
, NULL
, error
, NULL
, CFSTR("%s: %s"), conn_desc
, description
);
305 securityd_message_with_reply_sync(xpc_object_t message
, CFErrorRef
*error
)
307 xpc_object_t reply
= NULL
;
308 uint64_t operation
= xpc_dictionary_get_uint64(message
, kSecXPCKeyOperation
);
309 xpc_connection_t connection
= securityd_connection_for_operation((enum SecXPCOperation
)operation
);
311 const int max_tries
= SECURITYD_MAX_XPC_TRIES
;
313 unsigned int tries_left
= max_tries
;
315 if (reply
) xpc_release(reply
);
316 reply
= xpc_connection_send_message_with_reply_sync(connection
, message
);
317 } while (reply
== XPC_ERROR_CONNECTION_INTERRUPTED
&& --tries_left
> 0);
319 _securityd_process_message_reply(&reply
, error
, connection
, operation
);
325 _securityd_message_with_reply_async_inner(xpc_object_t message
,
326 dispatch_queue_t replyq
,
327 securityd_handler_t handler
,
330 uint64_t operation
= xpc_dictionary_get_uint64(message
, kSecXPCKeyOperation
);
331 xpc_connection_t connection
= securityd_connection_for_operation((enum SecXPCOperation
)operation
);
334 dispatch_retain(replyq
);
335 securityd_handler_t handlerCopy
= Block_copy(handler
);
336 xpc_connection_send_message_with_reply(connection
, message
, replyq
, ^(xpc_object_t _Nonnull reply
) {
337 if (reply
== XPC_ERROR_CONNECTION_INTERRUPTED
&& tries_left
> 0) {
338 _securityd_message_with_reply_async_inner(message
, replyq
, handlerCopy
, tries_left
- 1);
340 CFErrorRef error
= NULL
;
341 _securityd_process_message_reply(&reply
, &error
, connection
, operation
);
342 handlerCopy(reply
, error
);
343 CFReleaseNull(error
);
345 xpc_release(message
);
346 dispatch_release(replyq
);
347 Block_release(handlerCopy
);
352 securityd_message_with_reply_async(xpc_object_t message
,
353 dispatch_queue_t replyq
,
354 securityd_handler_t handler
)
356 _securityd_message_with_reply_async_inner(message
, replyq
, handler
, SECURITYD_MAX_XPC_TRIES
);
361 securityd_create_message(enum SecXPCOperation op
, CFErrorRef
* error
)
363 xpc_object_t message
= xpc_dictionary_create(NULL
, NULL
, 0);
365 xpc_dictionary_set_uint64(message
, kSecXPCKeyOperation
, op
);
367 SecCFCreateError(kSecXPCErrorConnectionFailed
, sSecXPCErrorDomain
,
368 CFSTR("xpc_dictionary_create returned NULL"), NULL
, error
);
373 // Return true if there is no error in message, return false and set *error if there is.
374 bool securityd_message_no_error(xpc_object_t message
, CFErrorRef
*error
) {
375 xpc_object_t xpc_error
= NULL
;
379 xpc_error
= xpc_dictionary_get_value(message
, kSecXPCKeyError
);
380 if (xpc_error
== NULL
)
383 CFErrorRef localError
= SecCreateCFErrorWithXPCObject(xpc_error
);
386 secdebug("xpc", "Talking to securityd failed with error: %@", localError
);
389 uint64_t operation
= xpc_dictionary_get_uint64(message
, kSecXPCKeyOperation
);
391 secdebug("xpc", "Talking to %s failed with error: %@",
392 (is_trust_operation((enum SecXPCOperation
)operation
)) ? "trustd" : "secd", localError
);
398 CFReleaseSafe(localError
);
403 bool securityd_send_sync_and_do(enum SecXPCOperation op
, CFErrorRef
*error
,
404 bool (^add_to_message
)(xpc_object_t message
, CFErrorRef
* error
),
405 bool (^handle_response
)(xpc_object_t _Nonnull response
, CFErrorRef
* error
)) {
406 xpc_object_t message
= securityd_create_message(op
, error
);
409 if (!add_to_message
|| add_to_message(message
, error
)) {
410 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
412 if (securityd_message_no_error(response
, error
)) {
413 ok
= (!handle_response
|| handle_response(response
, error
));
415 xpc_release(response
);
418 xpc_release(message
);
424 void securityd_send_async_and_do(enum SecXPCOperation op
, dispatch_queue_t replyq
,
425 bool (^add_to_message
)(xpc_object_t message
, CFErrorRef
* error
),
426 securityd_handler_t handler
) {
427 CFErrorRef error
= NULL
;
428 xpc_object_t message
= securityd_create_message(op
, &error
);
429 if (message
== NULL
) {
430 handler(NULL
, error
);
431 CFReleaseNull(error
);
435 if (add_to_message
!= NULL
) {
436 if (!add_to_message(message
, &error
)) {
437 handler(NULL
, error
);
438 xpc_release(message
);
439 CFReleaseNull(error
);
444 securityd_message_with_reply_async(message
, replyq
, ^(xpc_object_t reply
, CFErrorRef error2
) {
445 if (error2
!= NULL
) {
446 handler(NULL
, error2
);
449 CFErrorRef error3
= NULL
;
450 if (!securityd_message_no_error(reply
, &error3
)) {
451 handler(NULL
, error3
);
452 CFReleaseNull(error3
);
455 handler(reply
, NULL
);
457 xpc_release(message
);
462 _SecSecuritydCopyWhoAmI(CFErrorRef
*error
)
464 CFDictionaryRef reply
= NULL
;
465 xpc_object_t message
= securityd_create_message(kSecXPCOpWhoAmI
, error
);
467 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
469 reply
= _CFXPCCreateCFObjectFromXPCObject(response
);
470 xpc_release(response
);
472 secerror("Securityd failed getting whoamid with error: %@",
473 error
? *error
: NULL
);
475 xpc_release(message
);
481 _SecSyncBubbleTransfer(CFArrayRef services
, uid_t uid
, CFErrorRef
*error
)
483 xpc_object_t message
;
486 message
= securityd_create_message(kSecXPCOpTransmogrifyToSyncBubble
, error
);
488 xpc_dictionary_set_int64(message
, "uid", uid
);
489 if (SecXPCDictionarySetPList(message
, "services", services
, error
)) {
490 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
492 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
494 securityd_message_no_error(response
, error
);
495 xpc_release(response
);
497 xpc_release(message
);
504 _SecSystemKeychainTransfer(CFErrorRef
*error
)
506 xpc_object_t message
;
509 message
= securityd_create_message(kSecXPCOpTransmogrifyToSystemKeychain
, error
);
511 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
513 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
515 securityd_message_no_error(response
, error
);
516 xpc_release(response
);
518 xpc_release(message
);
524 _SecSyncDeleteUserViews(uid_t uid
, CFErrorRef
*error
)
526 xpc_object_t message
;
529 message
= securityd_create_message(kSecXPCOpDeleteUserView
, error
);
531 xpc_dictionary_set_int64(message
, "uid", uid
);
533 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
535 reply
= xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
537 securityd_message_no_error(response
, error
);
538 xpc_release(response
);
540 xpc_release(message
);
545 XPC_RETURNS_RETAINED xpc_endpoint_t
546 _SecSecuritydCopyEndpoint(enum SecXPCOperation op
, CFErrorRef
*error
)
548 xpc_endpoint_t endpoint
= NULL
;
549 xpc_object_t message
= securityd_create_message(op
, error
);
551 xpc_object_t response
= securityd_message_with_reply_sync(message
, error
);
553 endpoint
= xpc_dictionary_get_value(response
, kSecXPCKeyEndpoint
);
555 if(xpc_get_type(endpoint
) != XPC_TYPE_ENDPOINT
) {
556 secerror("endpoint was not an endpoint");
559 xpc_retain(endpoint
);
562 secerror("endpoint was null");
564 xpc_release(response
);
566 secerror("Securityd failed getting endpoint with error: %@", error
? *error
: NULL
);
568 xpc_release(message
);
574 XPC_RETURNS_RETAINED xpc_endpoint_t
575 _SecSecuritydCopyCKKSEndpoint(CFErrorRef
*error
)
580 XPC_RETURNS_RETAINED xpc_endpoint_t
581 _SecSecuritydCopyKeychainControlEndpoint(CFErrorRef
* error
)
583 return _SecSecuritydCopyEndpoint(kSecXPCOpKeychainControlEndpoint
, error
);
586 XPC_RETURNS_RETAINED xpc_endpoint_t
587 _SecSecuritydCopySFKeychainEndpoint(CFErrorRef
* error
)
589 return _SecSecuritydCopyEndpoint(kSecXPCOpSFKeychainEndpoint
, error
);