Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 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> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 21 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 22 | #include <keystore/IKeystoreService.h> |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 25 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 26 | #include <keystore/keystore.h> |
| 27 | |
| 28 | using namespace android; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 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 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 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 STING_ARG_DATA_STDIN_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 | uint8_t* data; \ |
| 87 | size_t dataSize; \ |
| 88 | read_input(&data, &dataSize); \ |
| 89 | int32_t ret = service->cmd(String16(argv[2]), data, dataSize); \ |
| 90 | if (ret < 0) { \ |
| 91 | fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \ |
| 92 | return 1; \ |
| 93 | } else { \ |
| 94 | printf(#cmd ": %s (%d)\n", responses[ret], ret); \ |
| 95 | return 0; \ |
| 96 | } \ |
| 97 | } \ |
| 98 | } while (0) |
| 99 | |
| 100 | #define SINGLE_ARG_DATA_RETURN(cmd) \ |
| 101 | do { \ |
| 102 | if (strcmp(argv[1], #cmd) == 0) { \ |
| 103 | if (argc < 3) { \ |
| 104 | fprintf(stderr, "Usage: %s " #cmd " <name>\n", argv[0]); \ |
| 105 | return 1; \ |
| 106 | } \ |
| 107 | uint8_t* data; \ |
| 108 | size_t dataSize; \ |
| 109 | int32_t ret = service->cmd(String16(argv[2]), &data, &dataSize); \ |
| 110 | if (ret < 0) { \ |
| 111 | fprintf(stderr, "%s: could not connect: %d\n", argv[0], ret); \ |
| 112 | return 1; \ |
| 113 | } else if (ret != ::NO_ERROR) { \ |
| 114 | fprintf(stderr, "%s: " #cmd ": %s (%d)\n", argv[0], responses[ret], ret); \ |
| 115 | return 1; \ |
| 116 | } else { \ |
| 117 | fwrite(data, dataSize, 1, stdout); \ |
| 118 | fflush(stdout); \ |
| 119 | free(data); \ |
| 120 | return 0; \ |
| 121 | } \ |
| 122 | } \ |
| 123 | } while (0) |
| 124 | |
| 125 | static int saw(sp<IKeystoreService> service, const String16& name) { |
| 126 | Vector<String16> matches; |
| 127 | int32_t ret = service->saw(name, &matches); |
| 128 | if (ret < 0) { |
| 129 | fprintf(stderr, "saw: could not connect: %d\n", ret); |
| 130 | return 1; |
| 131 | } else if (ret != ::NO_ERROR) { |
| 132 | fprintf(stderr, "saw: %s (%d)\n", responses[ret], ret); |
| 133 | return 1; |
| 134 | } else { |
| 135 | Vector<String16>::const_iterator it = matches.begin(); |
| 136 | for (; it != matches.end(); ++it) { |
| 137 | printf("%s\n", String8(*it).string()); |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 143 | int main(int argc, char* argv[]) |
| 144 | { |
| 145 | if (argc < 2) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 146 | fprintf(stderr, "Usage: %s action [parameter ...]\n", argv[0]); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 147 | return 1; |
| 148 | } |
| 149 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 150 | sp<IServiceManager> sm = defaultServiceManager(); |
| 151 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); |
| 152 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 153 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 154 | if (service == NULL) { |
| 155 | fprintf(stderr, "%s: error: could not connect to keystore service\n", argv[0]); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 156 | return 1; |
| 157 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * All the commands should return a value |
| 161 | */ |
| 162 | |
| 163 | NO_ARG_INT_RETURN(test); |
| 164 | |
| 165 | SINGLE_ARG_DATA_RETURN(get); |
| 166 | |
| 167 | // TODO: insert |
| 168 | |
| 169 | SINGLE_ARG_INT_RETURN(del); |
| 170 | |
| 171 | SINGLE_ARG_INT_RETURN(exist); |
| 172 | |
| 173 | if (strcmp(argv[1], "saw") == 0) { |
| 174 | return saw(service, argc < 3 ? String16("") : String16(argv[2])); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 175 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 176 | |
| 177 | NO_ARG_INT_RETURN(reset); |
| 178 | |
| 179 | SINGLE_ARG_INT_RETURN(password); |
| 180 | |
| 181 | NO_ARG_INT_RETURN(lock); |
| 182 | |
| 183 | SINGLE_ARG_INT_RETURN(unlock); |
| 184 | |
| 185 | NO_ARG_INT_RETURN(zero); |
| 186 | |
| 187 | SINGLE_ARG_INT_RETURN(generate); |
| 188 | |
| 189 | SINGLE_ARG_DATA_RETURN(get_pubkey); |
| 190 | |
| 191 | SINGLE_ARG_INT_RETURN(del_key); |
| 192 | |
| 193 | // TODO: grant |
| 194 | |
| 195 | // TODO: ungrant |
| 196 | |
| 197 | // TODO: getmtime |
| 198 | |
| 199 | fprintf(stderr, "%s: unknown command: %s\n", argv[0], argv[1]); |
| 200 | return 1; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 201 | } |