Skip to content

Commit 250b74b

Browse files
authored
Merge pull request #681 from junaruga/wip/fix-test-pkey-ec
Fix test_pkey_ec.rb on FIPS.
2 parents bdf060f + d07183f commit 250b74b

File tree

6 files changed

+77
-17
lines changed

6 files changed

+77
-17
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ jobs:
148148
run: echo "OPENSSL_CONF=$(pwd)/test/openssl/fixtures/ssl/openssl_fips.cnf" >> $GITHUB_ENV
149149
if: matrix.fips-enabled
150150

151-
- name: set fips environment variable for testing.
152-
run: echo "TEST_RUBY_OPENSSL_FIPS_ENABLED=true" >> $GITHUB_ENV
153-
if: matrix.fips-enabled
154-
155151
- name: load ruby
156152
uses: ruby/setup-ruby@v1
157153
with:
@@ -180,7 +176,5 @@ jobs:
180176
# TODO Fix other tests, and run all the tests on FIPS module.
181177
- name: test on fips module
182178
run: |
183-
bundle exec rake debug &&
184-
ruby -I./lib -ropenssl \
185-
-e 'Dir.glob "./test/openssl/{test_fips.rb,test_pkey.rb}", &method(:require)'
179+
rake test_fips TESTOPTS="-v --no-show-detail-immediately"
186180
if: matrix.fips-enabled

Rakefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,29 @@ Rake::TestTask.new do |t|
1818
t.warning = true
1919
end
2020

21+
desc 'Run tests for fips'
22+
task :test_fips do
23+
ENV['TEST_RUBY_OPENSSL_FIPS_ENABLED'] = 'true'
24+
Rake::Task['test_fips_internal'].invoke
25+
end
26+
27+
Rake::TestTask.new(:test_fips_internal) do |t|
28+
t.libs << 'test/openssl'
29+
t.test_files = FileList[
30+
'test/openssl/test_fips.rb',
31+
'test/openssl/test_pkey.rb',
32+
'test/openssl/test_pkey_ec.rb',
33+
]
34+
t.warning = true
35+
end
36+
2137
RDoc::Task.new do |rdoc|
2238
rdoc.main = "README.md"
2339
rdoc.rdoc_files.include("*.md", "lib/**/*.rb", "ext/**/*.c")
2440
end
2541

2642
task :test => [:compile, :debug]
43+
task :test_fips => [:compile, :debug]
2744

2845
# Print Ruby and compiler info for debugging purpose.
2946
task :debug_compiler do

test/openssl/test_fips.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def test_fips_mode_get_is_false_on_fips_mode_disabled
2828
end
2929

3030
def test_fips_mode_is_reentrant
31-
OpenSSL.fips_mode = false
32-
OpenSSL.fips_mode = false
31+
assert_separately(["-ropenssl"], <<~"end;")
32+
OpenSSL.fips_mode = false
33+
OpenSSL.fips_mode = false
34+
end;
3335
end
3436

3537
def test_fips_mode_get_with_fips_mode_set

test/openssl/test_pkey.rb

Lines changed: 33 additions & 8 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+
# Ed25519 is not FIPS-approved.
86+
omit_on_fips
87+
8588
# Test vector from RFC 8032 Section 7.1 TEST 2
8689
priv_pem = <<~EOF
8790
-----BEGIN PRIVATE KEY-----
@@ -96,15 +99,11 @@ def test_ed25519
9699
begin
97100
priv = OpenSSL::PKey.read(priv_pem)
98101
pub = OpenSSL::PKey.read(pub_pem)
99-
rescue OpenSSL::PKey::PKeyError
102+
rescue OpenSSL::PKey::PKeyError => e
100103
# OpenSSL < 1.1.1
101-
if !openssl?(1, 1, 1)
102-
pend "Ed25519 is not implemented"
103-
elsif OpenSSL.fips_mode && openssl?(3, 1, 0, 0)
104-
# See OpenSSL providers/fips/fipsprov.c PROV_NAMES_ED25519 entries
105-
# with FIPS_UNAPPROVED_PROPERTIES in OpenSSL 3.1+.
106-
pend "Ed25519 is not approved in OpenSSL 3.1+ FIPS code"
107-
end
104+
pend "Ed25519 is not implemented" unless openssl?(1, 1, 1)
105+
106+
raise e
108107
end
109108
assert_instance_of OpenSSL::PKey::PKey, priv
110109
assert_instance_of OpenSSL::PKey::PKey, pub
@@ -145,6 +144,32 @@ def test_ed25519
145144
assert_raise(OpenSSL::PKey::PKeyError) { priv.derive(pub) }
146145
end
147146

147+
def test_ed25519_not_approved_on_fips
148+
omit_on_non_fips
149+
# Ed25519 is technically allowed in the OpenSSL 3.0 code as a kind of bug.
150+
# So, we need to omit OpenSSL 3.0.
151+
#
152+
# See OpenSSL providers/fips/fipsprov.c PROV_NAMES_ED25519 entries with
153+
# FIPS_DEFAULT_PROPERTIES on openssl-3.0 branch and
154+
# FIPS_UNAPPROVED_PROPERTIES on openssl-3.1 branch.
155+
#
156+
# See also
157+
# https://github.com/openssl/openssl/issues/20758#issuecomment-1639658102
158+
# for details.
159+
unless openssl?(3, 1, 0, 0)
160+
omit 'Ed25519 is allowed in the OpenSSL 3.0 FIPS code as a kind of bug'
161+
end
162+
163+
priv_pem = <<~EOF
164+
-----BEGIN PRIVATE KEY-----
165+
MC4CAQAwBQYDK2VwBCIEIEzNCJso/5banbbDRuwRTg9bijGfNaumJNqM9u1PuKb7
166+
-----END PRIVATE KEY-----
167+
EOF
168+
assert_raise(OpenSSL::PKey::PKeyError) do
169+
OpenSSL::PKey.read(priv_pem)
170+
end
171+
end
172+
148173
def test_x25519
149174
# Test vector from RFC 7748 Section 6.1
150175
alice_pem = <<~EOF

test/openssl/test_pkey_ec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def test_ECPrivateKey_with_parameters
229229
end
230230

231231
def test_ECPrivateKey_encrypted
232+
omit_on_fips
233+
232234
p256 = Fixtures.pkey("p256")
233235
# key = abcdef
234236
pem = <<~EOF

test/openssl/utils.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ def teardown
139139
# OpenSSL error stack must be empty
140140
assert_equal([], OpenSSL.errors)
141141
end
142+
143+
# Omit the tests in FIPS.
144+
#
145+
# For example, the password based encryption used in the PEM format uses MD5
146+
# for deriving the encryption key from the password, and MD5 is not
147+
# FIPS-approved.
148+
#
149+
# See https://github.com/openssl/openssl/discussions/21830#discussioncomment-6865636
150+
# for details.
151+
def omit_on_fips
152+
return unless OpenSSL.fips_mode
153+
154+
omit 'An encryption used in the test is not FIPS-approved'
155+
end
156+
157+
def omit_on_non_fips
158+
return if OpenSSL.fips_mode
159+
160+
omit "Only for OpenSSL FIPS"
161+
end
142162
end
143163

144164
class OpenSSL::SSLTestCase < OpenSSL::TestCase

0 commit comments

Comments
 (0)