adb_auth_host.c revision d5fcafaf41f8ec90986c813f75ec78402096af2d
1/* 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 48#define TRACE_TAG TRACE_AUTH 49 50#define ANDROID_PATH ".android" 51#define ADB_KEY_FILE "adb_key" 52 53 54struct adb_private_key { 55 struct listnode node; 56 RSA *rsa; 57}; 58 59static struct listnode key_list; 60 61 62/* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */ 63static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey) 64{ 65 int ret = 1; 66 unsigned int i; 67 68 BN_CTX* ctx = BN_CTX_new(); 69 BIGNUM* r32 = BN_new(); 70 BIGNUM* rr = BN_new(); 71 BIGNUM* r = BN_new(); 72 BIGNUM* rem = BN_new(); 73 BIGNUM* n = BN_new(); 74 BIGNUM* n0inv = BN_new(); 75 76 if (RSA_size(rsa) != RSANUMBYTES) { 77 ret = 0; 78 goto out; 79 } 80 81 BN_set_bit(r32, 32); 82 BN_copy(n, rsa->n); 83 BN_set_bit(r, RSANUMWORDS * 32); 84 BN_mod_sqr(rr, r, n, ctx); 85 BN_div(NULL, rem, n, r32, ctx); 86 BN_mod_inverse(n0inv, rem, r32, ctx); 87 88 pkey->len = RSANUMWORDS; 89 pkey->n0inv = 0 - BN_get_word(n0inv); 90 for (i = 0; i < RSANUMWORDS; i++) { 91 BN_div(rr, rem, rr, r32, ctx); 92 pkey->rr[i] = BN_get_word(rem); 93 BN_div(n, rem, n, r32, ctx); 94 pkey->n[i] = BN_get_word(rem); 95 } 96 pkey->exponent = BN_get_word(rsa->e); 97 98out: 99 BN_free(n0inv); 100 BN_free(n); 101 BN_free(rem); 102 BN_free(r); 103 BN_free(rr); 104 BN_free(r32); 105 BN_CTX_free(ctx); 106 107 return ret; 108} 109 110static void get_user_info(char *buf, size_t len) 111{ 112 char hostname[1024], username[1024]; 113 int ret; 114 115#ifndef _WIN32 116 ret = gethostname(hostname, sizeof(hostname)); 117 if (ret < 0) 118#endif 119 strcpy(hostname, "unknown"); 120 121#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET 122 ret = getlogin_r(username, sizeof(username)); 123 if (ret < 0) 124#endif 125 strcpy(username, "unknown"); 126 127 ret = snprintf(buf, len, " %s@%s", username, hostname); 128 if (ret >= (signed)len) 129 buf[len - 1] = '\0'; 130} 131 132static int write_public_keyfile(RSA *private_key, const char *private_key_path) 133{ 134 RSAPublicKey pkey; 135 BIO *bio, *b64, *bfile; 136 char path[PATH_MAX], info[MAX_PAYLOAD]; 137 int ret; 138 139 ret = snprintf(path, sizeof(path), "%s.pub", private_key_path); 140 if (ret >= (signed)sizeof(path)) 141 return 0; 142 143 ret = RSA_to_RSAPublicKey(private_key, &pkey); 144 if (!ret) { 145 D("Failed to convert to publickey\n"); 146 return 0; 147 } 148 149 bfile = BIO_new_file(path, "w"); 150 if (!bfile) { 151 D("Failed to open '%s'\n", path); 152 return 0; 153 } 154 155 D("Writing public key to '%s'\n", path); 156 157 b64 = BIO_new(BIO_f_base64()); 158 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); 159 160 bio = BIO_push(b64, bfile); 161 BIO_write(bio, &pkey, sizeof(pkey)); 162 BIO_flush(bio); 163 BIO_pop(b64); 164 BIO_free(b64); 165 166 get_user_info(info, sizeof(info)); 167 BIO_write(bfile, info, strlen(info)); 168 BIO_flush(bfile); 169 BIO_free_all(bfile); 170 171 return 1; 172} 173 174static int generate_key(const char *file) 175{ 176 EVP_PKEY* pkey = EVP_PKEY_new(); 177 BIGNUM* exponent = BN_new(); 178 RSA* rsa = RSA_new(); 179 FILE *f = NULL; 180 int ret = 0; 181 182 D("generate_key '%s'\n", file); 183 184 if (!pkey || !exponent || !rsa) { 185 D("Failed to allocate key\n"); 186 goto out; 187 } 188 189 BN_set_word(exponent, RSA_F4); 190 RSA_generate_key_ex(rsa, 2048, exponent, NULL); 191 EVP_PKEY_set1_RSA(pkey, rsa); 192 193 f = fopen(file, "w"); 194 if (!f) { 195 D("Failed to open '%s'\n", file); 196 goto out; 197 } 198 199 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) { 200 D("Failed to write key\n"); 201 goto out; 202 } 203 204 if (!write_public_keyfile(rsa, file)) { 205 D("Failed to write public key\n"); 206 goto out; 207 } 208 209 ret = 1; 210 211out: 212 if (f) 213 fclose(f); 214 EVP_PKEY_free(pkey); 215 RSA_free(rsa); 216 BN_free(exponent); 217 return ret; 218} 219 220static int read_key(const char *file, struct listnode *list) 221{ 222 struct adb_private_key *key; 223 FILE *f; 224 225 D("read_key '%s'\n", file); 226 227 f = fopen(file, "r"); 228 if (!f) { 229 D("Failed to open '%s'\n", file); 230 return 0; 231 } 232 233 key = malloc(sizeof(*key)); 234 if (!key) { 235 D("Failed to alloc key\n"); 236 fclose(f); 237 return 0; 238 } 239 key->rsa = RSA_new(); 240 241 if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) { 242 D("Failed to read key\n"); 243 fclose(f); 244 RSA_free(key->rsa); 245 free(key); 246 return 0; 247 } 248 249 fclose(f); 250 list_add_tail(list, &key->node); 251 return 1; 252} 253 254static int get_user_keyfilepath(char *filename, size_t len) 255{ 256 const char *format, *home; 257 char android_dir[PATH_MAX]; 258 struct stat buf; 259#ifdef _WIN32 260 char path[PATH_MAX]; 261 home = getenv("ANDROID_SDK_HOME"); 262 if (!home) { 263 SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path); 264 home = path; 265 } 266 format = "%s\\%s"; 267#else 268 home = getenv("HOME"); 269 if (!home) 270 return -1; 271 format = "%s/%s"; 272#endif 273 274 D("home '%s'\n", home); 275 276 if (snprintf(android_dir, sizeof(android_dir), format, home, 277 ANDROID_PATH) >= (int)sizeof(android_dir)) 278 return -1; 279 280 if (stat(android_dir, &buf)) { 281 if (adb_mkdir(android_dir, 0750) < 0) { 282 D("Cannot mkdir '%s'", android_dir); 283 return -1; 284 } 285 } 286 287 return snprintf(filename, len, format, android_dir, ADB_KEY_FILE); 288} 289 290static int get_user_key(struct listnode *list) 291{ 292 struct stat buf; 293 char path[PATH_MAX]; 294 int ret; 295 296 ret = get_user_keyfilepath(path, sizeof(path)); 297 if (ret < 0 || ret >= (signed)sizeof(path)) { 298 D("Error getting user key filename"); 299 return 0; 300 } 301 302 D("user key '%s'\n", path); 303 304 if (stat(path, &buf) == -1) { 305 if (!generate_key(path)) { 306 D("Failed to generate new key\n"); 307 return 0; 308 } 309 } 310 311 return read_key(path, list); 312} 313 314static void get_vendor_keys(struct listnode *list) 315{ 316 const char *adb_keys_path; 317 char keys_path[MAX_PAYLOAD]; 318 char *path; 319 char *save; 320 struct stat buf; 321 322 adb_keys_path = getenv("ADB_VENDOR_KEYS"); 323 if (!adb_keys_path) 324 return; 325 strncpy(keys_path, adb_keys_path, sizeof(keys_path)); 326 327 path = adb_strtok_r(keys_path, ENV_PATH_SEPARATOR_STR, &save); 328 while (path) { 329 D("Reading: '%s'\n", path); 330 331 if (stat(path, &buf)) 332 D("Can't read '%s'\n", path); 333 else if (!read_key(path, list)) 334 D("Failed to read '%s'\n", path); 335 336 path = adb_strtok_r(NULL, ENV_PATH_SEPARATOR_STR, &save); 337 } 338} 339 340int adb_auth_sign(void *node, void *token, size_t token_size, void *sig) 341{ 342 unsigned int len; 343 struct adb_private_key *key = node_to_item(node, struct adb_private_key, node); 344 345 if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) { 346 return 0; 347 } 348 349 D("adb_auth_sign len=%d\n", len); 350 return (int)len; 351} 352 353void *adb_auth_nextkey(void *current) 354{ 355 struct listnode *item; 356 357 if (list_empty(&key_list)) 358 return NULL; 359 360 if (!current) 361 return list_head(&key_list); 362 363 list_for_each(item, &key_list) { 364 if (item == current) { 365 /* current is the last item, we tried all the keys */ 366 if (item->next == &key_list) 367 return NULL; 368 return item->next; 369 } 370 } 371 372 return NULL; 373} 374 375int adb_auth_get_userkey(unsigned char *data, size_t len) 376{ 377 char path[PATH_MAX]; 378 char *file; 379 int ret; 380 381 ret = get_user_keyfilepath(path, sizeof(path) - 4); 382 if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) { 383 D("Error getting user key filename"); 384 return 0; 385 } 386 strcat(path, ".pub"); 387 388 file = load_file(path, (unsigned*)&ret); 389 if (!file) { 390 D("Can't load '%s'\n", path); 391 return 0; 392 } 393 394 if (len < (size_t)(ret + 1)) { 395 D("%s: Content too large ret=%d\n", path, ret); 396 return 0; 397 } 398 399 memcpy(data, file, ret); 400 data[ret] = '\0'; 401 402 return ret + 1; 403} 404 405void adb_auth_init(void) 406{ 407 int ret; 408 409 D("adb_auth_init\n"); 410 411 list_init(&key_list); 412 413 ret = get_user_key(&key_list); 414 if (!ret) { 415 D("Failed to get user key\n"); 416 return; 417 } 418 419 get_vendor_keys(&key_list); 420} 421