aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2019-07-04 11:34:45 +1200
committerGitHub <noreply@github.com>2019-07-04 11:34:45 +1200
commit96efa29e613b2b311a85714978a70939e7eb81b0 (patch)
tree93755ea113df156e9ab0e1df7bc4457ea164b384
parent5c4391f767b5db55ffa73531ff6449a87b6c1154 (diff)
parente30b9a27f00338b065e90c6172d1c4509edc2853 (diff)
downloadruby-openssl-96efa29e613b2b311a85714978a70939e7eb81b0.tar.gz
Merge pull request #255 from jeremyevans/rsa-private-encrypt-segv
Fix segfaults in OpenSSL::PKey::RSA#private_{en,de}crypt when private exp not set
-rw-r--r--ext/openssl/ossl_pkey_rsa.c12
-rw-r--r--test/test_pkey_rsa.rb9
2 files changed, 15 insertions, 6 deletions
diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c
index 761866c6..e09813a4 100644
--- a/ext/openssl/ossl_pkey_rsa.c
+++ b/ext/openssl/ossl_pkey_rsa.c
@@ -488,13 +488,13 @@ static VALUE
ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self)
{
RSA *rsa;
- const BIGNUM *rsa_n;
+ const BIGNUM *rsa_n, *rsa_d;
int buf_len, pad;
VALUE str, buffer, padding;
GetRSA(self, rsa);
- RSA_get0_key(rsa, &rsa_n, NULL, NULL);
- if (!rsa_n)
+ RSA_get0_key(rsa, &rsa_n, NULL, &rsa_d);
+ if (!rsa_n || !rsa_d)
ossl_raise(eRSAError, "incomplete RSA");
if (!RSA_PRIVATE(self, rsa))
ossl_raise(eRSAError, "private key needed.");
@@ -522,13 +522,13 @@ static VALUE
ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
{
RSA *rsa;
- const BIGNUM *rsa_n;
+ const BIGNUM *rsa_n, *rsa_d;
int buf_len, pad;
VALUE str, buffer, padding;
GetRSA(self, rsa);
- RSA_get0_key(rsa, &rsa_n, NULL, NULL);
- if (!rsa_n)
+ RSA_get0_key(rsa, &rsa_n, NULL, &rsa_d);
+ if (!rsa_n || !rsa_d)
ossl_raise(eRSAError, "incomplete RSA");
if (!RSA_PRIVATE(self, rsa))
ossl_raise(eRSAError, "private key needed.");
diff --git a/test/test_pkey_rsa.rb b/test/test_pkey_rsa.rb
index 58558daa..b368a9cb 100644
--- a/test/test_pkey_rsa.rb
+++ b/test/test_pkey_rsa.rb
@@ -4,6 +4,15 @@ require_relative "utils"
if defined?(OpenSSL)
class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
+ def test_no_private_exp
+ key = OpenSSL::PKey::RSA.new
+ rsa = Fixtures.pkey("rsa2048")
+ key.set_key(rsa.n, rsa.e, nil)
+ key.set_factors(rsa.p, rsa.q)
+ assert_raise(OpenSSL::PKey::RSAError){ key.private_encrypt("foo") }
+ assert_raise(OpenSSL::PKey::RSAError){ key.private_decrypt("foo") }
+ end
+
def test_padding
key = OpenSSL::PKey::RSA.new(512, 3)