adb_auth_host.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #include <stdio.h>
  17. #ifdef _WIN32
  18. # define WIN32_LEAN_AND_MEAN
  19. # include "windows.h"
  20. # include "shlobj.h"
  21. #else
  22. # include <sys/types.h>
  23. # include <sys/stat.h>
  24. # include <unistd.h>
  25. #endif
  26. #include <string.h>
  27. #include "sysdeps.h"
  28. #include "adb.h"
  29. #include "adb_auth.h"
  30. /* HACK: we need the RSAPublicKey struct
  31. * but RSA_verify conflits with openssl */
  32. #define RSA_verify RSA_verify_mincrypt
  33. #include "mincrypt/rsa.h"
  34. #undef RSA_verify
  35. #include <cutils/list.h>
  36. #include <openssl/evp.h>
  37. #include <openssl/objects.h>
  38. #include <openssl/pem.h>
  39. #include <openssl/rsa.h>
  40. #include <openssl/sha.h>
  41. #define TRACE_TAG TRACE_AUTH
  42. #define ANDROID_PATH ".android"
  43. #define ADB_KEY_FILE "adbkey"
  44. struct adb_private_key {
  45. struct listnode node;
  46. RSA *rsa;
  47. };
  48. static struct listnode key_list;
  49. /* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
  50. static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
  51. {
  52. int ret = 1;
  53. unsigned int i;
  54. BN_CTX* ctx = BN_CTX_new();
  55. BIGNUM* r32 = BN_new();
  56. BIGNUM* rr = BN_new();
  57. BIGNUM* r = BN_new();
  58. BIGNUM* rem = BN_new();
  59. BIGNUM* n = BN_new();
  60. BIGNUM* n0inv = BN_new();
  61. if (RSA_size(rsa) != RSANUMBYTES) {
  62. ret = 0;
  63. goto out;
  64. }
  65. BN_set_bit(r32, 32);
  66. BN_copy(n, rsa->n);
  67. BN_set_bit(r, RSANUMWORDS * 32);
  68. BN_mod_sqr(rr, r, n, ctx);
  69. BN_div(NULL, rem, n, r32, ctx);
  70. BN_mod_inverse(n0inv, rem, r32, ctx);
  71. pkey->len = RSANUMWORDS;
  72. pkey->n0inv = 0 - BN_get_word(n0inv);
  73. for (i = 0; i < RSANUMWORDS; i++) {
  74. BN_div(rr, rem, rr, r32, ctx);
  75. pkey->rr[i] = BN_get_word(rem);
  76. BN_div(n, rem, n, r32, ctx);
  77. pkey->n[i] = BN_get_word(rem);
  78. }
  79. pkey->exponent = BN_get_word(rsa->e);
  80. out:
  81. BN_free(n0inv);
  82. BN_free(n);
  83. BN_free(rem);
  84. BN_free(r);
  85. BN_free(rr);
  86. BN_free(r32);
  87. BN_CTX_free(ctx);
  88. return ret;
  89. }
  90. static void get_user_info(char *buf, size_t len)
  91. {
  92. char hostname[1024], username[1024];
  93. int ret;
  94. #ifndef _WIN32
  95. ret = gethostname(hostname, sizeof(hostname));
  96. if (ret < 0)
  97. #endif
  98. strcpy(hostname, "unknown");
  99. #if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
  100. ret = getlogin_r(username, sizeof(username));
  101. if (ret < 0)
  102. #endif
  103. strcpy(username, "unknown");
  104. ret = snprintf(buf, len, " %s@%s", username, hostname);
  105. if (ret >= (signed)len)
  106. buf[len - 1] = '\0';
  107. }
  108. static int write_public_keyfile(RSA *private_key, const char *private_key_path)
  109. {
  110. RSAPublicKey pkey;
  111. BIO *bio, *b64, *bfile;
  112. char path[PATH_MAX], info[MAX_PAYLOAD];
  113. int ret;
  114. ret = snprintf(path, sizeof(path), "%s.pub", private_key_path);
  115. if (ret >= (signed)sizeof(path))
  116. return 0;
  117. ret = RSA_to_RSAPublicKey(private_key, &pkey);
  118. if (!ret) {
  119. D("Failed to convert to publickey\n");
  120. return 0;
  121. }
  122. bfile = BIO_new_file(path, "w");
  123. if (!bfile) {
  124. D("Failed to open '%s'\n", path);
  125. return 0;
  126. }
  127. D("Writing public key to '%s'\n", path);
  128. b64 = BIO_new(BIO_f_base64());
  129. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  130. bio = BIO_push(b64, bfile);
  131. BIO_write(bio, &pkey, sizeof(pkey));
  132. BIO_flush(bio);
  133. BIO_pop(b64);
  134. BIO_free(b64);
  135. get_user_info(info, sizeof(info));
  136. BIO_write(bfile, info, strlen(info));
  137. BIO_flush(bfile);
  138. BIO_free_all(bfile);
  139. return 1;
  140. }
  141. static int generate_key(const char *file)
  142. {
  143. EVP_PKEY* pkey = EVP_PKEY_new();
  144. BIGNUM* exponent = BN_new();
  145. RSA* rsa = RSA_new();
  146. mode_t old_mask;
  147. FILE *f = NULL;
  148. int ret = 0;
  149. D("generate_key '%s'\n", file);
  150. if (!pkey || !exponent || !rsa) {
  151. D("Failed to allocate key\n");
  152. goto out;
  153. }
  154. BN_set_word(exponent, RSA_F4);
  155. RSA_generate_key_ex(rsa, 2048, exponent, NULL);
  156. EVP_PKEY_set1_RSA(pkey, rsa);
  157. old_mask = umask(077);
  158. f = fopen(file, "w");
  159. if (!f) {
  160. D("Failed to open '%s'\n", file);
  161. umask(old_mask);
  162. goto out;
  163. }
  164. umask(old_mask);
  165. if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
  166. D("Failed to write key\n");
  167. goto out;
  168. }
  169. if (!write_public_keyfile(rsa, file)) {
  170. D("Failed to write public key\n");
  171. goto out;
  172. }
  173. ret = 1;
  174. out:
  175. if (f)
  176. fclose(f);
  177. EVP_PKEY_free(pkey);
  178. RSA_free(rsa);
  179. BN_free(exponent);
  180. return ret;
  181. }
  182. static int read_key(const char *file, struct listnode *list)
  183. {
  184. struct adb_private_key *key;
  185. FILE *f;
  186. D("read_key '%s'\n", file);
  187. f = fopen(file, "r");
  188. if (!f) {
  189. D("Failed to open '%s'\n", file);
  190. return 0;
  191. }
  192. key = malloc(sizeof(*key));
  193. if (!key) {
  194. D("Failed to alloc key\n");
  195. fclose(f);
  196. return 0;
  197. }
  198. key->rsa = RSA_new();
  199. if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) {
  200. D("Failed to read key\n");
  201. fclose(f);
  202. RSA_free(key->rsa);
  203. free(key);
  204. return 0;
  205. }
  206. fclose(f);
  207. list_add_tail(list, &key->node);
  208. return 1;
  209. }
  210. static int get_user_keyfilepath(char *filename, size_t len)
  211. {
  212. const char *format, *home;
  213. char android_dir[PATH_MAX];
  214. struct stat buf;
  215. #ifdef _WIN32
  216. char path[PATH_MAX];
  217. home = getenv("ANDROID_SDK_HOME");
  218. if (!home) {
  219. SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path);
  220. home = path;
  221. }
  222. format = "%s\\%s";
  223. #else
  224. home = getenv("HOME");
  225. if (!home)
  226. return -1;
  227. format = "%s/%s";
  228. #endif
  229. D("home '%s'\n", home);
  230. if (snprintf(android_dir, sizeof(android_dir), format, home,
  231. ANDROID_PATH) >= (int)sizeof(android_dir))
  232. return -1;
  233. if (stat(android_dir, &buf)) {
  234. if (adb_mkdir(android_dir, 0750) < 0) {
  235. D("Cannot mkdir '%s'", android_dir);
  236. return -1;
  237. }
  238. }
  239. return snprintf(filename, len, format, android_dir, ADB_KEY_FILE);
  240. }
  241. static int get_user_key(struct listnode *list)
  242. {
  243. struct stat buf;
  244. char path[PATH_MAX];
  245. int ret;
  246. ret = get_user_keyfilepath(path, sizeof(path));
  247. if (ret < 0 || ret >= (signed)sizeof(path)) {
  248. D("Error getting user key filename");
  249. return 0;
  250. }
  251. D("user key '%s'\n", path);
  252. if (stat(path, &buf) == -1) {
  253. if (!generate_key(path)) {
  254. D("Failed to generate new key\n");
  255. return 0;
  256. }
  257. }
  258. return read_key(path, list);
  259. }
  260. static void get_vendor_keys(struct listnode *list)
  261. {
  262. const char *adb_keys_path;
  263. char keys_path[MAX_PAYLOAD];
  264. char *path;
  265. char *save;
  266. struct stat buf;
  267. adb_keys_path = getenv("ADB_VENDOR_KEYS");
  268. if (!adb_keys_path)
  269. return;
  270. strncpy(keys_path, adb_keys_path, sizeof(keys_path));
  271. path = adb_strtok_r(keys_path, ENV_PATH_SEPARATOR_STR, &save);
  272. while (path) {
  273. D("Reading: '%s'\n", path);
  274. if (stat(path, &buf))
  275. D("Can't read '%s'\n", path);
  276. else if (!read_key(path, list))
  277. D("Failed to read '%s'\n", path);
  278. path = adb_strtok_r(NULL, ENV_PATH_SEPARATOR_STR, &save);
  279. }
  280. }
  281. int adb_auth_sign(void *node, void *token, size_t token_size, void *sig)
  282. {
  283. unsigned int len;
  284. struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
  285. if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
  286. return 0;
  287. }
  288. D("adb_auth_sign len=%d\n", len);
  289. return (int)len;
  290. }
  291. void *adb_auth_nextkey(void *current)
  292. {
  293. struct listnode *item;
  294. if (list_empty(&key_list))
  295. return NULL;
  296. if (!current)
  297. return list_head(&key_list);
  298. list_for_each(item, &key_list) {
  299. if (item == current) {
  300. /* current is the last item, we tried all the keys */
  301. if (item->next == &key_list)
  302. return NULL;
  303. return item->next;
  304. }
  305. }
  306. return NULL;
  307. }
  308. int adb_auth_get_userkey(unsigned char *data, size_t len)
  309. {
  310. char path[PATH_MAX];
  311. char *file;
  312. int ret;
  313. ret = get_user_keyfilepath(path, sizeof(path) - 4);
  314. if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
  315. D("Error getting user key filename");
  316. return 0;
  317. }
  318. strcat(path, ".pub");
  319. file = load_file(path, (unsigned*)&ret);
  320. if (!file) {
  321. D("Can't load '%s'\n", path);
  322. return 0;
  323. }
  324. if (len < (size_t)(ret + 1)) {
  325. D("%s: Content too large ret=%d\n", path, ret);
  326. return 0;
  327. }
  328. memcpy(data, file, ret);
  329. data[ret] = '\0';
  330. return ret + 1;
  331. }
  332. void adb_auth_init(void)
  333. {
  334. int ret;
  335. D("adb_auth_init\n");
  336. list_init(&key_list);
  337. ret = get_user_key(&key_list);
  338. if (!ret) {
  339. D("Failed to get user key\n");
  340. return;
  341. }
  342. get_vendor_keys(&key_list);
  343. }