Skip to content

Commit 1fa9fc5

Browse files
authored
Merge pull request #694 from junaruga/wip/fips-test-pkey-dh
Fix test_pkey_dh.rb in FIPS.
2 parents ee740bf + 6a4ff26 commit 1fa9fc5

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Rake::TestTask.new(:test_fips_internal) do |t|
2929
t.test_files = FileList[
3030
'test/openssl/test_fips.rb',
3131
'test/openssl/test_pkey.rb',
32+
'test/openssl/test_pkey_dh.rb',
3233
'test/openssl/test_pkey_ec.rb',
3334
]
3435
t.warning = true

test/openssl/fixtures/pkey/dh1024.pem

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN DH PARAMETERS-----
2+
MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
3+
+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
4+
87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
5+
YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
6+
7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
7+
ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==
8+
-----END DH PARAMETERS-----

test/openssl/test_pkey_dh.rb

+44-20
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,26 @@ def test_new_generate
1818
assert_key(dh)
1919
end if ENV["OSSL_TEST_ALL"]
2020

21-
def test_new_break
21+
def test_new_break_on_non_fips
22+
omit_on_fips
23+
2224
assert_nil(OpenSSL::PKey::DH.new(NEW_KEYLEN) { break })
2325
assert_raise(RuntimeError) do
2426
OpenSSL::PKey::DH.new(NEW_KEYLEN) { raise }
2527
end
2628
end
2729

30+
def test_new_break_on_fips
31+
omit_on_non_fips
32+
33+
# The block argument is not executed in FIPS case.
34+
# See https://github.com/ruby/openssl/issues/692 for details.
35+
assert(OpenSSL::PKey::DH.new(NEW_KEYLEN) { break })
36+
assert(OpenSSL::PKey::DH.new(NEW_KEYLEN) { raise })
37+
end
38+
2839
def test_derive_key
29-
params = Fixtures.pkey("dh1024")
40+
params = Fixtures.pkey("dh2048_ffdhe2048")
3041
dh1 = OpenSSL::PKey.generate_key(params)
3142
dh2 = OpenSSL::PKey.generate_key(params)
3243
dh1_pub = OpenSSL::PKey.read(dh1.public_to_der)
@@ -44,34 +55,38 @@ def test_derive_key
4455
end
4556

4657
def test_DHparams
47-
dh1024 = Fixtures.pkey("dh1024")
48-
dh1024params = dh1024.public_key
58+
dh = Fixtures.pkey("dh2048_ffdhe2048")
59+
dh_params = dh.public_key
4960

5061
asn1 = OpenSSL::ASN1::Sequence([
51-
OpenSSL::ASN1::Integer(dh1024.p),
52-
OpenSSL::ASN1::Integer(dh1024.g)
62+
OpenSSL::ASN1::Integer(dh.p),
63+
OpenSSL::ASN1::Integer(dh.g)
5364
])
5465
key = OpenSSL::PKey::DH.new(asn1.to_der)
55-
assert_same_dh dh1024params, key
66+
assert_same_dh dh_params, key
5667

5768
pem = <<~EOF
5869
-----BEGIN DH PARAMETERS-----
59-
MIGHAoGBAKnKQ8MNK6nYZzLrrcuTsLxuiJGXoOO5gT+tljOTbHBuiktdMTITzIY0
60-
pFxIvjG05D7HoBZQfrR0c92NGWPkAiCkhQKB8JCbPVzwNLDy6DZ0pmofDKrEsYHG
61-
AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
70+
MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
71+
+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
72+
87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
73+
YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
74+
7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
75+
ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==
6276
-----END DH PARAMETERS-----
6377
EOF
78+
6479
key = OpenSSL::PKey::DH.new(pem)
65-
assert_same_dh dh1024params, key
80+
assert_same_dh dh_params, key
6681
key = OpenSSL::PKey.read(pem)
67-
assert_same_dh dh1024params, key
82+
assert_same_dh dh_params, key
6883

69-
assert_equal asn1.to_der, dh1024.to_der
70-
assert_equal pem, dh1024.export
84+
assert_equal asn1.to_der, dh.to_der
85+
assert_equal pem, dh.export
7186
end
7287

7388
def test_public_key
74-
dh = Fixtures.pkey("dh1024")
89+
dh = Fixtures.pkey("dh2048_ffdhe2048")
7590
public_key = dh.public_key
7691
assert_no_key(public_key) #implies public_key.public? is false!
7792
assert_equal(dh.to_der, public_key.to_der)
@@ -80,7 +95,8 @@ def test_public_key
8095

8196
def test_generate_key
8297
# Deprecated in v3.0.0; incompatible with OpenSSL 3.0
83-
dh = Fixtures.pkey("dh1024").public_key # creates a copy with params only
98+
# Creates a copy with params only
99+
dh = Fixtures.pkey("dh2048_ffdhe2048").public_key
84100
assert_no_key(dh)
85101
dh.generate_key!
86102
assert_key(dh)
@@ -91,7 +107,15 @@ def test_generate_key
91107
end if !openssl?(3, 0, 0)
92108

93109
def test_params_ok?
94-
dh0 = Fixtures.pkey("dh1024")
110+
# Skip the tests in old OpenSSL version 1.1.1c or early versions before
111+
# applying the following commits in OpenSSL 1.1.1d to make `DH_check`
112+
# function pass the RFC 7919 FFDHE group texts.
113+
# https://github.com/openssl/openssl/pull/9435
114+
unless openssl?(1, 1, 1, 4)
115+
pend 'DH check for RFC 7919 FFDHE group texts is not implemented'
116+
end
117+
118+
dh0 = Fixtures.pkey("dh2048_ffdhe2048")
95119

96120
dh1 = OpenSSL::PKey::DH.new(OpenSSL::ASN1::Sequence([
97121
OpenSSL::ASN1::Integer(dh0.p),
@@ -108,7 +132,7 @@ def test_params_ok?
108132

109133
def test_dup
110134
# Parameters only
111-
dh1 = Fixtures.pkey("dh1024")
135+
dh1 = Fixtures.pkey("dh2048_ffdhe2048")
112136
dh2 = dh1.dup
113137
assert_equal dh1.to_der, dh2.to_der
114138
assert_not_equal nil, dh1.p
@@ -125,7 +149,7 @@ def test_dup
125149
end
126150

127151
# With a key pair
128-
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh1024"))
152+
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh2048_ffdhe2048"))
129153
dh4 = dh3.dup
130154
assert_equal dh3.to_der, dh4.to_der
131155
assert_equal dh1.to_der, dh4.to_der # encodes parameters only
@@ -136,7 +160,7 @@ def test_dup
136160
end
137161

138162
def test_marshal
139-
dh = Fixtures.pkey("dh1024")
163+
dh = Fixtures.pkey("dh2048_ffdhe2048")
140164
deserialized = Marshal.load(Marshal.dump(dh))
141165

142166
assert_equal dh.to_der, deserialized.to_der

test/openssl/utils.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ def teardown
151151
def omit_on_fips
152152
return unless OpenSSL.fips_mode
153153

154-
omit 'An encryption used in the test is not FIPS-approved'
154+
omit <<~MESSAGE
155+
Only for OpenSSL non-FIPS with the following possible reasons:
156+
* A testing logic is non-FIPS specific.
157+
* An encryption used in the test is not FIPS-approved.
158+
MESSAGE
155159
end
156160

157161
def omit_on_non_fips

0 commit comments

Comments
 (0)