class SSLTool::PEMScanner

Constants

RX_PEM_BLOCK

Attributes

cert_strings[R]
garbage_strings[R]
key_strings[R]
pem_strings[R]

Public Class Methods

certs_from(s) click to toggle source
# File lib/ssltool/pem_scanner.rb, line 24
def certs_from(s)
  scan(s).certs
end
keys_from(s) click to toggle source
# File lib/ssltool/pem_scanner.rb, line 20
def keys_from(s)
  scan(s).keys
end
new(s) click to toggle source
# File lib/ssltool/pem_scanner.rb, line 29
def initialize(s)
  s                              = s.dup.force_encoding('BINARY').gsub(/\r\n?/, "\n").gsub(/\s+\n/, "\n")
  @pem_strings, @garbage_strings = s.split(RX_PEM_BLOCK).map(&:strip).reject(&:empty?).partition { |s| s =~ RX_PEM_BLOCK }
  @cert_strings                  = @pem_strings.select { |s| s =~ /-----BEGIN CERTIFICATE-----/ }
  @key_strings                   = @pem_strings.select { |s| s =~ /-----BEGIN (EC |RSA )?PRIVATE KEY-----/ }
  @garbage_strings              += @pem_strings - @cert_strings - @key_strings
end

Public Instance Methods

certificates() click to toggle source
# File lib/ssltool/pem_scanner.rb, line 37
def certificates
  @certificates ||= cert_strings.map do |s|
    begin
      Certificate.new(s)
    rescue OpenSSL::X509::CertificateError
      garbage_strings << cert_strings.delete(s)
      nil
    end
  end.compact
end
Also aliased as: certs
certs()
Alias for: certificates
keys() click to toggle source
# File lib/ssltool/pem_scanner.rb, line 49
def keys
  KeyHelper.keys(*key_strings)
end