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