blob: e11bff000f28d2a2db1725e630e1acf48d09c64e [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
40/* HACK: we need the RSAPublicKey struct
41 * but RSA_verify conflits with openssl */
42#define RSA_verify RSA_verify_mincrypt
43#include "mincrypt/rsa.h"
44#undef RSA_verify
45
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070046#include <base/strings.h>
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070047#include <cutils/list.h>
48
49#include <openssl/evp.h>
50#include <openssl/objects.h>
51#include <openssl/pem.h>
52#include <openssl/rsa.h>
53#include <openssl/sha.h>
54
Adam Langley179d9d62014-09-03 14:34:47 -070055#if defined(OPENSSL_IS_BORINGSSL)
56#include <openssl/base64.h>
57#endif
58
Dan Albert286bb6d2015-07-09 20:35:09 +000059#define ANDROID_PATH ".android"
60#define ADB_KEY_FILE "adbkey"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070061
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070062struct adb_private_key {
63 struct listnode node;
64 RSA *rsa;
65};
66
67static struct listnode key_list;
68
69
70/* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
71static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
72{
73 int ret = 1;
74 unsigned int i;
75
76 BN_CTX* ctx = BN_CTX_new();
77 BIGNUM* r32 = BN_new();
78 BIGNUM* rr = BN_new();
79 BIGNUM* r = BN_new();
80 BIGNUM* rem = BN_new();
81 BIGNUM* n = BN_new();
82 BIGNUM* n0inv = BN_new();
83
84 if (RSA_size(rsa) != RSANUMBYTES) {
85 ret = 0;
86 goto out;
87 }
88
89 BN_set_bit(r32, 32);
90 BN_copy(n, rsa->n);
91 BN_set_bit(r, RSANUMWORDS * 32);
92 BN_mod_sqr(rr, r, n, ctx);
93 BN_div(NULL, rem, n, r32, ctx);
94 BN_mod_inverse(n0inv, rem, r32, ctx);
95
96 pkey->len = RSANUMWORDS;
97 pkey->n0inv = 0 - BN_get_word(n0inv);
98 for (i = 0; i < RSANUMWORDS; i++) {
99 BN_div(rr, rem, rr, r32, ctx);
100 pkey->rr[i] = BN_get_word(rem);
101 BN_div(n, rem, n, r32, ctx);
102 pkey->n[i] = BN_get_word(rem);
103 }
104 pkey->exponent = BN_get_word(rsa->e);
105
106out:
107 BN_free(n0inv);
108 BN_free(n);
109 BN_free(rem);
110 BN_free(r);
111 BN_free(rr);
112 BN_free(r32);
113 BN_CTX_free(ctx);
114
115 return ret;
116}
117
118static void get_user_info(char *buf, size_t len)
119{
120 char hostname[1024], username[1024];
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800121 int ret = -1;
122
123 if (getenv("HOSTNAME") != NULL) {
124 strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
125 hostname[sizeof(hostname)-1] = '\0';
126 ret = 0;
127 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700128
129#ifndef _WIN32
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700130 if (ret < 0)
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800131 ret = gethostname(hostname, sizeof(hostname));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700132#endif
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800133 if (ret < 0)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700134 strcpy(hostname, "unknown");
135
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800136 ret = -1;
137
138 if (getenv("LOGNAME") != NULL) {
139 strncpy(username, getenv("LOGNAME"), sizeof(username));
140 username[sizeof(username)-1] = '\0';
141 ret = 0;
142 }
143
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700144#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700145 if (ret < 0)
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800146 ret = getlogin_r(username, sizeof(username));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700147#endif
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800148 if (ret < 0)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700149 strcpy(username, "unknown");
150
151 ret = snprintf(buf, len, " %s@%s", username, hostname);
152 if (ret >= (signed)len)
153 buf[len - 1] = '\0';
154}
155
156static int write_public_keyfile(RSA *private_key, const char *private_key_path)
157{
158 RSAPublicKey pkey;
Adam Langley179d9d62014-09-03 14:34:47 -0700159 FILE *outfile = NULL;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100160 char path[PATH_MAX], info[MAX_PAYLOAD_V1];
Dan Albertbac34742015-02-25 17:51:28 -0800161 uint8_t* encoded = nullptr;
Adam Langley179d9d62014-09-03 14:34:47 -0700162 size_t encoded_length;
163 int ret = 0;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700164
Adam Langley179d9d62014-09-03 14:34:47 -0700165 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
166 (int)sizeof(path)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700167 D("Path too long while writing public key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700168 return 0;
Adam Langley179d9d62014-09-03 14:34:47 -0700169 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700170
Adam Langley179d9d62014-09-03 14:34:47 -0700171 if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700172 D("Failed to convert to publickey");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700173 return 0;
174 }
175
Spencer Low9b960312015-05-07 19:08:29 -0700176 outfile = fopen(path, "w");
Adam Langley179d9d62014-09-03 14:34:47 -0700177 if (!outfile) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700178 D("Failed to open '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700179 return 0;
180 }
181
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700182 D("Writing public key to '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700183
Adam Langley179d9d62014-09-03 14:34:47 -0700184#if defined(OPENSSL_IS_BORINGSSL)
185 if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700186 D("Public key too large to base64 encode");
Adam Langley179d9d62014-09-03 14:34:47 -0700187 goto out;
188 }
189#else
190 /* While we switch from OpenSSL to BoringSSL we have to implement
191 * |EVP_EncodedLength| here. */
192 encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
193#endif
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700194
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700195 encoded = new uint8_t[encoded_length];
Dan Albertbac34742015-02-25 17:51:28 -0800196 if (encoded == nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700197 D("Allocation failure");
Adam Langley179d9d62014-09-03 14:34:47 -0700198 goto out;
199 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700200
Adam Langley179d9d62014-09-03 14:34:47 -0700201 encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700202 get_user_info(info, sizeof(info));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700203
Adam Langley179d9d62014-09-03 14:34:47 -0700204 if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
205 fwrite(info, strlen(info), 1, outfile) != 1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700206 D("Write error while writing public key");
Adam Langley179d9d62014-09-03 14:34:47 -0700207 goto out;
208 }
209
210 ret = 1;
211
212 out:
213 if (outfile != NULL) {
214 fclose(outfile);
215 }
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700216 delete[] encoded;
Adam Langley179d9d62014-09-03 14:34:47 -0700217 return ret;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700218}
219
220static int generate_key(const char *file)
221{
222 EVP_PKEY* pkey = EVP_PKEY_new();
223 BIGNUM* exponent = BN_new();
224 RSA* rsa = RSA_new();
Benoit Goby64b31032012-08-31 12:14:21 -0700225 mode_t old_mask;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700226 FILE *f = NULL;
227 int ret = 0;
228
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700229 D("generate_key '%s'", file);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700230
231 if (!pkey || !exponent || !rsa) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700232 D("Failed to allocate key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700233 goto out;
234 }
235
236 BN_set_word(exponent, RSA_F4);
237 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
238 EVP_PKEY_set1_RSA(pkey, rsa);
239
Benoit Goby64b31032012-08-31 12:14:21 -0700240 old_mask = umask(077);
241
Spencer Low9b960312015-05-07 19:08:29 -0700242 f = fopen(file, "w");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700243 if (!f) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700244 D("Failed to open '%s'", file);
Benoit Goby64b31032012-08-31 12:14:21 -0700245 umask(old_mask);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700246 goto out;
247 }
248
Benoit Goby64b31032012-08-31 12:14:21 -0700249 umask(old_mask);
250
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700251 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700252 D("Failed to write key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700253 goto out;
254 }
255
256 if (!write_public_keyfile(rsa, file)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700257 D("Failed to write public key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700258 goto out;
259 }
260
261 ret = 1;
262
263out:
264 if (f)
265 fclose(f);
266 EVP_PKEY_free(pkey);
267 RSA_free(rsa);
268 BN_free(exponent);
269 return ret;
270}
271
272static int read_key(const char *file, struct listnode *list)
273{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700274 D("read_key '%s'", file);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700275
Spencer Low9b960312015-05-07 19:08:29 -0700276 FILE* fp = fopen(file, "r");
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700277 if (!fp) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700278 D("Failed to open '%s': %s", file, strerror(errno));
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700279 return 0;
280 }
281
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700282 adb_private_key* key = new adb_private_key;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700283 key->rsa = RSA_new();
284
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700285 if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700286 D("Failed to read key");
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700287 fclose(fp);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700288 RSA_free(key->rsa);
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700289 delete key;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700290 return 0;
291 }
292
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700293 fclose(fp);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700294 list_add_tail(list, &key->node);
295 return 1;
296}
297
Dan Albert286bb6d2015-07-09 20:35:09 +0000298static int get_user_keyfilepath(char *filename, size_t len)
299{
300 const char *format, *home;
301 char android_dir[PATH_MAX];
302 struct stat buf;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700303#ifdef _WIN32
Spencer Lowcf4ff642015-05-11 01:08:48 -0700304 std::string home_str;
Dan Albert286bb6d2015-07-09 20:35:09 +0000305 home = getenv("ANDROID_SDK_HOME");
306 if (!home) {
Spencer Lowcf4ff642015-05-11 01:08:48 -0700307 WCHAR path[MAX_PATH];
Spencer Low8df90322015-08-02 18:50:17 -0700308 const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
309 if (FAILED(hr)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700310 D("SHGetFolderPathW failed: %s",
Spencer Low8df90322015-08-02 18:50:17 -0700311 SystemErrorCodeToString(hr).c_str());
Spencer Lowcf4ff642015-05-11 01:08:48 -0700312 return -1;
313 }
Spencer Lowd21dc822015-11-12 15:20:15 -0800314 if (!android::base::WideToUTF8(path, &home_str)) {
315 return -1;
316 }
Spencer Lowcf4ff642015-05-11 01:08:48 -0700317 home = home_str.c_str();
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700318 }
Dan Albert286bb6d2015-07-09 20:35:09 +0000319 format = "%s\\%s";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700320#else
Dan Albert286bb6d2015-07-09 20:35:09 +0000321 home = getenv("HOME");
322 if (!home)
323 return -1;
324 format = "%s/%s";
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700325#endif
326
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700327 D("home '%s'", home);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700328
Dan Albert286bb6d2015-07-09 20:35:09 +0000329 if (snprintf(android_dir, sizeof(android_dir), format, home,
330 ANDROID_PATH) >= (int)sizeof(android_dir))
331 return -1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700332
Dan Albert286bb6d2015-07-09 20:35:09 +0000333 if (stat(android_dir, &buf)) {
334 if (adb_mkdir(android_dir, 0750) < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700335 D("Cannot mkdir '%s'", android_dir);
Dan Albert286bb6d2015-07-09 20:35:09 +0000336 return -1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700337 }
338 }
339
Dan Albert286bb6d2015-07-09 20:35:09 +0000340 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700341}
342
343static int get_user_key(struct listnode *list)
344{
Dan Albert286bb6d2015-07-09 20:35:09 +0000345 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700351 D("Error getting user key filename");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700352 return 0;
353 }
354
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700355 D("user key '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700356
Dan Albert286bb6d2015-07-09 20:35:09 +0000357 if (stat(path, &buf) == -1) {
358 if (!generate_key(path)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700359 D("Failed to generate new key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700360 return 0;
361 }
362 }
363
Dan Albert286bb6d2015-07-09 20:35:09 +0000364 return read_key(path, list);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700365}
366
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700367static void get_vendor_keys(struct listnode* key_list) {
368 const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
369 if (adb_keys_path == nullptr) {
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700370 return;
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700371 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700372
Elliott Hughes65fe2512015-10-07 15:59:35 -0700373 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700374 if (!read_key(path.c_str(), key_list)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700375 D("Failed to read '%s'", path.c_str());
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -0700376 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700377 }
378}
379
Dan Albertbac34742015-02-25 17:51:28 -0800380int adb_auth_sign(void *node, const unsigned char* token, size_t token_size,
381 unsigned char* sig)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700382{
383 unsigned int len;
384 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
385
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000386 if (token_size != TOKEN_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700387 D("Unexpected token size %zd", token_size);
Sami Tolvanen7b9c20d2015-01-27 16:48:35 +0000388 return 0;
389 }
390
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700391 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
392 return 0;
393 }
394
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700395 D("adb_auth_sign len=%d", len);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700396 return (int)len;
397}
398
399void *adb_auth_nextkey(void *current)
400{
401 struct listnode *item;
402
403 if (list_empty(&key_list))
404 return NULL;
405
406 if (!current)
407 return list_head(&key_list);
408
409 list_for_each(item, &key_list) {
410 if (item == current) {
411 /* current is the last item, we tried all the keys */
412 if (item->next == &key_list)
413 return NULL;
414 return item->next;
415 }
416 }
417
418 return NULL;
419}
420
421int adb_auth_get_userkey(unsigned char *data, size_t len)
422{
Dan Albert286bb6d2015-07-09 20:35:09 +0000423 char path[PATH_MAX];
424 int ret = get_user_keyfilepath(path, sizeof(path) - 4);
425 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700426 D("Error getting user key filename");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700427 return 0;
428 }
Dan Albert286bb6d2015-07-09 20:35:09 +0000429 strcat(path, ".pub");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700430
Dan Albertbac34742015-02-25 17:51:28 -0800431 // TODO(danalbert): ReadFileToString
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700432 // Note that on Windows, load_file() does not do CR/LF translation, but
433 // ReadFileToString() uses the C Runtime which uses CR/LF translation by
434 // default (by is overridable with _setmode()).
Dan Albertbac34742015-02-25 17:51:28 -0800435 unsigned size;
Dan Albert286bb6d2015-07-09 20:35:09 +0000436 char* file_data = reinterpret_cast<char*>(load_file(path, &size));
Dan Albertbac34742015-02-25 17:51:28 -0800437 if (file_data == nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700438 D("Can't load '%s'", path);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700439 return 0;
440 }
441
Dan Albertbac34742015-02-25 17:51:28 -0800442 if (len < (size_t)(size + 1)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700443 D("%s: Content too large ret=%d", path, size);
Dan Albertbac34742015-02-25 17:51:28 -0800444 free(file_data);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700445 return 0;
446 }
447
Dan Albertbac34742015-02-25 17:51:28 -0800448 memcpy(data, file_data, size);
449 free(file_data);
450 file_data = nullptr;
451 data[size] = '\0';
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700452
Dan Albertbac34742015-02-25 17:51:28 -0800453 return size + 1;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700454}
455
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800456int adb_auth_keygen(const char* filename) {
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800457 return (generate_key(filename) == 0);
458}
459
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700460void adb_auth_init(void)
461{
462 int ret;
463
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700464 D("adb_auth_init");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700465
466 list_init(&key_list);
467
468 ret = get_user_key(&key_list);
469 if (!ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700470 D("Failed to get user key");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700471 return;
472 }
473
474 get_vendor_keys(&key_list);
475}