Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loSocket = createobject("CkSocket")

//  Connect to the server using TLS.
llBTls = .T.
lnMaxWaitMs = 5000
llSuccess = loSocket.Connect("example.com",5000,llBTls,lnMaxWaitMs)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    return
endif

//  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
//  validate the server's identity.
loCert = createobject("CkCert")
llSuccess = loSocket.GetServerCert(loCert)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    release loCert
    return
endif

? "Server certificate subject: " + loCert.SubjectCN
? "Issued by: " + loCert.IssuerCN


release loSocket
release loCert