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

Examine Client Certificates for an Accepted TLS Connection

See more Socket/SSL/TLS Examples

Demonstrates how to access the client certificates for a TLS connection accepted by your application acting as the server.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oListenSslSocket
LOCAL oCert
LOCAL nMyPort
LOCAL nBackLog
LOCAL nMaxWaitMillisec
LOCAL oClientSock
LOCAL nNumClientCerts
LOCAL oClientCert
LOCAL i

nSuccess := 0

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

oListenSslSocket := CreateObject("Chilkat.Socket")

//  An SSL/TLS server needs a digital certificate.  This example loads it from a PFX file.
//  This is the server's certificate.

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile("qa_data/serverCert/myServerCert.pfx", "pfx_password")
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oListenSslSocket:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  To accept client client certificates in the TLS handshake,
//  we must indicate a list of acceptable client certificate root CA DN's
//  that are allowed.  (DN is an acronym for Distinguished Name.)
//  Call AddSslAcceptableClientCaDn once for each acceptable CA DN.
//  Here are a few examples so you can see the general format of a DN.
oListenSslSocket:AddSslAcceptableClientCaDn("C=SE, O=AddTrust AB, OU=AddTrust External TTP Network, CN=AddTrust External CA Root")
oListenSslSocket:AddSslAcceptableClientCaDn("O=Digital Signature Trust Co., CN=DST Root CA X3")

//  Initialize with our server's TLS certificate.
nSuccess := oListenSslSocket:InitSslServer(oCert)
IF (nSuccess == 0)
    ? oListenSslSocket:LastErrorText
    oListenSslSocket:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Bind and listen on a port:
nMyPort := 8123
//  Allow for a max of 5 queued connect requests.
nBackLog := 5
nSuccess := oListenSslSocket:BindAndListen(nMyPort, nBackLog)
IF (nSuccess == 0)
    ? oListenSslSocket:LastErrorText
    oListenSslSocket:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Accept the next incoming connection.
nMaxWaitMillisec := 20000

oClientSock := CreateObject("Chilkat.Socket")
nSuccess := oListenSslSocket:AcceptNext(nMaxWaitMillisec, oClientSock)
IF (nSuccess == 0)
    ? oListenSslSocket:LastErrorText
    oListenSslSocket:destroy()
    oCert:destroy()
    oClientSock:destroy()
    RETURN
ENDIF

//  Examine the client certs chain.  The 1st cert will be the client certificate, and
//  the subsequent certs will be the certs in the chain of authentication.
nNumClientCerts := oClientSock:NumReceivedClientCerts
? "numClientCerts = " + Str(nNumClientCerts)

oClientCert := CreateObject("Chilkat.Cert")
i := 0
DO WHILE i < nNumClientCerts
    oClientSock:GetRcvdClientCert(i, oClientCert)
    ? oClientCert:SubjectDN
    i := i + 1
ENDDO

//  Close the connection with the client
nSuccess := oClientSock:Close(1000)

oListenSslSocket:destroy()
oCert:destroy()
oClientSock:destroy()
oClientCert:destroy()