Merge "adb: rationalize types." am: 8caf4e7443

am: e335bea277

Change-Id: I045ccbebbc1005a501768da3222a211e49821895
This commit is contained in:
Josh Gao 2016-10-07 20:44:43 +00:00 committed by android-build-merger
commit 010818f313
8 changed files with 50 additions and 60 deletions

View file

@ -94,6 +94,18 @@ void fatal_errno(const char* fmt, ...) {
abort(); 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* get_apacket(void)
{ {
apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket))); apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket)));

View file

@ -18,6 +18,7 @@
#define __ADB_H #define __ADB_H
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <string> #include <string>
@ -56,25 +57,27 @@ class atransport;
struct usb_handle; struct usb_handle;
struct amessage { struct amessage {
unsigned command; /* command identifier constant */ uint32_t command; /* command identifier constant */
unsigned arg0; /* first argument */ uint32_t arg0; /* first argument */
unsigned arg1; /* second argument */ uint32_t arg1; /* second argument */
unsigned data_length; /* length of payload (0 is allowed) */ uint32_t data_length; /* length of payload (0 is allowed) */
unsigned data_check; /* checksum of data payload */ uint32_t data_check; /* checksum of data payload */
unsigned magic; /* command ^ 0xffffffff */ uint32_t magic; /* command ^ 0xffffffff */
}; };
struct apacket struct apacket
{ {
apacket *next; apacket *next;
unsigned len; size_t len;
unsigned char *ptr; char* ptr;
amessage msg; 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 /* the adisconnect structure is used to record a callback that
** will be called whenever a transport is disconnected (e.g. by the user) ** will be called whenever a transport is disconnected (e.g. by the user)
** this should be used to cleanup objects that depend on the ** this should be used to cleanup objects that depend on the

View file

@ -36,11 +36,10 @@
void adb_auth_init(); void adb_auth_init();
int adb_auth_keygen(const char* filename); 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::string adb_auth_get_userkey();
std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys(); 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 #else // !ADB_HOST
@ -50,8 +49,8 @@ void adbd_auth_init(void);
void adbd_auth_verified(atransport *t); void adbd_auth_verified(atransport *t);
void adbd_cloexec_auth_socket(); void adbd_cloexec_auth_socket();
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);
void adbd_auth_confirm_key(unsigned char *data, size_t len, atransport *t); void adbd_auth_confirm_key(const char* data, size_t len, atransport* t);
void send_auth_request(atransport *t); void send_auth_request(atransport *t);

View file

@ -298,14 +298,15 @@ std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() {
return result; 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) { if (token_size != TOKEN_SIZE) {
D("Unexpected token size %zd", token_size); D("Unexpected token size %zd", token_size);
return 0; return 0;
} }
unsigned int len; 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; return 0;
} }
@ -448,7 +449,7 @@ static void send_auth_publickey(atransport* t) {
send_packet(p, 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(); std::shared_ptr<RSA> key = t->NextKey();
if (key == nullptr) { if (key == nullptr) {
// No more private keys to try, send the public key. // No more private keys to try, send the public key.

View file

@ -46,7 +46,7 @@ static bool needs_retry = false;
bool auth_required = true; 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 }; static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr };
for (const auto& path : key_paths) { 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; 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); RSA_free(key);
if (verified) return true; 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) { if (!usb_transport) {
usb_transport = t; usb_transport = t;
t->AddDisconnect(&usb_disconnect); t->AddDisconnect(&usb_disconnect);

View file

@ -122,7 +122,7 @@ restart:
} }
static int local_socket_enqueue(asocket* s, apacket* p) { static int local_socket_enqueue(asocket* s, apacket* p) {
D("LS(%d): enqueue %d", s->id, p->len); D("LS(%d): enqueue %zu", s->id, p->len);
p->ptr = p->data; p->ptr = p->data;
@ -195,7 +195,7 @@ static void local_socket_destroy(asocket* s) {
/* dispose of any unwritten data */ /* dispose of any unwritten data */
for (p = s->pkt_first; p; p = n) { for (p = s->pkt_first; p; p = n) {
D("LS(%d): discarding %d bytes", s->id, p->len); D("LS(%d): discarding %zu bytes", s->id, p->len);
n = p->next; n = p->next;
put_apacket(p); put_apacket(p);
} }
@ -305,7 +305,7 @@ static void local_socket_event_func(int fd, unsigned ev, void* _s) {
if (ev & FDE_READ) { if (ev & FDE_READ) {
apacket* p = get_apacket(); apacket* p = get_apacket();
unsigned char* x = p->data; char* x = p->data;
const size_t max_payload = s->get_max_payload(); const size_t max_payload = s->get_max_payload();
size_t avail = max_payload; size_t avail = max_payload;
int r = 0; int r = 0;
@ -553,7 +553,7 @@ static void local_socket_close_notify(asocket* s) {
s->close(s); s->close(s);
} }
static unsigned unhex(unsigned char* s, int len) { static unsigned unhex(char* s, int len) {
unsigned n = 0, c; unsigned n = 0, c;
while (len-- > 0) { while (len-- > 0) {
@ -665,7 +665,7 @@ static int smart_socket_enqueue(asocket* s, apacket* p) {
TransportType type = kTransportAny; TransportType type = kTransportAny;
#endif #endif
D("SS(%d): enqueue %d", s->id, p->len); D("SS(%d): enqueue %zu", s->id, p->len);
if (s->pkt_first == 0) { if (s->pkt_first == 0) {
s->pkt_first = p; s->pkt_first = p;
@ -698,7 +698,7 @@ static int smart_socket_enqueue(asocket* s, apacket* p) {
D("SS(%d): len is %d", s->id, len); D("SS(%d): len is %d", s->id, len);
/* can't do anything until we have the full header */ /* can't do anything until we have the full header */
if ((len + 4) > p->len) { if ((len + 4) > p->len) {
D("SS(%d): waiting for %d more bytes", s->id, len + 4 - p->len); D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - p->len);
return 0; return 0;
} }

View file

@ -150,32 +150,17 @@ static void transport_socket_events(int fd, unsigned events, void *_t)
} }
} }
void send_packet(apacket *p, atransport *t) void send_packet(apacket* p, atransport* t) {
{
unsigned char *x;
unsigned sum;
unsigned count;
p->msg.magic = p->msg.command ^ 0xffffffff; p->msg.magic = p->msg.command ^ 0xffffffff;
p->msg.data_check = calculate_apacket_checksum(p);
count = p->msg.data_length;
x = (unsigned char *) p->data;
sum = 0;
while(count-- > 0){
sum += *x++;
}
p->msg.data_check = sum;
print_packet("send", p); print_packet("send", p);
if (t == NULL) { if (t == NULL) {
D("Transport is null"); fatal("Transport is null");
// Zap errno because print_packet() and other stuff have errno effect.
errno = 0;
fatal_errno("Transport is null");
} }
if(write_packet(t->transport_socket, t->serial, &p)){ if (write_packet(t->transport_socket, t->serial, &p)) {
fatal_errno("cannot enqueue packet on transport socket"); fatal_errno("cannot enqueue packet on transport socket");
} }
} }
@ -1052,23 +1037,11 @@ int check_header(apacket *p, atransport *t)
return 0; return 0;
} }
int check_data(apacket *p) int check_data(apacket* p) {
{ if (calculate_apacket_checksum(p) != p->msg.data_check) {
unsigned count, sum;
unsigned char *x;
count = p->msg.data_length;
x = p->data;
sum = 0;
while(count-- > 0) {
sum += *x++;
}
if(sum != p->msg.data_check) {
return -1; return -1;
} else {
return 0;
} }
return 0;
} }
#if ADB_HOST #if ADB_HOST

View file

@ -112,7 +112,7 @@ public:
std::shared_ptr<RSA> NextKey(); std::shared_ptr<RSA> NextKey();
#endif #endif
unsigned char token[TOKEN_SIZE] = {}; char token[TOKEN_SIZE] = {};
size_t failed_auth_attempts = 0; size_t failed_auth_attempts = 0;
const std::string connection_state_name() const; const std::string connection_state_name() const;