blob: 215bbe65461d4316a97f0bb1b678d0463bc27c77 [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
19#include "sysdeps.h"
20#include "adb_auth.h"
Dan Albertba3a2512015-02-18 17:47:33 -080021
22#include <errno.h>
23#include <stdio.h>
24#include <string.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080029#include "transport.h"
Dan Albertba3a2512015-02-18 17:47:33 -080030
Elliott Hughes5cba5042015-06-17 15:23:42 -070031bool auth_required = true;
Dan Albertbd0b7502015-02-18 18:22:45 -080032
Dan Albertba3a2512015-02-18 17:47:33 -080033void send_auth_request(atransport *t)
34{
Yabin Cui7a3f8d62015-09-02 17:44:28 -070035 D("Calling send_auth_request");
Dan Albertba3a2512015-02-18 17:47:33 -080036 apacket *p;
37 int ret;
38
39 ret = adb_auth_generate_token(t->token, sizeof(t->token));
40 if (ret != sizeof(t->token)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070041 D("Error generating token ret=%d", ret);
Dan Albertba3a2512015-02-18 17:47:33 -080042 return;
43 }
44
45 p = get_apacket();
46 memcpy(p->data, t->token, ret);
47 p->msg.command = A_AUTH;
48 p->msg.arg0 = ADB_AUTH_TOKEN;
49 p->msg.data_length = ret;
50 send_packet(p, t);
51}
52
53void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
54{
Yabin Cui7a3f8d62015-09-02 17:44:28 -070055 D("Calling send_auth_response");
Dan Albertba3a2512015-02-18 17:47:33 -080056 apacket *p = get_apacket();
57 int ret;
58
59 ret = adb_auth_sign(t->key, token, token_size, p->data);
60 if (!ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070061 D("Error signing the token");
Dan Albertba3a2512015-02-18 17:47:33 -080062 put_apacket(p);
63 return;
64 }
65
66 p->msg.command = A_AUTH;
67 p->msg.arg0 = ADB_AUTH_SIGNATURE;
68 p->msg.data_length = ret;
69 send_packet(p, t);
70}
71
72void send_auth_publickey(atransport *t)
73{
Yabin Cui7a3f8d62015-09-02 17:44:28 -070074 D("Calling send_auth_publickey");
Elliott Hughese8b663f2016-05-26 22:43:19 -070075 std::string key = adb_auth_get_userkey();
76 if (key.empty()) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070077 D("Failed to get user public key");
Dan Albertba3a2512015-02-18 17:47:33 -080078 return;
79 }
80
Elliott Hughese8b663f2016-05-26 22:43:19 -070081 if (key.size() >= MAX_PAYLOAD_V1) {
82 D("User public key too large (%zu B)", key.size());
83 return;
84 }
85
86 apacket* p = get_apacket();
87 memcpy(p->data, key.c_str(), key.size() + 1);
88
Dan Albertba3a2512015-02-18 17:47:33 -080089 p->msg.command = A_AUTH;
90 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
Elliott Hughese8b663f2016-05-26 22:43:19 -070091 p->msg.data_length = key.size();
Dan Albertba3a2512015-02-18 17:47:33 -080092 send_packet(p, t);
93}
94
95void adb_auth_verified(atransport *t)
96{
97 handle_online(t);
98 send_connect(t);
99}