Skip to content

Commit 7f5b5ad

Browse files
committed
Merge branch 'maint-3.0'
* maint-3.0: Ruby/OpenSSL 3.0.1 Ruby/OpenSSL 2.2.2 Ruby/OpenSSL 2.1.4 Make GitHub Actions happy on 2.1/2.2 branches test/openssl/test_cipher: skip AES-CCM tests on OpenSSL <= 1.1.1b pkey/ec: check existence of public key component before exporting pkey: restore support for decoding "openssl ecparam -genkey" output pkey: clear error queue before each OSSL_DECODER_from_bio() call pkey/dsa: let PKey::DSA.generate choose appropriate q size hmac: use EVP_PKEY_new_raw_private_key() if available x509*: fix error queue leak in #extensions= and #attributes= methods Check if the option is an Hash in `pkey_ctx_apply_options0()`
2 parents ed83759 + e5bbd01 commit 7f5b5ad

File tree

16 files changed

+175
-17
lines changed

16 files changed

+175
-17
lines changed

History.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
Version 3.0.1
2+
=============
3+
4+
Merged changes in 2.1.4 and 2.2.2. Additionally, the following issues are fixed
5+
by this release.
6+
7+
Bug fixes
8+
---------
9+
10+
* Add missing type check in OpenSSL::PKey::PKey#sign's optional parameters.
11+
[[GitHub #531]](https://github.com/ruby/openssl/pull/531)
12+
* Work around OpenSSL 3.0's HMAC issues with a zero-length key.
13+
[[GitHub #538]](https://github.com/ruby/openssl/pull/538)
14+
* Fix a regression in OpenSSL::PKey::DSA.generate's default of 'q' size.
15+
[[GitHub #483]](https://github.com/ruby/openssl/issues/483)
16+
[[GitHub #539]](https://github.com/ruby/openssl/pull/539)
17+
* Restore OpenSSL::PKey.read's ability to decode "openssl ecparam -genkey"
18+
output when linked against OpenSSL 3.0.
19+
[[GitHub #535]](https://github.com/ruby/openssl/pull/535)
20+
[[GitHub #540]](https://github.com/ruby/openssl/pull/540)
21+
* Restore error checks in OpenSSL::PKey::EC#{to_der,to_pem}.
22+
[[GitHub #541]](https://github.com/ruby/openssl/pull/541)
23+
24+
125
Version 3.0.0
226
=============
327

@@ -100,6 +124,12 @@ Notable changes
100124
[[GitHub #342]](https://github.com/ruby/openssl/issues/342)
101125

102126

127+
Version 2.2.2
128+
=============
129+
130+
Merged changes in 2.1.4.
131+
132+
103133
Version 2.2.1
104134
=============
105135

@@ -194,6 +224,16 @@ Notable changes
194224
[[GitHub #297]](https://github.com/ruby/openssl/pull/297)
195225

196226

227+
Version 2.1.4
228+
=============
229+
230+
Bug fixes
231+
---------
232+
233+
* Do not use pkg-config if --with-openssl-dir option is specified.
234+
[[GitHub #486]](https://github.com/ruby/openssl/pull/486)
235+
236+
197237
Version 2.1.3
198238
=============
199239

ext/openssl/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def find_openssl_library
174174

175175
# added in 1.1.1
176176
have_func("EVP_PKEY_check", evp_h)
177+
have_func("EVP_PKEY_new_raw_private_key", evp_h)
177178
have_func("SSL_CTX_set_ciphersuites", ssl_h)
178179

179180
# added in 3.0.0

ext/openssl/ossl_hmac.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,19 @@ ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
9797

9898
GetHMAC(self, ctx);
9999
StringValue(key);
100+
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
101+
pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
102+
(unsigned char *)RSTRING_PTR(key),
103+
RSTRING_LENINT(key));
104+
if (!pkey)
105+
ossl_raise(eHMACError, "EVP_PKEY_new_raw_private_key");
106+
#else
100107
pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
101108
(unsigned char *)RSTRING_PTR(key),
102109
RSTRING_LENINT(key));
103110
if (!pkey)
104111
ossl_raise(eHMACError, "EVP_PKEY_new_mac_key");
112+
#endif
105113
if (EVP_DigestSignInit(ctx, NULL, ossl_evp_get_digestbyname(digest),
106114
NULL, pkey) != 1) {
107115
EVP_PKEY_free(pkey);

ext/openssl/ossl_pkey.c

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,56 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass)
9999
/* First check DER */
100100
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
101101
goto out;
102+
OSSL_BIO_reset(bio);
102103

103104
/* Then check PEM; multiple OSSL_DECODER_from_bio() calls may be needed */
104-
OSSL_BIO_reset(bio);
105105
if (OSSL_DECODER_CTX_set_input_type(dctx, "PEM") != 1)
106106
goto out;
107-
while (OSSL_DECODER_from_bio(dctx, bio) != 1) {
108-
if (BIO_eof(bio))
107+
/*
108+
* First check for private key formats. This is to keep compatibility with
109+
* ruby/openssl < 3.0 which decoded the following as a private key.
110+
*
111+
* $ openssl ecparam -name prime256v1 -genkey -outform PEM
112+
* -----BEGIN EC PARAMETERS-----
113+
* BggqhkjOPQMBBw==
114+
* -----END EC PARAMETERS-----
115+
* -----BEGIN EC PRIVATE KEY-----
116+
* MHcCAQEEIAG8ugBbA5MHkqnZ9ujQF93OyUfL9tk8sxqM5Wv5tKg5oAoGCCqGSM49
117+
* AwEHoUQDQgAEVcjhJfkwqh5C7kGuhAf8XaAjVuG5ADwb5ayg/cJijCgs+GcXeedj
118+
* 86avKpGH84DXUlB23C/kPt+6fXYlitUmXQ==
119+
* -----END EC PRIVATE KEY-----
120+
*
121+
* While the first PEM block is a proper encoding of ECParameters, thus
122+
* OSSL_DECODER_from_bio() would pick it up, ruby/openssl used to return
123+
* the latter instead. Existing applications expect this behavior.
124+
*
125+
* Note that normally, the input is supposed to contain a single decodable
126+
* PEM block only, so this special handling should not create a new problem.
127+
*/
128+
OSSL_DECODER_CTX_set_selection(dctx, EVP_PKEY_KEYPAIR);
129+
while (1) {
130+
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
109131
goto out;
132+
if (BIO_eof(bio))
133+
break;
110134
pos2 = BIO_tell(bio);
111135
if (pos2 < 0 || pos2 <= pos)
136+
break;
137+
ossl_clear_error();
138+
pos = pos2;
139+
}
140+
141+
OSSL_BIO_reset(bio);
142+
OSSL_DECODER_CTX_set_selection(dctx, 0);
143+
while (1) {
144+
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
112145
goto out;
146+
if (BIO_eof(bio))
147+
break;
148+
pos2 = BIO_tell(bio);
149+
if (pos2 < 0 || pos2 <= pos)
150+
break;
151+
ossl_clear_error();
113152
pos = pos2;
114153
}
115154

@@ -200,6 +239,7 @@ static VALUE
200239
pkey_ctx_apply_options0(VALUE args_v)
201240
{
202241
VALUE *args = (VALUE *)args_v;
242+
Check_Type(args[1], T_HASH);
203243

204244
rb_block_call(args[1], rb_intern("each"), 0, NULL,
205245
pkey_ctx_apply_options_i, args[0]);

ext/openssl/ossl_pkey_ec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ ossl_ec_key_export(int argc, VALUE *argv, VALUE self)
414414
EC_KEY *ec;
415415

416416
GetEC(self, ec);
417+
if (EC_KEY_get0_public_key(ec) == NULL)
418+
ossl_raise(eECError, "can't export - no public key set");
417419
if (EC_KEY_get0_private_key(ec))
418420
return ossl_pkey_export_traditional(argc, argv, self, 0);
419421
else
@@ -432,6 +434,8 @@ ossl_ec_key_to_der(VALUE self)
432434
EC_KEY *ec;
433435

434436
GetEC(self, ec);
437+
if (EC_KEY_get0_public_key(ec) == NULL)
438+
ossl_raise(eECError, "can't export - no public key set");
435439
if (EC_KEY_get0_private_key(ec))
436440
return ossl_pkey_export_traditional(0, NULL, self, 1);
437441
else

ext/openssl/ossl_x509cert.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,12 +642,12 @@ ossl_x509_set_extensions(VALUE self, VALUE ary)
642642
OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
643643
}
644644
GetX509(self, x509);
645-
while ((ext = X509_delete_ext(x509, 0)))
646-
X509_EXTENSION_free(ext);
645+
for (i = X509_get_ext_count(x509); i > 0; i--)
646+
X509_EXTENSION_free(X509_delete_ext(x509, 0));
647647
for (i=0; i<RARRAY_LEN(ary); i++) {
648648
ext = GetX509ExtPtr(RARRAY_AREF(ary, i));
649649
if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext */
650-
ossl_raise(eX509CertError, NULL);
650+
ossl_raise(eX509CertError, "X509_add_ext");
651651
}
652652
}
653653

ext/openssl/ossl_x509crl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ ossl_x509crl_set_extensions(VALUE self, VALUE ary)
474474
OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
475475
}
476476
GetX509CRL(self, crl);
477-
while ((ext = X509_CRL_delete_ext(crl, 0)))
478-
X509_EXTENSION_free(ext);
477+
for (i = X509_CRL_get_ext_count(crl); i > 0; i--)
478+
X509_EXTENSION_free(X509_CRL_delete_ext(crl, 0));
479479
for (i=0; i<RARRAY_LEN(ary); i++) {
480480
ext = GetX509ExtPtr(RARRAY_AREF(ary, i)); /* NO NEED TO DUP */
481481
if (!X509_CRL_add_ext(crl, ext, -1)) {
482-
ossl_raise(eX509CRLError, NULL);
482+
ossl_raise(eX509CRLError, "X509_CRL_add_ext");
483483
}
484484
}
485485

ext/openssl/ossl_x509req.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,13 @@ ossl_x509req_set_attributes(VALUE self, VALUE ary)
380380
OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Attr);
381381
}
382382
GetX509Req(self, req);
383-
while ((attr = X509_REQ_delete_attr(req, 0)))
384-
X509_ATTRIBUTE_free(attr);
383+
for (i = X509_REQ_get_attr_count(req); i > 0; i--)
384+
X509_ATTRIBUTE_free(X509_REQ_delete_attr(req, 0));
385385
for (i=0;i<RARRAY_LEN(ary); i++) {
386386
item = RARRAY_AREF(ary, i);
387387
attr = GetX509AttrPtr(item);
388388
if (!X509_REQ_add1_attr(req, attr)) {
389-
ossl_raise(eX509ReqError, NULL);
389+
ossl_raise(eX509ReqError, "X509_REQ_add1_attr");
390390
}
391391
}
392392
return ary;

ext/openssl/ossl_x509revoked.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ ossl_x509revoked_set_extensions(VALUE self, VALUE ary)
223223
OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
224224
}
225225
GetX509Rev(self, rev);
226-
while ((ext = X509_REVOKED_delete_ext(rev, 0)))
227-
X509_EXTENSION_free(ext);
226+
for (i = X509_REVOKED_get_ext_count(rev); i > 0; i--)
227+
X509_EXTENSION_free(X509_REVOKED_delete_ext(rev, 0));
228228
for (i=0; i<RARRAY_LEN(ary); i++) {
229229
item = RARRAY_AREF(ary, i);
230230
ext = GetX509ExtPtr(item);
231231
if(!X509_REVOKED_add_ext(rev, ext, -1)) {
232-
ossl_raise(eX509RevError, NULL);
232+
ossl_raise(eX509RevError, "X509_REVOKED_add_ext");
233233
}
234234
}
235235

lib/openssl/pkey.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,16 @@ class << self
167167
# +size+::
168168
# The desired key size in bits.
169169
def generate(size, &blk)
170+
# FIPS 186-4 specifies four (L,N) pairs: (1024,160), (2048,224),
171+
# (2048,256), and (3072,256).
172+
#
173+
# q size is derived here with compatibility with
174+
# DSA_generator_parameters_ex() which previous versions of ruby/openssl
175+
# used to call.
176+
qsize = size >= 2048 ? 256 : 160
170177
dsaparams = OpenSSL::PKey.generate_parameters("DSA", {
171178
"dsa_paramgen_bits" => size,
179+
"dsa_paramgen_q_bits" => qsize,
172180
}, &blk)
173181
OpenSSL::PKey.generate_key(dsaparams)
174182
end

0 commit comments

Comments
 (0)