Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Socket;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Socket socket = new Socket();

        boolean ssl = true;
        int maxWaitMillisec = 20000;

        // The SSL server hostname may be an IP address, a domain name,
        // or "localhost".
        String sslServerHost = "www.paypal.com";
        int sslServerPort = 443;

        // Connect to the SSL server:
        try {
            socket.connect(sslServerHost, sslServerPort, ssl, maxWaitMillisec);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Cert cert = new Cert();

        boolean bExpired = false;
        boolean bRevoked = false;
        boolean bSignatureVerified = false;
        boolean bTrustedRoot = false;

        try {
            socket.getServerCert(cert);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println("Server Certificate:");
            System.out.println("Distinguished Name: " + cert.getSubjectDN());
            System.out.println("Common Name: " + cert.getSubjectCN());
            System.out.println("Issuer Distinguished Name: " + cert.getIssuerDN());
            System.out.println("Issuer Common Name: " + cert.getIssuerCN());

            bExpired = cert.getExpired();
            bRevoked = cert.getRevoked();
            bSignatureVerified = cert.getSignatureVerified();
            bTrustedRoot = cert.getTrustedRoot();

            System.out.println("Expired: " + bExpired);
            System.out.println("Revoked: " + bRevoked);
            System.out.println("Signature Verified: " + bSignatureVerified);
            System.out.println("Trusted Root: " + bTrustedRoot);
        }

        // Close the connection with the server
        // Wait a max of 20 seconds (20000 millsec)
        socket.closeConnection(20000);
    }
}