Skip to content

Commit dda139d

Browse files
rheniumtenderworks
authored andcommitted
[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 ruby/openssl#535 ruby/openssl@d486c82833
1 parent 9765521 commit dda139d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

ext/openssl/ossl_pkey.c

+36
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,42 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass)
104104
/* Then check PEM; multiple OSSL_DECODER_from_bio() calls may be needed */
105105
if (OSSL_DECODER_CTX_set_input_type(dctx, "PEM") != 1)
106106
goto out;
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)
131+
goto out;
132+
if (BIO_eof(bio))
133+
break;
134+
pos2 = BIO_tell(bio);
135+
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);
107143
while (1) {
108144
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
109145
goto out;

test/openssl/test_pkey_ec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ def test_ECPrivateKey
199199
assert_equal pem, p256.export
200200
end
201201

202+
def test_ECPrivateKey_with_parameters
203+
p256 = Fixtures.pkey("p256")
204+
205+
# The format used by "openssl ecparam -name prime256v1 -genkey -outform PEM"
206+
#
207+
# "EC PARAMETERS" block should be ignored if it is followed by an
208+
# "EC PRIVATE KEY" block
209+
in_pem = <<~EOF
210+
-----BEGIN EC PARAMETERS-----
211+
BggqhkjOPQMBBw==
212+
-----END EC PARAMETERS-----
213+
-----BEGIN EC PRIVATE KEY-----
214+
MHcCAQEEIID49FDqcf1O1eO8saTgG70UbXQw9Fqwseliit2aWhH1oAoGCCqGSM49
215+
AwEHoUQDQgAEFglk2c+oVUIKQ64eZG9bhLNPWB7lSZ/ArK41eGy5wAzU/0G51Xtt
216+
CeBUl+MahZtn9fO1JKdF4qJmS39dXnpENg==
217+
-----END EC PRIVATE KEY-----
218+
EOF
219+
220+
key = OpenSSL::PKey::EC.new(in_pem)
221+
assert_same_ec p256, key
222+
assert_equal p256.to_der, key.to_der
223+
end
224+
202225
def test_ECPrivateKey_encrypted
203226
p256 = Fixtures.pkey("p256")
204227
# key = abcdef

0 commit comments

Comments
 (0)