ruby-changes:43072
From: rhe <ko1@a...>
Date: Tue, 24 May 2016 22:09:07 +0900 (JST)
Subject: [ruby-changes:43072] rhe:r55146 (trunk): openssl: make Cipher#key= and #iv= reject too long values
rhe 2016-05-24 22:09:03 +0900 (Tue, 24 May 2016) New Revision: 55146 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55146 Log: openssl: make Cipher#key= and #iv= reject too long values * ext/openssl/ossl_cipher.c (ossl_cipher_set_key, ossl_cipher_set_iv): Reject too long values as well as too short ones. Currently they just truncate the input but this would hide bugs and lead to unexpected encryption/decryption results. * test/openssl/test_cipher.rb: Test that Cipher#key= and #iv= reject Strings with invalid length. Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_cipher.c trunk/test/openssl/test_cipher.rb Index: test/openssl/test_cipher.rb =================================================================== --- test/openssl/test_cipher.rb (revision 55145) +++ test/openssl/test_cipher.rb (revision 55146) @@ -80,6 +80,18 @@ class OpenSSL::TestCipher < OpenSSL::Tes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_cipher.rb#L80 assert_equal(s1, s2, "encrypt reset") end + def test_key_iv_set + # default value for DES-EDE3-CBC + assert_equal(24, @c1.key_len) + assert_equal(8, @c1.iv_len) + assert_raise(ArgumentError) { @c1.key = "\x01" * 23 } + @c1.key = "\x01" * 24 + assert_raise(ArgumentError) { @c1.key = "\x01" * 25 } + assert_raise(ArgumentError) { @c1.iv = "\x01" * 7 } + @c1.iv = "\x01" * 8 + assert_raise(ArgumentError) { @c1.iv = "\x01" * 9 } + end + def test_empty_data @c1.encrypt assert_raise(ArgumentError){ @c1.update("") } Index: ChangeLog =================================================================== --- ChangeLog (revision 55145) +++ ChangeLog (revision 55146) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue May 24 22:04:15 2016 Kazuki Yamaguchi <k@r...> + + * ext/openssl/ossl_cipher.c (ossl_cipher_set_key, ossl_cipher_set_iv): + Reject too long values as well as too short ones. Currently they + just truncate the input but this would hide bugs and lead to + unexpected encryption/decryption results. + + * test/openssl/test_cipher.rb: Test that Cipher#key= and #iv= reject + Strings with invalid length. + Tue May 24 21:32:21 2016 Kazuki Yamaguchi <k@r...> * ext/openssl/ossl_x509ext.c (ossl_x509ext_set_value): Use Index: ext/openssl/ossl_cipher.c =================================================================== --- ext/openssl/ossl_cipher.c (revision 55145) +++ ext/openssl/ossl_cipher.c (revision 55146) @@ -480,15 +480,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L480 ossl_cipher_set_key(VALUE self, VALUE key) { EVP_CIPHER_CTX *ctx; + int key_len; StringValue(key); GetCipher(self, ctx); - if (RSTRING_LEN(key) < EVP_CIPHER_CTX_key_length(ctx)) - ossl_raise(eCipherError, "key length too short"); + key_len = EVP_CIPHER_CTX_key_length(ctx); + if (RSTRING_LEN(key) != key_len) + ossl_raise(rb_eArgError, "key must be %d bytes", key_len); if (EVP_CipherInit_ex(ctx, NULL, NULL, (unsigned char *)RSTRING_PTR(key), NULL, -1) != 1) - ossl_raise(eCipherError, NULL); + ossl_raise(eCipherError, NULL); return key; } @@ -512,12 +514,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L514 ossl_cipher_set_iv(VALUE self, VALUE iv) { EVP_CIPHER_CTX *ctx; + int iv_len; StringValue(iv); GetCipher(self, ctx); - if (RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx)) - ossl_raise(eCipherError, "iv length too short"); + iv_len = EVP_CIPHER_CTX_iv_length(ctx); + if (RSTRING_LEN(iv) != iv_len) + ossl_raise(rb_eArgError, "iv must be %d bytes", iv_len); if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, (unsigned char *)RSTRING_PTR(iv), -1) != 1) ossl_raise(eCipherError, NULL); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/