The problem
I'm trying to connect to a local server via a SSL socket. The server uses a SSL certificate that:
- Is self-signed.
- Has a MD5withRSA signature (?)
I am aware of the security implications, but for reasons beyond my control, I cannot change or request changes to this certificate, thus I am forced to work with it - or rather, around it.
To overcome the above two points, I have created:
- A
TrustManager
that allows all certificates. - An
AlgorithmConstraints
that allows all algorithms.
I end up with the following error:
Caused by: java.security.cert.CertPathValidatorException: Algorithm constraints check failed on signature algorithm: MD5withRSA
It seems that my approach to relaxing the algorithm constrains is flawed.
Note: I am aware that that the MD5withRSA
algorithm can be allowed via altering the JVM configuration or a commandline parameter, but I need to do this programatically.
The code
The TrustManager
was created as follows:
TrustManager[] trustManager = new TrustManager[] {
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
The AlgorithmConstraints
was created as follows:
AlgorithmConstraints algorithmConstraints = new AlgorithmConstraints() {
@Override
public boolean permits(Set<CryptoPrimitive> primitives, String algorithm, AlgorithmParameters parameters) {
return true;
}
@Override
public boolean permits(Set<CryptoPrimitive> primitives, Key key) {
return true;
}
@Override
public boolean permits(Set<CryptoPrimitive> primitives, String algorithm, Key key, AlgorithmParameters parameters) {
return true;
}
}
And finally, the socket code is as follows:
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManager, new java.security.SecureRandom());
SSLSocketFactory factory = sslContext.getSocketFactory();
SSLSocket sslsocket = (SSLSocket) factory.createSocket(server, port);
SSLParameters sslParameters = new SSLParameters();
sslParameters.setAlgorithmConstraints(algorithmConstraints);
sslsocket.setSSLParameters(sslParameters);
sslSocket.startHandshake();
Suggestions?