Retrieving the Certification Path of an SSL Server : Certificate « Security « Java






Retrieving the Certification Path of an SSL Server

   

import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Main {
  public static void main(String[] argv) throws Exception {
    int port = 443;
    String hostname = "hostname";
    SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);

    socket.startHandshake();

    // Retrieve the server's certificate chain
    Certificate[] serverCerts = socket.getSession().getPeerCertificates();

    socket.close();
  }
}

   
    
    
  








Related examples in the same category

1.Signature Test
2.Specify the keystore of certificates using the javax.net.ssl.keyStore system property:
3.Retrieving a Certificate from a Key Store
4.Adding a Certificate to a Key Store
5.Creating a Certification Path
6.Listing the Most-Trusted Certificate Authorities (CA) in a Key Store
7.Validating a Certification Path using the most-trusted CAs in the JDK's cacerts file.
8.Importing a Certificate from a File
9.Getting the Subject and Issuer Distinguished Names of an X509 Certificate
10.Creates a CertStore from the contents of a file-system directory.