Skip to content

Commit 7e411b4

Browse files
authored
Merge pull request ruby#615 from junaruga/wip/fips-read
Fix OpenSSL::PKey.read that cannot parse PKey in the FIPS mode.
2 parents 5f505c5 + 8149cdf commit 7e411b4

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,7 @@ jobs:
149149
# Run only the passing tests on the FIPS mode as a temporary workaround.
150150
# TODO Fix other tests, and run all the tests on FIPS mode.
151151
- name: test on fips mode
152-
run: ruby -Ilib test/openssl/test_fips.rb
152+
run: |
153+
ruby -I./lib -ropenssl \
154+
-e 'Dir.glob "./test/openssl/{test_fips.rb,test_pkey.rb}", &method(:require)'
153155
if: matrix.fips_enabled

ext/openssl/ossl_pkey.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass)
101101
goto out;
102102
OSSL_BIO_reset(bio);
103103

104-
/* Then check PEM; multiple OSSL_DECODER_from_bio() calls may be needed */
105-
if (OSSL_DECODER_CTX_set_input_type(dctx, "PEM") != 1)
106-
goto out;
107104
/*
105+
* Then check PEM; multiple OSSL_DECODER_from_bio() calls may be needed.
106+
*
108107
* First check for private key formats. This is to keep compatibility with
109108
* ruby/openssl < 3.0 which decoded the following as a private key.
110109
*
@@ -124,8 +123,19 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass)
124123
*
125124
* Note that normally, the input is supposed to contain a single decodable
126125
* PEM block only, so this special handling should not create a new problem.
126+
*
127+
* Note that we need to create the OSSL_DECODER_CTX variable each time when
128+
* we use the different selection as a workaround.
129+
* https://github.com/openssl/openssl/issues/20657
127130
*/
128-
OSSL_DECODER_CTX_set_selection(dctx, EVP_PKEY_KEYPAIR);
131+
OSSL_DECODER_CTX_free(dctx);
132+
dctx = NULL;
133+
dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL,
134+
EVP_PKEY_KEYPAIR, NULL, NULL);
135+
if (!dctx)
136+
goto out;
137+
if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, ppass) != 1)
138+
goto out;
129139
while (1) {
130140
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
131141
goto out;
@@ -139,7 +149,13 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass)
139149
}
140150

141151
OSSL_BIO_reset(bio);
142-
OSSL_DECODER_CTX_set_selection(dctx, 0);
152+
OSSL_DECODER_CTX_free(dctx);
153+
dctx = NULL;
154+
dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL, 0, NULL, NULL);
155+
if (!dctx)
156+
goto out;
157+
if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, ppass) != 1)
158+
goto out;
143159
while (1) {
144160
if (OSSL_DECODER_from_bio(dctx, bio) == 1)
145161
goto out;

test/openssl/test_pkey.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def test_hmac_sign_verify
8282
end
8383

8484
def test_ed25519
85+
# https://github.com/openssl/openssl/issues/20758
86+
pend('Not supported on FIPS mode enabled') if OpenSSL.fips_mode
87+
8588
# Test vector from RFC 8032 Section 7.1 TEST 2
8689
priv_pem = <<~EOF
8790
-----BEGIN PRIVATE KEY-----
@@ -127,6 +130,8 @@ def test_ed25519
127130
end
128131

129132
def test_x25519
133+
pend('Not supported on FIPS mode enabled') if OpenSSL.fips_mode
134+
130135
# Test vector from RFC 7748 Section 6.1
131136
alice_pem = <<~EOF
132137
-----BEGIN PRIVATE KEY-----
@@ -153,6 +158,8 @@ def test_x25519
153158
end
154159

155160
def test_compare?
161+
pend('Not supported on FIPS mode enabled') if OpenSSL.fips_mode
162+
156163
key1 = Fixtures.pkey("rsa1024")
157164
key2 = Fixtures.pkey("rsa1024")
158165
key3 = Fixtures.pkey("rsa2048")

test/openssl/utils.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# frozen_string_literal: true
22
begin
33
require "openssl"
4-
5-
# Disable FIPS mode for tests for installations
6-
# where FIPS mode would be enabled by default.
7-
# Has no effect on all other installations.
8-
OpenSSL.fips_mode=false
94
rescue LoadError
105
end
116

0 commit comments

Comments
 (0)