3 // securitydservicectrl
5 // Created by Wade Benson on 12/2/12.
6 // Copyright (c) 2012 Apple. All rights reserved.
9 #include "securityd_service.h"
13 #include <dispatch/dispatch.h>
14 #include <AssertMacros.h>
15 #include <CoreFoundation/CoreFoundation.h>
16 #include <Security/SecKeychainPriv.h>
19 hextostr(const uint8_t *buf
, size_t len
, char *hexbuf
)
23 static const char hexdigits
[] = "0123456789abcdef";
24 for (i
= 0; i
< len
; i
++) {
25 *s
++ = hexdigits
[buf
[i
]>>4];
26 *s
++ = hexdigits
[buf
[i
]&0xf];
32 int main(int argc
, const char * argv
[])
35 OSStatus status
= noErr
;
36 uint8_t testkey
[128] = "\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef";
37 xpc_connection_t connection
= xpc_connection_create_mach_service(SECURITYD_SERVICE_NAME
, NULL
, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
);
39 xpc_connection_set_event_handler(connection
, ^(xpc_object_t event
) {
40 if (xpc_get_type(event
) == XPC_TYPE_ERROR
) {
41 printf("XPC error\n");
44 xpc_connection_resume(connection
);
47 printf("Usage: securityservicectrl < get | set | stash | login | loginstash >\n");
51 if (strcmp(argv
[1], "get") == 0) {
52 action
= SERVICE_STASH_GET_KEY
;
55 } else if (strcmp(argv
[1], "set") == 0) {
56 action
= SERVICE_STASH_SET_KEY
;
59 } else if (strcmp(argv
[1], "stash") == 0) {
60 action
= SERVICE_STASH_BLOB
;
63 } else if (strcmp(argv
[1], "login") == 0) {
64 printf("SecKeychainLogin() null passwd\n");
65 status
= SecKeychainLogin((uint32
) strlen("test"), "test", 0, NULL
);
66 printf("Returned: %i\n", status
);
67 return status
? 1 : 0;
69 } else if (strcmp(argv
[1], "loginstash") == 0) {
70 printf("SecKeychainStash()\n");
71 status
= SecKeychainStash();
72 printf("Returned: %i\n", status
);
73 return status
? 1 : 0;
76 printf("%s not known\n", argv
[1]);
81 xpc_object_t message
= xpc_dictionary_create(NULL
, NULL
, 0);
82 xpc_dictionary_set_uint64(message
, SERVICE_XPC_REQUEST
, action
);
84 if (action
== SERVICE_STASH_SET_KEY
)
85 xpc_dictionary_set_data(message
, SERVICE_XPC_KEY
, testkey
, 16);
87 xpc_object_t reply
= xpc_connection_send_message_with_reply_sync(connection
, message
);
88 require_action(reply
!= NULL
, done
, status
= -1);
89 require_action(xpc_get_type(reply
) != XPC_TYPE_ERROR
, done
, status
= -1);
91 if (action
== SERVICE_STASH_GET_KEY
) {
93 const uint8_t *keydata
= xpc_dictionary_get_data(reply
, SERVICE_XPC_KEY
, &len
);
95 char buf
[sizeof(testkey
) + 1];
96 printf("\tkey = %s\n", hextostr(keydata
, len
> sizeof(testkey
) ? sizeof(testkey
) : len
, buf
));
100 status
= (OSStatus
)xpc_dictionary_get_int64(reply
, SERVICE_XPC_RC
);
104 xpc_release(message
);
108 xpc_release(connection
);
110 printf("Returned: %i\n", status
);
112 return status
? 1 : 0;