Skip to content

Commit da72839

Browse files
rheniumtenderworks
authored andcommitted
[ruby/openssl] pkey/ec: check existence of public key component before exporting
i2d_PUBKEY_bio() against an EC_KEY without the public key component trggers a null dereference. This is a regression introduced by commit ruby/openssl@56f0d34d63fb ("pkey: refactor #export/#to_pem and #to_der", 2017-06-14). Fixes ruby/openssl#527 (comment) Fixes ruby/openssl#369 (comment) ruby/openssl@f6ee0fa4de
1 parent dda139d commit da72839

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

ext/openssl/ossl_pkey_ec.c

+4
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ ossl_ec_key_export(int argc, VALUE *argv, VALUE self)
414414
EC_KEY *ec;
415415

416416
GetEC(self, ec);
417+
if (EC_KEY_get0_public_key(ec) == NULL)
418+
ossl_raise(eECError, "can't export - no public key set");
417419
if (EC_KEY_get0_private_key(ec))
418420
return ossl_pkey_export_traditional(argc, argv, self, 0);
419421
else
@@ -432,6 +434,8 @@ ossl_ec_key_to_der(VALUE self)
432434
EC_KEY *ec;
433435

434436
GetEC(self, ec);
437+
if (EC_KEY_get0_public_key(ec) == NULL)
438+
ossl_raise(eECError, "can't export - no public key set");
435439
if (EC_KEY_get0_private_key(ec))
436440
return ossl_pkey_export_traditional(0, NULL, self, 1);
437441
else

test/openssl/test_pkey_ec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def test_generate
6161
def test_generate_key
6262
ec = OpenSSL::PKey::EC.new("prime256v1")
6363
assert_equal false, ec.private?
64+
assert_raise(OpenSSL::PKey::ECError) { ec.to_der }
6465
ec.generate_key!
6566
assert_equal true, ec.private?
67+
assert_nothing_raised { ec.to_der }
6668
end if !openssl?(3, 0, 0)
6769

6870
def test_marshal

0 commit comments

Comments
 (0)