Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG AUTH |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 19 | #include "adb.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "adb_auth.h" |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 21 | #include "adb_io.h" |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 22 | #include "fdevent.h" |
| 23 | #include "sysdeps.h" |
| 24 | #include "transport.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 25 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <resolv.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 30 | #include <memory> |
| 31 | |
| 32 | #include <android-base/file.h> |
| 33 | #include <android-base/strings.h> |
| 34 | #include <crypto_utils/android_pubkey.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 35 | #include <openssl/obj_mac.h> |
| 36 | #include <openssl/rsa.h> |
| 37 | #include <openssl/sha.h> |
| 38 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 39 | static fdevent* listener_fde = nullptr; |
| 40 | static fdevent* framework_fde = nullptr; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 41 | static int framework_fd = -1; |
| 42 | |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 43 | static void adb_disconnected(void* unused, atransport* t); |
| 44 | static struct adisconnect adb_disconnect = {adb_disconnected, nullptr}; |
| 45 | static atransport* adb_transport; |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 46 | static bool needs_retry = false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 47 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 48 | bool auth_required = true; |
| 49 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 50 | bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 51 | static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr }; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 53 | for (const auto& path : key_paths) { |
| 54 | if (access(path, R_OK) == 0) { |
| 55 | LOG(INFO) << "Loading keys from " << path; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 57 | std::string content; |
| 58 | if (!android::base::ReadFileToString(path, &content)) { |
| 59 | PLOG(ERROR) << "Couldn't read " << path; |
| 60 | continue; |
| 61 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 62 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 63 | 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 Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 68 | // 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 Gao | 776c2ec | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 71 | if (b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path; |
| 73 | continue; |
| 74 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 76 | 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 Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 81 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 82 | bool verified = |
| 83 | (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size, |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 84 | reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(), |
| 85 | key) == 1); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 86 | RSA_free(key); |
| 87 | if (verified) return true; |
| 88 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 91 | return false; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 94 | static bool adbd_auth_generate_token(void* token, size_t token_size) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 95 | 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 Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 102 | static void adb_disconnected(void* unused, atransport* t) { |
| 103 | LOG(INFO) << "ADB disconnect"; |
| 104 | adb_transport = nullptr; |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 105 | needs_retry = false; |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 106 | 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 Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 115 | static void framework_disconnected() { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 116 | LOG(INFO) << "Framework disconnect"; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 117 | if (framework_fde) { |
| 118 | fdevent_destroy(framework_fde); |
| 119 | framework_fd = -1; |
| 120 | } |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 123 | static void adbd_auth_event(int fd, unsigned events, void*) { |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 124 | if (events & FDE_READ) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 125 | char response[2]; |
| 126 | int ret = unix_read(fd, response, sizeof(response)); |
Vince Harron | af436b1 | 2014-09-25 21:51:15 -0700 | [diff] [blame] | 127 | if (ret <= 0) { |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 128 | framework_disconnected(); |
| 129 | } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') { |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 130 | if (adb_transport) { |
| 131 | adbd_auth_verified(adb_transport); |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 132 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 137 | void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) { |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 138 | if (!adb_transport) { |
| 139 | adb_transport = t; |
| 140 | t->AddDisconnect(&adb_disconnect); |
Benoit Goby | b66356c | 2013-04-01 17:39:06 -0700 | [diff] [blame] | 141 | } |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 142 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 143 | if (framework_fd < 0) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 144 | LOG(ERROR) << "Client not connected"; |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 145 | needs_retry = true; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (key[len - 1] != '\0') { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 150 | LOG(ERROR) << "Key must be a null-terminated string"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 154 | 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 Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 160 | LOG(DEBUG) << "Sending '" << msg << "'"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 161 | |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 162 | if (!WriteFdExactly(framework_fd, msg, msg_len)) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 163 | PLOG(ERROR) << "Failed to write PK"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 164 | return; |
| 165 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 168 | static void adbd_auth_listener(int fd, unsigned events, void* data) { |
Josh Gao | 78e1eb1 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 169 | int s = adb_socket_accept(fd, nullptr, nullptr); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 170 | if (s < 0) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 171 | PLOG(ERROR) << "Failed to accept"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 175 | if (framework_fd >= 0) { |
Josh Gao | 443a52c | 2016-02-25 14:11:32 -0800 | [diff] [blame] | 176 | LOG(WARNING) << "adb received framework auth socket connection again"; |
Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 177 | framework_disconnected(); |
| 178 | } |
| 179 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 180 | framework_fd = s; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 181 | framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr); |
| 182 | fdevent_add(framework_fde, FDE_READ); |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 183 | |
| 184 | if (needs_retry) { |
| 185 | needs_retry = false; |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 186 | send_auth_request(adb_transport); |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 187 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 190 | void adbd_cloexec_auth_socket() { |
| 191 | int fd = android_get_control_socket("adbd"); |
| 192 | if (fd == -1) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 193 | PLOG(ERROR) << "Failed to get adbd socket"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 194 | return; |
| 195 | } |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 196 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 197 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 198 | |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 199 | void adbd_auth_init(void) { |
| 200 | int fd = android_get_control_socket("adbd"); |
| 201 | if (fd == -1) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 202 | PLOG(ERROR) << "Failed to get adbd socket"; |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (listen(fd, 4) == -1) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 207 | PLOG(ERROR) << "Failed to listen on '" << fd << "'"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 211 | listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 212 | fdevent_add(listener_fde, FDE_READ); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 213 | } |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 214 | |
| 215 | void 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 Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 224 | p->msg.command = A_AUTH; |
| 225 | p->msg.arg0 = ADB_AUTH_TOKEN; |
| 226 | p->msg.data_length = sizeof(t->token); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 227 | p->payload.assign(t->token, t->token + sizeof(t->token)); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 228 | send_packet(p, t); |
| 229 | } |
| 230 | |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 231 | void adbd_auth_verified(atransport* t) { |
| 232 | LOG(INFO) << "adb client authorized"; |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 233 | handle_online(t); |
| 234 | send_connect(t); |
| 235 | } |