Skip to content

Commit

Permalink
Fixing rylarson#2
Browse files Browse the repository at this point in the history
Making the certificate chain an array when setting a key so that storing
works.
Also adding an example of how to set a key to readme
  • Loading branch information
Daniel Leyden committed Jun 15, 2017
1 parent 45549ed commit cca83c0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ certificate_chain = keystore.get_certificate_chain('my_key')
This gem supports writing trusted certificate entries and private key entries. It currently supports
writing DSA, RSA, and EC private key entries.

Example usage:

```
require 'keystores'
keystore = OpenSSL::JKS.new
key = OpenSSL::PKey::RSA.new(File.read('my_key.pem'))
cert_chain = OpenSSL::X509::Certificate.new(File.read('my_cert.pem'))
private_key_password = 'key_password'
keystore.set_key_entry('my-key', key, cert_chain, private_key_password)
key_store_password = 'keystores'
keystore.store('my_keystore.jks', key_store_password)
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rylarson/keystores.
Expand Down
6 changes: 3 additions & 3 deletions lib/keystores/java_key_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def set_certificate_entry(aliaz, certificate)
end
end

def set_key_entry(aliaz, key, certificate_chain, password=nil)
def set_key_entry(aliaz, key, certificate_chain, password)
@entries_mutex.synchronize do
entry = @entries[aliaz]
if !entry.nil? && entry.is_a?(TrustedCertificateEntry)
Expand All @@ -188,7 +188,7 @@ def set_key_entry(aliaz, key, certificate_chain, password=nil)
# 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
entry.certificate_chain = [certificate_chain].flatten

@entries[aliaz] = entry
end
Expand Down Expand Up @@ -365,4 +365,4 @@ class TrustedCertificateEntry
attr_accessor :creation_date, :certificate
end
end
end
end
14 changes: 14 additions & 0 deletions spec/java_key_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@

expect { keystore.get_key('test_rsa_private_key_entry', nil) }.to raise_error(IOError)
expect(keystore.get_key('test_rsa_private_key_entry', 'keystores')).to be_a(OpenSSL::PKey::RSA)

# ensure that the created keystore can then be stored and re-read
stored = StringIO.new
stored.set_encoding('BINARY', 'BINARY')
expect { keystore.store(stored, 'keystores') }.not_to raise_error
stored.rewind

reloaded_store = OpenSSL::JKS.new
reloaded_store.load(stored, 'keystores')

expect(reloaded_store.size).to eq(keystore.size)
expect(reloaded_store.contains_alias('test_rsa_private_key_entry')).to be_truthy
expect(reloaded_store.get_key('test_rsa_private_key_entry', 'keystores').to_der).to \
eq(keystore.get_key('test_rsa_private_key_entry', 'keystores').to_der)
end

context 'writing a keystore' do
Expand Down

0 comments on commit cca83c0

Please sign in to comment.