blob: 7a3a4f5c5d6eb9048f2eeecf37ea4fa7472f8f59 [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"
Michael Grooverc0469002018-12-28 20:35:37 -080021#include "adb_io.h"
Josh Gaob51193a2019-06-28 13:50:37 -070022#include "fdevent/fdevent.h"
Elliott Hughes801066a2016-06-29 17:42:01 -070023#include "sysdeps.h"
24#include "transport.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070025
Dan Albertb302d122015-02-24 15:51:19 -080026#include <resolv.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070027#include <stdio.h>
28#include <string.h>
Michael Groover02b74272019-04-25 18:33:35 -070029#include <iomanip>
Benoit Goby2cc19e42012-04-12 12:23:49 -070030
Michael Groover02b74272019-04-25 18:33:35 -070031#include <algorithm>
Elliott Hughes801066a2016-06-29 17:42:01 -070032#include <memory>
33
34#include <android-base/file.h>
35#include <android-base/strings.h>
36#include <crypto_utils/android_pubkey.h>
Mattias Nisslera947b492016-03-31 16:32:09 +020037#include <openssl/obj_mac.h>
38#include <openssl/rsa.h>
39#include <openssl/sha.h>
40
Josh Gao9528df22018-05-14 11:14:33 -070041static fdevent* listener_fde = nullptr;
42static fdevent* framework_fde = nullptr;
Michael Groover02b74272019-04-25 18:33:35 -070043static auto& framework_mutex = *new std::mutex();
44static int framework_fd GUARDED_BY(framework_mutex) = -1;
45static auto& connected_keys GUARDED_BY(framework_mutex) = *new std::vector<std::string>;
Benoit Goby2cc19e42012-04-12 12:23:49 -070046
Michael Grooverc0469002018-12-28 20:35:37 -080047static void adb_disconnected(void* unused, atransport* t);
48static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
49static atransport* adb_transport;
Benoit Gobyd592d6c2013-01-15 19:59:14 -080050static bool needs_retry = false;
Benoit Goby2cc19e42012-04-12 12:23:49 -070051
Josh Gaoeac20582016-10-05 19:02:29 -070052bool auth_required = true;
53
Michael Groover02b74272019-04-25 18:33:35 -070054bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig,
55 std::string* auth_key) {
Elliott Hughes801066a2016-06-29 17:42:01 -070056 static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
Benoit Goby2cc19e42012-04-12 12:23:49 -070057
Elliott Hughes801066a2016-06-29 17:42:01 -070058 for (const auto& path : key_paths) {
59 if (access(path, R_OK) == 0) {
60 LOG(INFO) << "Loading keys from " << path;
Elliott Hughes801066a2016-06-29 17:42:01 -070061 std::string content;
62 if (!android::base::ReadFileToString(path, &content)) {
63 PLOG(ERROR) << "Couldn't read " << path;
64 continue;
65 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070066
Elliott Hughes801066a2016-06-29 17:42:01 -070067 for (const auto& line : android::base::Split(content, "\n")) {
Michael Groover02b74272019-04-25 18:33:35 -070068 if (line.empty()) continue;
69 *auth_key = line;
Elliott Hughes801066a2016-06-29 17:42:01 -070070 // TODO: do we really have to support both ' ' and '\t'?
71 char* sep = strpbrk(const_cast<char*>(line.c_str()), " \t");
72 if (sep) *sep = '\0';
Benoit Goby2cc19e42012-04-12 12:23:49 -070073
Elliott Hughes801066a2016-06-29 17:42:01 -070074 // b64_pton requires one additional byte in the target buffer for
75 // decoding to succeed. See http://b/28035006 for details.
76 uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1];
Josh Gao0560feb2019-01-22 19:36:15 -080077 if (b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) {
Elliott Hughes801066a2016-06-29 17:42:01 -070078 LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path;
79 continue;
80 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070081
Elliott Hughes801066a2016-06-29 17:42:01 -070082 RSA* key = nullptr;
83 if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
84 LOG(ERROR) << "Failed to parse key " << line.c_str() << " in " << path;
85 continue;
86 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070087
Josh Gao67ac3792016-10-06 13:31:44 -070088 bool verified =
89 (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
Josh Gao839b9322018-02-05 18:49:10 -080090 reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(),
91 key) == 1);
Elliott Hughes801066a2016-06-29 17:42:01 -070092 RSA_free(key);
93 if (verified) return true;
94 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070095 }
96 }
Michael Groover02b74272019-04-25 18:33:35 -070097 auth_key->clear();
Elliott Hughes801066a2016-06-29 17:42:01 -070098 return false;
Benoit Goby2cc19e42012-04-12 12:23:49 -070099}
100
Michael Groover02b74272019-04-25 18:33:35 -0700101static bool adbd_send_key_message_locked(std::string_view msg_type, std::string_view key)
102 REQUIRES(framework_mutex) {
103 if (framework_fd < 0) {
104 LOG(ERROR) << "Client not connected to send msg_type " << msg_type;
105 return false;
106 }
107 std::string msg = std::string(msg_type) + std::string(key);
108 int msg_len = msg.length();
109 if (msg_len >= static_cast<int>(MAX_FRAMEWORK_PAYLOAD)) {
110 LOG(ERROR) << "Key too long (" << msg_len << ")";
111 return false;
112 }
113
114 LOG(DEBUG) << "Sending '" << msg << "'";
115 if (!WriteFdExactly(framework_fd, msg.c_str(), msg_len)) {
116 PLOG(ERROR) << "Failed to write " << msg_type;
117 return false;
118 }
119 return true;
120}
121
Josh Gaoeac20582016-10-05 19:02:29 -0700122static bool adbd_auth_generate_token(void* token, size_t token_size) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700123 FILE* fp = fopen("/dev/urandom", "re");
124 if (!fp) return false;
125 bool okay = (fread(token, token_size, 1, fp) == 1);
126 fclose(fp);
127 return okay;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700128}
129
Michael Grooverc0469002018-12-28 20:35:37 -0800130static void adb_disconnected(void* unused, atransport* t) {
131 LOG(INFO) << "ADB disconnect";
132 adb_transport = nullptr;
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800133 needs_retry = false;
Michael Groover02b74272019-04-25 18:33:35 -0700134 {
135 std::lock_guard<std::mutex> lock(framework_mutex);
136 if (framework_fd >= 0) {
137 adbd_send_key_message_locked("DC", t->auth_key);
Michael Grooverc0469002018-12-28 20:35:37 -0800138 }
Michael Groover02b74272019-04-25 18:33:35 -0700139 connected_keys.erase(std::remove(connected_keys.begin(), connected_keys.end(), t->auth_key),
140 connected_keys.end());
Michael Grooverc0469002018-12-28 20:35:37 -0800141 }
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800142}
143
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800144static void framework_disconnected() {
Elliott Hughes801066a2016-06-29 17:42:01 -0700145 LOG(INFO) << "Framework disconnect";
Josh Gao9528df22018-05-14 11:14:33 -0700146 if (framework_fde) {
147 fdevent_destroy(framework_fde);
Michael Groover02b74272019-04-25 18:33:35 -0700148 {
149 std::lock_guard<std::mutex> lock(framework_mutex);
150 framework_fd = -1;
151 }
Josh Gao9528df22018-05-14 11:14:33 -0700152 }
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800153}
154
Josh Gaoeac20582016-10-05 19:02:29 -0700155static void adbd_auth_event(int fd, unsigned events, void*) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700156 if (events & FDE_READ) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700157 char response[2];
158 int ret = unix_read(fd, response, sizeof(response));
Vince Harron82125422014-09-25 21:51:15 -0700159 if (ret <= 0) {
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800160 framework_disconnected();
161 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
Michael Grooverc0469002018-12-28 20:35:37 -0800162 if (adb_transport) {
163 adbd_auth_verified(adb_transport);
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800164 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700165 }
166 }
167}
168
Michael Groover02b74272019-04-25 18:33:35 -0700169void adbd_auth_confirm_key(atransport* t) {
Michael Grooverc0469002018-12-28 20:35:37 -0800170 if (!adb_transport) {
171 adb_transport = t;
172 t->AddDisconnect(&adb_disconnect);
Benoit Gobyc0028882013-04-01 17:39:06 -0700173 }
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800174
Michael Groover02b74272019-04-25 18:33:35 -0700175 {
176 std::lock_guard<std::mutex> lock(framework_mutex);
177 if (framework_fd < 0) {
178 LOG(ERROR) << "Client not connected";
179 needs_retry = true;
180 return;
181 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700182
Michael Groover02b74272019-04-25 18:33:35 -0700183 adbd_send_key_message_locked("PK", t->auth_key);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700184 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700185}
186
Josh Gaoeac20582016-10-05 19:02:29 -0700187static void adbd_auth_listener(int fd, unsigned events, void* data) {
Josh Gao782d8d42016-08-23 15:41:56 -0700188 int s = adb_socket_accept(fd, nullptr, nullptr);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700189 if (s < 0) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700190 PLOG(ERROR) << "Failed to accept";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700191 return;
192 }
193
Michael Groover02b74272019-04-25 18:33:35 -0700194 {
195 std::lock_guard<std::mutex> lock(framework_mutex);
196 if (framework_fd >= 0) {
197 LOG(WARNING) << "adb received framework auth socket connection again";
198 framework_disconnected();
199 }
200
201 framework_fd = s;
202 framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
203 fdevent_add(framework_fde, FDE_READ);
204
205 if (needs_retry) {
206 needs_retry = false;
207 send_auth_request(adb_transport);
208 }
209
210 // if a client connected before the framework was available notify the framework of the
211 // connected key now.
212 if (!connected_keys.empty()) {
213 for (const auto& key : connected_keys) {
214 adbd_send_key_message_locked("CK", key);
215 }
216 }
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800217 }
Michael Groover02b74272019-04-25 18:33:35 -0700218}
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800219
Michael Groover02b74272019-04-25 18:33:35 -0700220void adbd_notify_framework_connected_key(atransport* t) {
221 if (!adb_transport) {
222 adb_transport = t;
223 t->AddDisconnect(&adb_disconnect);
224 }
225 {
226 std::lock_guard<std::mutex> lock(framework_mutex);
227 if (std::find(connected_keys.begin(), connected_keys.end(), t->auth_key) ==
228 connected_keys.end()) {
229 connected_keys.push_back(t->auth_key);
230 }
231 if (framework_fd >= 0) {
232 adbd_send_key_message_locked("CK", t->auth_key);
233 }
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800234 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700235}
236
Pavel Labath0bdb8672015-03-17 11:03:36 -0700237void adbd_cloexec_auth_socket() {
238 int fd = android_get_control_socket("adbd");
239 if (fd == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700240 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700241 return;
242 }
Nick Kralevich777523e2014-07-18 20:57:35 -0700243 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath0bdb8672015-03-17 11:03:36 -0700244}
Benoit Goby2cc19e42012-04-12 12:23:49 -0700245
Pavel Labath0bdb8672015-03-17 11:03:36 -0700246void adbd_auth_init(void) {
247 int fd = android_get_control_socket("adbd");
248 if (fd == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700249 PLOG(ERROR) << "Failed to get adbd socket";
Pavel Labath0bdb8672015-03-17 11:03:36 -0700250 return;
251 }
252
253 if (listen(fd, 4) == -1) {
Elliott Hughes801066a2016-06-29 17:42:01 -0700254 PLOG(ERROR) << "Failed to listen on '" << fd << "'";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700255 return;
256 }
257
Yi Kong86e67182018-07-13 18:15:16 -0700258 listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr);
Josh Gao9528df22018-05-14 11:14:33 -0700259 fdevent_add(listener_fde, FDE_READ);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700260}
Josh Gaoeac20582016-10-05 19:02:29 -0700261
262void send_auth_request(atransport* t) {
263 LOG(INFO) << "Calling send_auth_request...";
264
265 if (!adbd_auth_generate_token(t->token, sizeof(t->token))) {
266 PLOG(ERROR) << "Error generating token";
267 return;
268 }
269
270 apacket* p = get_apacket();
Josh Gaoeac20582016-10-05 19:02:29 -0700271 p->msg.command = A_AUTH;
272 p->msg.arg0 = ADB_AUTH_TOKEN;
273 p->msg.data_length = sizeof(t->token);
Josh Gao839b9322018-02-05 18:49:10 -0800274 p->payload.assign(t->token, t->token + sizeof(t->token));
Josh Gaoeac20582016-10-05 19:02:29 -0700275 send_packet(p, t);
276}
277
Josh Gao03eba902017-07-26 11:06:55 -0700278void adbd_auth_verified(atransport* t) {
279 LOG(INFO) << "adb client authorized";
Josh Gaoeac20582016-10-05 19:02:29 -0700280 handle_online(t);
281 send_connect(t);
282}