class CertificateParser
Public Class Methods
new(attrs = {})
click to toggle source
# File lib/certutil/certificate_parser.rb, line 59 def initialize(attrs = {}) @input = attrs[:string] @name = attrs[:name] @path = Dir.pwd info "Certificate loaded#{' from ' + attrs[:source].to_s if attrs[:source]}: #{@name}." end
new_from_file(path)
click to toggle source
# File lib/certutil/certificate_parser.rb, line 12 def self.new_from_file(path) # should return nil if it's not a valid cert debug "Parsing #{path}..." begin string = File.read(path) if string path_array = path.split(File::SEPARATOR) debug "Creating..." self.new(string: string, name: path_array.last.gsub(/(\.crt)|(.pem)/, ""), source: :file) end rescue nil end end
new_from_gist(gist_link)
click to toggle source
# File lib/certutil/certificate_parser.rb, line 35 def self.new_from_gist(gist_link) # not yet end
new_from_hostname(hostname)
click to toggle source
# File lib/certutil/certificate_parser.rb, line 28 def self.new_from_hostname(hostname) debug "Fetching certificate from #{hostname}..." debug "Creating..." string = `openssl s_client -connect #{hostname}:443 -showcerts </dev/null` self.new(string: string, name: hostname, source: :hostname) unless string == "" end
new_from_input(input)
click to toggle source
# File lib/certutil/certificate_parser.rb, line 8 def self.new_from_input(input) new_from_file(File.expand_path(input)) || new_from_hostname(input) end
new_from_string(string, opts = {})
click to toggle source
# File lib/certutil/certificate_parser.rb, line 39 def self.new_from_string(string, opts = {}) self.new(string: string, name: (opts[:name] || "temp")) end
Public Instance Methods
certs()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 43 def certs @certs ||= split_input end
decoded()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 51 def decoded debug "lazy loading decoded..." @decoded ||= decode debug "lazy loading done." @decoded end
flipit!()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 47 def flipit! @certs = certs.reverse end
write_crt_file!()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 75 def write_crt_file! info "Writing .crt file..." joined = certs.join("\n") + "\n" File.open(File.join(@path, "#{@name}.crt"), "w") { |f| f.write(joined) } debug "--> Wrote #{@path} -- #{@name}.crt." end
write_crt_files!()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 66 def write_crt_files! info "Writing separate .crt files..." certs.each_with_index do |cert, i| debug "right now, wd is #{Dir.pwd}" File.open(File.join(@path, "#{@name}-#{i}.crt"), "w") { |f| f.write(cert) } debug "--> Wrote #{@path} -- #{@name}-#{i}.crt." end end
write_txt_file!()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 90 def write_txt_file! info "Writing .txt file..." joined = decoded.join("\n-----\n") + "\n" File.open(File.join(@path, "#{@name}-decoded.txt"), "w") { |f| f.write(joined) } debug "--> Wrote #{@path}/#{@name}-decoded.txt." end
write_txt_files!()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 82 def write_txt_files! info "Writing separate .txt files..." decoded.each_with_index do |cert, i| File.open(File.join(@path, "#{@name}-#{i}-decoded.txt"), "w") { |f| f.write(cert) } debug "--> Wrote #{@path}/#{@name}-#{i}-decoded.txt." end end
Private Instance Methods
decode()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 113 def decode debug "Decoding..." output = self.certs.map do |cert| Open3.popen3("openssl x509 -text -noout") do |stdin, stdout, stderr| stdin.write(cert) stdin.close_write out = stdout.read debug "stdout: #{out}" out end end debug "decode output is #{output}." output end
split_input()
click to toggle source
# File lib/certutil/certificate_parser.rb, line 99 def split_input debug "splitting..." matches = @input.scan(/-+BEGIN CERTIFICATE-----.*?-+END CERTIFICATE-+/m) matches = matches.map do |cert| # cert.gsub!(/-----BEGIN CERTIFICATE-----\n?/, "") # cert.gsub!(/-----END CERTIFICATE-----\n?/, "") # cert.chomp cert end debug "Found #{matches.count} matches." matches end