blob: 1ffab0940f6fe95adcbfde1c0a1654e4018b88c2 [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");
Dan Albertba3a2512015-02-18 17:47:33 -080075 apacket *p = get_apacket();
76 int ret;
77
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010078 ret = adb_auth_get_userkey(p->data, MAX_PAYLOAD_V1);
Dan Albertba3a2512015-02-18 17:47:33 -080079 if (!ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070080 D("Failed to get user public key");
Dan Albertba3a2512015-02-18 17:47:33 -080081 put_apacket(p);
82 return;
83 }
84
85 p->msg.command = A_AUTH;
86 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
87 p->msg.data_length = ret;
88 send_packet(p, t);
89}
90
91void adb_auth_verified(atransport *t)
92{
93 handle_online(t);
94 send_connect(t);
95}