blob: 22ea9ff879d541db182c574d1503c9fb4b7a3d88 [file] [log] [blame]
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG AUTH
Dan Albert33134262015-03-19 15:21:08 -070018
Elliott Hughes0aeb5052016-06-29 17:42:01 -070019#include "sysdeps.h"
Dan Albert33134262015-03-19 15:21:08 -070020
Dan Albert76649012015-02-24 15:51:19 -080021#include <resolv.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070022#include <stdio.h>
23#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070024
Michael Groover7eeda6b2019-04-25 18:33:35 -070025#include <algorithm>
Josh Gao607fd542019-12-09 15:44:57 -080026#include <iomanip>
27#include <map>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070028#include <memory>
29
Josh Gao27523262019-10-22 12:30:39 -070030#include <adbd_auth.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070031#include <android-base/file.h>
Josh Gao607fd542019-12-09 15:44:57 -080032#include <android-base/no_destructor.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070033#include <android-base/strings.h>
34#include <crypto_utils/android_pubkey.h>
Mattias Nissler097b6bb2016-03-31 16:32:09 +020035#include <openssl/obj_mac.h>
36#include <openssl/rsa.h>
37#include <openssl/sha.h>
Joshua Duongd85f5c02019-11-20 14:18:43 -080038#include <openssl/ssl.h>
Mattias Nissler097b6bb2016-03-31 16:32:09 +020039
Josh Gao607fd542019-12-09 15:44:57 -080040#include "adb.h"
41#include "adb_auth.h"
42#include "adb_io.h"
Joshua Duongd85f5c02019-11-20 14:18:43 -080043#include "adb_wifi.h"
Josh Gao607fd542019-12-09 15:44:57 -080044#include "fdevent/fdevent.h"
45#include "transport.h"
46#include "types.h"
47
Josh Gao27523262019-10-22 12:30:39 -070048static AdbdAuthContext* auth_ctx;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070049
Michael Groover3857dea2018-12-28 20:35:37 -080050static void adb_disconnected(void* unused, atransport* t);
51static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070052
Josh Gao607fd542019-12-09 15:44:57 -080053static android::base::NoDestructor<std::map<uint32_t, weak_ptr<atransport>>> transports;
54static uint32_t transport_auth_id = 0;
55
Josh Gao3bd28792016-10-05 19:02:29 -070056bool auth_required = true;
57
Josh Gao607fd542019-12-09 15:44:57 -080058static 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
64static 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 Gao27523262019-10-22 12:30:39 -070087static void IteratePublicKeys(std::function<bool(std::string_view public_key)> f) {
88 adbd_auth_get_public_keys(
89 auth_ctx,
Joshua Duong51378f42020-02-18 18:29:25 -080090 [](void* opaque, const char* public_key, size_t len) {
91 return (*static_cast<decltype(f)*>(opaque))(std::string_view(public_key, len));
Josh Gao27523262019-10-22 12:30:39 -070092 },
93 &f);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070094}
95
Josh Gao27523262019-10-22 12:30:39 -070096bool 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 Groover7eeda6b2019-04-25 18:33:35 -0700100
Josh Gao27523262019-10-22 12:30:39 -0700101 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 Groover7eeda6b2019-04-25 18:33:35 -0700131}
132
Josh Gao3bd28792016-10-05 19:02:29 -0700133static bool adbd_auth_generate_token(void* token, size_t token_size) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700134 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 Gobyd5fcafa2012-04-12 12:23:49 -0700139}
140
Pavel Labath64d9adc2015-03-17 11:03:36 -0700141void adbd_cloexec_auth_socket() {
142 int fd = android_get_control_socket("adbd");
143 if (fd == -1) {
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700144 PLOG(ERROR) << "Failed to get adbd socket";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700145 return;
146 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700147 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath64d9adc2015-03-17 11:03:36 -0700148}
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700149
Josh Gao27523262019-10-22 12:30:39 -0700150static void adbd_auth_key_authorized(void* arg, uint64_t id) {
151 LOG(INFO) << "adb client authorized";
Josh Gao607fd542019-12-09 15:44:57 -0800152 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 Gao27523262019-10-22 12:30:39 -0700162}
163
Joshua Duongd85f5c02019-11-20 14:18:43 -0800164static 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 Labath64d9adc2015-03-17 11:03:36 -0700171void adbd_auth_init(void) {
Joshua Duong51378f42020-02-18 18:29:25 -0800172 AdbdAuthCallbacksV1 cb;
Josh Gao27523262019-10-22 12:30:39 -0700173 cb.version = 1;
Joshua Duong51378f42020-02-18 18:29:25 -0800174 cb.key_authorized = adbd_auth_key_authorized;
Joshua Duongd85f5c02019-11-20 14:18:43 -0800175 cb.key_removed = adbd_key_removed;
Josh Gao27523262019-10-22 12:30:39 -0700176 auth_ctx = adbd_auth_new(&cb);
Joshua Duongd85f5c02019-11-20 14:18:43 -0800177 adbd_wifi_init(auth_ctx);
Josh Gao27523262019-10-22 12:30:39 -0700178 std::thread([]() {
179 adb_thread_setname("adbd auth");
180 adbd_auth_run(auth_ctx);
181 LOG(FATAL) << "auth thread terminated";
182 }).detach();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700183}
Josh Gao3bd28792016-10-05 19:02:29 -0700184
185void 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 Gao3bd28792016-10-05 19:02:29 -0700194 p->msg.command = A_AUTH;
195 p->msg.arg0 = ADB_AUTH_TOKEN;
196 p->msg.data_length = sizeof(t->token);
Josh Gaof571fcb2018-02-05 18:49:10 -0800197 p->payload.assign(t->token, t->token + sizeof(t->token));
Josh Gao3bd28792016-10-05 19:02:29 -0700198 send_packet(p, t);
199}
200
Josh Gao184f5712017-07-26 11:06:55 -0700201void adbd_auth_verified(atransport* t) {
202 LOG(INFO) << "adb client authorized";
Josh Gao3bd28792016-10-05 19:02:29 -0700203 handle_online(t);
204 send_connect(t);
205}
Josh Gao27523262019-10-22 12:30:39 -0700206
207static void adb_disconnected(void* unused, atransport* t) {
208 LOG(INFO) << "ADB disconnect";
209 adbd_auth_notify_disconnect(auth_ctx, t->auth_id);
210}
211
212void adbd_auth_confirm_key(atransport* t) {
213 LOG(INFO) << "prompting user to authorize key";
214 t->AddDisconnect(&adb_disconnect);
Josh Gao607fd542019-12-09 15:44:57 -0800215 adbd_auth_prompt_user(auth_ctx, t->auth_key.data(), t->auth_key.size(),
216 transport_to_callback_arg(t));
Josh Gao27523262019-10-22 12:30:39 -0700217}
218
219void adbd_notify_framework_connected_key(atransport* t) {
220 adbd_auth_notify_auth(auth_ctx, t->auth_key.data(), t->auth_key.size());
221}