blob: 365bf77a9ac5b4ba4f51518aa16460a054574613 [file] [log] [blame]
Benoit Goby2cc19e42012-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG AUTH
Dan Albertdb6fe642015-03-19 15:21:08 -070018
Josh Gao22cb70b2016-08-18 22:00:12 -070019#include <dirent.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070020#include <stdio.h>
Christopher Ferris054d1702014-11-06 14:34:24 -080021#include <stdlib.h>
Dan Albertdb6fe642015-03-19 15:21:08 -070022#include <string.h>
Josh Gao22cb70b2016-08-18 22:00:12 -070023#if defined(__linux__)
24#include <sys/inotify.h>
25#endif
Benoit Goby2cc19e42012-04-12 12:23:49 -070026
Josh Gao22cb70b2016-08-18 22:00:12 -070027#include <map>
Elliott Hughes801066a2016-06-29 17:42:01 -070028#include <mutex>
Josh Gao22cb70b2016-08-18 22:00:12 -070029#include <set>
30#include <string>
Benoit Goby2cc19e42012-04-12 12:23:49 -070031
David Pursellc573d522016-01-27 08:52:53 -080032#include <android-base/errors.h>
Elliott Hughese0a6e2a2016-05-26 22:43:19 -070033#include <android-base/file.h>
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -070034#include <android-base/stringprintf.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080035#include <android-base/strings.h>
Mattias Nisslera947b492016-03-31 16:32:09 +020036#include <crypto_utils/android_pubkey.h>
Elliott Hughesab942fd2016-06-21 16:50:48 -070037#include <openssl/base64.h>
Benoit Goby2cc19e42012-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 Gao22cb70b2016-08-18 22:00:12 -070044#include "adb.h"
45#include "adb_auth.h"
46#include "adb_utils.h"
47#include "sysdeps.h"
Josh Gaoeac20582016-10-05 19:02:29 -070048#include "transport.h"
Benoit Goby2cc19e42012-04-12 12:23:49 -070049
Josh Gao22cb70b2016-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 Goby2cc19e42012-04-12 12:23:49 -070054
Elliott Hughesab942fd2016-06-21 16:50:48 -070055static std::string get_user_info() {
Elliott Hughes801066a2016-06-29 17:42:01 -070056 LOG(INFO) << "get_user_info...";
57
Elliott Hughesab942fd2016-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 Goby2cc19e42012-04-12 12:23:49 -070063#endif
Elliott Hughesab942fd2016-06-21 16:50:48 -070064 if (hostname.empty()) hostname = "unknown";
Benoit Goby2cc19e42012-04-12 12:23:49 -070065
Elliott Hughesab942fd2016-06-21 16:50:48 -070066 std::string username;
67 if (getenv("LOGNAME")) username = getenv("LOGNAME");
Benoit Goby2cc19e42012-04-12 12:23:49 -070068#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Elliott Hughesab942fd2016-06-21 16:50:48 -070069 if (username.empty() && getlogin()) username = getlogin();
Benoit Goby2cc19e42012-04-12 12:23:49 -070070#endif
Elliott Hughesab942fd2016-06-21 16:50:48 -070071 if (username.empty()) hostname = "unknown";
Benoit Goby2cc19e42012-04-12 12:23:49 -070072
Elliott Hughesab942fd2016-06-21 16:50:48 -070073 return " " + username + "@" + hostname;
Benoit Goby2cc19e42012-04-12 12:23:49 -070074}
75
Elliott Hughesab942fd2016-06-21 16:50:48 -070076static bool write_public_keyfile(RSA* private_key, const std::string& private_key_path) {
Elliott Hughes801066a2016-06-29 17:42:01 -070077 LOG(INFO) << "write_public_keyfile...";
78
Mattias Nisslera947b492016-03-31 16:32:09 +020079 uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE];
Elliott Hughesab942fd2016-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 Goby2cc19e42012-04-12 12:23:49 -070083 }
84
Elliott Hughes6aa72892017-05-01 13:45:30 -070085 size_t expected_length;
86 if (!EVP_EncodedLength(&expected_length, sizeof(binary_key_data))) {
Elliott Hughesab942fd2016-06-21 16:50:48 -070087 LOG(ERROR) << "Public key too large to base64 encode";
88 return false;
Adam Langley29f6cdb2014-09-03 14:34:47 -070089 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070090
Elliott Hughesab942fd2016-06-21 16:50:48 -070091 std::string content;
Elliott Hughes6aa72892017-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 Goby2cc19e42012-04-12 12:23:49 -070096
Elliott Hughesab942fd2016-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 Nisslera947b492016-03-31 16:32:09 +0200103 }
104
Elliott Hughesab942fd2016-06-21 16:50:48 -0700105 return true;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700106}
107
Elliott Hughes801066a2016-06-29 17:42:01 -0700108static int generate_key(const std::string& file) {
109 LOG(INFO) << "generate_key(" << file << ")...";
110
Benoit Gobycb37c502012-08-31 12:14:21 -0700111 mode_t old_mask;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700112 FILE *f = NULL;
113 int ret = 0;
114
Elliott Hughes801066a2016-06-29 17:42:01 -0700115 EVP_PKEY* pkey = EVP_PKEY_new();
116 BIGNUM* exponent = BN_new();
117 RSA* rsa = RSA_new();
Benoit Goby2cc19e42012-04-12 12:23:49 -0700118 if (!pkey || !exponent || !rsa) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700119 LOG(ERROR) << "Failed to allocate key";
Benoit Goby2cc19e42012-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 Gobycb37c502012-08-31 12:14:21 -0700127 old_mask = umask(077);
128
Elliott Hughes801066a2016-06-29 17:42:01 -0700129 f = fopen(file.c_str(), "w");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700130 if (!f) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700131 PLOG(ERROR) << "Failed to open " << file;
Benoit Gobycb37c502012-08-31 12:14:21 -0700132 umask(old_mask);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700133 goto out;
134 }
135
Benoit Gobycb37c502012-08-31 12:14:21 -0700136 umask(old_mask);
137
Benoit Goby2cc19e42012-04-12 12:23:49 -0700138 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700139 D("Failed to write key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700140 goto out;
141 }
142
143 if (!write_public_keyfile(rsa, file)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700144 D("Failed to write public key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700145 goto out;
146 }
147
148 ret = 1;
149
150out:
Elliott Hughes801066a2016-06-29 17:42:01 -0700151 if (f) fclose(f);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700152 EVP_PKEY_free(pkey);
153 RSA_free(rsa);
154 BN_free(exponent);
155 return ret;
156}
157
Josh Gao22cb70b2016-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 Goby2cc19e42012-04-12 12:23:49 -0700175
Elliott Hughes801066a2016-06-29 17:42:01 -0700176 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file.c_str(), "r"), fclose);
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700177 if (!fp) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700178 PLOG(ERROR) << "Failed to open '" << file << "'";
179 return false;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700180 }
181
Elliott Hughes801066a2016-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 Goby2cc19e42012-04-12 12:23:49 -0700187 }
188
Josh Gao22cb70b2016-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 Hughes801066a2016-06-29 17:42:01 -0700198 return true;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700199}
200
Josh Gao22cb70b2016-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 Gao22cb70b2016-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 Gaoe9e7bac2016-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 Gao22cb70b2016-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 Hughes801066a2016-06-29 17:42:01 -0700249static std::string get_user_key_path() {
Josh Gao62ff9d42016-08-30 15:23:35 -0700250 return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adbkey";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700251}
252
Elliott Hughes801066a2016-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 Goby2cc19e42012-04-12 12:23:49 -0700258 }
259
Elliott Hughes801066a2016-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 Albert3d978e62015-07-09 20:35:09 +0000263 if (!generate_key(path)) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700264 LOG(ERROR) << "Failed to generate new key";
265 return false;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700266 }
267 }
268
Josh Gao22cb70b2016-08-18 22:00:12 -0700269 return read_key_file(path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700270}
271
Josh Gao22cb70b2016-08-18 22:00:12 -0700272static std::set<std::string> get_vendor_keys() {
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700273 const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
274 if (adb_keys_path == nullptr) {
Josh Gao22cb70b2016-08-18 22:00:12 -0700275 return std::set<std::string>();
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700276 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700277
Josh Gao22cb70b2016-08-18 22:00:12 -0700278 std::set<std::string> result;
Elliott Hughes85952832015-10-07 15:59:35 -0700279 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Josh Gao22cb70b2016-08-18 22:00:12 -0700280 result.emplace(path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700281 }
Josh Gao22cb70b2016-08-18 22:00:12 -0700282 return result;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700283}
284
Josh Gao22cb70b2016-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 Goby2cc19e42012-04-12 12:23:49 -0700287
Josh Gao22cb70b2016-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 Hughes801066a2016-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 Gao67ac3792016-10-06 13:31:44 -0700302static int adb_auth_sign(RSA* key, const char* token, size_t token_size, char* sig) {
Sami Tolvanenb92a35c2015-01-27 16:48:35 +0000303 if (token_size != TOKEN_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700304 D("Unexpected token size %zd", token_size);
Sami Tolvanenb92a35c2015-01-27 16:48:35 +0000305 return 0;
306 }
307
Elliott Hughes801066a2016-06-29 17:42:01 -0700308 unsigned int len;
Josh Gao67ac3792016-10-06 13:31:44 -0700309 if (!RSA_sign(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
310 reinterpret_cast<uint8_t*>(sig), &len, key)) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700311 return 0;
312 }
313
Yabin Cui815ad882015-09-02 17:44:28 -0700314 D("adb_auth_sign len=%d", len);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700315 return (int)len;
316}
317
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700318std::string adb_auth_get_userkey() {
Elliott Hughes801066a2016-06-29 17:42:01 -0700319 std::string path = get_user_key_path();
320 if (path.empty()) {
321 PLOG(ERROR) << "Error getting user key filename";
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700322 return "";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700323 }
Elliott Hughes801066a2016-06-29 17:42:01 -0700324 path += ".pub";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700325
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700326 std::string content;
327 if (!android::base::ReadFileToString(path, &content)) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700328 PLOG(ERROR) << "Can't load '" << path << "'";
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700329 return "";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700330 }
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700331 return content;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700332}
333
Nick Kralevich6183c962014-11-13 15:17:29 -0800334int adb_auth_keygen(const char* filename) {
Nick Kralevich6183c962014-11-13 15:17:29 -0800335 return (generate_key(filename) == 0);
336}
337
Josh Gao22cb70b2016-08-18 22:00:12 -0700338#if defined(__linux__)
339static void adb_auth_inotify_update(int fd, unsigned fd_event, void*) {
340 LOG(INFO) << "adb_auth_inotify_update called";
341 if (!(fd_event & FDE_READ)) {
342 return;
343 }
344
345 char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
346 while (true) {
347 ssize_t rc = TEMP_FAILURE_RETRY(unix_read(fd, buf, sizeof(buf)));
348 if (rc == -1) {
349 if (errno == EAGAIN) {
350 LOG(INFO) << "done reading inotify fd";
351 break;
352 }
353 PLOG(FATAL) << "read of inotify event failed";
354 }
355
356 // The read potentially returned multiple events.
357 char* start = buf;
358 char* end = buf + rc;
359
360 while (start < end) {
361 inotify_event* event = reinterpret_cast<inotify_event*>(start);
362 auto root_it = g_monitored_paths.find(event->wd);
363 if (root_it == g_monitored_paths.end()) {
364 LOG(FATAL) << "observed inotify event for unmonitored path, wd = " << event->wd;
365 }
366
367 std::string path = root_it->second;
368 if (event->len > 0) {
369 path += '/';
370 path += event->name;
371 }
372
373 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
374 if (event->mask & IN_ISDIR) {
375 LOG(INFO) << "ignoring new directory at '" << path << "'";
376 } else {
377 LOG(INFO) << "observed new file at '" << path << "'";
378 read_keys(path, false);
379 }
380 } else {
381 LOG(WARNING) << "unmonitored event for " << path << ": 0x" << std::hex
382 << event->mask;
383 }
384
385 start += sizeof(struct inotify_event) + event->len;
386 }
387 }
388}
389
390static void adb_auth_inotify_init(const std::set<std::string>& paths) {
391 LOG(INFO) << "adb_auth_inotify_init...";
Josh Gao6946f112017-01-18 18:14:17 -0800392
Josh Gao22cb70b2016-08-18 22:00:12 -0700393 int infd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
Josh Gao6946f112017-01-18 18:14:17 -0800394 if (infd < 0) {
395 PLOG(ERROR) << "failed to create inotify fd";
396 return;
397 }
398
Josh Gao22cb70b2016-08-18 22:00:12 -0700399 for (const std::string& path : paths) {
400 int wd = inotify_add_watch(infd, path.c_str(), IN_CREATE | IN_MOVED_TO);
401 if (wd < 0) {
402 PLOG(ERROR) << "failed to inotify_add_watch on path '" << path;
403 continue;
404 }
405
406 g_monitored_paths[wd] = path;
407 LOG(INFO) << "watch descriptor " << wd << " registered for " << path;
408 }
409
410 fdevent* event = fdevent_create(infd, adb_auth_inotify_update, nullptr);
411 fdevent_add(event, FDE_READ);
412}
413#endif
414
Elliott Hughes801066a2016-06-29 17:42:01 -0700415void adb_auth_init() {
416 LOG(INFO) << "adb_auth_init...";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700417
Elliott Hughes801066a2016-06-29 17:42:01 -0700418 if (!get_user_key()) {
419 LOG(ERROR) << "Failed to get user key";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700420 return;
421 }
422
Josh Gao22cb70b2016-08-18 22:00:12 -0700423 const auto& key_paths = get_vendor_keys();
424
425#if defined(__linux__)
426 adb_auth_inotify_init(key_paths);
427#endif
428
429 for (const std::string& path : key_paths) {
430 read_keys(path.c_str());
431 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700432}
Josh Gaoeac20582016-10-05 19:02:29 -0700433
434static void send_auth_publickey(atransport* t) {
435 LOG(INFO) << "Calling send_auth_publickey";
436
437 std::string key = adb_auth_get_userkey();
438 if (key.empty()) {
439 D("Failed to get user public key");
440 return;
441 }
442
443 if (key.size() >= MAX_PAYLOAD_V1) {
444 D("User public key too large (%zu B)", key.size());
445 return;
446 }
447
448 apacket* p = get_apacket();
449 memcpy(p->data, key.c_str(), key.size() + 1);
450
451 p->msg.command = A_AUTH;
452 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
453
454 // adbd expects a null-terminated string.
455 p->msg.data_length = key.size() + 1;
456 send_packet(p, t);
457}
458
Josh Gao67ac3792016-10-06 13:31:44 -0700459void send_auth_response(const char* token, size_t token_size, atransport* t) {
Josh Gaoeac20582016-10-05 19:02:29 -0700460 std::shared_ptr<RSA> key = t->NextKey();
461 if (key == nullptr) {
462 // No more private keys to try, send the public key.
463 send_auth_publickey(t);
464 return;
465 }
466
467 LOG(INFO) << "Calling send_auth_response";
468 apacket* p = get_apacket();
469
470 int ret = adb_auth_sign(key.get(), token, token_size, p->data);
471 if (!ret) {
472 D("Error signing the token");
473 put_apacket(p);
474 return;
475 }
476
477 p->msg.command = A_AUTH;
478 p->msg.arg0 = ADB_AUTH_SIGNATURE;
479 p->msg.data_length = ret;
480 send_packet(p, t);
481}