From ba3a251749c88210949008ae26f8bc04812084c1 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 17:47:33 -0800 Subject: [PATCH] Move the adb auth code into its own file. Change-Id: I84cf0bd7777f0147119e7c6afc4096c2e93156a2 --- adb/Android.mk | 11 ++++-- adb/adb.c | 66 +----------------------------------- adb/adb.h | 5 +++ adb/adb_auth.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ adb/adb_auth.h | 2 ++ 5 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 adb/adb_auth.c diff --git a/adb/Android.mk b/adb/Android.mk index 83d168b4b..51318ef50 100644 --- a/adb/Android.mk +++ b/adb/Android.mk @@ -14,8 +14,15 @@ LOCAL_PATH:= $(call my-dir) # divergence makes this difficult to do all at once. For now, we will start # small by moving common files into a static library. Hopefully some day we can # get enough of adb in here that we no longer need minadb. https://b/17626262 -LIBADB_SRC_FILES := transport.c transport_usb.c -LIBADB_C_FLAGS := -Wall -Werror -D_XOPEN_SOURCE -D_GNU_SOURCE +LIBADB_SRC_FILES := \ + adb_auth.c \ + transport.c \ + transport_usb.c \ + +LIBADB_C_FLAGS := \ + -Wall -Werror \ + -D_XOPEN_SOURCE -D_GNU_SOURCE \ + -fvisibility=hidden \ LIBADB_LINUX_SRC_FILES := fdevent.cpp LIBADB_WINDOWS_SRC_FILES := sysdeps_win32.c diff --git a/adb/adb.c b/adb/adb.c index e9671e393..a336478d6 100644 --- a/adb/adb.c +++ b/adb/adb.c @@ -286,7 +286,7 @@ static void send_msg_with_okay(int fd, const char* msg, size_t msglen) { } #endif // ADB_HOST -static void send_connect(atransport *t) +void send_connect(atransport *t) { D("Calling send_connect \n"); apacket *cp = get_apacket(); @@ -298,70 +298,6 @@ static void send_connect(atransport *t) send_packet(cp, t); } -void send_auth_request(atransport *t) -{ - D("Calling send_auth_request\n"); - apacket *p; - int ret; - - ret = adb_auth_generate_token(t->token, sizeof(t->token)); - if (ret != sizeof(t->token)) { - D("Error generating token ret=%d\n", ret); - return; - } - - p = get_apacket(); - memcpy(p->data, t->token, ret); - p->msg.command = A_AUTH; - p->msg.arg0 = ADB_AUTH_TOKEN; - p->msg.data_length = ret; - send_packet(p, t); -} - -static void send_auth_response(uint8_t *token, size_t token_size, atransport *t) -{ - D("Calling send_auth_response\n"); - apacket *p = get_apacket(); - int ret; - - ret = adb_auth_sign(t->key, token, token_size, p->data); - if (!ret) { - D("Error signing the token\n"); - put_apacket(p); - return; - } - - p->msg.command = A_AUTH; - p->msg.arg0 = ADB_AUTH_SIGNATURE; - p->msg.data_length = ret; - send_packet(p, t); -} - -static void send_auth_publickey(atransport *t) -{ - D("Calling send_auth_publickey\n"); - apacket *p = get_apacket(); - int ret; - - ret = adb_auth_get_userkey(p->data, sizeof(p->data)); - if (!ret) { - D("Failed to get user public key\n"); - put_apacket(p); - return; - } - - p->msg.command = A_AUTH; - p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; - p->msg.data_length = ret; - send_packet(p, t); -} - -void adb_auth_verified(atransport *t) -{ - handle_online(t); - send_connect(t); -} - #if ADB_HOST static char *connection_state_name(atransport *t) { diff --git a/adb/adb.h b/adb/adb.h index 600bdd50f..48f2a2501 100644 --- a/adb/adb.h +++ b/adb/adb.h @@ -424,6 +424,11 @@ typedef enum { int sendfailmsg(int fd, const char *reason); int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); +void handle_online(atransport *t); +void handle_offline(atransport *t); + +void send_connect(atransport *t); + #ifdef __cplusplus } #endif diff --git a/adb/adb_auth.c b/adb/adb_auth.c new file mode 100644 index 000000000..00cf46912 --- /dev/null +++ b/adb/adb_auth.c @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015 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. + */ + +#define TRACE_TAG TRACE_ADB + +#include +#include +#include +#include +#include + +#include "adb.h" +#include "adb_auth.h" +#include "sysdeps.h" + +void send_auth_request(atransport *t) +{ + D("Calling send_auth_request\n"); + apacket *p; + int ret; + + ret = adb_auth_generate_token(t->token, sizeof(t->token)); + if (ret != sizeof(t->token)) { + D("Error generating token ret=%d\n", ret); + return; + } + + p = get_apacket(); + memcpy(p->data, t->token, ret); + p->msg.command = A_AUTH; + p->msg.arg0 = ADB_AUTH_TOKEN; + p->msg.data_length = ret; + send_packet(p, t); +} + +void send_auth_response(uint8_t *token, size_t token_size, atransport *t) +{ + D("Calling send_auth_response\n"); + apacket *p = get_apacket(); + int ret; + + ret = adb_auth_sign(t->key, token, token_size, p->data); + if (!ret) { + D("Error signing the token\n"); + put_apacket(p); + return; + } + + p->msg.command = A_AUTH; + p->msg.arg0 = ADB_AUTH_SIGNATURE; + p->msg.data_length = ret; + send_packet(p, t); +} + +void send_auth_publickey(atransport *t) +{ + D("Calling send_auth_publickey\n"); + apacket *p = get_apacket(); + int ret; + + ret = adb_auth_get_userkey(p->data, sizeof(p->data)); + if (!ret) { + D("Failed to get user public key\n"); + put_apacket(p); + return; + } + + p->msg.command = A_AUTH; + p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; + p->msg.data_length = ret; + send_packet(p, t); +} + +void adb_auth_verified(atransport *t) +{ + handle_online(t); + send_connect(t); +} diff --git a/adb/adb_auth.h b/adb/adb_auth.h index b718b428f..4431b5bd1 100644 --- a/adb/adb_auth.h +++ b/adb/adb_auth.h @@ -26,6 +26,8 @@ int adb_auth_keygen(const char* filename); void adb_auth_verified(atransport *t); void send_auth_request(atransport *t); +void send_auth_response(uint8_t *token, size_t token_size, atransport *t); +void send_auth_publickey(atransport *t); /* AUTH packets first argument */ /* Request */