Commit graph

532 commits

Author SHA1 Message Date
Jun Aruga
f5ca8d0e31 [ruby/openssl] test/openssl/test_pkey.rb: Fix pending tests in FIPS case.
f9980d88aa
2023-08-16 14:48:42 +09:00
Jun Aruga
8ca0d53fd0 [ruby/openssl] Use openssl? instead of OpenSSL::OPENSSL_VERSION_NUMBER.
Update the `openssl?` method by adding status argument.

Note the format is below.

* OpenSSL 3: 0xMNN00PP0 (major minor 00 patch 0)
* OpenSSL 1: 0xMNNFFPPS (major minor fix patch status)

See <https://www.openssl.org/docs/man1.1.1/man3/OPENSSL_VERSION_NUMBER.html>
for details.

db8deaacd3
2023-08-16 14:48:42 +09:00
Kazuki Yamaguchi
12bdacdca5 [ruby/openssl] Include "additional data" message in OpenSSL errors
Error entries in the OpenSSL error queue may contain additional
contextual information associated with the error, which can be helpful
when debugging.

This "additional data" is currently only printed to stderr when
OpenSSL.debug is enabled. Let's include this in the exception messages
raised with ossl_raise(), too.

	$ ruby -Ilib -ropenssl -e'OpenSSL.debug=true; OpenSSL::SSL::SSLContext.new.ecdh_curves="P-256:not-a-curve"'
	-e:1: warning: error on stack: error:0A080106:SSL routines:gid_cb:passed invalid argument (group 'not-a-curve' cannot be set)
	-e:1:in `ecdh_curves=': passed invalid argument (group 'not-a-curve' cannot be set) (OpenSSL::SSL::SSLError)
		from -e:1:in `<main>'

1c5bbdd68e
2023-08-16 14:48:41 +09:00
Kazuki Yamaguchi
382b42be94 [ruby/openssl] config: relax test assertions against error messages
Test that the error message contains the necessary text instead of the
exact match. Exceptions raised by ossl_raise() may carry additional
context information in the message.

fd6f76c0d3
2023-08-16 14:48:41 +09:00
Kazuki Yamaguchi
01d368e7b0 [ruby/openssl] ssl: raise SSLError if loading ca_file or ca_path fails
When compiled with OpenSSL <= 1.1.1, OpenSSL::SSL::SSLContext#setup
does not raise an exception on an error return from
SSL_CTX_load_verify_locations(), but instead only prints a verbose-mode
warning. This is not helpful since it very likely indicates an actual
error, such as the specified file not being readable.

Also, OpenSSL's error queue is not correctly cleared:

	$ ruby -w -ropenssl -e'OpenSSL.debug=true; ctx=OpenSSL::SSL::SSLContext.new; ctx.ca_file="bad-path"; ctx.setup; pp OpenSSL.errors'
	-e:1: warning: can't set verify locations
	["error:02001002:system library:fopen:No such file or directory",
	 "error:2006D080:BIO routines:BIO_new_file:no such file",
	 "error:0B084002:x509 certificate routines:X509_load_cert_crl_file: system lib"]

The behavior is currently different when compiled with OpenSSL >= 3.0:
SSLError is raised if SSL_CTX_load_verify_file() or
SSL_CTX_load_verify_dir() fails.

This inconsistency was unintentionally introduced by commit 5375a55ffc
("ssl: use SSL_CTX_load_verify_{file,dir}() if available", 2020-02-22).
However, raising SSLError seems more appropriate in this situation.
Let's adjust the OpenSSL <= 1.1.1 code so that it behaves the same way
as the OpenSSL >= 3.0 code currently does.

Fixes: https://github.com/ruby/openssl/issues/649

7eb10f7b75
2023-08-16 14:48:41 +09:00
Kazuki Yamaguchi
4465941e68 [ruby/openssl] Revert "Relax error message check for OpenSSL 3.1"
This reverts commit fc4629d246.

The test case "test_connect_certificate_verify_failed_exception_message"
does want to check the reason behind a certificate verification failure
to be included in the exception message.

c309745eb8
2023-08-16 14:48:39 +09:00
Ryo Kajiwara
4b6d667c63 [ruby/openssl] Add support for raw private/public keys
(https://github.com/ruby/openssl/pull/646)

Add OpenSSL::PKey.new_raw_private_key, #raw_private_key and public
equivalents. These methods are useful for importing and exporting keys
that support "raw private/public key". Currently, OpenSSL implements
X25519/X448 and Ed25519/Ed448 keys.

[rhe: rewrote commit message]

3f29525618

Co-authored-by: Bart de Water <bartdewater@gmail.com>
2023-07-12 23:40:58 +09:00
qwyng
e9f62fa5a7 [ruby/openssl] add OpenSSL Provider support
189c167e40
[rhe: tool/update-deps --fix to update ext/openssl/depend]
2023-06-19 17:45:51 +09:00
Theo Buehler
bc0539a9b7 [ruby/openssl] Fix modular square root test with LibreSSL >= 3.8
If x is a modular square root of a (mod p) then so is (p - x). Both
answers are valid. In particular, both 2 and 3 are valid square roots
of 4 (mod 5). Do not assume that a particular square root is chosen by
the algorithm. Indeed, the algorithm in OpenSSL and LibreSSL <= 3.7
returns a non-deterministic answer in many cases. LibreSSL 3.8 and
later will always return the smaller of the two possible answers. This
breaks the current test case.

Instead of checking for a particular square root, check that the square
of the claimed square root is the given value. This is always true. Add
the simplest test case where the answer is indeed non-deterministic.

93548ae959
2023-06-19 01:57:09 +09:00
Jun Aruga
366d8005b1 [ruby/openssl] CI: Add the test/openssl/test_pkey.rb on the FIPS mode case.
It's to test the `OpenSSL::PKey.read` in the `test/openssl/test_pkey.rb`.

I added the pending status to the following tests failing on the FIPS mode
case in the `test/openssl/test_pkey.rb`.

* `test_ed25519`
* `test_x25519`
* `test_compare?`

8149cdf6e8
2023-06-19 01:57:09 +09:00
Jun Aruga
920bc71284 [ruby/openssl] Drop a common logic disabling the FIPS mode in the tests.
We want to run the unit tests in the FIPS mode too.

ab92baff34
2023-06-19 01:57:09 +09:00
Hiroshi SHIBATA
74600917a8 [ruby/openssl] Revert "Skip OpenSSL::TestHMAC#test_dup when running with RHEL9"
This reverts commit 9493d4a3bb.

b880a023dd
2023-05-19 09:25:11 +09:00
Jun Aruga
678d41bc51 [ruby/openssl] Implement FIPS functions on OpenSSL 3.
This commit is to implement the `OpenSSL::OPENSSL_FIPS`, `ossl_fips_mode_get`
and `ossl_fips_mode_set` to pass the test `test/openssl/test_fips.rb`.

It seems that the `OPENSSL_FIPS` macro is not used on the FIPS mode case any
more, and some FIPS related APIs also were removed in OpenSSL 3.

See the document <https://github.com/openssl/openssl/blob/master/doc/man7/migration_guide.pod#removed-fips_mode-and-fips_mode_set>
the section OPENSSL 3.0 > Main Changes from OpenSSL 1.1.1 >
Other notable deprecations and changes - Removed FIPS_mode() and FIPS_mode_set() .

The `OpenSSL::OPENSSL_FIPS` returns always true in OpenSSL 3 because the used
functions `EVP_default_properties_enable_fips` and `EVP_default_properties_is_fips_enabled`
works with the OpenSSL installed without FIPS option.

The `TEST_RUBY_OPENSSL_FIPS_ENABLED` is set on the FIPS mode case on the CI.
Because I want to test that the `OpenSSL.fips_mode` returns the `true` or
'false' surely in the CI. You can test the FIPS mode case by setting
`TEST_RUBY_OPENSSL_FIPS_ENABLED` on local too. Right now I don't find a better
way to get the status of the FIPS mode enabled or disabled for this purpose. I
am afraid of the possibility that the FIPS test case is unintentionally skipped.

I also replaced the ambiguous "returns" with "should return" in the tests.

c5b2bc1268
2023-05-19 09:25:10 +09:00
Jun Aruga
741a3bd5a5 [ruby/openssl] CI: Add OpenSSL FIPS mode case.
test/openssl/fixtures/ssl/openssl_fips.cnf.tmpl:

I referred to the following document for the openssl config file for FIPS mode.
<https://www.openssl.org/docs/manmaster/man7/fips_module.html>
- Making all applications use the FIPS module by default

It seems that the `.include` syntax only requires the absolute path.
So, the placeholder OPENSSL_DIR in the template file is replaced with the
actual OpenSSL directory.

.github/workflows/test.yml:

The `TEST_RUBY_OPENSSL_FIPS_ENABLED` environment variable is set
in the FIPS mode CI case. It can be used in the unit tests.

18b017218c
2023-05-19 09:25:10 +09:00
Nobuyoshi Nakada
0b303c6830 [ruby/openssl] Relax error message check for OpenSSL 3.1
A tentative measures fo https://github.com/ruby/openssl/issues/606.

With OpenSSL 3.1.0, the error message at connection using "self-signed
certificate" seems to return `SSL_R_TLSV1_ALERT_UNKNOWN_CA` instead of
`SSL_R_CERTIFICATE_VERIFY_FAILED`.

fc4629d246
2023-03-16 17:17:46 +09:00
Hiroshi SHIBATA
b432867429
Skip OpenSSL::TestHMAC#test_dup when running with RHEL9 2023-01-30 16:55:14 +09:00
Hiroshi SHIBATA
967dec5d61
[ruby/openssl] Added CoreAssertions
520601e11d

  This commits swtich to use ruby/ruby's envutil.rb instead of
  vendored in openssl repo.
2023-01-27 11:30:13 +09:00
Andrew Konchin
5afc830130
[ruby/openssl] Do not require test file in a forked process in tests
ae784673d7
2023-01-27 11:30:13 +09:00
Kazuki Yamaguchi
24c9adcaeb [ruby/openssl] test/openssl/test_pkey.rb: allow failures in test_s_generate_parameters
Commit f2e2a5e5ed ("test/openssl/test_pkey.rb: allow failures in
test_s_generate_parameters", 2022-12-23) was completely bogus. The
problem in OpenSSL 3.0.0-3.0.5 is that errors from the callback are
sometimes silently suppressed.

ccc1594492
2022-12-26 15:09:21 +09:00
Kazuki Yamaguchi
75e7b85e3e [ruby/openssl] test/openssl/test_pkey.rb: allow failures in test_s_generate_parameters
The root cause has been fixed by OpenSSL 3.0.6, but Ubuntu 22.04's
OpenSSL package has not backported the patch yet.

Reference: https://github.com/ruby/openssl/issues/492

f2e2a5e5ed
2022-12-23 09:39:15 +09:00
Joe Truba
0e11d2c3f8 [ruby/openssl] pkey/ec: check private key validity with OpenSSL 3
The behavior of EVP_PKEY_public_check changed between OpenSSL 1.1.1
and 3.0 so that it no longer validates the private key. Instead, private
keys can be validated through EVP_PKEY_private_check and
EVP_PKEY_pairwise_check.

[ky: simplified condition to use either EVP_PKEY_check() or
EVP_PKEY_public_check().]

e38a63ab3d
2022-12-23 09:39:15 +09:00
Kazuki Yamaguchi
a4b4997c69 [ruby/openssl] test/openssl/test_ssl.rb: do not run SSL tests if not available
a3d230d4e0
2022-12-23 09:39:15 +09:00
Kazuki Yamaguchi
dd6f3276e0 [ruby/openssl] ssl: disable NPN support on LibreSSL
As noted in commit a2ed156cc9 ("test/test_ssl: do not run NPN tests
for LibreSSL >= 2.6.1", 2017-08-13), NPN is known not to work properly
on LibreSSL.

Disable NPN support on LibreSSL, whether OPENSSL_NO_NEXTPROTONEG is
defined or not.

NPN is less relevant today anyway. Let's also silence test suite when
it's not available.

289f6e0e1f
2022-12-23 09:39:15 +09:00
Kazuki Yamaguchi
7c073cc112 [ruby/openssl] test/openssl/test_asn1.rb: remove pend for unsupported LibreSSL versions
Commit af895bc559 ("asn1: avoid truncating OID in
OpenSSL::ASN1::ObjectId#oid", 2016-12-15) added this test case.

The OBJ_obj2txt() issue was fixed by LibreSSL 2.5.1 (released in 2017)
and is no longer relevant today.

6a188f1a29
2022-12-23 09:39:15 +09:00
Kazuki Yamaguchi
5591e4935d [ruby/openssl] test/openssl/test_asn1.rb: skip failing tests on LibreSSL 3.6.0
LibreSSL 3.6.0 expects the seconds part in UTCTime and GeneralizedTime
to be always present. LibreSSL 3.6.0 release note [1] says:

> - The ASN.1 time parser has been refactored and rewritten using CBS.
>   It has been made stricter in that it now enforces the rules from
>   RFC 5280.

[1] https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-3.6.0-relnotes.txt

bbc540fe83
2022-12-23 09:39:15 +09:00
Yusuke Nakamura
d4dce27d89
[ruby/openssl] Allow empty string to OpenSSL::Cipher#update
For some reasons, plaintext may be empty string.

ref https://www.rfc-editor.org/rfc/rfc9001.html#section-5.8

953592a29e
2022-12-13 18:07:41 +09:00
Jarek Prokop
ce025a5cb4
[ruby/openssl] Use SHA256 instead of SHA1 where needed in tests.
Systems such as RHEL 9 are moving away from SHA1
disabling it completely in default configuration.

32648da2f6
2022-12-13 18:07:41 +09:00
Ben Toews
149cb049f1 [ruby/openssl] add BN#mod_sqrt
4619ab3e76
2022-10-17 23:38:34 +09:00
Christophe De La Fuente
17998ad3bb [ruby/openssl] Add support to SSL_CTX_set_keylog_callback
- This callback is invoked when TLS key material is generated or
  received, in order to allow applications to store this keying material
  for debugging purposes.
- It is invoked with an `SSLSocket` and a string containing the key
  material in the format used by NSS for its SSLKEYLOGFILE debugging
  output.
- This commit adds the Ruby binding `keylog_cb` and the related tests
- It is only compatible with OpenSSL >= 1.1.1. Even if LibreSSL implements
  `SSL_CTX_set_keylog_callback()` from v3.4.2, it does nothing (see
  648d39f0f0)

3b63232cf1
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
b69d41e1c4 [ruby/openssl] pkey/ec: check existence of public key component before exporting
i2d_PUBKEY_bio() against an EC_KEY without the public key component
trggers a null dereference.

This is a regression introduced by commit 56f0d34d63 ("pkey:
refactor #export/#to_pem and #to_der", 2017-06-14).

Fixes https://github.com/ruby/openssl/pull/527#issuecomment-1220504524
Fixes https://github.com/ruby/openssl/issues/369#issuecomment-1221554057

f6ee0fa4de
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
0677b2fb87 [ruby/openssl] pkey: restore support for decoding "openssl ecparam -genkey" output
Scan through the input for a private key, then fallback to generic
decoder.

OpenSSL 3.0's OSSL_DECODER supports encoded key parameters. The PEM
header "-----BEGIN EC PARAMETERS-----" is used by one of such encoding
formats. While this is useful for OpenSSL::PKey::PKey, an edge case has
been discovered.

The openssl CLI command line "openssl ecparam -genkey" prints two PEM
blocks in a row, one for EC parameters and another for the private key.
Feeding the whole output into OSSL_DECODER results in only the first PEM
block, the key parameters, being decoded. Previously, ruby/openssl did
not support decoding key parameters and it would decode the private key
PEM block instead.

While the new behavior is technically correct, "openssl ecparam -genkey"
is so widely used that ruby/openssl does not want to break existing
applications.

Fixes https://github.com/ruby/openssl/pull/535

d486c82833
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
10f93a8bd7 [ruby/openssl] pkey/dsa: let PKey::DSA.generate choose appropriate q size
DSA parameters generation via EVP_PKEY_paramgen() will not automatically
adjust the size of q value but uses 224 bits by default unless specified
explicitly. This behavior is different from the now-deprecated
DSA_generate_parameters_ex(), which PKey::DSA.generate used to call.

Fixes https://github.com/ruby/openssl/issues/483

Fixes: 1800a8d5eb ("pkey/dsa: use high level EVP interface to generate parameters and keys", 2020-05-17)

0105975a0b
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
65bba0ef6f [ruby/openssl] hmac: use EVP_PKEY_new_raw_private_key() if available
Current OpenSSL 3.0.x release has a regression with zero-length MAC
keys. While this issue should be fixed in a future release of OpenSSL,
we can use EVP_PKEY_new_raw_private_key() in place of the problematic
EVP_PKEY_new_mac_key() to avoid the issue. OpenSSL 3.0's man page
recommends using it regardless:

> EVP_PKEY_new_mac_key() works in the same way as
> EVP_PKEY_new_raw_private_key().  New applications should use
> EVP_PKEY_new_raw_private_key() instead.

Fixes https://github.com/ruby/openssl/issues/369#issuecomment-1224912710

4293f18b1f
2022-10-17 16:35:35 +09:00
madblobfish
79543b9a53 [ruby/openssl] ssl: enable generating keying material from SSL sessions
Add OpenSSL::SSL::SSLSocket#export_keying_material to support RFC 5705

65530b887e
2022-10-17 16:35:35 +09:00
Nobuhiro IMAI
a98096349e [ruby/openssl] Check if the option is an Hash in pkey_ctx_apply_options0()
causes SEGV if it is an Array or something like that.

ef23525210
2022-10-17 16:35:35 +09:00
MSP-Greg
c865e8d161 [ruby/openssl] [CI] TestHMAC#test_dup - remove 'pend' for OpenSSL 3
626b0434a6
2022-10-17 16:35:35 +09:00
Nobuyoshi Nakada
949c3afb48 [ruby/openssl] Skip a new test when old OpenSSL
It does not raise an error when setting an invalid value to SSLContext
ciphers on Ubuntu 18.04.

8c96a69b0d
2022-07-09 15:26:32 +09:00
Nobuyoshi Nakada
d77ebe8eea
[ruby/openssl] Strip trailing spaces [ci skip]
862d92de93
2022-07-09 00:39:18 +09:00
Jarek Prokop
4d6a29320d [ruby/openssl] Let OpenSSL choose the digest if digest for Openssl::OCSP::BasicResponse#sign is nil.
27efcd7e1c
2022-07-08 23:18:22 +09:00
Jeremy Evans
def445303a [ruby/openssl] Fix test of cipher name to pass in LibreSSL 3.4
LibreSSL 3.5 switched the cipher naming to match OpenSSL.

bf198278bd
2022-07-08 23:18:18 +09:00
twkmd12
09daf78fb5 [ruby/openssl] Add 'ciphersuites=' method to allow setting of TLSv1.3 cipher suites along with some unit tests (https://github.com/ruby/openssl/pull/493)
Add OpenSSL::SSL::SSLContext#ciphersuites= method along with unit tests.

12250c7cef
2022-07-08 23:18:11 +09:00
Jeremy Evans
01025a0055 [ruby/openssl] Skip optional wildcard SAN tests on LibreSSL 3.5.0+
RFC 6066 states how some wildcard SAN entries MAY be handled, but
it does not say they MUST be handled.  LibreSSL 3.5.0 only handles
suffix wildcard SANs, not prefix wildcard SANs, or interior
wildcard SANs, so return early from the wildcard SAN tests on
LibreSSL 3.5.0.

Fixes #471

717d7009d6
2022-07-08 23:18:07 +09:00
Yusuke Nakamura
44bfe72892 [ruby/openssl] Add tast cases to OpenSSL::BN using ractor
OpenSSL::BN has been make ractor-safed in 9e7cf9e ,
but there was no test.
And to use 'ractor' annotation, update test-unit to v3.4.6 or higher.

7541a66911
2021-12-25 03:31:59 +09:00
Kazuki Yamaguchi
2df917ed4f [ruby/openssl] ssl: update test_options_disable_versions
Use the combination of TLS 1.2 and TLS 1.3 instead of TLS 1.1 and TLS
1.2 so that will the test case will be run on latest platforms.

e168df0f35
2021-12-20 23:42:04 +09:00
Kazuki Yamaguchi
15eefd30ad [ruby/openssl] ssl: update test_accept_errors_include_peeraddr test case
Use a different invalid data example to prevent SSLSocket#accept from
reaching EOF.

2e089c1916
2021-12-20 23:42:03 +09:00
Kazuki Yamaguchi
79a6f4349d [ruby/openssl] hmac: skip test_dup on OpenSSL 3.0 for now
EVP_MD_CTX_copy() doesn't seem to work as intended on HMAC EVP_MD_CTX
on OpenSSL 3.0.0 and causes a double free. I haven't found the root
problem yet, but let's skip the test case for now.

4699581639
2021-12-20 23:42:03 +09:00
Kazuki Yamaguchi
cfcdd2b4bd [ruby/openssl] cipher: update test_ciphers
Do not attempt to actually use all algorithms. Not all algorithms listed
in OpenSSL::Cipher.ciphers are always available.

91d04f991f
2021-12-20 23:42:02 +09:00
Kazuki Yamaguchi
8ebf597885 [ruby/openssl] pkey: deprecate PKey#set_* methods
OpenSSL 3.0 made EVP_PKEY immutable. This means we can only have a const
pointer of the low level struct and the following methods can no longer
be provided when linked against OpenSSL 3.0:

 - OpenSSL::PKey::RSA#set_key
 - OpenSSL::PKey::RSA#set_factors
 - OpenSSL::PKey::RSA#set_crt_params
 - OpenSSL::PKey::DSA#set_pqg
 - OpenSSL::PKey::DSA#set_key
 - OpenSSL::PKey::DH#set_pqg
 - OpenSSL::PKey::DH#set_key
 - OpenSSL::PKey::EC#group=
 - OpenSSL::PKey::EC#private_key=
 - OpenSSL::PKey::EC#public_key=

There is no direct replacement for this functionality at the moment.
I plan to introduce a wrapper around EVP_PKEY_fromdata(), which takes
all key components at once to construct an EVP_PKEY.

6848d2d969
2021-12-20 23:42:02 +09:00
Kazuki Yamaguchi
b93ae54258 [ruby/openssl] pkey/ec: deprecate OpenSSL::PKey::EC#generate_key!
OpenSSL::PKey::EC#generate_key! will not work on OpenSSL 3.0 because
keys are made immutable. Users should use OpenSSL::PKey.generate_key
instead.

5e2e66cce8
2021-12-20 23:42:02 +09:00
Kazuki Yamaguchi
0d698be04f [ruby/openssl] pkey/dh: deprecate OpenSSL::PKey::DH#generate_key!
OpenSSL::PKey::DH#generate_key! will not work on OpenSSL 3.0 because
keys are made immutable. Users should use OpenSSL::PKey.generate_key
instead.

8ee6a582c7
2021-12-20 23:42:02 +09:00