| 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> | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 26 | #include <chrono> | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 27 | #include <iomanip> | 
|  | 28 | #include <map> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 29 | #include <memory> | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 30 | #include <thread> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 31 |  | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 32 | #include <adb/crypto/rsa_2048_key.h> | 
|  | 33 | #include <adb/tls/adb_ca_list.h> | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 34 | #include <adbd_auth.h> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 35 | #include <android-base/file.h> | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 36 | #include <android-base/no_destructor.h> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> | 
|  | 38 | #include <crypto_utils/android_pubkey.h> | 
| Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 39 | #include <openssl/obj_mac.h> | 
|  | 40 | #include <openssl/rsa.h> | 
|  | 41 | #include <openssl/sha.h> | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 42 | #include <openssl/ssl.h> | 
| Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 43 |  | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 44 | #include "adb.h" | 
|  | 45 | #include "adb_auth.h" | 
|  | 46 | #include "adb_io.h" | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 47 | #include "adb_wifi.h" | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 48 | #include "fdevent/fdevent.h" | 
|  | 49 | #include "transport.h" | 
|  | 50 | #include "types.h" | 
|  | 51 |  | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 52 | using namespace adb::crypto; | 
|  | 53 | using namespace adb::tls; | 
|  | 54 | using namespace std::chrono_literals; | 
|  | 55 |  | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 56 | static AdbdAuthContext* auth_ctx; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 57 |  | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 58 | static RSA* rsa_pkey = nullptr; | 
|  | 59 |  | 
| Michael Groover | 3857dea | 2018-12-28 20:35:37 -0800 | [diff] [blame] | 60 | static void adb_disconnected(void* unused, atransport* t); | 
|  | 61 | static struct adisconnect adb_disconnect = {adb_disconnected, nullptr}; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 62 |  | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 63 | static android::base::NoDestructor<std::map<uint32_t, weak_ptr<atransport>>> transports; | 
|  | 64 | static uint32_t transport_auth_id = 0; | 
|  | 65 |  | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 66 | bool auth_required = true; | 
|  | 67 |  | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 68 | static void* transport_to_callback_arg(atransport* transport) { | 
|  | 69 | uint32_t id = transport_auth_id++; | 
|  | 70 | (*transports)[id] = transport->weak(); | 
|  | 71 | return reinterpret_cast<void*>(id); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | static atransport* transport_from_callback_arg(void* id) { | 
|  | 75 | uint64_t id_u64 = reinterpret_cast<uint64_t>(id); | 
|  | 76 | if (id_u64 > std::numeric_limits<uint32_t>::max()) { | 
|  | 77 | LOG(FATAL) << "transport_from_callback_arg called on out of range value: " << id_u64; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | uint32_t id_u32 = static_cast<uint32_t>(id_u64); | 
|  | 81 | auto it = transports->find(id_u32); | 
|  | 82 | if (it == transports->end()) { | 
|  | 83 | LOG(ERROR) << "transport_from_callback_arg failed to find transport for id " << id_u32; | 
|  | 84 | return nullptr; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | atransport* t = it->second.get(); | 
|  | 88 | if (!t) { | 
|  | 89 | LOG(WARNING) << "transport_from_callback_arg found already destructed transport"; | 
|  | 90 | return nullptr; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | transports->erase(it); | 
|  | 94 | return t; | 
|  | 95 | } | 
|  | 96 |  | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 97 | static void IteratePublicKeys(std::function<bool(std::string_view public_key)> f) { | 
|  | 98 | adbd_auth_get_public_keys( | 
|  | 99 | auth_ctx, | 
| Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 100 | [](void* opaque, const char* public_key, size_t len) { | 
|  | 101 | return (*static_cast<decltype(f)*>(opaque))(std::string_view(public_key, len)); | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 102 | }, | 
|  | 103 | &f); | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 106 | bssl::UniquePtr<STACK_OF(X509_NAME)> adbd_tls_client_ca_list() { | 
|  | 107 | if (!auth_required) { | 
|  | 108 | return nullptr; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | bssl::UniquePtr<STACK_OF(X509_NAME)> ca_list(sk_X509_NAME_new_null()); | 
|  | 112 |  | 
|  | 113 | IteratePublicKeys([&](std::string_view public_key) { | 
|  | 114 | // TODO: do we really have to support both ' ' and '\t'? | 
|  | 115 | std::vector<std::string> split = android::base::Split(std::string(public_key), " \t"); | 
|  | 116 | uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1]; | 
|  | 117 | const std::string& pubkey = split[0]; | 
|  | 118 | if (b64_pton(pubkey.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) { | 
|  | 119 | LOG(ERROR) << "Invalid base64 key " << pubkey; | 
|  | 120 | return true; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | RSA* key = nullptr; | 
|  | 124 | if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) { | 
|  | 125 | LOG(ERROR) << "Failed to parse key " << pubkey; | 
|  | 126 | return true; | 
|  | 127 | } | 
|  | 128 | bssl::UniquePtr<RSA> rsa_key(key); | 
|  | 129 |  | 
|  | 130 | unsigned char* dkey = nullptr; | 
|  | 131 | int len = i2d_RSA_PUBKEY(rsa_key.get(), &dkey); | 
|  | 132 | if (len <= 0 || dkey == nullptr) { | 
|  | 133 | LOG(ERROR) << "Failed to encode RSA public key"; | 
|  | 134 | return true; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | uint8_t digest[SHA256_DIGEST_LENGTH]; | 
|  | 138 | // Put the encoded key in the commonName attribute of the issuer name. | 
|  | 139 | // Note that the commonName has a max length of 64 bytes, which is less | 
|  | 140 | // than the SHA256_DIGEST_LENGTH. | 
|  | 141 | SHA256(dkey, len, digest); | 
|  | 142 | OPENSSL_free(dkey); | 
|  | 143 |  | 
|  | 144 | auto digest_str = SHA256BitsToHexString( | 
|  | 145 | std::string_view(reinterpret_cast<const char*>(&digest[0]), sizeof(digest))); | 
|  | 146 | LOG(INFO) << "fingerprint=[" << digest_str << "]"; | 
|  | 147 | auto issuer = CreateCAIssuerFromEncodedKey(digest_str); | 
|  | 148 | CHECK(bssl::PushToStack(ca_list.get(), std::move(issuer))); | 
|  | 149 | return true; | 
|  | 150 | }); | 
|  | 151 |  | 
|  | 152 | return ca_list; | 
|  | 153 | } | 
|  | 154 |  | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 155 | bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig, | 
|  | 156 | std::string* auth_key) { | 
|  | 157 | bool authorized = false; | 
|  | 158 | auth_key->clear(); | 
| Michael Groover | 7eeda6b | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 159 |  | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 160 | IteratePublicKeys([&](std::string_view public_key) { | 
|  | 161 | // TODO: do we really have to support both ' ' and '\t'? | 
|  | 162 | std::vector<std::string> split = android::base::Split(std::string(public_key), " \t"); | 
|  | 163 | uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1]; | 
|  | 164 | const std::string& pubkey = split[0]; | 
|  | 165 | if (b64_pton(pubkey.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) { | 
|  | 166 | LOG(ERROR) << "Invalid base64 key " << pubkey; | 
|  | 167 | return true; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | RSA* key = nullptr; | 
|  | 171 | if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) { | 
|  | 172 | LOG(ERROR) << "Failed to parse key " << pubkey; | 
|  | 173 | return true; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | bool verified = | 
|  | 177 | (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size, | 
|  | 178 | reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(), key) == 1); | 
|  | 179 | RSA_free(key); | 
|  | 180 | if (verified) { | 
|  | 181 | *auth_key = public_key; | 
|  | 182 | authorized = true; | 
|  | 183 | return false; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | return true; | 
|  | 187 | }); | 
|  | 188 |  | 
|  | 189 | return authorized; | 
| Michael Groover | 7eeda6b | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 192 | static bool adbd_auth_generate_token(void* token, size_t token_size) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 193 | FILE* fp = fopen("/dev/urandom", "re"); | 
|  | 194 | if (!fp) return false; | 
|  | 195 | bool okay = (fread(token, token_size, 1, fp) == 1); | 
|  | 196 | fclose(fp); | 
|  | 197 | return okay; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 200 | void adbd_cloexec_auth_socket() { | 
|  | 201 | int fd = android_get_control_socket("adbd"); | 
|  | 202 | if (fd == -1) { | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 203 | PLOG(ERROR) << "Failed to get adbd socket"; | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 204 | return; | 
|  | 205 | } | 
| Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 206 | fcntl(fd, F_SETFD, FD_CLOEXEC); | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 207 | } | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 208 |  | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 209 | static void adbd_auth_key_authorized(void* arg, uint64_t id) { | 
|  | 210 | LOG(INFO) << "adb client authorized"; | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 211 | fdevent_run_on_main_thread([=]() { | 
|  | 212 | LOG(INFO) << "arg = " << reinterpret_cast<uintptr_t>(arg); | 
|  | 213 | auto* transport = transport_from_callback_arg(arg); | 
|  | 214 | if (!transport) { | 
|  | 215 | LOG(ERROR) << "authorization received for deleted transport, ignoring"; | 
|  | 216 | return; | 
|  | 217 | } | 
|  | 218 | transport->auth_id = id; | 
|  | 219 | adbd_auth_verified(transport); | 
|  | 220 | }); | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 223 | static void adbd_key_removed(const char* public_key, size_t len) { | 
|  | 224 | // The framework removed the key from its keystore. We need to disconnect all | 
|  | 225 | // devices using that key. Search by t->auth_key | 
|  | 226 | std::string_view auth_key(public_key, len); | 
|  | 227 | kick_all_transports_by_auth_key(auth_key); | 
|  | 228 | } | 
|  | 229 |  | 
| Pavel Labath | 64d9adc | 2015-03-17 11:03:36 -0700 | [diff] [blame] | 230 | void adbd_auth_init(void) { | 
| Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 231 | AdbdAuthCallbacksV1 cb; | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 232 | cb.version = 1; | 
| Joshua Duong | 51378f4 | 2020-02-18 18:29:25 -0800 | [diff] [blame] | 233 | cb.key_authorized = adbd_auth_key_authorized; | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 234 | cb.key_removed = adbd_key_removed; | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 235 | auth_ctx = adbd_auth_new(&cb); | 
| Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 236 | adbd_wifi_init(auth_ctx); | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 237 | std::thread([]() { | 
|  | 238 | adb_thread_setname("adbd auth"); | 
|  | 239 | adbd_auth_run(auth_ctx); | 
|  | 240 | LOG(FATAL) << "auth thread terminated"; | 
|  | 241 | }).detach(); | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 242 | } | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | void send_auth_request(atransport* t) { | 
|  | 245 | LOG(INFO) << "Calling send_auth_request..."; | 
|  | 246 |  | 
|  | 247 | if (!adbd_auth_generate_token(t->token, sizeof(t->token))) { | 
|  | 248 | PLOG(ERROR) << "Error generating token"; | 
|  | 249 | return; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | apacket* p = get_apacket(); | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 253 | p->msg.command = A_AUTH; | 
|  | 254 | p->msg.arg0 = ADB_AUTH_TOKEN; | 
|  | 255 | p->msg.data_length = sizeof(t->token); | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 256 | p->payload.assign(t->token, t->token + sizeof(t->token)); | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 257 | send_packet(p, t); | 
|  | 258 | } | 
|  | 259 |  | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 260 | void adbd_auth_verified(atransport* t) { | 
|  | 261 | LOG(INFO) << "adb client authorized"; | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 262 | handle_online(t); | 
|  | 263 | send_connect(t); | 
|  | 264 | } | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 265 |  | 
|  | 266 | static void adb_disconnected(void* unused, atransport* t) { | 
|  | 267 | LOG(INFO) << "ADB disconnect"; | 
|  | 268 | adbd_auth_notify_disconnect(auth_ctx, t->auth_id); | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | void adbd_auth_confirm_key(atransport* t) { | 
|  | 272 | LOG(INFO) << "prompting user to authorize key"; | 
|  | 273 | t->AddDisconnect(&adb_disconnect); | 
| Josh Gao | 607fd54 | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 274 | adbd_auth_prompt_user(auth_ctx, t->auth_key.data(), t->auth_key.size(), | 
|  | 275 | transport_to_callback_arg(t)); | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | void adbd_notify_framework_connected_key(atransport* t) { | 
| Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 279 | t->auth_id = adbd_auth_notify_auth(auth_ctx, t->auth_key.data(), t->auth_key.size()); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | int adbd_tls_verify_cert(X509_STORE_CTX* ctx, std::string* auth_key) { | 
|  | 283 | if (!auth_required) { | 
|  | 284 | // Any key will do. | 
|  | 285 | LOG(INFO) << __func__ << ": auth not required"; | 
|  | 286 | return 1; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | bool authorized = false; | 
|  | 290 | X509* cert = X509_STORE_CTX_get0_cert(ctx); | 
|  | 291 | if (cert == nullptr) { | 
|  | 292 | LOG(INFO) << "got null x509 certificate"; | 
|  | 293 | return 0; | 
|  | 294 | } | 
|  | 295 | bssl::UniquePtr<EVP_PKEY> evp_pkey(X509_get_pubkey(cert)); | 
|  | 296 | if (evp_pkey == nullptr) { | 
|  | 297 | LOG(INFO) << "got null evp_pkey from x509 certificate"; | 
|  | 298 | return 0; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | IteratePublicKeys([&](std::string_view public_key) { | 
|  | 302 | // TODO: do we really have to support both ' ' and '\t'? | 
|  | 303 | std::vector<std::string> split = android::base::Split(std::string(public_key), " \t"); | 
|  | 304 | uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1]; | 
|  | 305 | const std::string& pubkey = split[0]; | 
|  | 306 | if (b64_pton(pubkey.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) { | 
|  | 307 | LOG(ERROR) << "Invalid base64 key " << pubkey; | 
|  | 308 | return true; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | RSA* key = nullptr; | 
|  | 312 | if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) { | 
|  | 313 | LOG(ERROR) << "Failed to parse key " << pubkey; | 
|  | 314 | return true; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | bool verified = false; | 
|  | 318 | bssl::UniquePtr<EVP_PKEY> known_evp(EVP_PKEY_new()); | 
|  | 319 | EVP_PKEY_set1_RSA(known_evp.get(), key); | 
|  | 320 | if (EVP_PKEY_cmp(known_evp.get(), evp_pkey.get())) { | 
|  | 321 | LOG(INFO) << "Matched auth_key=" << public_key; | 
|  | 322 | verified = true; | 
|  | 323 | } else { | 
|  | 324 | LOG(INFO) << "auth_key doesn't match [" << public_key << "]"; | 
|  | 325 | } | 
|  | 326 | RSA_free(key); | 
|  | 327 | if (verified) { | 
|  | 328 | *auth_key = public_key; | 
|  | 329 | authorized = true; | 
|  | 330 | return false; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | return true; | 
|  | 334 | }); | 
|  | 335 |  | 
|  | 336 | return authorized ? 1 : 0; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | void adbd_auth_tls_handshake(atransport* t) { | 
|  | 340 | if (rsa_pkey == nullptr) { | 
|  | 341 | // Generate a random RSA key to feed into the X509 certificate | 
|  | 342 | auto rsa_2048 = CreateRSA2048Key(); | 
|  | 343 | CHECK(rsa_2048.has_value()); | 
|  | 344 | rsa_pkey = EVP_PKEY_get1_RSA(rsa_2048->GetEvpPkey()); | 
|  | 345 | CHECK(rsa_pkey); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | std::thread([t]() { | 
|  | 349 | std::string auth_key; | 
|  | 350 | if (t->connection()->DoTlsHandshake(rsa_pkey, &auth_key)) { | 
|  | 351 | LOG(INFO) << "auth_key=" << auth_key; | 
|  | 352 | if (t->IsTcpDevice()) { | 
|  | 353 | t->auth_key = auth_key; | 
|  | 354 | adbd_wifi_secure_connect(t); | 
|  | 355 | } else { | 
|  | 356 | adbd_auth_verified(t); | 
|  | 357 | adbd_notify_framework_connected_key(t); | 
|  | 358 | } | 
|  | 359 | } else { | 
|  | 360 | // Only allow one attempt at the handshake. | 
|  | 361 | t->Kick(); | 
|  | 362 | } | 
|  | 363 | }).detach(); | 
| Josh Gao | 2752326 | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 364 | } |