Java
Java
Get the Server Certificate from a TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.
Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.
Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkSocket socket = new CkSocket();
// Connect to the server using TLS.
boolean bTls = true;
int maxWaitMs = 5000;
success = socket.Connect("example.com",5000,bTls,maxWaitMs);
if (success == false) {
System.out.println(socket.lastErrorText());
return;
}
// Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
// validate the server's identity.
CkCert cert = new CkCert();
success = socket.GetServerCert(cert);
if (success == false) {
System.out.println(socket.lastErrorText());
return;
}
System.out.println("Server certificate subject: " + cert.subjectCN());
System.out.println("Issued by: " + cert.issuerCN());
}
}