class Ronin::Support::Crypto::CertChain

Represents a X509 or TLS certificate chain.

@api public

@since 1.0.0

Attributes

certs[R]

The certificates in the certificate chain.

@return [Array<Cert>]

Public Class Methods

load(string) click to toggle source

Alias for {parse}.

@param [String] string

The string to parse.

@return [CertChain]

The parsed certificate chain.

@see parse

# File lib/ronin/support/crypto/cert_chain.rb, line 87
def self.load(string)
  parse(string)
end
load_file(path) click to toggle source

Reads and parses the certificate chain from a file.

@param [String] path

The path to the file to parse.

@return [CertChain]

The parsed certificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 100
def self.load_file(path)
  parse(File.read(path))
end
new(certs) click to toggle source

The certificates in the certificate chain.

@param [Array<Cert>] certs

The certificates that make up the certificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 47
def initialize(certs)
  @certs = certs
end
parse(string) click to toggle source

Parses a certificate chain.

@param [String] string

The string to parse.

@return [CertChain]

The parsed certificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 60
def self.parse(string)
  cert_buffer = String.new
  certs       = []

  string.each_line do |line|
    cert_buffer << line

    if line.chomp == '-----END CERTIFICATE-----'
      certs << Cert.parse(cert_buffer)
      cert_buffer.clear
    end
  end

  return new(certs)
end

Public Instance Methods

[](index_or_range,length=nil) click to toggle source

Accesses one or more certificates at the index or range/length.

@param [Integer, Range<Integer,Integer>] index_or_range

The index or range of indices.

@param [Integer, nil] length

Optional length.

@return [Cert, Array<Cert>, nil]

The certificate(s) at the index or range of indices.
# File lib/ronin/support/crypto/cert_chain.rb, line 133
def [](index_or_range,length=nil)
  @certs[index_or_range,*length]
end
each(&block) click to toggle source

Enumerates over the certificates in the certificate chain.

@yield [cert]

If a block is given, it will be passed each certificate in the
certificate chain.

@yieldparam [Cert] cert

A parsed certificate object in the certificate chain.

@return [Enumerator]

If no block is given an Enumerator object will be returned.
# File lib/ronin/support/crypto/cert_chain.rb, line 117
def each(&block)
  @certs.each(&block)
end
intermediates() click to toggle source

The intermediary certificates.

@return [Array<Cert>]

The certificates between the {#root} and {#leaf} certificates.
# File lib/ronin/support/crypto/cert_chain.rb, line 167
def intermediates
  @certs[1..-2]
end
issuer() click to toggle source

The issuer certificate.

@return [Cert]

The second-to-last certificate in the certificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 153
def issuer
  if @certs.length == 1
    @certs[0]
  else
    @certs[1]
  end
end
leaf() click to toggle source

The leaf certificate.

@return [Cert]

The last certificate in the certiificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 143
def leaf
  @certs.first
end
length() click to toggle source

The number of certificates in the certificate chain.

@return [Integer]

# File lib/ronin/support/crypto/cert_chain.rb, line 186
def length
  @certs.length
end
root() click to toggle source

The root certificate.

@return [Cert]

The first certificate in the certificate chain.
# File lib/ronin/support/crypto/cert_chain.rb, line 177
def root
  @certs.last
end
to_pem() click to toggle source

Converts the certificate chain to a PEM encoded certificate chain.

@return [String]

# File lib/ronin/support/crypto/cert_chain.rb, line 195
def to_pem
  @certs.map(&:to_pem).join
end
Also aliased as: to_s
to_s()
Alias for: to_pem