Commit graph

1176 commits

Author SHA1 Message Date
Kazuki Yamaguchi
06a56a7ffc [ruby/openssl] ssl: fix potential memory leak in SSLContext#setup
If SSL_CTX_add_extra_chain_cert() fails, the refcount of x509 must be
handled by the caller. This should only occur due to a malloc failure
inside the function.

80bcf727dc
2025-05-15 16:51:15 +00:00
Kazuki Yamaguchi
b43c7cf8c4 [ruby/openssl] cipher: remove Cipher#encrypt(password, iv) form
OpenSSL::Cipher#encrypt and #decrypt have long supported a hidden
feature to derive a key and an IV from the String argument, but in an
inappropriate way.

This feature is undocumented, untested, and has been deprecated since
commit 0dc43217b1 on 2004-06-30,
which started printing a non-verbose warning. More than 20 years later,
it must be safe to remove it entirely.

The deprecated usage:

	# `password` is a String, `iv` is either a String or nil
	cipher = OpenSSL::Cipher.new("aes-256-cbc")
	cipher.encrypt(password, iv)
	p cipher.update("data") << cipher.final

was equivalent to:

	cipher = OpenSSL::Cipher.new("aes-256-cbc")
	cipher.encrypt

	iv ||= "OpenSSL for Ruby rulez!"
	key = ((cipher.key_len + 15) / 16).times.inject([""]) { |ary, _|
	  ary << OpenSSL::Digest.digest("MD5", ary.last + password + iv[0, 8].ljust(8, "\0"))
	}.join
	cipher.key = key[...cipher.key_len]
	cipher.iv = iv[...cipher.iv_len].ljust(cipher.iv_len, "\0")
	p cipher.update("data") << cipher.final

e46d992ea1
2025-05-15 16:50:25 +00:00
Kazuki Yamaguchi
93afcfcde3 [ruby/openssl] asn1: check for missing EOC in indefinite length encoding
EOC octets are required at the end of contents of a constructed encoding
that uses the indefinite length form. This cannot be assumed from the
end of the input. Raise an exception when necessary.

bc20c13a7c
2025-04-20 07:41:15 +00:00
Kazuki Yamaguchi
0a8a641d0a [ruby/openssl] ssl: fix SSLSocket#syswrite with String-convertible objects
Correctly pass the new object assigned by StringValue() to
ossl_ssl_write_internal_safe().

This is a follow-up to commit 0d8c17aa85 (Reduce
OpenSSL::Buffering#do_write overhead, 2024-12-21).

3ff096196a
2025-04-16 15:16:50 +00:00
Sarun Rattanasiri
9ec8dc9c65 [ruby/openssl] mark initialize_copy as :nodoc:
17f87d2cf0
2025-03-12 16:03:47 +00:00
Kazuki Yamaguchi
4ac75f6f64 [ruby/openssl] x509name: do not check for negative return from X509_NAME_entry_count()
The function never returns a negative number.

895ce6fdfc
2025-02-11 16:42:26 +00:00
Kazuki Yamaguchi
47cdf98fa4 [ruby/openssl] x509: do not check for negative return from X509_*_get_ext_count()
These functions wrap X509v3_get_ext_count(). The implementation can
never return a negative number, and this behavior is documented in the
man page.

5164725855
2025-02-11 16:42:26 +00:00
Kazuki Yamaguchi
8888ad6902 [ruby/openssl] ossl.c: avoid using sk_*() functions with NULL
Always use explicit NULL checks before interacting with STACK_OF(*).
Even though most OpenSSL functions named sk_*() do not crash if we pass
NULL as the receiver object, depending on this behavior would be a bad
idea.

Checks for a negative number return from sk_*_num() are removed. This
can only happen when the stack is NULL.

ossl_*_sk2ary() must no longer be called with NULL.

84cffd4f77
2025-02-11 16:42:25 +00:00
Samuel Chiang
06faf28558 [ruby/openssl] Add build support for AWS-LC
CI Changes
1. I've split the original patch up to make it easier to digest, but
that forces my hand to turn off testing in the AWS-LC CI for the time
being. However, do let me know if you would prefer to review the test
adjustments in the same PR and I can remove the temporary CI workaround.
2. AWS-LC has a few no-op functions and we use -Wdeprecated-declarations
to alert the consuming application of these. I've leveraged the
skip-warnings CI option so that the build doesn't fail.

Build Adjustments
1. AWS-LC FIPS mode is decided at compile time. This is different from
OpenSSL's togglable FIPS switch, so I've adjusted the build to account
for this.
2. AWS-LC does not support for the two KEY_SIG or KEY_EX flags that were
only ever supported by old MSIE.
3. AWS-LC has no current support for post handshake authentication in
TLS 1.3.
4. EC_GROUP structures for named curves in AWS-LC are constant, static,
and immutable by default. This means that the EC_GROUP_set_* functions
are essentially no-ops due to the immutability of the structure. We've
introduced a new API for consumers that depend on the OpenSSL's default
mutability of the EC_GROUP structure called
EC_GROUP_new_by_curve_name_mutable. Since Ruby has a bit of
functionality that's dependent on the mutability of these structures,
I've made the corresponding adjustments to allow things to work as
expected.

e53ec5a101
2025-02-11 15:35:03 +00:00
Kazuki Yamaguchi
f84d75eecc [ruby/openssl] pkey/ec: remove deprecated PKey::EC::Point#mul(ary, ary [, bn]) form
The method has two forms, each corresponding to EC_POINT_mul() and
EC_POINTs_mul(). The latter form does not work with any OpenSSL or
LibreSSL versions that are still supported by upstream.

The latter form has an extremely confusing behavior, too, and using it
would print a deprecation warning since commit 812de4253d in 2020,
which went to 3.0.0. Let's remove it.

7343d3c559
2025-02-03 09:47:48 +00:00
Kazuki Yamaguchi
5a14f53695 [ruby/openssl] ssl: separate SSLContext#min_version= and #max_version=
Make these methods simple wrappers around
SSL_CTX_set_{min,max}_proto_version().

When we introduced these methods in commit 18603949d3 [1], which went
to v2.1.0, we added a private method to SSLContext that set both the
minimum and maximum protocol versions at the same time. This was to
allow emulating the behavior using SSL options on older OpenSSL versions
that lack SSL_CTX_set_{min,max}_proto_version(). Since we no longer
support OpenSSL 1.0.2, the related code has already been removed.

In OpenSSL 1.1.1 or later, setting the minimum or maximum version to 0
is not equivalent to leaving it unset. Similar to SSL options, which we
avoid overwriting as of commit 00bec0d905 and commit 77c3db2d65 [2],
a system-wide configuration file may define a default protocol version
bounds. Setting the minimum version should not unset the maximum
version, and vice versa.

[1] https://github.com/ruby/openssl/pull/142
[2] https://github.com/ruby/openssl/pull/767

5766386321
2025-02-03 09:46:03 +00:00
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