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);
36 RSA_PRIVATE(
VALUE obj, RSA *rsa)
51 rsa_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) {
106 rsa_blocking_gen(
void *arg)
114 rsa_generate(
int size,
unsigned long exp)
119 RSA *
rsa = RSA_new();
120 BIGNUM *
e = BN_new();
123 if (!rsa || !e || !cb) {
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);
190 obj = rsa_instance(klass, rsa);
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;
346 VALUE cipher, pass, str;
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;
396 if((len = i2d_func(rsa,
NULL)) <= 0)
400 if(i2d_func(rsa, &p) < 0)
421 VALUE str, buffer, padding;
424 RSA_get0_key(rsa, &rsa_n,
NULL,
NULL);
428 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
453 VALUE str, buffer, padding;
456 RSA_get0_key(rsa, &rsa_n,
NULL,
NULL);
460 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
485 VALUE str, buffer, padding;
488 RSA_get0_key(rsa, &rsa_n,
NULL,
NULL);
491 if (!RSA_PRIVATE(
self, rsa))
494 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
519 VALUE str, buffer, padding;
522 RSA_get0_key(rsa, &rsa_n,
NULL,
NULL);
525 if (!RSA_PRIVATE(
self, rsa))
528 pad = (
argc == 1) ? RSA_PKCS1_PADDING :
NUM2INT(padding);
551 ossl_rsa_get_params(
VALUE self)
555 const BIGNUM *n, *
e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
558 RSA_get0_key(rsa, &n, &e, &d);
559 RSA_get0_factors(rsa, &p, &q);
560 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
586 ossl_rsa_to_text(
VALUE self)
593 if (!(out = BIO_new(BIO_s_mem()))) {
596 if (!RSA_print(out, rsa, 0)) {
612 ossl_rsa_to_public_key(
VALUE self)
620 rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(pkey));
687 #define DefRSAConst(x) rb_define_const(cRSA, #x, INT2NUM(RSA_##x))
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
#define SetPKey(obj, pkey)
#define ossl_str_adjust(str, p)
BIO * ossl_obj2bio(volatile VALUE *pobj)
void rb_str_set_len(VALUE, long)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
#define GetPKey(obj, pkey)
#define OSSL_PKEY_IS_PRIVATE(obj)
VALUE ossl_rsa_new(EVP_PKEY *pkey)
VALUE ossl_membio2str(BIO *bio)
#define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2)
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
VALUE ossl_to_der_if_possible(VALUE obj)
void ossl_generate_cb_stop(void *ptr)
void ossl_clear_error(void)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
RUBY_EXTERN VALUE rb_cObject
const EVP_CIPHER * ossl_evp_get_cipherbyname(VALUE obj)
#define OSSL_BIO_reset(bio)
#define DEF_OSSL_PKEY_BN(class, keytype, name)
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
#define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
#define GetPKeyRSA(obj, pkey)
VALUE ossl_pem_passwd_value(VALUE pass)
register unsigned int len
VALUE rb_define_module_under(VALUE outer, const char *name)
void ossl_raise(VALUE exc, const char *fmt,...)
#define BN_GENCB_free(cb)
VALUE ossl_bn_new(const BIGNUM *bn)
#define RSTRING_LENINT(str)
#define RB_INTEGER_TYPE_P(obj)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_new(const char *, long)