blob: 7be883c309ca1e463e01c092f2a7e057ad73c045 [file] [log] [blame]
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001/*
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
Dan Albert76649012015-02-24 15:51:19 -080017#include <resolv.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070018#include <stdio.h>
19#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070020
21#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080022
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070023#include "adb.h"
24#include "adb_auth.h"
Dan Albert76649012015-02-24 15:51:19 -080025#include "cutils/list.h"
26#include "cutils/sockets.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070027#include "fdevent.h"
28#include "mincrypt/rsa.h"
Doug Zongker515e1632013-04-10 09:22:02 -070029#include "mincrypt/sha.h"
Dan Albert76649012015-02-24 15:51:19 -080030#include "transport.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070031
32#define TRACE_TAG TRACE_AUTH
33
34
35struct adb_public_key {
36 struct listnode node;
37 RSAPublicKey key;
38};
39
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070040static char *key_paths[] = {
41 "/adb_keys",
42 "/data/misc/adb/adb_keys",
43 NULL
44};
45
46static fdevent listener_fde;
47static int framework_fd = -1;
48
Benoit Goby045a4a92013-01-15 19:59:14 -080049static void usb_disconnected(void* unused, atransport* t);
50static struct adisconnect usb_disconnect = { usb_disconnected, 0, 0, 0 };
51static atransport* usb_transport;
52static bool needs_retry = false;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070053
54static void read_keys(const char *file, struct listnode *list)
55{
56 struct adb_public_key *key;
57 FILE *f;
58 char buf[MAX_PAYLOAD];
59 char *sep;
60 int ret;
61
Nick Kralevichfe8d7f42014-07-18 20:57:35 -070062 f = fopen(file, "re");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070063 if (!f) {
64 D("Can't open '%s'\n", file);
65 return;
66 }
67
68 while (fgets(buf, sizeof(buf), f)) {
69 /* Allocate 4 extra bytes to decode the base64 data in-place */
70 key = calloc(1, sizeof(*key) + 4);
71 if (!key) {
72 D("Can't malloc key\n");
73 break;
74 }
75
76 sep = strpbrk(buf, " \t");
77 if (sep)
78 *sep = '\0';
79
80 ret = __b64_pton(buf, (u_char *)&key->key, sizeof(key->key) + 4);
81 if (ret != sizeof(key->key)) {
82 D("%s: Invalid base64 data ret=%d\n", file, ret);
83 free(key);
84 continue;
85 }
86
87 if (key->key.len != RSANUMWORDS) {
88 D("%s: Invalid key len %d\n", file, key->key.len);
89 free(key);
90 continue;
91 }
92
93 list_add_tail(list, &key->node);
94 }
95
96 fclose(f);
97}
98
99static void free_keys(struct listnode *list)
100{
101 struct listnode *item;
102
103 while (!list_empty(list)) {
104 item = list_head(list);
105 list_remove(item);
106 free(node_to_item(item, struct adb_public_key, node));
107 }
108}
109
Benoit Goby345cb062013-01-14 21:26:30 -0800110static void load_keys(struct listnode *list)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700111{
112 char *path;
113 char **paths = key_paths;
114 struct stat buf;
115
Benoit Goby345cb062013-01-14 21:26:30 -0800116 list_init(list);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700117
118 while ((path = *paths++)) {
119 if (!stat(path, &buf)) {
120 D("Loading keys from '%s'\n", path);
Benoit Goby345cb062013-01-14 21:26:30 -0800121 read_keys(path, list);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700122 }
123 }
124}
125
126int adb_auth_generate_token(void *token, size_t token_size)
127{
128 FILE *f;
129 int ret;
130
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700131 f = fopen("/dev/urandom", "re");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700132 if (!f)
133 return 0;
134
135 ret = fread(token, token_size, 1, f);
136
137 fclose(f);
138 return ret * token_size;
139}
140
141int adb_auth_verify(void *token, void *sig, int siglen)
142{
143 struct listnode *item;
144 struct adb_public_key *key;
Benoit Goby345cb062013-01-14 21:26:30 -0800145 struct listnode key_list;
146 int ret = 0;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700147
148 if (siglen != RSANUMBYTES)
149 return 0;
150
Benoit Goby345cb062013-01-14 21:26:30 -0800151 load_keys(&key_list);
152
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700153 list_for_each(item, &key_list) {
154 key = node_to_item(item, struct adb_public_key, node);
Doug Zongker515e1632013-04-10 09:22:02 -0700155 ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700156 if (ret)
Benoit Goby345cb062013-01-14 21:26:30 -0800157 break;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700158 }
159
Benoit Goby345cb062013-01-14 21:26:30 -0800160 free_keys(&key_list);
161
162 return ret;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700163}
164
Benoit Goby045a4a92013-01-15 19:59:14 -0800165static void usb_disconnected(void* unused, atransport* t)
166{
Benoit Gobyb66356c2013-04-01 17:39:06 -0700167 D("USB disconnect\n");
Benoit Goby045a4a92013-01-15 19:59:14 -0800168 remove_transport_disconnect(usb_transport, &usb_disconnect);
169 usb_transport = NULL;
170 needs_retry = false;
171}
172
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700173static void adb_auth_event(int fd, unsigned events, void *data)
174{
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700175 char response[2];
176 int ret;
177
178 if (events & FDE_READ) {
179 ret = unix_read(fd, response, sizeof(response));
Vince Harronaf436b12014-09-25 21:51:15 -0700180 if (ret <= 0) {
Benoit Gobyb66356c2013-04-01 17:39:06 -0700181 D("Framework disconnect\n");
Benoit Goby045a4a92013-01-15 19:59:14 -0800182 if (usb_transport)
183 fdevent_remove(&usb_transport->auth_fde);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700184 framework_fd = -1;
185 }
186 else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
Benoit Goby045a4a92013-01-15 19:59:14 -0800187 if (usb_transport)
188 adb_auth_verified(usb_transport);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700189 }
190 }
191}
192
193void adb_auth_confirm_key(unsigned char *key, size_t len, atransport *t)
194{
195 char msg[MAX_PAYLOAD];
196 int ret;
197
Benoit Gobyb66356c2013-04-01 17:39:06 -0700198 if (!usb_transport) {
199 usb_transport = t;
200 add_transport_disconnect(t, &usb_disconnect);
201 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800202
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700203 if (framework_fd < 0) {
204 D("Client not connected\n");
Benoit Goby045a4a92013-01-15 19:59:14 -0800205 needs_retry = true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700206 return;
207 }
208
209 if (key[len - 1] != '\0') {
210 D("Key must be a null-terminated string\n");
211 return;
212 }
213
214 ret = snprintf(msg, sizeof(msg), "PK%s", key);
215 if (ret >= (signed)sizeof(msg)) {
216 D("Key too long. ret=%d", ret);
217 return;
218 }
219 D("Sending '%s'\n", msg);
220
221 ret = unix_write(framework_fd, msg, ret);
222 if (ret < 0) {
223 D("Failed to write PK, errno=%d\n", errno);
224 return;
225 }
226
227 fdevent_install(&t->auth_fde, framework_fd, adb_auth_event, t);
228 fdevent_add(&t->auth_fde, FDE_READ);
229}
230
231static void adb_auth_listener(int fd, unsigned events, void *data)
232{
233 struct sockaddr addr;
234 socklen_t alen;
235 int s;
236
237 alen = sizeof(addr);
238
239 s = adb_socket_accept(fd, &addr, &alen);
240 if (s < 0) {
241 D("Failed to accept: errno=%d\n", errno);
242 return;
243 }
244
245 framework_fd = s;
Benoit Goby045a4a92013-01-15 19:59:14 -0800246
247 if (needs_retry) {
248 needs_retry = false;
249 send_auth_request(usb_transport);
250 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700251}
252
253void adb_auth_init(void)
254{
255 int fd, ret;
256
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700257 fd = android_get_control_socket("adbd");
258 if (fd < 0) {
259 D("Failed to get adbd socket\n");
260 return;
261 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700262 fcntl(fd, F_SETFD, FD_CLOEXEC);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700263
264 ret = listen(fd, 4);
265 if (ret < 0) {
266 D("Failed to listen on '%d'\n", fd);
267 return;
268 }
269
270 fdevent_install(&listener_fde, fd, adb_auth_listener, NULL);
271 fdevent_add(&listener_fde, FDE_READ);
272}