Module: OpenSSLExtensions::X509::Certificate

Defined in:
lib/openssl-extensions/x509/certificate.rb

Overview

Extends OpenSSL::X509::Certificate with shortcut methods.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Equality is tested by comparing the generated PEM signatures.



13
14
15
# File 'lib/openssl-extensions/x509/certificate.rb', line 13

def ==(other)
  to_pem == other.to_pem
end

#allows_certificate_signing?Boolean

Returns true if this certificate is authorized to sign for other certificates (useful for determining CA roots and intermediary certificates).

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/openssl-extensions/x509/certificate.rb', line 22

def allows_certificate_signing?
  usage = read_extension_by_oid('keyUsage')
  usage.nil? || !!(usage.match(%r{\bCertificate Sign\b}))
end

#authority_key_identifierObject



27
28
29
# File 'lib/openssl-extensions/x509/certificate.rb', line 27

def authority_key_identifier
  OpenSSLExtensions::X509::AuthorityKeyIdentifier.new(read_extension_by_oid('authorityKeyIdentifier'))
end

#hashObject

Override the default Object#hash to identify uniqueness of the Certificate. This uses a hash of the certificate PEM.



35
36
37
# File 'lib/openssl-extensions/x509/certificate.rb', line 35

def hash
  to_pem.hash
end

#issuing_certificate?(issuer) ⇒ Boolean

Returns true if the certificate given is the issuer certificate for this certificate.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/openssl-extensions/x509/certificate.rb', line 42

def issuing_certificate?(issuer)
  (self.authority_key_identifier.key_id &&
    issuer.subject_key_identifier &&
    self.authority_key_identifier.key_id == issuer.subject_key_identifier) ||
    (!self.authority_key_identifier.key_id &&
     self.issuer.common_name == issuer.subject.common_name &&
     self.issuer.country == issuer.subject.country &&
     self.issuer.organization == issuer.subject.organization)
end

#root?Boolean

Returns true if this certificate is a root certificate (it is its own issuer).

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/openssl-extensions/x509/certificate.rb', line 61

def root?
  issuer.to_s == subject.to_s &&
    (subject_key_identifier && authority_key_identifier.key_id ? subject_key_identifier == authority_key_identifier.key_id : true)
end

#strengthObject

Returns the bit strength of the public certificate.



69
70
71
# File 'lib/openssl-extensions/x509/certificate.rb', line 69

def strength
  public_key.n.num_bits
end

#subject_alternative_namesObject Also known as: sans

Returns a collection of subject alternative names on the certificate. If no alternative names were provided, then this returns an empty set.



77
78
79
80
# File 'lib/openssl-extensions/x509/certificate.rb', line 77

def subject_alternative_names
  names_string = read_extension_by_oid('subjectAltName')
  names_string ? names_string.scan(%r{DNS:([^,]+)}).flatten : []
end

#subject_key_identifierObject



83
84
85
# File 'lib/openssl-extensions/x509/certificate.rb', line 83

def subject_key_identifier
  read_extension_by_oid('subjectKeyIdentifier')
end