12#include "lib/crypt_ops/compat_openssl.h"
19DISABLE_GCC_WARNING(
"-Wredundant-decls")
21#include <openssl/err.h>
22#include <openssl/rsa.h>
23#include <openssl/pem.h>
24#include <openssl/evp.h>
25#include <openssl/engine.h>
26#include <openssl/rand.h>
27#include <openssl/bn.h>
28#include <openssl/conf.h>
30ENABLE_GCC_WARNING(
"-Wredundant-decls")
32#include "lib/log/log.h"
54 RSA_get0_factors(k->key, &p, &q);
61crypto_new_pk_from_openssl_rsa_(RSA *rsa)
76 return RSAPrivateKey_dup(env->
key);
83crypto_pk_get_openssl_evp_pkey_,(
crypto_pk_t *env,
int private))
86 EVP_PKEY *pkey = NULL;
89 if (!(key = RSAPrivateKey_dup(env->
key)))
92 if (!(key = RSAPublicKey_dup(env->
key)))
95 if (!(pkey = EVP_PKEY_new()))
97 if (!(EVP_PKEY_assign_RSA(pkey, key)))
118 return crypto_new_pk_from_openssl_rsa_(rsa);
154 BIGNUM *e = BN_new();
163 if (RSA_generate_key_ex(r, bits, e, NULL) == -1)
191 r = RSA_check_key(env->
key);
212 RSA_get0_key(env->
key, &n, &e, &d);
226 char a_is_non_null = (a != NULL) && (a->
key != NULL);
227 char b_is_non_null = (b != NULL) && (b->
key != NULL);
228 char an_argument_is_null = !a_is_non_null | !b_is_non_null;
230 result =
tor_memcmp(&a_is_non_null, &b_is_non_null,
sizeof(a_is_non_null));
231 if (an_argument_is_null)
234 const BIGNUM *a_n, *a_e;
235 const BIGNUM *b_n, *b_e;
237 const BIGNUM *a_d, *b_d;
238 RSA_get0_key(a->
key, &a_n, &a_e, &a_d);
239 RSA_get0_key(b->
key, &b_n, &b_e, &b_d);
244 result = BN_cmp(a_n, b_n);
247 return BN_cmp(a_e, b_e);
257 return (
size_t) RSA_size((RSA*)env->
key);
270 const BIGNUM *n, *e, *d;
271 RSA_get0_key(env->
key, &n, &e, &d);
274 return RSA_bits(env->
key);
299 dest->
key = RSAPrivateKey_dup(src->
key);
312 dest->
key = RSAPublicKey_dup(src->
key);
326 new_key = RSAPrivateKey_dup(env->
key);
329 new_key = RSAPublicKey_dup(env->
key);
336 log_err(
LD_CRYPTO,
"Unable to duplicate a %s key: openssl failed.",
337 privatekey?
"private":
"public");
339 privatekey ?
"Duplicating a private key" :
340 "Duplicating a public key");
346 return crypto_new_pk_from_openssl_rsa_(new_key);
359 const char *from,
size_t fromlen,
int padding)
368 r = RSA_public_encrypt((
int)fromlen,
369 (
unsigned char*)from, (
unsigned char*)to,
370 env->
key, crypto_get_rsa_padding(padding));
389 const char *from,
size_t fromlen,
390 int padding,
int warnOnFailure)
403 r = RSA_private_decrypt((
int)fromlen,
404 (
unsigned char*)from, (
unsigned char*)to,
405 env->
key, crypto_get_rsa_padding(padding));
409 "performing RSA decryption");
426 const char *from,
size_t fromlen))
434 r = RSA_public_decrypt((
int)fromlen,
435 (
unsigned char*)from, (
unsigned char*)to,
436 env->
key, RSA_PKCS1_PADDING);
455 const char *from,
size_t fromlen)
467 r = RSA_private_encrypt((
int)fromlen,
468 (
unsigned char*)from, (
unsigned char*)to,
469 (RSA*)env->
key, RSA_PKCS1_PADDING);
484 unsigned char *buf = NULL;
486 len = i2d_RSAPublicKey(pk->
key, &buf);
487 if (len < 0 || buf == NULL)
497 memcpy(dest,buf,len);
510 const unsigned char *cp;
511 cp = buf = tor_malloc(len);
513 rsa = d2i_RSAPublicKey(NULL, &cp, len);
519 return crypto_new_pk_from_openssl_rsa_(rsa);
530 unsigned char *buf = NULL;
532 len = i2d_RSAPrivateKey(pk->
key, &buf);
533 if (len < 0 || buf == NULL)
543 memcpy(dest,buf,len);
552rsa_private_key_too_long(RSA *rsa,
int max_bits)
554 const BIGNUM *n, *e, *p, *q, *d, *dmp1, *dmq1, *iqmp;
561 dmp1 = RSA_get0_dmp1(rsa);
562 dmq1 = RSA_get0_dmq1(rsa);
563 iqmp = RSA_get0_iqmp(rsa);
565 if (RSA_bits(rsa) > max_bits)
568 if (n && BN_num_bits(n) > max_bits)
570 if (e && BN_num_bits(e) > max_bits)
572 if (p && BN_num_bits(p) > max_bits)
574 if (q && BN_num_bits(q) > max_bits)
576 if (d && BN_num_bits(d) > max_bits)
578 if (dmp1 && BN_num_bits(dmp1) > max_bits)
580 if (dmq1 && BN_num_bits(dmq1) > max_bits)
582 if (iqmp && BN_num_bits(iqmp) > max_bits)
599 const unsigned char *cp;
600 cp = buf = tor_malloc(len);
602 rsa = d2i_RSAPrivateKey(NULL, &cp, len);
608 if (max_bits >= 0 && rsa_private_key_too_long(rsa, max_bits)) {
609 log_info(
LD_CRYPTO,
"Private key longer than expected.");
613 crypto_pk_t *result = crypto_new_pk_from_openssl_rsa_(rsa);
615 crypto_pk_free(result);
void crypto_openssl_log_errors(int severity, const char *doing)
Headers for crypto_rsa.c.
void crypto_pk_assign_private(crypto_pk_t *dest, const crypto_pk_t *src)
crypto_pk_t * crypto_pk_new(void)
int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b)
int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
size_t crypto_pk_keysize(const crypto_pk_t *env)
int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
int crypto_pk_is_valid_private_key(const crypto_pk_t *env)
int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding, int warnOnFailure)
int crypto_pk_asn1_encode(const crypto_pk_t *pk, char *dest, size_t dest_len)
crypto_pk_t * crypto_pk_asn1_decode(const char *str, size_t len)
crypto_pk_t * crypto_pk_copy_full(crypto_pk_t *orig)
int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding)
void crypto_pk_assign_public(crypto_pk_t *dest, const crypto_pk_t *src)
crypto_pk_t * crypto_pk_asn1_decode_private(const char *str, size_t len, int max_bits)
int crypto_pk_num_bits(crypto_pk_t *env)
crypto_pk_t * crypto_pk_dup_key(crypto_pk_t *orig)
int crypto_pk_key_is_private(const crypto_pk_t *key)
int crypto_pk_asn1_encode_private(const crypto_pk_t *pk, char *dest, size_t dest_len)
int crypto_pk_public_checksig(const crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
int crypto_pk_public_exponent_ok(const crypto_pk_t *env)
void crypto_pk_free_(crypto_pk_t *env)
Common functions for cryptographic routines.
int tor_memcmp(const void *a, const void *b, size_t len)
Wrappers for reading and writing data to files on disk.
#define MOCK_IMPL(rv, funcname, arglist)
Macros to manage assertions, fatal and non-fatal.
#define tor_fragile_assert()