blob: 00268a71bc89360de1c74db7a03c73278a29444c [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
Elliott Hughes0aeb5052016-06-29 17:42:01 -070019#include "adb.h"
Dan Albert33134262015-03-19 15:21:08 -070020#include "adb_auth.h"
Josh Gao2d03ad42019-07-08 14:36:02 -070021#include "adb_io.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070022#include "fdevent.h"
23#include "sysdeps.h"
24#include "transport.h"
Dan Albert33134262015-03-19 15:21:08 -070025
Dan Albert76649012015-02-24 15:51:19 -080026#include <resolv.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070027#include <stdio.h>
28#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070029
Elliott Hughes0aeb5052016-06-29 17:42:01 -070030#include <memory>
31
32#include <android-base/file.h>
33#include <android-base/strings.h>
34#include <crypto_utils/android_pubkey.h>
Mattias Nissler097b6bb2016-03-31 16:32:09 +020035#include <openssl/obj_mac.h>
36#include <openssl/rsa.h>
37#include <openssl/sha.h>
38
Josh Gao71f775a2018-05-14 11:14:33 -070039static fdevent* listener_fde = nullptr;
40static fdevent* framework_fde = nullptr;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070041static int framework_fd = -1;
42
Benoit Goby045a4a92013-01-15 19:59:14 -080043static void usb_disconnected(void* unused, atransport* t);
Yabin Cuib3298242015-08-28 15:09:44 -070044static struct adisconnect usb_disconnect = { usb_disconnected, nullptr};
Benoit Goby045a4a92013-01-15 19:59:14 -080045static atransport* usb_transport;
46static bool needs_retry = false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070047
Josh Gao3bd28792016-10-05 19:02:29 -070048bool auth_required = true;
49
Josh Gaof571fcb2018-02-05 18:49:10 -080050bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070051 static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070052
Elliott Hughes0aeb5052016-06-29 17:42:01 -070053 for (const auto& path : key_paths) {
54 if (access(path, R_OK) == 0) {
55 LOG(INFO) << "Loading keys from " << path;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070056
Elliott Hughes0aeb5052016-06-29 17:42:01 -070057 std::string content;
58 if (!android::base::ReadFileToString(path, &content)) {
59 PLOG(ERROR) << "Couldn't read " << path;
60 continue;
61 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070062
Elliott Hughes0aeb5052016-06-29 17:42:01 -070063 for (const auto& line : android::base::Split(content, "\n")) {
64 // TODO: do we really have to support both ' ' and '\t'?
65 char* sep = strpbrk(const_cast<char*>(line.c_str()), " \t");
66 if (sep) *sep = '\0';
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070067
Elliott Hughes0aeb5052016-06-29 17:42:01 -070068 // b64_pton requires one additional byte in the target buffer for
69 // decoding to succeed. See http://b/28035006 for details.
70 uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1];
Josh Gao776c2ec2019-01-22 19:36:15 -080071 if (b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070072 LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path;
73 continue;
74 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070075
Elliott Hughes0aeb5052016-06-29 17:42:01 -070076 RSA* key = nullptr;
77 if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
78 LOG(ERROR) << "Failed to parse key " << line.c_str() << " in " << path;
79 continue;
80 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070081
Josh Gao06d61d42016-10-06 13:31:44 -070082 bool verified =
83 (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
Josh Gaof571fcb2018-02-05 18:49:10 -080084 reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(),
85 key) == 1);
Elliott Hughes0aeb5052016-06-29 17:42:01 -070086 RSA_free(key);
87 if (verified) return true;
88 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070089 }
90 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -070091 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070092}
93
Josh Gao3bd28792016-10-05 19:02:29 -070094static bool adbd_auth_generate_token(void* token, size_t token_size) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070095 FILE* fp = fopen("/dev/urandom", "re");
96 if (!fp) return false;
97 bool okay = (fread(token, token_size, 1, fp) == 1);
98 fclose(fp);
99 return okay;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700100}
101
Josh Gao9f486112016-02-23 18:05:57 -0800102static void usb_disconnected(void* unused, atransport* t) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700103 LOG(INFO) << "USB disconnect";
Yi Kongaed415c2018-07-13 18:15:16 -0700104 usb_transport = nullptr;
Benoit Goby045a4a92013-01-15 19:59:14 -0800105 needs_retry = false;
106}
107
Josh Gao9f486112016-02-23 18:05:57 -0800108static void framework_disconnected() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700109 LOG(INFO) << "Framework disconnect";
Josh Gao71f775a2018-05-14 11:14:33 -0700110 if (framework_fde) {
111 fdevent_destroy(framework_fde);
112 framework_fd = -1;
113 }
Josh Gao9f486112016-02-23 18:05:57 -0800114}
115
Josh Gao3bd28792016-10-05 19:02:29 -0700116static void adbd_auth_event(int fd, unsigned events, void*) {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700117 if (events & FDE_READ) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700118 char response[2];
119 int ret = unix_read(fd, response, sizeof(response));
Vince Harronaf436b12014-09-25 21:51:15 -0700120 if (ret <= 0) {
Josh Gao9f486112016-02-23 18:05:57 -0800121 framework_disconnected();
122 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
123 if (usb_transport) {
Josh Gao3bd28792016-10-05 19:02:29 -0700124 adbd_auth_verified(usb_transport);
Josh Gao9f486112016-02-23 18:05:57 -0800125 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700126 }
127 }
128}
129
Josh Gao06d61d42016-10-06 13:31:44 -0700130void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) {
Benoit Gobyb66356c2013-04-01 17:39:06 -0700131 if (!usb_transport) {
132 usb_transport = t;
Yabin Cuib3298242015-08-28 15:09:44 -0700133 t->AddDisconnect(&usb_disconnect);
Benoit Gobyb66356c2013-04-01 17:39:06 -0700134 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800135
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700136 if (framework_fd < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700137 LOG(ERROR) << "Client not connected";
Benoit Goby045a4a92013-01-15 19:59:14 -0800138 needs_retry = true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700139 return;
140 }
141
142 if (key[len - 1] != '\0') {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700143 LOG(ERROR) << "Key must be a null-terminated string";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700144 return;
145 }
146
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700147 char msg[MAX_PAYLOAD_V1];
148 int msg_len = snprintf(msg, sizeof(msg), "PK%s", key);
149 if (msg_len >= static_cast<int>(sizeof(msg))) {
150 LOG(ERROR) << "Key too long (" << msg_len << ")";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700151 return;
152 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700153 LOG(DEBUG) << "Sending '" << msg << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700154
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700155 if (unix_write(framework_fd, msg, msg_len) == -1) {
156 PLOG(ERROR) << "Failed to write PK";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700157 return;
158 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700159}
160
Josh Gao3bd28792016-10-05 19:02:29 -0700161static void adbd_auth_listener(int fd, unsigned events, void* data) {
Josh Gao78e1eb12016-08-23 15:41:56 -0700162 int s = adb_socket_accept(fd, nullptr, nullptr);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700163 if (s < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700164 PLOG(ERROR) << "Failed to accept";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700165 return;
166 }
167
Josh Gao9f486112016-02-23 18:05:57 -0800168 if (framework_fd >= 0) {
Josh Gao443a52c2016-02-25 14:11:32 -0800169 LOG(WARNING) << "adb received framework auth socket connection again";
Josh Gao9f486112016-02-23 18:05:57 -0800170 framework_disconnected();
171 }
172
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700173 framework_fd = s;
Josh Gao71f775a2018-05-14 11:14:33 -0700174 framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
175 fdevent_add(framework_fde, FDE_READ);
Benoit Goby045a4a92013-01-15 19:59:14 -0800176
177 if (needs_retry) {
178 needs_retry = false;
179 send_auth_request(usb_transport);
180 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700181}
182
Pavel Labath64d9adc2015-03-17 11:03:36 -0700183void adbd_cloexec_auth_socket() {
184 int fd = android_get_control_socket("adbd");
185 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700186 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700187 return;
188 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700189 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath64d9adc2015-03-17 11:03:36 -0700190}
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700191
Pavel Labath64d9adc2015-03-17 11:03:36 -0700192void adbd_auth_init(void) {
193 int fd = android_get_control_socket("adbd");
194 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700195 PLOG(ERROR) << "Failed to get adbd socket";
Pavel Labath64d9adc2015-03-17 11:03:36 -0700196 return;
197 }
198
199 if (listen(fd, 4) == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700200 PLOG(ERROR) << "Failed to listen on '" << fd << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700201 return;
202 }
203
Yi Kongaed415c2018-07-13 18:15:16 -0700204 listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr);
Josh Gao71f775a2018-05-14 11:14:33 -0700205 fdevent_add(listener_fde, FDE_READ);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700206}
Josh Gao3bd28792016-10-05 19:02:29 -0700207
208void send_auth_request(atransport* t) {
209 LOG(INFO) << "Calling send_auth_request...";
210
211 if (!adbd_auth_generate_token(t->token, sizeof(t->token))) {
212 PLOG(ERROR) << "Error generating token";
213 return;
214 }
215
216 apacket* p = get_apacket();
Josh Gao3bd28792016-10-05 19:02:29 -0700217 p->msg.command = A_AUTH;
218 p->msg.arg0 = ADB_AUTH_TOKEN;
219 p->msg.data_length = sizeof(t->token);
Josh Gaof571fcb2018-02-05 18:49:10 -0800220 p->payload.assign(t->token, t->token + sizeof(t->token));
Josh Gao3bd28792016-10-05 19:02:29 -0700221 send_packet(p, t);
222}
223
Josh Gao184f5712017-07-26 11:06:55 -0700224void adbd_auth_verified(atransport* t) {
225 LOG(INFO) << "adb client authorized";
Josh Gao3bd28792016-10-05 19:02:29 -0700226 handle_online(t);
227 send_connect(t);
228}