Class: Thieve::KeyInfo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, type, key) ⇒ KeyInfo

Returns a new instance of KeyInfo.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

Instance Attribute Details

#extObject (readonly)

File extension to use when exporting



7
8
9
# File 'lib/thieve/key_info.rb', line 7

def ext
  @ext
end

#fileObject (readonly)

File that the key was found in



10
11
12
# File 'lib/thieve/key_info.rb', line 10

def file
  @file
end

#fingerprintObject (readonly)

The fingerprint



13
14
15
# File 'lib/thieve/key_info.rb', line 13

def fingerprint
  @fingerprint
end

#keyObject (readonly)

The actual key



16
17
18
# File 'lib/thieve/key_info.rb', line 16

def key
  @key
end

#matchObject

The matching cert/key



19
20
21
# File 'lib/thieve/key_info.rb', line 19

def match
  @match
end

#opensslObject (readonly)

The OpenSSL object



22
23
24
# File 'lib/thieve/key_info.rb', line 22

def openssl
  @openssl
end

#typeObject (readonly)

Type of key/cert



25
26
27
# File 'lib/thieve/key_info.rb', line 25

def type
  @type
end

Instance Method Details

#export(directory) ⇒ Object



27
28
29
30
31
32
# 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_jsonObject



108
109
110
111
112
113
114
115
116
# File 'lib/thieve/key_info.rb', line 108

def to_json
    return {
        "file" => file,
        "fingerprint" => fingerprint,
        "key" => key,
        "match" => match || "",
        "type" => type
    }
end

#to_sObject



118
119
120
121
122
123
124
# 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