Example usage for javax.net.ssl HttpsURLConnection getServerCertificates

List of usage examples for javax.net.ssl HttpsURLConnection getServerCertificates

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection getServerCertificates.

Prototype

public abstract java.security.cert.Certificate[] getServerCertificates() throws SSLPeerUnverifiedException;

Source Link

Document

Returns the server's certificate chain which was established as part of defining the session.

Usage

From source file:homenetapp.HomeNetAppGui.java

private void checkClientCert() {
    try {/*from   w  w w.  j  a v  a  2s .c om*/
        URL url = new URL("https://" + homenetapp.clientServer + "/");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.connect();
        Certificate[] certs = conn.getServerCertificates();

        //System.out.println("Cert Chain Length: "+certs.length);

        Certificate c = certs[0];
        X509Certificate xc = (X509Certificate) c;

        String[] from = homenetapp.splitTokens(xc.getIssuerX500Principal().getName(), "=, ");
        String[] to = homenetapp.splitTokens(xc.getSubjectX500Principal().getName(), "=, ");

        certPropertiesLabel.setText("<html>Issued by: " + from[1] + "<br>For: " + to[1] + "<br>Expires: "
                + xc.getNotAfter() + "</html>");

        System.out.println("Cert: " + c.getType());

        System.out.println("Not After: " + xc.getNotAfter());
        System.out.println("Subject DN: " + xc.getSubjectX500Principal());
        System.out.println("Issuer DN: " + xc.getIssuerX500Principal());
        System.out.println("getSigAlgName: " + xc.getSigAlgName());

    } catch (Exception e) {
        certPropertiesLabel.setText("Failed to load certficate");
    }

}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public String[] getSSLCertificates(Object connection, String url) throws IOException {
    if (connection instanceof HttpsURLConnection) {
        HttpsURLConnection conn = (HttpsURLConnection) connection;

        try {/*from  w w  w.  ja  va  2 s . co m*/
            conn.connect();
            java.security.cert.Certificate[] certs = conn.getServerCertificates();
            String[] out = new String[certs.length];
            int i = 0;
            for (java.security.cert.Certificate cert : certs) {
                MessageDigest md = MessageDigest.getInstance("SHA1");
                md.update(cert.getEncoded());
                out[i++] = "SHA1:" + dumpHex(md.digest());
            }
            return out;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return new String[0];

}