Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL nBTls
LOCAL nMaxWaitMs
LOCAL oCert

nSuccess := 0

oSocket := CreateObject("Chilkat.Socket")

//  Connect to the server using TLS.
nBTls := 1
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("example.com", 5000, nBTls, nMaxWaitMs)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

//  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
//  validate the server's identity.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oSocket:GetServerCert(oCert)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    oCert:destroy()
    RETURN
ENDIF

? "Server certificate subject: " + oCert:SubjectCN
? "Issued by: " + oCert:IssuerCN

oSocket:destroy()
oCert:destroy()