blob: c4ffc85a2383f298c5fbdca405610402222d9188 [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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG AUTH
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "adb_auth.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <resolv.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070023#include <stdio.h>
24#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070025
Dan Albert76649012015-02-24 15:51:19 -080026#include "cutils/list.h"
27#include "cutils/sockets.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070028#include "mincrypt/rsa.h"
Doug Zongker515e1632013-04-10 09:22:02 -070029#include "mincrypt/sha.h"
Dan Albert33134262015-03-19 15:21:08 -070030
31#include "adb.h"
32#include "fdevent.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "transport.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070034
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070035struct adb_public_key {
36 struct listnode node;
37 RSAPublicKey key;
38};
39
Dan Albertbac34742015-02-25 17:51:28 -080040static const char *key_paths[] = {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070041 "/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);
Yabin Cuib3298242015-08-28 15:09:44 -070050static struct adisconnect usb_disconnect = { usb_disconnected, nullptr};
Benoit Goby045a4a92013-01-15 19:59:14 -080051static 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{
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070056 FILE *f;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010057 char buf[MAX_PAYLOAD_V1];
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070058 char *sep;
59 int ret;
60
Nick Kralevichfe8d7f42014-07-18 20:57:35 -070061 f = fopen(file, "re");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070062 if (!f) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070063 D("Can't open '%s'", file);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070064 return;
65 }
66
67 while (fgets(buf, sizeof(buf), f)) {
68 /* Allocate 4 extra bytes to decode the base64 data in-place */
Dan Albertbac34742015-02-25 17:51:28 -080069 auto key = reinterpret_cast<adb_public_key*>(
70 calloc(1, sizeof(adb_public_key) + 4));
71 if (key == nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070072 D("Can't malloc key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070073 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070082 D("%s: Invalid base64 data ret=%d", file, ret);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070083 free(key);
84 continue;
85 }
86
87 if (key->key.len != RSANUMWORDS) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070088 D("%s: Invalid key len %d", file, key->key.len);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070089 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{
Dan Albertbac34742015-02-25 17:51:28 -0800112 const char* path;
113 const char** paths = key_paths;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700114 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700120 D("Loading keys from '%s'", 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
Dan Albertbac34742015-02-25 17:51:28 -0800141int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700142{
143 struct listnode *item;
Benoit Goby345cb062013-01-14 21:26:30 -0800144 struct listnode key_list;
145 int ret = 0;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700146
147 if (siglen != RSANUMBYTES)
148 return 0;
149
Benoit Goby345cb062013-01-14 21:26:30 -0800150 load_keys(&key_list);
151
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700152 list_for_each(item, &key_list) {
Dan Albertbac34742015-02-25 17:51:28 -0800153 adb_public_key* key = node_to_item(item, struct adb_public_key, node);
Doug Zongker515e1632013-04-10 09:22:02 -0700154 ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700155 if (ret)
Benoit Goby345cb062013-01-14 21:26:30 -0800156 break;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700157 }
158
Benoit Goby345cb062013-01-14 21:26:30 -0800159 free_keys(&key_list);
160
161 return ret;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700162}
163
Benoit Goby045a4a92013-01-15 19:59:14 -0800164static void usb_disconnected(void* unused, atransport* t)
165{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700166 D("USB disconnect");
Benoit Goby045a4a92013-01-15 19:59:14 -0800167 usb_transport = NULL;
168 needs_retry = false;
169}
170
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700171static void adb_auth_event(int fd, unsigned events, void *data)
172{
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700173 char response[2];
174 int ret;
175
176 if (events & FDE_READ) {
177 ret = unix_read(fd, response, sizeof(response));
Vince Harronaf436b12014-09-25 21:51:15 -0700178 if (ret <= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700179 D("Framework disconnect");
Benoit Goby045a4a92013-01-15 19:59:14 -0800180 if (usb_transport)
181 fdevent_remove(&usb_transport->auth_fde);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700182 framework_fd = -1;
183 }
184 else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
Benoit Goby045a4a92013-01-15 19:59:14 -0800185 if (usb_transport)
186 adb_auth_verified(usb_transport);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700187 }
188 }
189}
190
191void adb_auth_confirm_key(unsigned char *key, size_t len, atransport *t)
192{
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100193 char msg[MAX_PAYLOAD_V1];
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700194 int ret;
195
Benoit Gobyb66356c2013-04-01 17:39:06 -0700196 if (!usb_transport) {
197 usb_transport = t;
Yabin Cuib3298242015-08-28 15:09:44 -0700198 t->AddDisconnect(&usb_disconnect);
Benoit Gobyb66356c2013-04-01 17:39:06 -0700199 }
Benoit Goby045a4a92013-01-15 19:59:14 -0800200
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700201 if (framework_fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700202 D("Client not connected");
Benoit Goby045a4a92013-01-15 19:59:14 -0800203 needs_retry = true;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700204 return;
205 }
206
207 if (key[len - 1] != '\0') {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700208 D("Key must be a null-terminated string");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700209 return;
210 }
211
212 ret = snprintf(msg, sizeof(msg), "PK%s", key);
213 if (ret >= (signed)sizeof(msg)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700214 D("Key too long. ret=%d", ret);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700215 return;
216 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700217 D("Sending '%s'", msg);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700218
219 ret = unix_write(framework_fd, msg, ret);
220 if (ret < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700221 D("Failed to write PK, errno=%d", errno);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700222 return;
223 }
224
225 fdevent_install(&t->auth_fde, framework_fd, adb_auth_event, t);
226 fdevent_add(&t->auth_fde, FDE_READ);
227}
228
229static void adb_auth_listener(int fd, unsigned events, void *data)
230{
Erik Kline7e16cc12015-12-01 17:27:59 +0900231 sockaddr_storage addr;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700232 socklen_t alen;
233 int s;
234
235 alen = sizeof(addr);
236
Erik Kline7e16cc12015-12-01 17:27:59 +0900237 s = adb_socket_accept(fd, reinterpret_cast<sockaddr*>(&addr), &alen);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700238 if (s < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700239 D("Failed to accept: errno=%d", errno);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700240 return;
241 }
242
243 framework_fd = s;
Benoit Goby045a4a92013-01-15 19:59:14 -0800244
245 if (needs_retry) {
246 needs_retry = false;
247 send_auth_request(usb_transport);
248 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700249}
250
Pavel Labath64d9adc2015-03-17 11:03:36 -0700251void adbd_cloexec_auth_socket() {
252 int fd = android_get_control_socket("adbd");
253 if (fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700254 D("Failed to get adbd socket");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700255 return;
256 }
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700257 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath64d9adc2015-03-17 11:03:36 -0700258}
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700259
Pavel Labath64d9adc2015-03-17 11:03:36 -0700260void adbd_auth_init(void) {
261 int fd = android_get_control_socket("adbd");
262 if (fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700263 D("Failed to get adbd socket");
Pavel Labath64d9adc2015-03-17 11:03:36 -0700264 return;
265 }
266
267 if (listen(fd, 4) == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700268 D("Failed to listen on '%d'", fd);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700269 return;
270 }
271
272 fdevent_install(&listener_fde, fd, adb_auth_listener, NULL);
273 fdevent_add(&listener_fde, FDE_READ);
274}