]> git.saurik.com Git - apple/security.git/blob - securityd/securityd_service/securitydservicectrl/main.c
Security-57740.1.18.tar.gz
[apple/security.git] / securityd / securityd_service / securitydservicectrl / main.c
1 //
2 // main.c
3 // securitydservicectrl
4 //
5 // Created by Wade Benson on 12/2/12.
6 // Copyright (c) 2012 Apple. All rights reserved.
7 //
8
9 #include "securityd_service.h"
10 #include "securityd_service_client.h"
11
12 #include <stdio.h>
13 #include <xpc/xpc.h>
14 #include <dispatch/dispatch.h>
15 #include <AssertMacros.h>
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <Security/SecKeychainPriv.h>
18
19 static inline char *
20 hextostr(const uint8_t *buf, size_t len, char *hexbuf)
21 {
22 char *s = hexbuf;
23 size_t i;
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];
28 }
29 *s = '\0';
30 return hexbuf;
31 }
32
33 int main(int argc, const char * argv[])
34 {
35 uint64_t action = 0;
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);
39
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");
43 }
44 });
45 xpc_connection_resume(connection);
46
47 if (argc != 2) {
48 printf("Usage: securityservicectrl < get | set | stash | login | loginstash | unload >\n");
49 return 1;
50 }
51
52 if (strcmp(argv[1], "get") == 0) {
53 action = SERVICE_STASH_GET_KEY;
54 printf("Get key\n");
55
56 } else if (strcmp(argv[1], "set") == 0) {
57 action = SERVICE_STASH_SET_KEY;
58 printf("Set key\n");
59
60 } else if (strcmp(argv[1], "stash") == 0) {
61 action = SERVICE_STASH_BLOB;
62 printf("Stash\n");
63
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;
69
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;
75
76 } else if (strcmp(argv[1], "unload") == 0) {
77 return service_client_kb_unload(NULL);
78
79 } else {
80 printf("%s not known\n", argv[1]);
81 return 1;
82 }
83
84 // Send
85 xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
86 xpc_dictionary_set_uint64(message, SERVICE_XPC_REQUEST, action);
87
88 if (action == SERVICE_STASH_SET_KEY)
89 xpc_dictionary_set_data(message, SERVICE_XPC_KEY, testkey, 16);
90
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);
94
95 if (action == SERVICE_STASH_GET_KEY) {
96 size_t len = 0;
97 const uint8_t *keydata = xpc_dictionary_get_data(reply, SERVICE_XPC_KEY, &len);
98 if (keydata) {
99 char buf[sizeof(testkey) + 1];
100 printf("\tkey = %s\n", hextostr(keydata, len > sizeof(testkey) ? sizeof(testkey) : len, buf));
101 }
102 }
103
104 status = (OSStatus)xpc_dictionary_get_int64(reply, SERVICE_XPC_RC);
105
106 done:
107 if (message)
108 xpc_release(message);
109 if (reply)
110 xpc_release(reply);
111 if (connection)
112 xpc_release(connection);
113
114 printf("Returned: %i\n", status);
115
116 return status ? 1 : 0;
117 }
118