SSLSocket ignora la discrepancia de dominio
Estoy leyendo desde SSL Socket pero el host no coincide con el certificado (por ejemplo,host = "localhost"
). Yo esperaría la excepción, pero el siguiente código habla felizmente con el servidor remoto sin ningún problema.
try (
final Socket socket = SSLSocketFactory.getDefault().createSocket(host, port);
final OutputStream os = socket.getOutputStream();
final InputStream is = socket.getInputStream()) {
os.write(("HEAD / HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n").getBytes());
os.flush();
final byte[] bytes = new byte[1024];
int n;
while ((n = is.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, n));
}
System.out.println();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Por eso he intentado otro enfoque:
try {
final HttpURLConnection conn = (HttpURLConnection) new URL("https://" + host + ":" + port + "/").openConnection();
try (InputStream is = conn.getInputStream()) {
IOUtils.copy(is, System.out);
} catch (final IOException e1) {
try (InputStream es = conn.getErrorStream()) {
if (es != null) {
IOUtils.copy(es, System.out);
}
}
}
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Desafortunadamente, todavía no obtengo ninguna excepción de SSL, solo WARN en los registros:2013-07-31 16:02:27,182 WARN nio - javax.net.ssl.SSLException: Received fatal alert: certificate_unknown
¿Cómo obtener la excepción SSL si el certificado no coincide?