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); \
27RSA_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) {
106rsa_blocking_gen(
void *
arg)
114rsa_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)) {
275ossl_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);
302ossl_rsa_is_public(
VALUE self)
321ossl_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)) {
383ossl_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,
741ossl_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);
776ossl_rsa_to_text(
VALUE self)
783 if (!(out = BIO_new(BIO_s_mem()))) {
786 if (!RSA_print(out,
rsa, 0)) {
802ossl_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))
char str[HTML_ESCAPE_MAX_LEN+1]
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
VALUE rb_define_module_under(VALUE, const char *)
void rb_define_alias(VALUE, const char *, const char *)
Defines an alias of a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *)
VALUE rb_cObject
Object class.
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
#define BN_GENCB_free(cb)
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
VALUE ossl_pem_passwd_value(VALUE pass)
VALUE ossl_to_der_if_possible(VALUE obj)
void ossl_raise(VALUE exc, const char *fmt,...)
void ossl_clear_error(void)
#define ossl_str_adjust(str, p)
#define OSSL_BIO_reset(bio)
BIO * ossl_obj2bio(volatile VALUE *pobj)
VALUE ossl_membio2str(BIO *bio)
VALUE ossl_bn_new(const BIGNUM *bn)
const EVP_CIPHER * ossl_evp_get_cipherbyname(VALUE obj)
const EVP_MD * ossl_evp_get_digestbyname(VALUE obj)
EVP_PKEY * GetPrivPKeyPtr(VALUE obj)
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
void ossl_generate_cb_stop(void *ptr)
#define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3)
#define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2)
#define GetPKey(obj, pkey)
#define SetPKey(obj, pkey)
#define DEF_OSSL_PKEY_BN(class, keytype, name)
#define OSSL_PKEY_IS_PRIVATE(obj)
#define GetPKeyRSA(obj, pkey)
VALUE ossl_rsa_new(EVP_PKEY *pkey)
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)