blob: a829bac0af82fb2b4783438a36ae4a1066b7b689 [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"
Michael Groover3857dea2018-12-28 20:35:37 -080021#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
Michael Groover3857dea2018-12-28 20:35:37 -080043static void adb_disconnected(void* unused, atransport* t);
44static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
45static atransport* adb_transport;
Benoit Goby045a4a92013-01-15 19:59:14 -080046static 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
Michael Groover3857dea2018-12-28 20:35:37 -0800102static void adb_disconnected(void* unused, atransport* t) {
103 LOG(INFO) << "ADB disconnect";
104 adb_transport = nullptr;
Benoit Goby045a4a92013-01-15 19:59:14 -0800105 needs_retry = false;
Michael Groover3857dea2018-12-28 20:35:37 -0800106 if (framework_fd >= 0) {
107 const char msg[] = "DC";
108 LOG(DEBUG) << "Sending '" << msg << "'";
109 if (!WriteFdExactly(framework_fd, msg, sizeof(msg))) {
110 PLOG(ERROR) << "Failed to send disconnected message";
111 }
112 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800113}
114
Josh Gao9f486112016-02-23 18:05:57 -0800115static void framework_disconnected() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700116 LOG(INFO) << "Framework disconnect";
Josh Gao71f775a2018-05-14 11:14:33 -0700117 if (framework_fde) {
118 fdevent_destroy(framework_fde);
119 framework_fd = -1;
120 }
Josh Gao9f486112016-02-23 18:05:57 -0800121}
122
Josh Gao3bd28792016-10-05 19:02:29 -0700123static void adbd_auth_event(int fd, unsigned events, void*) {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700124 if (events & FDE_READ) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700125 char response[2];
126 int ret = unix_read(fd, response, sizeof(response));
Vince Harronaf436b12014-09-25 21:51:15 -0700127 if (ret <= 0) {
Josh Gao9f486112016-02-23 18:05:57 -0800128 framework_disconnected();
129 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
Michael Groover3857dea2018-12-28 20:35:37 -0800130 if (adb_transport) {
131 adbd_auth_verified(adb_transport);
Josh Gao9f486112016-02-23 18:05:57 -0800132 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700133 }
134 }
135}
136
Josh Gao06d61d42016-10-06 13:31:44 -0700137void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) {
Michael Groover3857dea2018-12-28 20:35:37 -0800138 if (!adb_transport) {
139 adb_transport = t;
140 t->AddDisconnect(&adb_disconnect);
Benoit Gobyb66356c2013-04-01 17:39:06 -0700141 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800142
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700143 if (framework_fd < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700144 LOG(ERROR) << "Client not connected";
Benoit Goby045a4a92013-01-15 19:59:14 -0800145 needs_retry = true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700146 return;
147 }
148
149 if (key[len - 1] != '\0') {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700150 LOG(ERROR) << "Key must be a null-terminated string";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700151 return;
152 }
153
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700154 char msg[MAX_PAYLOAD_V1];
155 int msg_len = snprintf(msg, sizeof(msg), "PK%s", key);
156 if (msg_len >= static_cast<int>(sizeof(msg))) {
157 LOG(ERROR) << "Key too long (" << msg_len << ")";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700158 return;
159 }
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700160 LOG(DEBUG) << "Sending '" << msg << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700161
Michael Groover3857dea2018-12-28 20:35:37 -0800162 if (!WriteFdExactly(framework_fd, msg, msg_len)) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700163 PLOG(ERROR) << "Failed to write PK";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700164 return;
165 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700166}
167
Josh Gao3bd28792016-10-05 19:02:29 -0700168static void adbd_auth_listener(int fd, unsigned events, void* data) {
Josh Gao78e1eb12016-08-23 15:41:56 -0700169 int s = adb_socket_accept(fd, nullptr, nullptr);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700170 if (s < 0) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700171 PLOG(ERROR) << "Failed to accept";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700172 return;
173 }
174
Josh Gao9f486112016-02-23 18:05:57 -0800175 if (framework_fd >= 0) {
Josh Gao443a52c2016-02-25 14:11:32 -0800176 LOG(WARNING) << "adb received framework auth socket connection again";
Josh Gao9f486112016-02-23 18:05:57 -0800177 framework_disconnected();
178 }
179
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700180 framework_fd = s;
Josh Gao71f775a2018-05-14 11:14:33 -0700181 framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
182 fdevent_add(framework_fde, FDE_READ);
Benoit Goby045a4a92013-01-15 19:59:14 -0800183
184 if (needs_retry) {
185 needs_retry = false;
Michael Groover3857dea2018-12-28 20:35:37 -0800186 send_auth_request(adb_transport);
Benoit Goby045a4a92013-01-15 19:59:14 -0800187 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700188}
189
Pavel Labath64d9adc2015-03-17 11:03:36 -0700190void adbd_cloexec_auth_socket() {
191 int fd = android_get_control_socket("adbd");
192 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700193 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700194 return;
195 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700196 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath64d9adc2015-03-17 11:03:36 -0700197}
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700198
Pavel Labath64d9adc2015-03-17 11:03:36 -0700199void adbd_auth_init(void) {
200 int fd = android_get_control_socket("adbd");
201 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700202 PLOG(ERROR) << "Failed to get adbd socket";
Pavel Labath64d9adc2015-03-17 11:03:36 -0700203 return;
204 }
205
206 if (listen(fd, 4) == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700207 PLOG(ERROR) << "Failed to listen on '" << fd << "'";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700208 return;
209 }
210
Yi Kongaed415c2018-07-13 18:15:16 -0700211 listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr);
Josh Gao71f775a2018-05-14 11:14:33 -0700212 fdevent_add(listener_fde, FDE_READ);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700213}
Josh Gao3bd28792016-10-05 19:02:29 -0700214
215void send_auth_request(atransport* t) {
216 LOG(INFO) << "Calling send_auth_request...";
217
218 if (!adbd_auth_generate_token(t->token, sizeof(t->token))) {
219 PLOG(ERROR) << "Error generating token";
220 return;
221 }
222
223 apacket* p = get_apacket();
Josh Gao3bd28792016-10-05 19:02:29 -0700224 p->msg.command = A_AUTH;
225 p->msg.arg0 = ADB_AUTH_TOKEN;
226 p->msg.data_length = sizeof(t->token);
Josh Gaof571fcb2018-02-05 18:49:10 -0800227 p->payload.assign(t->token, t->token + sizeof(t->token));
Josh Gao3bd28792016-10-05 19:02:29 -0700228 send_packet(p, t);
229}
230
Josh Gao184f5712017-07-26 11:06:55 -0700231void adbd_auth_verified(atransport* t) {
232 LOG(INFO) << "adb client authorized";
Josh Gao3bd28792016-10-05 19:02:29 -0700233 handle_online(t);
234 send_connect(t);
235}