Benoit Goby | 2cc19e4 | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG AUTH |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 19 | #include "adb_auth.h" |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 20 | #include "adb.h" |
Yurii Zubrytskyi | 70ae70a | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 21 | #include "adb_utils.h" |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 22 | #include "sysdeps.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 23 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
Christopher Ferris | 054d170 | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 26 | #include <string.h> |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 28 | #include <mutex> |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 29 | |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 30 | #include <android-base/errors.h> |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
Yurii Zubrytskyi | 09ecaa5 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/strings.h> |
Mattias Nissler | a947b49 | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 34 | #include <crypto_utils/android_pubkey.h> |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 35 | #include <openssl/base64.h> |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 36 | #include <openssl/evp.h> |
| 37 | #include <openssl/objects.h> |
| 38 | #include <openssl/pem.h> |
| 39 | #include <openssl/rsa.h> |
| 40 | #include <openssl/sha.h> |
| 41 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 42 | #include "sysdeps/mutex.h" |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 44 | static std::mutex& g_key_list_mutex = *new std::mutex; |
| 45 | static std::deque<RSA*>& g_key_list = *new std::deque<RSA*>; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 47 | static std::string get_user_info() { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 48 | LOG(INFO) << "get_user_info..."; |
| 49 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 50 | std::string hostname; |
| 51 | if (getenv("HOSTNAME")) hostname = getenv("HOSTNAME"); |
| 52 | #if !defined(_WIN32) |
| 53 | char buf[64]; |
| 54 | if (hostname.empty() && gethostname(buf, sizeof(buf)) != -1) hostname = buf; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 55 | #endif |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 56 | if (hostname.empty()) hostname = "unknown"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 58 | std::string username; |
| 59 | if (getenv("LOGNAME")) username = getenv("LOGNAME"); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 60 | #if !defined _WIN32 && !defined ADB_HOST_ON_TARGET |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 61 | if (username.empty() && getlogin()) username = getlogin(); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 62 | #endif |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 63 | if (username.empty()) hostname = "unknown"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 64 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 65 | return " " + username + "@" + hostname; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 68 | static bool write_public_keyfile(RSA* private_key, const std::string& private_key_path) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 69 | LOG(INFO) << "write_public_keyfile..."; |
| 70 | |
Mattias Nissler | a947b49 | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 71 | uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE]; |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 72 | if (!android_pubkey_encode(private_key, binary_key_data, sizeof(binary_key_data))) { |
| 73 | LOG(ERROR) << "Failed to convert to public key"; |
| 74 | return false; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 77 | size_t base64_key_length; |
Mattias Nissler | a947b49 | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 78 | if (!EVP_EncodedLength(&base64_key_length, sizeof(binary_key_data))) { |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 79 | LOG(ERROR) << "Public key too large to base64 encode"; |
| 80 | return false; |
Adam Langley | 29f6cdb | 2014-09-03 14:34:47 -0700 | [diff] [blame] | 81 | } |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 83 | std::string content; |
| 84 | content.resize(base64_key_length); |
| 85 | base64_key_length = EVP_EncodeBlock(reinterpret_cast<uint8_t*>(&content[0]), binary_key_data, |
Mattias Nissler | a947b49 | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 86 | sizeof(binary_key_data)); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 88 | content += get_user_info(); |
| 89 | |
| 90 | std::string path(private_key_path + ".pub"); |
| 91 | if (!android::base::WriteStringToFile(content, path)) { |
| 92 | PLOG(ERROR) << "Failed to write public key to '" << path << "'"; |
| 93 | return false; |
Mattias Nissler | a947b49 | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Elliott Hughes | ab942fd | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 96 | return true; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 99 | static int generate_key(const std::string& file) { |
| 100 | LOG(INFO) << "generate_key(" << file << ")..."; |
| 101 | |
Benoit Goby | cb37c50 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 102 | mode_t old_mask; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 103 | FILE *f = NULL; |
| 104 | int ret = 0; |
| 105 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 106 | EVP_PKEY* pkey = EVP_PKEY_new(); |
| 107 | BIGNUM* exponent = BN_new(); |
| 108 | RSA* rsa = RSA_new(); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 109 | if (!pkey || !exponent || !rsa) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 110 | LOG(ERROR) << "Failed to allocate key"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | BN_set_word(exponent, RSA_F4); |
| 115 | RSA_generate_key_ex(rsa, 2048, exponent, NULL); |
| 116 | EVP_PKEY_set1_RSA(pkey, rsa); |
| 117 | |
Benoit Goby | cb37c50 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 118 | old_mask = umask(077); |
| 119 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 120 | f = fopen(file.c_str(), "w"); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 121 | if (!f) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 122 | PLOG(ERROR) << "Failed to open " << file; |
Benoit Goby | cb37c50 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 123 | umask(old_mask); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 124 | goto out; |
| 125 | } |
| 126 | |
Benoit Goby | cb37c50 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 127 | umask(old_mask); |
| 128 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 129 | if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 130 | D("Failed to write key"); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | if (!write_public_keyfile(rsa, file)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 135 | D("Failed to write public key"); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | ret = 1; |
| 140 | |
| 141 | out: |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 142 | if (f) fclose(f); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 143 | EVP_PKEY_free(pkey); |
| 144 | RSA_free(rsa); |
| 145 | BN_free(exponent); |
| 146 | return ret; |
| 147 | } |
| 148 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 149 | static bool read_key(const std::string& file) { |
| 150 | LOG(INFO) << "read_key '" << file << "'..."; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 151 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 152 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file.c_str(), "r"), fclose); |
Elliott Hughes | fb2ba51 | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 153 | if (!fp) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 154 | PLOG(ERROR) << "Failed to open '" << file << "'"; |
| 155 | return false; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 158 | RSA* key = RSA_new(); |
| 159 | if (!PEM_read_RSAPrivateKey(fp.get(), &key, nullptr, nullptr)) { |
| 160 | LOG(ERROR) << "Failed to read key"; |
| 161 | RSA_free(key); |
| 162 | return false; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 165 | g_key_list.push_back(key); |
| 166 | return true; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 169 | static std::string get_user_key_path() { |
Yurii Zubrytskyi | 09ecaa5 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 170 | const std::string home = adb_get_homedir_path(true); |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 171 | LOG(DEBUG) << "adb_get_homedir_path returned '" << home << "'"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 172 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 173 | const std::string android_dir = android::base::StringPrintf("%s%c.android", home.c_str(), |
| 174 | OS_PATH_SEPARATOR); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 175 | |
Yurii Zubrytskyi | 09ecaa5 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 176 | struct stat buf; |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 177 | if (stat(android_dir.c_str(), &buf) == -1) { |
| 178 | if (adb_mkdir(android_dir.c_str(), 0750) == -1) { |
| 179 | PLOG(ERROR) << "Cannot mkdir '" << android_dir << "'"; |
| 180 | return ""; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 184 | return android_dir + OS_PATH_SEPARATOR + "adbkey"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 187 | static bool get_user_key() { |
| 188 | std::string path = get_user_key_path(); |
| 189 | if (path.empty()) { |
| 190 | PLOG(ERROR) << "Error getting user key filename"; |
| 191 | return false; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 194 | struct stat buf; |
| 195 | if (stat(path.c_str(), &buf) == -1) { |
| 196 | LOG(INFO) << "User key '" << path << "' does not exist..."; |
Dan Albert | 3d978e6 | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 197 | if (!generate_key(path)) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 198 | LOG(ERROR) << "Failed to generate new key"; |
| 199 | return false; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 203 | return read_key(path); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 206 | static void get_vendor_keys() { |
Elliott Hughes | fb2ba51 | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 207 | const char* adb_keys_path = getenv("ADB_VENDOR_KEYS"); |
| 208 | if (adb_keys_path == nullptr) { |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 209 | return; |
Elliott Hughes | fb2ba51 | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 210 | } |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 211 | |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 212 | for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 213 | if (!read_key(path.c_str())) { |
| 214 | PLOG(ERROR) << "Failed to read '" << path << "'"; |
Elliott Hughes | fb2ba51 | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 215 | } |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 219 | std::deque<RSA*> adb_auth_get_private_keys() { |
| 220 | std::deque<RSA*> result; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 221 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 222 | // Copy all the currently known keys, increasing their reference count so they're |
| 223 | // usable until both we and the caller have freed our pointers. |
| 224 | std::lock_guard<std::mutex> lock(g_key_list_mutex); |
| 225 | for (const auto& key : g_key_list) { |
| 226 | RSA_up_ref(key); // Since we're handing out another pointer to this key... |
| 227 | result.push_back(key); |
| 228 | } |
| 229 | |
| 230 | // Add a sentinel to the list. Our caller uses this to mean "out of private keys, |
| 231 | // but try using the public key" (the empty deque could otherwise mean this _or_ |
| 232 | // that this function hasn't been called yet to request the keys). |
| 233 | result.push_back(nullptr); |
| 234 | |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | int adb_auth_sign(RSA* key, const unsigned char* token, size_t token_size, unsigned char* sig) { |
Sami Tolvanen | b92a35c | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 239 | if (token_size != TOKEN_SIZE) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 240 | D("Unexpected token size %zd", token_size); |
Sami Tolvanen | b92a35c | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 244 | unsigned int len; |
| 245 | if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key)) { |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 249 | D("adb_auth_sign len=%d", len); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 250 | return (int)len; |
| 251 | } |
| 252 | |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 253 | std::string adb_auth_get_userkey() { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 254 | std::string path = get_user_key_path(); |
| 255 | if (path.empty()) { |
| 256 | PLOG(ERROR) << "Error getting user key filename"; |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 257 | return ""; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 258 | } |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 259 | path += ".pub"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 260 | |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 261 | std::string content; |
| 262 | if (!android::base::ReadFileToString(path, &content)) { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 263 | PLOG(ERROR) << "Can't load '" << path << "'"; |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 264 | return ""; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 265 | } |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 266 | return content; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Nick Kralevich | 6183c96 | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 269 | int adb_auth_keygen(const char* filename) { |
Nick Kralevich | 6183c96 | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 270 | return (generate_key(filename) == 0); |
| 271 | } |
| 272 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 273 | void adb_auth_init() { |
| 274 | LOG(INFO) << "adb_auth_init..."; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 275 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 276 | if (!get_user_key()) { |
| 277 | LOG(ERROR) << "Failed to get user key"; |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame^] | 281 | std::lock_guard<std::mutex> lock(g_key_list_mutex); |
| 282 | get_vendor_keys(); |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 283 | } |