Commit graph

1165 commits

Author SHA1 Message Date
Kazuki Yamaguchi
1b731c1f43 [ruby/openssl] pkey: avoid calling i2d_PUBKEY family on an incomplete key
Call ossl_pkey_check_public_key() to ensure that
EVP_PKEY_missing_parameters() passes. This check should be cheap.

DSA#{to_der,to_pem,export,to_s} and PKey#{public_to_der,public_to_pem}
cause a segfault if the receiver is an empty DSA instance with no
parameters set.

Fixes <https://github.com/ruby/openssl/issues/845>.

5aeed935e5
2025-01-29 17:14:41 +00:00
Kazuki Yamaguchi
495b1cad04 [ruby/openssl] ts: use TS_VERIFY_CTX_set0_{store,certs}() on OpenSSL 3.4
In OpenSSL 3.4, TS_VERIFY_CTX_set_certs() and TS_VERIFY_CTX_set_store()
are deprecated in favor of the new functions with "set0" in the names.

The new functions have a slightly different behavior. They will free the
previous value automatically. Increment the reference counter of
X509_STORE before setting it to TS_VERIFY_CTX, and do not try to
manually unset it.

We avoided doing this to work around a bug that was present in older
versions of OpenSSL, which has now been fixed in OpenSSL 1.0.2 by commit
bff9ce4db3.

ce37f7d93a
2025-01-23 01:45:52 +09:00
Kazuki Yamaguchi
87316d58fa [ruby/openssl] pkey: change PKey::{RSA,DSA,DH}#params to use nil for missing parameters
The returned Hash from these methods contain 0 in place of a missing
parameter in the key, for example:

	pkey = OpenSSL::PKey.read(OpenSSL::PKey::RSA.new(2048).public_to_pem)
	pp pkey.params
	#=>
	# {"n"=>#<OpenSSL::BN 2869346734[...snip]>,
	#  "e"=>#<OpenSSL::BN 65537>,
	#  "d"=>#<OpenSSL::BN 0>,
	#  "p"=>#<OpenSSL::BN 0>,
	#  "q"=>#<OpenSSL::BN 0>,
	#  "dmp1"=>#<OpenSSL::BN 0>,
	#  "dmq1"=>#<OpenSSL::BN 0>,
	#  "iqmp"=>#<OpenSSL::BN 0>}

Let's use nil instead, which is more appropriate for indicating a
missing value.

f247ec3dec
2025-01-23 01:45:52 +09:00
Kazuki Yamaguchi
ec4592280f [ruby/openssl] pkey: implement PKey::{RSA,DSA,DH}#params in Ruby
Move the definitions to lib/openssl/pkey.rb. They need not to be in the
extension and can be implemented using existing methods.

This reduces direct usage of the now-deprecated OpenSSL APIs around the
low-level structs such as DH, DSA, or RSA.

c14178f387
2025-01-23 01:45:51 +09:00
Kazuki Yamaguchi
72480389d1 [ruby/openssl] ssl: fix SSLSocket#sysread leaking locktmp String on timeout
Commit 3bbf5178a9 made blocking methods on SSLSocket follow the
IO#timeout= value. The commit changed io_wait_readable() to potentially
raise an exception without unlocking the String.

The String is currently locked for the entire duration of a #sysread
method call. This does not seem to be necessary, as SSL_read() does not
require that the same buffer is specified when retrying. Locking the
String during each SSL_read() call should be sufficient.

8f791d73f5
2025-01-22 16:45:19 +00:00
Theo Buehler
72fdba156d [ruby/openssl] Use X509_ALGOR_get0() accessor for X509_ALGOR
While the struct is currently still public in OpenSSL, there has been
an accessor since OpenSSL 0.9.8h. It would be nice if this accessor
could be used so that the struct can be made opaque at some point in
the future.

812aeab2f5
2025-01-21 18:17:06 +00:00
Kazuki Yamaguchi
43c48e3030 [ruby/openssl] Require OpenSSL 1.1.1 or later
Drop support for OpenSSL 1.1.0. OpenSSL 1.1.0 was a non-LTS release and
it has reached upstream EOL in 2019-12 along with OpenSSL 1.0.2.
Distributions that shipped with OpenSSL 1.1.0 include:

 - Debian 9 (EOL 2022-06)
 - Ubuntu 18.04 LTS (EOL 2023-04)

ba83abe920
2025-01-21 18:14:14 +00:00
Kazuki Yamaguchi
c6c1e7a92a [ruby/openssl] extconf.rb: remove dir_config("kerberos")
The dir_config was introduced by Ruby trunk r4181. Since support for
Kerberos cipher suites has been removed in OpenSSL 1.1.0, it is no
longer necessary.

Although ruby/openssl did not directly depend on the MIT Kerberos
library, it was an optional transitive dependency. Unless it was
disabled by a compile-time option, the OpenSSL headers would try to
include <krb5.h>.

78d028c332
2025-01-20 17:12:57 +00:00
Kazuki Yamaguchi
441862dc9f [ruby/openssl] Require OpenSSL 1.1.0 or later
Drop support for OpenSSL 1.0.2. It has reached upstream EOL in 2019-12.

Most distributions that shipped with OpenSSL 1.0.2 have also reached
EOL, or provide a newer version in the package repository:

 - RHEL 7 (EOL 2024-06)
 - Ubuntu 16.04 LTS (EOL 2021-04)
 - Amazon Linux 2 (EOL 2026-06, but OpenSSL 1.1.1 can be installed via
   the openssl11{,-devel} package)

38ec6fd50e
2025-01-20 17:12:57 +00:00
Kazuki Yamaguchi
010e8bff88 [ruby/openssl] pkcs7: remove unnecessary const cast
PKCS7_encrypt() and PKCS7_SIGNER_INFO_set() take const EVP_CIPHER and
EVP_MD at least since OpenSSL 0.9.7.

9db621a5c0
2025-01-20 17:12:55 +00:00
Kazuki Yamaguchi
04cc762941 [ruby/openssl] engine: remove constants for ENGINE_METHOD_BN_MOD_EXP{,_CRT}
These macros do not exist in OpenSSL 0.9.7 or later, which was released
in 2002.

938a1e6aab
2025-01-20 17:12:55 +00:00
Kazuki Yamaguchi
318519caaa [ruby/openssl] engine: remove mention of LibreSSL from OpenSSL::Engine
These paths are only reachable when it is compiled against OpenSSL.
LibreSSL 3.9 does not support ENGINE and defines OPENSSL_NO_ENGINE.

e153d6ab47
2025-01-14 12:38:17 +00:00
Kazuki Yamaguchi
731d3ec301 [ruby/openssl] ossl.c: use OPENSSL_init_ssl() and OpenSSL_version() with LibreSSL
LibreSSL 2.7.0 added support for OPENSSL_init_ssl() and
OpenSSL_version().

1328415097
2025-01-14 12:38:16 +00:00
Kazuki Yamaguchi
0fb64bda9b [ruby/openssl] Require LibreSSL 3.9 or later
Drop support for LibreSSL 3.1-3.8. LibreSSL 3.8 has reached its EOL in
2024-10.

f33d611f9f
2025-01-14 12:38:16 +00:00
Jean Boussier
4f79485889 [ruby/openssl] Refactor buffer usage to only use append_as_bytes
28f2901c48
2025-01-14 11:54:47 +00:00
Jean Boussier
2f5d31d38a [ruby/openssl] Reduce OpenSSL::Buffering#do_write overhead
[Bug #20972]

The `rb_str_new_freeze` was added in https://github.com/ruby/openssl/issues/452
to better handle concurrent use of a Socket, but SSL sockets can't be used
concurrently AFAIK, so we might as well just error cleanly.

By using `rb_str_locktmp` we can ensure attempts at concurrent write
will raise an error, be we avoid causing a copy of the bytes.

We also use the newer `String#append_as_bytes` method when available
to save on some more copies.

0d8c17aa85

Co-Authored-By: luke.gru@gmail.com
2025-01-14 11:54:47 +00:00
Kazuki Yamaguchi
9e3e1c7fc9 [ruby/openssl] Ruby/OpenSSL 3.3.0
e5153dbbb4
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi
4862462097 [ruby/openssl] digest: remove optional parameter from OpenSSL::Digest#finish
OpenSSL::Digest#finish overrides Digest::Instance#finish and is called
from the Digest::Class framework in the digest library. This method is
not supposed to take any arguments, as suggested by the RDoc comment for
Digest::Instance#finish.

It is a private method and not exposed to users. Let's remove it.

This optional parameter exists since r15602 in Ruby trunk, the commit
which converted OpenSSL::Digest to a subclass of Digest::Class.

dcb2a4f30b
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi
9de2b407d7 [ruby/openssl] digest: make output buffer String independent in #finish
Likewise, OpenSSL::Digest#finish needs to make the output buffer
independent before writing to it.

9cc8a83466
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi
637f019f1f [ruby/openssl] cipher: make output buffer String independent
OpenSSL::Cipher#update accepts a String as the second argument to be
used as the output buffer. The buffer must be directly writable, in
other words, it must not be frozen and not a shared string.

rb_str_resize() does not make the String independent if the String
already has the intended length. Use the rb_str_modify() family instead
to check it.

Fixes: https://bugs.ruby-lang.org/issues/20937

1de3b80a46
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi
c79b435407 [ruby/openssl] pkcs12: add PKCS12#set_mac
Add a binding for PKCS12_set_mac() to set MAC parameters and
(re-)calculate MAC for the content.

This allows generating PKCS #12 with consistent MAC parameters with
different OpenSSL versions. OpenSSL 3.0 changed the default hash
function used for HMAC and the KDF from SHA-1 to SHA-256.

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

f5ed2a74b6
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi
c9bbf7e3eb [ruby/openssl] ssl: do not clear existing SSL options in SSLContext#set_params
Apply SSL options set in DEFAULT_PARAMS without clearing existing
options.

It currently clears options in order to avoid setting one of the
options included in OpenSSL::SSL::OP_ALL unless explicitly specified,
namely OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS. Now that
OpenSSL::SSL::OP_ALL has been removed from SSLContext#initialize, it is
no longer necessary.

77c3db2d65
2024-12-07 08:15:08 +00:00
Kazuki Yamaguchi
510c190739 [ruby/openssl] ssl: do not enable OpenSSL::SSL::OP_ALL by default
Respect the SSL options set by default by SSL_CTX() and by the
system-wide OpenSSL configuration file.

OpenSSL::SSL::SSLContext#initialize currently adds OpenSSL::SSL::OP_ALL
on top of the default SSL options. Let's stop doing it.

OpenSSL::SSL::OP_ALL is a set of options that changes OpenSSL's behavior
to workaround various TLS implementation bugs. Using it is considered
usually safe, but is not completely harmless.

00bec0d905
2024-12-07 08:15:08 +00:00
Kazuki Yamaguchi
33196b7ab0 [ruby/openssl] ssl: improve documentation of SSLContext#options=
9120fcde6a
2024-12-07 08:15:07 +00:00
Kazuki Yamaguchi
09d516b62e [ruby/openssl] Call Init_ossl_*() functions in alphabetical order
It was originally sorted in alphabetical order, but it has been broken
over time. Let's fix it.

974c67f38f
2024-12-07 07:55:47 +00:00
Kazuki Yamaguchi
1df63d9451 [ruby/openssl] Mark variables and functions as static whenever possible
85d6b7f192
2024-12-07 07:55:47 +00:00
Kazuki Yamaguchi
cbe7bfd9a8 [ruby/openssl] ts: fix exception class raised when getting an OID name
get_asn1obj() is used by several methods in OpenSSL::Timestamp to get
the string representation of an OID. On an error, such as memory
allocation failure, it can raise OpenSSL::X509::AttributeError. It
should be OpenSSL::Timestamp::TimestampError instead.

a424aad1df
2024-12-07 07:55:46 +00:00
Kazuki Yamaguchi
f8e9302e66 [ruby/openssl] ts: avoid using OpenSSL::PKCS7's internals
Internals of OpenSSL::PKCS7 should be kept within ossl_pkcs7.c.

Add a new ossl_pkcs7_new() function for duplicating and wrapping an
OpenSSL PKCS7 object in OpenSSL::PKCS7. This follows the convention
used by other ossl_*_new() functions.

b5f79f771e
2024-12-07 07:55:46 +00:00
Kazuki Yamaguchi
870cce9798 [ruby/openssl] x509store: fix exception class in OpenSSL::X509::StoreContext#verify
Follow-up commit 0789643d73 (openssl: clear OpenSSL error
queue before return to Ruby, 2016-05-18). It should raise
OpenSSL::X509::StoreError instead of OpenSSL::X509::CertificateError.

0201f23ad6
2024-12-07 07:55:46 +00:00
Kazuki Yamaguchi
3656c1db29 [ruby/openssl] ns_spki: fix exception class in OpenSSL::Netscape::SPKI#to_der
It should raise OpenSSL::Netscape::SPKIError instead of
OpenSSL::X509::CertificateError.

No test cases covered this because it only occurs in exceptional
cases, such as memory allocation failure.

527b6101d1
2024-12-07 07:55:45 +00:00
Kazuki Yamaguchi
b207b956c1 [ruby/openssl] pkey/ec: fix exception class in OpenSSL::PKey::EC.new
Fix a copy-and-paste error introduced in commit 74f6c61756 (pkey:
allocate EVP_PKEY on #initialize, 2021-04-12).

It should raise OpenSSL::PKey::ECError instead of
OpenSSL::PKey::DSAError.

b1f6a04abf
2024-12-07 07:55:45 +00:00
HoneyryderChuck
5444885726 [ruby/openssl] make configs shareable when frozen
654cb22e21
2024-12-07 07:52:02 +00:00
HoneyryderChuck
9cae90f9d7 [ruby/openssl] freeze OpenSSL::Config::DEFAULT_CONFIG_FILE
3cc1825435
2024-12-07 07:52:01 +00:00
HoneyryderChuck
2a006fe54b [ruby/openssl] make config frozen on initialize
50599513cf
2024-12-07 07:52:01 +00:00
Kazuki Yamaguchi
06fc13a15c [ruby/openssl] ssl: handle callback exceptions in SSLSocket#sysread and #syswrite
Check the ID_callback_state ivar after SSL_read() or SSL_write()
returns, similar to what ossl_start_ssl() does.

Previously, callbacks that can raise a Ruby exception were only called
from ossl_start_ssl(). This has changed in OpenSSL 1.1.1. Particularly,
the session_new_cb will be called whenever a client receives a
NewSessionTicket message, which can happen at any time during a TLS 1.3
connection.

aac9ce1304
2024-12-07 07:37:32 +00:00
Kazuki Yamaguchi
19acb3af2e [ruby/openssl] ssl: fix potential exception in servername_cb
ssl_servername_cb() is a callback function called from OpenSSL and Ruby
exceptions must not be raised from it. Allocate the Array within
rb_protect().

3a2bf74d35
2024-12-07 07:36:19 +00:00
Josh Cooper
b4d13fac3d [ruby/openssl] Support signing CRLs using Ed25519
Allow CRLs to be signed using Ed25519 private keys by passing a nil digest.

b62375bcde
2024-11-22 17:26:03 +00:00
Josh Cooper
6389c9a395 [ruby/openssl] Support signing requests using Ed25519
Allow requests to be signed using Ed25519 private keys by passing a nil digest.
This is similar to commit b0fc100091 when signing certs.

Calling PKey#public_key is deprecated and does not work for Ed25519. The same
can be accomplished by passing the private key.

d96090320d
2024-11-22 17:26:02 +00:00
Kazuki Yamaguchi
a0f1f16145 asn1: fix ObjectId#==
Compare by the dotted decimal notation rather than the NID.

OpenSSL::ASN1::ObjectId can store OIDs that are not registered in
OpenSSL's internal table. NID is not defined for such an OID, but it is
not an error.

The == method also should not raise TypeError if the other object is
not an instance of OpenSSL::ASN1::ObjectId.

Fixes: https://github.com/ruby/openssl/issues/791
2024-11-14 11:21:39 +09:00
Kazuki Yamaguchi
4e7e2e9e89 [ruby/openssl] pkcs7: fix memory leak in error path of PKCS7.new and .read_smime
A follow-up to commit 27e11f2d1d and 07eceb7f63. The PKCS7 object
must be freed before raising an exception.

172eee4665
2024-11-14 11:21:39 +09:00
Kazuki Yamaguchi
419fb2f3b9 [ruby/openssl] x509: fix handling of multiple URIs in Certificate#crl_uris
The implementation of OpenSSL::X509::Certificate#crl_uris makes the
assumption that each DistributionPoint in the CRL distribution points
extension contains a single general name of type URI. This is not
guaranteed by RFC 5280. A DistributionPoint may contain zero or more
than one URIs.

Let's include all URIs found in the extension. If only non-URI pointers
are found, return nil.

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

71f4fef2fa
2024-11-14 11:21:39 +09:00
HoneyryderChuck
2335768437 [ruby/openssl] make bn shareable when frozen
d3c8e661e8
2024-11-13 07:25:51 +00:00
Kazuki Yamaguchi
cb49ed2660 Update ext/openssl/depend
This is a follow-up to commit e5860e5654.
2024-11-06 21:59:38 +09:00
HoneyryderChuck
e5860e5654 [ruby/openssl] move ractor safe macro to ossl.h
in order to import or define the RUBY_TYPED_FROZEN_SHAREABLE macro.

b8504c2215
2024-11-06 10:17:41 +00:00
Kazuki Yamaguchi
27d77a9c73 [ruby/openssl] pkcs7: remove default cipher from PKCS7.encrypt
Require that users explicitly specify the desired algorithm. In my
opinion, we are not in a position to specify the default cipher.

When OpenSSL::PKCS7.encrypt is given only two arguments, it uses
"RC2-40-CBC" as the symmetric cipher algorithm. 40-bit RC2 is a US
export-grade cipher and considered insecure.

Although this is technically a breaking change, the impact should be
minimal. Even when OpenSSL is compiled with RC2 support and the macro
OPENSSL_NO_RC2 is not defined, it will not actually work on modern
systems because RC2 is part of the legacy provider.

439f456bfa
2024-10-31 08:31:16 +00:00
Kazuki Yamaguchi
339a8dd5e7 [ruby/openssl] ssl: remove redundant ossl_ssl_ex_vcb_idx
The SSL ex_data index is used for storing the verify_callback Proc. The
only user of it, ossl_ssl_verify_callback(), can find the callback by
looking at the SSLContext object which is always known.

3a3d6e258b
2024-10-31 08:28:34 +00:00
Yuki Morohoshi
772a213a29 [ruby/openssl] [DOC] better wording for OpenSSL::Config document.
26370636f3

Co-authored-by: Olle Jonsson <olle.jonsson@gmail.com>
2024-10-31 08:26:12 +00:00
Yuki Morohoshi
9d94a3b8aa [ruby/openssl] [DOC] Replace removed method in example for OpenSSL::Config#to_s
93c7bf52ac
2024-10-31 08:26:12 +00:00
kojix2
550ac2f2ed
[DOC] Fix typos 2024-10-31 12:44:50 +09:00
Samuel Giddins
0d3ce31234 [ruby/openssl] Set time directly on the x509 store
(https://github.com/ruby/openssl/pull/770)

Instead of an ivar, so other ossl functions that take a store will use the correct time when verifying

21aadc66ae
2024-07-24 16:51:00 +00:00