blob: 128c3dffee8bc7c0ae1e13a52aab3623f7f71b2a [file] [log] [blame]
Benoit Goby2cc19e42012-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG AUTH
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "adb_auth.h"
21
Dan Albertb302d122015-02-24 15:51:19 -080022#include <resolv.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070023#include <stdio.h>
24#include <string.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070025
Dan Albertb302d122015-02-24 15:51:19 -080026#include "cutils/list.h"
27#include "cutils/sockets.h"
Benoit Goby2cc19e42012-04-12 12:23:49 -070028#include "mincrypt/rsa.h"
Doug Zongker25129a52013-04-10 09:22:02 -070029#include "mincrypt/sha.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070030
31#include "adb.h"
32#include "fdevent.h"
Dan Albertb302d122015-02-24 15:51:19 -080033#include "transport.h"
Benoit Goby2cc19e42012-04-12 12:23:49 -070034
Benoit Goby2cc19e42012-04-12 12:23:49 -070035struct adb_public_key {
36 struct listnode node;
37 RSAPublicKey key;
38};
39
Dan Albertf30d73c2015-02-25 17:51:28 -080040static const char *key_paths[] = {
Benoit Goby2cc19e42012-04-12 12:23:49 -070041 "/adb_keys",
42 "/data/misc/adb/adb_keys",
43 NULL
44};
45
46static fdevent listener_fde;
Josh Gao7a8c7cb2016-02-23 18:05:57 -080047static fdevent framework_fde;
Benoit Goby2cc19e42012-04-12 12:23:49 -070048static int framework_fd = -1;
49
Benoit Gobyd592d6c2013-01-15 19:59:14 -080050static void usb_disconnected(void* unused, atransport* t);
Yabin Cui2d4c1982015-08-28 15:09:44 -070051static struct adisconnect usb_disconnect = { usb_disconnected, nullptr};
Benoit Gobyd592d6c2013-01-15 19:59:14 -080052static atransport* usb_transport;
53static bool needs_retry = false;
Benoit Goby2cc19e42012-04-12 12:23:49 -070054
55static void read_keys(const char *file, struct listnode *list)
56{
Benoit Goby2cc19e42012-04-12 12:23:49 -070057 FILE *f;
Tamas Berghammera1c60c02015-07-13 19:12:28 +010058 char buf[MAX_PAYLOAD_V1];
Benoit Goby2cc19e42012-04-12 12:23:49 -070059 char *sep;
60 int ret;
61
Nick Kralevich777523e2014-07-18 20:57:35 -070062 f = fopen(file, "re");
Benoit Goby2cc19e42012-04-12 12:23:49 -070063 if (!f) {
Yabin Cui815ad882015-09-02 17:44:28 -070064 D("Can't open '%s'", file);
Benoit Goby2cc19e42012-04-12 12:23:49 -070065 return;
66 }
67
68 while (fgets(buf, sizeof(buf), f)) {
69 /* Allocate 4 extra bytes to decode the base64 data in-place */
Dan Albertf30d73c2015-02-25 17:51:28 -080070 auto key = reinterpret_cast<adb_public_key*>(
71 calloc(1, sizeof(adb_public_key) + 4));
72 if (key == nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -070073 D("Can't malloc key");
Benoit Goby2cc19e42012-04-12 12:23:49 -070074 break;
75 }
76
77 sep = strpbrk(buf, " \t");
78 if (sep)
79 *sep = '\0';
80
81 ret = __b64_pton(buf, (u_char *)&key->key, sizeof(key->key) + 4);
82 if (ret != sizeof(key->key)) {
Yabin Cui815ad882015-09-02 17:44:28 -070083 D("%s: Invalid base64 data ret=%d", file, ret);
Benoit Goby2cc19e42012-04-12 12:23:49 -070084 free(key);
85 continue;
86 }
87
88 if (key->key.len != RSANUMWORDS) {
Yabin Cui815ad882015-09-02 17:44:28 -070089 D("%s: Invalid key len %d", file, key->key.len);
Benoit Goby2cc19e42012-04-12 12:23:49 -070090 free(key);
91 continue;
92 }
93
94 list_add_tail(list, &key->node);
95 }
96
97 fclose(f);
98}
99
100static void free_keys(struct listnode *list)
101{
102 struct listnode *item;
103
104 while (!list_empty(list)) {
105 item = list_head(list);
106 list_remove(item);
107 free(node_to_item(item, struct adb_public_key, node));
108 }
109}
110
Benoit Gobyd84bc662013-01-14 21:26:30 -0800111static void load_keys(struct listnode *list)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700112{
Dan Albertf30d73c2015-02-25 17:51:28 -0800113 const char* path;
114 const char** paths = key_paths;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700115 struct stat buf;
116
Benoit Gobyd84bc662013-01-14 21:26:30 -0800117 list_init(list);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700118
119 while ((path = *paths++)) {
120 if (!stat(path, &buf)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700121 D("Loading keys from '%s'", path);
Benoit Gobyd84bc662013-01-14 21:26:30 -0800122 read_keys(path, list);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700123 }
124 }
125}
126
127int adb_auth_generate_token(void *token, size_t token_size)
128{
129 FILE *f;
130 int ret;
131
Nick Kralevich777523e2014-07-18 20:57:35 -0700132 f = fopen("/dev/urandom", "re");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700133 if (!f)
134 return 0;
135
136 ret = fread(token, token_size, 1, f);
137
138 fclose(f);
139 return ret * token_size;
140}
141
Dan Albertf30d73c2015-02-25 17:51:28 -0800142int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700143{
144 struct listnode *item;
Benoit Gobyd84bc662013-01-14 21:26:30 -0800145 struct listnode key_list;
146 int ret = 0;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700147
148 if (siglen != RSANUMBYTES)
149 return 0;
150
Benoit Gobyd84bc662013-01-14 21:26:30 -0800151 load_keys(&key_list);
152
Benoit Goby2cc19e42012-04-12 12:23:49 -0700153 list_for_each(item, &key_list) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800154 adb_public_key* key = node_to_item(item, struct adb_public_key, node);
Doug Zongker25129a52013-04-10 09:22:02 -0700155 ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700156 if (ret)
Benoit Gobyd84bc662013-01-14 21:26:30 -0800157 break;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700158 }
159
Benoit Gobyd84bc662013-01-14 21:26:30 -0800160 free_keys(&key_list);
161
162 return ret;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700163}
164
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800165static void usb_disconnected(void* unused, atransport* t) {
Yabin Cui815ad882015-09-02 17:44:28 -0700166 D("USB disconnect");
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800167 usb_transport = NULL;
168 needs_retry = false;
169}
170
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800171static void framework_disconnected() {
172 D("Framework disconnect");
173 fdevent_remove(&framework_fde);
174 framework_fd = -1;
175}
176
177static void adb_auth_event(int fd, unsigned events, void*) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700178 char response[2];
179 int ret;
180
181 if (events & FDE_READ) {
182 ret = unix_read(fd, response, sizeof(response));
Vince Harron82125422014-09-25 21:51:15 -0700183 if (ret <= 0) {
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800184 framework_disconnected();
185 } else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
186 if (usb_transport) {
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800187 adb_auth_verified(usb_transport);
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800188 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700189 }
190 }
191}
192
193void adb_auth_confirm_key(unsigned char *key, size_t len, atransport *t)
194{
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100195 char msg[MAX_PAYLOAD_V1];
Benoit Goby2cc19e42012-04-12 12:23:49 -0700196 int ret;
197
Benoit Gobyc0028882013-04-01 17:39:06 -0700198 if (!usb_transport) {
199 usb_transport = t;
Yabin Cui2d4c1982015-08-28 15:09:44 -0700200 t->AddDisconnect(&usb_disconnect);
Benoit Gobyc0028882013-04-01 17:39:06 -0700201 }
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800202
Benoit Goby2cc19e42012-04-12 12:23:49 -0700203 if (framework_fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700204 D("Client not connected");
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800205 needs_retry = true;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700206 return;
207 }
208
209 if (key[len - 1] != '\0') {
Yabin Cui815ad882015-09-02 17:44:28 -0700210 D("Key must be a null-terminated string");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700211 return;
212 }
213
214 ret = snprintf(msg, sizeof(msg), "PK%s", key);
215 if (ret >= (signed)sizeof(msg)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700216 D("Key too long. ret=%d", ret);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700217 return;
218 }
Yabin Cui815ad882015-09-02 17:44:28 -0700219 D("Sending '%s'", msg);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700220
221 ret = unix_write(framework_fd, msg, ret);
222 if (ret < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700223 D("Failed to write PK, errno=%d", errno);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700224 return;
225 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700226}
227
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800228static void adb_auth_listener(int fd, unsigned events, void* data) {
Erik Kline3c8d44d2015-12-01 17:27:59 +0900229 sockaddr_storage addr;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700230 socklen_t alen;
231 int s;
232
233 alen = sizeof(addr);
234
Erik Kline3c8d44d2015-12-01 17:27:59 +0900235 s = adb_socket_accept(fd, reinterpret_cast<sockaddr*>(&addr), &alen);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700236 if (s < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700237 D("Failed to accept: errno=%d", errno);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700238 return;
239 }
240
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800241 if (framework_fd >= 0) {
Josh Gao5ca49db2016-02-25 14:11:32 -0800242 LOG(WARNING) << "adb received framework auth socket connection again";
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800243 framework_disconnected();
244 }
245
Benoit Goby2cc19e42012-04-12 12:23:49 -0700246 framework_fd = s;
Josh Gao7a8c7cb2016-02-23 18:05:57 -0800247 fdevent_install(&framework_fde, framework_fd, adb_auth_event, nullptr);
248 fdevent_add(&framework_fde, FDE_READ);
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800249
250 if (needs_retry) {
251 needs_retry = false;
252 send_auth_request(usb_transport);
253 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700254}
255
Pavel Labath0bdb8672015-03-17 11:03:36 -0700256void adbd_cloexec_auth_socket() {
257 int fd = android_get_control_socket("adbd");
258 if (fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700259 D("Failed to get adbd socket");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700260 return;
261 }
Nick Kralevich777523e2014-07-18 20:57:35 -0700262 fcntl(fd, F_SETFD, FD_CLOEXEC);
Pavel Labath0bdb8672015-03-17 11:03:36 -0700263}
Benoit Goby2cc19e42012-04-12 12:23:49 -0700264
Pavel Labath0bdb8672015-03-17 11:03:36 -0700265void adbd_auth_init(void) {
266 int fd = android_get_control_socket("adbd");
267 if (fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700268 D("Failed to get adbd socket");
Pavel Labath0bdb8672015-03-17 11:03:36 -0700269 return;
270 }
271
272 if (listen(fd, 4) == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700273 D("Failed to listen on '%d'", fd);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700274 return;
275 }
276
277 fdevent_install(&listener_fde, fd, adb_auth_listener, NULL);
278 fdevent_add(&listener_fde, FDE_READ);
279}