Java SSL pass certificate chain as client certificate
<p>I want to build up an HTTPS connection with SSL/TLSv1.2.
I have several things loaded.</p>

<pre><code>X509Certificate rootCertificate = ...;
X509Certificate intermediateCertificate = ...;
X509Certificate myPublicCertificate = ...;
KeyStore myPrivateKey = ...;


KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(myPrivateKey, "password");
SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

SSLSocketFactory socketFactory = context.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
URL url = new URL(urlPath);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
</code></pre>

<p>Now I have to connect to to different servers.</p>

<p><strong>Connection to Server1 (works)</strong></p>

<p>Server1 has as Cert Authorities the <em>rootCertificate</em> as well as the <em>intermediateCertificate</em>. The connection could be established.</p>

<p><strong>Connection to Server2 (doesn't work)</strong>
Server2 has only the <em>rootCertificate</em> as Cert Authorities configured. But <em>MyPrivateKey</em> is signed with the <em>intermediateCertificate</em> which Server2 doesn't know. During SSL handshake following logs occur:</p>

<pre><code>*** ServerHelloDone
Warning: no suitable certificate found - continuing without client authentication
*** Certificate chain
&lt;Empty&gt;
***
</code></pre>

<p>How do I configure the connection that it knows <em>MyPrivateKey</em> as well as <em>intermediateCertificate</em>. Is it even possible??</p>