Thanks. But then the server certificate must be in my keystore. Otherwise I can't open the SSL connection with the server. Isn't it?
And is there any way to add the certificate to the keystore at runtime as a trusted certificate? The javadoc of setCertificateEntry() in KeyStore class says that if the alias is already present in the keystore as a trusted certificate entry, then only new certificate will be a trusted one. But if I have to add a new certificate with different alias and must be trusted, how can I achieve it?
Thanks again,
Gaurav
Right. That is what I was thinking. So can you please explain me the flow. I will be implementing my own TrustManager which picks the certificates from my keystore. Then at run time, I will get certificates that my TrustManager must check with certificates in the keystore. If those are trusted certificates, SSLScoket must be created. So should I have my own SSLScoket implementation or the the default will work with my TrustManager implementation? And is there any System property to set the custom TrustManager?
Please let me know if I am missing anything.
Thanks again.
Gaurav
You create an SSLContext, you initialize it with your own X509TrustManager, and use the SSLContext to create an SSLSocketFactory. Use this SSLSocketFactory to create SSLSockets as required. Those SSLSockets will then call your TrustManager with each server certificate received.
None of this requires a customization of SSLSocket.
Hey thanks. I got the idea. I have one more doubt. Actually, I am going to get the server certificates through some custom API. And I want to verify them with the certificates in the KeyStore. So can you tell me how the SSLSocket send the certificate recieved to the TrustManager?
I am still not very much clear about how all these things are going to work?
Thanks
Gaurav