blob: c72fe42085045dcc07095f83a3d90070c6f684f4 [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>
18
19#ifdef _WIN32
20# define WIN32_LEAN_AND_MEAN
21# include "windows.h"
22# include "shlobj.h"
23#else
24# include <sys/types.h>
25# include <sys/stat.h>
26# include <unistd.h>
27#endif
28#include <string.h>
29
30#include "sysdeps.h"
31#include "adb.h"
32#include "adb_auth.h"
33
34/* HACK: we need the RSAPublicKey struct
35 * but RSA_verify conflits with openssl */
36#define RSA_verify RSA_verify_mincrypt
37#include "mincrypt/rsa.h"
38#undef RSA_verify
39
40#include <cutils/list.h>
41
42#include <openssl/evp.h>
43#include <openssl/objects.h>
44#include <openssl/pem.h>
45#include <openssl/rsa.h>
46#include <openssl/sha.h>
47
Adam Langley29f6cdb2014-09-03 14:34:47 -070048#if defined(OPENSSL_IS_BORINGSSL)
49#include <openssl/base64.h>
50#endif
51
Benoit Goby2cc19e42012-04-12 12:23:49 -070052#define TRACE_TAG TRACE_AUTH
53
54#define ANDROID_PATH ".android"
Benoit Gobycb37c502012-08-31 12:14:21 -070055#define ADB_KEY_FILE "adbkey"
Benoit Goby2cc19e42012-04-12 12:23:49 -070056
57
58struct adb_private_key {
59 struct listnode node;
60 RSA *rsa;
61};
62
63static struct listnode key_list;
64
65
66/* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
67static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
68{
69 int ret = 1;
70 unsigned int i;
71
72 BN_CTX* ctx = BN_CTX_new();
73 BIGNUM* r32 = BN_new();
74 BIGNUM* rr = BN_new();
75 BIGNUM* r = BN_new();
76 BIGNUM* rem = BN_new();
77 BIGNUM* n = BN_new();
78 BIGNUM* n0inv = BN_new();
79
80 if (RSA_size(rsa) != RSANUMBYTES) {
81 ret = 0;
82 goto out;
83 }
84
85 BN_set_bit(r32, 32);
86 BN_copy(n, rsa->n);
87 BN_set_bit(r, RSANUMWORDS * 32);
88 BN_mod_sqr(rr, r, n, ctx);
89 BN_div(NULL, rem, n, r32, ctx);
90 BN_mod_inverse(n0inv, rem, r32, ctx);
91
92 pkey->len = RSANUMWORDS;
93 pkey->n0inv = 0 - BN_get_word(n0inv);
94 for (i = 0; i < RSANUMWORDS; i++) {
95 BN_div(rr, rem, rr, r32, ctx);
96 pkey->rr[i] = BN_get_word(rem);
97 BN_div(n, rem, n, r32, ctx);
98 pkey->n[i] = BN_get_word(rem);
99 }
100 pkey->exponent = BN_get_word(rsa->e);
101
102out:
103 BN_free(n0inv);
104 BN_free(n);
105 BN_free(rem);
106 BN_free(r);
107 BN_free(rr);
108 BN_free(r32);
109 BN_CTX_free(ctx);
110
111 return ret;
112}
113
114static void get_user_info(char *buf, size_t len)
115{
116 char hostname[1024], username[1024];
117 int ret;
118
119#ifndef _WIN32
120 ret = gethostname(hostname, sizeof(hostname));
121 if (ret < 0)
122#endif
123 strcpy(hostname, "unknown");
124
125#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
126 ret = getlogin_r(username, sizeof(username));
127 if (ret < 0)
128#endif
129 strcpy(username, "unknown");
130
131 ret = snprintf(buf, len, " %s@%s", username, hostname);
132 if (ret >= (signed)len)
133 buf[len - 1] = '\0';
134}
135
136static int write_public_keyfile(RSA *private_key, const char *private_key_path)
137{
138 RSAPublicKey pkey;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700139 FILE *outfile = NULL;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700140 char path[PATH_MAX], info[MAX_PAYLOAD];
Adam Langley29f6cdb2014-09-03 14:34:47 -0700141 uint8_t *encoded = NULL;
142 size_t encoded_length;
143 int ret = 0;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700144
Adam Langley29f6cdb2014-09-03 14:34:47 -0700145 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
146 (int)sizeof(path)) {
147 D("Path too long while writing public key\n");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700148 return 0;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700149 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700150
Adam Langley29f6cdb2014-09-03 14:34:47 -0700151 if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700152 D("Failed to convert to publickey\n");
153 return 0;
154 }
155
Adam Langley29f6cdb2014-09-03 14:34:47 -0700156 outfile = fopen(path, "w");
157 if (!outfile) {
Benoit Goby2cc19e42012-04-12 12:23:49 -0700158 D("Failed to open '%s'\n", path);
159 return 0;
160 }
161
162 D("Writing public key to '%s'\n", path);
163
Adam Langley29f6cdb2014-09-03 14:34:47 -0700164#if defined(OPENSSL_IS_BORINGSSL)
165 if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
166 D("Public key too large to base64 encode");
167 goto out;
168 }
169#else
170 /* While we switch from OpenSSL to BoringSSL we have to implement
171 * |EVP_EncodedLength| here. */
172 encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
173#endif
Benoit Goby2cc19e42012-04-12 12:23:49 -0700174
Adam Langley29f6cdb2014-09-03 14:34:47 -0700175 encoded = malloc(encoded_length);
176 if (encoded == NULL) {
177 D("Allocation failure");
178 goto out;
179 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700180
Adam Langley29f6cdb2014-09-03 14:34:47 -0700181 encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700182 get_user_info(info, sizeof(info));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700183
Adam Langley29f6cdb2014-09-03 14:34:47 -0700184 if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
185 fwrite(info, strlen(info), 1, outfile) != 1) {
186 D("Write error while writing public key");
187 goto out;
188 }
189
190 ret = 1;
191
192 out:
193 if (outfile != NULL) {
194 fclose(outfile);
195 }
196 if (encoded != NULL) {
197 free(encoded);
198 }
199 return ret;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700200}
201
202static int generate_key(const char *file)
203{
204 EVP_PKEY* pkey = EVP_PKEY_new();
205 BIGNUM* exponent = BN_new();
206 RSA* rsa = RSA_new();
Benoit Gobycb37c502012-08-31 12:14:21 -0700207 mode_t old_mask;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700208 FILE *f = NULL;
209 int ret = 0;
210
211 D("generate_key '%s'\n", file);
212
213 if (!pkey || !exponent || !rsa) {
214 D("Failed to allocate key\n");
215 goto out;
216 }
217
218 BN_set_word(exponent, RSA_F4);
219 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
220 EVP_PKEY_set1_RSA(pkey, rsa);
221
Benoit Gobycb37c502012-08-31 12:14:21 -0700222 old_mask = umask(077);
223
Benoit Goby2cc19e42012-04-12 12:23:49 -0700224 f = fopen(file, "w");
225 if (!f) {
226 D("Failed to open '%s'\n", file);
Benoit Gobycb37c502012-08-31 12:14:21 -0700227 umask(old_mask);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700228 goto out;
229 }
230
Benoit Gobycb37c502012-08-31 12:14:21 -0700231 umask(old_mask);
232
Benoit Goby2cc19e42012-04-12 12:23:49 -0700233 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
234 D("Failed to write key\n");
235 goto out;
236 }
237
238 if (!write_public_keyfile(rsa, file)) {
239 D("Failed to write public key\n");
240 goto out;
241 }
242
243 ret = 1;
244
245out:
246 if (f)
247 fclose(f);
248 EVP_PKEY_free(pkey);
249 RSA_free(rsa);
250 BN_free(exponent);
251 return ret;
252}
253
254static int read_key(const char *file, struct listnode *list)
255{
256 struct adb_private_key *key;
257 FILE *f;
258
259 D("read_key '%s'\n", file);
260
261 f = fopen(file, "r");
262 if (!f) {
263 D("Failed to open '%s'\n", file);
264 return 0;
265 }
266
267 key = malloc(sizeof(*key));
268 if (!key) {
269 D("Failed to alloc key\n");
270 fclose(f);
271 return 0;
272 }
273 key->rsa = RSA_new();
274
275 if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) {
276 D("Failed to read key\n");
277 fclose(f);
278 RSA_free(key->rsa);
279 free(key);
280 return 0;
281 }
282
283 fclose(f);
284 list_add_tail(list, &key->node);
285 return 1;
286}
287
288static int get_user_keyfilepath(char *filename, size_t len)
289{
290 const char *format, *home;
291 char android_dir[PATH_MAX];
292 struct stat buf;
293#ifdef _WIN32
294 char path[PATH_MAX];
295 home = getenv("ANDROID_SDK_HOME");
296 if (!home) {
297 SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path);
298 home = path;
299 }
300 format = "%s\\%s";
301#else
302 home = getenv("HOME");
303 if (!home)
304 return -1;
305 format = "%s/%s";
306#endif
307
308 D("home '%s'\n", home);
309
310 if (snprintf(android_dir, sizeof(android_dir), format, home,
311 ANDROID_PATH) >= (int)sizeof(android_dir))
312 return -1;
313
314 if (stat(android_dir, &buf)) {
315 if (adb_mkdir(android_dir, 0750) < 0) {
316 D("Cannot mkdir '%s'", android_dir);
317 return -1;
318 }
319 }
320
321 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
322}
323
324static int get_user_key(struct listnode *list)
325{
326 struct stat buf;
327 char path[PATH_MAX];
328 int ret;
329
330 ret = get_user_keyfilepath(path, sizeof(path));
331 if (ret < 0 || ret >= (signed)sizeof(path)) {
332 D("Error getting user key filename");
333 return 0;
334 }
335
336 D("user key '%s'\n", path);
337
338 if (stat(path, &buf) == -1) {
339 if (!generate_key(path)) {
340 D("Failed to generate new key\n");
341 return 0;
342 }
343 }
344
345 return read_key(path, list);
346}
347
348static void get_vendor_keys(struct listnode *list)
349{
350 const char *adb_keys_path;
351 char keys_path[MAX_PAYLOAD];
352 char *path;
353 char *save;
354 struct stat buf;
355
356 adb_keys_path = getenv("ADB_VENDOR_KEYS");
357 if (!adb_keys_path)
358 return;
359 strncpy(keys_path, adb_keys_path, sizeof(keys_path));
360
361 path = adb_strtok_r(keys_path, ENV_PATH_SEPARATOR_STR, &save);
362 while (path) {
363 D("Reading: '%s'\n", path);
364
365 if (stat(path, &buf))
366 D("Can't read '%s'\n", path);
367 else if (!read_key(path, list))
368 D("Failed to read '%s'\n", path);
369
370 path = adb_strtok_r(NULL, ENV_PATH_SEPARATOR_STR, &save);
371 }
372}
373
374int adb_auth_sign(void *node, void *token, size_t token_size, void *sig)
375{
376 unsigned int len;
377 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
378
379 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
380 return 0;
381 }
382
383 D("adb_auth_sign len=%d\n", len);
384 return (int)len;
385}
386
387void *adb_auth_nextkey(void *current)
388{
389 struct listnode *item;
390
391 if (list_empty(&key_list))
392 return NULL;
393
394 if (!current)
395 return list_head(&key_list);
396
397 list_for_each(item, &key_list) {
398 if (item == current) {
399 /* current is the last item, we tried all the keys */
400 if (item->next == &key_list)
401 return NULL;
402 return item->next;
403 }
404 }
405
406 return NULL;
407}
408
409int adb_auth_get_userkey(unsigned char *data, size_t len)
410{
411 char path[PATH_MAX];
412 char *file;
413 int ret;
414
415 ret = get_user_keyfilepath(path, sizeof(path) - 4);
416 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
417 D("Error getting user key filename");
418 return 0;
419 }
420 strcat(path, ".pub");
421
422 file = load_file(path, (unsigned*)&ret);
423 if (!file) {
424 D("Can't load '%s'\n", path);
425 return 0;
426 }
427
428 if (len < (size_t)(ret + 1)) {
429 D("%s: Content too large ret=%d\n", path, ret);
430 return 0;
431 }
432
433 memcpy(data, file, ret);
434 data[ret] = '\0';
435
436 return ret + 1;
437}
438
439void adb_auth_init(void)
440{
441 int ret;
442
443 D("adb_auth_init\n");
444
445 list_init(&key_list);
446
447 ret = get_user_key(&key_list);
448 if (!ret) {
449 D("Failed to get user key\n");
450 return;
451 }
452
453 get_vendor_keys(&key_list);
454}