blob: 24af024b88ebcaa24502494afa44d7030bbb977e [file] [log] [blame]
Kenny Roota91203b2012-02-15 15:00:46 -08001/*
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>
Kenny Roota91203b2012-02-15 15:00:46 -080021
Kenny Root07438c82012-11-02 15:41:02 -070022#include <keystore/IKeystoreService.h>
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
Kenny Roota91203b2012-02-15 15:00:46 -080025
Kenny Root07438c82012-11-02 15:41:02 -070026#include <keystore/keystore.h>
27
28using namespace android;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010029using namespace keystore;
Kenny Roota91203b2012-02-15 15:00:46 -080030
31static const char* responses[] = {
32 NULL,
33 /* [NO_ERROR] = */ "No error",
34 /* [LOCKED] = */ "Locked",
35 /* [UNINITIALIZED] = */ "Uninitialized",
36 /* [SYSTEM_ERROR] = */ "System error",
37 /* [PROTOCOL_ERROR] = */ "Protocol error",
38 /* [PERMISSION_DENIED] = */ "Permission denied",
39 /* [KEY_NOT_FOUND] = */ "Key not found",
40 /* [VALUE_CORRUPTED] = */ "Value corrupted",
41 /* [UNDEFINED_ACTION] = */ "Undefined action",
42 /* [WRONG_PASSWORD] = */ "Wrong password (last chance)",
43 /* [WRONG_PASSWORD + 1] = */ "Wrong password (2 tries left)",
44 /* [WRONG_PASSWORD + 2] = */ "Wrong password (3 tries left)",
45 /* [WRONG_PASSWORD + 3] = */ "Wrong password (4 tries left)",
46};
47
Kenny Root07438c82012-11-02 15:41:02 -070048#define NO_ARG_INT_RETURN(cmd) \
49 do { \
50 if (strcmp(argv[1], #cmd) == 0) { \
51 int32_t ret = service->cmd(); \
52 if (ret < 0) { \
53 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
54 return 1; \
55 } else { \
56 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
57 return 0; \
58 } \
59 } \
60 } while (0)
61
62#define SINGLE_ARG_INT_RETURN(cmd) \
63 do { \
64 if (strcmp(argv[1], #cmd) == 0) { \
65 if (argc < 3) { \
66 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
67 return 1; \
68 } \
69 int32_t ret = service->cmd(String16(argv[2])); \
70 if (ret < 0) { \
71 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
72 return 1; \
73 } else { \
74 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
75 return 0; \
76 } \
77 } \
78 } while (0)
79
Chad Brubaker94436162015-05-12 15:18:26 -070080#define SINGLE_INT_ARG_INT_RETURN(cmd) \
81 do { \
82 if (strcmp(argv[1], #cmd) == 0) { \
83 if (argc < 3) { \
84 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
85 return 1; \
86 } \
87 int32_t ret = service->cmd(atoi(argv[2])); \
88 if (ret < 0) { \
89 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
90 return 1; \
91 } else { \
92 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
93 return 0; \
94 } \
95 } \
96 } while (0)
97
Kenny Rootb88c3eb2013-02-13 14:43:43 -080098#define SINGLE_ARG_PLUS_UID_INT_RETURN(cmd) \
99 do { \
100 if (strcmp(argv[1], #cmd) == 0) { \
101 if (argc < 3) { \
102 fprintf(stderr, "Usage: %s " #cmd " <name> <uid>\n", argv[0]); \
103 return 1; \
104 } \
105 int uid = -1; \
106 if (argc > 3) { \
107 uid = atoi(argv[3]); \
108 fprintf(stderr, "Running as uid %d\n", uid); \
109 } \
110 int32_t ret = service->cmd(String16(argv[2]), uid); \
111 if (ret < 0) { \
112 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
113 return 1; \
114 } else { \
115 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
116 return 0; \
117 } \
118 } \
119 } while (0)
120
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700121#define SINGLE_ARG_PLUS_UID_DATA_RETURN(cmd) \
122 do { \
123 if (strcmp(argv[1], #cmd) == 0) { \
124 if (argc < 3) { \
125 fprintf(stderr, "Usage: %s " #cmd " <name> <uid>\n", argv[0]); \
126 return 1; \
127 } \
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100128 hidl_vec<uint8_t> data; \
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700129 int uid = -1; \
130 if (argc > 3) { \
131 uid = atoi(argv[3]); \
132 fprintf(stderr, "Running as uid %d\n", uid); \
133 } \
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100134 int32_t ret = service->cmd(String16(argv[2]), uid, &data); \
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700135 if (ret < 0) { \
136 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
137 return 1; \
138 } else if (ret != ::NO_ERROR) { \
139 fprintf(stderr, "%s: " #cmd ": %s (%d)\n", argv[0], responses[ret], ret); \
140 return 1; \
141 } else { \
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100142 fwrite(&data[0], data.size(), 1, stdout); \
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700143 fflush(stdout); \
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700144 return 0; \
145 } \
146 } \
147 } while (0)
148
Kenny Root07438c82012-11-02 15:41:02 -0700149#define STING_ARG_DATA_STDIN_INT_RETURN(cmd) \
150 do { \
151 if (strcmp(argv[1], #cmd) == 0) { \
152 if (argc < 3) { \
153 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
154 return 1; \
155 } \
156 uint8_t* data; \
157 size_t dataSize; \
158 read_input(&data, &dataSize); \
159 int32_t ret = service->cmd(String16(argv[2]), data, dataSize); \
160 if (ret < 0) { \
161 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
162 return 1; \
163 } else { \
164 printf(#cmd ": %s (%d)\n", responses[ret], ret); \
165 return 0; \
166 } \
167 } \
168 } while (0)
169
170#define SINGLE_ARG_DATA_RETURN(cmd) \
171 do { \
172 if (strcmp(argv[1], #cmd) == 0) { \
173 if (argc < 3) { \
174 fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \
175 return 1; \
176 } \
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100177 hidl_vec<uint8_t> data; \
178 int32_t ret = service->cmd(String16(argv[2]), &data); \
Kenny Root07438c82012-11-02 15:41:02 -0700179 if (ret < 0) { \
180 fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \
181 return 1; \
182 } else if (ret != ::NO_ERROR) { \
183 fprintf(stderr, "%s: " #cmd ": %s (%d)\n", argv[0], responses[ret], ret); \
184 return 1; \
185 } else { \
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100186 fwrite(&data[0], data.size(), 1, stdout); \
Kenny Root07438c82012-11-02 15:41:02 -0700187 fflush(stdout); \
Kenny Root07438c82012-11-02 15:41:02 -0700188 return 0; \
189 } \
190 } \
191 } while (0)
192
Chih-Hung Hsieh24b2a392016-07-28 10:35:24 -0700193static int list(const sp<IKeystoreService>& service, const String16& name, int uid) {
Kenny Root07438c82012-11-02 15:41:02 -0700194 Vector<String16> matches;
Chad Brubaker94436162015-05-12 15:18:26 -0700195 int32_t ret = service->list(name, uid, &matches);
Kenny Root07438c82012-11-02 15:41:02 -0700196 if (ret < 0) {
Chad Brubaker94436162015-05-12 15:18:26 -0700197 fprintf(stderr, "list: could not connect: %d\n", ret);
Kenny Root07438c82012-11-02 15:41:02 -0700198 return 1;
199 } else if (ret != ::NO_ERROR) {
Chad Brubaker94436162015-05-12 15:18:26 -0700200 fprintf(stderr, "list: %s (%d)\n", responses[ret], ret);
Kenny Root07438c82012-11-02 15:41:02 -0700201 return 1;
202 } else {
203 Vector<String16>::const_iterator it = matches.begin();
204 for (; it != matches.end(); ++it) {
205 printf("%s\n", String8(*it).string());
206 }
207 return 0;
208 }
209}
210
Kenny Roota91203b2012-02-15 15:00:46 -0800211int main(int argc, char* argv[])
212{
213 if (argc < 2) {
Kenny Root07438c82012-11-02 15:41:02 -0700214 fprintf(stderr, "Usage: %s action [parameter ...]\n", argv[0]);
Kenny Roota91203b2012-02-15 15:00:46 -0800215 return 1;
216 }
217
Kenny Root07438c82012-11-02 15:41:02 -0700218 sp<IServiceManager> sm = defaultServiceManager();
219 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
220 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
Kenny Roota91203b2012-02-15 15:00:46 -0800221
Kenny Root07438c82012-11-02 15:41:02 -0700222 if (service == NULL) {
223 fprintf(stderr, "%s: error: could not connect to keystore service\n", argv[0]);
Kenny Roota91203b2012-02-15 15:00:46 -0800224 return 1;
225 }
Kenny Root07438c82012-11-02 15:41:02 -0700226
227 /*
228 * All the commands should return a value
229 */
230
Chad Brubaker94436162015-05-12 15:18:26 -0700231 SINGLE_INT_ARG_INT_RETURN(getState);
Kenny Root07438c82012-11-02 15:41:02 -0700232
Chad Brubakerad6a7f52015-09-09 14:55:22 -0700233 SINGLE_ARG_PLUS_UID_DATA_RETURN(get);
Kenny Root07438c82012-11-02 15:41:02 -0700234
235 // TODO: insert
236
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800237 SINGLE_ARG_PLUS_UID_INT_RETURN(del);
Kenny Root07438c82012-11-02 15:41:02 -0700238
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800239 SINGLE_ARG_PLUS_UID_INT_RETURN(exist);
Kenny Root07438c82012-11-02 15:41:02 -0700240
Chad Brubaker94436162015-05-12 15:18:26 -0700241 if (strcmp(argv[1], "list") == 0) {
242 return list(service, argc < 3 ? String16("") : String16(argv[2]),
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800243 argc < 4 ? -1 : atoi(argv[3]));
Kenny Roota91203b2012-02-15 15:00:46 -0800244 }
Kenny Root07438c82012-11-02 15:41:02 -0700245
246 NO_ARG_INT_RETURN(reset);
247
Chad Brubakereecdd122015-05-07 10:19:40 -0700248 // TODO: notifyUserPasswordChanged
Kenny Root07438c82012-11-02 15:41:02 -0700249
Chad Brubaker94436162015-05-12 15:18:26 -0700250 SINGLE_INT_ARG_INT_RETURN(lock);
Kenny Root07438c82012-11-02 15:41:02 -0700251
Chad Brubakereecdd122015-05-07 10:19:40 -0700252 // TODO: unlock
Kenny Root07438c82012-11-02 15:41:02 -0700253
Chad Brubaker94436162015-05-12 15:18:26 -0700254 SINGLE_INT_ARG_INT_RETURN(isEmpty);
Kenny Root07438c82012-11-02 15:41:02 -0700255
Kenny Root0c540aa2013-04-03 09:22:15 -0700256 // TODO: generate
Kenny Root07438c82012-11-02 15:41:02 -0700257
258 SINGLE_ARG_DATA_RETURN(get_pubkey);
259
Kenny Root07438c82012-11-02 15:41:02 -0700260 // TODO: grant
261
262 // TODO: ungrant
263
264 // TODO: getmtime
265
266 fprintf(stderr, "%s: unknown command: %s\n", argv[0], argv[1]);
267 return 1;
Kenny Roota91203b2012-02-15 15:00:46 -0800268}