Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.0.0+

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

my $socket = Chilkat2::Socket->new;

my $ssl = 1;
my $maxWaitMillisec = 20000;

# The SSL server hostname may be an IP address, a domain name,
# or "localhost".
my $sslServerHost = 'www.paypal.com';
my $sslServerPort = 443;

# Connect to the SSL server:
my $success = $socket->Connect($sslServerHost, $sslServerPort, $ssl, $maxWaitMillisec);
if (!$success) {
    print $socket->LastErrorText, "\n";
    exit;
}

my $cert = Chilkat2::Cert->new;

my $bExpired = 0;
my $bRevoked = 0;
my $bSignatureVerified = 0;
my $bTrustedRoot = 0;

$success = $socket->GetServerCert($cert);
if ($success) {
    print "Server Certificate:\n";
    print 'Distinguished Name: ', $cert->SubjectDN, "\n";
    print 'Common Name: ', $cert->SubjectCN, "\n";
    print 'Issuer Distinguished Name: ', $cert->IssuerDN, "\n";
    print 'Issuer Common Name: ', $cert->IssuerCN, "\n";

    $bExpired = $cert->Expired;
    $bRevoked = $cert->Revoked;
    $bSignatureVerified = $cert->SignatureVerified;
    $bTrustedRoot = $cert->TrustedRoot;

    print 'Expired: ', ($bExpired ? 1 : 0), "\n";
    print 'Revoked: ', ($bRevoked ? 1 : 0), "\n";
    print 'Signature Verified: ', ($bSignatureVerified ? 1 : 0), "\n";
    print 'Trusted Root: ', ($bTrustedRoot ? 1 : 0), "\n";
}

# Close the connection with the server
# Wait a max of 20 seconds (20000 millsec)
$success = $socket->Close(20000);