By Toby

2011-06-14 08:23:14 8 Comments

I understand that the keystore would usually hold private/public keys and the trust store only public keys (and represents the list of trusted parties you intend to communicate with). Well, that's my first assumption, so if that's not correct, I probably haven't started very well...

I was interested though in understanding how / when you distinguish the stores when using keytool.

So, far I've created a keystore using

keytool -import -alias bob -file bob.crt -keystore keystore.ks

which creates my keystore.ks file. I answer yes to the question do I trust bob but it is unclear to me if this has created a keystore file or a truststore file? I can set up my application to use the file as either.

and with System.setProperty( "", "ssl") set, I can see the certificate under trusted certifications (but not under the keystore section). The particular certificate I'm importing has only a public key and I intend to use it to send stuff over an SSL connection to Bob (but perhaps that's best left for another question!).

Any pointers or clarifications would be much appreciated. Is the output of keytool the same whatever you import and its just convention that says one is a keystore and the other a trust store? What's the relationship when using SSL etc?


@Snehal Masne 2019-05-21 11:10:28

In simplest terms :

Keystore is used to store your credential (server or client) while truststore is used to store others credential (Certificates from CA).

Keystore is needed when you are setting up server side on SSL, it is used to store server's identity certificate, which server will present to a client on the connection while trust store setup on client side must contain to make the connection work. If you browser to connect to any website over SSL it verifies certificate presented by server against its truststore.

@Akash5288 2016-12-22 10:10:01

Keystore is used by a server to store private keys, and Truststore is used by third party client to store public keys provided by server to access. I have done that in my production application. Below are the steps for generating java certificates for SSL communication:

  1. Generate a certificate using keygen command in windows:

keytool -genkey -keystore server.keystore -alias mycert -keyalg RSA -keysize 2048 -validity 3950

  1. Self certify the certificate:

keytool -selfcert -alias mycert -keystore server.keystore -validity 3950

  1. Export certificate to folder:

keytool -export -alias mycert -keystore server.keystore -rfc -file mycert.cer

  1. Import Certificate into client Truststore:

keytool -importcert -alias mycert -file mycert.cer -keystore truststore

@Deepak 2019-06-13 05:00:59

Hi, I have one scenario where I have two different applications within the same container (tomcat). From both applications, I have to call the rest endpoints from both sides to each application. Like, from A to B and B to A (A and B are the two applications). Do I need to use the truststore in this scenario? As I am using custom rest client which is using keystore. Please suggest.

@Balaji Boggaram Ramanarayan 2018-02-16 19:12:23

To explain in common usecase/purpose or layman way:

TrustStore : As the name indicates, its normally used to store the certificates of trusted entities. A process can maintain a store of certificates of all its trusted parties which it trusts.

keyStore : Used to store the server keys (both public and private) along with signed cert.

During the SSL handshake,

  1. A client tries to access https://

  2. And thus, Server responds by providing a SSL certificate (which is stored in its keyStore)

  3. Now, the client receives the SSL certificate and verifies it via trustStore (i.e the client's trustStore already has pre-defined set of certificates which it trusts.). Its like : Can I trust this server ? Is this the same server whom I am trying to talk to ? No middle man attacks ?

  4. Once, the client verifies that it is talking to server which it trusts, then SSL communication can happen over a shared secret key.

Note : I am not talking here anything about client authentication on server side. If a server wants to do a client authentication too, then the server also maintains a trustStore to verify client.

@Team END_TECH 2017-12-19 13:58:07

keystore simply stores private keys, wheras truststore stores public keys. You will want to generate a java certificate for SSL communication. You can use a keygen command in windows, this will probably be the most easy solution.

@user207421 2018-12-06 10:23:19

A truststore stores trusted certicificates.

@Bruno 2011-06-14 09:26:22

The terminology is a bit confusing indeed, but both and are used to specify which keystores to use, for two different purposes. Keystores come in various formats and are not even necessarily files (see this question), and keytool is just a tool to perform various operations on them (import/export/list/...).

The and parameters are the default parameters used to build KeyManagers and TrustManagers (respectively), then used to build an SSLContext which essentially contains the SSL/TLS settings to use when making an SSL/TLS connection via an SSLSocketFactory or an SSLEngine. These system properties are just where the default values come from, which is then used by SSLContext.getDefault(), itself used by SSLSocketFactory.getDefault() for example. (All of this can be customized via the API in a number of places, if you don't want to use the default values and that specific SSLContexts for a given purpose.)

The difference between the KeyManager and TrustManager (and thus between and is as follows (quoted from the JSSE ref guide):

TrustManager: Determines whether the remote authentication credentials (and thus the connection) should be trusted.

KeyManager: Determines which authentication credentials to send to the remote host.

(Other parameters are available and their default values are described in the JSSE ref guide. Note that while there is a default value for the trust store, there isn't one for the key store.)

Essentially, the keystore in is meant to contain your private keys and certificates, whereas the is meant to contain the CA certificates you're willing to trust when a remote party presents its certificate. In some cases, they can be one and the same store, although it's often better practice to use distinct stores (especially when they're file-based).

@Toby 2011-06-15 08:03:07

thanks for the reply, it clears things up a little. I'm still confused though when it comes to usage, I can use a pk12 pri/pub key (xxx.p12) as a keystore (via -D) and create a SSL connection (trusted) without any mention of a truststore via -D... oh well.

@Bruno 2011-06-15 09:09:10

You don't need to specify a truststore, because there's a default value for it (it's bundled with the JRE), usually in $JAVA_HOME/lib/security/cacerts (see 2nd JSSE ref guide link I sent). Like browsers, it contains a default set of trusted CA certificates. In general, a client will always use a truststore to check the server cert but the keystore will only be used if the server requests a client cert, and the server will always use a keystore for its own key+cert but the truststore will only be used if the client sends a client certificate.

@hagrawal 2017-02-13 15:51:53

Thanks for useful information. In Weblogic, there is "identity-key-store" which stores the server's SSL certificate and then there is "trust-key-store" which stores the SSL certificates which server trusts, so am I correct if I say that "identity-key-store" is nothing but a "keystore" and "trust-key-store" is nothing but a "truststore" ?

@musiKk 2011-06-14 08:35:27

There is no difference between keystore and truststore files. Both are files in the proprietary JKS file format. The distinction is in the use: To the best of my knowledge, Java will only use the store that is referenced by to look for certificates to trust when creating SSL connections. Same for keys and But in theory it's fine to use one and the same file for trust- and keystores.

@Donal Fellows 2011-06-14 08:53:08

You can use different types of keystore (e.g., PKCS12) by setting the and system properties.

@musiKk 2011-06-14 09:01:42

@Donal: Good addition. Do you happen to know if there is a list of all supported containers? I only know of PKCS12 and JKS (the former being the result of trial and error...).

@Bruno 2011-06-14 09:42:58

the keystore formats vary depending on the providers available (see this list for those bundled with the Oracle JRE by default). There was also a discussion in this question. Other providers (e.g. BouncyCastle) can be used for other formats.

Related Questions

Sponsored Content

32 Answered Questions

[SOLVED] Create ArrayList from array

54 Answered Questions

[SOLVED] Creating a memory leak with Java

2 Answered Questions

[SOLVED] How to import a jks certificate in java trust store

2 Answered Questions

1 Answered Questions

2 Answered Questions

Connect RESTHeart to MongoDB over TLS/SSL

2 Answered Questions

0 Answered Questions

Scala playframework WSClient doesn't send certificate

1 Answered Questions

[SOLVED] keytool error: java.lang.Exception: Keystore file exists, but is empty

3 Answered Questions

[SOLVED] Generate private and public key file using keytool

Sponsored Content