blob: 26ecc3cc22d26ae86a026c289e698fe5b250962d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Copyright (C) 2012 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#include <stdio.h>
18#include <string.h>
19//#include <resolv.h>
20#include <cutils/list.h>
21#include <cutils/sockets.h>
22
23#include "sysdeps.h"
24#include "adb.h"
25#include "adb_auth.h"
26#include "fdevent.h"
27#include "mincrypt/rsa.h"
28#include "mincrypt/sha.h"
29
30#define TRACE_TAG TRACE_AUTH
31
32
33struct adb_public_key {
34 struct listnode node;
35 RSAPublicKey key;
36};
37
38static char *key_paths[] = {
39 "/adb_keys",
40 "/data/misc/adb/adb_keys",
41 NULL
42};
43
44static fdevent listener_fde;
45static int framework_fd = -1;
46
47static void usb_disconnected(void* unused, atransport* t);
48static struct adisconnect usb_disconnect = { usb_disconnected, 0, 0, 0 };
49static atransport* usb_transport;
50static bool needs_retry = false;
51
52extern int b64_pton(const char *src, unsigned char *dst, int dstsiz);
53static void read_keys(const char *file, struct listnode *list)
54{
55 struct adb_public_key *key;
56 FILE *f;
57 char buf[MAX_PAYLOAD];
58 char *sep;
59 int ret;
60
61 f = fopen(file, "r");
62 if (!f) {
63 D("Can't open '%s'\n", file);
64 return;
65 }
66
67 while (fgets(buf, sizeof(buf), f)) {
68 /* Allocate 4 extra bytes to decode the base64 data in-place */
69 key = calloc(1, sizeof(*key) + 4);
70 if (!key) {
71 D("Can't malloc key\n");
72 break;
73 }
74
75 sep = strpbrk(buf, " \t");
76 if (sep)
77 *sep = '\0';
78
79 ret = b64_pton(buf, (u_char *)&key->key, sizeof(key->key) + 4);
80 if (ret != sizeof(key->key)) {
81 D("%s: Invalid base64 data ret=%d\n", file, ret);
82 free(key);
83 continue;
84 }
85
86 if (key->key.len != RSANUMWORDS) {
87 D("%s: Invalid key len %d\n", file, key->key.len);
88 free(key);
89 continue;
90 }
91
92 list_add_tail(list, &key->node);
93 }
94
95 fclose(f);
96}
97
98static void free_keys(struct listnode *list)
99{
100 struct listnode *item;
101
102 while (!list_empty(list)) {
103 item = list_head(list);
104 list_remove(item);
105 free(node_to_item(item, struct adb_public_key, node));
106 }
107}
108
109static void load_keys(struct listnode *list)
110{
111 char *path;
112 char **paths = key_paths;
113 struct stat buf;
114
115 list_init(list);
116
117 while ((path = *paths++)) {
118 if (!stat(path, &buf)) {
119 D("Loading keys from '%s'\n", path);
120 read_keys(path, list);
121 }
122 }
123}
124
125int adb_auth_generate_token(void *token, size_t token_size)
126{
127 FILE *f;
128 int ret;
129
130 f = fopen("/dev/urandom", "r");
131 if (!f)
132 return 0;
133
134 ret = fread(token, token_size, 1, f);
135
136 fclose(f);
137 return ret * token_size;
138}
139
140int adb_auth_verify(void *token, void *sig, int siglen)
141{
142 struct listnode *item;
143 struct adb_public_key *key;
144 struct listnode key_list;
145 int ret = 0;
146
147 if (siglen != RSANUMBYTES)
148 return 0;
149
150 load_keys(&key_list);
151
152 list_for_each(item, &key_list) {
153 key = node_to_item(item, struct adb_public_key, node);
154 ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE);
155 if (ret)
156 break;
157 }
158
159 free_keys(&key_list);
160
161 return ret;
162}
163
164static void usb_disconnected(void* unused, atransport* t)
165{
166 D("USB disconnect\n");
167 remove_transport_disconnect(usb_transport, &usb_disconnect);
168 usb_transport = NULL;
169 needs_retry = false;
170}
171
172static void adb_auth_event(int fd, unsigned events, void *data)
173{
174 char response[2];
175 int ret;
176
177 if (events & FDE_READ) {
178 ret = unix_read(fd, response, sizeof(response));
179 if (ret < 0) {
180 D("Framework disconnect\n");
181 if (usb_transport)
182 fdevent_remove(&usb_transport->auth_fde);
183 framework_fd = -1;
184 }
185 else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
186 if (usb_transport)
187 adb_auth_verified(usb_transport);
188 }
189 }
190}
191
192void adb_auth_confirm_key(unsigned char *key, size_t len, atransport *t)
193{
194 char msg[MAX_PAYLOAD];
195 int ret;
196
197 if (!usb_transport) {
198 usb_transport = t;
199 add_transport_disconnect(t, &usb_disconnect);
200 }
201
202 if (framework_fd < 0) {
203 D("Client not connected\n");
204 needs_retry = true;
205 return;
206 }
207
208 if (key[len - 1] != '\0') {
209 D("Key must be a null-terminated string\n");
210 return;
211 }
212
213 ret = snprintf(msg, sizeof(msg), "PK%s", key);
214 if (ret >= (signed)sizeof(msg)) {
215 D("Key too long. ret=%d", ret);
216 return;
217 }
218 D("Sending '%s'\n", msg);
219
220 ret = unix_write(framework_fd, msg, ret);
221 if (ret < 0) {
222 D("Failed to write PK, errno=%d\n", errno);
223 return;
224 }
225
226 fdevent_install(&t->auth_fde, framework_fd, adb_auth_event, t);
227 fdevent_add(&t->auth_fde, FDE_READ);
228}
229
230static void adb_auth_listener(int fd, unsigned events, void *data)
231{
232 struct sockaddr addr;
233 socklen_t alen;
234 int s;
235
236 alen = sizeof(addr);
237
238 s = adb_socket_accept(fd, &addr, &alen);
239 if (s < 0) {
240 D("Failed to accept: errno=%d\n", errno);
241 return;
242 }
243
244 framework_fd = s;
245
246 if (needs_retry) {
247 needs_retry = false;
248 send_auth_request(usb_transport);
249 }
250}
251
252void adb_auth_init(void)
253{
254 int fd, ret;
255
256 fd = android_get_control_socket("adbd");
257 if (fd < 0) {
258 D("Failed to get adbd socket\n");
259 return;
260 }
261
262 ret = listen(fd, 4);
263 if (ret < 0) {
264 D("Failed to listen on '%d'\n", fd);
265 return;
266 }
267
268 fdevent_install(&listener_fde, fd, adb_auth_listener, NULL);
269 fdevent_add(&listener_fde, FDE_READ);
270}