blob: 9d8dfc04a23b589b7d69da20f8ddec01398c23f5 [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
Elliott Hughes801066a2016-06-29 17:42:01 -070019#include "adb.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070020#include "adb_auth.h"
Elliott Hughes801066a2016-06-29 17:42:01 -070021#include "fdevent.h"
22#include "sysdeps.h"
23#include "transport.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070024
Dan Albertb302d122015-02-24 15:51:19 -080025#include <resolv.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070026#include <stdio.h>
27#include <string.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070028
Elliott Hughes801066a2016-06-29 17:42:01 -070029#include <memory>
30
31#include <android-base/file.h>
32#include <android-base/strings.h>
33#include <crypto_utils/android_pubkey.h>
Mattias Nisslera947b492016-03-31 16:32:09 +020034#include <openssl/obj_mac.h>
35#include <openssl/rsa.h>
36#include <openssl/sha.h>
37
Benoit Goby2cc19e42012-04-12 12:23:49 -070038static fdevent listener_fde;
Josh Gao7a8c7cb2016-02-23 18:05:57 -080039static fdevent framework_fde;
Benoit Goby2cc19e42012-04-12 12:23:49 -070040static int framework_fd = -1;
41
Benoit Gobyd592d6c2013-01-15 19:59:14 -080042static void usb_disconnected(void* unused, atransport* t);
Yabin Cui2d4c1982015-08-28 15:09:44 -070043static struct adisconnect usb_disconnect = { usb_disconnected, nullptr};
Benoit Gobyd592d6c2013-01-15 19:59:14 -080044static atransport* usb_transport;
45static bool needs_retry = false;
Benoit Goby2cc19e42012-04-12 12:23:49 -070046
Elliott Hughes801066a2016-06-29 17:42:01 -070047bool adb_auth_verify(uint8_t* token, size_t token_size, uint8_t* sig, int sig_len) {
48 static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
Benoit Goby2cc19e42012-04-12 12:23:49 -070049
Elliott Hughes801066a2016-06-29 17:42:01 -070050 for (const auto& path : key_paths) {
51 if (access(path, R_OK) == 0) {
52 LOG(INFO) << "Loading keys from " << path;
Benoit Goby2cc19e42012-04-12 12:23:49 -070053
Elliott Hughes801066a2016-06-29 17:42:01 -070054 std::string content;
55 if (!android::base::ReadFileToString(path, &content)) {
56 PLOG(ERROR) << "Couldn't read " << path;
57 continue;
58 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070059
Elliott Hughes801066a2016-06-29 17:42:01 -070060 for (const auto& line : android::base::Split(content, "\n")) {
61 // TODO: do we really have to support both ' ' and '\t'?
62 char* sep = strpbrk(const_cast<char*>(line.c_str()), " \t");
63 if (sep) *sep = '\0';
Benoit Goby2cc19e42012-04-12 12:23:49 -070064
Elliott Hughes801066a2016-06-29 17:42:01 -070065 // b64_pton requires one additional byte in the target buffer for
66 // decoding to succeed. See http://b/28035006 for details.
67 uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1];
68 if (__b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) {
69 LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path;
70 continue;
71 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070072
Elliott Hughes801066a2016-06-29 17:42:01 -070073 RSA* key = nullptr;
74 if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
75 LOG(ERROR) << "Failed to parse key " << line.c_str() << " in " << path;
76 continue;
77 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070078
Elliott Hughes801066a2016-06-29 17:42:01 -070079 bool verified = (RSA_verify(NID_sha1, token, token_size, sig, sig_len, key) == 1);
80 RSA_free(key);
81 if (verified) return true;
82 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070083 }
84 }
Elliott Hughes801066a2016-06-29 17:42:01 -070085 return false;
Benoit Goby2cc19e42012-04-12 12:23:49 -070086}
87
Elliott Hughes801066a2016-06-29 17:42:01 -070088bool adb_auth_generate_token(void* token, size_t token_size) {
89 FILE* fp = fopen("/dev/urandom", "re");
90 if (!fp) return false;
91 bool okay = (fread(token, token_size, 1, fp) == 1);
92 fclose(fp);
93 return okay;
Benoit Goby2cc19e42012-04-12 12:23:49 -070094}
95
Josh Gao7a8c7cb2016-02-23 18:05:57 -080096static void usb_disconnected(void* unused, atransport* t) {
Elliott Hughes801066a2016-06-29 17:42:01 -070097 LOG(INFO) << "USB disconnect";
Benoit Gobyd592d6c2013-01-15 19:59:14 -080098 usb_transport = NULL;
99 needs_retry = false;
100}
101
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800102static void framework_disconnected() {
Elliott Hughes801066a2016-06-29 17:42:01 -0700103 LOG(INFO) << "Framework disconnect";
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800104 fdevent_remove(&framework_fde);
105 framework_fd = -1;
106}
107
108static void adb_auth_event(int fd, unsigned events, void*) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700109 if (events & FDE_READ) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700110 char response[2];
111 int ret = unix_read(fd, response, sizeof(response));
Vince Harron82125422014-09-25 21:51:15 -0700112 if (ret <= 0) {
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800113 framework_disconnected();
114 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
115 if (usb_transport) {
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800116 adb_auth_verified(usb_transport);
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800117 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700118 }
119 }
120}
121
Elliott Hughes801066a2016-06-29 17:42:01 -0700122void adb_auth_confirm_key(unsigned char* key, size_t len, atransport* t) {
Benoit Gobyc0028882013-04-01 17:39:06 -0700123 if (!usb_transport) {
124 usb_transport = t;
Yabin Cui2d4c1982015-08-28 15:09:44 -0700125 t->AddDisconnect(&usb_disconnect);
Benoit Gobyc0028882013-04-01 17:39:06 -0700126 }
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800127
Benoit Goby2cc19e42012-04-12 12:23:49 -0700128 if (framework_fd < 0) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700129 LOG(ERROR) << "Client not connected";
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800130 needs_retry = true;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700131 return;
132 }
133
134 if (key[len - 1] != '\0') {
Elliott Hughes801066a2016-06-29 17:42:01 -0700135 LOG(ERROR) << "Key must be a null-terminated string";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700136 return;
137 }
138
Elliott Hughes801066a2016-06-29 17:42:01 -0700139 char msg[MAX_PAYLOAD_V1];
140 int msg_len = snprintf(msg, sizeof(msg), "PK%s", key);
141 if (msg_len >= static_cast<int>(sizeof(msg))) {
142 LOG(ERROR) << "Key too long (" << msg_len << ")";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700143 return;
144 }
Elliott Hughes801066a2016-06-29 17:42:01 -0700145 LOG(DEBUG) << "Sending '" << msg << "'";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700146
Elliott Hughes801066a2016-06-29 17:42:01 -0700147 if (unix_write(framework_fd, msg, msg_len) == -1) {
148 PLOG(ERROR) << "Failed to write PK";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700149 return;
150 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700151}
152
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800153static void adb_auth_listener(int fd, unsigned events, void* data) {
Erik Kline3c8d44d2015-12-01 17:27:59 +0900154 sockaddr_storage addr;
Elliott Hughes801066a2016-06-29 17:42:01 -0700155 socklen_t alen = sizeof(addr);
156 int s = adb_socket_accept(fd, reinterpret_cast<sockaddr*>(&addr), &alen);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700157 if (s < 0) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700158 PLOG(ERROR) << "Failed to accept";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700159 return;
160 }
161
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800162 if (framework_fd >= 0) {
Josh Gao5ca49db2016-02-25 14:11:32 -0800163 LOG(WARNING) << "adb received framework auth socket connection again";
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800164 framework_disconnected();
165 }
166
Benoit Goby2cc19e42012-04-12 12:23:49 -0700167 framework_fd = s;
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800168 fdevent_install(&framework_fde, framework_fd, adb_auth_event, nullptr);
169 fdevent_add(&framework_fde, FDE_READ);
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800170
171 if (needs_retry) {
172 needs_retry = false;
173 send_auth_request(usb_transport);
174 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700175}
176
Pavel Labath0bdb8672015-03-17 11:03:36 -0700177void adbd_cloexec_auth_socket() {
178 int fd = android_get_control_socket("adbd");
179 if (fd == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700180 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700181 return;
182 }
Nick Kralevich777523e2014-07-18 20:57:35 -0700183 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath0bdb8672015-03-17 11:03:36 -0700184}
Benoit Goby2cc19e42012-04-12 12:23:49 -0700185
Pavel Labath0bdb8672015-03-17 11:03:36 -0700186void adbd_auth_init(void) {
187 int fd = android_get_control_socket("adbd");
188 if (fd == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700189 PLOG(ERROR) << "Failed to get adbd socket";
Pavel Labath0bdb8672015-03-17 11:03:36 -0700190 return;
191 }
192
193 if (listen(fd, 4) == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700194 PLOG(ERROR) << "Failed to listen on '" << fd << "'";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700195 return;
196 }
197
198 fdevent_install(&listener_fde, fd, adb_auth_listener, NULL);
199 fdevent_add(&listener_fde, FDE_READ);
200}