010-mbedtls.patch 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. --- a/adb/adb_auth_host.c
  2. +++ b/adb/adb_auth_host.c
  3. @@ -39,15 +39,13 @@
  4. #include <cutils/list.h>
  5. -#include <openssl/evp.h>
  6. -#include <openssl/objects.h>
  7. -#include <openssl/pem.h>
  8. -#include <openssl/rsa.h>
  9. -#include <openssl/sha.h>
  10. -
  11. -#if defined(OPENSSL_IS_BORINGSSL)
  12. -#include <openssl/base64.h>
  13. -#endif
  14. +#include <mbedtls/rsa.h>
  15. +#include <mbedtls/sha1.h>
  16. +#include <mbedtls/base64.h>
  17. +#include <mbedtls/entropy.h>
  18. +#include <mbedtls/ctr_drbg.h>
  19. +#include <mbedtls/pk.h>
  20. +#include <mbedtls/pem.h>
  21. #define TRACE_TAG TRACE_AUTH
  22. @@ -57,56 +55,92 @@
  23. struct adb_private_key {
  24. struct listnode node;
  25. - RSA *rsa;
  26. + mbedtls_pk_context pk;
  27. };
  28. static struct listnode key_list;
  29. +static mbedtls_ctr_drbg_context ctr_drbg;
  30. /* Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format */
  31. -static int RSA_to_RSAPublicKey(RSA *rsa, RSAPublicKey *pkey)
  32. +static int RSA_to_RSAPublicKey(mbedtls_pk_context *pk, RSAPublicKey *pkey)
  33. {
  34. int ret = 1;
  35. unsigned int i;
  36. + mbedtls_mpi r32, rr, r, rem, n, n0inv, e, tmp;
  37. + mbedtls_rsa_context *rsa;
  38. + unsigned char buf[sizeof(uint32_t)];
  39. +
  40. + if (mbedtls_pk_get_type(pk) != MBEDTLS_PK_RSA) {
  41. + return 0;
  42. + }
  43. - BN_CTX* ctx = BN_CTX_new();
  44. - BIGNUM* r32 = BN_new();
  45. - BIGNUM* rr = BN_new();
  46. - BIGNUM* r = BN_new();
  47. - BIGNUM* rem = BN_new();
  48. - BIGNUM* n = BN_new();
  49. - BIGNUM* n0inv = BN_new();
  50. + rsa = mbedtls_pk_rsa(*pk);
  51. + if (!rsa) {
  52. + return 0;
  53. + }
  54. - if (RSA_size(rsa) != RSANUMBYTES) {
  55. + mbedtls_mpi_init(&r32);
  56. + mbedtls_mpi_init(&rr);
  57. + mbedtls_mpi_init(&r);
  58. + mbedtls_mpi_init(&rem);
  59. + mbedtls_mpi_init(&n);
  60. + mbedtls_mpi_init(&n0inv);
  61. + mbedtls_mpi_init(&e);
  62. + mbedtls_mpi_init(&tmp);
  63. +
  64. + if (mbedtls_rsa_get_len(rsa) != RSANUMBYTES) {
  65. ret = 0;
  66. goto out;
  67. }
  68. - BN_set_bit(r32, 32);
  69. - BN_copy(n, rsa->n);
  70. - BN_set_bit(r, RSANUMWORDS * 32);
  71. - BN_mod_sqr(rr, r, n, ctx);
  72. - BN_div(NULL, rem, n, r32, ctx);
  73. - BN_mod_inverse(n0inv, rem, r32, ctx);
  74. + mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, &e);
  75. +
  76. + mbedtls_mpi_lset(&r32, 1);
  77. + mbedtls_mpi_shift_l(&r32, 32);
  78. + mbedtls_mpi_lset(&r, 1);
  79. + mbedtls_mpi_shift_l(&r, RSANUMWORDS * 32);
  80. + mbedtls_mpi_mul_mpi(&rr, &r, &r);
  81. + mbedtls_mpi_mod_mpi(&rr, &rr, &n);
  82. + mbedtls_mpi_div_mpi(NULL, &rem, &n, &r32);
  83. + mbedtls_mpi_inv_mod(&n0inv, &rem, &r32);
  84. pkey->len = RSANUMWORDS;
  85. - pkey->n0inv = 0 - BN_get_word(n0inv);
  86. +
  87. + mbedtls_mpi_write_binary(&n0inv, buf, sizeof(buf));
  88. + uint32_t n0inv_val = ((buf[0] << 24) | (buf[1] << 16) |
  89. + (buf[2] << 8) | buf[3]);
  90. + pkey->n0inv = 0 - n0inv_val;
  91. +
  92. for (i = 0; i < RSANUMWORDS; i++) {
  93. - BN_div(rr, rem, rr, r32, ctx);
  94. - pkey->rr[i] = BN_get_word(rem);
  95. - BN_div(n, rem, n, r32, ctx);
  96. - pkey->n[i] = BN_get_word(rem);
  97. + mbedtls_mpi_div_mpi(&tmp, &rem, &rr, &r32);
  98. + mbedtls_mpi_copy(&rr, &tmp);
  99. +
  100. + mbedtls_mpi_write_binary(&rem, buf, sizeof(buf));
  101. + pkey->rr[i] = ((buf[0] << 24) | (buf[1] << 16) |
  102. + (buf[2] << 8) | buf[3]);
  103. +
  104. + mbedtls_mpi_div_mpi(&tmp, &rem, &n, &r32);
  105. + mbedtls_mpi_copy(&n, &tmp);
  106. +
  107. + mbedtls_mpi_write_binary(&rem, buf, sizeof(buf));
  108. + pkey->n[i] = ((buf[0] << 24) | (buf[1] << 16) |
  109. + (buf[2] << 8) | buf[3]);
  110. }
  111. - pkey->exponent = BN_get_word(rsa->e);
  112. +
  113. + mbedtls_mpi_write_binary(&e, buf, sizeof(buf));
  114. + pkey->exponent = ((buf[0] << 24) | (buf[1] << 16) |
  115. + (buf[2] << 8) | buf[3]);
  116. out:
  117. - BN_free(n0inv);
  118. - BN_free(n);
  119. - BN_free(rem);
  120. - BN_free(r);
  121. - BN_free(rr);
  122. - BN_free(r32);
  123. - BN_CTX_free(ctx);
  124. + mbedtls_mpi_free(&tmp);
  125. + mbedtls_mpi_free(&e);
  126. + mbedtls_mpi_free(&n0inv);
  127. + mbedtls_mpi_free(&n);
  128. + mbedtls_mpi_free(&rem);
  129. + mbedtls_mpi_free(&r);
  130. + mbedtls_mpi_free(&rr);
  131. + mbedtls_mpi_free(&r32);
  132. return ret;
  133. }
  134. @@ -133,7 +167,7 @@ static void get_user_info(char *buf, siz
  135. buf[len - 1] = '\0';
  136. }
  137. -static int write_public_keyfile(RSA *private_key, const char *private_key_path)
  138. +static int write_public_keyfile(mbedtls_pk_context *private_key, const char *private_key_path)
  139. {
  140. RSAPublicKey pkey;
  141. FILE *outfile = NULL;
  142. @@ -161,16 +195,7 @@ static int write_public_keyfile(RSA *pri
  143. D("Writing public key to '%s'\n", path);
  144. -#if defined(OPENSSL_IS_BORINGSSL)
  145. - if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
  146. - D("Public key too large to base64 encode");
  147. - goto out;
  148. - }
  149. -#else
  150. - /* While we switch from OpenSSL to BoringSSL we have to implement
  151. - * |EVP_EncodedLength| here. */
  152. encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
  153. -#endif
  154. encoded = malloc(encoded_length);
  155. if (encoded == NULL) {
  156. @@ -178,7 +203,12 @@ static int write_public_keyfile(RSA *pri
  157. goto out;
  158. }
  159. - encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
  160. + if (mbedtls_base64_encode(encoded, encoded_length, &encoded_length,
  161. + (unsigned char*)&pkey, sizeof(pkey)) != 0) {
  162. + D("Base64 encoding failed");
  163. + goto out;
  164. + }
  165. +
  166. get_user_info(info, sizeof(info));
  167. if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
  168. @@ -201,23 +231,25 @@ static int write_public_keyfile(RSA *pri
  169. static int generate_key(const char *file)
  170. {
  171. - EVP_PKEY* pkey = EVP_PKEY_new();
  172. - BIGNUM* exponent = BN_new();
  173. - RSA* rsa = RSA_new();
  174. + mbedtls_pk_context pk;
  175. mode_t old_mask;
  176. FILE *f = NULL;
  177. int ret = 0;
  178. D("generate_key '%s'\n", file);
  179. - if (!pkey || !exponent || !rsa) {
  180. - D("Failed to allocate key\n");
  181. + mbedtls_pk_init(&pk);
  182. +
  183. + if (mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) != 0) {
  184. + D("Failed to setup key context\n");
  185. goto out;
  186. }
  187. - BN_set_word(exponent, RSA_F4);
  188. - RSA_generate_key_ex(rsa, 2048, exponent, NULL);
  189. - EVP_PKEY_set1_RSA(pkey, rsa);
  190. + if (mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk), mbedtls_ctr_drbg_random, &ctr_drbg,
  191. + 2048, 65537) != 0) {
  192. + D("Failed to generate key\n");
  193. + goto out;
  194. + }
  195. old_mask = umask(077);
  196. @@ -230,12 +262,20 @@ static int generate_key(const char *file
  197. umask(old_mask);
  198. - if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
  199. - D("Failed to write key\n");
  200. + unsigned char buf[16000];
  201. + size_t len;
  202. +
  203. + if (mbedtls_pk_write_key_pem(&pk, buf, sizeof(buf)) != 0) {
  204. + D("Failed to write key to buffer\n");
  205. goto out;
  206. }
  207. - if (!write_public_keyfile(rsa, file)) {
  208. + if (fwrite(buf, strlen((char*)buf), 1, f) != 1) {
  209. + D("Failed to write buffer to file\n");
  210. + goto out;
  211. + }
  212. +
  213. + if (!write_public_keyfile(&pk, file)) {
  214. D("Failed to write public key\n");
  215. goto out;
  216. }
  217. @@ -243,44 +283,32 @@ static int generate_key(const char *file
  218. ret = 1;
  219. out:
  220. - if (f)
  221. + if (f) {
  222. fclose(f);
  223. - EVP_PKEY_free(pkey);
  224. - RSA_free(rsa);
  225. - BN_free(exponent);
  226. + }
  227. + mbedtls_pk_free(&pk);
  228. return ret;
  229. }
  230. static int read_key(const char *file, struct listnode *list)
  231. {
  232. struct adb_private_key *key;
  233. - FILE *f;
  234. -
  235. - D("read_key '%s'\n", file);
  236. -
  237. - f = fopen(file, "r");
  238. - if (!f) {
  239. - D("Failed to open '%s'\n", file);
  240. - return 0;
  241. - }
  242. key = malloc(sizeof(*key));
  243. if (!key) {
  244. D("Failed to alloc key\n");
  245. - fclose(f);
  246. return 0;
  247. }
  248. - key->rsa = RSA_new();
  249. - if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) {
  250. + mbedtls_pk_init(&key->pk);
  251. +
  252. + if (mbedtls_pk_parse_keyfile(&key->pk, file, NULL, mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
  253. D("Failed to read key\n");
  254. - fclose(f);
  255. - RSA_free(key->rsa);
  256. + mbedtls_pk_free(&key->pk);
  257. free(key);
  258. return 0;
  259. }
  260. - fclose(f);
  261. list_add_tail(list, &key->node);
  262. return 1;
  263. }
  264. @@ -373,15 +401,20 @@ static void get_vendor_keys(struct listn
  265. int adb_auth_sign(void *node, void *token, size_t token_size, void *sig)
  266. {
  267. - unsigned int len;
  268. struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
  269. + unsigned char hash[20];
  270. + size_t sig_len;
  271. +
  272. + mbedtls_sha1((unsigned char*)token, token_size, hash);
  273. - if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
  274. + if (mbedtls_pk_sign(&key->pk, MBEDTLS_MD_SHA1, hash, sizeof(hash),
  275. + sig, mbedtls_pk_get_len(&key->pk), &sig_len,
  276. + mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
  277. return 0;
  278. }
  279. - D("adb_auth_sign len=%d\n", len);
  280. - return (int)len;
  281. + D("adb_auth_sign len=%d\n", (int)sig_len);
  282. + return (int)sig_len;
  283. }
  284. void *adb_auth_nextkey(void *current)
  285. @@ -439,10 +472,19 @@ int adb_auth_get_userkey(unsigned char *
  286. void adb_auth_init(void)
  287. {
  288. int ret;
  289. + mbedtls_entropy_context entropy;
  290. D("adb_auth_init\n");
  291. list_init(&key_list);
  292. + mbedtls_entropy_init(&entropy);
  293. + mbedtls_ctr_drbg_init(&ctr_drbg);
  294. +
  295. + if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  296. + (const unsigned char *)"adb_auth", 8) != 0) {
  297. + D("Failed to seed RNG\n");
  298. + return;
  299. + }
  300. ret = get_user_key(&key_list);
  301. if (!ret) {