Commit graph

200 commits

Author SHA1 Message Date
Grant Gardner
4d4ac00123 [ruby/openssl] Add SSLSocket#readbyte
Companion to getbyte but raise EOFError
Similar to https://github.com/ruby/openssl/pull/438

c40f70711a
2024-07-03 08:54:18 +00:00
HoneyryderChuck
9c5e9d29f0 [ruby/openssl] rewriting most of the asn1 init code in ruby
to have as much of the lib in ruby as possible

8305051728
2024-06-27 16:00:55 +00:00
Kazuki Yamaguchi
69c0b1438a [ruby/openssl] Fix references to the license text
Update the references to the file "LICENCE" with "COPYING".

The file LICENCE doesn't exist in ruby/ruby nor ruby/openssl. This has
been always the case since OpenSSL for Ruby 2 was merged to the ruby
tree as a standard library in 2003.

In OpenSSL for Ruby 2's CVS repository[1], the LICENCE file contained
an old version of the Ruby License, identical to the COPYING file that
was in Ruby's tree at that time (r4128[2]).

[1] http://cvs.savannah.gnu.org/viewvc/rubypki/ossl2/LICENCE?revision=1.1.1.1&view=markup
[2] 231247c010/COPYING

5bccf07d04
2024-06-08 10:59:17 +00:00
Jaymz Julian
d39993a4ce [ruby/openssl] Fix performance regression in do_write(s)
This causes significant performance issues when using large (>10meg) writes

Fix by adjusting the buffer write function to clear the buffer once, rather than
piece by piece, avoiding a case where a large write (in our case, around
70mbytes) will consume 100% of CPU. This takes a webrick GET request via SSL
from around 200kbyts/sec and consuming 100% of a core, to line speed on gigabit
ethernet and 6% cpu utlization.

d4389b425d
2024-05-02 16:24:36 +09:00
Samuel Williams
368ce0758e [ruby/openssl] Further clarification of documentation.
0697f2f8b4
2024-04-30 14:51:59 +00:00
Samuel Williams
f1afae2459 [ruby/openssl] More documentation.
c8377eaf8d
2024-04-30 14:51:59 +00:00
Samuel Williams
1699772ac4 [ruby/openssl] Introduce basic support for close_read and close_write.
c99d24cee9
2024-04-30 14:51:58 +00:00
Samuel Williams
4f634d3c85 [ruby/openssl] Add support for IO#timeout.
(https://github.com/ruby/openssl/pull/714)

* Add support for IO#timeout.

3bbf5178a9
2024-01-17 17:09:03 +00:00
Ewoud Kohl van Wijngaarden
6213ab1a51 [ruby/openssl] Only set min_version on OpenSSL < 1.1.0
Both Red Hat and Debian-like systems configure the minimum TLS version
to be 1.2 by default, but allow users to change this via configs.

On Red Hat and derivatives this happens via crypto-policies[1], which in
writes settings in /etc/crypto-policies/back-ends/opensslcnf.config.
Most notably, it sets TLS.MinProtocol there. For Debian there's
MinProtocol in /etc/ssl/openssl.cnf. Both default to TLSv1.2, which is
considered a secure default.

In constrast, the SSLContext has a hard coded OpenSSL::SSL::TLS1_VERSION
for min_version. TLS 1.0 and 1.1 are considered insecure. By always
setting this in the default parameters, the system wide default can't be
respected, even if a developer wants to.

This takes the approach that's also done for ciphers: it's only set for
OpenSSL < 1.1.0.

[1]: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/security_hardening/using-the-system-wide-cryptographic-policies_security-hardening

ae215a47ae
2024-01-17 16:41:47 +00:00
Samuel Williams
f7178045bb [ruby/openssl] Add support for gets(chomp: true).
8aa3849cff
2024-01-13 00:28:26 +00:00
Samuel Williams
08d4e5ebef [ruby/openssl] Add more methods to SocketForwarder.
39eaa9f677
2024-01-13 00:28:26 +00:00
Kazuki Yamaguchi
2df00640ff [ruby/openssl] Ruby/OpenSSL 3.2.0
6b3dd6a372
2023-09-21 20:01:23 +00:00
Mau Magnaguagno
60a6de81a8 [ruby/openssl] Prefer String#unpack1
(https://github.com/ruby/openssl/pull/586)

String#unpack1 avoids the intermediate array created by String#unpack
for single elements, while also making a call to Array#first/[0]
unnecessary.

8eb0715a42
2023-09-06 19:24:53 +09:00
Kazuki Yamaguchi
b6d7cdc2ba [ruby/openssl] ssl: use ffdhe2048 from RFC 7919 as the default DH group parameters
In TLS 1.2 or before, if DH group parameters for DHE are not supplied
with SSLContext#tmp_dh= or #tmp_dh_callback=, we currently use the
self-generated parameters added in commit bb3399a61c ("support 2048
bit length DH-key", 2016-01-15) as the fallback.

While there is no known weakness in the current parameters, it would be
a good idea to switch to pre-defined, more well audited parameters.

This also allows the fallback to work in the FIPS mode.

The PEM encoding was derived with:

	# RFC 7919 Appendix A.1. ffdhe2048
	print OpenSSL::PKey.read(OpenSSL::ASN1::Sequence([OpenSSL::ASN1::Integer((<<-END).split.join.to_i(16)), OpenSSL::ASN1::Integer(2)]).to_der).to_pem
	    FFFFFFFF FFFFFFFF ADF85458 A2BB4A9A AFDC5620 273D3CF1
	    D8B9C583 CE2D3695 A9E13641 146433FB CC939DCE 249B3EF9
	    7D2FE363 630C75D8 F681B202 AEC4617A D3DF1ED5 D5FD6561
	    2433F51F 5F066ED0 85636555 3DED1AF3 B557135E 7F57C935
	    984F0C70 E0E68B77 E2A689DA F3EFE872 1DF158A1 36ADE735
	    30ACCA4F 483A797A BC0AB182 B324FB61 D108A94B B2C8E3FB
	    B96ADAB7 60D7F468 1D4F42A3 DE394DF4 AE56EDE7 6372BB19
	    0B07A7C8 EE0A6D70 9E02FCE1 CDF7E2EC C03404CD 28342F61
	    9172FE9C E98583FF 8E4F1232 EEF28183 C3FE3B1B 4C6FAD73
	    3BB5FCBC 2EC22005 C58EF183 7D1683B2 C6F34A26 C1B2EFFA
	    886B4238 61285C97 FFFFFFFF FFFFFFFF
	END

a5527cb4f4
2023-09-06 19:24:43 +09:00
Mau Magnaguagno
4a042b2255 [ruby/openssl] Refactor Buffering consume_rbuff and getbyte methods
Prefer ``slice!`` for ``Buffering#consume_rbuff`` and safe navigation with ``ord`` for ``Buffering#getbyte``, similar to ``each_byte``.

5f6abff178
2023-09-06 19:24:02 +09:00
Mau Magnaguagno
5b67c15cd8
[ruby/openssl] [DOC] Remove repeated example from Digest
5a36cc3cb2
2023-01-31 13:14:06 +09:00
Kazuki Yamaguchi
2d2baaf2f4 [ruby/openssl] Ruby/OpenSSL 3.1.0
c2f7d775c6
2022-12-23 09:39:15 +09:00
Yuta Saito
782777a803 [ruby/openssl] Undefine OpenSSL::SSL for no socket platforms
This fixes a linkage error about `ossl_ssl_type` on platforms which do
not have socket, like WASI.

Even before this patch, some items are disabled under `OPENSSL_NO_SOCK` since
ee22fad45d
However, due to some new use of OpenSSL::SSL::Socket over the past few years,
the build under `OPENSSL_NO_SOCK` had been broken.

This patch guards whole `OpenSSL::SSL` items by `OPENSSL_NO_SOCK`.

[ky: adjusted to apply on top of my previous commit that removed the
OpenSSL::ExtConfig, and added a guard to lib/openssl/ssl.rb.]

b0cfac6a96
2022-12-23 09:39:15 +09:00
Bart de Water
6166fa612c [ruby/openssl] Call out insecure PKCS #1 v1.5 default padding for RSA
fd5eaa6dfc
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
04bf83d6f7 [ruby/openssl] bump version number to 3.1.0.pre
fceb978a5d
2022-10-17 16:35:35 +09:00
Kazuki Yamaguchi
cd83f5b961 [ruby/openssl] Ruby/OpenSSL 3.0.1
e5bbd015dc
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
efce9ecf72 [ruby/openssl] Ruby/OpenSSL 3.0.0
bff4080091
2021-12-25 03:31:59 +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
Kazuki Yamaguchi
50b90c5fc3 [ruby/openssl] pkey/ec: avoid using EC#public_key= in EC#dh_compute_key
Similarly to DH#compute_key, work around it by constructing a
SubjectPublicKeyInfo. This should be considered as a temporary
implementation.

fc9aabc18d
2021-12-20 23:42:01 +09:00
Kazuki Yamaguchi
dc3f37c6cc [ruby/openssl] pkey/dh: avoid using DH#set_key in DH#compute_key
DH#set_key will not work on OpenSSL 3.0 because keys are immutable.
For now, let's reimplement DH#compute_key by manually constructing a
DER-encoded SubjectPublicKeyInfo structure and feeding it to
OpenSSL::PKey.read.

Eventually, we should implement a new method around EVP_PKEY_fromdata()
and use it instead.

46ca47060c
2021-12-20 23:42:01 +09:00
Kazuki Yamaguchi
b474049c78 [ruby/openssl] x509name: improve docs for X509::Name
Add docs for X509::Name.parse_openssl and X509::Name.parse_rfc2253,
which are currently undocumented despite being widely used.

Small changes are also made to #to_s and the class description to
recommend using RFC 2253-based methods.

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

74041a35d4
2021-11-01 17:48:03 +09:00
Kazuki Yamaguchi
cc8ff8b50d [ruby/openssl] require Ruby 2.6 or later
Drop support for Ruby 2.3, 2.4, and 2.5.

As of 2021-10, Ruby 2.6 is the oldest version that still receives
security fixes from the Ruby core team, so it doesn't make much sense
to keep code for those ancient versions.

3436bd040d
2021-10-16 19:39:13 +09:00
Kazuki Yamaguchi
4991dabdd0 [ruby/openssl] bump version number to 3.0.0.pre
baa83a8a57
2021-10-16 18:57:57 +09:00
Kazuki Yamaguchi
cea3c55d58 [ruby/openssl] Ruby/OpenSSL 2.2.1
65e7207a07
2021-10-16 18:34:35 +09:00
Kazuki Yamaguchi
6105ef7629 [ruby/openssl] ssl: add SSLContext#tmp_dh=
Provide a wrapper of SSL_set0_tmp_dh_pkey()/SSL_CTX_set_tmp_dh(), which
sets the DH parameters used for ephemeral DH key exchange.

SSLContext#tmp_dh_callback= already exists for this purpose, as a
wrapper around SSL_CTX_set_tmp_dh_callback(), but it is considered
obsolete and the OpenSSL API is deprecated for future removal. There is
no practical use case where an application needs to use different DH
parameters nowadays. This was originally introduced to support export
grade ciphers.

RDoc for #tmp_dh_callback= is updated to recommend the new #tmp_dh=.

Note that current versions of OpenSSL support automatic ECDHE curve
selection which is enabled by default. SSLContext#tmp_dh= should only be
necessary if you must allow ancient clients which don't support ECDHE.

aa43da4f04
2021-10-16 18:19:52 +09:00
Kazuki Yamaguchi
49217086ad [ruby/openssl] ssl: remove private method SSLSocket#tmp_ecdh_callback
Commit ee037e146037 ("ssl: remove SSL::SSLContext#tmp_ecdh_callback",
2020-08-12) forgot to remove the method.

bef9ea84e4
2021-10-16 18:19:51 +09:00
Kazuki Yamaguchi
bd356c6899 [ruby/openssl] Strip trailing spaces
68fa9c86f1
2021-07-18 18:04:50 +09:00
Kazuki Yamaguchi
5fc2912e60 [ruby/openssl] pkey/dsa: refactor DSA#sys{sign,verify} with PKey#{sign,verify}_raw
With the newly added OpenSSL::PKey::PKey#{sign,verify}_raw,
OpenSSL::PKey::DSA's low level signing operation methods can be
implemented in Ruby. The definitions are now in lib/openssl/pkey.rb.

ce805adf0c
2021-07-18 17:45:01 +09:00
Kazuki Yamaguchi
0c23e4a7aa [ruby/openssl] pkey/ec: refactor EC#dsa_{sign,verify}_asn1 with PKey#{sign,verify}_raw
With the newly added OpenSSL::PKey::PKey#{sign,verify}_raw,
OpenSSL::PKey::EC's low level signing operation methods can be
implemented in Ruby. The definitions are now in lib/openssl/pkey.rb.

1f9da0cd9d
2021-07-18 17:45:00 +09:00
Kazuki Yamaguchi
857a177b03 [ruby/openssl] pkey/rsa: port RSA#{private,public}_{encrypt,decrypt} to the EVP API
Implement these methods using the new OpenSSL::PKey::PKey#{encrypt,sign}
family. The definitions are now in lib/openssl/pkey.rb.

Also, recommend using those generic methods in the documentation.

2dfc1779d3
2021-07-18 17:44:59 +09:00
Samuel Williams
1146a94aee [ruby/openssl] Implement Certificate.load to load certificate chain. (https://github.com/ruby/openssl/pull/441)
* Add feature for loading the chained certificate into Certificate array.

05e1c015d6

Co-authored-by: Sao I Kuan <saoikuan@gmail.com>
2021-07-18 17:44:55 +09:00
Aaron Patterson
593164c2be [ruby/openssl] Add SSLSocket#getbyte
Normal sockets respond to `getbyte`, so we should make SSLSocket respond
to `getbyte` as well.  This way we can substitute SSLSockets for regular
sockets.

ac1490b7c9
2021-07-18 17:44:53 +09:00
Kazuki Yamaguchi
3fe8387950 [ruby/openssl] pkey: implement {DH,DSA,RSA}#public_key in Ruby
The low-level API that is used to implement #public_key is deprecated
in OpenSSL 3.0. It is actually very simple to implement in another way,
using existing methods only, in much shorter code. Let's do it.

While we are at it, the documentation is updated to recommend against
using #public_key. Now that OpenSSL::PKey::PKey implements public_to_der
method, there is no real use case for #public_key in newly written Ruby
programs.

48a6c391ef
2021-07-18 17:44:51 +09:00
Kazuki Yamaguchi
38436d1f5c [ruby/openssl] pkey/dsa: use high level EVP interface to generate parameters and keys
Implement PKey::DSA.new(size) and PKey::DSA.generate using
OpenSSL::PKey.generate_parameters and .generate_key instead of the low
level DSA functions.

1800a8d5eb
2021-07-18 17:44:49 +09:00
Kazuki Yamaguchi
b8dcf9c8fd [ruby/openssl] pkey/rsa: use high level EVP interface to generate parameters and keys
Implement PKey::RSA.new(size, exponent) and PKey::RSA.generate using
OpenSSL::PKey.generate_key instead of the low level RSA functions.

363fd10713
2021-07-18 17:44:48 +09:00
Kazuki Yamaguchi
098985a5e6 [ruby/openssl] pkey/dh: use high level EVP interface to generate parameters and keys
Implement PKey::DH.new(size, gen), PKey::DH.generate(size, gen), and
PKey::DH#generate_key! using PKey.generate_parameters and .generate_key
instead of the low level DH functions.

Note that the EVP interface can enforce additional restrictions - for
example, DH key shorter than 2048 bits is no longer accepted by default
in OpenSSL 3.0. The test code is updated accordingly.

c2e9b16f0b
2021-07-18 17:44:47 +09:00
Kazuki Yamaguchi
81325db5f8 [ruby/openssl] ssl: initialize verify_mode and verify_hostname with default values
SSLContext's verify_mode expects an SSL_VERIFY_* constant (an integer)
and verify_hostname expects either true or false. However, they are set
to nil after calling OpenSSL::SSL::SSLContext.new, which is surprising.

Set a proper value to them by default: verify_mode is set to
OpenSSL::SSL::VERIFY_NONE and verify_hostname is set to false by
default.

Note that this does not change the default behavior. The certificate
verification was never performed unless verify_mode is set to
OpenSSL::SSL::VERIFY_PEER by a user. The same applies to
verify_hostname.

87d869352c
2021-03-16 19:16:11 +09:00
Kazuki Yamaguchi
13198d4968 [ruby/openssl] hmac: implement base64digest methods
OpenSSL::HMAC implements the similar interface as ::Digest. Let's add
base64digest methods to OpenSSL::HMAC, too, for feature parity.

098bcb68af
2021-03-16 19:16:11 +09:00
Kazuki Yamaguchi
b91f62f384 [ruby/openssl] hmac: migrate from the low-level HMAC API to the EVP API
Use the EVP API instead of the low-level HMAC API. Use of the HMAC API
has been discouraged and is being marked as deprecated starting from
OpenSSL 3.0.0.

The two singleton methods OpenSSL::HMAC, HMAC.digest and HMAC.hexdigest
are now in lib/openssl/hmac.rb.

0317e2fc02
2021-03-16 19:16:11 +09:00
Kazuki Yamaguchi
9d3ffe09c4 [ruby/openssl] pkey: reimplement PKey::DH#compute_key and PKey::EC#dh_compute_key
Use the new OpenSSL::PKey::PKey#derive instead of the raw
{EC,}DH_compute_key(), mainly to reduce amount of the C code.

28edf6bafc
2021-03-16 19:16:11 +09:00
Kazuki Yamaguchi
22aeb6373e [ruby/openssl] config: revert to C implementation of OpenSSL::Config
Revert OpenSSL::Config to using the OpenSSL API and remove our own
parser implementation for the config file syntax.

OpenSSL::Config now wraps a CONF object. Accessor methods deal with the
object directly rather than Ruby-level internal state.

This work is based on the old C code we used before 2010.

c891e0ea89
2021-03-16 19:16:10 +09:00
Kazuki Yamaguchi
67f5847c61 [ruby/openssl] config: remove deprecated methods
Remove 4 deprecated methods.

The following two methods have been marked as deprecated since 2003,
by r4531 (ruby.git commit 78ff3833fb).

 - OpenSSL::Config#value
 - OpenSSL::Config#section

Other two methods are removed because the corresponding functions
disappeared in OpenSSL 1.1.0.

 - OpenSSL::Config#add_value
 - OpenSSL::Config#[]=

9783d7f21c
2021-03-16 19:16:10 +09:00
Nobuyoshi Nakada
d2bf6133f6
Remove trailing spaces [ci skip] 2020-07-20 13:34:16 +09:00
Bart de Water
0b2c70eaa1 [ruby/openssl] Look up digest by name instead of constant
b28fb2f05c
2020-05-13 15:47:51 +09:00