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