Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create keystore and truststore using self-signed certificate?

We have JAVA server and client communicate over network using SSL. The server and client mutually authenticate each other using certificates. The keystore type used by server and client is JKS. The keystore and truststore file names for the server and client are: server.keystore, server.truststore, client.keystore, and client.truststore.

I am using Self-Signed certificates for testing only.

Questions:

Q1. I would like to know why I need to add server’s and client’s own certificates into their respective truststores, in step 6.

Q2. Can I reduce the number steps to achieve the same thing? If yes, then how?

Steps to create RSA key, self-signed certificates, keystore, and truststore for a server

1. Generate a private RSA key

openssl genrsa -out diagserverCA.key 2048

2. Create a x509 certificate

openssl req -x509 -new -nodes -key diagserverCA.key -sha256 -days 1024 -out diagserverCA.pem

3. Create a PKCS12 keystore from private key and public certificate.

openssl pkcs12 -export -name server-cert -in diagserverCA.pem -inkey diagserverCA.key -out serverkeystore.p12

4. Convert PKCS12 keystore into a JKS keystore

keytool -importkeystore -destkeystore server.keystore -srckeystore serverkeystore.p12 -srcstoretype pkcs12 -alias server-cert

5. Import a client's certificate to the server's trust store.

keytool -import -alias client-cert -file diagclientCA.pem -keystore server.truststore

6. Import a server's certificate to the server's trust store.

keytool -import -alias server-cert -file diagserverCA.pem -keystore server.truststore

Steps to create RSA private key, self-signed certificate, keystore, and truststore for a client

1. Generate a private key

openssl genrsa -out diagclientCA.key 2048

2. Create a x509 certificate

openssl req -x509 -new -nodes -key diagclientCA.key -sha256 -days 1024 -out diagclientCA.pem

3. Create PKCS12 keystore from private key and public certificate.

openssl pkcs12 -export -name client-cert -in diagclientCA.pem -inkey diagclientCA.key -out clientkeystore.p12

4. Convert a PKCS12 keystore into a JKS keystore

keytool -importkeystore -destkeystore client.keystore -srckeystore clientkeystore.p12 -srcstoretype pkcs12 -alias client-cert

5. Import a server's certificate to the client's trust store.

keytool -import -alias server-cert -file diagserverCA.pem -keystore client.truststore

6. Import a client's certificate to the client's trust store.

keytool -import -alias client-cert -file diagclientCA.pem -keystore client.truststore

like image 413
vic99 Avatar asked Feb 22 '17 17:02

vic99


People also ask

Is Cacerts a keystore or Truststore?

'cacerts' is a truststore. A trust store is used to authenticate peers. A keystore is used to authenticate yourself.


1 Answers

Q1. I would like to know why I need to add server’s and client’s own certificates into their respective truststores, in step 6.

You don't. You add the server and client certificates into each other's truststores. The server and client have no need to trust their own certicifates, but they do need to trust each other's.

Q2. Can I reduce the number steps to achieve the same thing? If yes, then how?

You can do the entire thing with the keytool. Plenty of documented examples. You don't need to use openssl at all.

Critique:

  • In the first part, steps 5 and 6 are both wrong. There should be one step: exporting the server's certificate to the client's truststore.
  • Similarly, in the second part, steps 5 and 6 are again wrong, and again there should be only step: exporting the client's certificate to the server's keystore.
  • In other words, the two step 5s should be interchanged, and the two step 6s deleted.

You will find correct instructions for doing the lot in the JSSE Reference Guide in the JDK documentation. About three steps each. But all it really goes to show is that self-signed certificates really aren't worth the paper they're printed on. Get CA-signed certificates. Much more value and much easier to deploy (no export step).

Where did you get this rubbish?

like image 177
user207421 Avatar answered Oct 16 '22 14:10

user207421