crypto_rsa.c 17 KB

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