diff options
author | thekuwayama <thekuwayama@gmail.com> | 2019-06-20 19:31:01 +0900 |
---|---|---|
committer | thekuwayama <thekuwayama@gmail.com> | 2019-06-20 19:31:01 +0900 |
commit | 87953b640e359d2f9213dfba24af773ce06fec4f (patch) | |
tree | f7f91dd342dd7da7a0bc8a3c8f4502e67d23f383 /ext/openssl/ossl_pkey_rsa.c | |
parent | 1c7181d20003533fbe53f9505245812ae1646708 (diff) | |
download | ruby-openssl-87953b640e359d2f9213dfba24af773ce06fec4f.tar.gz |
fix RSA#export and RSA#to_der to check key, factors and crt_params
Diffstat (limited to 'ext/openssl/ossl_pkey_rsa.c')
-rw-r--r-- | ext/openssl/ossl_pkey_rsa.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c index 69dcfba1..761866c6 100644 --- a/ext/openssl/ossl_pkey_rsa.c +++ b/ext/openssl/ossl_pkey_rsa.c @@ -341,6 +341,7 @@ static VALUE ossl_rsa_export(int argc, VALUE *argv, VALUE self) { RSA *rsa; + const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp; BIO *out; const EVP_CIPHER *ciph = NULL; VALUE cipher, pass, str; @@ -356,7 +357,10 @@ ossl_rsa_export(int argc, VALUE *argv, VALUE self) if (!(out = BIO_new(BIO_s_mem()))) { ossl_raise(eRSAError, NULL); } - if (RSA_HAS_PRIVATE(rsa)) { + RSA_get0_key(rsa, &n, &e, &d); + RSA_get0_factors(rsa, &p, &q); + RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); + if (n && e && d && p && q && dmp1 && dmq1 && iqmp) { if (!PEM_write_bio_RSAPrivateKey(out, rsa, ciph, NULL, 0, ossl_pem_passwd_cb, (void *)pass)) { BIO_free(out); @@ -383,23 +387,27 @@ static VALUE ossl_rsa_to_der(VALUE self) { RSA *rsa; + const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp; int (*i2d_func)(const RSA *, unsigned char **); - unsigned char *p; + unsigned char *ptr; long len; VALUE str; GetRSA(self, rsa); - if (RSA_HAS_PRIVATE(rsa)) + RSA_get0_key(rsa, &n, &e, &d); + RSA_get0_factors(rsa, &p, &q); + RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); + if (n && e && d && p && q && dmp1 && dmq1 && iqmp) i2d_func = i2d_RSAPrivateKey; else i2d_func = (int (*)(const RSA *, unsigned char **))i2d_RSA_PUBKEY; if((len = i2d_func(rsa, NULL)) <= 0) ossl_raise(eRSAError, NULL); str = rb_str_new(0, len); - p = (unsigned char *)RSTRING_PTR(str); - if(i2d_func(rsa, &p) < 0) + ptr = (unsigned char *)RSTRING_PTR(str); + if(i2d_func(rsa, &ptr) < 0) ossl_raise(eRSAError, NULL); - ossl_str_adjust(str, p); + ossl_str_adjust(str, ptr); return str; } |