12 #if !defined(OPENSSL_NO_RSA)
14 #define GetPKeyRSA(obj, pkey) do { \
15 GetPKey((obj), (pkey)); \
16 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) { \
17 ossl_raise(rb_eRuntimeError, "THIS IS NOT A RSA!") ; \
20 #define GetRSA(obj, rsa) do { \
22 GetPKeyRSA((obj), _pkey); \
23 (rsa) = EVP_PKEY_get0_RSA(_pkey); \
27 RSA_HAS_PRIVATE(RSA *rsa)
31 RSA_get0_factors(rsa, &p, &q);
60 if (!(pkey = EVP_PKEY_new())) {
63 if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
78 obj = rsa_instance(
cRSA, RSA_new());
82 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
106 rsa_blocking_gen(
void *
arg)
114 rsa_generate(
int size,
unsigned long exp)
119 RSA *
rsa = RSA_new();
120 BIGNUM *
e = BN_new();
129 for (
i = 0;
i < (
int)
sizeof(
exp) * 8; ++
i) {
130 if (
exp & (1UL <<
i)) {
131 if (BN_set_bit(
e,
i) == 0) {
147 if (cb_arg.
yield == 1) {
149 rsa_blocking_gen(&gen_arg);
157 if (!gen_arg.result) {
247 rsa = d2i_RSAPrivateKey_bio(in,
NULL);
251 rsa = d2i_RSA_PUBKEY_bio(in,
NULL);
259 rsa = d2i_RSAPublicKey_bio(in,
NULL);
266 if (!EVP_PKEY_assign_RSA(pkey,
rsa)) {
275 ossl_rsa_initialize_copy(
VALUE self,
VALUE other)
281 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
285 rsa_new = ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey, (d2i_of_void *)d2i_RSAPrivateKey, (
char *)
rsa);
289 EVP_PKEY_assign_RSA(pkey, rsa_new);
302 ossl_rsa_is_public(
VALUE self)
321 ossl_rsa_is_private(
VALUE self)
345 const EVP_CIPHER *ciph =
NULL;
352 if (!
NIL_P(cipher)) {
356 if (!(out = BIO_new(BIO_s_mem()))) {
359 if (RSA_HAS_PRIVATE(
rsa)) {
360 if (!PEM_write_bio_RSAPrivateKey(out,
rsa, ciph,
NULL, 0,
366 if (!PEM_write_bio_RSA_PUBKEY(out,
rsa)) {
383 ossl_rsa_to_der(
VALUE self)
386 int (*i2d_func)(
const RSA *,
unsigned char **);
392 if (RSA_HAS_PRIVATE(
rsa))
393 i2d_func = i2d_RSAPrivateKey;
395 i2d_func = (
int (*)(
const RSA *,
unsigned char **))i2d_RSA_PUBKEY;
400 if(i2d_func(
rsa, &p) < 0)
428 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
460 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
491 if (!RSA_PRIVATE(
self,
rsa))
494 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
525 if (!RSA_PRIVATE(
self,
rsa))
528 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
575 VALUE digest, data, options, kwargs[2], signature;
576 static ID kwargs_ids[2];
578 EVP_PKEY_CTX *pkey_ctx;
579 const EVP_MD *md, *mgf1md;
584 if (!kwargs_ids[0]) {
599 buf_len = EVP_PKEY_size(pkey);
608 if (EVP_DigestSignInit(md_ctx, &pkey_ctx, md,
NULL, pkey) != 1)
611 if (EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1)
614 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len) != 1)
617 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1md) != 1)
623 if (EVP_DigestSignFinal(md_ctx, (
unsigned char *)
RSTRING_PTR(signature), &buf_len) != 1)
662 VALUE digest, signature, data, options, kwargs[2];
663 static ID kwargs_ids[2];
665 EVP_PKEY_CTX *pkey_ctx;
666 const EVP_MD *md, *mgf1md;
670 if (!kwargs_ids[0]) {
693 if (EVP_DigestVerifyInit(md_ctx, &pkey_ctx, md,
NULL, pkey) != 1)
696 if (EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1)
699 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len) != 1)
702 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1md) != 1)
708 result = EVP_DigestVerifyFinal(md_ctx,
741 ossl_rsa_get_params(
VALUE self)
745 const BIGNUM *
n, *
e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
748 RSA_get0_key(
rsa, &
n, &
e, &d);
749 RSA_get0_factors(
rsa, &p, &q);
750 RSA_get0_crt_params(
rsa, &dmp1, &dmq1, &iqmp);
776 ossl_rsa_to_text(
VALUE self)
783 if (!(out = BIO_new(BIO_s_mem()))) {
786 if (!RSA_print(out,
rsa, 0)) {
802 ossl_rsa_to_public_key(
VALUE self)
810 rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(pkey));
877 #define DefRSAConst(x) rb_define_const(cRSA, #x, INT2NUM(RSA_##x))