Consider the following function:
public Certificate[] getCertificates(String ip) throws Exception {
try (SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket()) {
SocketAddress socketAddress = new InetSocketAddress(ip, port);
sslSocket.connect(socketAddress, 10000);
Certificate[] certificates = sslSocket.getSession().getPeerCertificates();
return certificates;
}
}
On which terms this code can hang? Basically, I've put a 10 seconds timeout for the connection phase, but is it possible that the code will block for a long time on the getSession().getPeerCertificates() part?
Please consider the fact that the ip and port can be arbitrary (not even SSL or sometimes not even routable)
Also, this is how I initiate the sslContext
private SSLContext getSslContext() throws Exception {
SSLContext sslCtx;
sslCtx = SSLContext.getInstance("TLSv1.2");
sslCtx.init(null, new TrustManager[]{new X509TrustManager() {
private X509Certificate[] accepted;
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String string) {
accepted = xcs;
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return accepted;
}
}}, null);
return sslCtx;
}