/ securityd / securityd_service / KeyStore / KeyStoreEvents.c
KeyStoreEvents.c
 1  /* Copyright (c) 2013 Apple Inc. All Rights Reserved. */
 2  
 3  #include "AppleKeyStoreEvents.h"
 4  
 5  #include <unistd.h>
 6  #include <notify.h>
 7  #include <syslog.h>
 8  #include <AssertMacros.h>
 9  #include <IOKit/IOKitLib.h>
10  #include <Kernel/IOKit/crypto/AppleKeyStoreDefs.h>
11  #include <xpc/event_private.h>
12  #include <os/log.h>
13  
14  static void aksNotificationCallback(void *refcon,io_service_t service, natural_t messageType, void *messageArgument)
15  {
16  	if(messageType == kAppleKeyStoreLockStateChangeMessage) {
17          os_log(OS_LOG_DEFAULT, "KeyStoreNotifier posting lockstate change");
18  		notify_post(kAppleKeyStoreLockStatusNotificationID);
19  	} else if (messageType == kAppleKeyStoreFirstUnlockMessage) {
20          os_log(OS_LOG_DEFAULT, "KeyStoreNotifier posting first unlock");
21  		notify_post(kAppleKeyStoreFirstUnlockNotificationID);
22      }
23  }
24  
25  static void start(dispatch_queue_t queue)
26  {
27  	IOReturn result;
28  	io_service_t aksService = IO_OBJECT_NULL;
29  	IONotificationPortRef aksNotifyPort = NULL;
30  	io_object_t notification = IO_OBJECT_NULL;
31  
32  	aksService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching(kAppleKeyStoreServiceName));
33  	require_action(aksService, cleanup, syslog(LOG_ERR, "KeyStoreNotifier - Can't find %s service", kAppleKeyStoreServiceName));
34  
35  	aksNotifyPort = IONotificationPortCreate(kIOMasterPortDefault);
36  	require_action(aksNotifyPort, cleanup, syslog(LOG_ERR, "KeyStoreNotifier - Can't create notification port"));
37  
38      IONotificationPortSetDispatchQueue(aksNotifyPort, queue);
39  
40  	result = IOServiceAddInterestNotification(aksNotifyPort, aksService, kIOGeneralInterest, aksNotificationCallback, NULL, &notification);
41  	require_noerr_action(result, cleanup, syslog(LOG_ERR, "KeyStoreNotifier - Can't register for notification: %08x", result));
42  	return;
43  
44  cleanup:
45  	if (aksNotifyPort) IONotificationPortDestroy(aksNotifyPort);
46      if (notification) IOObjectRelease(notification);
47      if (aksService) IOObjectRelease(aksService);
48  	return;
49  }
50  
51  void
52  init_keystore_events(xpc_event_module_t module)
53  {
54      start(xpc_event_module_get_queue(module));
55  }