Sample code for 30+ languages & platforms
Perl

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

Perl
use chilkat();

$success = 0;

$socket = chilkat::CkSocket->new();

#  Connect to the server using TLS.
$bTls = 1;
$maxWaitMs = 5000;
$success = $socket->Connect("example.com",5000,$bTls,$maxWaitMs);
if ($success == 0) {
    print $socket->lastErrorText() . "\r\n";
    exit;
}

#  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
#  validate the server's identity.
$cert = chilkat::CkCert->new();
$success = $socket->GetServerCert($cert);
if ($success == 0) {
    print $socket->lastErrorText() . "\r\n";
    exit;
}

print "Server certificate subject: " . $cert->subjectCN() . "\r\n";
print "Issued by: " . $cert->issuerCN() . "\r\n";