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 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Christopher Ferris | 67a7a4a | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 22 | #include <string.h> |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 23 | #if defined(__linux__) |
| 24 | #include <sys/inotify.h> |
| 25 | #endif |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 26 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 27 | #include <map> |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 28 | #include <mutex> |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 29 | #include <set> |
| 30 | #include <string> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 31 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 32 | #include <adb/crypto/rsa_2048_key.h> |
David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 33 | #include <android-base/errors.h> |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> |
Yurii Zubrytskyi | dace015 | 2016-05-26 09:46:10 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 37 | #include <crypto_utils/android_pubkey.h> |
Elliott Hughes | 625faf0 | 2016-06-21 16:50:48 -0700 | [diff] [blame] | 38 | #include <openssl/base64.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 39 | #include <openssl/evp.h> |
| 40 | #include <openssl/objects.h> |
| 41 | #include <openssl/pem.h> |
| 42 | #include <openssl/rsa.h> |
| 43 | #include <openssl/sha.h> |
| 44 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 45 | #include "adb.h" |
| 46 | #include "adb_auth.h" |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 47 | #include "adb_io.h" |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 48 | #include "adb_utils.h" |
| 49 | #include "sysdeps.h" |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 50 | #include "transport.h" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 51 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 52 | static std::mutex& g_keys_mutex = *new std::mutex; |
| 53 | static std::map<std::string, std::shared_ptr<RSA>>& g_keys = |
| 54 | *new std::map<std::string, std::shared_ptr<RSA>>; |
| 55 | static std::map<int, std::string>& g_monitored_paths = *new std::map<int, std::string>; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 56 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 57 | using namespace adb::crypto; |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 58 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 59 | static bool generate_key(const std::string& file) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 60 | LOG(INFO) << "generate_key(" << file << ")..."; |
| 61 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 62 | auto rsa_2048 = CreateRSA2048Key(); |
| 63 | if (!rsa_2048) { |
| 64 | LOG(ERROR) << "Unable to create key"; |
| 65 | return false; |
| 66 | } |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 67 | std::string pubkey; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 68 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 69 | RSA* rsa = EVP_PKEY_get0_RSA(rsa_2048->GetEvpPkey()); |
| 70 | CHECK(rsa); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 71 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 72 | if (!CalculatePublicKey(&pubkey, rsa)) { |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 73 | LOG(ERROR) << "failed to calculate public key"; |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 74 | return false; |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 77 | mode_t old_mask = umask(077); |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 78 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 79 | std::unique_ptr<FILE, decltype(&fclose)> f(nullptr, &fclose); |
| 80 | f.reset(fopen(file.c_str(), "w")); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 81 | if (!f) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 82 | PLOG(ERROR) << "Failed to open " << file; |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 83 | umask(old_mask); |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 84 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Benoit Goby | 64b3103 | 2012-08-31 12:14:21 -0700 | [diff] [blame] | 87 | umask(old_mask); |
| 88 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 89 | if (!PEM_write_PrivateKey(f.get(), rsa_2048->GetEvpPkey(), nullptr, nullptr, 0, nullptr, |
| 90 | nullptr)) { |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 91 | LOG(ERROR) << "Failed to write key"; |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 92 | return false; |
Josh Gao | 032989a | 2019-04-29 12:36:32 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (!android::base::WriteStringToFile(pubkey, file + ".pub")) { |
| 96 | PLOG(ERROR) << "failed to write public key"; |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 97 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 100 | return true; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 103 | static std::string hash_key(RSA* key) { |
| 104 | unsigned char* pubkey = nullptr; |
| 105 | int len = i2d_RSA_PUBKEY(key, &pubkey); |
| 106 | if (len < 0) { |
| 107 | LOG(ERROR) << "failed to encode RSA public key"; |
| 108 | return std::string(); |
| 109 | } |
| 110 | |
| 111 | std::string result; |
| 112 | result.resize(SHA256_DIGEST_LENGTH); |
| 113 | SHA256(pubkey, len, reinterpret_cast<unsigned char*>(&result[0])); |
| 114 | OPENSSL_free(pubkey); |
| 115 | return result; |
| 116 | } |
| 117 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 118 | static std::shared_ptr<RSA> read_key_file(const std::string& file) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 119 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file.c_str(), "r"), fclose); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 120 | if (!fp) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 121 | PLOG(ERROR) << "Failed to open '" << file << "'"; |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 122 | return nullptr; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 125 | RSA* key = RSA_new(); |
| 126 | if (!PEM_read_RSAPrivateKey(fp.get(), &key, nullptr, nullptr)) { |
Elliott Hughes | 7fb1407 | 2019-10-07 08:21:58 -0700 | [diff] [blame] | 127 | LOG(ERROR) << "Failed to read key from '" << file << "'"; |
| 128 | ERR_print_errors_fp(stderr); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 129 | RSA_free(key); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | return std::shared_ptr<RSA>(key, RSA_free); |
| 134 | } |
| 135 | |
| 136 | static bool load_key(const std::string& file) { |
| 137 | std::shared_ptr<RSA> key = read_key_file(file); |
| 138 | if (!key) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 139 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 142 | std::lock_guard<std::mutex> lock(g_keys_mutex); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 143 | std::string fingerprint = hash_key(key.get()); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 144 | if (g_keys.find(fingerprint) != g_keys.end()) { |
| 145 | LOG(INFO) << "ignoring already-loaded key: " << file; |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 146 | } else { |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 147 | g_keys[fingerprint] = std::move(key); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 148 | } |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 149 | return true; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 152 | static bool load_keys(const std::string& path, bool allow_dir = true) { |
| 153 | LOG(INFO) << "load_keys '" << path << "'..."; |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 154 | |
| 155 | struct stat st; |
| 156 | if (stat(path.c_str(), &st) != 0) { |
| 157 | PLOG(ERROR) << "failed to stat '" << path << "'"; |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if (S_ISREG(st.st_mode)) { |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 162 | return load_key(path); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 163 | } else if (S_ISDIR(st.st_mode)) { |
| 164 | if (!allow_dir) { |
| 165 | // inotify isn't recursive. It would break expectations to load keys in nested |
| 166 | // directories but not monitor them for new keys. |
| 167 | LOG(WARNING) << "refusing to recurse into directory '" << path << "'"; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 172 | if (!dir) { |
| 173 | PLOG(ERROR) << "failed to open directory '" << path << "'"; |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | bool result = false; |
| 178 | while (struct dirent* dent = readdir(dir.get())) { |
| 179 | std::string name = dent->d_name; |
| 180 | |
| 181 | // We can't use dent->d_type here because it's not available on Windows. |
| 182 | if (name == "." || name == "..") { |
| 183 | continue; |
| 184 | } |
| 185 | |
Josh Gao | a27666b | 2016-12-14 16:59:29 -0800 | [diff] [blame] | 186 | if (!android::base::EndsWith(name, ".adb_key")) { |
| 187 | LOG(INFO) << "skipping non-adb_key '" << path << "/" << name << "'"; |
| 188 | continue; |
| 189 | } |
| 190 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 191 | result |= load_key((path + OS_PATH_SEPARATOR + name)); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 192 | } |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | LOG(ERROR) << "unexpected type for '" << path << "': 0x" << std::hex << st.st_mode; |
| 197 | return false; |
| 198 | } |
| 199 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 200 | static std::string get_user_key_path() { |
Josh Gao | e0b7502 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 201 | return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adbkey"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Elliott Hughes | 7fb1407 | 2019-10-07 08:21:58 -0700 | [diff] [blame] | 204 | static bool load_userkey() { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 205 | std::string path = get_user_key_path(); |
| 206 | if (path.empty()) { |
| 207 | PLOG(ERROR) << "Error getting user key filename"; |
| 208 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 211 | struct stat buf; |
| 212 | if (stat(path.c_str(), &buf) == -1) { |
| 213 | LOG(INFO) << "User key '" << path << "' does not exist..."; |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 214 | if (!generate_key(path)) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 215 | LOG(ERROR) << "Failed to generate new key"; |
| 216 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 220 | return load_key(path); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 223 | static std::set<std::string> get_vendor_keys() { |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 224 | const char* adb_keys_path = getenv("ADB_VENDOR_KEYS"); |
| 225 | if (adb_keys_path == nullptr) { |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 226 | return std::set<std::string>(); |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 227 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 228 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 229 | std::set<std::string> result; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 230 | for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) { |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 231 | result.emplace(path); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 232 | } |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 233 | return result; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 236 | std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() { |
| 237 | std::deque<std::shared_ptr<RSA>> result; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 238 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 239 | // Copy all the currently known keys. |
| 240 | std::lock_guard<std::mutex> lock(g_keys_mutex); |
| 241 | for (const auto& it : g_keys) { |
| 242 | result.push_back(it.second); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Add a sentinel to the list. Our caller uses this to mean "out of private keys, |
| 246 | // but try using the public key" (the empty deque could otherwise mean this _or_ |
| 247 | // that this function hasn't been called yet to request the keys). |
| 248 | result.push_back(nullptr); |
| 249 | |
| 250 | return result; |
| 251 | } |
| 252 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 253 | static std::string adb_auth_sign(RSA* key, const char* token, size_t token_size) { |
Sami Tolvanen | 7b9c20d | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 254 | if (token_size != TOKEN_SIZE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 255 | D("Unexpected token size %zd", token_size); |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 256 | return nullptr; |
Sami Tolvanen | 7b9c20d | 2015-01-27 16:48:35 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 259 | std::string result; |
| 260 | result.resize(MAX_PAYLOAD); |
| 261 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 262 | unsigned int len; |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 263 | if (!RSA_sign(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size, |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 264 | reinterpret_cast<uint8_t*>(&result[0]), &len, key)) { |
| 265 | return std::string(); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 268 | result.resize(len); |
| 269 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 270 | D("adb_auth_sign len=%d", len); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 271 | return result; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 274 | static bool pubkey_from_privkey(std::string* out, const std::string& path) { |
| 275 | std::shared_ptr<RSA> privkey = read_key_file(path); |
| 276 | if (!privkey) { |
| 277 | return false; |
| 278 | } |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 279 | return CalculatePublicKey(out, privkey.get()); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 282 | bssl::UniquePtr<EVP_PKEY> adb_auth_get_user_privkey() { |
| 283 | std::string path = get_user_key_path(); |
| 284 | if (path.empty()) { |
| 285 | PLOG(ERROR) << "Error getting user key filename"; |
| 286 | return nullptr; |
| 287 | } |
| 288 | |
| 289 | std::shared_ptr<RSA> rsa_privkey = read_key_file(path); |
| 290 | if (!rsa_privkey) { |
| 291 | return nullptr; |
| 292 | } |
| 293 | |
| 294 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 295 | if (!pkey) { |
| 296 | LOG(ERROR) << "Failed to allocate key"; |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
| 300 | EVP_PKEY_set1_RSA(pkey.get(), rsa_privkey.get()); |
| 301 | return pkey; |
| 302 | } |
| 303 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 304 | std::string adb_auth_get_userkey() { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 305 | std::string path = get_user_key_path(); |
| 306 | if (path.empty()) { |
| 307 | PLOG(ERROR) << "Error getting user key filename"; |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 308 | return ""; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 309 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 310 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 311 | std::string result; |
| 312 | if (!pubkey_from_privkey(&result, path)) { |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 313 | return ""; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 314 | } |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 315 | return result; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 318 | int adb_auth_keygen(const char* filename) { |
Joshua Duong | ef28ca4 | 2019-12-19 16:36:30 -0800 | [diff] [blame] | 319 | return !generate_key(filename); |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 322 | int adb_auth_pubkey(const char* filename) { |
| 323 | std::string pubkey; |
| 324 | if (!pubkey_from_privkey(&pubkey, filename)) { |
| 325 | return 1; |
| 326 | } |
| 327 | pubkey.push_back('\n'); |
| 328 | |
| 329 | return WriteFdExactly(STDOUT_FILENO, pubkey.data(), pubkey.size()) ? 0 : 1; |
| 330 | } |
| 331 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 332 | #if defined(__linux__) |
| 333 | static void adb_auth_inotify_update(int fd, unsigned fd_event, void*) { |
| 334 | LOG(INFO) << "adb_auth_inotify_update called"; |
| 335 | if (!(fd_event & FDE_READ)) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | char buf[sizeof(struct inotify_event) + NAME_MAX + 1]; |
| 340 | while (true) { |
| 341 | ssize_t rc = TEMP_FAILURE_RETRY(unix_read(fd, buf, sizeof(buf))); |
| 342 | if (rc == -1) { |
| 343 | if (errno == EAGAIN) { |
| 344 | LOG(INFO) << "done reading inotify fd"; |
| 345 | break; |
| 346 | } |
| 347 | PLOG(FATAL) << "read of inotify event failed"; |
| 348 | } |
| 349 | |
| 350 | // The read potentially returned multiple events. |
| 351 | char* start = buf; |
| 352 | char* end = buf + rc; |
| 353 | |
| 354 | while (start < end) { |
| 355 | inotify_event* event = reinterpret_cast<inotify_event*>(start); |
| 356 | auto root_it = g_monitored_paths.find(event->wd); |
| 357 | if (root_it == g_monitored_paths.end()) { |
| 358 | LOG(FATAL) << "observed inotify event for unmonitored path, wd = " << event->wd; |
| 359 | } |
| 360 | |
| 361 | std::string path = root_it->second; |
| 362 | if (event->len > 0) { |
| 363 | path += '/'; |
| 364 | path += event->name; |
| 365 | } |
| 366 | |
| 367 | if (event->mask & (IN_CREATE | IN_MOVED_TO)) { |
| 368 | if (event->mask & IN_ISDIR) { |
| 369 | LOG(INFO) << "ignoring new directory at '" << path << "'"; |
| 370 | } else { |
| 371 | LOG(INFO) << "observed new file at '" << path << "'"; |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 372 | load_keys(path, false); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 373 | } |
| 374 | } else { |
| 375 | LOG(WARNING) << "unmonitored event for " << path << ": 0x" << std::hex |
| 376 | << event->mask; |
| 377 | } |
| 378 | |
| 379 | start += sizeof(struct inotify_event) + event->len; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void adb_auth_inotify_init(const std::set<std::string>& paths) { |
| 385 | LOG(INFO) << "adb_auth_inotify_init..."; |
Josh Gao | fb9a7e5 | 2017-01-18 18:14:17 -0800 | [diff] [blame] | 386 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 387 | int infd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); |
Josh Gao | fb9a7e5 | 2017-01-18 18:14:17 -0800 | [diff] [blame] | 388 | if (infd < 0) { |
| 389 | PLOG(ERROR) << "failed to create inotify fd"; |
| 390 | return; |
| 391 | } |
| 392 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 393 | for (const std::string& path : paths) { |
| 394 | int wd = inotify_add_watch(infd, path.c_str(), IN_CREATE | IN_MOVED_TO); |
| 395 | if (wd < 0) { |
| 396 | PLOG(ERROR) << "failed to inotify_add_watch on path '" << path; |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | g_monitored_paths[wd] = path; |
| 401 | LOG(INFO) << "watch descriptor " << wd << " registered for " << path; |
| 402 | } |
| 403 | |
| 404 | fdevent* event = fdevent_create(infd, adb_auth_inotify_update, nullptr); |
| 405 | fdevent_add(event, FDE_READ); |
| 406 | } |
| 407 | #endif |
| 408 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 409 | void adb_auth_init() { |
| 410 | LOG(INFO) << "adb_auth_init..."; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 411 | |
Elliott Hughes | 7fb1407 | 2019-10-07 08:21:58 -0700 | [diff] [blame] | 412 | if (!load_userkey()) { |
| 413 | LOG(ERROR) << "Failed to load (or generate) user key"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 417 | const auto& key_paths = get_vendor_keys(); |
| 418 | |
| 419 | #if defined(__linux__) |
| 420 | adb_auth_inotify_init(key_paths); |
| 421 | #endif |
| 422 | |
| 423 | for (const std::string& path : key_paths) { |
Greg Kaiser | e2125fd | 2019-03-26 11:58:53 -0700 | [diff] [blame] | 424 | load_keys(path); |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 425 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 426 | } |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 427 | |
| 428 | static void send_auth_publickey(atransport* t) { |
| 429 | LOG(INFO) << "Calling send_auth_publickey"; |
| 430 | |
| 431 | std::string key = adb_auth_get_userkey(); |
| 432 | if (key.empty()) { |
| 433 | D("Failed to get user public key"); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | if (key.size() >= MAX_PAYLOAD_V1) { |
| 438 | D("User public key too large (%zu B)", key.size()); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | apacket* p = get_apacket(); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 443 | p->msg.command = A_AUTH; |
| 444 | p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; |
| 445 | |
| 446 | // adbd expects a null-terminated string. |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 447 | p->payload.assign(key.data(), key.data() + key.size() + 1); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 448 | p->msg.data_length = p->payload.size(); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 449 | send_packet(p, t); |
| 450 | } |
| 451 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 452 | void send_auth_response(const char* token, size_t token_size, atransport* t) { |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 453 | std::shared_ptr<RSA> key = t->NextKey(); |
| 454 | if (key == nullptr) { |
| 455 | // No more private keys to try, send the public key. |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 456 | t->SetConnectionState(kCsUnauthorized); |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 457 | t->SetConnectionEstablished(true); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 458 | send_auth_publickey(t); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | LOG(INFO) << "Calling send_auth_response"; |
| 463 | apacket* p = get_apacket(); |
| 464 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 465 | std::string result = adb_auth_sign(key.get(), token, token_size); |
| 466 | if (result.empty()) { |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 467 | D("Error signing the token"); |
| 468 | put_apacket(p); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | p->msg.command = A_AUTH; |
| 473 | p->msg.arg0 = ADB_AUTH_SIGNATURE; |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 474 | p->payload.assign(result.begin(), result.end()); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 475 | p->msg.data_length = p->payload.size(); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 476 | send_packet(p, t); |
| 477 | } |