Skip to content

Instantly share code, notes, and snippets.

@niklasvincent
Created June 25, 2015 11:09
Show Gist options
  • Save niklasvincent/20de859ad9bd7f8571f8 to your computer and use it in GitHub Desktop.
Save niklasvincent/20de859ad9bd7f8571f8 to your computer and use it in GitHub Desktop.
Test SSL connection in JVM
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLContext;
import java.security.cert.X509Certificate;
public class TestSSL
{
public static void main(String [] args)
{
try {
if (args.length < 1) {
System.out.println("Usage: java TestSSL <hostname>");
System.exit(0);
}
System.out.println("Java " + System.getProperty("java.version"));
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, null, null);
SSLSocketFactory ssf = sc.getSocketFactory();
SSLSocket s = (SSLSocket) ssf.createSocket(args[0], 443);
s.startHandshake();
SSLSession session = s.getSession();
java.security.cert.Certificate[] servercerts = session.getPeerCertificates();
for (int i = 0; i < servercerts.length; i++) {
X509Certificate xc = (X509Certificate) servercerts[i];
System.out.println("=====================================================");
System.out.println(xc.getSubjectDN());
System.out.println(xc.getNotAfter());
System.out.println(xc.getIssuerDN());
System.out.println(xc.getSigAlgName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment