blob: ab641eb4f308b8dc12129a7e2253e90cc6279a71 [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
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070022#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080023#include <stdlib.h>
Dan Albert33134262015-03-19 15:21:08 -070024#include <string.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070025
26#ifdef _WIN32
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080027# ifndef WIN32_LEAN_AND_MEAN
28# define WIN32_LEAN_AND_MEAN
29# endif
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070030# include "windows.h"
31# include "shlobj.h"
32#else
33# include <sys/types.h>
34# include <sys/stat.h>
35# include <unistd.h>
36#endif
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070037
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070038#include "adb.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070039
David Pursell5f787ed2016-01-27 08:52:53 -080040#include <android-base/errors.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080041#include <android-base/strings.h>
Mattias Nissler097b6bb2016-03-31 16:32:09 +020042#include <crypto_utils/android_pubkey.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070043#include <cutils/list.h>
44
45#include <openssl/evp.h>
46#include <openssl/objects.h>
47#include <openssl/pem.h>
48#include <openssl/rsa.h>
49#include <openssl/sha.h>
50
Adam Langley179d9d62014-09-03 14:34:47 -070051#if defined(OPENSSL_IS_BORINGSSL)
52#include <openssl/base64.h>
53#endif
54
Dan Albert286bb6d2015-07-09 20:35:09 +000055#define ANDROID_PATH ".android"
56#define ADB_KEY_FILE "adbkey"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070057
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070058struct adb_private_key {
59 struct listnode node;
60 RSA *rsa;
61};
62
63static struct listnode key_list;
64
65
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070066static void get_user_info(char *buf, size_t len)
67{
68 char hostname[1024], username[1024];
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080069 int ret = -1;
70
71 if (getenv("HOSTNAME") != NULL) {
72 strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
73 hostname[sizeof(hostname)-1] = '\0';
74 ret = 0;
75 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070076
77#ifndef _WIN32
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070078 if (ret < 0)
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080079 ret = gethostname(hostname, sizeof(hostname));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070080#endif
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080081 if (ret < 0)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070082 strcpy(hostname, "unknown");
83
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080084 ret = -1;
85
86 if (getenv("LOGNAME") != NULL) {
87 strncpy(username, getenv("LOGNAME"), sizeof(username));
88 username[sizeof(username)-1] = '\0';
89 ret = 0;
90 }
91
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070092#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070093 if (ret < 0)
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080094 ret = getlogin_r(username, sizeof(username));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070095#endif
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080096 if (ret < 0)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070097 strcpy(username, "unknown");
98
99 ret = snprintf(buf, len, " %s@%s", username, hostname);
100 if (ret >= (signed)len)
101 buf[len - 1] = '\0';
102}
103
104static int write_public_keyfile(RSA *private_key, const char *private_key_path)
105{
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200106 uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE];
107 uint8_t* base64_key_data = nullptr;
108 size_t base64_key_length = 0;
Adam Langley179d9d62014-09-03 14:34:47 -0700109 FILE *outfile = NULL;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100110 char path[PATH_MAX], info[MAX_PAYLOAD_V1];
Adam Langley179d9d62014-09-03 14:34:47 -0700111 int ret = 0;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700112
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200113 if (!android_pubkey_encode(private_key, binary_key_data,
114 sizeof(binary_key_data))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700115 D("Failed to convert to publickey");
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200116 goto out;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700117 }
118
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700119 D("Writing public key to '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700120
Adam Langley179d9d62014-09-03 14:34:47 -0700121#if defined(OPENSSL_IS_BORINGSSL)
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200122 if (!EVP_EncodedLength(&base64_key_length, sizeof(binary_key_data))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700123 D("Public key too large to base64 encode");
Adam Langley179d9d62014-09-03 14:34:47 -0700124 goto out;
125 }
126#else
127 /* While we switch from OpenSSL to BoringSSL we have to implement
128 * |EVP_EncodedLength| here. */
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200129 base64_key_length = 1 + ((sizeof(binary_key_data) + 2) / 3 * 4);
Adam Langley179d9d62014-09-03 14:34:47 -0700130#endif
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700131
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200132 base64_key_data = new uint8_t[base64_key_length];
133 if (base64_key_data == nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700134 D("Allocation failure");
Adam Langley179d9d62014-09-03 14:34:47 -0700135 goto out;
136 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700137
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200138 base64_key_length = EVP_EncodeBlock(base64_key_data, binary_key_data,
139 sizeof(binary_key_data));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700140 get_user_info(info, sizeof(info));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700141
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200142 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
143 (int)sizeof(path)) {
144 D("Path too long while writing public key");
145 goto out;
146 }
147
148 outfile = fopen(path, "w");
149 if (!outfile) {
150 D("Failed to open '%s'", path);
151 goto out;
152 }
153
154 if (fwrite(base64_key_data, base64_key_length, 1, outfile) != 1 ||
Adam Langley179d9d62014-09-03 14:34:47 -0700155 fwrite(info, strlen(info), 1, outfile) != 1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700156 D("Write error while writing public key");
Adam Langley179d9d62014-09-03 14:34:47 -0700157 goto out;
158 }
159
160 ret = 1;
161
162 out:
163 if (outfile != NULL) {
164 fclose(outfile);
165 }
Mattias Nissler097b6bb2016-03-31 16:32:09 +0200166 delete[] base64_key_data;
Adam Langley179d9d62014-09-03 14:34:47 -0700167 return ret;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700168}
169
170static int generate_key(const char *file)
171{
172 EVP_PKEY* pkey = EVP_PKEY_new();
173 BIGNUM* exponent = BN_new();
174 RSA* rsa = RSA_new();
Benoit Goby64b31032012-08-31 12:14:21 -0700175 mode_t old_mask;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700176 FILE *f = NULL;
177 int ret = 0;
178
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700179 D("generate_key '%s'", file);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700180
181 if (!pkey || !exponent || !rsa) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700182 D("Failed to allocate key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700183 goto out;
184 }
185
186 BN_set_word(exponent, RSA_F4);
187 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
188 EVP_PKEY_set1_RSA(pkey, rsa);
189
Benoit Goby64b31032012-08-31 12:14:21 -0700190 old_mask = umask(077);
191
Spencer Low9b960312015-05-07 19:08:29 -0700192 f = fopen(file, "w");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700193 if (!f) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700194 D("Failed to open '%s'", file);
Benoit Goby64b31032012-08-31 12:14:21 -0700195 umask(old_mask);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700196 goto out;
197 }
198
Benoit Goby64b31032012-08-31 12:14:21 -0700199 umask(old_mask);
200
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700201 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700202 D("Failed to write key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700203 goto out;
204 }
205
206 if (!write_public_keyfile(rsa, file)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("Failed to write public key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700208 goto out;
209 }
210
211 ret = 1;
212
213out:
214 if (f)
215 fclose(f);
216 EVP_PKEY_free(pkey);
217 RSA_free(rsa);
218 BN_free(exponent);
219 return ret;
220}
221
222static int read_key(const char *file, struct listnode *list)
223{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700224 D("read_key '%s'", file);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700225
Spencer Low9b960312015-05-07 19:08:29 -0700226 FILE* fp = fopen(file, "r");
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700227 if (!fp) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700228 D("Failed to open '%s': %s", file, strerror(errno));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700229 return 0;
230 }
231
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700232 adb_private_key* key = new adb_private_key;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700233 key->rsa = RSA_new();
234
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700235 if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700236 D("Failed to read key");
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700237 fclose(fp);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700238 RSA_free(key->rsa);
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700239 delete key;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700240 return 0;
241 }
242
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700243 fclose(fp);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700244 list_add_tail(list, &key->node);
245 return 1;
246}
247
Dan Albert286bb6d2015-07-09 20:35:09 +0000248static int get_user_keyfilepath(char *filename, size_t len)
249{
250 const char *format, *home;
251 char android_dir[PATH_MAX];
252 struct stat buf;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700253#ifdef _WIN32
Spencer Lowcf4ff642015-05-11 01:08:48 -0700254 std::string home_str;
Dan Albert286bb6d2015-07-09 20:35:09 +0000255 home = getenv("ANDROID_SDK_HOME");
256 if (!home) {
Spencer Lowcf4ff642015-05-11 01:08:48 -0700257 WCHAR path[MAX_PATH];
Spencer Low8df90322015-08-02 18:50:17 -0700258 const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
259 if (FAILED(hr)) {
David Pursell5f787ed2016-01-27 08:52:53 -0800260 D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str());
Spencer Lowcf4ff642015-05-11 01:08:48 -0700261 return -1;
262 }
Spencer Lowd21dc822015-11-12 15:20:15 -0800263 if (!android::base::WideToUTF8(path, &home_str)) {
264 return -1;
265 }
Spencer Lowcf4ff642015-05-11 01:08:48 -0700266 home = home_str.c_str();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700267 }
Dan Albert286bb6d2015-07-09 20:35:09 +0000268 format = "%s\\%s";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700269#else
Dan Albert286bb6d2015-07-09 20:35:09 +0000270 home = getenv("HOME");
271 if (!home)
272 return -1;
273 format = "%s/%s";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700274#endif
275
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700276 D("home '%s'", home);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700277
Dan Albert286bb6d2015-07-09 20:35:09 +0000278 if (snprintf(android_dir, sizeof(android_dir), format, home,
279 ANDROID_PATH) >= (int)sizeof(android_dir))
280 return -1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700281
Dan Albert286bb6d2015-07-09 20:35:09 +0000282 if (stat(android_dir, &buf)) {
283 if (adb_mkdir(android_dir, 0750) < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700284 D("Cannot mkdir '%s'", android_dir);
Dan Albert286bb6d2015-07-09 20:35:09 +0000285 return -1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700286 }
287 }
288
Dan Albert286bb6d2015-07-09 20:35:09 +0000289 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700290}
291
292static int get_user_key(struct listnode *list)
293{
Dan Albert286bb6d2015-07-09 20:35:09 +0000294 struct stat buf;
295 char path[PATH_MAX];
296 int ret;
297
298 ret = get_user_keyfilepath(path, sizeof(path));
299 if (ret < 0 || ret >= (signed)sizeof(path)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700300 D("Error getting user key filename");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700301 return 0;
302 }
303
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700304 D("user key '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700305
Dan Albert286bb6d2015-07-09 20:35:09 +0000306 if (stat(path, &buf) == -1) {
307 if (!generate_key(path)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700308 D("Failed to generate new key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700309 return 0;
310 }
311 }
312
Dan Albert286bb6d2015-07-09 20:35:09 +0000313 return read_key(path, list);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700314}
315
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700316static void get_vendor_keys(struct listnode* key_list) {
317 const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
318 if (adb_keys_path == nullptr) {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700319 return;
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700320 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700321
Elliott Hughes65fe2512015-10-07 15:59:35 -0700322 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700323 if (!read_key(path.c_str(), key_list)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700324 D("Failed to read '%s'", path.c_str());
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700325 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700326 }
327}
328
Dan Albertbac34742015-02-25 17:51:28 -0800329int adb_auth_sign(void *node, const unsigned char* token, size_t token_size,
330 unsigned char* sig)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700331{
332 unsigned int len;
333 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
334
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000335 if (token_size != TOKEN_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700336 D("Unexpected token size %zd", token_size);
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000337 return 0;
338 }
339
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700340 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
341 return 0;
342 }
343
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700344 D("adb_auth_sign len=%d", len);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700345 return (int)len;
346}
347
348void *adb_auth_nextkey(void *current)
349{
350 struct listnode *item;
351
352 if (list_empty(&key_list))
353 return NULL;
354
355 if (!current)
356 return list_head(&key_list);
357
358 list_for_each(item, &key_list) {
359 if (item == current) {
360 /* current is the last item, we tried all the keys */
361 if (item->next == &key_list)
362 return NULL;
363 return item->next;
364 }
365 }
366
367 return NULL;
368}
369
370int adb_auth_get_userkey(unsigned char *data, size_t len)
371{
Dan Albert286bb6d2015-07-09 20:35:09 +0000372 char path[PATH_MAX];
373 int ret = get_user_keyfilepath(path, sizeof(path) - 4);
374 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700375 D("Error getting user key filename");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700376 return 0;
377 }
Dan Albert286bb6d2015-07-09 20:35:09 +0000378 strcat(path, ".pub");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700379
Dan Albertbac34742015-02-25 17:51:28 -0800380 // TODO(danalbert): ReadFileToString
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700381 // Note that on Windows, load_file() does not do CR/LF translation, but
382 // ReadFileToString() uses the C Runtime which uses CR/LF translation by
383 // default (by is overridable with _setmode()).
Dan Albertbac34742015-02-25 17:51:28 -0800384 unsigned size;
Dan Albert286bb6d2015-07-09 20:35:09 +0000385 char* file_data = reinterpret_cast<char*>(load_file(path, &size));
Dan Albertbac34742015-02-25 17:51:28 -0800386 if (file_data == nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700387 D("Can't load '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700388 return 0;
389 }
390
Dan Albertbac34742015-02-25 17:51:28 -0800391 if (len < (size_t)(size + 1)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700392 D("%s: Content too large ret=%d", path, size);
Dan Albertbac34742015-02-25 17:51:28 -0800393 free(file_data);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700394 return 0;
395 }
396
Dan Albertbac34742015-02-25 17:51:28 -0800397 memcpy(data, file_data, size);
398 free(file_data);
399 file_data = nullptr;
400 data[size] = '\0';
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700401
Dan Albertbac34742015-02-25 17:51:28 -0800402 return size + 1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700403}
404
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800405int adb_auth_keygen(const char* filename) {
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800406 return (generate_key(filename) == 0);
407}
408
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700409void adb_auth_init(void)
410{
411 int ret;
412
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700413 D("adb_auth_init");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700414
415 list_init(&key_list);
416
417 ret = get_user_key(&key_list);
418 if (!ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700419 D("Failed to get user key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700420 return;
421 }
422
423 get_vendor_keys(&key_list);
424}