crypto_rsa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_rsa.c
  8. * \brief Block of functions related with RSA utilities and operations.
  9. **/
  10. #include "lib/crypt_ops/crypto_cipher.h"
  11. #include "lib/crypt_ops/crypto_curve25519.h"
  12. #include "lib/crypt_ops/crypto_digest.h"
  13. #include "lib/crypt_ops/crypto_format.h"
  14. #include "lib/crypt_ops/compat_openssl.h"
  15. #include "lib/crypt_ops/crypto_rand.h"
  16. #include "lib/crypt_ops/crypto_rsa.h"
  17. #include "lib/crypt_ops/crypto_util.h"
  18. #include "lib/ctime/di_ops.h"
  19. #include "lib/log/util_bug.h"
  20. #include "lib/fs/files.h"
  21. #include "lib/log/log.h"
  22. #include "lib/encoding/binascii.h"
  23. #include "lib/encoding/pem.h"
  24. #include <string.h>
  25. #ifdef HAVE_SYS_STAT_H
  26. #include <sys/stat.h>
  27. #endif
  28. /** Return the number of bytes added by padding method <b>padding</b>.
  29. */
  30. int
  31. crypto_get_rsa_padding_overhead(int padding)
  32. {
  33. switch (padding)
  34. {
  35. case PK_PKCS1_OAEP_PADDING: return PKCS1_OAEP_PADDING_OVERHEAD;
  36. default: tor_assert(0); return -1; // LCOV_EXCL_LINE
  37. }
  38. }
  39. #ifdef ENABLE_OPENSSL
  40. /** Given a padding method <b>padding</b>, return the correct OpenSSL constant.
  41. */
  42. int
  43. crypto_get_rsa_padding(int padding)
  44. {
  45. switch (padding)
  46. {
  47. case PK_PKCS1_OAEP_PADDING: return RSA_PKCS1_OAEP_PADDING;
  48. default: tor_assert(0); return -1; // LCOV_EXCL_LINE
  49. }
  50. }
  51. #endif
  52. /** Compare the public-key components of a and b. Return non-zero iff
  53. * a==b. A NULL key is considered to be distinct from all non-NULL
  54. * keys, and equal to itself.
  55. *
  56. * Note that this may leak information about the keys through timing.
  57. */
  58. int
  59. crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b)
  60. {
  61. return (crypto_pk_cmp_keys(a, b) == 0);
  62. }
  63. /** Perform a hybrid (public/secret) encryption on <b>fromlen</b>
  64. * bytes of data from <b>from</b>, with padding type 'padding',
  65. * storing the results on <b>to</b>.
  66. *
  67. * Returns the number of bytes written on success, -1 on failure.
  68. *
  69. * The encrypted data consists of:
  70. * - The source data, padded and encrypted with the public key, if the
  71. * padded source data is no longer than the public key, and <b>force</b>
  72. * is false, OR
  73. * - The beginning of the source data prefixed with a 16-byte symmetric key,
  74. * padded and encrypted with the public key; followed by the rest of
  75. * the source data encrypted in AES-CTR mode with the symmetric key.
  76. *
  77. * NOTE that this format does not authenticate the symmetrically encrypted
  78. * part of the data, and SHOULD NOT BE USED for new protocols.
  79. */
  80. int
  81. crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env,
  82. char *to, size_t tolen,
  83. const char *from,
  84. size_t fromlen,
  85. int padding, int force)
  86. {
  87. int overhead, outlen, r;
  88. size_t pkeylen, symlen;
  89. crypto_cipher_t *cipher = NULL;
  90. char *buf = NULL;
  91. tor_assert(env);
  92. tor_assert(from);
  93. tor_assert(to);
  94. tor_assert(fromlen < SIZE_T_CEILING);
  95. overhead = crypto_get_rsa_padding_overhead(padding);
  96. pkeylen = crypto_pk_keysize(env);
  97. if (!force && fromlen+overhead <= pkeylen) {
  98. /* It all fits in a single encrypt. */
  99. return crypto_pk_public_encrypt(env,to,
  100. tolen,
  101. from,fromlen,padding);
  102. }
  103. tor_assert(tolen >= fromlen + overhead + CIPHER_KEY_LEN);
  104. tor_assert(tolen >= pkeylen);
  105. char key[CIPHER_KEY_LEN];
  106. crypto_rand(key, sizeof(key)); /* generate a new key. */
  107. cipher = crypto_cipher_new(key);
  108. buf = tor_malloc(pkeylen+1);
  109. memcpy(buf, key, CIPHER_KEY_LEN);
  110. memcpy(buf+CIPHER_KEY_LEN, from, pkeylen-overhead-CIPHER_KEY_LEN);
  111. /* Length of symmetrically encrypted data. */
  112. symlen = fromlen-(pkeylen-overhead-CIPHER_KEY_LEN);
  113. outlen = crypto_pk_public_encrypt(env,to,tolen,buf,pkeylen-overhead,padding);
  114. if (outlen!=(int)pkeylen) {
  115. goto err;
  116. }
  117. r = crypto_cipher_encrypt(cipher, to+outlen,
  118. from+pkeylen-overhead-CIPHER_KEY_LEN, symlen);
  119. if (r<0) goto err;
  120. memwipe(buf, 0, pkeylen);
  121. memwipe(key, 0, sizeof(key));
  122. tor_free(buf);
  123. crypto_cipher_free(cipher);
  124. tor_assert(outlen+symlen < INT_MAX);
  125. return (int)(outlen + symlen);
  126. err:
  127. memwipe(buf, 0, pkeylen);
  128. memwipe(key, 0, sizeof(key));
  129. tor_free(buf);
  130. crypto_cipher_free(cipher);
  131. return -1;
  132. }
  133. /** Invert crypto_pk_obsolete_public_hybrid_encrypt. Returns the number of
  134. * bytes written on success, -1 on failure.
  135. *
  136. * NOTE that this format does not authenticate the symmetrically encrypted
  137. * part of the data, and SHOULD NOT BE USED for new protocols.
  138. */
  139. int
  140. crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env,
  141. char *to,
  142. size_t tolen,
  143. const char *from,
  144. size_t fromlen,
  145. int padding, int warnOnFailure)
  146. {
  147. int outlen, r;
  148. size_t pkeylen;
  149. crypto_cipher_t *cipher = NULL;
  150. char *buf = NULL;
  151. tor_assert(fromlen < SIZE_T_CEILING);
  152. pkeylen = crypto_pk_keysize(env);
  153. if (fromlen <= pkeylen) {
  154. return crypto_pk_private_decrypt(env,to,tolen,from,fromlen,padding,
  155. warnOnFailure);
  156. }
  157. buf = tor_malloc(pkeylen);
  158. outlen = crypto_pk_private_decrypt(env,buf,pkeylen,from,pkeylen,padding,
  159. warnOnFailure);
  160. if (outlen<0) {
  161. log_fn(warnOnFailure?LOG_WARN:LOG_DEBUG, LD_CRYPTO,
  162. "Error decrypting public-key data");
  163. goto err;
  164. }
  165. if (outlen < CIPHER_KEY_LEN) {
  166. log_fn(warnOnFailure?LOG_WARN:LOG_INFO, LD_CRYPTO,
  167. "No room for a symmetric key");
  168. goto err;
  169. }
  170. cipher = crypto_cipher_new(buf);
  171. if (!cipher) {
  172. goto err;
  173. }
  174. memcpy(to,buf+CIPHER_KEY_LEN,outlen-CIPHER_KEY_LEN);
  175. outlen -= CIPHER_KEY_LEN;
  176. tor_assert(tolen - outlen >= fromlen - pkeylen);
  177. r = crypto_cipher_decrypt(cipher, to+outlen, from+pkeylen, fromlen-pkeylen);
  178. if (r<0)
  179. goto err;
  180. memwipe(buf,0,pkeylen);
  181. tor_free(buf);
  182. crypto_cipher_free(cipher);
  183. tor_assert(outlen + fromlen < INT_MAX);
  184. return (int)(outlen + (fromlen-pkeylen));
  185. err:
  186. memwipe(buf,0,pkeylen);
  187. tor_free(buf);
  188. crypto_cipher_free(cipher);
  189. return -1;
  190. }
  191. /** Given a private or public key <b>pk</b>, put a fingerprint of the
  192. * public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1 bytes of
  193. * space). Return 0 on success, -1 on failure.
  194. *
  195. * Fingerprints are computed as the SHA1 digest of the ASN.1 encoding
  196. * of the public key, converted to hexadecimal, in upper case, with a
  197. * space after every four digits.
  198. *
  199. * If <b>add_space</b> is false, omit the spaces.
  200. */
  201. int
  202. crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
  203. {
  204. char digest[DIGEST_LEN];
  205. char hexdigest[HEX_DIGEST_LEN+1];
  206. if (crypto_pk_get_digest(pk, digest)) {
  207. return -1;
  208. }
  209. base16_encode(hexdigest,sizeof(hexdigest),digest,DIGEST_LEN);
  210. if (add_space) {
  211. crypto_add_spaces_to_fp(fp_out, FINGERPRINT_LEN+1, hexdigest);
  212. } else {
  213. strncpy(fp_out, hexdigest, HEX_DIGEST_LEN+1);
  214. }
  215. return 0;
  216. }
  217. /** Given a private or public key <b>pk</b>, put a hashed fingerprint of
  218. * the public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1
  219. * bytes of space). Return 0 on success, -1 on failure.
  220. *
  221. * Hashed fingerprints are computed as the SHA1 digest of the SHA1 digest
  222. * of the ASN.1 encoding of the public key, converted to hexadecimal, in
  223. * upper case.
  224. */
  225. int
  226. crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out)
  227. {
  228. char digest[DIGEST_LEN], hashed_digest[DIGEST_LEN];
  229. if (crypto_pk_get_digest(pk, digest)) {
  230. return -1;
  231. }
  232. if (crypto_digest(hashed_digest, digest, DIGEST_LEN) < 0) {
  233. return -1;
  234. }
  235. base16_encode(fp_out, FINGERPRINT_LEN + 1, hashed_digest, DIGEST_LEN);
  236. return 0;
  237. }
  238. /** Copy <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, adding spaces
  239. * every four characters. */
  240. void
  241. crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
  242. {
  243. int n = 0;
  244. char *end = out+outlen;
  245. tor_assert(outlen < SIZE_T_CEILING);
  246. while (*in && out<end) {
  247. *out++ = *in++;
  248. if (++n == 4 && *in && out<end) {
  249. n = 0;
  250. *out++ = ' ';
  251. }
  252. }
  253. tor_assert(out<end);
  254. *out = '\0';
  255. }
  256. /** Check a siglen-byte long signature at <b>sig</b> against
  257. * <b>datalen</b> bytes of data at <b>data</b>, using the public key
  258. * in <b>env</b>. Return 0 if <b>sig</b> is a correct signature for
  259. * SHA1(data). Else return -1.
  260. */
  261. MOCK_IMPL(int,
  262. crypto_pk_public_checksig_digest,(crypto_pk_t *env, const char *data,
  263. size_t datalen, const char *sig,
  264. size_t siglen))
  265. {
  266. char digest[DIGEST_LEN];
  267. char *buf;
  268. size_t buflen;
  269. int r;
  270. tor_assert(env);
  271. tor_assert(data);
  272. tor_assert(sig);
  273. tor_assert(datalen < SIZE_T_CEILING);
  274. tor_assert(siglen < SIZE_T_CEILING);
  275. if (crypto_digest(digest,data,datalen)<0) {
  276. log_warn(LD_BUG, "couldn't compute digest");
  277. return -1;
  278. }
  279. buflen = crypto_pk_keysize(env);
  280. buf = tor_malloc(buflen);
  281. r = crypto_pk_public_checksig(env,buf,buflen,sig,siglen);
  282. if (r != DIGEST_LEN) {
  283. log_warn(LD_CRYPTO, "Invalid signature");
  284. tor_free(buf);
  285. return -1;
  286. }
  287. if (tor_memneq(buf, digest, DIGEST_LEN)) {
  288. log_warn(LD_CRYPTO, "Signature mismatched with digest.");
  289. tor_free(buf);
  290. return -1;
  291. }
  292. tor_free(buf);
  293. return 0;
  294. }
  295. /** Compute a SHA1 digest of <b>fromlen</b> bytes of data stored at
  296. * <b>from</b>; sign the data with the private key in <b>env</b>, and
  297. * store it in <b>to</b>. Return the number of bytes written on
  298. * success, and -1 on failure.
  299. *
  300. * <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
  301. * at least the length of the modulus of <b>env</b>.
  302. */
  303. int
  304. crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  305. const char *from, size_t fromlen)
  306. {
  307. int r;
  308. char digest[DIGEST_LEN];
  309. if (crypto_digest(digest,from,fromlen)<0)
  310. return -1;
  311. r = crypto_pk_private_sign(env,to,tolen,digest,DIGEST_LEN);
  312. memwipe(digest, 0, sizeof(digest));
  313. return r;
  314. }
  315. /** Given a private or public key <b>pk</b>, put a SHA1 hash of the
  316. * public key into <b>digest_out</b> (must have DIGEST_LEN bytes of space).
  317. * Return 0 on success, -1 on failure.
  318. */
  319. int
  320. crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
  321. {
  322. char *buf;
  323. size_t buflen;
  324. int len;
  325. int rv = -1;
  326. buflen = crypto_pk_keysize(pk)*2;
  327. buf = tor_malloc(buflen);
  328. len = crypto_pk_asn1_encode(pk, buf, buflen);
  329. if (len < 0)
  330. goto done;
  331. if (crypto_digest(digest_out, buf, len) < 0)
  332. goto done;
  333. rv = 0;
  334. done:
  335. tor_free(buf);
  336. return rv;
  337. }
  338. /** Compute all digests of the DER encoding of <b>pk</b>, and store them
  339. * in <b>digests_out</b>. Return 0 on success, -1 on failure. */
  340. int
  341. crypto_pk_get_common_digests(crypto_pk_t *pk, common_digests_t *digests_out)
  342. {
  343. char *buf;
  344. size_t buflen;
  345. int len;
  346. int rv = -1;
  347. buflen = crypto_pk_keysize(pk)*2;
  348. buf = tor_malloc(buflen);
  349. len = crypto_pk_asn1_encode(pk, buf, buflen);
  350. if (len < 0)
  351. goto done;
  352. if (crypto_common_digests(digests_out, (char*)buf, len) < 0)
  353. goto done;
  354. rv = 0;
  355. done:
  356. tor_free(buf);
  357. return rv;
  358. }
  359. static const char RSA_PUBLIC_TAG[] = "RSA PUBLIC KEY";
  360. static const char RSA_PRIVATE_TAG[] = "RSA PRIVATE KEY";
  361. /* These are overestimates for how many extra bytes we might need to encode
  362. * a key in DER */
  363. #define PRIVATE_ASN_MAX_OVERHEAD_FACTOR 16
  364. #define PUBLIC_ASN_MAX_OVERHEAD_FACTOR 3
  365. /** Helper: PEM-encode <b>env</b> and write it to a newly allocated string.
  366. * If <b>private_key</b>, write the private part of <b>env</b>; otherwise
  367. * write only the public portion. On success, set *<b>dest</b> to the new
  368. * string, *<b>len</b> to the string's length, and return 0. On failure,
  369. * return -1.
  370. */
  371. static int
  372. crypto_pk_write_to_string_generic(crypto_pk_t *env,
  373. char **dest, size_t *len,
  374. bool private_key)
  375. {
  376. const int factor =
  377. private_key ? PRIVATE_ASN_MAX_OVERHEAD_FACTOR
  378. : PUBLIC_ASN_MAX_OVERHEAD_FACTOR;
  379. size_t buflen = crypto_pk_keysize(env) * factor;
  380. const char *tag =
  381. private_key ? RSA_PRIVATE_TAG : RSA_PUBLIC_TAG;
  382. char *buf = tor_malloc(buflen);
  383. char *result = NULL;
  384. size_t resultlen = 0;
  385. int rv = -1;
  386. int n = private_key
  387. ? crypto_pk_asn1_encode_private(env, buf, buflen)
  388. : crypto_pk_asn1_encode(env, buf, buflen);
  389. if (n < 0)
  390. goto done;
  391. resultlen = pem_encoded_size(n, tag);
  392. result = tor_malloc(resultlen);
  393. if (pem_encode(result, resultlen,
  394. (const unsigned char *)buf, n, tag) < 0) {
  395. goto done;
  396. }
  397. *dest = result;
  398. *len = resultlen;
  399. rv = 0;
  400. done:
  401. if (rv < 0 && result) {
  402. memwipe(result, 0, resultlen);
  403. tor_free(result);
  404. }
  405. memwipe(buf, 0, buflen);
  406. tor_free(buf);
  407. return rv;
  408. }
  409. /** PEM-encode the public key portion of <b>env</b> and write it to a
  410. * newly allocated string. On success, set *<b>dest</b> to the new
  411. * string, *<b>len</b> to the string's length, and return 0. On
  412. * failure, return -1.
  413. */
  414. int
  415. crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  416. char **dest, size_t *len)
  417. {
  418. return crypto_pk_write_to_string_generic(env, dest, len, false);
  419. }
  420. /** PEM-encode the private key portion of <b>env</b> and write it to a
  421. * newly allocated string. On success, set *<b>dest</b> to the new
  422. * string, *<b>len</b> to the string's length, and return 0. On
  423. * failure, return -1.
  424. */
  425. int
  426. crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  427. char **dest, size_t *len)
  428. {
  429. return crypto_pk_write_to_string_generic(env, dest, len, true);
  430. }
  431. /**
  432. * Helper. Read a PEM-encoded RSA from the first <b>len</b> characters of
  433. * <b>src</b>, and store the result in <b>env</b>. If <b>private_key</b>,
  434. * expect a private key; otherwise expect a public key. Return 0 on success,
  435. * -1 on failure. If len is -1, the string is nul-terminated.
  436. */
  437. static int
  438. crypto_pk_read_from_string_generic(crypto_pk_t *env, const char *src,
  439. size_t len, bool private_key)
  440. {
  441. if (len == (size_t)-1) // "-1" indicates "use the length of the string."
  442. len = strlen(src);
  443. const char *tag =
  444. private_key ? RSA_PRIVATE_TAG : RSA_PUBLIC_TAG;
  445. size_t buflen = len;
  446. uint8_t *buf = tor_malloc(buflen);
  447. int rv = -1;
  448. int n = pem_decode(buf, buflen, src, len, tag);
  449. if (n < 0)
  450. goto done;
  451. crypto_pk_t *pk = private_key
  452. ? crypto_pk_asn1_decode_private((const char*)buf, n)
  453. : crypto_pk_asn1_decode((const char*)buf, n);
  454. if (! pk)
  455. goto done;
  456. if (private_key)
  457. crypto_pk_assign_private(env, pk);
  458. else
  459. crypto_pk_assign_public(env, pk);
  460. crypto_pk_free(pk);
  461. rv = 0;
  462. done:
  463. memwipe(buf, 0, buflen);
  464. tor_free(buf);
  465. return rv;
  466. }
  467. /** Read a PEM-encoded public key from the first <b>len</b> characters of
  468. * <b>src</b>, and store the result in <b>env</b>. Return 0 on success, -1 on
  469. * failure. If len is -1, the string is nul-terminated.
  470. */
  471. int
  472. crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  473. const char *src, size_t len)
  474. {
  475. return crypto_pk_read_from_string_generic(env, src, len, false);
  476. }
  477. /** Read a PEM-encoded private key from the <b>len</b>-byte string <b>src</b>
  478. * into <b>env</b>. Return 0 on success, -1 on failure. If len is -1,
  479. * the string is nul-terminated.
  480. */
  481. int
  482. crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  483. const char *src, ssize_t len)
  484. {
  485. return crypto_pk_read_from_string_generic(env, src, len, true);
  486. }
  487. /** If a file is longer than this, we won't try to decode its private key */
  488. #define MAX_PRIVKEY_FILE_LEN (16*1024*1024)
  489. /** Read a PEM-encoded private key from the file named by
  490. * <b>keyfile</b> into <b>env</b>. Return 0 on success, -1 on failure.
  491. */
  492. int
  493. crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  494. const char *keyfile)
  495. {
  496. struct stat st;
  497. char *buf = read_file_to_str(keyfile, 0, &st);
  498. if (!buf)
  499. return -1;
  500. if (st.st_size > MAX_PRIVKEY_FILE_LEN)
  501. return -1;
  502. int rv = crypto_pk_read_private_key_from_string(env, buf,
  503. (ssize_t)st.st_size);
  504. memwipe(buf, 0, (size_t)st.st_size);
  505. tor_free(buf);
  506. return rv;
  507. }
  508. /** Write the private key from <b>env</b> into the file named by <b>fname</b>,
  509. * PEM-encoded. Return 0 on success, -1 on failure.
  510. */
  511. int
  512. crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  513. const char *fname)
  514. {
  515. char *s = NULL;
  516. size_t n = 0;
  517. if (crypto_pk_write_private_key_to_string(env, &s, &n) < 0)
  518. return -1;
  519. int rv = write_bytes_to_file(fname, s, n, 0);
  520. memwipe(s, 0, n);
  521. tor_free(s);
  522. return rv;
  523. }
  524. /** Given a crypto_pk_t <b>pk</b>, allocate a new buffer containing the
  525. * Base64 encoding of the DER representation of the private key as a NUL
  526. * terminated string, and return it via <b>priv_out</b>. Return 0 on
  527. * success, -1 on failure.
  528. *
  529. * It is the caller's responsibility to sanitize and free the resulting buffer.
  530. */
  531. int
  532. crypto_pk_base64_encode_private(const crypto_pk_t *pk, char **priv_out)
  533. {
  534. size_t buflen = crypto_pk_keysize(pk)*16;
  535. char *buf = tor_malloc(buflen);
  536. char *result = NULL;
  537. size_t reslen = 0;
  538. bool ok = false;
  539. int n = crypto_pk_asn1_encode_private(pk, buf, buflen);
  540. if (n < 0)
  541. goto done;
  542. reslen = base64_encode_size(n, 0)+1;
  543. result = tor_malloc(reslen);
  544. if (base64_encode(result, reslen, buf, n, 0) < 0)
  545. goto done;
  546. ok = true;
  547. done:
  548. memwipe(buf, 0, buflen);
  549. tor_free(buf);
  550. if (result && ! ok) {
  551. memwipe(result, 0, reslen);
  552. tor_free(result);
  553. }
  554. *priv_out = result;
  555. return ok ? 0 : -1;
  556. }
  557. /** Given a string containing the Base64 encoded DER representation of the
  558. * private key <b>str</b>, decode and return the result on success, or NULL
  559. * on failure.
  560. */
  561. crypto_pk_t *
  562. crypto_pk_base64_decode_private(const char *str, size_t len)
  563. {
  564. crypto_pk_t *pk = NULL;
  565. char *der = tor_malloc_zero(len + 1);
  566. int der_len = base64_decode(der, len, str, len);
  567. if (der_len <= 0) {
  568. log_warn(LD_CRYPTO, "Stored RSA private key seems corrupted (base64).");
  569. goto out;
  570. }
  571. pk = crypto_pk_asn1_decode_private(der, der_len);
  572. out:
  573. memwipe(der, 0, len+1);
  574. tor_free(der);
  575. return pk;
  576. }