Skip to content

Commit bb10767

Browse files
committed
cipher: disallow setting AAD for non-AEAD ciphers
EVP_CipherUpdate() must not be call with the output parameter set to NULL when the cipher does not support AEAD. Check the flag of EVP_CIPHER, and raise an exception as necessary. Reference: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/83337 Reference: https://bugs.ruby-lang.org/issues/14024
1 parent c35c692 commit bb10767

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ ossl_cipher_set_auth_data(VALUE self, VALUE data)
580580
in_len = RSTRING_LEN(data);
581581

582582
GetCipher(self, ctx);
583+
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
584+
ossl_raise(eCipherError, "AEAD not supported by this cipher");
583585

584586
if (!ossl_cipher_update_long(ctx, NULL, &out_len, in, in_len))
585587
ossl_raise(eCipherError, "couldn't set additional authenticated data");

test/test_cipher.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ def test_aes_gcm_key_iv_order_issue
297297
assert_equal tag1, tag2
298298
end if has_cipher?("aes-128-gcm")
299299

300+
def test_non_aead_cipher_set_auth_data
301+
assert_raise(OpenSSL::Cipher::CipherError) {
302+
cipher = OpenSSL::Cipher.new("aes-128-cfb").encrypt
303+
cipher.auth_data = "123"
304+
}
305+
end
306+
300307
private
301308

302309
def new_encryptor(algo, **kwargs)

0 commit comments

Comments
 (0)