Class: MkChain

Inherits:
Object
  • Object
show all
Defined in:
lib/mkchain.rb

Defined Under Namespace

Classes: NoChainFoundException

Class Method Summary collapse

Class Method Details

.chain(cert_str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mkchain.rb', line 7

def self.chain(cert_str)
  chain = []
  cert = OpenSSL::X509::Certificate.new(cert_str)

  loop do
    url = cert.extensions.select { |ext| ext.oid == 'authorityInfoAccess' }
      .first.value.match(%r{^CA Issuers - URI:(https?://.+)$})[1] rescue break

    cert = OpenSSL::X509::Certificate.new(open(url).read) rescue break
    chain << cert.to_pem
  end

  raise NoChainFoundException, 'No intermediate chain found' if chain.empty?

  # the last cert will be the root cert, which doesn't belong in the chain
  chain[0..-1].join
end