Sample code for 30+ languages & platforms
Perl

Get a Received Client Certificate (Server Side)

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Use NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.

Chilkat Perl Downloads

Perl
use chilkat();

$success = 0;

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

#  The file path is relative to the application's current working directory.  An absolute path
#  may also be used.  Supply the path appropriate to your own environment.
#  The PFX password should come from a secure source rather than being hard-coded.
$pfxPassword = "myPfxPassword";
$cert = chilkat::CkCert->new();
$success = $cert->LoadPfxFile("qa_data/server.pfx",$pfxPassword);
if ($success == 0) {
    print $cert->lastErrorText() . "\r\n";
    exit;
}

$success = $socket->InitSslServer($cert);
if ($success == 0) {
    print $socket->lastErrorText() . "\r\n";
    exit;
}

$success = $socket->BindAndListen(5000,25);
if ($success == 0) {
    print $socket->lastErrorText() . "\r\n";
    exit;
}

#  Accept a client connection.  The accepted connection carries any client certificate presented
#  during the TLS handshake.
$connectedSock = chilkat::CkSocket->new();
$success = $socket->AcceptNext(20000,$connectedSock);
if ($success == 0) {
    print $socket->lastErrorText() . "\r\n";
    exit;
}

#  Copy each received client certificate and inspect it.
$numClientCerts = $connectedSock->get_NumReceivedClientCerts();
$clientCert = chilkat::CkCert->new();

for ($i = 0; $i <= $numClientCerts - 1; $i++) {
    $success = $connectedSock->GetRcvdClientCert($i,$clientCert);
    if ($success == 0) {
        print $connectedSock->lastErrorText() . "\r\n";
        exit;
    }

    print "Client certificate subject: " . $clientCert->subjectCN() . "\r\n";
}