Sample code for 30+ languages & platforms
Chilkat2-Python

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 Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

socket = chilkat2.Socket()

# 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 = chilkat2.Cert()
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 = chilkat2.Socket()
success = socket.AcceptNext(20000,connectedSock)
if (success == False):
    print(socket.LastErrorText)
    sys.exit()

# Copy each received client certificate and inspect it.
numClientCerts = connectedSock.NumReceivedClientCerts
clientCert = chilkat2.Cert()

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)