Skip to content

Commit 857943a

Browse files
committed
Fix test_pkey_dh.rb in FIPS.
We use dh2048_ffdhe2048.pem file (DH 2048 bits) instead of dh1024.pem file in both non-FIPS and FIPS cases. Because the following command fails to generate the pem file with 1024 bits. And the OpenSSL FIPS 140-2 security policy document explains the DH public keys are allowed from 2048 bits.[1] ``` $ 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.pem 1024 Generating DH parameters, 1024 bit long safe prime dhparam: Generating DH key parameters failed ``` The dh2048_ffdhe2048.pem file was created by the following command with the OpenSSL FIPS configuration file. The logic to generate the DH pem file is different between non-FIPS and FIPS cases. In FIPS, it seems that the command always returns the text defined as ffdhe2048 in RFC 7919 unlike non-FIPS.[2] As the generated pem file is a normal and valid PKCS#3-style group parameter, we use the file for the non-FIPS case too. ``` $ 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_ffdhe2048.pem 2048 ``` Note that the key names are intentionally hard-corded because we want to be intentional for modifying the file content. * [1] https://www.openssl.org/source/ - OpenSSL 3.0.8 FIPS 140-2 security policy document page 25, Table 10 – Public Keys - DH Public - DH (2048/3072/4096/6144/8192) public key agreement key * [2] RFC7919 - Appendix A.1: ffdhe2048 https://www.rfc-editor.org/rfc/rfc7919#appendix-A.1
1 parent fac5cac commit 857943a

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
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

test/openssl/fixtures/pkey/dh1024.pem

Lines changed: 0 additions & 5 deletions
This file was deleted.
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: 31 additions & 25 deletions
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,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("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
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("dh2048_ffdhe2048")
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("dh2048_ffdhe2048")
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,8 @@ 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+
# Creates a copy with params only
89+
dh = Fixtures.pkey("dh2048_ffdhe2048").public_key
8490
assert_no_key(dh)
8591
dh.generate_key!
8692
assert_key(dh)
@@ -91,7 +97,7 @@ def test_generate_key
9197
end if !openssl?(3, 0, 0)
9298

9399
def test_params_ok?
94-
dh0 = Fixtures.pkey("dh1024")
100+
dh0 = Fixtures.pkey("dh2048_ffdhe2048")
95101

96102
dh1 = OpenSSL::PKey::DH.new(OpenSSL::ASN1::Sequence([
97103
OpenSSL::ASN1::Integer(dh0.p),
@@ -108,7 +114,7 @@ def test_params_ok?
108114

109115
def test_dup
110116
# Parameters only
111-
dh1 = Fixtures.pkey("dh1024")
117+
dh1 = Fixtures.pkey("dh2048_ffdhe2048")
112118
dh2 = dh1.dup
113119
assert_equal dh1.to_der, dh2.to_der
114120
assert_not_equal nil, dh1.p
@@ -125,7 +131,7 @@ def test_dup
125131
end
126132

127133
# With a key pair
128-
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh1024"))
134+
dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh2048_ffdhe2048"))
129135
dh4 = dh3.dup
130136
assert_equal dh3.to_der, dh4.to_der
131137
assert_equal dh1.to_der, dh4.to_der # encodes parameters only
@@ -136,7 +142,7 @@ def test_dup
136142
end
137143

138144
def test_marshal
139-
dh = Fixtures.pkey("dh1024")
145+
dh = Fixtures.pkey("dh2048_ffdhe2048")
140146
deserialized = Marshal.load(Marshal.dump(dh))
141147

142148
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)