summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2025-11-03 18:26:28 +0900
committergit <svn-admin@ruby-lang.org>2025-11-06 13:25:09 +0000
commit26751e40857be6faf91d0c87362ebae769f51faa (patch)
tree065e65f53cc1696d4797d5b27f4dcee3bece0931
parent18ab5023b63af88eddb083e89e19224e9f25af4d (diff)
[ruby/openssl] cipher: raise CipherError for unsupported algorithm name
Raise OpenSSL::Cipher::CipherError instead of ArgumentError or RuntimeError for consistency. https://github.com/ruby/openssl/commit/78601c9c34
-rw-r--r--ext/openssl/ossl_cipher.c10
-rw-r--r--test/openssl/test_cipher.rb5
2 files changed, 7 insertions, 8 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index f449c63b69..075e92c67f 100644
--- a/ext/openssl/ossl_cipher.c
+++ b/ext/openssl/ossl_cipher.c
@@ -65,8 +65,8 @@ ossl_evp_get_cipherbyname(VALUE obj)
StringValueCStr(obj);
cipher = EVP_get_cipherbyname(RSTRING_PTR(obj));
if (!cipher)
- ossl_raise(rb_eArgError,
- "unsupported cipher algorithm: %"PRIsVALUE, obj);
+ ossl_raise(eCipherError, "unsupported cipher algorithm: %"PRIsVALUE,
+ obj);
return cipher;
}
@@ -114,17 +114,13 @@ ossl_cipher_initialize(VALUE self, VALUE str)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
- char *name;
- name = StringValueCStr(str);
GetCipherInit(self, ctx);
if (ctx) {
ossl_raise(rb_eRuntimeError, "Cipher already initialized!");
}
+ cipher = ossl_evp_get_cipherbyname(str);
AllocCipher(self, ctx);
- if (!(cipher = EVP_get_cipherbyname(name))) {
- ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%"PRIsVALUE")", str);
- }
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
diff --git a/test/openssl/test_cipher.rb b/test/openssl/test_cipher.rb
index 10858b3535..30c9a59d41 100644
--- a/test/openssl/test_cipher.rb
+++ b/test/openssl/test_cipher.rb
@@ -112,6 +112,9 @@ class OpenSSL::TestCipher < OpenSSL::TestCase
cipher = OpenSSL::Cipher.new("DES-EDE3-CBC")
assert_raise(RuntimeError) { cipher.__send__(:initialize, "DES-EDE3-CBC") }
assert_raise(RuntimeError) { OpenSSL::Cipher.allocate.final }
+ assert_raise(OpenSSL::Cipher::CipherError) {
+ OpenSSL::Cipher.new("no such algorithm")
+ }
end
def test_ctr_if_exists
@@ -368,7 +371,7 @@ class OpenSSL::TestCipher < OpenSSL::TestCase
begin
cipher = OpenSSL::Cipher.new("id-aes192-wrap-pad").encrypt
- rescue OpenSSL::Cipher::CipherError, RuntimeError
+ rescue OpenSSL::Cipher::CipherError
omit "id-aes192-wrap-pad is not supported: #$!"
end
cipher.key = kek