title: Registering multiple keystores in JVM
body: <p>I have two applications running in the same java virtual machine, and both use different keystores and truststores.</p>
  
  <p>A viable option would be use a single keystore and import all the other ones into the shared keystore (e.g. keytool -import), but it would really help my requirements if I could use separate keystores for separate applications running in the same jvm.</p>
  
  <p>I could set the keystore and truststores to be used as jvm parameters or system properties as follows:</p>
  
  <pre><code>java -Djavax.net.ssl.keyStore=serverKeys 
  -Djavax.net.ssl.keyStorePassword=password 
  -Djavax.net.ssl.trustStore=serverTrust 
  -Djavax.net.ssl.trustStorePassword=password SSLApplication
  </code></pre>
  
  <p>or</p>
  
  <pre><code>System.setProperty("javax.net.ssl.keyStore","serverKeys")
  </code></pre>
  
  <p>But the problem with this approach is that it specifies the keystore/truststore to be used at a JVM level, thus all applications running in the same JVM gets the same keystore/truststore.</p>
  
  <p>I have also tried creating a custom SSLContext and setting it as the default, but it also sets the context for all applications running in the same JVM.</p>
  
  <pre><code>SSLContext context = SSLContext.getInstance("SSL");
  context.init(kms, tms, null);
  SSLContext.setDefault(context);
  </code></pre>
  
  <p>I want to be able use different keystores/truststores without modifying individual application codes. </p>
  
  <p><strong>A solution that can <em>dynamically register multiple key stores</em> in addition to the default keystore/certs in jre into jvm would be great.</strong></p>
  
  <p>The solution will work in this way: </p>
  
  <ul>
  <li>When JVM boots, it loads all the default certs/keystores 
  from jre/certs folder (default java behaviour when no keystores specified).</li>
  <li>When App 1 loads it registers its keystores, </li>
  <li>then when App 2 loads it registers its keystores...</li>
  </ul>
  
  <p>Please let me know your ideas or solutions.
  Thanks in advance!</p>