Class: Ronin::Support::Crypto::CertChain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/crypto/cert_chain.rb

Overview

Represents a X509 or TLS certificate chain.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(certs) ⇒ CertChain

The certificates in the certificate chain.

Parameters:

  • certs (Array<Cert>)

    The certificates that make up the certificate chain.

Since:

  • 1.0.0



47
48
49
# File 'lib/ronin/support/crypto/cert_chain.rb', line 47

def initialize(certs)
  @certs = certs
end

Instance Attribute Details

#certsArray<Cert> (readonly)

The certificates in the certificate chain.

Returns:

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/crypto/cert_chain.rb', line 39

def certs
  @certs
end

Class Method Details

.load(string) ⇒ CertChain

Alias for parse.

Parameters:

  • string (String)

    The string to parse.

Returns:

  • (CertChain)

    The parsed certificate chain.

See Also:

Since:

  • 1.0.0



87
88
89
# File 'lib/ronin/support/crypto/cert_chain.rb', line 87

def self.load(string)
  parse(string)
end

.load_file(path) ⇒ CertChain

Reads and parses the certificate chain from a file.

Parameters:

  • path (String)

    The path to the file to parse.

Returns:

  • (CertChain)

    The parsed certificate chain.

Since:

  • 1.0.0



100
101
102
# File 'lib/ronin/support/crypto/cert_chain.rb', line 100

def self.load_file(path)
  parse(File.read(path))
end

.parse(string) ⇒ CertChain

Parses a certificate chain.

Parameters:

  • string (String)

    The string to parse.

Returns:

  • (CertChain)

    The parsed certificate chain.

Since:

  • 1.0.0



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

Instance Method Details

#[](index_or_range, length = nil) ⇒ Cert, ...

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

Parameters:

  • index_or_range (Integer, Range<Integer,Integer>)

    The index or range of indices.

  • length (Integer, nil) (defaults to: nil)

    Optional length.

Returns:

  • (Cert, Array<Cert>, nil)

    The certificate(s) at the index or range of indices.

Since:

  • 1.0.0



133
134
135
# File 'lib/ronin/support/crypto/cert_chain.rb', line 133

def [](index_or_range,length=nil)
  @certs[index_or_range,*length]
end

#each {|cert| ... } ⇒ Enumerator

Enumerates over the certificates in the certificate chain.

Yields:

  • (cert)

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

Yield Parameters:

  • cert (Cert)

    A parsed certificate object in the certificate chain.

Returns:

  • (Enumerator)

    If no block is given an Enumerator object will be returned.

Since:

  • 1.0.0



117
118
119
# File 'lib/ronin/support/crypto/cert_chain.rb', line 117

def each(&block)
  @certs.each(&block)
end

#intermediatesArray<Cert>

The intermediary certificates.

Returns:

Since:

  • 1.0.0



167
168
169
# File 'lib/ronin/support/crypto/cert_chain.rb', line 167

def intermediates
  @certs[1..-2]
end

#issuerCert

The issuer certificate.

Returns:

  • (Cert)

    The second-to-last certificate in the certificate chain.

Since:

  • 1.0.0



153
154
155
156
157
158
159
# File 'lib/ronin/support/crypto/cert_chain.rb', line 153

def issuer
  if @certs.length == 1
    @certs[0]
  else
    @certs[1]
  end
end

#leafCert

The leaf certificate.

Returns:

  • (Cert)

    The last certificate in the certiificate chain.

Since:

  • 1.0.0



143
144
145
# File 'lib/ronin/support/crypto/cert_chain.rb', line 143

def leaf
  @certs.first
end

#lengthInteger

The number of certificates in the certificate chain.

Returns:

Since:

  • 1.0.0



186
187
188
# File 'lib/ronin/support/crypto/cert_chain.rb', line 186

def length
  @certs.length
end

#rootCert

The root certificate.

Returns:

  • (Cert)

    The first certificate in the certificate chain.

Since:

  • 1.0.0



177
178
179
# File 'lib/ronin/support/crypto/cert_chain.rb', line 177

def root
  @certs.last
end

#to_pemString Also known as: to_s

Converts the certificate chain to a PEM encoded certificate chain.

Returns:

Since:

  • 1.0.0



195
196
197
# File 'lib/ronin/support/crypto/cert_chain.rb', line 195

def to_pem
  @certs.map(&:to_pem).join
end