blob: a85919989475a91e0a559f34ca35e26d69e84789 [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
17#include <stdio.h>
Christopher Ferris054d1702014-11-06 14:34:24 -080018#include <stdlib.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070019
20#ifdef _WIN32
Christopher Ferris054d1702014-11-06 14:34:24 -080021# ifndef WIN32_LEAN_AND_MEAN
22# define WIN32_LEAN_AND_MEAN
23# endif
Benoit Goby2cc19e42012-04-12 12:23:49 -070024# include "windows.h"
25# include "shlobj.h"
26#else
27# include <sys/types.h>
28# include <sys/stat.h>
29# include <unistd.h>
30#endif
31#include <string.h>
32
33#include "sysdeps.h"
34#include "adb.h"
35#include "adb_auth.h"
36
37/* HACK: we need the RSAPublicKey struct
38 * but RSA_verify conflits with openssl */
39#define RSA_verify RSA_verify_mincrypt
40#include "mincrypt/rsa.h"
41#undef RSA_verify
42
43#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 Langley29f6cdb2014-09-03 14:34:47 -070051#if defined(OPENSSL_IS_BORINGSSL)
52#include <openssl/base64.h>
53#endif
54
Benoit Goby2cc19e42012-04-12 12:23:49 -070055#define TRACE_TAG TRACE_AUTH
56
57#define ANDROID_PATH ".android"
Benoit Gobycb37c502012-08-31 12:14:21 -070058#define ADB_KEY_FILE "adbkey"
Benoit Goby2cc19e42012-04-12 12:23:49 -070059
60
61struct adb_private_key {
62 struct listnode node;
63 RSA *rsa;
64};
65
66static struct listnode key_list;
67
68
69/* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
70static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
71{
72 int ret = 1;
73 unsigned int i;
74
75 BN_CTX* ctx = BN_CTX_new();
76 BIGNUM* r32 = BN_new();
77 BIGNUM* rr = BN_new();
78 BIGNUM* r = BN_new();
79 BIGNUM* rem = BN_new();
80 BIGNUM* n = BN_new();
81 BIGNUM* n0inv = BN_new();
82
83 if (RSA_size(rsa) != RSANUMBYTES) {
84 ret = 0;
85 goto out;
86 }
87
88 BN_set_bit(r32, 32);
89 BN_copy(n, rsa->n);
90 BN_set_bit(r, RSANUMWORDS * 32);
91 BN_mod_sqr(rr, r, n, ctx);
92 BN_div(NULL, rem, n, r32, ctx);
93 BN_mod_inverse(n0inv, rem, r32, ctx);
94
95 pkey->len = RSANUMWORDS;
96 pkey->n0inv = 0 - BN_get_word(n0inv);
97 for (i = 0; i < RSANUMWORDS; i++) {
98 BN_div(rr, rem, rr, r32, ctx);
99 pkey->rr[i] = BN_get_word(rem);
100 BN_div(n, rem, n, r32, ctx);
101 pkey->n[i] = BN_get_word(rem);
102 }
103 pkey->exponent = BN_get_word(rsa->e);
104
105out:
106 BN_free(n0inv);
107 BN_free(n);
108 BN_free(rem);
109 BN_free(r);
110 BN_free(rr);
111 BN_free(r32);
112 BN_CTX_free(ctx);
113
114 return ret;
115}
116
117static void get_user_info(char *buf, size_t len)
118{
119 char hostname[1024], username[1024];
Nick Kralevich6183c962014-11-13 15:17:29 -0800120 int ret = -1;
121
122 if (getenv("HOSTNAME") != NULL) {
123 strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
124 hostname[sizeof(hostname)-1] = '\0';
125 ret = 0;
126 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700127
128#ifndef _WIN32
Benoit Goby2cc19e42012-04-12 12:23:49 -0700129 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -0800130 ret = gethostname(hostname, sizeof(hostname));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700131#endif
Nick Kralevich6183c962014-11-13 15:17:29 -0800132 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700133 strcpy(hostname, "unknown");
134
Nick Kralevich6183c962014-11-13 15:17:29 -0800135 ret = -1;
136
137 if (getenv("LOGNAME") != NULL) {
138 strncpy(username, getenv("LOGNAME"), sizeof(username));
139 username[sizeof(username)-1] = '\0';
140 ret = 0;
141 }
142
Benoit Goby2cc19e42012-04-12 12:23:49 -0700143#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Benoit Goby2cc19e42012-04-12 12:23:49 -0700144 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -0800145 ret = getlogin_r(username, sizeof(username));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700146#endif
Nick Kralevich6183c962014-11-13 15:17:29 -0800147 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700148 strcpy(username, "unknown");
149
150 ret = snprintf(buf, len, " %s@%s", username, hostname);
151 if (ret >= (signed)len)
152 buf[len - 1] = '\0';
153}
154
155static int write_public_keyfile(RSA *private_key, const char *private_key_path)
156{
157 RSAPublicKey pkey;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700158 FILE *outfile = NULL;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700159 char path[PATH_MAX], info[MAX_PAYLOAD];
Adam Langley29f6cdb2014-09-03 14:34:47 -0700160 uint8_t *encoded = NULL;
161 size_t encoded_length;
162 int ret = 0;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700163
Adam Langley29f6cdb2014-09-03 14:34:47 -0700164 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
165 (int)sizeof(path)) {
166 D("Path too long while writing public key\n");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700167 return 0;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700168 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700169
Adam Langley29f6cdb2014-09-03 14:34:47 -0700170 if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700171 D("Failed to convert to publickey\n");
172 return 0;
173 }
174
Adam Langley29f6cdb2014-09-03 14:34:47 -0700175 outfile = fopen(path, "w");
176 if (!outfile) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700177 D("Failed to open '%s'\n", path);
178 return 0;
179 }
180
181 D("Writing public key to '%s'\n", path);
182
Adam Langley29f6cdb2014-09-03 14:34:47 -0700183#if defined(OPENSSL_IS_BORINGSSL)
184 if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
185 D("Public key too large to base64 encode");
186 goto out;
187 }
188#else
189 /* While we switch from OpenSSL to BoringSSL we have to implement
190 * |EVP_EncodedLength| here. */
191 encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
192#endif
Benoit Goby2cc19e42012-04-12 12:23:49 -0700193
Adam Langley29f6cdb2014-09-03 14:34:47 -0700194 encoded = malloc(encoded_length);
195 if (encoded == NULL) {
196 D("Allocation failure");
197 goto out;
198 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700199
Adam Langley29f6cdb2014-09-03 14:34:47 -0700200 encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700201 get_user_info(info, sizeof(info));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700202
Adam Langley29f6cdb2014-09-03 14:34:47 -0700203 if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
204 fwrite(info, strlen(info), 1, outfile) != 1) {
205 D("Write error while writing public key");
206 goto out;
207 }
208
209 ret = 1;
210
211 out:
212 if (outfile != NULL) {
213 fclose(outfile);
214 }
215 if (encoded != NULL) {
216 free(encoded);
217 }
218 return ret;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700219}
220
221static int generate_key(const char *file)
222{
223 EVP_PKEY* pkey = EVP_PKEY_new();
224 BIGNUM* exponent = BN_new();
225 RSA* rsa = RSA_new();
Benoit Gobycb37c502012-08-31 12:14:21 -0700226 mode_t old_mask;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700227 FILE *f = NULL;
228 int ret = 0;
229
230 D("generate_key '%s'\n", file);
231
232 if (!pkey || !exponent || !rsa) {
233 D("Failed to allocate key\n");
234 goto out;
235 }
236
237 BN_set_word(exponent, RSA_F4);
238 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
239 EVP_PKEY_set1_RSA(pkey, rsa);
240
Benoit Gobycb37c502012-08-31 12:14:21 -0700241 old_mask = umask(077);
242
Benoit Goby2cc19e42012-04-12 12:23:49 -0700243 f = fopen(file, "w");
244 if (!f) {
245 D("Failed to open '%s'\n", file);
Benoit Gobycb37c502012-08-31 12:14:21 -0700246 umask(old_mask);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700247 goto out;
248 }
249
Benoit Gobycb37c502012-08-31 12:14:21 -0700250 umask(old_mask);
251
Benoit Goby2cc19e42012-04-12 12:23:49 -0700252 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
253 D("Failed to write key\n");
254 goto out;
255 }
256
257 if (!write_public_keyfile(rsa, file)) {
258 D("Failed to write public key\n");
259 goto out;
260 }
261
262 ret = 1;
263
264out:
265 if (f)
266 fclose(f);
267 EVP_PKEY_free(pkey);
268 RSA_free(rsa);
269 BN_free(exponent);
270 return ret;
271}
272
273static int read_key(const char *file, struct listnode *list)
274{
275 struct adb_private_key *key;
276 FILE *f;
277
278 D("read_key '%s'\n", file);
279
280 f = fopen(file, "r");
281 if (!f) {
282 D("Failed to open '%s'\n", file);
283 return 0;
284 }
285
286 key = malloc(sizeof(*key));
287 if (!key) {
288 D("Failed to alloc key\n");
289 fclose(f);
290 return 0;
291 }
292 key->rsa = RSA_new();
293
294 if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) {
295 D("Failed to read key\n");
296 fclose(f);
297 RSA_free(key->rsa);
298 free(key);
299 return 0;
300 }
301
302 fclose(f);
303 list_add_tail(list, &key->node);
304 return 1;
305}
306
307static int get_user_keyfilepath(char *filename, size_t len)
308{
309 const char *format, *home;
310 char android_dir[PATH_MAX];
311 struct stat buf;
312#ifdef _WIN32
313 char path[PATH_MAX];
314 home = getenv("ANDROID_SDK_HOME");
315 if (!home) {
316 SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path);
317 home = path;
318 }
319 format = "%s\\%s";
320#else
321 home = getenv("HOME");
322 if (!home)
323 return -1;
324 format = "%s/%s";
325#endif
326
327 D("home '%s'\n", home);
328
329 if (snprintf(android_dir, sizeof(android_dir), format, home,
330 ANDROID_PATH) >= (int)sizeof(android_dir))
331 return -1;
332
333 if (stat(android_dir, &buf)) {
334 if (adb_mkdir(android_dir, 0750) < 0) {
335 D("Cannot mkdir '%s'", android_dir);
336 return -1;
337 }
338 }
339
340 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
341}
342
343static int get_user_key(struct listnode *list)
344{
345 struct stat buf;
346 char path[PATH_MAX];
347 int ret;
348
349 ret = get_user_keyfilepath(path, sizeof(path));
350 if (ret < 0 || ret >= (signed)sizeof(path)) {
351 D("Error getting user key filename");
352 return 0;
353 }
354
355 D("user key '%s'\n", path);
356
357 if (stat(path, &buf) == -1) {
358 if (!generate_key(path)) {
359 D("Failed to generate new key\n");
360 return 0;
361 }
362 }
363
364 return read_key(path, list);
365}
366
367static void get_vendor_keys(struct listnode *list)
368{
369 const char *adb_keys_path;
370 char keys_path[MAX_PAYLOAD];
371 char *path;
372 char *save;
373 struct stat buf;
374
375 adb_keys_path = getenv("ADB_VENDOR_KEYS");
376 if (!adb_keys_path)
377 return;
378 strncpy(keys_path, adb_keys_path, sizeof(keys_path));
379
380 path = adb_strtok_r(keys_path, ENV_PATH_SEPARATOR_STR, &save);
381 while (path) {
382 D("Reading: '%s'\n", path);
383
384 if (stat(path, &buf))
385 D("Can't read '%s'\n", path);
386 else if (!read_key(path, list))
387 D("Failed to read '%s'\n", path);
388
389 path = adb_strtok_r(NULL, ENV_PATH_SEPARATOR_STR, &save);
390 }
391}
392
393int adb_auth_sign(void *node, void *token, size_t token_size, void *sig)
394{
395 unsigned int len;
396 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
397
398 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
399 return 0;
400 }
401
402 D("adb_auth_sign len=%d\n", len);
403 return (int)len;
404}
405
406void *adb_auth_nextkey(void *current)
407{
408 struct listnode *item;
409
410 if (list_empty(&key_list))
411 return NULL;
412
413 if (!current)
414 return list_head(&key_list);
415
416 list_for_each(item, &key_list) {
417 if (item == current) {
418 /* current is the last item, we tried all the keys */
419 if (item->next == &key_list)
420 return NULL;
421 return item->next;
422 }
423 }
424
425 return NULL;
426}
427
428int adb_auth_get_userkey(unsigned char *data, size_t len)
429{
430 char path[PATH_MAX];
431 char *file;
432 int ret;
433
434 ret = get_user_keyfilepath(path, sizeof(path) - 4);
435 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
436 D("Error getting user key filename");
437 return 0;
438 }
439 strcat(path, ".pub");
440
441 file = load_file(path, (unsigned*)&ret);
442 if (!file) {
443 D("Can't load '%s'\n", path);
444 return 0;
445 }
446
447 if (len < (size_t)(ret + 1)) {
448 D("%s: Content too large ret=%d\n", path, ret);
449 return 0;
450 }
451
452 memcpy(data, file, ret);
453 data[ret] = '\0';
454
455 return ret + 1;
456}
457
Nick Kralevich6183c962014-11-13 15:17:29 -0800458int adb_auth_keygen(const char* filename) {
459 adb_trace_mask |= (1 << TRACE_AUTH);
460 return (generate_key(filename) == 0);
461}
462
Benoit Goby2cc19e42012-04-12 12:23:49 -0700463void adb_auth_init(void)
464{
465 int ret;
466
467 D("adb_auth_init\n");
468
469 list_init(&key_list);
470
471 ret = get_user_key(&key_list);
472 if (!ret) {
473 D("Failed to get user key\n");
474 return;
475 }
476
477 get_vendor_keys(&key_list);
478}