Skip to content

Commit

Permalink
pkey: make PKey.read raise PKey::PKeyError rather than ArgumentError
Browse files Browse the repository at this point in the history
PKey.read is a generic method to load an arbitrary PKey structure from a
PEM or DER encoded String. Each PKey classes's constructor also can load
from a String, but the behavior on error is different. While they raises
its own exception (are subclasses of PKey::PKeyError), PKey.read raises
ArgumentError.  [Bug #11774]
  • Loading branch information
rhenium committed Jul 3, 2016
1 parent 08e1881 commit 2a833d8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)

BIO_free(bio);
if (!pkey)
ossl_raise(rb_eArgError, "Could not parse PKey");
ossl_raise(ePKeyError, "Could not parse PKey");

return ossl_pkey_new(pkey);
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_pkey_dsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_export_password_length
def test_export_password_funny
key = OpenSSL::TestUtils::TEST_KEY_DSA256
pem = key.export(OpenSSL::Cipher.new('AES-128-CBC'), "pass\0wd")
assert_raise(ArgumentError) do
assert_raise(OpenSSL::PKey::PKeyError) do
OpenSSL::PKey.read(pem, "pass")
end
key2 = OpenSSL::PKey.read(pem, "pass\0wd")
Expand Down
2 changes: 1 addition & 1 deletion test/test_pkey_ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_export_password_length
def test_export_password_funny
key = OpenSSL::TestUtils::TEST_KEY_EC_P256V1
pem = key.export(OpenSSL::Cipher.new('AES-128-CBC'), "pass\0wd")
assert_raise(ArgumentError) do
assert_raise(OpenSSL::PKey::PKeyError) do
OpenSSL::PKey.read(pem, "pass")
end
key2 = OpenSSL::PKey.read(pem, "pass\0wd")
Expand Down
4 changes: 2 additions & 2 deletions test/test_pkey_rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_read_private_key_pem_pw
def test_read_private_key_pem_pw_exception
pem = OpenSSL::TestUtils::TEST_KEY_RSA1024.to_pem(OpenSSL::Cipher.new('AES-128-CBC'), 'secret')
# it raises an ArgumentError from PEM reading. The exception raised inside are ignored for now.
assert_raise(ArgumentError) do
assert_raise(OpenSSL::PKey::PKeyError) do
OpenSSL::PKey.read(pem) do
raise RuntimeError
end
Expand All @@ -285,7 +285,7 @@ def test_export_password_funny
end
# password containing NUL byte
pem = key.export(OpenSSL::Cipher.new('AES-128-CBC'), "pass\0wd")
assert_raise(ArgumentError) do
assert_raise(OpenSSL::PKey::PKeyError) do
OpenSSL::PKey.read(pem, "pass")
end
key2 = OpenSSL::PKey.read(pem, "pass\0wd")
Expand Down

0 comments on commit 2a833d8

Please sign in to comment.