diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index f9a473caf..91b0fdb75 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -398,6 +398,13 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self) } out_len = in_len + EVP_MAX_BLOCK_LENGTH; + /* + * In-place EVP_CipherUpdate() is not reliable if the context may be + * holding a partial or withheld block. + */ + if (data == str && EVP_CIPHER_CTX_block_size(ctx) != 1) + data = rb_str_new_frozen(data); + if (NIL_P(str)) str = rb_str_buf_new(out_len); else { @@ -412,6 +419,7 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self) (unsigned char *)RSTRING_PTR(data), in_len)) ossl_raise(eCipherError, "EVP_CipherUpdate"); rb_str_set_len(str, out_len); + RB_GC_GUARD(data); return str; } diff --git a/test/openssl/test_cipher.rb b/test/openssl/test_cipher.rb index 890dc760d..27e7da0db 100644 --- a/test/openssl/test_cipher.rb +++ b/test/openssl/test_cipher.rb @@ -162,6 +162,29 @@ def test_update_with_buffer assert_equal expected, shared + cipher.final end + def test_update_in_place_with_buffered_data + data = "0123456789abcdef".b * 8 + %w[aes-128-ecb aes-128-cbc].each do |algo| + [:encrypt, :decrypt].each do |direction| + # 3 bytes leave a partial block in the context. 16 bytes leave a whole + # block there when decrypting, withheld until #final can unpad it. + [3, 16].each do |buffered_len| + cipher = OpenSSL::Cipher.new(algo).__send__(direction) + cipher.random_key + cipher.random_iv + buffered = "a" * buffered_len + expected = cipher.update(buffered) << cipher.update(data) + + cipher.reset + buf = data.dup + actual = cipher.update(buffered) << cipher.update(buf, buf) + assert_equal expected, actual, + "#{algo} #{direction} buffered_len=#{buffered_len}" + end + end + end + end + def test_ciphers ciphers = OpenSSL::Cipher.ciphers assert_kind_of Array, ciphers