Todos los HostnameVerifier confiados causan errores SSL con HttpURLConnection
Estoy usando HttpURLConnection para conectarme a sitios SSL. Ocasionalmente, estos usan certificados autofirmados / SSL de otro modo mal comportamiento, por lo que tengo un modo donde se puede acceder a estos. Estoy usando el código típico recomendado en muchos lugares en línea:
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Don't verify host names
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
El problema es que esto causa errores SSL en muchos sitios, por ejemplo:
https://cong-shalom.org/ Causas:
javax.net.ssl.SSLException: Received fatal alert: internal_error
Salida cuando javax.net.debug = todo habilitado:
[java] Thread-8, READ: TLSv1.2 Alert, length = 2
[java] Thread-8, RECV TLSv1.2 ALERT: fatal, internal_error
[java] Thread-8, called closeSocket()
https://www.territrespicio.com Causas:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
Salida cuando javax.net.debug = todo habilitado:
[java] Thread-13, READ: TLSv1.2 Alert, length = 2
[java] Thread-13, RECV TLSv1.2 ALERT: fatal, handshake_failure
[java] Thread-13, called closeSocket()
Ninguno de estos problemas está relacionado con el TrustManager, comentar la parte HostnameVerifier siempre permite que la conexión funcione correctamente.
Notas:
Versión de Java: 1.8.0_111Desafortunadamente, no puedo cambiar a httpclient apache (hay mucho código construido alrededor de HttpURLConnection)