Skip to content

Commit d9ed13e

Browse files
committed
Fix test_pkey_dh.rb in FIPS.
Add dh2048_fips.pem file (DH 2048 bits) in FIPS case, because a logic to generate the pem file in FIPS module is different from the logic in non-FIPS default behavior. Created the DH file with 2048 bits, because the following command failed to generate the DH 1024 bits pem file in FIPS. ``` $ OPENSSL_CONF=/home/jaruga/.local/openssl-3.3.0-dev-fips-debug-1aa08644ec/ssl/openssl_fips.cnf \ /home/jaruga/.local/openssl-3.3.0-dev-fips-debug-1aa08644ec/bin/openssl dhparam -out dh1024_fips.pem 1024 Generating DH parameters, 1024 bit long safe prime dhparam: Generating DH key parameters failed ``` The dh2048_fips.pem file was created by the following command. ``` $ OPENSSL_CONF=/home/jaruga/.local/openssl-3.3.0-dev-fips-debug-1aa08644ec/ssl/openssl_fips.cnf \ /home/jaruga/.local/openssl-3.3.0-dev-fips-debug-1aa08644ec/bin/openssl dhparam -out dh2048_fips.pem 2048 ```
1 parent fac5cac commit d9ed13e

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
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
Lines changed: 8 additions & 0 deletions
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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class OpenSSL::TestPKeyDH < OpenSSL::PKeyTestCase
77
NEW_KEYLEN = 2048
8+
KEY_NAME = (OpenSSL.fips_mode) ? "dh2048_fips" : "dh1024"
89

910
def test_new_empty
1011
dh = OpenSSL::PKey::DH.new
@@ -18,15 +19,25 @@ def test_new_generate
1819
assert_key(dh)
1920
end if ENV["OSSL_TEST_ALL"]
2021

21-
def test_new_break
22+
def test_new_break_on_non_fips
23+
omit_on_fips
24+
2225
assert_nil(OpenSSL::PKey::DH.new(NEW_KEYLEN) { break })
2326
assert_raise(RuntimeError) do
2427
OpenSSL::PKey::DH.new(NEW_KEYLEN) { raise }
2528
end
2629
end
2730

31+
def test_new_break_on_fips
32+
omit_on_non_fips
33+
34+
# The block argument is not executed in FIPS case.
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(KEY_NAME)
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,28 @@ 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(KEY_NAME)
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
56-
57-
pem = <<~EOF
58-
-----BEGIN DH PARAMETERS-----
59-
MIGHAoGBAKnKQ8MNK6nYZzLrrcuTsLxuiJGXoOO5gT+tljOTbHBuiktdMTITzIY0
60-
pFxIvjG05D7HoBZQfrR0c92NGWPkAiCkhQKB8JCbPVzwNLDy6DZ0pmofDKrEsYHG
61-
AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
62-
-----END DH PARAMETERS-----
63-
EOF
66+
assert_same_dh dh_params, key
67+
68+
pem = Fixtures.pkey_str(KEY_NAME)
6469
key = OpenSSL::PKey::DH.new(pem)
65-
assert_same_dh dh1024params, key
70+
assert_same_dh dh_params, key
6671
key = OpenSSL::PKey.read(pem)
67-
assert_same_dh dh1024params, key
72+
assert_same_dh dh_params, key
6873

69-
assert_equal asn1.to_der, dh1024.to_der
70-
assert_equal pem, dh1024.export
74+
assert_equal asn1.to_der, dh.to_der
75+
assert_equal pem, dh.export
7176
end
7277

7378
def test_public_key
74-
dh = Fixtures.pkey("dh1024")
79+
dh = Fixtures.pkey(KEY_NAME)
7580
public_key = dh.public_key
7681
assert_no_key(public_key) #implies public_key.public? is false!
7782
assert_equal(dh.to_der, public_key.to_der)
@@ -80,7 +85,7 @@ def test_public_key
8085

8186
def test_generate_key
8287
# Deprecated in v3.0.0; incompatible with OpenSSL 3.0
83-
dh = Fixtures.pkey("dh1024").public_key # creates a copy with params only
88+
dh = Fixtures.pkey(KEY_NAME).public_key # creates a copy with params only
8489
assert_no_key(dh)
8590
dh.generate_key!
8691
assert_key(dh)
@@ -91,7 +96,7 @@ def test_generate_key
9196
end if !openssl?(3, 0, 0)
9297

9398
def test_params_ok?
94-
dh0 = Fixtures.pkey("dh1024")
99+
dh0 = Fixtures.pkey(KEY_NAME)
95100

96101
dh1 = OpenSSL::PKey::DH.new(OpenSSL::ASN1::Sequence([
97102
OpenSSL::ASN1::Integer(dh0.p),
@@ -108,7 +113,7 @@ def test_params_ok?
108113

109114
def test_dup
110115
# Parameters only
111-
dh1 = Fixtures.pkey("dh1024")
116+
dh1 = Fixtures.pkey(KEY_NAME)
112117
dh2 = dh1.dup
113118
assert_equal dh1.to_der, dh2.to_der
114119
assert_not_equal nil, dh1.p
@@ -125,7 +130,7 @@ def test_dup
125130
end
126131

127132
# With a key pair
128-
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh1024"))
133+
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey(KEY_NAME))
129134
dh4 = dh3.dup
130135
assert_equal dh3.to_der, dh4.to_der
131136
assert_equal dh1.to_der, dh4.to_der # encodes parameters only
@@ -136,7 +141,7 @@ def test_dup
136141
end
137142

138143
def test_marshal
139-
dh = Fixtures.pkey("dh1024")
144+
dh = Fixtures.pkey(KEY_NAME)
140145
deserialized = Marshal.load(Marshal.dump(dh))
141146

142147
assert_equal dh.to_der, deserialized.to_der

test/openssl/utils.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ module Fixtures
1616
module_function
1717

1818
def pkey(name)
19-
OpenSSL::PKey.read(read_file("pkey", name))
19+
OpenSSL::PKey.read(pkey_str(name))
20+
end
21+
22+
def pkey_str(name)
23+
read_file("pkey", name)
2024
end
2125

2226
def read_file(category, name)
@@ -151,7 +155,11 @@ def teardown
151155
def omit_on_fips
152156
return unless OpenSSL.fips_mode
153157

154-
omit 'An encryption used in the test is not FIPS-approved'
158+
omit <<~MESSAGE
159+
Only for OpenSSL non-FIPS with the following possible reasons:
160+
* A testing logic is non-FIPS specific.
161+
* An encryption used in the test is not FIPS-approved.
162+
MESSAGE
155163
end
156164

157165
def omit_on_non_fips

0 commit comments

Comments
 (0)