3 // securitydservicectrl
5 // Created by Wade Benson on 12/2/12.
6 // Copyright (c) 2012 Apple. All rights reserved.
9 #include "securityd_service.h"
10 #include "securityd_service_client.h"
14 #include <dispatch/dispatch.h>
15 #include <AssertMacros.h>
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <Security/SecKeychainPriv.h>
20 hextostr(const uint8_t *buf
, size_t len
, char *hexbuf
)
24 static const char hexdigits
[] = "0123456789abcdef";
25 for (i
= 0; i
< len
; i
++) {
26 *s
++ = hexdigits
[buf
[i
]>>4];
27 *s
++ = hexdigits
[buf
[i
]&0xf];
33 int main(int argc
, const char * argv
[])
36 OSStatus status
= noErr
;
37 uint8_t testkey
[128] = "\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef";
38 xpc_connection_t connection
= xpc_connection_create_mach_service(SECURITYD_SERVICE_NAME
, NULL
, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
);
40 xpc_connection_set_event_handler(connection
, ^(xpc_object_t event
) {
41 if (xpc_get_type(event
) == XPC_TYPE_ERROR
) {
42 printf("XPC error\n");
45 xpc_connection_resume(connection
);
48 printf("Usage: securityservicectrl < get | set | stash | login | loginstash | unload >\n");
52 if (strcmp(argv
[1], "get") == 0) {
53 action
= SERVICE_STASH_GET_KEY
;
56 } else if (strcmp(argv
[1], "set") == 0) {
57 action
= SERVICE_STASH_SET_KEY
;
60 } else if (strcmp(argv
[1], "stash") == 0) {
61 action
= SERVICE_STASH_BLOB
;
64 } else if (strcmp(argv
[1], "login") == 0) {
65 printf("SecKeychainLogin() null passwd\n");
66 status
= SecKeychainLogin((uint32
) strlen("test"), "test", 0, NULL
);
67 printf("Returned: %i\n", status
);
68 return status
? 1 : 0;
70 } else if (strcmp(argv
[1], "loginstash") == 0) {
71 printf("SecKeychainStash()\n");
72 status
= SecKeychainStash();
73 printf("Returned: %i\n", status
);
74 return status
? 1 : 0;
76 } else if (strcmp(argv
[1], "unload") == 0) {
77 return service_client_kb_unload(NULL
);
80 printf("%s not known\n", argv
[1]);
85 xpc_object_t message
= xpc_dictionary_create(NULL
, NULL
, 0);
86 xpc_dictionary_set_uint64(message
, SERVICE_XPC_REQUEST
, action
);
88 if (action
== SERVICE_STASH_SET_KEY
)
89 xpc_dictionary_set_data(message
, SERVICE_XPC_KEY
, testkey
, 16);
91 xpc_object_t reply
= xpc_connection_send_message_with_reply_sync(connection
, message
);
92 require_action(reply
!= NULL
, done
, status
= -1);
93 require_action(xpc_get_type(reply
) != XPC_TYPE_ERROR
, done
, status
= -1);
95 if (action
== SERVICE_STASH_GET_KEY
) {
97 const uint8_t *keydata
= xpc_dictionary_get_data(reply
, SERVICE_XPC_KEY
, &len
);
99 char buf
[sizeof(testkey
) + 1];
100 printf("\tkey = %s\n", hextostr(keydata
, len
> sizeof(testkey
) ? sizeof(testkey
) : len
, buf
));
104 status
= (OSStatus
)xpc_dictionary_get_int64(reply
, SERVICE_XPC_RC
);
108 xpc_release(message
);
112 xpc_release(connection
);
114 printf("Returned: %i\n", status
);
116 return status
? 1 : 0;