I am creating a jax-ws client app that needs to accept self-signed certificates. But then there is this common SSL Handshake exception problem when you are trying to import self-signed certificates. So the most common work around for that is to extend the JSSE security provider and to initialize SSL context to accept all certificates like:
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
TrustManager[] trustCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[]
certs, String authType) throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[]
certs, String authType) throws CertificateException {
return;
}
}
};
SSLContext sc = SSLContext.getInstance("SSLv3");
sc.init(null, trustCerts, null);
SocketFactory factory = sc.getSocketFactory();
SSLSocket socket;
socket = (SSLSocket) factory.createSocket(mInstance.getHostName(),getSecureConnectionEndpoint().getPort());
socket.startHandshake();
setCerts(socket.getSession().getPeerCertificates());
This was the only workaround I could find to the problem. There are many similar kind of questions here such as Import SSL certificates and they all propose the same solution.
This is obviously very prone to man in the middle attack and this solution seems to be legit only for testing purposes.
So my question, is there no way of making this whole process more secure when you try to import certificates programmatically?
Thank you in advance!