1 /*
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 
17 #define TRACE_TAG TRACE_ADB
18 
19 #include "sysdeps.h"
20 #include "adb_auth.h"
21 
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"
29 #include "transport.h"
30 
31 bool auth_required = true;
32 
send_auth_request(atransport * t)33 void send_auth_request(atransport *t)
34 {
35     D("Calling send_auth_request\n");
36     apacket *p;
37     int ret;
38 
39     ret = adb_auth_generate_token(t->token, sizeof(t->token));
40     if (ret != sizeof(t->token)) {
41         D("Error generating token ret=%d\n", ret);
42         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 
send_auth_response(uint8_t * token,size_t token_size,atransport * t)53 void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
54 {
55     D("Calling send_auth_response\n");
56     apacket *p = get_apacket();
57     int ret;
58 
59     ret = adb_auth_sign(t->key, token, token_size, p->data);
60     if (!ret) {
61         D("Error signing the token\n");
62         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 
send_auth_publickey(atransport * t)72 void send_auth_publickey(atransport *t)
73 {
74     D("Calling send_auth_publickey\n");
75     apacket *p = get_apacket();
76     int ret;
77 
78     ret = adb_auth_get_userkey(p->data, sizeof(p->data));
79     if (!ret) {
80         D("Failed to get user public key\n");
81         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 
adb_auth_verified(atransport * t)91 void adb_auth_verified(atransport *t)
92 {
93     handle_online(t);
94     send_connect(t);
95 }
96