Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG AUTH |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "adb_auth.h" |
Yurii Zubrytskyi | 376b275 | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 21 | #include "adb_utils.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 22 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Christopher Ferris | 67a7a4a | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 25 | #include <string.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 26 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 27 | #include "adb.h" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 28 | |
David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 29 | #include <android-base/errors.h> |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 30 | #include <android-base/file.h> |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 32 | #include <android-base/strings.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 33 | #include <crypto_utils/android_pubkey.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 34 | #include <cutils/list.h> |
| 35 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 36 | #include <openssl/base64.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 37 | #include <openssl/evp.h> |
| 38 | #include <openssl/objects.h> |
| 39 | #include <openssl/pem.h> |
| 40 | #include <openssl/rsa.h> |
| 41 | #include <openssl/sha.h> |
| 42 | |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 43 | #define ANDROID_PATH ".android" |
| 44 | #define ADB_KEY_FILE "adbkey" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 45 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 46 | struct adb_private_key { |
| 47 | struct listnode node; |
| 48 | RSA *rsa; |
| 49 | }; |
| 50 | |
| 51 | static struct listnode key_list; |
| 52 | |
| 53 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 54 | static std::string get_user_info() { |
| 55 | std::string hostname; |
| 56 | if (getenv("HOSTNAME")) hostname = getenv("HOSTNAME"); |
| 57 | #if !defined(_WIN32) |
| 58 | char buf[64]; |
| 59 | if (hostname.empty() && gethostname(buf, sizeof(buf)) != -1) hostname = buf; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 60 | #endif |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 61 | if (hostname.empty()) hostname = "unknown"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 62 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 63 | std::string username; |
| 64 | if (getenv("LOGNAME")) username = getenv("LOGNAME"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 65 | #if !defined _WIN32 && !defined ADB_HOST_ON_TARGET |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 66 | if (username.empty() && getlogin()) username = getlogin(); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 67 | #endif |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 68 | if (username.empty()) hostname = "unknown"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 70 | return " " + username + "@" + hostname; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 73 | static bool write_public_keyfile(RSA* private_key, const std::string& private_key_path) { |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 74 | uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE]; |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 75 | if (!android_pubkey_encode(private_key, binary_key_data, sizeof(binary_key_data))) { |
| 76 | LOG(ERROR) << "Failed to convert to public key"; |
| 77 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 80 | size_t base64_key_length; |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 81 | if (!EVP_EncodedLength(&base64_key_length, sizeof(binary_key_data))) { |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 82 | LOG(ERROR) << "Public key too large to base64 encode"; |
| 83 | return false; |
Adam Langley | 179d9d6 | 2014-09-03 14:34:47 -0700 | [diff] [blame] | 84 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 85 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 86 | std::string content; |
| 87 | content.resize(base64_key_length); |
| 88 | base64_key_length = EVP_EncodeBlock(reinterpret_cast<uint8_t*>(&content[0]), binary_key_data, |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 89 | sizeof(binary_key_data)); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 91 | content += get_user_info(); |
| 92 | |
| 93 | std::string path(private_key_path + ".pub"); |
| 94 | if (!android::base::WriteStringToFile(content, path)) { |
| 95 | PLOG(ERROR) << "Failed to write public key to '" << path << "'"; |
| 96 | return false; |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 99 | return true; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int generate_key(const char *file) |
| 103 | { |
| 104 | EVP_PKEY* pkey = EVP_PKEY_new(); |
| 105 | BIGNUM* exponent = BN_new(); |
| 106 | RSA* rsa = RSA_new(); |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 107 | mode_t old_mask; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 108 | FILE *f = NULL; |
| 109 | int ret = 0; |
| 110 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 111 | D("generate_key '%s'", file); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 112 | |
| 113 | if (!pkey || !exponent || !rsa) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 114 | D("Failed to allocate key"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | BN_set_word(exponent, RSA_F4); |
| 119 | RSA_generate_key_ex(rsa, 2048, exponent, NULL); |
| 120 | EVP_PKEY_set1_RSA(pkey, rsa); |
| 121 | |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 122 | old_mask = umask(077); |
| 123 | |
Spencer Low | 9b96031 | 2015-05-07 19:08:29 -0700 | [diff] [blame] | 124 | f = fopen(file, "w"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 125 | if (!f) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 126 | D("Failed to open '%s'", file); |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 127 | umask(old_mask); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 128 | goto out; |
| 129 | } |
| 130 | |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 131 | umask(old_mask); |
| 132 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 133 | if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 134 | D("Failed to write key"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | if (!write_public_keyfile(rsa, file)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 139 | D("Failed to write public key"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | ret = 1; |
| 144 | |
| 145 | out: |
| 146 | if (f) |
| 147 | fclose(f); |
| 148 | EVP_PKEY_free(pkey); |
| 149 | RSA_free(rsa); |
| 150 | BN_free(exponent); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | static int read_key(const char *file, struct listnode *list) |
| 155 | { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 156 | D("read_key '%s'", file); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 157 | |
Spencer Low | 9b96031 | 2015-05-07 19:08:29 -0700 | [diff] [blame] | 158 | FILE* fp = fopen(file, "r"); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 159 | if (!fp) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 160 | D("Failed to open '%s': %s", file, strerror(errno)); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 164 | adb_private_key* key = new adb_private_key; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 165 | key->rsa = RSA_new(); |
| 166 | |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 167 | if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 168 | D("Failed to read key"); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 169 | fclose(fp); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 170 | RSA_free(key->rsa); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 171 | delete key; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 175 | fclose(fp); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 176 | list_add_tail(list, &key->node); |
| 177 | return 1; |
| 178 | } |
| 179 | |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 180 | static int get_user_keyfilepath(char *filename, size_t len) |
| 181 | { |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 182 | const std::string home = adb_get_homedir_path(true); |
Yurii Zubrytskyi | 376b275 | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 183 | D("home '%s'", home.c_str()); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 184 | |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 185 | const std::string android_dir = |
| 186 | android::base::StringPrintf("%s%c%s", home.c_str(), |
| 187 | OS_PATH_SEPARATOR, ANDROID_PATH); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 188 | |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 189 | struct stat buf; |
| 190 | if (stat(android_dir.c_str(), &buf)) { |
| 191 | if (adb_mkdir(android_dir.c_str(), 0750) < 0) { |
| 192 | D("Cannot mkdir '%s'", android_dir.c_str()); |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 193 | return -1; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 197 | return snprintf(filename, len, "%s%c%s", |
| 198 | android_dir.c_str(), OS_PATH_SEPARATOR, ADB_KEY_FILE); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int get_user_key(struct listnode *list) |
| 202 | { |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 203 | struct stat buf; |
| 204 | char path[PATH_MAX]; |
| 205 | int ret; |
| 206 | |
| 207 | ret = get_user_keyfilepath(path, sizeof(path)); |
| 208 | if (ret < 0 || ret >= (signed)sizeof(path)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 209 | D("Error getting user key filename"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 213 | D("user key '%s'", path); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 214 | |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 215 | if (stat(path, &buf) == -1) { |
| 216 | if (!generate_key(path)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 217 | D("Failed to generate new key"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 222 | return read_key(path, list); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 225 | static void get_vendor_keys(struct listnode* key_list) { |
| 226 | const char* adb_keys_path = getenv("ADB_VENDOR_KEYS"); |
| 227 | if (adb_keys_path == nullptr) { |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 228 | return; |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 229 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 230 | |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 231 | for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) { |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 232 | if (!read_key(path.c_str(), key_list)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 233 | D("Failed to read '%s'", path.c_str()); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 234 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 238 | int adb_auth_sign(void *node, const unsigned char* token, size_t token_size, |
| 239 | unsigned char* sig) |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 240 | { |
| 241 | unsigned int len; |
| 242 | struct adb_private_key *key = node_to_item(node, struct adb_private_key, node); |
| 243 | |
Sami Tolvanen | 7b9c20d | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 244 | if (token_size != TOKEN_SIZE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 245 | D("Unexpected token size %zd", token_size); |
Sami Tolvanen | 7b9c20d | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 249 | if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) { |
| 250 | return 0; |
| 251 | } |
| 252 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 253 | D("adb_auth_sign len=%d", len); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 254 | return (int)len; |
| 255 | } |
| 256 | |
| 257 | void *adb_auth_nextkey(void *current) |
| 258 | { |
| 259 | struct listnode *item; |
| 260 | |
| 261 | if (list_empty(&key_list)) |
| 262 | return NULL; |
| 263 | |
| 264 | if (!current) |
| 265 | return list_head(&key_list); |
| 266 | |
| 267 | list_for_each(item, &key_list) { |
| 268 | if (item == current) { |
| 269 | /* current is the last item, we tried all the keys */ |
| 270 | if (item->next == &key_list) |
| 271 | return NULL; |
| 272 | return item->next; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 279 | std::string adb_auth_get_userkey() { |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 280 | char path[PATH_MAX]; |
| 281 | int ret = get_user_keyfilepath(path, sizeof(path) - 4); |
| 282 | if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 283 | D("Error getting user key filename"); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 284 | return ""; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 285 | } |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 286 | strcat(path, ".pub"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 287 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 288 | std::string content; |
| 289 | if (!android::base::ReadFileToString(path, &content)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 290 | D("Can't load '%s'", path); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 291 | return ""; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 292 | } |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 293 | return content; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 296 | int adb_auth_keygen(const char* filename) { |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 297 | return (generate_key(filename) == 0); |
| 298 | } |
| 299 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 300 | void adb_auth_init(void) |
| 301 | { |
| 302 | int ret; |
| 303 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 304 | D("adb_auth_init"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 305 | |
| 306 | list_init(&key_list); |
| 307 | |
| 308 | ret = get_user_key(&key_list); |
| 309 | if (!ret) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 310 | D("Failed to get user key"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | get_vendor_keys(&key_list); |
| 315 | } |