blob: 8f154fd930ee610c142ad0b1cc061ba5a6e0dfda [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"
21
Benoit Goby2cc19e42012-04-12 12:23:49 -070022#include <stdio.h>
Christopher Ferris054d1702014-11-06 14:34:24 -080023#include <stdlib.h>
Dan Albertdb6fe642015-03-19 15:21:08 -070024#include <string.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070025
26#ifdef _WIN32
Christopher Ferris054d1702014-11-06 14:34:24 -080027# ifndef WIN32_LEAN_AND_MEAN
28# define WIN32_LEAN_AND_MEAN
29# endif
Benoit Goby2cc19e42012-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 Goby2cc19e42012-04-12 12:23:49 -070037
Benoit Goby2cc19e42012-04-12 12:23:49 -070038#include "adb.h"
Benoit Goby2cc19e42012-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
David Pursellc573d522016-01-27 08:52:53 -080046#include <android-base/errors.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080047#include <android-base/strings.h>
Benoit Goby2cc19e42012-04-12 12:23:49 -070048#include <cutils/list.h>
49
50#include <openssl/evp.h>
51#include <openssl/objects.h>
52#include <openssl/pem.h>
53#include <openssl/rsa.h>
54#include <openssl/sha.h>
55
Adam Langley29f6cdb2014-09-03 14:34:47 -070056#if defined(OPENSSL_IS_BORINGSSL)
57#include <openssl/base64.h>
58#endif
59
Dan Albert3d978e62015-07-09 20:35:09 +000060#define ANDROID_PATH ".android"
61#define ADB_KEY_FILE "adbkey"
Benoit Goby2cc19e42012-04-12 12:23:49 -070062
Benoit Goby2cc19e42012-04-12 12:23:49 -070063struct adb_private_key {
64 struct listnode node;
65 RSA *rsa;
66};
67
68static struct listnode key_list;
69
70
71/* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
72static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
73{
74 int ret = 1;
75 unsigned int i;
76
77 BN_CTX* ctx = BN_CTX_new();
78 BIGNUM* r32 = BN_new();
79 BIGNUM* rr = BN_new();
80 BIGNUM* r = BN_new();
81 BIGNUM* rem = BN_new();
82 BIGNUM* n = BN_new();
83 BIGNUM* n0inv = BN_new();
84
85 if (RSA_size(rsa) != RSANUMBYTES) {
86 ret = 0;
87 goto out;
88 }
89
90 BN_set_bit(r32, 32);
91 BN_copy(n, rsa->n);
92 BN_set_bit(r, RSANUMWORDS * 32);
93 BN_mod_sqr(rr, r, n, ctx);
94 BN_div(NULL, rem, n, r32, ctx);
95 BN_mod_inverse(n0inv, rem, r32, ctx);
96
97 pkey->len = RSANUMWORDS;
98 pkey->n0inv = 0 - BN_get_word(n0inv);
99 for (i = 0; i < RSANUMWORDS; i++) {
100 BN_div(rr, rem, rr, r32, ctx);
101 pkey->rr[i] = BN_get_word(rem);
102 BN_div(n, rem, n, r32, ctx);
103 pkey->n[i] = BN_get_word(rem);
104 }
105 pkey->exponent = BN_get_word(rsa->e);
106
107out:
108 BN_free(n0inv);
109 BN_free(n);
110 BN_free(rem);
111 BN_free(r);
112 BN_free(rr);
113 BN_free(r32);
114 BN_CTX_free(ctx);
115
116 return ret;
117}
118
119static void get_user_info(char *buf, size_t len)
120{
121 char hostname[1024], username[1024];
Nick Kralevich6183c962014-11-13 15:17:29 -0800122 int ret = -1;
123
124 if (getenv("HOSTNAME") != NULL) {
125 strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
126 hostname[sizeof(hostname)-1] = '\0';
127 ret = 0;
128 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700129
130#ifndef _WIN32
Benoit Goby2cc19e42012-04-12 12:23:49 -0700131 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -0800132 ret = gethostname(hostname, sizeof(hostname));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700133#endif
Nick Kralevich6183c962014-11-13 15:17:29 -0800134 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700135 strcpy(hostname, "unknown");
136
Nick Kralevich6183c962014-11-13 15:17:29 -0800137 ret = -1;
138
139 if (getenv("LOGNAME") != NULL) {
140 strncpy(username, getenv("LOGNAME"), sizeof(username));
141 username[sizeof(username)-1] = '\0';
142 ret = 0;
143 }
144
Benoit Goby2cc19e42012-04-12 12:23:49 -0700145#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
Benoit Goby2cc19e42012-04-12 12:23:49 -0700146 if (ret < 0)
Nick Kralevich6183c962014-11-13 15:17:29 -0800147 ret = getlogin_r(username, sizeof(username));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700148#endif
Nick Kralevich6183c962014-11-13 15:17:29 -0800149 if (ret < 0)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700150 strcpy(username, "unknown");
151
152 ret = snprintf(buf, len, " %s@%s", username, hostname);
153 if (ret >= (signed)len)
154 buf[len - 1] = '\0';
155}
156
157static int write_public_keyfile(RSA *private_key, const char *private_key_path)
158{
159 RSAPublicKey pkey;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700160 FILE *outfile = NULL;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100161 char path[PATH_MAX], info[MAX_PAYLOAD_V1];
Dan Albertf30d73c2015-02-25 17:51:28 -0800162 uint8_t* encoded = nullptr;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700163 size_t encoded_length;
164 int ret = 0;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700165
Adam Langley29f6cdb2014-09-03 14:34:47 -0700166 if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
167 (int)sizeof(path)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700168 D("Path too long while writing public key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700169 return 0;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700170 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700171
Adam Langley29f6cdb2014-09-03 14:34:47 -0700172 if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700173 D("Failed to convert to publickey");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700174 return 0;
175 }
176
Spencer Low60bca2d2015-05-07 19:08:29 -0700177 outfile = fopen(path, "w");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700178 if (!outfile) {
Yabin Cui815ad882015-09-02 17:44:28 -0700179 D("Failed to open '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700180 return 0;
181 }
182
Yabin Cui815ad882015-09-02 17:44:28 -0700183 D("Writing public key to '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700184
Adam Langley29f6cdb2014-09-03 14:34:47 -0700185#if defined(OPENSSL_IS_BORINGSSL)
186 if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
Yabin Cui815ad882015-09-02 17:44:28 -0700187 D("Public key too large to base64 encode");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700188 goto out;
189 }
190#else
191 /* While we switch from OpenSSL to BoringSSL we have to implement
192 * |EVP_EncodedLength| here. */
193 encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
194#endif
Benoit Goby2cc19e42012-04-12 12:23:49 -0700195
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700196 encoded = new uint8_t[encoded_length];
Dan Albertf30d73c2015-02-25 17:51:28 -0800197 if (encoded == nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -0700198 D("Allocation failure");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700199 goto out;
200 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700201
Adam Langley29f6cdb2014-09-03 14:34:47 -0700202 encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700203 get_user_info(info, sizeof(info));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700204
Adam Langley29f6cdb2014-09-03 14:34:47 -0700205 if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
206 fwrite(info, strlen(info), 1, outfile) != 1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700207 D("Write error while writing public key");
Adam Langley29f6cdb2014-09-03 14:34:47 -0700208 goto out;
209 }
210
211 ret = 1;
212
213 out:
214 if (outfile != NULL) {
215 fclose(outfile);
216 }
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700217 delete[] encoded;
Adam Langley29f6cdb2014-09-03 14:34:47 -0700218 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
Yabin Cui815ad882015-09-02 17:44:28 -0700230 D("generate_key '%s'", file);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700231
232 if (!pkey || !exponent || !rsa) {
Yabin Cui815ad882015-09-02 17:44:28 -0700233 D("Failed to allocate key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700234 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
Spencer Low60bca2d2015-05-07 19:08:29 -0700243 f = fopen(file, "w");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700244 if (!f) {
Yabin Cui815ad882015-09-02 17:44:28 -0700245 D("Failed to open '%s'", 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)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700253 D("Failed to write key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700254 goto out;
255 }
256
257 if (!write_public_keyfile(rsa, file)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700258 D("Failed to write public key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700259 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{
Yabin Cui815ad882015-09-02 17:44:28 -0700275 D("read_key '%s'", file);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700276
Spencer Low60bca2d2015-05-07 19:08:29 -0700277 FILE* fp = fopen(file, "r");
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700278 if (!fp) {
Yabin Cui815ad882015-09-02 17:44:28 -0700279 D("Failed to open '%s': %s", file, strerror(errno));
Benoit Goby2cc19e42012-04-12 12:23:49 -0700280 return 0;
281 }
282
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700283 adb_private_key* key = new adb_private_key;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700284 key->rsa = RSA_new();
285
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700286 if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700287 D("Failed to read key");
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700288 fclose(fp);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700289 RSA_free(key->rsa);
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700290 delete key;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700291 return 0;
292 }
293
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700294 fclose(fp);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700295 list_add_tail(list, &key->node);
296 return 1;
297}
298
Dan Albert3d978e62015-07-09 20:35:09 +0000299static int get_user_keyfilepath(char *filename, size_t len)
300{
301 const char *format, *home;
302 char android_dir[PATH_MAX];
303 struct stat buf;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700304#ifdef _WIN32
Spencer Low6815c072015-05-11 01:08:48 -0700305 std::string home_str;
Dan Albert3d978e62015-07-09 20:35:09 +0000306 home = getenv("ANDROID_SDK_HOME");
307 if (!home) {
Spencer Low6815c072015-05-11 01:08:48 -0700308 WCHAR path[MAX_PATH];
Spencer Low1711e012015-08-02 18:50:17 -0700309 const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
310 if (FAILED(hr)) {
David Pursellc573d522016-01-27 08:52:53 -0800311 D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str());
Spencer Low6815c072015-05-11 01:08:48 -0700312 return -1;
313 }
Spencer Low50f5bf12015-11-12 15:20:15 -0800314 if (!android::base::WideToUTF8(path, &home_str)) {
315 return -1;
316 }
Spencer Low6815c072015-05-11 01:08:48 -0700317 home = home_str.c_str();
Benoit Goby2cc19e42012-04-12 12:23:49 -0700318 }
Dan Albert3d978e62015-07-09 20:35:09 +0000319 format = "%s\\%s";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700320#else
Dan Albert3d978e62015-07-09 20:35:09 +0000321 home = getenv("HOME");
322 if (!home)
323 return -1;
324 format = "%s/%s";
Benoit Goby2cc19e42012-04-12 12:23:49 -0700325#endif
326
Yabin Cui815ad882015-09-02 17:44:28 -0700327 D("home '%s'", home);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700328
Dan Albert3d978e62015-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 Goby2cc19e42012-04-12 12:23:49 -0700332
Dan Albert3d978e62015-07-09 20:35:09 +0000333 if (stat(android_dir, &buf)) {
334 if (adb_mkdir(android_dir, 0750) < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700335 D("Cannot mkdir '%s'", android_dir);
Dan Albert3d978e62015-07-09 20:35:09 +0000336 return -1;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700337 }
338 }
339
Dan Albert3d978e62015-07-09 20:35:09 +0000340 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700341}
342
343static int get_user_key(struct listnode *list)
344{
Dan Albert3d978e62015-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 Cui815ad882015-09-02 17:44:28 -0700351 D("Error getting user key filename");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700352 return 0;
353 }
354
Yabin Cui815ad882015-09-02 17:44:28 -0700355 D("user key '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700356
Dan Albert3d978e62015-07-09 20:35:09 +0000357 if (stat(path, &buf) == -1) {
358 if (!generate_key(path)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700359 D("Failed to generate new key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700360 return 0;
361 }
362 }
363
Dan Albert3d978e62015-07-09 20:35:09 +0000364 return read_key(path, list);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700365}
366
Elliott Hughesfb2ba512015-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 Goby2cc19e42012-04-12 12:23:49 -0700370 return;
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700371 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700372
Elliott Hughes85952832015-10-07 15:59:35 -0700373 for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700374 if (!read_key(path.c_str(), key_list)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700375 D("Failed to read '%s'", path.c_str());
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700376 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700377 }
378}
379
Dan Albertf30d73c2015-02-25 17:51:28 -0800380int adb_auth_sign(void *node, const unsigned char* token, size_t token_size,
381 unsigned char* sig)
Benoit Goby2cc19e42012-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 Tolvanenb92a35c2015-01-27 16:48:35 +0000386 if (token_size != TOKEN_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700387 D("Unexpected token size %zd", token_size);
Sami Tolvanenb92a35c2015-01-27 16:48:35 +0000388 return 0;
389 }
390
Benoit Goby2cc19e42012-04-12 12:23:49 -0700391 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
392 return 0;
393 }
394
Yabin Cui815ad882015-09-02 17:44:28 -0700395 D("adb_auth_sign len=%d", len);
Benoit Goby2cc19e42012-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 Albert3d978e62015-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 Cui815ad882015-09-02 17:44:28 -0700426 D("Error getting user key filename");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700427 return 0;
428 }
Dan Albert3d978e62015-07-09 20:35:09 +0000429 strcat(path, ".pub");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700430
Dan Albertf30d73c2015-02-25 17:51:28 -0800431 // TODO(danalbert): ReadFileToString
Spencer Low3a2421b2015-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 Albertf30d73c2015-02-25 17:51:28 -0800435 unsigned size;
Dan Albert3d978e62015-07-09 20:35:09 +0000436 char* file_data = reinterpret_cast<char*>(load_file(path, &size));
Dan Albertf30d73c2015-02-25 17:51:28 -0800437 if (file_data == nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -0700438 D("Can't load '%s'", path);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700439 return 0;
440 }
441
Dan Albertf30d73c2015-02-25 17:51:28 -0800442 if (len < (size_t)(size + 1)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700443 D("%s: Content too large ret=%d", path, size);
Dan Albertf30d73c2015-02-25 17:51:28 -0800444 free(file_data);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700445 return 0;
446 }
447
Dan Albertf30d73c2015-02-25 17:51:28 -0800448 memcpy(data, file_data, size);
449 free(file_data);
450 file_data = nullptr;
451 data[size] = '\0';
Benoit Goby2cc19e42012-04-12 12:23:49 -0700452
Dan Albertf30d73c2015-02-25 17:51:28 -0800453 return size + 1;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700454}
455
Nick Kralevich6183c962014-11-13 15:17:29 -0800456int adb_auth_keygen(const char* filename) {
Nick Kralevich6183c962014-11-13 15:17:29 -0800457 return (generate_key(filename) == 0);
458}
459
Benoit Goby2cc19e42012-04-12 12:23:49 -0700460void adb_auth_init(void)
461{
462 int ret;
463
Yabin Cui815ad882015-09-02 17:44:28 -0700464 D("adb_auth_init");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700465
466 list_init(&key_list);
467
468 ret = get_user_key(&key_list);
469 if (!ret) {
Yabin Cui815ad882015-09-02 17:44:28 -0700470 D("Failed to get user key");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700471 return;
472 }
473
474 get_vendor_keys(&key_list);
475}