blob: 03cebe97bde6f43f75cb8dab1d0851fd4d1509c2 [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"
Yurii Zubrytskyi70ae70a2016-05-25 15:17:10 -070021#include "adb_utils.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070022
Benoit Goby2cc19e42012-04-12 12:23:49 -070023#include <stdio.h>
Christopher Ferris054d1702014-11-06 14:34:24 -080024#include <stdlib.h>
Dan Albertdb6fe642015-03-19 15:21:08 -070025#include <string.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070026
Benoit Goby2cc19e42012-04-12 12:23:49 -070027#include "adb.h"
Benoit Goby2cc19e42012-04-12 12:23:49 -070028
David Pursellc573d522016-01-27 08:52:53 -080029#include <android-base/errors.h>
Elliott Hughese0a6e2a2016-05-26 22:43:19 -070030#include <android-base/file.h>
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -070031#include <android-base/stringprintf.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080032#include <android-base/strings.h>
Mattias Nisslera947b492016-03-31 16:32:09 +020033#include <crypto_utils/android_pubkey.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070034#include <cutils/list.h>
35
36#include <openssl/evp.h>
37#include <openssl/objects.h>
38#include <openssl/pem.h>
39#include <openssl/rsa.h>
40#include <openssl/sha.h>
41
Adam Langley29f6cdb2014-09-03 14:34:47 -070042#if defined(OPENSSL_IS_BORINGSSL)
43#include <openssl/base64.h>
44#endif
45
Dan Albert3d978e62015-07-09 20:35:09 +000046#define ANDROID_PATH ".android"
47#define ADB_KEY_FILE "adbkey"
Benoit Goby2cc19e42012-04-12 12:23:49 -070048
Benoit Goby2cc19e42012-04-12 12:23:49 -070049struct adb_private_key {
50 struct listnode node;
51 RSA *rsa;
52};
53
54static struct listnode key_list;
55
56
Benoit Goby2cc19e42012-04-12 12:23:49 -070057static void get_user_info(char *buf, size_t len)
58{
59 char hostname[1024], username[1024];
Nick Kralevich6183c962014-11-13 15:17:29 -080060 int ret = -1;
61
62 if (getenv("HOSTNAME") != NULL) {
63 strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
64 hostname[sizeof(hostname)-1] = '\0';
65 ret = 0;
66 }
Benoit Goby2cc19e42012-04-12 12:23:49 -070067
68#ifndef _WIN32
Benoit Goby2cc19e42012-04-12 12:23:49 -070069 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -080070 ret = gethostname(hostname, sizeof(hostname));
Benoit Goby2cc19e42012-04-12 12:23:49 -070071#endif
Nick Kralevich6183c962014-11-13 15:17:29 -080072 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -070073 strcpy(hostname, "unknown");
74
Nick Kralevich6183c962014-11-13 15:17:29 -080075 ret = -1;
76
77 if (getenv("LOGNAME") != NULL) {
78 strncpy(username, getenv("LOGNAME"), sizeof(username));
79 username[sizeof(username)-1] = '\0';
80 ret = 0;
81 }
82
Benoit Goby2cc19e42012-04-12 12:23:49 -070083#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Benoit Goby2cc19e42012-04-12 12:23:49 -070084 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -080085 ret = getlogin_r(username, sizeof(username));
Benoit Goby2cc19e42012-04-12 12:23:49 -070086#endif
Nick Kralevich6183c962014-11-13 15:17:29 -080087 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -070088 strcpy(username, "unknown");
89
90 ret = snprintf(buf, len, " %s@%s", username, hostname);
91 if (ret >= (signed)len)
92 buf[len - 1] = '\0';
93}
94
95static int write_public_keyfile(RSA *private_key, const char *private_key_path)
96{
Mattias Nisslera947b492016-03-31 16:32:09 +020097 uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE];
98 uint8_t* base64_key_data = nullptr;
99 size_t base64_key_length = 0;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700100 FILE *outfile = NULL;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100101 char path[PATH_MAX], info[MAX_PAYLOAD_V1];
Adam Langley29f6cdb2014-09-03 14:34:47 -0700102 int ret = 0;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700103
Mattias Nisslera947b492016-03-31 16:32:09 +0200104 if (!android_pubkey_encode(private_key, binary_key_data,
105 sizeof(binary_key_data))) {
Yabin Cui815ad882015-09-02 17:44:28 -0700106 D("Failed to convert to publickey");
Mattias Nisslera947b492016-03-31 16:32:09 +0200107 goto out;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700108 }
109
Yabin Cui815ad882015-09-02 17:44:28 -0700110 D("Writing public key to '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700111
Adam Langley29f6cdb2014-09-03 14:34:47 -0700112#if defined(OPENSSL_IS_BORINGSSL)
Mattias Nisslera947b492016-03-31 16:32:09 +0200113 if (!EVP_EncodedLength(&base64_key_length, sizeof(binary_key_data))) {
Yabin Cui815ad882015-09-02 17:44:28 -0700114 D("Public key too large to base64 encode");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700115 goto out;
116 }
117#else
118 /* While we switch from OpenSSL to BoringSSL we have to implement
119 * |EVP_EncodedLength| here. */
Mattias Nisslera947b492016-03-31 16:32:09 +0200120 base64_key_length = 1 + ((sizeof(binary_key_data) + 2) / 3 * 4);
Adam Langley29f6cdb2014-09-03 14:34:47 -0700121#endif
Benoit Goby2cc19e42012-04-12 12:23:49 -0700122
Mattias Nisslera947b492016-03-31 16:32:09 +0200123 base64_key_data = new uint8_t[base64_key_length];
124 if (base64_key_data == nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -0700125 D("Allocation failure");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700126 goto out;
127 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700128
Mattias Nisslera947b492016-03-31 16:32:09 +0200129 base64_key_length = EVP_EncodeBlock(base64_key_data, binary_key_data,
130 sizeof(binary_key_data));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700131 get_user_info(info, sizeof(info));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700132
Mattias Nisslera947b492016-03-31 16:32:09 +0200133 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
134 (int)sizeof(path)) {
135 D("Path too long while writing public key");
136 goto out;
137 }
138
139 outfile = fopen(path, "w");
140 if (!outfile) {
141 D("Failed to open '%s'", path);
142 goto out;
143 }
144
145 if (fwrite(base64_key_data, base64_key_length, 1, outfile) != 1 ||
Adam Langley29f6cdb2014-09-03 14:34:47 -0700146 fwrite(info, strlen(info), 1, outfile) != 1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700147 D("Write error while writing public key");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700148 goto out;
149 }
150
151 ret = 1;
152
153 out:
154 if (outfile != NULL) {
155 fclose(outfile);
156 }
Mattias Nisslera947b492016-03-31 16:32:09 +0200157 delete[] base64_key_data;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700158 return ret;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700159}
160
161static int generate_key(const char *file)
162{
163 EVP_PKEY* pkey = EVP_PKEY_new();
164 BIGNUM* exponent = BN_new();
165 RSA* rsa = RSA_new();
Benoit Gobycb37c502012-08-31 12:14:21 -0700166 mode_t old_mask;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700167 FILE *f = NULL;
168 int ret = 0;
169
Yabin Cui815ad882015-09-02 17:44:28 -0700170 D("generate_key '%s'", file);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700171
172 if (!pkey || !exponent || !rsa) {
Yabin Cui815ad882015-09-02 17:44:28 -0700173 D("Failed to allocate key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700174 goto out;
175 }
176
177 BN_set_word(exponent, RSA_F4);
178 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
179 EVP_PKEY_set1_RSA(pkey, rsa);
180
Benoit Gobycb37c502012-08-31 12:14:21 -0700181 old_mask = umask(077);
182
Spencer Low60bca2d2015-05-07 19:08:29 -0700183 f = fopen(file, "w");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700184 if (!f) {
Yabin Cui815ad882015-09-02 17:44:28 -0700185 D("Failed to open '%s'", file);
Benoit Gobycb37c502012-08-31 12:14:21 -0700186 umask(old_mask);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700187 goto out;
188 }
189
Benoit Gobycb37c502012-08-31 12:14:21 -0700190 umask(old_mask);
191
Benoit Goby2cc19e42012-04-12 12:23:49 -0700192 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700193 D("Failed to write key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700194 goto out;
195 }
196
197 if (!write_public_keyfile(rsa, file)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700198 D("Failed to write public key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700199 goto out;
200 }
201
202 ret = 1;
203
204out:
205 if (f)
206 fclose(f);
207 EVP_PKEY_free(pkey);
208 RSA_free(rsa);
209 BN_free(exponent);
210 return ret;
211}
212
213static int read_key(const char *file, struct listnode *list)
214{
Yabin Cui815ad882015-09-02 17:44:28 -0700215 D("read_key '%s'", file);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700216
Spencer Low60bca2d2015-05-07 19:08:29 -0700217 FILE* fp = fopen(file, "r");
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700218 if (!fp) {
Yabin Cui815ad882015-09-02 17:44:28 -0700219 D("Failed to open '%s': %s", file, strerror(errno));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700220 return 0;
221 }
222
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700223 adb_private_key* key = new adb_private_key;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700224 key->rsa = RSA_new();
225
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700226 if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700227 D("Failed to read key");
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700228 fclose(fp);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700229 RSA_free(key->rsa);
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700230 delete key;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700231 return 0;
232 }
233
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700234 fclose(fp);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700235 list_add_tail(list, &key->node);
236 return 1;
237}
238
Dan Albert3d978e62015-07-09 20:35:09 +0000239static int get_user_keyfilepath(char *filename, size_t len)
240{
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -0700241 const std::string home = adb_get_homedir_path(true);
Yurii Zubrytskyi70ae70a2016-05-25 15:17:10 -0700242 D("home '%s'", home.c_str());
Benoit Goby2cc19e42012-04-12 12:23:49 -0700243
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -0700244 const std::string android_dir =
245 android::base::StringPrintf("%s%c%s", home.c_str(),
246 OS_PATH_SEPARATOR, ANDROID_PATH);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700247
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -0700248 struct stat buf;
249 if (stat(android_dir.c_str(), &buf)) {
250 if (adb_mkdir(android_dir.c_str(), 0750) < 0) {
251 D("Cannot mkdir '%s'", android_dir.c_str());
Dan Albert3d978e62015-07-09 20:35:09 +0000252 return -1;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700253 }
254 }
255
Yurii Zubrytskyi09ecaa52016-05-26 09:46:10 -0700256 return snprintf(filename, len, "%s%c%s",
257 android_dir.c_str(), OS_PATH_SEPARATOR, ADB_KEY_FILE);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700258}
259
260static int get_user_key(struct listnode *list)
261{
Dan Albert3d978e62015-07-09 20:35:09 +0000262 struct stat buf;
263 char path[PATH_MAX];
264 int ret;
265
266 ret = get_user_keyfilepath(path, sizeof(path));
267 if (ret < 0 || ret >= (signed)sizeof(path)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700268 D("Error getting user key filename");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700269 return 0;
270 }
271
Yabin Cui815ad882015-09-02 17:44:28 -0700272 D("user key '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700273
Dan Albert3d978e62015-07-09 20:35:09 +0000274 if (stat(path, &buf) == -1) {
275 if (!generate_key(path)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700276 D("Failed to generate new key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700277 return 0;
278 }
279 }
280
Dan Albert3d978e62015-07-09 20:35:09 +0000281 return read_key(path, list);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700282}
283
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700284static void get_vendor_keys(struct listnode* key_list) {
285 const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
286 if (adb_keys_path == nullptr) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700287 return;
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700288 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700289
Elliott Hughes85952832015-10-07 15:59:35 -0700290 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700291 if (!read_key(path.c_str(), key_list)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700292 D("Failed to read '%s'", path.c_str());
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700293 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700294 }
295}
296
Dan Albertf30d73c2015-02-25 17:51:28 -0800297int adb_auth_sign(void *node, const unsigned char* token, size_t token_size,
298 unsigned char* sig)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700299{
300 unsigned int len;
301 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
302
Sami Tolvanenb92a35c2015-01-27 16:48:35 +0000303 if (token_size != TOKEN_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700304 D("Unexpected token size %zd", token_size);
Sami Tolvanenb92a35c2015-01-27 16:48:35 +0000305 return 0;
306 }
307
Benoit Goby2cc19e42012-04-12 12:23:49 -0700308 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
309 return 0;
310 }
311
Yabin Cui815ad882015-09-02 17:44:28 -0700312 D("adb_auth_sign len=%d", len);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700313 return (int)len;
314}
315
316void *adb_auth_nextkey(void *current)
317{
318 struct listnode *item;
319
320 if (list_empty(&key_list))
321 return NULL;
322
323 if (!current)
324 return list_head(&key_list);
325
326 list_for_each(item, &key_list) {
327 if (item == current) {
328 /* current is the last item, we tried all the keys */
329 if (item->next == &key_list)
330 return NULL;
331 return item->next;
332 }
333 }
334
335 return NULL;
336}
337
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700338std::string adb_auth_get_userkey() {
Dan Albert3d978e62015-07-09 20:35:09 +0000339 char path[PATH_MAX];
340 int ret = get_user_keyfilepath(path, sizeof(path) - 4);
341 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700342 D("Error getting user key filename");
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700343 return "";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700344 }
Dan Albert3d978e62015-07-09 20:35:09 +0000345 strcat(path, ".pub");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700346
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700347 std::string content;
348 if (!android::base::ReadFileToString(path, &content)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700349 D("Can't load '%s'", path);
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700350 return "";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700351 }
Elliott Hughese0a6e2a2016-05-26 22:43:19 -0700352 return content;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700353}
354
Nick Kralevich6183c962014-11-13 15:17:29 -0800355int adb_auth_keygen(const char* filename) {
Nick Kralevich6183c962014-11-13 15:17:29 -0800356 return (generate_key(filename) == 0);
357}
358
Benoit Goby2cc19e42012-04-12 12:23:49 -0700359void adb_auth_init(void)
360{
361 int ret;
362
Yabin Cui815ad882015-09-02 17:44:28 -0700363 D("adb_auth_init");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700364
365 list_init(&key_list);
366
367 ret = get_user_key(&key_list);
368 if (!ret) {
Yabin Cui815ad882015-09-02 17:44:28 -0700369 D("Failed to get user key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700370 return;
371 }
372
373 get_vendor_keys(&key_list);
374}