Skip to content

Commit 7cc5068

Browse files
committed
digest, cipher: raise ArgumentError for unknown algorithm name
Currently, either ArgumentError or RuntimeError is raised depending on the context. Always raise ArgumentError, as it better describes the error.
1 parent 950b447 commit 7cc5068

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,13 @@ ossl_cipher_initialize(VALUE self, VALUE str)
114114
{
115115
EVP_CIPHER_CTX *ctx;
116116
const EVP_CIPHER *cipher;
117-
char *name;
118117

119-
name = StringValueCStr(str);
120118
GetCipherInit(self, ctx);
121119
if (ctx) {
122120
ossl_raise(rb_eRuntimeError, "Cipher already initialized!");
123121
}
122+
cipher = ossl_evp_get_cipherbyname(str);
124123
AllocCipher(self, ctx);
125-
if (!(cipher = EVP_get_cipherbyname(name))) {
126-
ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%"PRIsVALUE")", str);
127-
}
128124
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
129125
ossl_raise(eCipherError, NULL);
130126

ext/openssl/ossl_digest.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ ossl_evp_get_digestbyname(VALUE obj)
5656
md = EVP_get_digestbyobj(oid);
5757
ASN1_OBJECT_free(oid);
5858
}
59-
if(!md)
60-
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%"PRIsVALUE").", obj);
59+
if (!md)
60+
ossl_raise(rb_eArgError,
61+
"unsupported digest algorithm: %"PRIsVALUE, obj);
6162
} else {
6263
EVP_MD_CTX *ctx;
6364

test/openssl/test_cipher.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_initialize
112112
cipher = OpenSSL::Cipher.new("DES-EDE3-CBC")
113113
assert_raise(RuntimeError) { cipher.__send__(:initialize, "DES-EDE3-CBC") }
114114
assert_raise(RuntimeError) { OpenSSL::Cipher.allocate.final }
115+
assert_raise(ArgumentError) { OpenSSL::Cipher.new("no such algorithm") }
115116
end
116117

117118
def test_ctr_if_exists
@@ -368,7 +369,7 @@ def test_aes_keywrap_pad
368369

369370
begin
370371
cipher = OpenSSL::Cipher.new("id-aes192-wrap-pad").encrypt
371-
rescue OpenSSL::Cipher::CipherError, RuntimeError
372+
rescue OpenSSL::Cipher::CipherError, ArgumentError
372373
omit "id-aes192-wrap-pad is not supported: #$!"
373374
end
374375
cipher.key = kek

test/openssl/test_digest.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def setup
1010
@d2 = OpenSSL::Digest::MD5.new
1111
end
1212

13+
def test_initialize
14+
assert_raise(ArgumentError) { OpenSSL::Digest.new("no such algorithm") }
15+
end
16+
1317
def test_digest
1418
null_hex = "d41d8cd98f00b204e9800998ecf8427e"
1519
null_bin = [null_hex].pack("H*")

0 commit comments

Comments
 (0)