Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

set socket [new_CkSocket]

#  Connect to the server using TLS.
set bTls 1
set maxWaitMs 5000
set success [CkSocket_Connect $socket "example.com" 5000 $bTls $maxWaitMs]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
#  validate the server's identity.
set cert [new_CkCert]

set success [CkSocket_GetServerCert $socket $cert]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    delete_CkCert $cert
    exit
}

puts "Server certificate subject: [CkCert_subjectCN $cert]"
puts "Issued by: [CkCert_issuerCN $cert]"

delete_CkSocket $socket
delete_CkCert $cert