Skip to content

Commit 265091b

Browse files
committed
cipher: raise CipherError for unsupported algorithm name
Raise OpenSSL::Cipher::CipherError instead of ArgumentError or RuntimeError for consistency.
1 parent f075a9b commit 265091b

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ ossl_evp_get_cipherbyname(VALUE obj)
6565
StringValueCStr(obj);
6666
cipher = EVP_get_cipherbyname(RSTRING_PTR(obj));
6767
if (!cipher)
68-
ossl_raise(rb_eArgError,
69-
"unsupported cipher algorithm: %"PRIsVALUE, obj);
68+
ossl_raise(eCipherError, "unsupported cipher algorithm: %"PRIsVALUE,
69+
obj);
7070

7171
return cipher;
7272
}
@@ -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

test/openssl/test_cipher.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ 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(OpenSSL::Cipher::CipherError) {
116+
OpenSSL::Cipher.new("no such algorithm")
117+
}
115118
end
116119

117120
def test_ctr_if_exists
@@ -368,7 +371,7 @@ def test_aes_keywrap_pad
368371

369372
begin
370373
cipher = OpenSSL::Cipher.new("id-aes192-wrap-pad").encrypt
371-
rescue OpenSSL::Cipher::CipherError, RuntimeError
374+
rescue OpenSSL::Cipher::CipherError
372375
omit "id-aes192-wrap-pad is not supported: #$!"
373376
end
374377
cipher.key = kek

0 commit comments

Comments
 (0)