Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loSocket = createobject("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.
lcPfxPassword = "myPfxPassword"
loCert = createobject("CkCert")
llSuccess = loCert.LoadPfxFile("qa_data/server.pfx",lcPfxPassword)
if (llSuccess = .F.) then
    ? loCert.LastErrorText
    release loSocket
    release loCert
    return
endif

llSuccess = loSocket.InitSslServer(loCert)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    release loCert
    return
endif

llSuccess = loSocket.BindAndListen(5000,25)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    release loCert
    return
endif

//  Accept a client connection.  The accepted connection carries any client certificate presented
//  during the TLS handshake.
loConnectedSock = createobject("CkSocket")
llSuccess = loSocket.AcceptNext(20000,loConnectedSock)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    release loCert
    release loConnectedSock
    return
endif

//  Copy each received client certificate and inspect it.
lnNumClientCerts = loConnectedSock.NumReceivedClientCerts
loClientCert = createobject("CkCert")

for i = 0 to lnNumClientCerts - 1
    llSuccess = loConnectedSock.GetRcvdClientCert(i,loClientCert)
    if (llSuccess = .F.) then
        ? loConnectedSock.LastErrorText
        release loSocket
        release loCert
        release loConnectedSock
        release loClientCert
        return
    endif

    ? "Client certificate subject: " + loClientCert.SubjectCN
next


release loSocket
release loCert
release loConnectedSock
release loClientCert