1 /* Copyright (c) 2012-2014 Apple Inc. All Rights Reserved. */
3 #include "securityd_service.h"
4 #include "securityd_service_client.h"
10 #include <xpc/private.h>
11 #include <dispatch/dispatch.h>
12 #include <sys/types.h>
14 #include <sys/codesign.h>
21 #include <uuid/uuid.h>
22 #include <bsm/libbsm.h>
24 #include <AssertMacros.h>
25 #include <Security/Security.h>
26 #include <Security/SecTask.h>
27 #include <Security/SecTaskPriv.h>
28 #include <Security/SecKeychainPriv.h>
29 #include <MobileKeyBag/MobileKeyBag.h>
31 #include <IOKit/IOKitLib.h>
32 #include <Kernel/IOKit/crypto/AppleFDEKeyStoreDefs.h>
34 #define LOG(...) os_log_debug(OS_LOG_DEFAULT, ##__VA_ARGS__);
36 static bool check_signature(xpc_connection_t connection
);
38 static pid_t
get_caller_pid(audit_token_t
* token
)
42 audit_token_to_au32(*token
, NULL
, NULL
, NULL
, NULL
, NULL
, &pid
, NULL
, NULL
);
47 // exported from libaks.a
48 kern_return_t
aks_register_for_notifications(mach_port_t server_port
, uintptr_t message_id
);
49 kern_return_t
aks_stash_escrow(keybag_handle_t handle
, bool create
, const void * secret
, int secret_len
, const void * in_data
, int in_data_len
, void ** out_data
, int * out_data_len
);
51 const char * kb_home_path
= "Library/Keychains";
52 const char * kb_user_bag
= "user.kb";
53 const char * kb_stash_bag
= "stash.kb";
55 #define HEXBUF_LEN 2048
62 } service_user_record_t
;
72 io_registry_entry_t service
;
76 service
= IOServiceGetMatchingService(kIOMasterPortDefault
, IOServiceMatching(kAppleFDEKeyStoreServiceName
));
77 if (service
== IO_OBJECT_NULL
)
78 return IO_OBJECT_NULL
;
80 kr
= IOServiceOpen(service
, mach_task_self(), 0, &conn
);
81 if (kr
!= KERN_SUCCESS
)
82 return IO_OBJECT_NULL
;
84 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStoreUserClientOpen
, NULL
, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
85 if (kr
!= KERN_SUCCESS
) {
87 return IO_OBJECT_NULL
;
94 closeiodev(io_connect_t conn
)
97 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStoreUserClientClose
, NULL
, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
98 if (kr
!= KERN_SUCCESS
)
100 IOServiceClose(conn
);
103 static dispatch_queue_t
104 _kb_service_get_dispatch_queue()
106 static dispatch_once_t onceToken
= 0;
107 static dispatch_queue_t connection_queue
= NULL
;
109 dispatch_once(&onceToken
, ^{
110 connection_queue
= dispatch_queue_create("kb-service-queue", DISPATCH_QUEUE_SERIAL
);
113 return connection_queue
;
116 static service_user_record_t
* get_user_record(uid_t uid
)
118 service_user_record_t
* ur
= NULL
;
120 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1) {
124 struct passwd pwbuf
, *pw
= NULL
;
126 if (((rc
= getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
)) == 0) && pw
!= NULL
) {
127 ur
= calloc(1u, sizeof(service_user_record_t
));
129 ur
->uid
= pw
->pw_uid
;
130 ur
->gid
= pw
->pw_gid
;
131 ur
->home
= strdup(pw
->pw_dir
);
132 ur
->name
= strdup(pw
->pw_name
);
134 os_log(OS_LOG_DEFAULT
, "failed (%d) to lookup user record for uid: %d", rc
, uid
);
141 static void free_user_record(service_user_record_t
* ur
)
154 static const char * get_host_uuid()
156 static uuid_string_t hostuuid
= {};
157 static dispatch_once_t onceToken
;
158 dispatch_once(&onceToken
, ^{
159 struct timespec timeout
= {30, 0};
161 if (gethostuuid(uuid
, &timeout
) == 0) {
162 uuid_unparse(uuid
, hostuuid
);
164 os_log(OS_LOG_DEFAULT
, "failed to get host uuid");
173 _kb_copy_bag_filename(service_user_record_t
* ur
, kb_bag_type_t type
)
175 char * bag_file
= NULL
;
176 const char * name
= NULL
;
180 case kb_bag_type_user
:
183 case kb_bag_type_stash
:
190 bag_file
= calloc(1u, PATH_MAX
);
191 require(bag_file
, done
);
193 snprintf(bag_file
, PATH_MAX
, "%s/%s/%s/%s", ur
->home
, kb_home_path
, get_host_uuid(), name
);
200 _kb_verify_create_path(service_user_record_t
* ur
)
202 bool created
= false;
203 struct stat st_info
= {};
204 char new_path
[PATH_MAX
] = {};
205 char kb_path
[PATH_MAX
] = {};
206 snprintf(kb_path
, sizeof(kb_path
), "%s/%s/%s", ur
->home
, kb_home_path
, get_host_uuid());
207 if (lstat(kb_path
, &st_info
) == 0) {
208 if (S_ISDIR(st_info
.st_mode
)) {
211 os_log(OS_LOG_DEFAULT
, "invalid directory at '%s' moving aside", kb_path
);
212 snprintf(new_path
, sizeof(new_path
), "%s-invalid", kb_path
);
214 if (rename(kb_path
, new_path
) != 0) {
215 os_log(OS_LOG_DEFAULT
, "failed to rename file: %s (%s)", kb_path
, strerror(errno
));
221 require_action(mkpath_np(kb_path
, 0700) == 0, done
, os_log(OS_LOG_DEFAULT
, "could not create path: %s (%s)", kb_path
, strerror(errno
)));
227 os_log(OS_LOG_DEFAULT
, "_kb_verify_create_path failed %s", kb_path
);
233 _set_thread_credentials(service_user_record_t
* ur
)
235 int rc
= pthread_setugid_np(ur
->uid
, ur
->gid
);
236 if (rc
) { os_log(OS_LOG_DEFAULT
, "failed to set thread credential: %i (%s)", errno
, strerror(errno
)); }
238 rc
= initgroups(ur
->name
, ur
->gid
);
239 if (rc
) { os_log(OS_LOG_DEFAULT
, "failed to initgroups: %i", rc
); }
243 _clear_thread_credentials()
245 int rc
= pthread_setugid_np(KAUTH_UID_NONE
, KAUTH_GID_NONE
);
246 if (rc
) { os_log(OS_LOG_DEFAULT
, "failed to reset thread credential: %i (%s)", errno
, strerror(errno
)); }
250 _kb_bag_exists(service_user_record_t
* ur
, const char * bag_file
)
253 struct stat st_info
= {};
254 char new_file
[PATH_MAX
] = {};
258 _set_thread_credentials(ur
);
259 if (lstat(bag_file
, &st_info
) == 0) {
260 if (S_ISREG(st_info
.st_mode
)) {
263 os_log(OS_LOG_DEFAULT
, "invalid file at '%s' moving aside", bag_file
);
264 snprintf(new_file
, sizeof(new_file
), "%s-invalid", bag_file
);
266 if (rename(bag_file
, new_file
) != 0) {
267 os_log(OS_LOG_DEFAULT
, "failed to rename file: %s (%s)", bag_file
, strerror(errno
));
273 _clear_thread_credentials();
278 _kb_save_bag_to_disk(service_user_record_t
* ur
, const char * bag_file
, void * data
, size_t length
)
283 require(bag_file
, done
);
285 _set_thread_credentials(ur
);
286 require(_kb_verify_create_path(ur
), done
);
288 fd
= open(bag_file
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_NOFOLLOW
, 0600);
289 require_action(fd
!= -1, done
, os_log(OS_LOG_DEFAULT
, "could not create file: %s (%s)", bag_file
, strerror(errno
)));
290 require_action(write(fd
, data
, length
) == length
, done
, os_log(OS_LOG_DEFAULT
, "failed to write keybag to disk %s (%s)", bag_file
, strerror(errno
)));
295 if (fd
!= -1) { close(fd
); }
296 _clear_thread_credentials();
301 _kb_load_bag_from_disk(service_user_record_t
* ur
, const char * bag_file
, uint8_t ** data
, size_t * length
)
305 uint8_t * buf
= NULL
;
307 struct stat st_info
= {};
309 require(bag_file
, done
);
311 _set_thread_credentials(ur
);
312 require(_kb_verify_create_path(ur
), done
);
313 require_action_quiet(lstat(bag_file
, &st_info
) == 0, done
, os_log(OS_LOG_DEFAULT
, "failed to stat file: %s (%s)", bag_file
, strerror(errno
)));
314 require_action(S_ISREG(st_info
.st_mode
), done
, os_log(OS_LOG_DEFAULT
, "failed to load, not a file: %s", bag_file
));
315 buf_size
= (size_t)st_info
.st_size
;
317 fd
= open(bag_file
, O_RDONLY
| O_NOFOLLOW
);
318 require_action(fd
!= -1, done
, os_log(OS_LOG_DEFAULT
, "could not open file: %s (%s)", bag_file
, strerror(errno
)));
320 buf
= (uint8_t *)calloc(1u, buf_size
);
321 require(buf
!= NULL
, done
);
322 require(read(fd
, buf
, buf_size
) == buf_size
, done
);
330 if (fd
!= -1) { close(fd
); }
331 if (buf
) { free(buf
); }
332 _clear_thread_credentials();
337 _kb_invalidate_bag(service_user_record_t
*ur
, const char * bag_file
)
344 if (_kb_load_bag_from_disk(ur
, bag_file
, &buf
, &buf_size
)) {
345 require_action(buf
&& buf_size
<= INT_MAX
, out
, os_log(OS_LOG_DEFAULT
, "failed to read: %s", bag_file
));
346 require_noerr_action(aks_invalidate_bag(buf
, (int)buf_size
), out
, os_log(OS_LOG_DEFAULT
, "failed to invalidate file: %s", bag_file
));
348 os_log(OS_LOG_DEFAULT
, "failed to read file: %s", bag_file
);
356 _kb_rename_bag_on_disk(service_user_record_t
* ur
, const char * bag_file
)
358 char new_file
[PATH_MAX
] = {};
360 _set_thread_credentials(ur
);
361 snprintf(new_file
, sizeof(new_file
), "%s-invalid", bag_file
);
363 rename(bag_file
, new_file
);
364 _kb_invalidate_bag(ur
, new_file
);
365 _clear_thread_credentials();
370 _kb_delete_bag_on_disk(service_user_record_t
* ur
, const char * bag_file
)
373 _set_thread_credentials(ur
);
374 _kb_invalidate_bag(ur
, bag_file
);
376 _clear_thread_credentials();
380 static int service_kb_load(service_context_t
*context
);
381 static int service_kb_load_uid(uid_t s_uid
);
383 #ifndef AKS_MACOS_ROOT_HANDLE
384 #define AKS_MACOS_ROOT_HANDLE 4 //temporary define to avoid dependency on AKS change, filed rdar://problem/30542034
385 #endif /* AKS_MACOS_ROOT_HANDLE */
388 _service_kb_set_system(keybag_handle_t handle
, keybag_handle_t special_handle
)
390 //Use reserved root handle for root sessions, since 0 clashes with device_keybag_handle in AKS
391 return aks_set_system(handle
, (special_handle
== 0) ? AKS_MACOS_ROOT_HANDLE
: special_handle
);
395 _service_kb_get_system(keybag_handle_t special_handle
, keybag_handle_t
* handle_out
)
397 //Use reserved root handle for root sessions, since 0 clashes with device_keybag_handle in AKS
398 return aks_get_system((special_handle
== 0) ? AKS_MACOS_ROOT_HANDLE
: special_handle
, handle_out
);
402 _kb_get_session_handle(service_context_t
* context
, keybag_handle_t
* handle_out
)
404 int rc
= KB_BagNotLoaded
;
405 require_noerr_quiet(_service_kb_get_system(context
->s_uid
, handle_out
), done
);
410 if (rc
== KB_BagNotLoaded
) {
411 if (service_kb_load(context
) == KB_Success
) {
412 if (_service_kb_get_system(context
->s_uid
, handle_out
) == kIOReturnSuccess
) {
420 static void update_keybag_handle(keybag_handle_t handle
)
422 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
423 uid_t uid
= abs(handle
);
424 uint8_t * buf
= NULL
;
426 service_user_record_t
* ur
= NULL
;
427 char * bag_file
= NULL
;
429 require_noerr(aks_save_bag(handle
, (void**)&buf
, (int*)&buf_size
), done
);
430 require(ur
= get_user_record(uid
), done
);
431 require(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
);
432 require(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
);
434 os_log(OS_LOG_DEFAULT
, "successfully updated handle %d", handle
);
438 if (ur
) free_user_record(ur
);
439 if (bag_file
) free(bag_file
);
444 _kb_get_options_for_uid(uid_t uid
, CFMutableDictionaryRef
*options_out
)
446 int result
= KB_GeneralError
;
447 CFMutableDictionaryRef options
= NULL
;
448 CFNumberRef cf_uid
= NULL
;
450 require(options_out
, out
);
452 require(options
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
), out
);
453 require(cf_uid
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &uid
), out
);
454 CFDictionaryAddValue(options
, kKeyBagDeviceHandle
, cf_uid
);
456 *options_out
= options
;
461 if (options
) { CFRelease(options
); }
462 if (cf_uid
) { CFRelease(cf_uid
); }
468 _kb_set_user_uuid(service_context_t
* context
, const void * secret
, int secret_len
)
470 int result
= KB_GeneralError
;
471 CFMutableDictionaryRef options
= NULL
;
472 CFDataRef passcode
= NULL
;
474 require_noerr(_kb_get_options_for_uid(context
->s_uid
, &options
), done
);
476 /* set user uuid, if not already set */
477 passcode
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, secret
, secret_len
, kCFAllocatorNull
);
478 MKBKeyBagSetUserUUID(options
, passcode
);
482 if (options
) { CFRelease(options
); }
483 if (passcode
) { CFRelease(passcode
); }
488 service_kb_create(service_context_t
* context
, const void * secret
, int secret_len
)
490 __block
int rc
= KB_GeneralError
;
492 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
493 uint8_t * buf
= NULL
;
495 keybag_handle_t private_handle
= bad_keybag_handle
, session_handle
= bad_keybag_handle
;
496 service_user_record_t
* ur
= get_user_record(context
->s_uid
);
497 char * bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
);
499 require(bag_file
, done
);
501 // check for the existance of the bagfile
502 require_action(!_kb_bag_exists(ur
, bag_file
), done
, rc
= KB_BagExists
);
504 require_noerr(rc
= aks_create_bag(secret
, secret_len
, kAppleKeyStoreDeviceBag
, &private_handle
), done
);
505 require_noerr(rc
= aks_save_bag(private_handle
, (void**)&buf
, (int*)&buf_size
), done
);
506 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
507 require_noerr(rc
= _service_kb_set_system(private_handle
, context
->s_uid
), done
);
508 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
510 if (secret
&& rc
== KB_Success
) {
511 aks_unlock_bag(session_handle
, secret
, secret_len
);
514 if (rc
== KB_Success
) {
515 _kb_set_user_uuid(context
, secret
, secret_len
);
519 if (private_handle
!= bad_keybag_handle
) {
520 aks_unload_bag(private_handle
);
523 if (bag_file
) { free(bag_file
); }
524 if (ur
) free_user_record(ur
);
530 /* Load s_uid's keybag, unless already loaded */
532 _service_kb_load_uid(uid_t s_uid
)
534 __block
int rc
= KB_GeneralError
;
536 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
537 uint8_t * buf
= NULL
;
539 keybag_handle_t private_handle
= bad_keybag_handle
, session_handle
= bad_keybag_handle
;
540 service_user_record_t
* ur
= NULL
;
541 char * bag_file
= NULL
;
544 rc
= _service_kb_get_system(s_uid
, &session_handle
);
545 if (rc
== kIOReturnNotFound
) {
546 require_action(ur
= get_user_record(s_uid
), done
, rc
= KB_GeneralError
; _stage
= 1);
547 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
; _stage
= 2);
548 require_action_quiet(_kb_load_bag_from_disk(ur
, bag_file
, &buf
, &buf_size
), done
, rc
= KB_BagNotFound
; _stage
= 3);
549 rc
= aks_load_bag(buf
, (int)buf_size
, &private_handle
);
551 case kAKSReturnBadDeviceKey
:
552 case kAKSReturnBadSignature
:
553 case kAKSReturnDecodeError
:
554 case kAKSReturnPolicyInvalid
:
555 os_log(OS_LOG_DEFAULT
, "bag load failed 0x%x for uid (%i), discarding", rc
, s_uid
);
556 _kb_rename_bag_on_disk(ur
, bag_file
);
559 case kAKSReturnSuccess
:
563 os_log(OS_LOG_DEFAULT
, "bag load failed 0x%x for uid (%i)", rc
, s_uid
);
566 require_noerr(rc
, done
; _stage
= 4);
567 require_noerr(rc
= _service_kb_set_system(private_handle
, s_uid
), done
; _stage
= 5);
569 require(rc
== KB_Success
, done
);
572 if (private_handle
!= bad_keybag_handle
) {
573 aks_unload_bag(private_handle
);
575 // this function should never fail unless bootstrapping the user for the first time, or rare conditions from aks_load_bag
576 if (rc
!= KB_Success
) {
577 os_log(OS_LOG_DEFAULT
, "%d: error %d loading keybag for uid (%i) at path: %s", _stage
, rc
, s_uid
, bag_file
);
580 if (ur
) free_user_record(ur
);
581 if (bag_file
) free(bag_file
);
588 service_kb_load_uid(uid_t s_uid
)
590 return _service_kb_load_uid(s_uid
);
594 service_kb_load(service_context_t
* context
)
596 return _service_kb_load_uid(context
->s_uid
);
600 service_kb_unload(service_context_t
*context
)
602 __block
int rc
= KB_GeneralError
;
604 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
605 keybag_handle_t session_handle
= bad_keybag_handle
;
607 rc
= _service_kb_get_system(context
->s_uid
, &session_handle
);
608 if (rc
== kIOReturnNotFound
) {
609 // No session bag, nothing to do
612 } else if (rc
!= kIOReturnSuccess
) {
613 os_log(OS_LOG_DEFAULT
, "error locating session keybag for uid (%i) in session (%i)", context
->s_uid
, context
->s_id
);
618 rc
= aks_unload_bag(session_handle
);
619 if (rc
!= kAKSReturnSuccess
) {
620 os_log(OS_LOG_DEFAULT
, "error unloading keybag for uid (%i) in session (%i)", context
->s_uid
, context
->s_id
);
623 os_log(OS_LOG_DEFAULT
, "successfully unloaded keybag (%ld) for uid (%i) in session (%i)", (long)session_handle
, context
->s_uid
, context
->s_id
);
631 service_kb_save(service_context_t
* context
)
633 __block
int rc
= KB_GeneralError
;
634 keybag_handle_t session_handle
;
635 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
637 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
638 uint8_t * buf
= NULL
;
640 service_user_record_t
* ur
= NULL
;
641 char * bag_file
= NULL
;
643 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
644 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
645 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
646 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
652 if (ur
) free_user_record(ur
);
653 if (bag_file
) free(bag_file
);
662 service_kb_unlock(service_context_t
* context
, const void * secret
, int secret_len
)
664 int rc
= KB_GeneralError
;
665 keybag_handle_t session_handle
;
666 CFDataRef passcode
= NULL
;
667 CFMutableDictionaryRef options
= NULL
;
669 require_noerr(_kb_get_options_for_uid(context
->s_uid
, &options
), done
);
671 /* technically, session_handle is not needed. Call this to handle lazy keybag loading */
672 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
674 require(passcode
= CFDataCreateWithBytesNoCopy(NULL
, secret
, secret_len
, kCFAllocatorNull
), done
);
676 rc
= MKBUnlockDevice(passcode
, options
);
677 os_log(OS_LOG_DEFAULT
, "MKBUnlockDevice result: (%ld)", (long)rc
);
680 if (options
) { CFRelease(options
); }
681 if (passcode
) { CFRelease(passcode
); }
686 service_kb_lock(service_context_t
* context
)
688 // this call has been disabled
693 service_kb_change_secret(service_context_t
* context
, const void * secret
, int secret_len
, const void * new_secret
, int new_secret_len
)
695 __block
int rc
= KB_GeneralError
;
696 keybag_handle_t session_handle
;
697 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
699 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
700 uint8_t * buf
= NULL
;
702 service_user_record_t
* ur
= NULL
;
703 char * bag_file
= NULL
;
705 require_noerr(rc
= aks_change_secret(session_handle
, secret
, secret_len
, new_secret
, new_secret_len
, generation_noop
, NULL
), done
);
706 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
707 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
708 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
709 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
715 if (ur
) free_user_record(ur
);
716 if (bag_file
) free(bag_file
);
725 service_kb_reset(service_context_t
* context
, const void * secret
, int secret_len
)
727 __block
int rc
= KB_GeneralError
;
728 service_user_record_t
* ur
= NULL
;
729 char * bag_file
= NULL
;
731 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
732 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
734 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
735 uint8_t * buf
= NULL
;
737 keybag_handle_t private_handle
= bad_keybag_handle
, session_handle
= bad_keybag_handle
;
739 os_log(OS_LOG_DEFAULT
, "resetting keybag for uid (%i) in session (%i)", context
->s_uid
, context
->s_id
);
740 _kb_rename_bag_on_disk(ur
, bag_file
);
742 require_noerr(rc
= aks_create_bag(secret
, secret_len
, kAppleKeyStoreDeviceBag
, &private_handle
), done
);
743 require_noerr(rc
= aks_save_bag(private_handle
, (void**)&buf
, (int*)&buf_size
), done
);
744 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
745 require_noerr(rc
= _service_kb_set_system(private_handle
, context
->s_uid
), done
);
746 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
748 if (secret
&& rc
== KB_Success
) {
749 aks_unlock_bag(session_handle
, secret
, secret_len
);
752 if (rc
== KB_Success
) {
753 _kb_set_user_uuid(context
, secret
, secret_len
);
757 if (private_handle
!= bad_keybag_handle
) {
758 aks_unload_bag(private_handle
);
765 if (ur
) free_user_record(ur
);
766 if (bag_file
) free(bag_file
);
771 service_kb_is_locked(service_context_t
* context
, xpc_object_t reply
)
773 int rc
= KB_GeneralError
;
774 keybag_state_t state
;
775 keybag_handle_t session_handle
;
776 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
778 require_noerr(rc
= aks_get_lock_state(session_handle
, &state
), done
);
780 xpc_dictionary_set_bool(reply
, SERVICE_XPC_LOCKED
, state
& keybag_state_locked
);
781 xpc_dictionary_set_bool(reply
, SERVICE_XPC_NO_PIN
, state
& keybag_state_no_pin
);
788 service_kb_wrap_key(service_context_t
*context
, xpc_object_t event
, xpc_object_t reply
)
790 int rc
= KB_GeneralError
;
794 keyclass_t key_class
;
795 keybag_handle_t session_handle
;
796 void *wrapped_key
= NULL
;
797 int wrapped_key_size
;
798 keyclass_t wrapped_key_class
;
800 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
802 key
= xpc_dictionary_get_data(event
, SERVICE_XPC_KEY
, &sz
);
803 require_action(key
!= NULL
, done
, rc
= KB_GeneralError
);
804 require_action(sz
<= APPLE_KEYSTORE_MAX_KEY_LEN
, done
, rc
= KB_GeneralError
);
806 key_class
= (keyclass_t
)xpc_dictionary_get_int64(event
, SERVICE_XPC_KEYCLASS
);
808 wrapped_key_size
= APPLE_KEYSTORE_MAX_ASYM_WRAPPED_KEY_LEN
;
809 wrapped_key
= calloc(1, wrapped_key_size
);
811 rc
= aks_wrap_key(key
, key_size
, key_class
, session_handle
, wrapped_key
, &wrapped_key_size
, &wrapped_key_class
);
812 if (rc
== KB_Success
) {
813 xpc_dictionary_set_data(reply
, SERVICE_XPC_WRAPPED_KEY
, wrapped_key
, wrapped_key_size
);
814 xpc_dictionary_set_int64(reply
, SERVICE_XPC_KEYCLASS
, wrapped_key_class
);
823 service_kb_unwrap_key(service_context_t
*context
, xpc_object_t event
, xpc_object_t reply
)
825 int rc
= KB_GeneralError
;
827 const void *wrapped_key
;
828 int wrapped_key_size
;
829 keyclass_t wrapped_key_class
;
830 keybag_handle_t session_handle
;
834 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
836 wrapped_key
= xpc_dictionary_get_data(event
, SERVICE_XPC_WRAPPED_KEY
, &sz
);
837 require_action(wrapped_key
!= NULL
, done
, rc
= KB_GeneralError
);
838 require_action(sz
<= APPLE_KEYSTORE_MAX_ASYM_WRAPPED_KEY_LEN
, done
, rc
= KB_GeneralError
);
839 wrapped_key_size
= (int)sz
;
840 wrapped_key_class
= (keyclass_t
)xpc_dictionary_get_int64(event
, SERVICE_XPC_KEYCLASS
);
842 key_size
= APPLE_KEYSTORE_MAX_KEY_LEN
;
843 key
= calloc(1, key_size
);
845 rc
= aks_unwrap_key(wrapped_key
, wrapped_key_size
, wrapped_key_class
, session_handle
, key
, &key_size
);
846 if (rc
== KB_Success
) {
847 xpc_dictionary_set_data(reply
, SERVICE_XPC_KEY
, key
, key_size
);
856 service_kb_stash_create(service_context_t
* context
, const void * key
, unsigned key_size
)
858 int rc
= KB_GeneralError
;
859 char * bag_file
= NULL
;
860 keybag_handle_t session_handle
;
861 service_user_record_t
* ur
= NULL
;
862 void * stashbag
= NULL
;
863 int stashbag_size
= 0;
864 __block
bool saved
= false;
867 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
868 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
869 require_noerr(rc
= aks_stash_escrow(session_handle
, true, key
, key_size
, NULL
, 0, (void**)&stashbag
, &stashbag_size
), done
);
870 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_stash
), done
, rc
= KB_GeneralError
);
872 // sync writing the bag to disk
873 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
874 saved
= _kb_save_bag_to_disk(ur
, bag_file
, stashbag
, stashbag_size
);
876 require_action(saved
, done
, rc
= KB_BagError
);
880 if (stashbag
) { free(stashbag
); }
881 if (bag_file
) { free(bag_file
); }
882 if (ur
) free_user_record(ur
);
887 service_kb_stash_load(service_context_t
* context
, const void * key
, unsigned key_size
, bool nondestructive
)
889 __block
int rc
= KB_GeneralError
;
890 char * bag_file
= NULL
;
891 keybag_handle_t session_handle
;
892 service_user_record_t
* ur
= NULL
;
893 __block
uint8_t * stashbag
= NULL
;
894 __block
size_t stashbag_size
= 0;
897 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
898 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
899 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_stash
), done
, rc
= KB_GeneralError
);
901 // sync loading the bag from disk
902 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
903 if (!_kb_load_bag_from_disk(ur
, bag_file
, &stashbag
, &stashbag_size
)) {
907 require_noerr(rc
, done
);
909 require_noerr(rc
= aks_stash_escrow(session_handle
, false, key
, key_size
, stashbag
, (int)stashbag_size
, NULL
, NULL
), done
);
913 if (stashbag
) { free(stashbag
); }
914 if ((bag_file
) && (!nondestructive
)) {
915 _kb_delete_bag_on_disk(ur
, bag_file
);
918 if (ur
) free_user_record(ur
);
923 // Get the keychain master key from the AppleFDEKeyStore.
924 // Note that this is a one-time call - the master key is
925 // removed from the keystore after it is returned.
926 // Requires the entitlement: com.apple.private.securityd.keychain
928 OSStatus
service_stash_get_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
930 getStashKey_InStruct_t inStruct
;
931 getStashKey_OutStruct_t outStruct
;
932 size_t outSize
= sizeof(outStruct
);
933 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
935 io_connect_t conn
= openiodev();
937 inStruct
.type
= kAppleFDEKeyStoreStash_master
;
939 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_getStashKey
,
941 &inStruct
, sizeof(inStruct
),
943 &outStruct
, &outSize
);
945 if (kr
== KERN_SUCCESS
) {
946 xpc_dictionary_set_data(reply
, SERVICE_XPC_KEY
, outStruct
.outBuf
.key
.key
, outStruct
.outBuf
.key
.keysize
);
947 service_kb_stash_load(context
, outStruct
.outBuf
.key
.key
, outStruct
.outBuf
.key
.keysize
, false);
949 os_log(OS_LOG_DEFAULT
, "failed to get stash key: %d", (int)kr
);
960 // Stash the keychain master key in the AppleFDEKeyStore and
961 // flag it as the keychain master key to be added to the
962 // reboot NVRAM blob.
963 // This requires two calls to the AKS: the first to store the
964 // key and get its uuid. The second uses the uuid to flag the
965 // key for blob inclusion.
967 OSStatus
service_stash_set_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
969 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
970 io_connect_t conn
= IO_OBJECT_NULL
;
971 size_t keydata_len
= 0;
974 keybag_state_t state
;
975 keybag_handle_t session_handle
;
976 require_noerr(_kb_get_session_handle(context
, &session_handle
), done
);
977 require_noerr(aks_get_lock_state(session_handle
, &state
), done
);
978 require_action(!(state
& keybag_lock_locked
), done
, kr
= CSSMERR_CSP_OS_ACCESS_DENIED
; LOG("stash failed keybag locked"));
983 // Store the key in the keystore and get its uuid
984 setKeyGetUUID_InStruct_t inStruct1
;
985 uuid_OutStruct_t outStruct1
;
988 const uint8_t *keydata
= xpc_dictionary_get_data(event
, SERVICE_XPC_KEY
, &keydata_len
);
989 require(keydata
, done
);
991 memcpy(&inStruct1
.inKey
.key
.key
, keydata
, keydata_len
);
992 inStruct1
.inKey
.key
.keysize
= (cryptosize_t
) keydata_len
;
993 len
= sizeof(outStruct1
);
994 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_setKeyGetUUID
,
996 &inStruct1
, sizeof(inStruct1
),
999 require(kr
== KERN_SUCCESS
, done
);
1001 // Now using the uuid stash it as the master key
1002 setStashKey_InStruct_t inStruct2
;
1003 memcpy(&inStruct2
.uuid
, &outStruct1
.uuid
, sizeof(outStruct1
.uuid
));
1004 inStruct2
.type
= kAppleFDEKeyStoreStash_master
;
1006 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_setStashKey
,
1008 &inStruct2
, sizeof(inStruct2
),
1012 if (kr
== KERN_SUCCESS
) {
1013 service_kb_stash_create(context
, keydata
, (unsigned)keydata_len
);
1016 os_log(OS_LOG_DEFAULT
, "set stashkey %d", (int)kr
);
1025 // Load the master stash key
1027 OSStatus
service_stash_load_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
1029 kern_return_t kr
= KERN_SUCCESS
;
1030 size_t keydata_len
= 0;
1032 const uint8_t *keydata
= xpc_dictionary_get_data(event
, SERVICE_XPC_KEY
, &keydata_len
);
1033 require(keydata
, done
);
1035 kr
= service_kb_stash_load(context
, keydata
, (cryptosize_t
) keydata_len
, true);
1042 // Signal the AppleFDEKeyStore to take the tagged FDE key
1043 // and keychain master key, stash them in an encrypted
1044 // blob structure and write the blob to NVRAM. The random
1045 // encryption key is written to the SMC.
1048 OSStatus
service_stash_blob(xpc_object_t event
, xpc_object_t reply
)
1050 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
1052 io_connect_t conn
= openiodev();
1053 require(conn
, done
);
1055 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_commitStash
,
1068 bool peer_has_entitlement(xpc_connection_t peer
, const char * entitlement
)
1070 bool entitled
= false;
1072 xpc_object_t value
= xpc_connection_copy_entitlement_value(peer
, entitlement
);
1073 if (value
&& (xpc_get_type(value
) == XPC_TYPE_BOOL
)) {
1074 entitled
= xpc_bool_get_value(value
);
1077 if (value
) xpc_release(value
);
1081 static char * sel_to_char(uint64_t sel
)
1084 case SERVICE_STASH_SET_KEY
:
1086 case SERVICE_STASH_GET_KEY
:
1088 case SERVICE_STASH_BLOB
:
1089 return "stash_blob";
1090 case SERVICE_KB_LOAD
:
1092 case SERVICE_KB_SAVE
:
1094 case SERVICE_KB_UNLOCK
:
1096 case SERVICE_KB_LOCK
:
1098 case SERVICE_KB_CHANGE_SECRET
:
1099 return "kb_change_secret";
1100 case SERVICE_KB_CREATE
:
1102 case SERVICE_KB_IS_LOCKED
:
1103 return "kb_is_locked";
1104 case SERVICE_KB_RESET
:
1106 case SERVICE_KB_UNLOAD
:
1108 case SERVICE_KB_LOAD_UID
:
1109 return "kb_load_uid";
1110 case SERVICE_KB_WRAP_KEY
:
1111 return "kb_wrap_key";
1112 case SERVICE_KB_UNWRAP_KEY
:
1113 return "kb_unwrap_key";
1119 static char * err_to_char(int err
)
1124 case KB_GeneralError
:
1125 return "general error";
1126 case KB_BagNotFound
:
1127 return "bag not found";
1130 case KB_BagNotLoaded
:
1131 return "bag not loaded";
1133 return "bag exists";
1134 case KB_InvalidSession
:
1135 return "invalid session";
1141 void service_peer_event_handler(xpc_connection_t connection
, xpc_object_t event
)
1143 xpc_type_t type
= xpc_get_type(event
);
1146 if (type
== XPC_TYPE_ERROR
) {
1147 if (event
== XPC_ERROR_CONNECTION_INVALID
) {
1150 assert(type
== XPC_TYPE_DICTIONARY
);
1152 int rc
= KB_GeneralError
;
1153 uint64_t request
= 0;
1154 const uint8_t * secret
= NULL
, * new_secret
= NULL
;
1155 size_t secret_len
= 0, new_secret_len
= 0, data_len
= 0;
1156 service_context_t
* context
= NULL
;
1157 bool free_context
= false;
1159 const char *entitlement
;
1161 xpc_object_t reply
= xpc_dictionary_create_reply(event
);
1163 request
= xpc_dictionary_get_uint64(event
, SERVICE_XPC_REQUEST
);
1166 // For SERVICE_KB_{UNLOAD,LOAD} only, allow non-securityd, non-root but
1167 // entitled callers.
1168 if (request
== SERVICE_KB_UNLOAD
|| request
== SERVICE_KB_LOAD_UID
) {
1170 case SERVICE_KB_UNLOAD
:
1171 entitlement
= "com.apple.private.securityd.keybag-unload";
1173 case SERVICE_KB_LOAD_UID
:
1174 entitlement
= "com.apple.private.securityd.keybag-load";
1177 if (!peer_has_entitlement(connection
, entitlement
) && !peer_has_entitlement(connection
, "com.apple.keystore.device")) {
1178 xpc_connection_cancel(connection
);
1182 if (xpc_connection_get_euid(connection
) != 0) {
1183 xpc_connection_cancel(connection
);
1186 if (!check_signature(connection
)) {
1187 xpc_connection_cancel(connection
);
1192 data
= xpc_dictionary_get_data(event
, SERVICE_XPC_CONTEXT
, &data_len
);
1193 require_action(data
|| request
== SERVICE_KB_UNLOAD
|| request
== SERVICE_KB_LOAD_UID
, done
, rc
= KB_GeneralError
);
1195 require(data_len
== sizeof(service_context_t
), done
);
1196 context
= (service_context_t
*)data
;
1198 audit_token_t audit_token
= { 0 };
1199 xpc_connection_get_audit_token(connection
, &audit_token
);
1200 context
= calloc(1, sizeof(service_context_t
));
1201 context
->s_id
= xpc_connection_get_asid(connection
);
1202 context
->s_uid
= xpc_connection_get_euid(connection
);
1203 context
->procToken
= audit_token
;
1204 free_context
= true;
1207 require_action(context
->s_id
!= AU_DEFAUDITSID
, done
, rc
= KB_InvalidSession
);
1208 require_action(context
->s_uid
!= AU_DEFAUDITID
, done
, rc
= KB_InvalidSession
); // we only want to work in actual user sessions.
1211 case SERVICE_KB_CREATE
:
1212 // if (kb_service_has_entitlement(peer, "com.apple.keystore.device")) {
1213 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
1214 rc
= service_kb_create(context
, secret
, (int)secret_len
);
1217 case SERVICE_KB_LOAD
:
1218 rc
= service_kb_load(context
);
1220 case SERVICE_KB_UNLOAD
:
1221 rc
= service_kb_unload(context
);
1223 case SERVICE_KB_SAVE
:
1224 rc
= service_kb_save(context
);
1226 case SERVICE_KB_UNLOCK
:
1227 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
1228 rc
= service_kb_unlock(context
, secret
, (int)secret_len
);
1230 case SERVICE_KB_LOCK
:
1231 rc
= service_kb_lock(context
);
1233 case SERVICE_KB_CHANGE_SECRET
:
1234 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
1235 new_secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET_NEW
, &new_secret_len
);
1236 rc
= service_kb_change_secret(context
, secret
, (int)secret_len
, new_secret
, (int)new_secret_len
);
1238 case SERVICE_KB_RESET
:
1239 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
1240 rc
= service_kb_reset(context
, secret
, (int)secret_len
);
1242 case SERVICE_KB_IS_LOCKED
:
1243 rc
= service_kb_is_locked(context
, reply
);
1245 case SERVICE_STASH_GET_KEY
:
1246 rc
= service_stash_get_key(context
, event
, reply
);
1248 case SERVICE_STASH_SET_KEY
:
1249 rc
= service_stash_set_key(context
, event
, reply
);
1251 case SERVICE_STASH_LOAD_KEY
:
1252 rc
= service_stash_load_key(context
, event
, reply
);
1254 case SERVICE_KB_LOAD_UID
:
1255 uid
= (uid_t
)xpc_dictionary_get_uint64(event
, SERVICE_XPC_UID
);
1256 rc
= service_kb_load_uid(uid
);
1258 case SERVICE_KB_WRAP_KEY
:
1259 rc
= service_kb_wrap_key(context
, event
, reply
);
1261 case SERVICE_KB_UNWRAP_KEY
:
1262 rc
= service_kb_unwrap_key(context
, event
, reply
);
1265 case SERVICE_STASH_BLOB
:
1266 rc
= service_stash_blob(event
, reply
);
1270 LOG("unknown service type");
1276 LOG("selector: %s (%llu), error: %s (%x), sid: %d, suid: %d, pid: %d", sel_to_char(request
), request
, err_to_char(rc
), rc
, context
? context
->s_id
: 0, context
? context
->s_uid
: 0, context
? get_caller_pid(&context
->procToken
) : 0);
1279 os_log(OS_LOG_DEFAULT
, "selector: %s (%llu), error: %s (%x), sid: %d, suid: %d, pid: %d", sel_to_char(request
), request
, err_to_char(rc
), rc
, context
? context
->s_id
: 0, context
? context
->s_uid
: 0, context
? get_caller_pid(&context
->procToken
) : 0);
1282 xpc_dictionary_set_int64(reply
, SERVICE_XPC_RC
, rc
);
1283 xpc_connection_send_message(connection
, reply
);
1291 bool check_signature(xpc_connection_t connection
)
1293 #if !(DEBUG || RC_BUILDIT_YES)
1294 audit_token_t token
;
1296 xpc_connection_get_audit_token(connection
, &token
);
1298 SecTaskRef task
= SecTaskCreateWithAuditToken(NULL
, token
);
1300 os_log(OS_LOG_DEFAULT
, "failed getting SecTaskRef of the client");
1304 uint32_t flags
= SecTaskGetCodeSignStatus(task
);
1305 /* check if valid and platform binary, but not platform path */
1306 if ((flags
& (CS_VALID
| CS_PLATFORM_BINARY
| CS_PLATFORM_PATH
)) != (CS_VALID
| CS_PLATFORM_BINARY
)) {
1307 os_log(OS_LOG_DEFAULT
, "client is not a platform binary: %0x08x", flags
);
1312 CFStringRef signingIdentity
= SecTaskCopySigningIdentifier(task
, NULL
);
1314 if (signingIdentity
== NULL
) {
1315 os_log(OS_LOG_DEFAULT
, "client have no code sign identity");
1319 bool res
= CFEqual(signingIdentity
, CFSTR("com.apple.securityd"));
1320 CFRelease(signingIdentity
);
1323 os_log(OS_LOG_DEFAULT
, "client is not not securityd");
1331 static void register_for_notifications()
1333 __block kern_return_t kr
;
1334 static mach_port_t mp
= MACH_PORT_NULL
;
1336 static dispatch_once_t onceToken
= 0;
1337 dispatch_once(&onceToken
, ^{
1338 kr
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &mp
);
1339 if (kr
== KERN_SUCCESS
) {
1340 dispatch_source_t mach_src
= dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV
, mp
, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0));
1341 dispatch_source_set_event_handler(mach_src
, ^{
1342 mach_msg_return_t mr
;
1343 uint8_t buf
[sizeof(aks_notification_msg_t
) + MAX_TRAILER_SIZE
] = {};
1344 aks_notification_msg_t
* msg
= (aks_notification_msg_t
*)buf
;
1345 mr
= mach_msg((mach_msg_header_t
*)&buf
, MACH_RCV_MSG
, 0, sizeof(buf
), mp
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1346 if (mr
== MACH_MSG_SUCCESS
&& msg
->hdr
.msgh_id
== AKS_NOTIFICATION_MSGID
) {
1348 } else if (mr
== MACH_MSG_SUCCESS
&& msg
->hdr
.msgh_id
== AKS_NOTIFICATION_WRITE_SYSTEM_KEYBAG
) {
1349 os_log(OS_LOG_DEFAULT
, "request to update handle %d", msg
->handle
);
1350 update_keybag_handle(msg
->handle
);
1352 os_log(OS_LOG_DEFAULT
, "mach_msg error: %x", mr
);
1355 dispatch_resume(mach_src
);
1357 os_log(OS_LOG_DEFAULT
, "failed to create notification port");
1362 kr
= aks_register_for_notifications(mp
, AKS_NOTIFICATION_WRITE_SYSTEM_KEYBAG
);
1363 if (kr
== KERN_SUCCESS
) {
1364 os_log(OS_LOG_DEFAULT
, "registered for notifications");
1366 os_log(OS_LOG_DEFAULT
, "failed to register for notifications %d", kr
);
1370 int main(int argc
, const char * argv
[])
1373 if (sandbox_init(SECURITYD_SERVICE_NAME
, SANDBOX_NAMED
, &errorbuf
) != 0) {
1374 os_log(OS_LOG_DEFAULT
, "sandbox_init failed %s", errorbuf
);
1375 sandbox_free_error(errorbuf
);
1381 register_for_notifications();
1383 xpc_connection_t listener
= xpc_connection_create_mach_service(SECURITYD_SERVICE_NAME
, NULL
, XPC_CONNECTION_MACH_SERVICE_LISTENER
);
1384 xpc_connection_set_event_handler(listener
, ^(xpc_object_t peer
) {
1385 // It is safe to cast 'peer' to xpc_connection_t assuming
1386 // we have a correct configuration in our launchd.plist.
1387 xpc_connection_set_event_handler(peer
, ^(xpc_object_t event
) {
1388 vproc_transaction_t transaction
= vproc_transaction_begin(NULL
);
1389 service_peer_event_handler(peer
, event
);
1390 vproc_transaction_end(NULL
, transaction
);
1392 xpc_connection_resume(peer
);
1394 xpc_connection_resume(listener
);