Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 06d61d4d authored by Josh Gao's avatar Josh Gao
Browse files

adb: rationalize types.

Use fixed length types for structs going over the wire, constify
arguments where possible, use char* instead of unsigned char* for
apacket data, and assorted other refactoring.

Bug: http://b/29273531
Test: python test_device.py with every combination of old/new adb and adbd
Change-Id: I0b6f818a32be5386985aa4519f542003cf427f9d
parent 4a8b178c
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -94,6 +94,18 @@ void fatal_errno(const char* fmt, ...) {
    abort();
}

uint32_t calculate_apacket_checksum(const apacket* p) {
    const unsigned char* x = reinterpret_cast<const unsigned char*>(p->data);
    uint32_t sum = 0;
    size_t count = p->msg.data_length;

    while (count-- > 0) {
        sum += *x++;
    }

    return sum;
}

apacket* get_apacket(void)
{
    apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket)));
+12 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#define __ADB_H

#include <limits.h>
#include <stdint.h>
#include <sys/types.h>

#include <string>
@@ -56,25 +57,27 @@ class atransport;
struct usb_handle;

struct amessage {
    unsigned command;       /* command identifier constant      */
    unsigned arg0;          /* first argument                   */
    unsigned arg1;          /* second argument                  */
    unsigned data_length;   /* length of payload (0 is allowed) */
    unsigned data_check;    /* checksum of data payload         */
    unsigned magic;         /* command ^ 0xffffffff             */
    uint32_t command;     /* command identifier constant      */
    uint32_t arg0;        /* first argument                   */
    uint32_t arg1;        /* second argument                  */
    uint32_t data_length; /* length of payload (0 is allowed) */
    uint32_t data_check;  /* checksum of data payload         */
    uint32_t magic;       /* command ^ 0xffffffff             */
};

struct apacket
{
    apacket *next;

    unsigned len;
    unsigned char *ptr;
    size_t len;
    char* ptr;

    amessage msg;
    unsigned char data[MAX_PAYLOAD];
    char data[MAX_PAYLOAD];
};

uint32_t calculate_apacket_checksum(const apacket* packet);

/* the adisconnect structure is used to record a callback that
** will be called whenever a transport is disconnected (e.g. by the user)
** this should be used to cleanup objects that depend on the
+3 −4
Original line number Diff line number Diff line
@@ -36,11 +36,10 @@
void adb_auth_init();

int adb_auth_keygen(const char* filename);
int adb_auth_sign(RSA* key, const unsigned char* token, size_t token_size, unsigned char* sig);
std::string adb_auth_get_userkey();
std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys();

void send_auth_response(uint8_t *token, size_t token_size, atransport *t);
void send_auth_response(const char* token, size_t token_size, atransport* t);

#else // !ADB_HOST

@@ -50,8 +49,8 @@ void adbd_auth_init(void);
void adbd_auth_verified(atransport *t);

void adbd_cloexec_auth_socket();
bool adbd_auth_verify(uint8_t* token, size_t token_size, uint8_t* sig, int sig_len);
void adbd_auth_confirm_key(unsigned char *data, size_t len, atransport *t);
bool adbd_auth_verify(const char* token, size_t token_size, const char* sig, int sig_len);
void adbd_auth_confirm_key(const char* data, size_t len, atransport* t);

void send_auth_request(atransport *t);

+4 −3
Original line number Diff line number Diff line
@@ -298,14 +298,15 @@ std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() {
    return result;
}

int adb_auth_sign(RSA* key, const unsigned char* token, size_t token_size, unsigned char* sig) {
static int adb_auth_sign(RSA* key, const char* token, size_t token_size, char* sig) {
    if (token_size != TOKEN_SIZE) {
        D("Unexpected token size %zd", token_size);
        return 0;
    }

    unsigned int len;
    if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key)) {
    if (!RSA_sign(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
                  reinterpret_cast<uint8_t*>(sig), &len, key)) {
        return 0;
    }

@@ -448,7 +449,7 @@ static void send_auth_publickey(atransport* t) {
    send_packet(p, t);
}

void send_auth_response(uint8_t* token, size_t token_size, atransport* t) {
void send_auth_response(const char* token, size_t token_size, atransport* t) {
    std::shared_ptr<RSA> key = t->NextKey();
    if (key == nullptr) {
        // No more private keys to try, send the public key.
+5 −3
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ static bool needs_retry = false;

bool auth_required = true;

bool adbd_auth_verify(uint8_t* token, size_t token_size, uint8_t* sig, int sig_len) {
bool adbd_auth_verify(const char* token, size_t token_size, const char* sig, int sig_len) {
    static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };

    for (const auto& path : key_paths) {
@@ -78,7 +78,9 @@ bool adbd_auth_verify(uint8_t* token, size_t token_size, uint8_t* sig, int sig_l
                    continue;
                }

                bool verified = (RSA_verify(NID_sha1, token, token_size, sig, sig_len, key) == 1);
                bool verified =
                    (RSA_verify(NID_sha1, reinterpret_cast<const uint8_t*>(token), token_size,
                                reinterpret_cast<const uint8_t*>(sig), sig_len, key) == 1);
                RSA_free(key);
                if (verified) return true;
            }
@@ -121,7 +123,7 @@ static void adbd_auth_event(int fd, unsigned events, void*) {
    }
}

void adbd_auth_confirm_key(unsigned char* key, size_t len, atransport* t) {
void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) {
    if (!usb_transport) {
        usb_transport = t;
        t->AddDisconnect(&usb_disconnect);
Loading