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 "sysdeps.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <resolv.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 24 | |
Michael Groover | 7eeda6b | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 26 | #include <iomanip> |
| 27 | #include <map> |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 28 | #include <memory> |
| 29 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 30 | #include <adbd_auth.h> |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 32 | #include <android-base/no_destructor.h> |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 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> |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 38 | #include <openssl/ssl.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 39 | |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 40 | #include "adb.h" |
| 41 | #include "adb_auth.h" |
| 42 | #include "adb_io.h" |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 43 | #include "adb_wifi.h" |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 44 | #include "fdevent/fdevent.h" |
| 45 | #include "transport.h" |
| 46 | #include "types.h" |
| 47 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 48 | static AdbdAuthContext* auth_ctx; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 49 | |
Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 50 | static void adb_disconnected(void* unused, atransport* t); |
| 51 | static struct adisconnect adb_disconnect = {adb_disconnected, nullptr}; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 52 | |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 53 | static android::base::NoDestructor<std::map<uint32_t, weak_ptr<atransport>>> transports; |
| 54 | static uint32_t transport_auth_id = 0; |
| 55 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 56 | bool auth_required = true; |
| 57 | |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 58 | static void* transport_to_callback_arg(atransport* transport) { |
| 59 | uint32_t id = transport_auth_id++; |
| 60 | (*transports)[id] = transport->weak(); |
| 61 | return reinterpret_cast<void*>(id); |
| 62 | } |
| 63 | |
| 64 | static atransport* transport_from_callback_arg(void* id) { |
| 65 | uint64_t id_u64 = reinterpret_cast<uint64_t>(id); |
| 66 | if (id_u64 > std::numeric_limits<uint32_t>::max()) { |
| 67 | LOG(FATAL) << "transport_from_callback_arg called on out of range value: " << id_u64; |
| 68 | } |
| 69 | |
| 70 | uint32_t id_u32 = static_cast<uint32_t>(id_u64); |
| 71 | auto it = transports->find(id_u32); |
| 72 | if (it == transports->end()) { |
| 73 | LOG(ERROR) << "transport_from_callback_arg failed to find transport for id " << id_u32; |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | atransport* t = it->second.get(); |
| 78 | if (!t) { |
| 79 | LOG(WARNING) << "transport_from_callback_arg found already destructed transport"; |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | transports->erase(it); |
| 84 | return t; |
| 85 | } |
| 86 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 87 | static void IteratePublicKeys(std::function<bool(std::string_view public_key)> f) { |
| 88 | adbd_auth_get_public_keys( |
| 89 | auth_ctx, |
Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 90 | [](void* opaque, const char* public_key, size_t len) { |
| 91 | return (*static_cast<decltype(f)*>(opaque))(std::string_view(public_key, len)); |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 92 | }, |
| 93 | &f); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 96 | bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig, |
| 97 | std::string* auth_key) { |
| 98 | bool authorized = false; |
| 99 | auth_key->clear(); |
Michael Groover | 7eeda6b | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 100 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 101 | IteratePublicKeys([&](std::string_view public_key) { |
| 102 | // TODO: do we really have to support both ' ' and '\t'? |
| 103 | std::vector<std::string> split = android::base::Split(std::string(public_key), " \t"); |
| 104 | uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1]; |
| 105 | const std::string& pubkey = split[0]; |
| 106 | if (b64_pton(pubkey.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) { |
| 107 | LOG(ERROR) << "Invalid base64 key " << pubkey; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | RSA* key = nullptr; |
| 112 | if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) { |
| 113 | LOG(ERROR) << "Failed to parse key " << pubkey; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool verified = |
| 118 | (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size, |
| 119 | reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(), key) == 1); |
| 120 | RSA_free(key); |
| 121 | if (verified) { |
| 122 | *auth_key = public_key; |
| 123 | authorized = true; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | }); |
| 129 | |
| 130 | return authorized; |
Michael Groover | 7eeda6b | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 133 | static bool adbd_auth_generate_token(void* token, size_t token_size) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 134 | FILE* fp = fopen("/dev/urandom", "re"); |
| 135 | if (!fp) return false; |
| 136 | bool okay = (fread(token, token_size, 1, fp) == 1); |
| 137 | fclose(fp); |
| 138 | return okay; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 141 | void adbd_cloexec_auth_socket() { |
| 142 | int fd = android_get_control_socket("adbd"); |
| 143 | if (fd == -1) { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 144 | PLOG(ERROR) << "Failed to get adbd socket"; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 145 | return; |
| 146 | } |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 147 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 148 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 149 | |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 150 | static void adbd_auth_key_authorized(void* arg, uint64_t id) { |
| 151 | LOG(INFO) << "adb client authorized"; |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 152 | fdevent_run_on_main_thread([=]() { |
| 153 | LOG(INFO) << "arg = " << reinterpret_cast<uintptr_t>(arg); |
| 154 | auto* transport = transport_from_callback_arg(arg); |
| 155 | if (!transport) { |
| 156 | LOG(ERROR) << "authorization received for deleted transport, ignoring"; |
| 157 | return; |
| 158 | } |
| 159 | transport->auth_id = id; |
| 160 | adbd_auth_verified(transport); |
| 161 | }); |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 164 | static void adbd_key_removed(const char* public_key, size_t len) { |
| 165 | // The framework removed the key from its keystore. We need to disconnect all |
| 166 | // devices using that key. Search by t->auth_key |
| 167 | std::string_view auth_key(public_key, len); |
| 168 | kick_all_transports_by_auth_key(auth_key); |
| 169 | } |
| 170 | |
Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 171 | void adbd_auth_init(void) { |
Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 172 | AdbdAuthCallbacksV1 cb; |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 173 | cb.version = 1; |
Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 174 | cb.key_authorized = adbd_auth_key_authorized; |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 175 | cb.key_removed = adbd_key_removed; |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 176 | auth_ctx = adbd_auth_new(&cb); |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 177 | adbd_wifi_init(auth_ctx); |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 178 | std::thread([]() { |
| 179 | adb_thread_setname("adbd auth"); |
| 180 | adbd_auth_run(auth_ctx); |
| 181 | LOG(FATAL) << "auth thread terminated"; |
| 182 | }).detach(); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 183 | } |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 184 | |
| 185 | void send_auth_request(atransport* t) { |
| 186 | LOG(INFO) << "Calling send_auth_request..."; |
| 187 | |
| 188 | if (!adbd_auth_generate_token(t->token, sizeof(t->token))) { |
| 189 | PLOG(ERROR) << "Error generating token"; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | apacket* p = get_apacket(); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 194 | p->msg.command = A_AUTH; |
| 195 | p->msg.arg0 = ADB_AUTH_TOKEN; |
| 196 | p->msg.data_length = sizeof(t->token); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 197 | p->payload.assign(t->token, t->token + sizeof(t->token)); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 198 | send_packet(p, t); |
| 199 | } |
| 200 | |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 201 | void adbd_auth_verified(atransport* t) { |
| 202 | LOG(INFO) << "adb client authorized"; |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 203 | handle_online(t); |
| 204 | send_connect(t); |
| 205 | } |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 206 | |
| 207 | static void adb_disconnected(void* unused, atransport* t) { |
| 208 | LOG(INFO) << "ADB disconnect"; |
| 209 | adbd_auth_notify_disconnect(auth_ctx, t->auth_id); |
| 210 | } |
| 211 | |
| 212 | void adbd_auth_confirm_key(atransport* t) { |
| 213 | LOG(INFO) << "prompting user to authorize key"; |
| 214 | t->AddDisconnect(&adb_disconnect); |
Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 215 | adbd_auth_prompt_user(auth_ctx, t->auth_key.data(), t->auth_key.size(), |
| 216 | transport_to_callback_arg(t)); |
Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void adbd_notify_framework_connected_key(atransport* t) { |
| 220 | adbd_auth_notify_auth(auth_ctx, t->auth_key.data(), t->auth_key.size()); |
| 221 | } |