Skip to content

Commit

Permalink
ssl: manually craft invalid SAN extensions in tests
Browse files Browse the repository at this point in the history
Starting with LibreSSL 3.5, OpenSSL::X509::ExtensionFactory refuses to
create SAN extensions that are not valid according to RFC 6125. While
this behavior makes sense, we need such invalid extensions to test our
own validation routine. Let's construct SAN extensions manually instead.
  • Loading branch information
rhenium committed Feb 25, 2025
1 parent d725783 commit b420d6d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,6 @@ def test_post_connection_check_wildcard_san
# buzz.example.net, respectively). ...
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
create_cert_with_san('DNS:baz*.example.com'), 'baz1.example.com'))

# LibreSSL 3.5.0+ doesn't support other wildcard certificates
# (it isn't required to, as RFC states MAY, not MUST)
return if libressl?

assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
create_cert_with_san('DNS:*baz.example.com'), 'foobaz.example.com'))
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
Expand Down Expand Up @@ -923,11 +918,17 @@ def test_post_connection_check_wildcard_cn
end

def create_cert_with_san(san)
ef = OpenSSL::X509::ExtensionFactory.new
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse("/DC=some/DC=site/CN=Some Site")
ext = ef.create_ext('subjectAltName', san)
cert.add_extension(ext)
v = OpenSSL::ASN1::Sequence(san.split(",").map { |item|
type, value = item.split(":", 2)
case type
when "DNS" then OpenSSL::ASN1::IA5String(value, 2, :IMPLICIT)
when "IP" then OpenSSL::ASN1::OctetString(IPAddr.new(value).hton, 7, :IMPLICIT)
else raise "unsupported"
end
})
cert.add_extension(OpenSSL::X509::Extension.new("subjectAltName", v))
cert
end

Expand Down

0 comments on commit b420d6d

Please sign in to comment.