blob: 446c3df72794d75e873536f876410e4fee522499 [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;
Elliott Hughese8b663f2016-05-26 22:43:19 -070067 p->msg.data_length = key.size();
Dan Albertba3a2512015-02-18 17:47:33 -080068 send_packet(p, t);
69}
70
Elliott Hughes0aeb5052016-06-29 17:42:01 -070071void send_auth_response(uint8_t* token, size_t token_size, atransport* t) {
72 RSA* key = t->NextKey();
73 if (key == nullptr) {
74 // No more private keys to try, send the public key.
75 send_auth_publickey(t);
76 return;
77 }
78
79 LOG(INFO) << "Calling send_auth_response";
80 apacket* p = get_apacket();
81
82 int ret = adb_auth_sign(key, token, token_size, p->data);
83
84 // Stop sharing this key.
85 RSA_free(key);
86 key = nullptr;
87
88 if (!ret) {
89 D("Error signing the token");
90 put_apacket(p);
91 return;
92 }
93
94 p->msg.command = A_AUTH;
95 p->msg.arg0 = ADB_AUTH_SIGNATURE;
96 p->msg.data_length = ret;
97 send_packet(p, t);
98}
99
Dan Albertba3a2512015-02-18 17:47:33 -0800100void adb_auth_verified(atransport *t)
101{
102 handle_online(t);
103 send_connect(t);
104}