| 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" | 
| Josh Gao | 2d03ad4 | 2019-07-08 14:36:02 -0700 | [diff] [blame] | 21 | #include "adb_io.h" | 
| Josh Gao | 57e09b1 | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 22 | #include "fdevent/fdevent.h" | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 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 |  | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 43 | static void usb_disconnected(void* unused, atransport* t); | 
| Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 44 | static struct adisconnect usb_disconnect = { usb_disconnected, nullptr}; | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 45 | static atransport* usb_transport; | 
 | 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 |  | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 102 | static void usb_disconnected(void* unused, atransport* t) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 103 |     LOG(INFO) << "USB disconnect"; | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 104 |     usb_transport = nullptr; | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 105 |     needs_retry = false; | 
 | 106 | } | 
 | 107 |  | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 108 | static void framework_disconnected() { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 109 |     LOG(INFO) << "Framework disconnect"; | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 110 |     if (framework_fde) { | 
 | 111 |         fdevent_destroy(framework_fde); | 
 | 112 |         framework_fd = -1; | 
 | 113 |     } | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 114 | } | 
 | 115 |  | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 116 | static void adbd_auth_event(int fd, unsigned events, void*) { | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 117 |     if (events & FDE_READ) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 118 |         char response[2]; | 
 | 119 |         int ret = unix_read(fd, response, sizeof(response)); | 
| Vince Harron | af436b1 | 2014-09-25 21:51:15 -0700 | [diff] [blame] | 120 |         if (ret <= 0) { | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 121 |             framework_disconnected(); | 
 | 122 |         } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') { | 
 | 123 |             if (usb_transport) { | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 124 |                 adbd_auth_verified(usb_transport); | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 125 |             } | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 126 |         } | 
 | 127 |     } | 
 | 128 | } | 
 | 129 |  | 
| Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 130 | 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] | 131 |     if (!usb_transport) { | 
 | 132 |         usb_transport = t; | 
| Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 133 |         t->AddDisconnect(&usb_disconnect); | 
| Benoit Goby | b66356c | 2013-04-01 17:39:06 -0700 | [diff] [blame] | 134 |     } | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 135 |  | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 136 |     if (framework_fd < 0) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 137 |         LOG(ERROR) << "Client not connected"; | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 138 |         needs_retry = true; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 139 |         return; | 
 | 140 |     } | 
 | 141 |  | 
 | 142 |     if (key[len - 1] != '\0') { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 143 |         LOG(ERROR) << "Key must be a null-terminated string"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 144 |         return; | 
 | 145 |     } | 
 | 146 |  | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 147 |     char msg[MAX_PAYLOAD_V1]; | 
 | 148 |     int msg_len = snprintf(msg, sizeof(msg), "PK%s", key); | 
 | 149 |     if (msg_len >= static_cast<int>(sizeof(msg))) { | 
 | 150 |         LOG(ERROR) << "Key too long (" << msg_len << ")"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 151 |         return; | 
 | 152 |     } | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 153 |     LOG(DEBUG) << "Sending '" << msg << "'"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 154 |  | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 155 |     if (unix_write(framework_fd, msg, msg_len) == -1) { | 
 | 156 |         PLOG(ERROR) << "Failed to write PK"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 157 |         return; | 
 | 158 |     } | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 159 | } | 
 | 160 |  | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 161 | static void adbd_auth_listener(int fd, unsigned events, void* data) { | 
| Josh Gao | 78e1eb1 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 162 |     int s = adb_socket_accept(fd, nullptr, nullptr); | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 163 |     if (s < 0) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 164 |         PLOG(ERROR) << "Failed to accept"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 165 |         return; | 
 | 166 |     } | 
 | 167 |  | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 168 |     if (framework_fd >= 0) { | 
| Josh Gao | 443a52c | 2016-02-25 14:11:32 -0800 | [diff] [blame] | 169 |         LOG(WARNING) << "adb received framework auth socket connection again"; | 
| Josh Gao | 9f48611 | 2016-02-23 18:05:57 -0800 | [diff] [blame] | 170 |         framework_disconnected(); | 
 | 171 |     } | 
 | 172 |  | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 173 |     framework_fd = s; | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 174 |     framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr); | 
 | 175 |     fdevent_add(framework_fde, FDE_READ); | 
| Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 176 |  | 
 | 177 |     if (needs_retry) { | 
 | 178 |         needs_retry = false; | 
 | 179 |         send_auth_request(usb_transport); | 
 | 180 |     } | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 181 | } | 
 | 182 |  | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 183 | void adbd_cloexec_auth_socket() { | 
 | 184 |     int fd = android_get_control_socket("adbd"); | 
 | 185 |     if (fd == -1) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 186 |         PLOG(ERROR) << "Failed to get adbd socket"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 187 |         return; | 
 | 188 |     } | 
| Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 189 |     fcntl(fd, F_SETFD, FD_CLOEXEC); | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 190 | } | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 191 |  | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 192 | void adbd_auth_init(void) { | 
 | 193 |     int fd = android_get_control_socket("adbd"); | 
 | 194 |     if (fd == -1) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 195 |         PLOG(ERROR) << "Failed to get adbd socket"; | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 196 |         return; | 
 | 197 |     } | 
 | 198 |  | 
 | 199 |     if (listen(fd, 4) == -1) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 200 |         PLOG(ERROR) << "Failed to listen on '" << fd << "'"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 201 |         return; | 
 | 202 |     } | 
 | 203 |  | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 204 |     listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr); | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 205 |     fdevent_add(listener_fde, FDE_READ); | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 206 | } | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 207 |  | 
 | 208 | void send_auth_request(atransport* t) { | 
 | 209 |     LOG(INFO) << "Calling send_auth_request..."; | 
 | 210 |  | 
 | 211 |     if (!adbd_auth_generate_token(t->token, sizeof(t->token))) { | 
 | 212 |         PLOG(ERROR) << "Error generating token"; | 
 | 213 |         return; | 
 | 214 |     } | 
 | 215 |  | 
 | 216 |     apacket* p = get_apacket(); | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 217 |     p->msg.command = A_AUTH; | 
 | 218 |     p->msg.arg0 = ADB_AUTH_TOKEN; | 
 | 219 |     p->msg.data_length = sizeof(t->token); | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 220 |     p->payload.assign(t->token, t->token + sizeof(t->token)); | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 221 |     send_packet(p, t); | 
 | 222 | } | 
 | 223 |  | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 224 | void adbd_auth_verified(atransport* t) { | 
 | 225 |     LOG(INFO) << "adb client authorized"; | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 226 |     handle_online(t); | 
 | 227 |     send_connect(t); | 
 | 228 | } |