Skip to content

Commit 468f8ce

Browse files
committed
pkey: add more tests for OpenSSL::PKey.read
Add tests covering edge cases in the current behavior to prevent accidental regressions. The next patches will update the OpenSSL 3.x path.
1 parent d377a34 commit 468f8ce

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/openssl/test_pkey.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ def test_s_generate_key
6060
assert_not_equal nil, pkey.private_key
6161
end
6262

63+
def test_s_read_pem_unknown_block
64+
# A PEM-encoded certificate and a PEM-encoded private key are combined.
65+
# Check that OSSL_STORE doesn't stop after the first PEM block.
66+
orig = Fixtures.pkey("rsa-1")
67+
subject = OpenSSL::X509::Name.new([["CN", "test"]])
68+
cert = issue_cert(subject, orig, 1, [], nil, nil)
69+
70+
input = cert.to_text + cert.to_pem + orig.to_text + orig.private_to_pem
71+
pkey = OpenSSL::PKey.read(input)
72+
assert_equal(orig.private_to_der, pkey.private_to_der)
73+
end
74+
75+
def test_s_read_der_then_pem
76+
# If the input is valid as both DER and PEM (which allows garbage data
77+
# before and after the block), it is read as DER
78+
#
79+
# TODO: Garbage data after DER should not be allowed, but it is currently
80+
# ignored
81+
orig1 = Fixtures.pkey("rsa-1")
82+
orig2 = Fixtures.pkey("rsa-2")
83+
pkey = OpenSSL::PKey.read(orig1.public_to_der + orig2.private_to_pem)
84+
assert_equal(orig1.public_to_der, pkey.public_to_der)
85+
assert_not_predicate(pkey, :private?)
86+
end
87+
6388
def test_hmac_sign_verify
6489
pkey = OpenSSL::PKey.generate_key("HMAC", { "key" => "abcd" })
6590

0 commit comments

Comments
 (0)