Skip to content

Commit

Permalink
Add helper to retrieve CRL URIs from a certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Oct 20, 2019
1 parent e7b4b3f commit e87999e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/openssl/x509.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ def authority_key_identifier
key_id.nil? ? nil : key_id.value
end
end

module CrlDistributionPoints
include Helpers

# Get the distributionPoint fullName URI from the certificate's CRL
# distribution points extension, as described in RFC5280 Section
# 4.2.1.13
#
# Returns an array of strings or nil or raises ASN1::ASN1Error.
def crl_uris
ext = find_extension("crlDistributionPoints")
return nil if ext.nil?

cdp_asn1 = ASN1.decode(ext.value_der)
if cdp_asn1.tag_class != :UNIVERSAL || cdp_asn1.tag != ASN1::SEQUENCE
raise ASN1::ASN1Error "invalid extension"
end

crl_uris = cdp_asn1.map do |crl_distribution_point|
distribution_point = crl_distribution_point.value.find do |v|
v.tag_class == :CONTEXT_SPECIFIC && v.tag == 0
end
full_name = distribution_point&.value&.find do |v|
v.tag_class == :CONTEXT_SPECIFIC && v.tag == 0
end
full_name&.value&.find do |v|
v.tag_class == :CONTEXT_SPECIFIC && v.tag == 6 # uniformResourceIdentifier
end
end

crl_uris.map(&:value)
end
end
end

class Name
Expand Down Expand Up @@ -234,6 +267,7 @@ def cleanup
class Certificate
include Extension::SubjectKeyIdentifier
include Extension::AuthorityKeyIdentifier
include Extension::CrlDistributionPoints

def pretty_print(q)
q.object_group(self) {
Expand Down
15 changes: 15 additions & 0 deletions test/test_x509cert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ def test_extension
assert_equal(ee1_exts[i].last, ext.critical?)
}

ef = OpenSSL::X509::ExtensionFactory.new
ef.config = OpenSSL::Config.parse(<<~_cnf_)
[crlDistPts]
URI.1 = http://www.example.com/crl
URI.2 = ldap://ldap.example.com/cn=ca?certificateRevocationList;binary
_cnf_
cdp_cert = generate_cert(@ee1, @rsa1024, 3, ca_cert)
ef.subject_certificate = cdp_cert
cdp_cert.add_extension(ef.create_extension("crlDistributionPoints", "@crlDistPts"))
cdp_cert.sign(@rsa2048, "sha256")
assert_equal(
["http://www.example.com/crl", "ldap://ldap.example.com/cn=ca?certificateRevocationList;binary"],
cdp_cert.crl_uris
)

no_exts_cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
assert_equal nil, no_exts_cert.authority_key_identifier
assert_equal nil, no_exts_cert.subject_key_identifier
Expand Down

0 comments on commit e87999e

Please sign in to comment.