class Thieve::KeyInfo

Attributes

ext[R]

File extension to use when exporting

file[R]

File that the key was found in

fingerprint[R]

The fingerprint

key[R]

The actual key

match[RW]

The matching cert/key

openssl[R]

The OpenSSL object

type[R]

Type of key/cert

Public Class Methods

new(file, type, key) click to toggle source
# File lib/thieve/key_info.rb, line 55
def initialize(file, type, key)
    @ext = type.gsub(/ +/, "_").downcase
    @file = file
    @key = key
    @match = nil
    @openssl = nil
    @type = type

    case @type
    when "CERTIFICATE"
        @openssl = OpenSSL::X509::Certificate.new(@key)
    when /^(NEW )?CERTIFICATE REQUEST$/
        @openssl = OpenSSL::X509::Request.new(@key)
    when "DH PARAMETERS", "DH PRIVATE KEY"
        @openssl = OpenSSL::PKey::DH.new(@key)
    when "DSA PRIVATE KEY"
        @openssl = OpenSSL::PKey::DSA.new(@key)
    when "EC PARAMETERS", "EC PRIVATE KEY"
        @openssl = OpenSSL::PKey::EC.new(@key)
    when /^PGP (PRIVATE|PUBLIC) KEY BLOCK$/
        command = "gpg --with-fingerprint << EOF\n#{@key}\nEOF"
        %x(#{command}).each_line do |line|
            line.match(/Key fingerprint = (.*)/) do |m|
                @fingerprint = m[1].gsub(" ", "").downcase
            end
        end
    #when "PGP SIGNATURE"
        # Not really sure what to do with this
        # TODO
       #@fingerprint = Digest::SHA256.hexdigest(@file.to_s + @key)
    when "PKCS5"
        @openssl = OpenSSL::PKCS5.new(@key)
    when "PKCS7"
        @openssl = OpenSSL::PKCS7.new(@key)
    when "PKCS12"
        @openssl = OpenSSL::PKCS12.new(@key)
    when "PRIVATE KEY", "PUBLIC KEY", "RSA PRIVATE KEY"
        if (!@key.match(/ENCRYPTED/))
            @openssl = OpenSSL::PKey::RSA.new(@key)
        end
    when "X509 CRL"
        @openssl = OpenSSL::X509::CRL.new(@key)
    else
        @fingerprint = Digest::SHA256.hexdigest(@file.to_s + @key)
    end

    if (@openssl)
        @fingerprint = OpenSSL::Digest::SHA1.new(
            @openssl.to_der
        ).to_s
    end
end

Public Instance Methods

export(directory) click to toggle source
# File lib/thieve/key_info.rb, line 27
def export(directory)
    FileUtils.mkdir_p(directory)
    File.open("#{directory}/#{@fingerprint}.#{@ext}", "w") do |f|
        f.write(@key)
    end
end
to_json() click to toggle source
# File lib/thieve/key_info.rb, line 108
def to_json
    return {
        "file" => file,
        "fingerprint" => fingerprint,
        "key" => key,
        "match" => match || "",
        "type" => type
    }
end
to_s() click to toggle source
# File lib/thieve/key_info.rb, line 118
def to_s
    ret = Array.new
    ret.push(hilight_file)
    ret.push(hilight_key)
    ret.push(hilight_match) if (@match)
    return ret.join("\n")
end

Private Instance Methods

hilight_file(file = @file) click to toggle source
# File lib/thieve/key_info.rb, line 34
def hilight_file(file = @file)
    return file if (!Thieve.hilight?)
    return file.to_s.light_blue
end
hilight_key(key = @key) click to toggle source
# File lib/thieve/key_info.rb, line 40
def hilight_key(key = @key)
    return key if (!Thieve.hilight?)
    return key.split("\n").map do |line|
        line.light_white
    end.join("\n")
end
hilight_match(match = @match) click to toggle source
# File lib/thieve/key_info.rb, line 48
def hilight_match(match = @match)
    return "" if (match.nil?)
    return "Matches #{match}" if (!Thieve.hilight?)
    return ["Matches".light_blue, match.light_green].join(" ")
end