Class: Keystores::JavaKeystore

Inherits:
Keystore
  • Object
show all
Defined in:
lib/keystores/java_key_store.rb

Overview

An implementation of a Java Key Store (JKS) Format

Direct Known Subclasses

OpenSSL::JKS

Defined Under Namespace

Classes: KeyEntry, TrustedCertificateEntry

Constant Summary collapse

TYPE =
'JKS'
MAGIC =

Defined by JavaKeyStore.java

0xfeedfeed
VERSION_1 =
0x01
VERSION_2 =
0x02
KEY_ENTRY_TAG =
1
TRUSTED_CERTIFICATE_ENTRY_TAG =
2

Instance Method Summary collapse

Methods inherited from Keystore

get_instance, register_algorithm

Constructor Details

#initializeJavaKeystore



21
22
23
24
# File 'lib/keystores/java_key_store.rb', line 21

def initialize
  @entries = {}
  @entries_mutex = Mutex.new
end

Instance Method Details

#aliasesObject



26
27
28
# File 'lib/keystores/java_key_store.rb', line 26

def aliases
  @entries.keys
end

#contains_alias(aliaz) ⇒ Object



30
31
32
# File 'lib/keystores/java_key_store.rb', line 30

def contains_alias(aliaz)
  @entries.has_key?(aliaz)
end

#delete_entry(aliaz) ⇒ Object



34
35
36
# File 'lib/keystores/java_key_store.rb', line 34

def delete_entry(aliaz)
  @entries_mutex.synchronize { @entries.delete(aliaz) }
end

#get_certificate(aliaz) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/keystores/java_key_store.rb', line 38

def get_certificate(aliaz)
  entry = @entries[aliaz]
  unless entry.nil?
    if entry.is_a? TrustedCertificateEntry
      entry.certificate
    elsif entry.is_a? KeyEntry
      entry.certificate_chain[0]
    else
      nil
    end
  end
end

#get_certificate_alias(certificate) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/keystores/java_key_store.rb', line 51

def get_certificate_alias(certificate)
  @entries.each do |aliaz, entry|
    if entry.is_a? TrustedCertificateEntry
      # We have to DER encode both of the certificates because OpenSSL::X509::Certificate doesn't implement equal?
      return aliaz if certificate.to_der == entry.certificate.to_der
    elsif entry.is_a? KeyEntry
      # We have to DER encode both of the certificates because OpenSSL::X509::Certificate doesn't implement equal?
      return aliaz if certificate.to_der == entry.certificate_chain[0].to_der
    end
  end
  nil
end

#get_certificate_chain(aliaz) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/keystores/java_key_store.rb', line 64

def get_certificate_chain(aliaz)
  entry = @entries[aliaz]
  if !entry.nil? && entry.is_a?(KeyEntry)
    entry.certificate_chain
  else
    nil
  end
end

#get_key(aliaz, password) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/keystores/java_key_store.rb', line 73

def get_key(aliaz, password)
  entry = @entries[aliaz]

  # This somewhat odd control flow mirrors the Java code for ease of porting
  # TODO clean this up
  if entry.nil? || !entry.is_a?(KeyEntry)
    return nil
  end

  if password.nil?
    raise IOError.new('Password must not be nil')
  end

  encrypted_private_key = entry.encrypted_private_key
  encrypted_private_key_info = Keystores::Jks::EncryptedPrivateKeyInfo.new(:encoded => encrypted_private_key)
  Keystores::Jks::KeyProtector.new(password).recover(encrypted_private_key_info)
end

#get_typeObject



91
92
93
# File 'lib/keystores/java_key_store.rb', line 91

def get_type
  TYPE
end

#is_certificate_entry(aliaz) ⇒ Object



95
96
97
# File 'lib/keystores/java_key_store.rb', line 95

def is_certificate_entry(aliaz)
  !@entries[aliaz].nil? && @entries[aliaz].is_a?(TrustedCertificateEntry)
end

#is_key_entry(aliaz) ⇒ Object



99
100
101
# File 'lib/keystores/java_key_store.rb', line 99

def is_key_entry(aliaz)
  !@entries[aliaz].nil? && @entries[aliaz].is_a?(KeyEntry)
end

#load(key_store_file, password) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/keystores/java_key_store.rb', line 103

def load(key_store_file, password)
  @entries_mutex.synchronize do
    key_store_bytes = key_store_file.respond_to?(:read) ? key_store_file.read : IO.binread(key_store_file)
    # We pass this Message Digest around and add all of the bytes we read to it so we can verify integrity
    md = get_pre_keyed_hash(password)

    magic = read_int!(key_store_bytes, md)
    version = read_int!(key_store_bytes, md)

    if magic != MAGIC || (version != VERSION_1 && version != VERSION_2)
      raise IOError.new('Invalid keystore format')
    end

    count = read_int!(key_store_bytes, md)

    count.times do
      tag = read_int!(key_store_bytes, md)

      if tag == 
        key_entry = KeyEntry.new
        aliaz = read_utf!(key_store_bytes, md)
        time = read_long!(key_store_bytes, md)

        key_entry.creation_date = time

        private_key_length = read_int!(key_store_bytes, md)
        encrypted_private_key = key_store_bytes.slice!(0..(private_key_length - 1))
        md << encrypted_private_key

        key_entry.encrypted_private_key = encrypted_private_key

        number_of_certs = read_int!(key_store_bytes, md)

        certificate_chain = []

        number_of_certs.times do
          certificate_chain << read_certificate(key_store_bytes, version, md)
        end

        key_entry.certificate_chain = certificate_chain
        @entries[aliaz] = key_entry
      elsif tag == 
        trusted_cert_entry = TrustedCertificateEntry.new
        aliaz = read_utf!(key_store_bytes, md)
        time = read_long!(key_store_bytes, md)

        trusted_cert_entry.creation_date = time
        certificate = read_certificate(key_store_bytes, version, md)
        trusted_cert_entry.certificate = certificate
        @entries[aliaz] = trusted_cert_entry
      else
        raise IOError.new('Unrecognized keystore entry')
      end
    end

    unless password.nil?
      verify_key_store_integrity(key_store_bytes, md)
    end
  end
end

#set_certificate_entry(aliaz, certificate) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/keystores/java_key_store.rb', line 164

def set_certificate_entry(aliaz, certificate)
  @entries_mutex.synchronize do
    entry = @entries[aliaz]
    if !entry.nil? && entry.is_a?(KeyEntry)
      raise ArgumentError.new('Cannot overwrite own certificate')
    end

    entry = TrustedCertificateEntry.new
    entry.certificate = certificate
    # Java uses new Date().getTime() which returns milliseconds since epoch, so we do the same here with %Q
    entry.creation_date = DateTime.now.strftime('%Q').to_i

    @entries[aliaz] = entry
  end
end

#set_key_entry(aliaz, key, certificate_chain, password) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/keystores/java_key_store.rb', line 180

def set_key_entry(aliaz, key, certificate_chain, password)
  @entries_mutex.synchronize do
    entry = @entries[aliaz]
    if !entry.nil? && entry.is_a?(TrustedCertificateEntry)
      raise ArgumentError.new('Cannot overwrite own key')
    end

    entry = KeyEntry.new
    # Java uses new Date().getTime() which returns milliseconds since epoch, so we do the same here with %Q
    entry.creation_date = DateTime.now.strftime('%Q').to_i
    entry.encrypted_private_key = Keystores::Jks::KeyProtector.new(password).protect(key)
    entry.certificate_chain = [certificate_chain].flatten

    @entries[aliaz] = entry
  end
end

#sizeObject



197
198
199
# File 'lib/keystores/java_key_store.rb', line 197

def size
  @entries.size
end

#store(key_store_file, password) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/keystores/java_key_store.rb', line 201

def store(key_store_file, password)
  @entries_mutex.synchronize do
    # password is mandatory when storing
    if password.nil?
      raise ArgumentError.new("password can't be null")
    end

    md = get_pre_keyed_hash(password)

    io = key_store_file.respond_to?(:write) ? key_store_file : File.open(key_store_file, 'wb')

    write_int(io, MAGIC, md)
    # Always write the latest version
    write_int(io, VERSION_2, md)
    write_int(io, @entries.size, md)

    @entries.each do |aliaz, entry|
      if entry.is_a? KeyEntry
        write_int(io, , md)
        write_utf(io, aliaz, md)
        write_long(io, entry.creation_date, md)
        write_int(io, entry.encrypted_private_key.length, md)
        write(io, entry.encrypted_private_key, md)

        certificate_chain = entry.certificate_chain
        chain_length = certificate_chain.nil? ? 0 : certificate_chain.length

        write_int(io, chain_length, md)

        unless certificate_chain.nil?
          certificate_chain.each { |certificate| write_certificate(io, certificate, md) }
        end
      elsif entry.is_a? TrustedCertificateEntry
        write_int(io, , md)
        write_utf(io, aliaz, md)
        write_long(io, entry.creation_date, md)
        write_certificate(io, entry.certificate, md)
      else
        raise IOError.new('Unrecognized keystore entry')
      end
    end
    # Write the keyed hash which is used to detect tampering with
    # the keystore (such as deleting or modifying key or
    # certificate entries).
    io.write(md.digest)
    io.flush
  end
end