Skip to content

Instantly share code, notes, and snippets.

@ehdez73
Created March 11, 2014 06:59
Show Gist options
  • Select an option

  • Save ehdez73/9480832 to your computer and use it in GitHub Desktop.

Select an option

Save ehdez73/9480832 to your computer and use it in GitHub Desktop.
Test host certificate
import java.io.IOException;
import java.net.UnknownHostException;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class CertTest {
public static void main(String[] args) throws UnknownHostException, IOException {
int port = 443;
String hostname = "google.es";
System.setProperty("javax.net.debug", "ssl");
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);
socket.startHandshake();
Certificate[] serverCerts = socket.getSession().getPeerCertificates();
System.out.println("number of found certificates: " + serverCerts.length);
for (Certificate certificate : serverCerts) {
System.out.println(certificate.getPublicKey());
System.out.println(certificate);
}
socket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment