crypto_rsa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. /** PEM-encode the public key portion of <b>env</b> and write it to a
  362. * newly allocated string. On success, set *<b>dest</b> to the new
  363. * string, *<b>len</b> to the string's length, and return 0. On
  364. * failure, return -1.
  365. */
  366. int
  367. crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  368. char **dest, size_t *len)
  369. {
  370. size_t buflen = crypto_pk_keysize(env) * 3;
  371. char *buf = tor_malloc(buflen);
  372. char *result = NULL;
  373. size_t resultlen = 0;
  374. int rv = -1;
  375. int n = crypto_pk_asn1_encode(env, buf, buflen);
  376. if (n < 0)
  377. goto done;
  378. resultlen = pem_encoded_size(n, RSA_PUBLIC_TAG);
  379. result = tor_malloc(resultlen);
  380. if (pem_encode(result, resultlen,
  381. (const unsigned char *)buf, n, RSA_PUBLIC_TAG) < 0) {
  382. goto done;
  383. }
  384. *dest = result;
  385. *len = resultlen;
  386. rv = 0;
  387. done:
  388. if (rv < 0 && result) {
  389. memwipe(result, 0, resultlen);
  390. tor_free(result);
  391. }
  392. memwipe(buf, 0, buflen);
  393. tor_free(buf);
  394. return rv;
  395. }
  396. /** PEM-encode the private key portion of <b>env</b> and write it to a
  397. * newly allocated string. On success, set *<b>dest</b> to the new
  398. * string, *<b>len</b> to the string's length, and return 0. On
  399. * failure, return -1.
  400. */
  401. int
  402. crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  403. char **dest, size_t *len)
  404. {
  405. size_t buflen = crypto_pk_keysize(env) * 16;
  406. char *buf = tor_malloc(buflen);
  407. char *result = NULL;
  408. size_t resultlen = 0;
  409. int rv = -1;
  410. int n = crypto_pk_asn1_encode_private(env, buf, buflen);
  411. if (n < 0)
  412. goto done;
  413. resultlen = pem_encoded_size(n, RSA_PRIVATE_TAG);
  414. result = tor_malloc(resultlen);
  415. if (pem_encode(result, resultlen,
  416. (const unsigned char *)buf, n, RSA_PRIVATE_TAG) < 0)
  417. goto done;
  418. *dest = result;
  419. *len = resultlen;
  420. rv = 0;
  421. done:
  422. if (rv < 0 && result) {
  423. memwipe(result, 0, resultlen);
  424. tor_free(result);
  425. }
  426. memwipe(buf, 0, buflen);
  427. tor_free(buf);
  428. return rv;
  429. }
  430. /** Read a PEM-encoded public key from the first <b>len</b> characters of
  431. * <b>src</b>, and store the result in <b>env</b>. Return 0 on success, -1 on
  432. * failure.
  433. */
  434. int
  435. crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  436. const char *src, size_t len)
  437. {
  438. if (len == (size_t)-1)
  439. len = strlen(src);
  440. size_t buflen = len;
  441. uint8_t *buf = tor_malloc(buflen);
  442. int rv = -1;
  443. int n = pem_decode(buf, buflen, src, len, RSA_PUBLIC_TAG);
  444. if (n < 0)
  445. goto done;
  446. crypto_pk_t *pk = crypto_pk_asn1_decode((const char*)buf, n);
  447. if (! pk)
  448. goto done;
  449. crypto_pk_assign_public(env, pk);
  450. crypto_pk_free(pk);
  451. rv = 0;
  452. done:
  453. memwipe(buf, 0, buflen);
  454. tor_free(buf);
  455. return rv;
  456. }
  457. /** Read a PEM-encoded private key from the <b>len</b>-byte string <b>s</b>
  458. * into <b>env</b>. Return 0 on success, -1 on failure. If len is -1,
  459. * the string is nul-terminated.
  460. */
  461. int
  462. crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  463. const char *s, ssize_t len)
  464. {
  465. if (len == -1)
  466. len = strlen(s);
  467. size_t buflen = len;
  468. uint8_t *buf = tor_malloc(buflen);
  469. int rv = -1;
  470. int n = pem_decode(buf, buflen, s, len, RSA_PRIVATE_TAG);
  471. if (n < 0) {
  472. goto done;
  473. }
  474. crypto_pk_t *pk = crypto_pk_asn1_decode_private((const char *)buf, n);
  475. if (! pk)
  476. goto done;
  477. crypto_pk_assign_private(env, pk);
  478. crypto_pk_free(pk);
  479. rv = 0;
  480. done:
  481. memwipe(buf, 0, buflen);
  482. tor_free(buf);
  483. return rv;
  484. }
  485. /** Read a PEM-encoded private key from the file named by
  486. * <b>keyfile</b> into <b>env</b>. Return 0 on success, -1 on failure.
  487. */
  488. int
  489. crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  490. const char *keyfile)
  491. {
  492. struct stat st;
  493. char *buf = read_file_to_str(keyfile, 0, &st);
  494. if (!buf)
  495. return -1;
  496. int rv = crypto_pk_read_private_key_from_string(env, buf, st.st_size);
  497. memwipe(buf, 0, st.st_size);
  498. tor_free(buf);
  499. return rv;
  500. }
  501. /** Write the private key from <b>env</b> into the file named by <b>fname</b>,
  502. * PEM-encoded. Return 0 on success, -1 on failure.
  503. */
  504. int
  505. crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  506. const char *fname)
  507. {
  508. char *s = NULL;
  509. size_t n = 0;
  510. if (crypto_pk_write_private_key_to_string(env, &s, &n) < 0)
  511. return -1;
  512. int rv = write_bytes_to_file(fname, s, n, 0);
  513. memwipe(s, 0, n);
  514. tor_free(s);
  515. return rv;
  516. }
  517. /** Given a crypto_pk_t <b>pk</b>, allocate a new buffer containing the
  518. * Base64 encoding of the DER representation of the private key as a NUL
  519. * terminated string, and return it via <b>priv_out</b>. Return 0 on
  520. * success, -1 on failure.
  521. *
  522. * It is the caller's responsibility to sanitize and free the resulting buffer.
  523. */
  524. int
  525. crypto_pk_base64_encode_private(const crypto_pk_t *pk, char **priv_out)
  526. {
  527. size_t buflen = crypto_pk_keysize(pk)*16;
  528. char *buf = tor_malloc(buflen);
  529. char *result = NULL;
  530. size_t reslen = 0;
  531. bool ok = false;
  532. int n = crypto_pk_asn1_encode_private(pk, buf, buflen);
  533. if (n < 0)
  534. goto done;
  535. reslen = base64_encode_size(n, 0)+1;
  536. result = tor_malloc(reslen);
  537. if (base64_encode(result, reslen, buf, n, 0) < 0)
  538. goto done;
  539. ok = true;
  540. done:
  541. memwipe(buf, 0, buflen);
  542. tor_free(buf);
  543. if (result && ! ok) {
  544. memwipe(result, 0, reslen);
  545. tor_free(result);
  546. }
  547. *priv_out = result;
  548. return ok ? 0 : -1;
  549. }
  550. /** Given a string containing the Base64 encoded DER representation of the
  551. * private key <b>str</b>, decode and return the result on success, or NULL
  552. * on failure.
  553. */
  554. crypto_pk_t *
  555. crypto_pk_base64_decode_private(const char *str, size_t len)
  556. {
  557. crypto_pk_t *pk = NULL;
  558. char *der = tor_malloc_zero(len + 1);
  559. int der_len = base64_decode(der, len, str, len);
  560. if (der_len <= 0) {
  561. log_warn(LD_CRYPTO, "Stored RSA private key seems corrupted (base64).");
  562. goto out;
  563. }
  564. pk = crypto_pk_asn1_decode_private(der, der_len);
  565. out:
  566. memwipe(der, 0, len+1);
  567. tor_free(der);
  568. return pk;
  569. }