blob: 3fd2b319454c6737b3af25240ad545fa0824c640 [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"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070021#include "fdevent.h"
22#include "sysdeps.h"
23#include "transport.h"
Dan Albert33134262015-03-19 15:21:08 -070024
Dan Albert76649012015-02-24 15:51:19 -080025#include <resolv.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070026#include <stdio.h>
27#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070028
Elliott Hughes0aeb5052016-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 Nissler097b6bb2016-03-31 16:32:09 +020034#include <openssl/obj_mac.h>
35#include <openssl/rsa.h>
36#include <openssl/sha.h>
37
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070038static fdevent listener_fde;
Josh Gao9f486112016-02-23 18:05:57 -080039static fdevent framework_fde;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070040static int framework_fd = -1;
41
Benoit Goby045a4a92013-01-15 19:59:14 -080042static void usb_disconnected(void* unused, atransport* t);
Yabin Cuib3298242015-08-28 15:09:44 -070043static struct adisconnect usb_disconnect = { usb_disconnected, nullptr};
Benoit Goby045a4a92013-01-15 19:59:14 -080044static atransport* usb_transport;
45static bool needs_retry = false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070046
Josh Gao3bd28792016-10-05 19:02:29 -070047bool auth_required = true;
48
Josh Gaof571fcb2018-02-05 18:49:10 -080049bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070050 static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070051
Elliott Hughes0aeb5052016-06-29 17:42:01 -070052 for (const auto& path : key_paths) {
53 if (access(path, R_OK) == 0) {
54 LOG(INFO) << "Loading keys from " << path;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070055
Elliott Hughes0aeb5052016-06-29 17:42:01 -070056 std::string content;
57 if (!android::base::ReadFileToString(path, &content)) {
58 PLOG(ERROR) << "Couldn't read " << path;
59 continue;
60 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070061
Elliott Hughes0aeb5052016-06-29 17:42:01 -070062 for (const auto& line : android::base::Split(content, "\n")) {
63 // TODO: do we really have to support both ' ' and '\t'?
64 char* sep = strpbrk(const_cast<char*>(line.c_str()), " \t");
65 if (sep) *sep = '\0';
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070066
Elliott Hughes0aeb5052016-06-29 17:42:01 -070067 // b64_pton requires one additional byte in the target buffer for
68 // decoding to succeed. See http://b/28035006 for details.
69 uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1];
70 if (__b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) {
71 LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path;
72 continue;
73 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070074
Elliott Hughes0aeb5052016-06-29 17:42:01 -070075 RSA* key = nullptr;
76 if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
77 LOG(ERROR) << "Failed to parse key " << line.c_str() << " in " << path;
78 continue;
79 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070080
Josh Gao06d61d42016-10-06 13:31:44 -070081 bool verified =
82 (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
Josh Gaof571fcb2018-02-05 18:49:10 -080083 reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(),
84 key) == 1);
Elliott Hughes0aeb5052016-06-29 17:42:01 -070085 RSA_free(key);
86 if (verified) return true;
87 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070088 }
89 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -070090 return false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070091}
92
Josh Gao3bd28792016-10-05 19:02:29 -070093static bool adbd_auth_generate_token(void* token, size_t token_size) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -070094 FILE* fp = fopen("/dev/urandom", "re");
95 if (!fp) return false;
96 bool okay = (fread(token, token_size, 1, fp) == 1);
97 fclose(fp);
98 return okay;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070099}
100
Josh Gao9f486112016-02-23 18:05:57 -0800101static void usb_disconnected(void* unused, atransport* t) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700102 LOG(INFO) << "USB disconnect";
Benoit Goby045a4a92013-01-15 19:59:14 -0800103 usb_transport = NULL;
104 needs_retry = false;
105}
106
Josh Gao9f486112016-02-23 18:05:57 -0800107static void framework_disconnected() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700108 LOG(INFO) << "Framework disconnect";
Josh Gao9f486112016-02-23 18:05:57 -0800109 fdevent_remove(&framework_fde);
110 framework_fd = -1;
111}
112
Josh Gao3bd28792016-10-05 19:02:29 -0700113static void adbd_auth_event(int fd, unsigned events, void*) {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700114 if (events & FDE_READ) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700115 char response[2];
116 int ret = unix_read(fd, response, sizeof(response));
Vince Harronaf436b12014-09-25 21:51:15 -0700117 if (ret <= 0) {
Josh Gao9f486112016-02-23 18:05:57 -0800118 framework_disconnected();
119 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
120 if (usb_transport) {
Josh Gao3bd28792016-10-05 19:02:29 -0700121 adbd_auth_verified(usb_transport);
Josh Gao9f486112016-02-23 18:05:57 -0800122 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700123 }
124 }
125}
126
Josh Gao06d61d42016-10-06 13:31:44 -0700127void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) {
Benoit Gobyb66356c2013-04-01 17:39:06 -0700128 if (!usb_transport) {
129 usb_transport = t;
Yabin Cuib3298242015-08-28 15:09:44 -0700130 t->AddDisconnect(&usb_disconnect);
Benoit Gobyb66356c2013-04-01 17:39:06 -0700131 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800132
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700133 if (framework_fd < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700134 LOG(ERROR) << "Client not connected";
Benoit Goby045a4a92013-01-15 19:59:14 -0800135 needs_retry = true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700136 return;
137 }
138
139 if (key[len - 1] != '\0') {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700140 LOG(ERROR) << "Key must be a null-terminated string";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700141 return;
142 }
143
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700144 char msg[MAX_PAYLOAD_V1];
145 int msg_len = snprintf(msg, sizeof(msg), "PK%s", key);
146 if (msg_len >= static_cast<int>(sizeof(msg))) {
147 LOG(ERROR) << "Key too long (" << msg_len << ")";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700148 return;
149 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700150 LOG(DEBUG) << "Sending '" << msg << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700151
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700152 if (unix_write(framework_fd, msg, msg_len) == -1) {
153 PLOG(ERROR) << "Failed to write PK";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700154 return;
155 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700156}
157
Josh Gao3bd28792016-10-05 19:02:29 -0700158static void adbd_auth_listener(int fd, unsigned events, void* data) {
Josh Gao78e1eb12016-08-23 15:41:56 -0700159 int s = adb_socket_accept(fd, nullptr, nullptr);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700160 if (s < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700161 PLOG(ERROR) << "Failed to accept";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700162 return;
163 }
164
Josh Gao9f486112016-02-23 18:05:57 -0800165 if (framework_fd >= 0) {
Josh Gao443a52c2016-02-25 14:11:32 -0800166 LOG(WARNING) << "adb received framework auth socket connection again";
Josh Gao9f486112016-02-23 18:05:57 -0800167 framework_disconnected();
168 }
169
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700170 framework_fd = s;
Josh Gao3bd28792016-10-05 19:02:29 -0700171 fdevent_install(&framework_fde, framework_fd, adbd_auth_event, nullptr);
Josh Gao9f486112016-02-23 18:05:57 -0800172 fdevent_add(&framework_fde, FDE_READ);
Benoit Goby045a4a92013-01-15 19:59:14 -0800173
174 if (needs_retry) {
175 needs_retry = false;
176 send_auth_request(usb_transport);
177 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700178}
179
Pavel Labath64d9adc2015-03-17 11:03:36 -0700180void adbd_cloexec_auth_socket() {
181 int fd = android_get_control_socket("adbd");
182 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700183 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700184 return;
185 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700186 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath64d9adc2015-03-17 11:03:36 -0700187}
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700188
Pavel Labath64d9adc2015-03-17 11:03:36 -0700189void adbd_auth_init(void) {
190 int fd = android_get_control_socket("adbd");
191 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700192 PLOG(ERROR) << "Failed to get adbd socket";
Pavel Labath64d9adc2015-03-17 11:03:36 -0700193 return;
194 }
195
196 if (listen(fd, 4) == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700197 PLOG(ERROR) << "Failed to listen on '" << fd << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700198 return;
199 }
200
Josh Gao3bd28792016-10-05 19:02:29 -0700201 fdevent_install(&listener_fde, fd, adbd_auth_listener, NULL);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700202 fdevent_add(&listener_fde, FDE_READ);
203}
Josh Gao3bd28792016-10-05 19:02:29 -0700204
205void send_auth_request(atransport* t) {
206 LOG(INFO) << "Calling send_auth_request...";
207
208 if (!adbd_auth_generate_token(t->token, sizeof(t->token))) {
209 PLOG(ERROR) << "Error generating token";
210 return;
211 }
212
213 apacket* p = get_apacket();
Josh Gao3bd28792016-10-05 19:02:29 -0700214 p->msg.command = A_AUTH;
215 p->msg.arg0 = ADB_AUTH_TOKEN;
216 p->msg.data_length = sizeof(t->token);
Josh Gaof571fcb2018-02-05 18:49:10 -0800217 p->payload.assign(t->token, t->token + sizeof(t->token));
Josh Gao3bd28792016-10-05 19:02:29 -0700218 send_packet(p, t);
219}
220
Josh Gao184f5712017-07-26 11:06:55 -0700221void adbd_auth_verified(atransport* t) {
222 LOG(INFO) << "adb client authorized";
Josh Gao3bd28792016-10-05 19:02:29 -0700223 handle_online(t);
224 send_connect(t);
225}