are used when a Java application needs to communicate over TLS
are password-protected files that sit on the same file system as our running application. The default format used for these files is JKS until Java 8. Since Java 9, though, the default keystore format is PKCS12. The biggest difference between JKS and PKCS12 is that JKS is a format specific to Java, while PKCS12 is a standardized and language-neutral way of storing encrypted private keys and certificates
Keystore vs Truststore - Differences
Keystore
typically holds onto certificates that identify us
contains private keys, and the certificates with their corresponding public keys
is used from KeyManager deciding which authentication credentials should be sent to the remote host for authentication during SSL handshake
In a SSL/TLS handshake the purpose of keyStore is to provide credential
Truststore
holds onto certificates that identify others
contains public certificates from other parties that you expect to communicate with, or from Certificate Authorities that you trust to identify other parties
is used by TrustManager to determine whether remote connection should be trusted
In a SSL/TLS handshake the purpose of trustStore is to verify credentials
Keystore vs Truststore - SLL/TLS Handshake
Let’s say we have a client that wants to communicate with a server over SSL/TLS
The server will look up the associated key from its keystore and present the public key and certificate to the client.
The client, then looks up the associated certificate in our truststore. If the certificate or Certificate Authorities presented by the server is not in our truststore, we’ll get an SSLHandshakeException and the connection won’t be set up successfully.
Interacting With Keystore and/or Truststore
We can interact with the keystore and/or truststore with either:
We see here that the truststore contains 92 trusted certificate entries and one of the entries is the verisignclass2gca entry. This means that the JVM will automatically trust certificates signed by verisignclass2g2ca
Setting Keystore and/or Truststore to be used in Java App
keystore
If we want to use an encrypted channel, we’ll have to set both:
javax.net.ssl.keyStore
javax.net.ssl.keyStorePassword
If our keystore format is different than the default, we use: