blob: c3aef16d43a34509525146942c354cdf6a31e261 [file] [log] [blame]
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG AUTH
Dan Albert33134262015-03-19 15:21:08 -070018
Josh Gao2e671202016-08-18 22:00:12 -070019#include <dirent.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070020#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080021#include <stdlib.h>
Dan Albert33134262015-03-19 15:21:08 -070022#include <string.h>
Josh Gao2e671202016-08-18 22:00:12 -070023#if defined(__linux__)
24#include <sys/inotify.h>
25#endif
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070026
Josh Gao2e671202016-08-18 22:00:12 -070027#include <map>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070028#include <mutex>
Josh Gao2e671202016-08-18 22:00:12 -070029#include <set>
30#include <string>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070031
David Pursell5f787ed2016-01-27 08:52:53 -080032#include <android-base/errors.h>
Elliott Hughese8b663f2016-05-26 22:43:19 -070033#include <android-base/file.h>
Yurii Zubrytskyidace0152016-05-26 09:46:10 -070034#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/strings.h>
Mattias Nissler097b6bb2016-03-31 16:32:09 +020036#include <crypto_utils/android_pubkey.h>
Elliott Hughes625faf02016-06-21 16:50:48 -070037#include <openssl/base64.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070038#include <openssl/evp.h>
39#include <openssl/objects.h>
40#include <openssl/pem.h>
41#include <openssl/rsa.h>
42#include <openssl/sha.h>
43
Josh Gao2e671202016-08-18 22:00:12 -070044#include "adb.h"
45#include "adb_auth.h"
46#include "adb_utils.h"
47#include "sysdeps.h"
Josh Gao3bd28792016-10-05 19:02:29 -070048#include "transport.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070049
Josh Gao2e671202016-08-18 22:00:12 -070050static std::mutex& g_keys_mutex = *new std::mutex;
51static std::map<std::string, std::shared_ptr<RSA>>& g_keys =
52 *new std::map<std::string, std::shared_ptr<RSA>>;
53static std::map<int, std::string>& g_monitored_paths = *new std::map<int, std::string>;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070054
Elliott Hughes625faf02016-06-21 16:50:48 -070055static std::string get_user_info() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070056 LOG(INFO) << "get_user_info...";
57
Elliott Hughes625faf02016-06-21 16:50:48 -070058 std::string hostname;
59 if (getenv("HOSTNAME")) hostname = getenv("HOSTNAME");
60#if !defined(_WIN32)
61 char buf[64];
62 if (hostname.empty() && gethostname(buf, sizeof(buf)) != -1) hostname = buf;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070063#endif
Elliott Hughes625faf02016-06-21 16:50:48 -070064 if (hostname.empty()) hostname = "unknown";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070065
Elliott Hughes625faf02016-06-21 16:50:48 -070066 std::string username;
67 if (getenv("LOGNAME")) username = getenv("LOGNAME");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070068#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Elliott Hughes625faf02016-06-21 16:50:48 -070069 if (username.empty() && getlogin()) username = getlogin();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070070#endif
Elliott Hughes625faf02016-06-21 16:50:48 -070071 if (username.empty()) hostname = "unknown";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070072
Elliott Hughes625faf02016-06-21 16:50:48 -070073 return " " + username + "@" + hostname;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070074}
75
Elliott Hughes625faf02016-06-21 16:50:48 -070076static bool write_public_keyfile(RSA* private_key, const std::string& private_key_path) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070077 LOG(INFO) << "write_public_keyfile...";
78
Mattias Nissler097b6bb2016-03-31 16:32:09 +020079 uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE];
Elliott Hughes625faf02016-06-21 16:50:48 -070080 if (!android_pubkey_encode(private_key, binary_key_data, sizeof(binary_key_data))) {
81 LOG(ERROR) << "Failed to convert to public key";
82 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070083 }
84
Elliott Hughes0b771b32017-05-01 13:45:30 -070085 size_t expected_length;
86 if (!EVP_EncodedLength(&expected_length, sizeof(binary_key_data))) {
Elliott Hughes625faf02016-06-21 16:50:48 -070087 LOG(ERROR) << "Public key too large to base64 encode";
88 return false;
Adam Langley179d9d62014-09-03 14:34:47 -070089 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070090
Elliott Hughes625faf02016-06-21 16:50:48 -070091 std::string content;
Elliott Hughes0b771b32017-05-01 13:45:30 -070092 content.resize(expected_length);
93 size_t actual_length = EVP_EncodeBlock(reinterpret_cast<uint8_t*>(&content[0]), binary_key_data,
94 sizeof(binary_key_data));
95 content.resize(actual_length);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070096
Elliott Hughes625faf02016-06-21 16:50:48 -070097 content += get_user_info();
98
99 std::string path(private_key_path + ".pub");
100 if (!android::base::WriteStringToFile(content, path)) {
101 PLOG(ERROR) << "Failed to write public key to '" << path << "'";
102 return false;
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200103 }
104
Elliott Hughes625faf02016-06-21 16:50:48 -0700105 return true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700106}
107
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700108static int generate_key(const std::string& file) {
109 LOG(INFO) << "generate_key(" << file << ")...";
110
Benoit Goby64b31032012-08-31 12:14:21 -0700111 mode_t old_mask;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700112 FILE *f = NULL;
113 int ret = 0;
114
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700115 EVP_PKEY* pkey = EVP_PKEY_new();
116 BIGNUM* exponent = BN_new();
117 RSA* rsa = RSA_new();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700118 if (!pkey || !exponent || !rsa) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700119 LOG(ERROR) << "Failed to allocate key";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700120 goto out;
121 }
122
123 BN_set_word(exponent, RSA_F4);
124 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
125 EVP_PKEY_set1_RSA(pkey, rsa);
126
Benoit Goby64b31032012-08-31 12:14:21 -0700127 old_mask = umask(077);
128
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700129 f = fopen(file.c_str(), "w");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700130 if (!f) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700131 PLOG(ERROR) << "Failed to open " << file;
Benoit Goby64b31032012-08-31 12:14:21 -0700132 umask(old_mask);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700133 goto out;
134 }
135
Benoit Goby64b31032012-08-31 12:14:21 -0700136 umask(old_mask);
137
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700138 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700139 D("Failed to write key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700140 goto out;
141 }
142
143 if (!write_public_keyfile(rsa, file)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700144 D("Failed to write public key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700145 goto out;
146 }
147
148 ret = 1;
149
150out:
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700151 if (f) fclose(f);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700152 EVP_PKEY_free(pkey);
153 RSA_free(rsa);
154 BN_free(exponent);
155 return ret;
156}
157
Josh Gao2e671202016-08-18 22:00:12 -0700158static std::string hash_key(RSA* key) {
159 unsigned char* pubkey = nullptr;
160 int len = i2d_RSA_PUBKEY(key, &pubkey);
161 if (len < 0) {
162 LOG(ERROR) << "failed to encode RSA public key";
163 return std::string();
164 }
165
166 std::string result;
167 result.resize(SHA256_DIGEST_LENGTH);
168 SHA256(pubkey, len, reinterpret_cast<unsigned char*>(&result[0]));
169 OPENSSL_free(pubkey);
170 return result;
171}
172
173static bool read_key_file(const std::string& file) {
174 LOG(INFO) << "read_key_file '" << file << "'...";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700175
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700176 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file.c_str(), "r"), fclose);
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700177 if (!fp) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700178 PLOG(ERROR) << "Failed to open '" << file << "'";
179 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700180 }
181
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700182 RSA* key = RSA_new();
183 if (!PEM_read_RSAPrivateKey(fp.get(), &key, nullptr, nullptr)) {
184 LOG(ERROR) << "Failed to read key";
185 RSA_free(key);
186 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700187 }
188
Josh Gao2e671202016-08-18 22:00:12 -0700189 std::lock_guard<std::mutex> lock(g_keys_mutex);
190 std::string fingerprint = hash_key(key);
191 if (g_keys.find(fingerprint) != g_keys.end()) {
192 LOG(INFO) << "ignoring already-loaded key: " << file;
193 RSA_free(key);
194 } else {
195 g_keys[fingerprint] = std::shared_ptr<RSA>(key, RSA_free);
196 }
197
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700198 return true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700199}
200
Josh Gao2e671202016-08-18 22:00:12 -0700201static bool read_keys(const std::string& path, bool allow_dir = true) {
202 LOG(INFO) << "read_keys '" << path << "'...";
203
204 struct stat st;
205 if (stat(path.c_str(), &st) != 0) {
206 PLOG(ERROR) << "failed to stat '" << path << "'";
207 return false;
208 }
209
210 if (S_ISREG(st.st_mode)) {
Josh Gao2e671202016-08-18 22:00:12 -0700211 return read_key_file(path);
212 } else if (S_ISDIR(st.st_mode)) {
213 if (!allow_dir) {
214 // inotify isn't recursive. It would break expectations to load keys in nested
215 // directories but not monitor them for new keys.
216 LOG(WARNING) << "refusing to recurse into directory '" << path << "'";
217 return false;
218 }
219
220 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
221 if (!dir) {
222 PLOG(ERROR) << "failed to open directory '" << path << "'";
223 return false;
224 }
225
226 bool result = false;
227 while (struct dirent* dent = readdir(dir.get())) {
228 std::string name = dent->d_name;
229
230 // We can't use dent->d_type here because it's not available on Windows.
231 if (name == "." || name == "..") {
232 continue;
233 }
234
Josh Gaoa27666b2016-12-14 16:59:29 -0800235 if (!android::base::EndsWith(name, ".adb_key")) {
236 LOG(INFO) << "skipping non-adb_key '" << path << "/" << name << "'";
237 continue;
238 }
239
240 result |= read_key_file((path + OS_PATH_SEPARATOR + name));
Josh Gao2e671202016-08-18 22:00:12 -0700241 }
242 return result;
243 }
244
245 LOG(ERROR) << "unexpected type for '" << path << "': 0x" << std::hex << st.st_mode;
246 return false;
247}
248
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700249static std::string get_user_key_path() {
Josh Gaoe0b75022016-08-30 15:23:35 -0700250 return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adbkey";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700251}
252
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700253static bool get_user_key() {
254 std::string path = get_user_key_path();
255 if (path.empty()) {
256 PLOG(ERROR) << "Error getting user key filename";
257 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700258 }
259
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700260 struct stat buf;
261 if (stat(path.c_str(), &buf) == -1) {
262 LOG(INFO) << "User key '" << path << "' does not exist...";
Dan Albert286bb6d2015-07-09 20:35:09 +0000263 if (!generate_key(path)) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700264 LOG(ERROR) << "Failed to generate new key";
265 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700266 }
267 }
268
Josh Gao2e671202016-08-18 22:00:12 -0700269 return read_key_file(path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700270}
271
Josh Gao2e671202016-08-18 22:00:12 -0700272static std::set<std::string> get_vendor_keys() {
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700273 const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
274 if (adb_keys_path == nullptr) {
Josh Gao2e671202016-08-18 22:00:12 -0700275 return std::set<std::string>();
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700276 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700277
Josh Gao2e671202016-08-18 22:00:12 -0700278 std::set<std::string> result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700279 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Josh Gao2e671202016-08-18 22:00:12 -0700280 result.emplace(path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700281 }
Josh Gao2e671202016-08-18 22:00:12 -0700282 return result;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700283}
284
Josh Gao2e671202016-08-18 22:00:12 -0700285std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() {
286 std::deque<std::shared_ptr<RSA>> result;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700287
Josh Gao2e671202016-08-18 22:00:12 -0700288 // Copy all the currently known keys.
289 std::lock_guard<std::mutex> lock(g_keys_mutex);
290 for (const auto& it : g_keys) {
291 result.push_back(it.second);
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700292 }
293
294 // Add a sentinel to the list. Our caller uses this to mean "out of private keys,
295 // but try using the public key" (the empty deque could otherwise mean this _or_
296 // that this function hasn't been called yet to request the keys).
297 result.push_back(nullptr);
298
299 return result;
300}
301
Josh Gaof571fcb2018-02-05 18:49:10 -0800302static std::string adb_auth_sign(RSA* key, const char* token, size_t token_size) {
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000303 if (token_size != TOKEN_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700304 D("Unexpected token size %zd", token_size);
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000305 return 0;
306 }
307
Josh Gaof571fcb2018-02-05 18:49:10 -0800308 std::string result;
309 result.resize(MAX_PAYLOAD);
310
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700311 unsigned int len;
Josh Gao06d61d42016-10-06 13:31:44 -0700312 if (!RSA_sign(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
Josh Gaof571fcb2018-02-05 18:49:10 -0800313 reinterpret_cast<uint8_t*>(&result[0]), &len, key)) {
314 return std::string();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700315 }
316
Josh Gaof571fcb2018-02-05 18:49:10 -0800317 result.resize(len);
318
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700319 D("adb_auth_sign len=%d", len);
Josh Gaof571fcb2018-02-05 18:49:10 -0800320 return result;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700321}
322
Elliott Hughese8b663f2016-05-26 22:43:19 -0700323std::string adb_auth_get_userkey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700324 std::string path = get_user_key_path();
325 if (path.empty()) {
326 PLOG(ERROR) << "Error getting user key filename";
Elliott Hughese8b663f2016-05-26 22:43:19 -0700327 return "";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700328 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700329 path += ".pub";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700330
Elliott Hughese8b663f2016-05-26 22:43:19 -0700331 std::string content;
332 if (!android::base::ReadFileToString(path, &content)) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700333 PLOG(ERROR) << "Can't load '" << path << "'";
Elliott Hughese8b663f2016-05-26 22:43:19 -0700334 return "";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700335 }
Elliott Hughese8b663f2016-05-26 22:43:19 -0700336 return content;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700337}
338
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800339int adb_auth_keygen(const char* filename) {
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800340 return (generate_key(filename) == 0);
341}
342
Josh Gao2e671202016-08-18 22:00:12 -0700343#if defined(__linux__)
344static void adb_auth_inotify_update(int fd, unsigned fd_event, void*) {
345 LOG(INFO) << "adb_auth_inotify_update called";
346 if (!(fd_event & FDE_READ)) {
347 return;
348 }
349
350 char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
351 while (true) {
352 ssize_t rc = TEMP_FAILURE_RETRY(unix_read(fd, buf, sizeof(buf)));
353 if (rc == -1) {
354 if (errno == EAGAIN) {
355 LOG(INFO) << "done reading inotify fd";
356 break;
357 }
358 PLOG(FATAL) << "read of inotify event failed";
359 }
360
361 // The read potentially returned multiple events.
362 char* start = buf;
363 char* end = buf + rc;
364
365 while (start < end) {
366 inotify_event* event = reinterpret_cast<inotify_event*>(start);
367 auto root_it = g_monitored_paths.find(event->wd);
368 if (root_it == g_monitored_paths.end()) {
369 LOG(FATAL) << "observed inotify event for unmonitored path, wd = " << event->wd;
370 }
371
372 std::string path = root_it->second;
373 if (event->len > 0) {
374 path += '/';
375 path += event->name;
376 }
377
378 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
379 if (event->mask & IN_ISDIR) {
380 LOG(INFO) << "ignoring new directory at '" << path << "'";
381 } else {
382 LOG(INFO) << "observed new file at '" << path << "'";
383 read_keys(path, false);
384 }
385 } else {
386 LOG(WARNING) << "unmonitored event for " << path << ": 0x" << std::hex
387 << event->mask;
388 }
389
390 start += sizeof(struct inotify_event) + event->len;
391 }
392 }
393}
394
395static void adb_auth_inotify_init(const std::set<std::string>& paths) {
396 LOG(INFO) << "adb_auth_inotify_init...";
Josh Gaofb9a7e52017-01-18 18:14:17 -0800397
Josh Gao2e671202016-08-18 22:00:12 -0700398 int infd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
Josh Gaofb9a7e52017-01-18 18:14:17 -0800399 if (infd < 0) {
400 PLOG(ERROR) << "failed to create inotify fd";
401 return;
402 }
403
Josh Gao2e671202016-08-18 22:00:12 -0700404 for (const std::string& path : paths) {
405 int wd = inotify_add_watch(infd, path.c_str(), IN_CREATE | IN_MOVED_TO);
406 if (wd < 0) {
407 PLOG(ERROR) << "failed to inotify_add_watch on path '" << path;
408 continue;
409 }
410
411 g_monitored_paths[wd] = path;
412 LOG(INFO) << "watch descriptor " << wd << " registered for " << path;
413 }
414
415 fdevent* event = fdevent_create(infd, adb_auth_inotify_update, nullptr);
416 fdevent_add(event, FDE_READ);
417}
418#endif
419
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700420void adb_auth_init() {
421 LOG(INFO) << "adb_auth_init...";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700422
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700423 if (!get_user_key()) {
424 LOG(ERROR) << "Failed to get user key";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700425 return;
426 }
427
Josh Gao2e671202016-08-18 22:00:12 -0700428 const auto& key_paths = get_vendor_keys();
429
430#if defined(__linux__)
431 adb_auth_inotify_init(key_paths);
432#endif
433
434 for (const std::string& path : key_paths) {
435 read_keys(path.c_str());
436 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700437}
Josh Gao3bd28792016-10-05 19:02:29 -0700438
439static void send_auth_publickey(atransport* t) {
440 LOG(INFO) << "Calling send_auth_publickey";
441
442 std::string key = adb_auth_get_userkey();
443 if (key.empty()) {
444 D("Failed to get user public key");
445 return;
446 }
447
448 if (key.size() >= MAX_PAYLOAD_V1) {
449 D("User public key too large (%zu B)", key.size());
450 return;
451 }
452
453 apacket* p = get_apacket();
Josh Gao3bd28792016-10-05 19:02:29 -0700454 p->msg.command = A_AUTH;
455 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
456
Josh Gaof571fcb2018-02-05 18:49:10 -0800457 p->payload = std::move(key);
458
Josh Gao3bd28792016-10-05 19:02:29 -0700459 // adbd expects a null-terminated string.
Josh Gaof571fcb2018-02-05 18:49:10 -0800460 p->payload.push_back('\0');
461 p->msg.data_length = p->payload.size();
Josh Gao3bd28792016-10-05 19:02:29 -0700462 send_packet(p, t);
463}
464
Josh Gao06d61d42016-10-06 13:31:44 -0700465void send_auth_response(const char* token, size_t token_size, atransport* t) {
Josh Gao3bd28792016-10-05 19:02:29 -0700466 std::shared_ptr<RSA> key = t->NextKey();
467 if (key == nullptr) {
468 // No more private keys to try, send the public key.
469 send_auth_publickey(t);
470 return;
471 }
472
473 LOG(INFO) << "Calling send_auth_response";
474 apacket* p = get_apacket();
475
Josh Gaof571fcb2018-02-05 18:49:10 -0800476 std::string result = adb_auth_sign(key.get(), token, token_size);
477 if (result.empty()) {
Josh Gao3bd28792016-10-05 19:02:29 -0700478 D("Error signing the token");
479 put_apacket(p);
480 return;
481 }
482
483 p->msg.command = A_AUTH;
484 p->msg.arg0 = ADB_AUTH_SIGNATURE;
Josh Gaof571fcb2018-02-05 18:49:10 -0800485 p->payload = std::move(result);
486 p->msg.data_length = p->payload.size();
Josh Gao3bd28792016-10-05 19:02:29 -0700487 send_packet(p, t);
488}