blob: 0b07158eb255d0ecb86067c50123befdecf69b3b [file] [log] [blame]
Dan Albertba3a2512015-02-18 17:47:33 -08001/*
2 * Copyright (C) 2015 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 ADB
Dan Albert33134262015-03-19 15:21:08 -070018
Elliott Hughes0aeb5052016-06-29 17:42:01 -070019#include "adb.h"
Dan Albert33134262015-03-19 15:21:08 -070020#include "adb_auth.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070021#include "transport.h"
Dan Albertba3a2512015-02-18 17:47:33 -080022
23#include <errno.h>
24#include <stdio.h>
25#include <string.h>
26#include <sys/types.h>
27#include <unistd.h>
28
Elliott Hughes5cba5042015-06-17 15:23:42 -070029bool auth_required = true;
Dan Albertbd0b7502015-02-18 18:22:45 -080030
Dan Albertba3a2512015-02-18 17:47:33 -080031void send_auth_request(atransport *t)
32{
Elliott Hughes0aeb5052016-06-29 17:42:01 -070033 LOG(INFO) << "Calling send_auth_request...";
Dan Albertba3a2512015-02-18 17:47:33 -080034
Elliott Hughes0aeb5052016-06-29 17:42:01 -070035 if (!adb_auth_generate_token(t->token, sizeof(t->token))) {
36 PLOG(ERROR) << "Error generating token";
Dan Albertba3a2512015-02-18 17:47:33 -080037 return;
38 }
39
Elliott Hughes0aeb5052016-06-29 17:42:01 -070040 apacket* p = get_apacket();
41 memcpy(p->data, t->token, sizeof(t->token));
Dan Albertba3a2512015-02-18 17:47:33 -080042 p->msg.command = A_AUTH;
43 p->msg.arg0 = ADB_AUTH_TOKEN;
Elliott Hughes0aeb5052016-06-29 17:42:01 -070044 p->msg.data_length = sizeof(t->token);
Dan Albertba3a2512015-02-18 17:47:33 -080045 send_packet(p, t);
46}
47
Elliott Hughes0aeb5052016-06-29 17:42:01 -070048static void send_auth_publickey(atransport* t) {
49 LOG(INFO) << "Calling send_auth_publickey";
Dan Albertba3a2512015-02-18 17:47:33 -080050
Elliott Hughese8b663f2016-05-26 22:43:19 -070051 std::string key = adb_auth_get_userkey();
52 if (key.empty()) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070053 D("Failed to get user public key");
Dan Albertba3a2512015-02-18 17:47:33 -080054 return;
55 }
56
Elliott Hughese8b663f2016-05-26 22:43:19 -070057 if (key.size() >= MAX_PAYLOAD_V1) {
58 D("User public key too large (%zu B)", key.size());
59 return;
60 }
61
62 apacket* p = get_apacket();
63 memcpy(p->data, key.c_str(), key.size() + 1);
64
Dan Albertba3a2512015-02-18 17:47:33 -080065 p->msg.command = A_AUTH;
66 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
Josh Gao8a0d0772016-08-22 12:50:23 -070067
68 // adbd expects a null-terminated string.
69 p->msg.data_length = key.size() + 1;
Dan Albertba3a2512015-02-18 17:47:33 -080070 send_packet(p, t);
71}
72
Elliott Hughes0aeb5052016-06-29 17:42:01 -070073void send_auth_response(uint8_t* token, size_t token_size, atransport* t) {
Josh Gao2e671202016-08-18 22:00:12 -070074 std::shared_ptr<RSA> key = t->NextKey();
Elliott Hughes0aeb5052016-06-29 17:42:01 -070075 if (key == nullptr) {
76 // No more private keys to try, send the public key.
77 send_auth_publickey(t);
78 return;
79 }
80
81 LOG(INFO) << "Calling send_auth_response";
82 apacket* p = get_apacket();
83
Josh Gao2e671202016-08-18 22:00:12 -070084 int ret = adb_auth_sign(key.get(), token, token_size, p->data);
Elliott Hughes0aeb5052016-06-29 17:42:01 -070085 if (!ret) {
86 D("Error signing the token");
87 put_apacket(p);
88 return;
89 }
90
91 p->msg.command = A_AUTH;
92 p->msg.arg0 = ADB_AUTH_SIGNATURE;
93 p->msg.data_length = ret;
94 send_packet(p, t);
95}
96
Dan Albertba3a2512015-02-18 17:47:33 -080097void adb_auth_verified(atransport *t)
98{
99 handle_online(t);
100 send_connect(t);
101}