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_key(rsa,
NULL, &e, &d);
36RSA_PRIVATE(
VALUE obj, RSA *rsa)
51rsa_instance(
VALUE klass, RSA *rsa)
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) {
190 obj = rsa_instance(klass,
rsa);
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)
344 const BIGNUM *n, *
e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
346 const EVP_CIPHER *ciph =
NULL;
353 if (!
NIL_P(cipher)) {
357 if (!(
out = BIO_new(BIO_s_mem()))) {
360 RSA_get0_key(
rsa, &n, &
e, &d);
361 RSA_get0_factors(
rsa, &p, &q);
362 RSA_get0_crt_params(
rsa, &dmp1, &dmq1, &iqmp);
363 if (n &&
e && d && p && q && dmp1 && dmq1 && iqmp) {
364 if (!PEM_write_bio_RSAPrivateKey(
out,
rsa, ciph,
NULL, 0,
370 if (!PEM_write_bio_RSA_PUBKEY(
out,
rsa)) {
387ossl_rsa_to_der(
VALUE self)
390 const BIGNUM *n, *
e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
391 int (*i2d_func)(
const RSA *,
unsigned char **);
397 RSA_get0_key(
rsa, &n, &
e, &d);
398 RSA_get0_factors(
rsa, &p, &q);
399 RSA_get0_crt_params(
rsa, &dmp1, &dmq1, &iqmp);
400 if (n &&
e && d && p && q && dmp1 && dmq1 && iqmp)
401 i2d_func = i2d_RSAPrivateKey;
403 i2d_func = (
int (*)(
const RSA *,
unsigned char **))i2d_RSA_PUBKEY;
408 if(i2d_func(
rsa, &
ptr) < 0)
436 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
439 buf_len = RSA_public_encrypt(RSTRING_LENINT(buffer), (
unsigned char *)
RSTRING_PTR(buffer),
468 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
471 buf_len = RSA_public_decrypt(RSTRING_LENINT(buffer), (
unsigned char *)
RSTRING_PTR(buffer),
499 if (!RSA_PRIVATE(self,
rsa))
502 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
505 buf_len = RSA_private_encrypt(RSTRING_LENINT(buffer), (
unsigned char *)
RSTRING_PTR(buffer),
533 if (!RSA_PRIVATE(self,
rsa))
536 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
539 buf_len = RSA_private_decrypt(RSTRING_LENINT(buffer), (
unsigned char *)
RSTRING_PTR(buffer),
583 VALUE digest, data, options, kwargs[2], signature;
584 static ID kwargs_ids[2];
586 EVP_PKEY_CTX *pkey_ctx;
587 const EVP_MD *md, *mgf1md;
592 if (!kwargs_ids[0]) {
593 kwargs_ids[0] = rb_intern_const(
"salt_length");
594 kwargs_ids[1] = rb_intern_const(
"mgf1_hash");
607 buf_len = EVP_PKEY_size(pkey);
616 if (EVP_DigestSignInit(md_ctx, &pkey_ctx, md,
NULL, pkey) != 1)
619 if (EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1)
622 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len) != 1)
625 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1md) != 1)
631 if (EVP_DigestSignFinal(md_ctx, (
unsigned char *)
RSTRING_PTR(signature), &buf_len) != 1)
670 VALUE digest, signature, data, options, kwargs[2];
671 static ID kwargs_ids[2];
673 EVP_PKEY_CTX *pkey_ctx;
674 const EVP_MD *md, *mgf1md;
678 if (!kwargs_ids[0]) {
679 kwargs_ids[0] = rb_intern_const(
"salt_length");
680 kwargs_ids[1] = rb_intern_const(
"mgf1_hash");
701 if (EVP_DigestVerifyInit(md_ctx, &pkey_ctx, md,
NULL, pkey) != 1)
704 if (EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1)
707 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len) != 1)
710 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1md) != 1)
716 result = EVP_DigestVerifyFinal(md_ctx,
749ossl_rsa_get_params(
VALUE self)
753 const BIGNUM *n, *
e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
756 RSA_get0_key(
rsa, &n, &
e, &d);
757 RSA_get0_factors(
rsa, &p, &q);
758 RSA_get0_crt_params(
rsa, &dmp1, &dmq1, &iqmp);
784ossl_rsa_to_text(
VALUE self)
791 if (!(
out = BIO_new(BIO_s_mem()))) {
794 if (!RSA_print(
out,
rsa, 0)) {
810ossl_rsa_to_public_key(
VALUE self)
818 rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(pkey));
885#define DefRSAConst(x) rb_define_const(cRSA, #x, INT2NUM(RSA_##x))
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
char str[HTML_ESCAPE_MAX_LEN+1]
#define RSTRING_LEN(string)
#define RSTRING_PTR(string)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module_under(VALUE outer, const char *name)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
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 *values)
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
VALUE rb_cObject
Object class.
VALUE rb_obj_class(VALUE)
unsigned in(void *in_desc, z_const unsigned char **buf)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
#define rb_str_new(str, len)
void rb_str_set_len(VALUE, long)
ID rb_intern(const char *)
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
#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)
#define RB_INTEGER_TYPE_P(obj)