From 26751e40857be6faf91d0c87362ebae769f51faa Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Mon, 3 Nov 2025 18:26:28 +0900 Subject: [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 --- ext/openssl/ossl_cipher.c | 10 +++------- test/openssl/test_cipher.rb | 5 ++++- 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 -- cgit v1.2.3