Sample code for 30+ languages & platforms
CkPython

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

CkPython
import sys
import chilkat

success = False

socket = chilkat.CkSocket()

#  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()
success = cert.LoadPfxFile("qa_data/server.pfx",pfxPassword)
if (success == False):
    print(cert.lastErrorText())
    sys.exit()

success = socket.InitSslServer(cert)
if (success == False):
    print(socket.lastErrorText())
    sys.exit()

success = socket.BindAndListen(5000,25)
if (success == False):
    print(socket.lastErrorText())
    sys.exit()

#  Accept a client connection.  The accepted connection carries any client certificate presented
#  during the TLS handshake.
connectedSock = chilkat.CkSocket()
success = socket.AcceptNext(20000,connectedSock)
if (success == False):
    print(socket.lastErrorText())
    sys.exit()

#  Copy each received client certificate and inspect it.
numClientCerts = connectedSock.get_NumReceivedClientCerts()
clientCert = chilkat.CkCert()

for i in range(0,numClientCerts):
    success = connectedSock.GetRcvdClientCert(i,clientCert)
    if (success == False):
        print(connectedSock.lastErrorText())
        sys.exit()

    print("Client certificate subject: " + clientCert.subjectCN())