Importing the private-key/public-certificate pair in the Java KeyStore

kingston

I used the following steps to create a new Java keystore with a pair of private/public key to be used by a Java (internal) server with TLS. Please notice that the certificate is selfsigned:

1) Generate key with AES256

openssl genrsa -aes256 -out server.key 1024

2) Generate cert request for CA

openssl req -x509 -sha256 -new -key server.key -out server.csr

3) Generate self signed expiry-time 10 years

openssl x509 -sha256 -days 3652 -in server.csr -signkey server.key -out selfsigned.crt

4) Use a program like KeyStoreExplorer to import the pair (private key and selfsigned certificate) in a new JKS

This works but I'd like to implement the last step without using a GUI.

I know how to import the self signed certificate only:

// create the keystore and import the public key. THIS WILL NOT IMPORT THE PRIVATE KEY SO THE KEYSTORE CAN'T BE USED ON THE SERVER TO MAKE THE TLS CONNECTION
/usr/java/jdk1.6.0_45/bin/keytool -import -alias myservercert -file server.crt -keystore mykeystore.jks

So the question is: how can I create a Java KeyStore and import both the certificate with the public key and the private key without using a GUI?

gtrig

With your private key and public certificate, you need to create a PKCS12 keystore first, then convert it into a JKS.

# Create PKCS12 keystore from private key and public certificate.
openssl pkcs12 -export -name myservercert -in selfsigned.crt -inkey server.key -out keystore.p12

# Convert PKCS12 keystore into a JKS keystore
keytool -importkeystore -destkeystore mykeystore.jks -srckeystore keystore.p12 -srcstoretype pkcs12 -alias myservercert

To verify the contents of the JKS, you can use this command:

keytool -list -v -keystore mykeystore.jks

If this was not a self-signed certificate, you would probably want to follow this step with importing the certificate chain leading up to the trusted CA cert.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

Importing private key to keystore

From Dev

Create java keystore from private key and CA certificate bundle

From Dev

Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

From Dev

Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

From Dev

importing a certificate to keystore in Tomcat using Powershell

From Dev

Certificate chain not found, but keystore contains private key

From Dev

link between private key and signed certificate in keystore

From Dev

Certificate chain not found, but keystore contains private key

From Dev

Saving private key with certificate in keystore - Android

From Dev

Java Keystore Private key import

From Dev

java add self signed certificate to keystore programatically

From Dev

Get certificate by alias in keystore with multiple entries in Java

From Dev

How to remove just one certificate from a certificate chain in a Java keystore

From Dev

java - need to import private key from jks(java keystore)

From Dev

How to validate a public and private key pair in Java

From Dev

How do I import a PKCS12 certificate into a java keystore?

From Dev

How to generate Java keystore from X.509 Certificate

From Dev

How to generate Java keystore from X.509 Certificate

From Dev

Java casting error when retrieving private key from keystore

From Dev

Adding certificate in keystore

From Dev

SSL certificate in keystore not working

From Dev

KeyStore Explorer - Created key pair?

From Dev

iOS: How to create PKCS12 (P12) keystore from private key and x509certificate in application programmatically?

From Dev

programmatically import .cer certificate into keystore

From Dev

Is Fingerprint of certificate always the same in keystore?

From Dev

Importing certificate to a keyring

From Dev

JAVA : How to make SSL connection with public certificate and private key

Related Related

  1. 1

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  2. 2

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  3. 3

    Importing private key to keystore

  4. 4

    Create java keystore from private key and CA certificate bundle

  5. 5

    Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

  6. 6

    Import elliptic curve Certificate and Private Key into Java Keystore using java.security.KeyStore

  7. 7

    importing a certificate to keystore in Tomcat using Powershell

  8. 8

    Certificate chain not found, but keystore contains private key

  9. 9

    link between private key and signed certificate in keystore

  10. 10

    Certificate chain not found, but keystore contains private key

  11. 11

    Saving private key with certificate in keystore - Android

  12. 12

    Java Keystore Private key import

  13. 13

    java add self signed certificate to keystore programatically

  14. 14

    Get certificate by alias in keystore with multiple entries in Java

  15. 15

    How to remove just one certificate from a certificate chain in a Java keystore

  16. 16

    java - need to import private key from jks(java keystore)

  17. 17

    How to validate a public and private key pair in Java

  18. 18

    How do I import a PKCS12 certificate into a java keystore?

  19. 19

    How to generate Java keystore from X.509 Certificate

  20. 20

    How to generate Java keystore from X.509 Certificate

  21. 21

    Java casting error when retrieving private key from keystore

  22. 22

    Adding certificate in keystore

  23. 23

    SSL certificate in keystore not working

  24. 24

    KeyStore Explorer - Created key pair?

  25. 25

    iOS: How to create PKCS12 (P12) keystore from private key and x509certificate in application programmatically?

  26. 26

    programmatically import .cer certificate into keystore

  27. 27

    Is Fingerprint of certificate always the same in keystore?

  28. 28

    Importing certificate to a keyring

  29. 29

    JAVA : How to make SSL connection with public certificate and private key

HotTag

Archive