Skip to content

Commit a8ef534

Browse files
committed
Merge branch 'maint-3.1' into maint-3.2
* maint-3.1: Fix modular square root test with LibreSSL >= 3.8 pkcs7: raise PKCS7Error for PKCS7 without content in PKCS7.read_smime pkcs7: raise ArgumentError for PKCS7 with no content in PKCS7.new cipher: fix buffer overflow in Cipher#update ssl: allow failure on test_connect_certificate_verify_failed_exception_message .github/workflows/test.yml: synchronize with master Only CSR version 1 (encoded as 0) is allowed by PKIX standards test_asn1.rb: Remove the assertions of the time string format without second. test/openssl/test_asn1.rb: skip failing tests on LibreSSL 3.6.0 Use EVP_Digest{Sign,Verify} when available Fix performance regression in do_write(s)
2 parents 6b3dd6a + 3cd3c27 commit a8ef534

File tree

8 files changed

+74
-36
lines changed

8 files changed

+74
-36
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ jobs:
5151
run: echo "RUBY_OPENSSL_EXTCFLAGS=-Werror" >> $GITHUB_ENV
5252
if: ${{ !matrix.skip-warnings }}
5353

54-
# Enable provider search path for OpenSSL 3.0 in MSYS2.
55-
# Remove when Ruby 3.2 build is updated
56-
- name: enable windows provider search path
57-
run: echo "OPENSSL_MODULES=$($env:RI_DEVKIT)\$($env:MSYSTEM_PREFIX)\lib\ossl-modules" >> $env:GITHUB_ENV
58-
if: runner.os == 'Windows' && matrix.ruby == '3.2'
59-
6054
- name: compile
6155
run: rake compile
6256

@@ -77,18 +71,21 @@ jobs:
7771
# https://www.openssl.org/source/
7872
- openssl-1.0.2u # EOL
7973
- openssl-1.1.0l # EOL
80-
- openssl-1.1.1v
81-
- openssl-3.0.10
82-
- openssl-3.1.2
74+
- openssl-1.1.1w # EOL
75+
- openssl-3.0.13
76+
- openssl-3.1.5
77+
- openssl-3.2.1
78+
- openssl-3.3.0
8379
# http://www.libressl.org/releases.html
8480
- libressl-3.1.5 # EOL
8581
- libressl-3.2.7 # EOL
8682
- libressl-3.3.6 # EOL
8783
- libressl-3.4.3 # EOL
8884
- libressl-3.5.3 # EOL
89-
- libressl-3.6.3
90-
- libressl-3.7.3
91-
- libressl-3.8.0 # Development release
85+
- libressl-3.6.3 # EOL
86+
- libressl-3.7.3 # EOL
87+
- libressl-3.8.4
88+
- libressl-3.9.1
9289
fips-enabled: [ false ]
9390
include:
9491
- { os: ubuntu-latest, ruby: "3.0", openssl: openssl-3.0.10, fips-enabled: true, append-configure: 'enable-fips', name-extra: 'fips' }

ext/openssl/ossl_cipher.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,23 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
386386
in = (unsigned char *)RSTRING_PTR(data);
387387
in_len = RSTRING_LEN(data);
388388
GetCipher(self, ctx);
389-
out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
390-
if (out_len <= 0) {
389+
390+
/*
391+
* As of OpenSSL 3.2, there is no reliable way to determine the required
392+
* output buffer size for arbitrary cipher modes.
393+
* https://github.com/openssl/openssl/issues/22628
394+
*
395+
* in_len+block_size is usually sufficient, but AES key wrap with padding
396+
* ciphers require in_len+15 even though they have a block size of 8 bytes.
397+
*
398+
* Using EVP_MAX_BLOCK_LENGTH (32) as a safe upper bound for ciphers
399+
* currently implemented in OpenSSL, but this can change in the future.
400+
*/
401+
if (in_len > LONG_MAX - EVP_MAX_BLOCK_LENGTH) {
391402
ossl_raise(rb_eRangeError,
392403
"data too big to make output buffer: %ld bytes", in_len);
393404
}
405+
out_len = in_len + EVP_MAX_BLOCK_LENGTH;
394406

395407
if (NIL_P(str)) {
396408
str = rb_str_new(0, out_len);
@@ -401,7 +413,7 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
401413

402414
if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str), &out_len, in, in_len))
403415
ossl_raise(eCipherError, NULL);
404-
assert(out_len < RSTRING_LEN(str));
416+
assert(out_len <= RSTRING_LEN(str));
405417
rb_str_set_len(str, out_len);
406418

407419
return str;

ext/openssl/ossl_pkcs7.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ ossl_pkcs7_s_read_smime(VALUE klass, VALUE arg)
165165
out = NULL;
166166
pkcs7 = SMIME_read_PKCS7(in, &out);
167167
BIO_free(in);
168-
if(!pkcs7) ossl_raise(ePKCS7Error, NULL);
168+
if (!pkcs7)
169+
ossl_raise(ePKCS7Error, "Could not parse the PKCS7");
170+
if (!pkcs7->d.ptr)
171+
ossl_raise(ePKCS7Error, "No content in PKCS7");
172+
169173
data = out ? ossl_membio2str(out) : Qnil;
170174
SetPKCS7(ret, pkcs7);
171175
ossl_pkcs7_set_data(ret, data);
@@ -346,6 +350,8 @@ ossl_pkcs7_initialize(int argc, VALUE *argv, VALUE self)
346350
BIO_free(in);
347351
if (!p7)
348352
ossl_raise(rb_eArgError, "Could not parse the PKCS7");
353+
if (!p7->d.ptr)
354+
ossl_raise(rb_eArgError, "No content in PKCS7");
349355

350356
RTYPEDDATA_DATA(self) = p7;
351357
PKCS7_free(p7_orig);

lib/openssl/buffering.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,18 @@ def do_write(s)
345345
@wbuffer << s
346346
@wbuffer.force_encoding(Encoding::BINARY)
347347
@sync ||= false
348-
if @sync or @wbuffer.size > BLOCK_SIZE
349-
until @wbuffer.empty?
350-
begin
351-
nwrote = syswrite(@wbuffer)
352-
rescue Errno::EAGAIN
353-
retry
348+
buffer_size = @wbuffer.size
349+
if @sync or buffer_size > BLOCK_SIZE
350+
nwrote = 0
351+
begin
352+
while nwrote < buffer_size do
353+
begin
354+
nwrote += syswrite(@wbuffer[nwrote, buffer_size - nwrote])
355+
rescue Errno::EAGAIN
356+
retry
357+
end
354358
end
359+
ensure
355360
@wbuffer[0, nwrote] = ""
356361
end
357362
end

test/openssl/test_asn1.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,6 @@ def test_utctime
406406
rescue OpenSSL::ASN1::ASN1Error
407407
pend "No negative time_t support?"
408408
end
409-
# Seconds is omitted. LibreSSL 3.6.0 requires it
410-
return if libressl?
411-
decode_test B(%w{ 17 0B }) + "1609082343Z".b,
412-
OpenSSL::ASN1::UTCTime.new(Time.utc(2016, 9, 8, 23, 43, 0))
413409
# not implemented
414410
# decode_test B(%w{ 17 11 }) + "500908234339+0930".b,
415411
# OpenSSL::ASN1::UTCTime.new(Time.new(1950, 9, 8, 23, 43, 39, "+09:30"))
@@ -428,10 +424,6 @@ def test_generalizedtime
428424
OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 34, 29))
429425
encode_decode_test B(%w{ 18 0F }) + "99990908234339Z".b,
430426
OpenSSL::ASN1::GeneralizedTime.new(Time.utc(9999, 9, 8, 23, 43, 39))
431-
# LibreSSL 3.6.0 requires the seconds element
432-
return if libressl?
433-
decode_test B(%w{ 18 0D }) + "201612081934Z".b,
434-
OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 34, 0))
435427
# not implemented
436428
# decode_test B(%w{ 18 13 }) + "20161208193439+0930".b,
437429
# OpenSSL::ASN1::GeneralizedTime.new(Time.new(2016, 12, 8, 19, 34, 39, "+09:30"))

test/openssl/test_cipher.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,22 @@ def test_aes_gcm_key_iv_order_issue
331331
assert_equal tag1, tag2
332332
end
333333

334+
def test_aes_keywrap_pad
335+
# RFC 5649 Section 6; The second example
336+
kek = ["5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8"].pack("H*")
337+
key = ["466f7250617369"].pack("H*")
338+
wrap = ["afbeb0f07dfbf5419200f2ccb50bb24f"].pack("H*")
339+
340+
begin
341+
cipher = OpenSSL::Cipher.new("id-aes192-wrap-pad").encrypt
342+
rescue OpenSSL::Cipher::CipherError, RuntimeError
343+
omit "id-aes192-wrap-pad is not supported: #$!"
344+
end
345+
cipher.key = kek
346+
ct = cipher.update(key) << cipher.final
347+
assert_equal wrap, ct
348+
end
349+
334350
def test_non_aead_cipher_set_auth_data
335351
assert_raise(OpenSSL::Cipher::CipherError) {
336352
cipher = OpenSSL::Cipher.new("aes-128-cfb").encrypt

test/openssl/test_pkcs7.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ def test_enveloped
155155
assert_equal(data, p7.decrypt(@rsa1024))
156156
end
157157

158+
def test_empty_signed_data_ruby_bug_19974
159+
data = "-----BEGIN PKCS7-----\nMAsGCSqGSIb3DQEHAg==\n-----END PKCS7-----\n"
160+
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(data) }
161+
162+
data = <<END
163+
MIME-Version: 1.0
164+
Content-Disposition: attachment; filename="smime.p7m"
165+
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"
166+
Content-Transfer-Encoding: base64
167+
168+
#{data}
169+
END
170+
assert_raise(OpenSSL::PKCS7::PKCS7Error) { OpenSSL::PKCS7.read_smime(data) }
171+
end
172+
158173
def test_graceful_parsing_failure #[ruby-core:43250]
159174
contents = File.read(__FILE__)
160175
assert_raise(ArgumentError) { OpenSSL::PKCS7.new(contents) }

test/openssl/test_x509req.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ def test_version
3939
assert_equal(0, req.version)
4040
req = OpenSSL::X509::Request.new(req.to_der)
4141
assert_equal(0, req.version)
42-
43-
req = issue_csr(1, @dn, @rsa1024, OpenSSL::Digest.new('SHA256'))
44-
assert_equal(1, req.version)
45-
req = OpenSSL::X509::Request.new(req.to_der)
46-
assert_equal(1, req.version)
4742
end
4843

4944
def test_subject
@@ -106,7 +101,7 @@ def test_sign_and_verify_rsa_sha1
106101
assert_equal(false, req.verify(@rsa2048))
107102
assert_equal(false, request_error_returns_false { req.verify(@dsa256) })
108103
assert_equal(false, request_error_returns_false { req.verify(@dsa512) })
109-
req.version = 1
104+
req.subject = OpenSSL::X509::Name.parse("/C=JP/CN=FooBarFooBar")
110105
assert_equal(false, req.verify(@rsa1024))
111106
rescue OpenSSL::X509::RequestError # RHEL 9 disables SHA1
112107
end

0 commit comments

Comments
 (0)