Class: SSLTool::PEMScanner

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

Constant Summary collapse

RX_PEM_BLOCK =
/(-----BEGIN.*?-----\n
  (?:[A-Za-z0-9\+\/]+\n)*
  [A-Za-z0-9\+\/]*
  (?:=|==|=\n=)?
  \n
  -----END.*?-----
)/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ PEMScanner

Returns a new instance of PEMScanner.



29
30
31
32
33
34
35
# 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

Instance Attribute Details

#cert_stringsObject (readonly)

Returns the value of attribute cert_strings.



7
8
9
# File 'lib/ssltool/pem_scanner.rb', line 7

def cert_strings
  @cert_strings
end

#garbage_stringsObject (readonly)

Returns the value of attribute garbage_strings.



7
8
9
# File 'lib/ssltool/pem_scanner.rb', line 7

def garbage_strings
  @garbage_strings
end

#key_stringsObject (readonly)

Returns the value of attribute key_strings.



7
8
9
# File 'lib/ssltool/pem_scanner.rb', line 7

def key_strings
  @key_strings
end

#pem_stringsObject (readonly)

Returns the value of attribute pem_strings.



7
8
9
# File 'lib/ssltool/pem_scanner.rb', line 7

def pem_strings
  @pem_strings
end

Class Method Details

.certs_from(s) ⇒ Object



24
25
26
# File 'lib/ssltool/pem_scanner.rb', line 24

def certs_from(s)
  scan(s).certs
end

.keys_from(s) ⇒ Object



20
21
22
# File 'lib/ssltool/pem_scanner.rb', line 20

def keys_from(s)
  scan(s).keys
end

Instance Method Details

#certificatesObject Also known as: certs



37
38
39
40
41
42
43
44
45
46
# 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

#keysObject



49
50
51
# File 'lib/ssltool/pem_scanner.rb', line 49

def keys
  KeyHelper.keys(*key_strings)
end