adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG AUTH
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2016-06-30 02:42:01 +02:00
|
|
|
#include "adb.h"
|
2015-03-19 23:21:08 +01:00
|
|
|
#include "adb_auth.h"
|
2018-12-29 05:35:37 +01:00
|
|
|
#include "adb_io.h"
|
2019-06-28 22:50:37 +02:00
|
|
|
#include "fdevent/fdevent.h"
|
2016-06-30 02:42:01 +02:00
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "transport.h"
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <resolv.h>
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2019-04-26 03:33:35 +02:00
|
|
|
#include <iomanip>
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
#include <algorithm>
|
2016-06-30 02:42:01 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <crypto_utils/android_pubkey.h>
|
2016-03-31 16:32:09 +02:00
|
|
|
#include <openssl/obj_mac.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
static fdevent* listener_fde = nullptr;
|
|
|
|
static fdevent* framework_fde = nullptr;
|
2019-04-26 03:33:35 +02:00
|
|
|
static auto& framework_mutex = *new std::mutex();
|
|
|
|
static int framework_fd GUARDED_BY(framework_mutex) = -1;
|
|
|
|
static auto& connected_keys GUARDED_BY(framework_mutex) = *new std::vector<std::string>;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2018-12-29 05:35:37 +01:00
|
|
|
static void adb_disconnected(void* unused, atransport* t);
|
|
|
|
static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
|
|
|
|
static atransport* adb_transport;
|
2013-01-16 04:59:14 +01:00
|
|
|
static bool needs_retry = false;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2016-10-06 04:02:29 +02:00
|
|
|
bool auth_required = true;
|
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig,
|
|
|
|
std::string* auth_key) {
|
2016-06-30 02:42:01 +02:00
|
|
|
static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2016-06-30 02:42:01 +02:00
|
|
|
for (const auto& path : key_paths) {
|
|
|
|
if (access(path, R_OK) == 0) {
|
|
|
|
LOG(INFO) << "Loading keys from " << path;
|
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(path, &content)) {
|
|
|
|
PLOG(ERROR) << "Couldn't read " << path;
|
|
|
|
continue;
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2016-06-30 02:42:01 +02:00
|
|
|
for (const auto& line : android::base::Split(content, "\n")) {
|
2019-04-26 03:33:35 +02:00
|
|
|
if (line.empty()) continue;
|
|
|
|
*auth_key = line;
|
2016-06-30 02:42:01 +02:00
|
|
|
// TODO: do we really have to support both ' ' and '\t'?
|
|
|
|
char* sep = strpbrk(const_cast<char*>(line.c_str()), " \t");
|
|
|
|
if (sep) *sep = '\0';
|
|
|
|
|
|
|
|
// b64_pton requires one additional byte in the target buffer for
|
|
|
|
// decoding to succeed. See http://b/28035006 for details.
|
|
|
|
uint8_t keybuf[ANDROID_PUBKEY_ENCODED_SIZE + 1];
|
2019-01-23 04:36:15 +01:00
|
|
|
if (b64_pton(line.c_str(), keybuf, sizeof(keybuf)) != ANDROID_PUBKEY_ENCODED_SIZE) {
|
2016-06-30 02:42:01 +02:00
|
|
|
LOG(ERROR) << "Invalid base64 key " << line.c_str() << " in " << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RSA* key = nullptr;
|
|
|
|
if (!android_pubkey_decode(keybuf, ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
|
|
|
|
LOG(ERROR) << "Failed to parse key " << line.c_str() << " in " << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:31:44 +02:00
|
|
|
bool verified =
|
|
|
|
(RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
|
2018-02-06 03:49:10 +01:00
|
|
|
reinterpret_cast<const uint8_t*>(sig.c_str()), sig.size(),
|
|
|
|
key) == 1);
|
2016-06-30 02:42:01 +02:00
|
|
|
RSA_free(key);
|
|
|
|
if (verified) return true;
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 03:33:35 +02:00
|
|
|
auth_key->clear();
|
2016-06-30 02:42:01 +02:00
|
|
|
return false;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
static bool adbd_send_key_message_locked(std::string_view msg_type, std::string_view key)
|
|
|
|
REQUIRES(framework_mutex) {
|
|
|
|
if (framework_fd < 0) {
|
|
|
|
LOG(ERROR) << "Client not connected to send msg_type " << msg_type;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string msg = std::string(msg_type) + std::string(key);
|
|
|
|
int msg_len = msg.length();
|
|
|
|
if (msg_len >= static_cast<int>(MAX_FRAMEWORK_PAYLOAD)) {
|
|
|
|
LOG(ERROR) << "Key too long (" << msg_len << ")";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Sending '" << msg << "'";
|
|
|
|
if (!WriteFdExactly(framework_fd, msg.c_str(), msg_len)) {
|
|
|
|
PLOG(ERROR) << "Failed to write " << msg_type;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:02:29 +02:00
|
|
|
static bool adbd_auth_generate_token(void* token, size_t token_size) {
|
2016-06-30 02:42:01 +02:00
|
|
|
FILE* fp = fopen("/dev/urandom", "re");
|
|
|
|
if (!fp) return false;
|
|
|
|
bool okay = (fread(token, token_size, 1, fp) == 1);
|
|
|
|
fclose(fp);
|
|
|
|
return okay;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 05:35:37 +01:00
|
|
|
static void adb_disconnected(void* unused, atransport* t) {
|
|
|
|
LOG(INFO) << "ADB disconnect";
|
|
|
|
adb_transport = nullptr;
|
2013-01-16 04:59:14 +01:00
|
|
|
needs_retry = false;
|
2019-04-26 03:33:35 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(framework_mutex);
|
|
|
|
if (framework_fd >= 0) {
|
|
|
|
adbd_send_key_message_locked("DC", t->auth_key);
|
2018-12-29 05:35:37 +01:00
|
|
|
}
|
2019-04-26 03:33:35 +02:00
|
|
|
connected_keys.erase(std::remove(connected_keys.begin(), connected_keys.end(), t->auth_key),
|
|
|
|
connected_keys.end());
|
2018-12-29 05:35:37 +01:00
|
|
|
}
|
2013-01-16 04:59:14 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 03:05:57 +01:00
|
|
|
static void framework_disconnected() {
|
2016-06-30 02:42:01 +02:00
|
|
|
LOG(INFO) << "Framework disconnect";
|
2018-05-14 20:14:33 +02:00
|
|
|
if (framework_fde) {
|
|
|
|
fdevent_destroy(framework_fde);
|
2019-04-26 03:33:35 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(framework_mutex);
|
|
|
|
framework_fd = -1;
|
|
|
|
}
|
2018-05-14 20:14:33 +02:00
|
|
|
}
|
2016-02-24 03:05:57 +01:00
|
|
|
}
|
|
|
|
|
2016-10-06 04:02:29 +02:00
|
|
|
static void adbd_auth_event(int fd, unsigned events, void*) {
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
if (events & FDE_READ) {
|
2016-06-30 02:42:01 +02:00
|
|
|
char response[2];
|
|
|
|
int ret = unix_read(fd, response, sizeof(response));
|
2014-09-26 06:51:15 +02:00
|
|
|
if (ret <= 0) {
|
2016-02-24 03:05:57 +01:00
|
|
|
framework_disconnected();
|
|
|
|
} else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
|
2018-12-29 05:35:37 +01:00
|
|
|
if (adb_transport) {
|
|
|
|
adbd_auth_verified(adb_transport);
|
2016-02-24 03:05:57 +01:00
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
void adbd_auth_confirm_key(atransport* t) {
|
2018-12-29 05:35:37 +01:00
|
|
|
if (!adb_transport) {
|
|
|
|
adb_transport = t;
|
|
|
|
t->AddDisconnect(&adb_disconnect);
|
2013-04-02 02:39:06 +02:00
|
|
|
}
|
2013-01-16 04:59:14 +01:00
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(framework_mutex);
|
|
|
|
if (framework_fd < 0) {
|
|
|
|
LOG(ERROR) << "Client not connected";
|
|
|
|
needs_retry = true;
|
|
|
|
return;
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
adbd_send_key_message_locked("PK", t->auth_key);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:02:29 +02:00
|
|
|
static void adbd_auth_listener(int fd, unsigned events, void* data) {
|
2016-08-24 00:41:56 +02:00
|
|
|
int s = adb_socket_accept(fd, nullptr, nullptr);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
if (s < 0) {
|
2016-06-30 02:42:01 +02:00
|
|
|
PLOG(ERROR) << "Failed to accept";
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(framework_mutex);
|
|
|
|
if (framework_fd >= 0) {
|
|
|
|
LOG(WARNING) << "adb received framework auth socket connection again";
|
|
|
|
framework_disconnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
framework_fd = s;
|
|
|
|
framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
|
|
|
|
fdevent_add(framework_fde, FDE_READ);
|
|
|
|
|
|
|
|
if (needs_retry) {
|
|
|
|
needs_retry = false;
|
|
|
|
send_auth_request(adb_transport);
|
|
|
|
}
|
2016-02-24 03:05:57 +01:00
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
// if a client connected before the framework was available notify the framework of the
|
|
|
|
// connected key now.
|
|
|
|
if (!connected_keys.empty()) {
|
|
|
|
for (const auto& key : connected_keys) {
|
|
|
|
adbd_send_key_message_locked("CK", key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 04:59:14 +01:00
|
|
|
|
2019-04-26 03:33:35 +02:00
|
|
|
void adbd_notify_framework_connected_key(atransport* t) {
|
|
|
|
if (!adb_transport) {
|
|
|
|
adb_transport = t;
|
|
|
|
t->AddDisconnect(&adb_disconnect);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(framework_mutex);
|
|
|
|
if (std::find(connected_keys.begin(), connected_keys.end(), t->auth_key) ==
|
|
|
|
connected_keys.end()) {
|
|
|
|
connected_keys.push_back(t->auth_key);
|
|
|
|
}
|
|
|
|
if (framework_fd >= 0) {
|
|
|
|
adbd_send_key_message_locked("CK", t->auth_key);
|
|
|
|
}
|
2013-01-16 04:59:14 +01:00
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-17 19:03:36 +01:00
|
|
|
void adbd_cloexec_auth_socket() {
|
|
|
|
int fd = android_get_control_socket("adbd");
|
|
|
|
if (fd == -1) {
|
2016-06-30 02:42:01 +02:00
|
|
|
PLOG(ERROR) << "Failed to get adbd socket";
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-19 05:57:35 +02:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
2015-03-17 19:03:36 +01:00
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
2015-03-17 19:03:36 +01:00
|
|
|
void adbd_auth_init(void) {
|
|
|
|
int fd = android_get_control_socket("adbd");
|
|
|
|
if (fd == -1) {
|
2016-06-30 02:42:01 +02:00
|
|
|
PLOG(ERROR) << "Failed to get adbd socket";
|
2015-03-17 19:03:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 4) == -1) {
|
2016-06-30 02:42:01 +02:00
|
|
|
PLOG(ERROR) << "Failed to listen on '" << fd << "'";
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:15:16 +02:00
|
|
|
listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr);
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_add(listener_fde, FDE_READ);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
2016-10-06 04:02:29 +02:00
|
|
|
|
|
|
|
void send_auth_request(atransport* t) {
|
|
|
|
LOG(INFO) << "Calling send_auth_request...";
|
|
|
|
|
|
|
|
if (!adbd_auth_generate_token(t->token, sizeof(t->token))) {
|
|
|
|
PLOG(ERROR) << "Error generating token";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apacket* p = get_apacket();
|
|
|
|
p->msg.command = A_AUTH;
|
|
|
|
p->msg.arg0 = ADB_AUTH_TOKEN;
|
|
|
|
p->msg.data_length = sizeof(t->token);
|
2018-02-06 03:49:10 +01:00
|
|
|
p->payload.assign(t->token, t->token + sizeof(t->token));
|
2016-10-06 04:02:29 +02:00
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:06:55 +02:00
|
|
|
void adbd_auth_verified(atransport* t) {
|
|
|
|
LOG(INFO) << "adb client authorized";
|
2016-10-06 04:02:29 +02:00
|
|
|
handle_online(t);
|
|
|
|
send_connect(t);
|
|
|
|
}
|