Created
June 25, 2015 11:09
-
-
Save niklasvincent/20de859ad9bd7f8571f8 to your computer and use it in GitHub Desktop.
Test SSL connection in JVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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