1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/types.h>
21
22 #include <keystore/IKeystoreService.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/IServiceManager.h>
25
26 #include <keystore/keystore.h>
27
28 using namespace android;
29
30 static const char* responses[] = {
31 NULL,
32 /* [NO_ERROR] = */ "No error",
33 /* [LOCKED] = */ "Locked",
34 /* [UNINITIALIZED] = */ "Uninitialized",
35 /* [SYSTEM_ERROR] = */ "System error",
36 /* [PROTOCOL_ERROR] = */ "Protocol error",
37 /* [PERMISSION_DENIED] = */ "Permission denied",
38 /* [KEY_NOT_FOUND] = */ "Key not found",
39 /* [VALUE_CORRUPTED] = */ "Value corrupted",
40 /* [UNDEFINED_ACTION] = */ "Undefined action",
41 /* [WRONG_PASSWORD] = */ "Wrong password (last chance)",
42 /* [WRONG_PASSWORD + 1] = */ "Wrong password (2 tries left)",
43 /* [WRONG_PASSWORD + 2] = */ "Wrong password (3 tries left)",
44 /* [WRONG_PASSWORD + 3] = */ "Wrong password (4 tries left)",
45 };
46
47 #define NO_ARG_INT_RETURN(cmd) \
48 do { \
49 if (strcmp(argv[1], #cmd) == 0) { \
50 int32_t ret = service->cmd(); \
51 if (ret < 0) { \
52 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
53 return 1; \
54 } else { \
55 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
56 return 0; \
57 } \
58 } \
59 } while (0)
60
61 #define SINGLE_ARG_INT_RETURN(cmd) \
62 do { \
63 if (strcmp(argv[1], #cmd) == 0) { \
64 if (argc < 3) { \
65 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
66 return 1; \
67 } \
68 int32_t ret = service->cmd(String16(argv[2])); \
69 if (ret < 0) { \
70 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
71 return 1; \
72 } else { \
73 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
74 return 0; \
75 } \
76 } \
77 } while (0)
78
79 #define SINGLE_INT_ARG_INT_RETURN(cmd) \
80 do { \
81 if (strcmp(argv[1], #cmd) == 0) { \
82 if (argc < 3) { \
83 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
84 return 1; \
85 } \
86 int32_t ret = service->cmd(atoi(argv[2])); \
87 if (ret < 0) { \
88 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
89 return 1; \
90 } else { \
91 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
92 return 0; \
93 } \
94 } \
95 } while (0)
96
97 #define SINGLE_ARG_PLUS_UID_INT_RETURN(cmd) \
98 do { \
99 if (strcmp(argv[1], #cmd) == 0) { \
100 if (argc < 3) { \
101 fprintf(stderr, "Usage: %s " #cmd " <name> <uid>\n", argv[0]); \
102 return 1; \
103 } \
104 int uid = -1; \
105 if (argc > 3) { \
106 uid = atoi(argv[3]); \
107 fprintf(stderr, "Running as uid %d\n", uid); \
108 } \
109 int32_t ret = service->cmd(String16(argv[2]), uid); \
110 if (ret < 0) { \
111 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
112 return 1; \
113 } else { \
114 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
115 return 0; \
116 } \
117 } \
118 } while (0)
119
120 #define STING_ARG_DATA_STDIN_INT_RETURN(cmd) \
121 do { \
122 if (strcmp(argv[1], #cmd) == 0) { \
123 if (argc < 3) { \
124 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
125 return 1; \
126 } \
127 uint8_t* data; \
128 size_t dataSize; \
129 read_input(&data, &dataSize); \
130 int32_t ret = service->cmd(String16(argv[2]), data, dataSize); \
131 if (ret < 0) { \
132 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
133 return 1; \
134 } else { \
135 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
136 return 0; \
137 } \
138 } \
139 } while (0)
140
141 #define SINGLE_ARG_DATA_RETURN(cmd) \
142 do { \
143 if (strcmp(argv[1], #cmd) == 0) { \
144 if (argc < 3) { \
145 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
146 return 1; \
147 } \
148 uint8_t* data; \
149 size_t dataSize; \
150 int32_t ret = service->cmd(String16(argv[2]), &data, &dataSize); \
151 if (ret < 0) { \
152 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
153 return 1; \
154 } else if (ret != ::NO_ERROR) { \
155 fprintf(stderr, "%s: " #cmd ": %s (%d)\n", argv[0], responses[ret], ret); \
156 return 1; \
157 } else { \
158 fwrite(data, dataSize, 1, stdout); \
159 fflush(stdout); \
160 free(data); \
161 return 0; \
162 } \
163 } \
164 } while (0)
165
list(sp<IKeystoreService> service,const String16 & name,int uid)166 static int list(sp<IKeystoreService> service, const String16& name, int uid) {
167 Vector<String16> matches;
168 int32_t ret = service->list(name, uid, &matches);
169 if (ret < 0) {
170 fprintf(stderr, "list: could not connect: %d\n", ret);
171 return 1;
172 } else if (ret != ::NO_ERROR) {
173 fprintf(stderr, "list: %s (%d)\n", responses[ret], ret);
174 return 1;
175 } else {
176 Vector<String16>::const_iterator it = matches.begin();
177 for (; it != matches.end(); ++it) {
178 printf("%s\n", String8(*it).string());
179 }
180 return 0;
181 }
182 }
183
main(int argc,char * argv[])184 int main(int argc, char* argv[])
185 {
186 if (argc < 2) {
187 fprintf(stderr, "Usage: %s action [parameter ...]\n", argv[0]);
188 return 1;
189 }
190
191 sp<IServiceManager> sm = defaultServiceManager();
192 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
193 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
194
195 if (service == NULL) {
196 fprintf(stderr, "%s: error: could not connect to keystore service\n", argv[0]);
197 return 1;
198 }
199
200 /*
201 * All the commands should return a value
202 */
203
204 SINGLE_INT_ARG_INT_RETURN(getState);
205
206 SINGLE_ARG_DATA_RETURN(get);
207
208 // TODO: insert
209
210 SINGLE_ARG_PLUS_UID_INT_RETURN(del);
211
212 SINGLE_ARG_PLUS_UID_INT_RETURN(exist);
213
214 if (strcmp(argv[1], "list") == 0) {
215 return list(service, argc < 3 ? String16("") : String16(argv[2]),
216 argc < 4 ? -1 : atoi(argv[3]));
217 }
218
219 NO_ARG_INT_RETURN(reset);
220
221 // TODO: notifyUserPasswordChanged
222
223 SINGLE_INT_ARG_INT_RETURN(lock);
224
225 // TODO: unlock
226
227 SINGLE_INT_ARG_INT_RETURN(isEmpty);
228
229 // TODO: generate
230
231 SINGLE_ARG_DATA_RETURN(get_pubkey);
232
233 // TODO: grant
234
235 // TODO: ungrant
236
237 // TODO: getmtime
238
239 fprintf(stderr, "%s: unknown command: %s\n", argv[0], argv[1]);
240 return 1;
241 }
242