title: How to create keystore and truststore using self-signed certificate?
body: <p>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.</p>
  <p>I am using Self-Signed certificates for testing only.</p>
  <p><strong>Questions:</strong></p>
  <p>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.</p>
  <p>Q2. Can I reduce the number steps to achieve the same thing? If yes, then how?</p>
  <h1><strong>Steps to create RSA key, self-signed certificates, keystore, and truststore for a server</strong></h1>
  <p><strong>1. Generate a private RSA key</strong></p>
  <p>openssl genrsa -out diagserverCA.key 2048</p>
  <p><strong>2. Create a x509 certificate</strong></p>
  <p>openssl req -x509 -new -nodes -key diagserverCA.key -sha256 -days 1024 -out diagserverCA.pem</p>
  <p><strong>3. Create a PKCS12 keystore from private key and public certificate.</strong></p>
  <p>openssl pkcs12 -export -name server-cert -in diagserverCA.pem -inkey diagserverCA.key -out serverkeystore.p12</p>
  <p><strong>4. Convert PKCS12 keystore into a JKS keystore</strong></p>
  <p>keytool -importkeystore -destkeystore server.keystore -srckeystore serverkeystore.p12 -srcstoretype pkcs12 -alias server-cert</p>
  <p><strong>5. Import a client's certificate to the server's trust store.</strong></p>
  <p>keytool -import -alias client-cert -file diagclientCA.pem -keystore server.truststore</p>
  <p><strong>6. Import a server's certificate to the server's trust store.</strong></p>
  <p>keytool -import -alias  server-cert -file diagserverCA.pem -keystore server.truststore</p>
  <h1><strong>Steps to create RSA private key, self-signed certificate, keystore, and truststore for a client</strong></h1>
  <p><strong>1. Generate a private key</strong></p>
  <p>openssl genrsa -out diagclientCA.key 2048</p>
  <p><strong>2. Create a x509 certificate</strong></p>
  <p>openssl req -x509 -new -nodes -key diagclientCA.key -sha256 -days 1024 -out diagclientCA.pem</p>
  <p><strong>3. Create PKCS12 keystore from private key and public certificate.</strong></p>
  <p>openssl pkcs12 -export -name client-cert -in diagclientCA.pem -inkey diagclientCA.key -out clientkeystore.p12</p>
  <p><strong>4. Convert a PKCS12 keystore into a JKS keystore</strong></p>
  <p>keytool -importkeystore -destkeystore client.keystore -srckeystore clientkeystore.p12 -srcstoretype pkcs12 -alias client-cert</p>
  <p><strong>5. Import a server's certificate to the client's trust store.</strong></p>
  <p>keytool -import -alias server-cert -file diagserverCA.pem -keystore client.truststore</p>
  <p><strong>6. Import a client's certificate to the client's trust store.</strong></p>
  <p>keytool -import -alias client-cert -file diagclientCA.pem -keystore client.truststore</p>