Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oSocket
LOCAL cPfxPassword
LOCAL oCert
LOCAL oConnectedSock
LOCAL nNumClientCerts
LOCAL oClientCert
LOCAL i
nSuccess := 0
oSocket := CreateObject("Chilkat.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.
cPfxPassword := "myPfxPassword"
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile("qa_data/server.pfx", cPfxPassword)
IF (nSuccess == 0)
? oCert:LastErrorText
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
nSuccess := oSocket:InitSslServer(oCert)
IF (nSuccess == 0)
? oSocket:LastErrorText
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
nSuccess := oSocket:BindAndListen(5000, 25)
IF (nSuccess == 0)
? oSocket:LastErrorText
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
// Accept a client connection. The accepted connection carries any client certificate presented
// during the TLS handshake.
oConnectedSock := CreateObject("Chilkat.Socket")
nSuccess := oSocket:AcceptNext(20000, oConnectedSock)
IF (nSuccess == 0)
? oSocket:LastErrorText
oSocket:destroy()
oCert:destroy()
oConnectedSock:destroy()
RETURN
ENDIF
// Copy each received client certificate and inspect it.
nNumClientCerts := oConnectedSock:NumReceivedClientCerts
oClientCert := CreateObject("Chilkat.Cert")
FOR i := 0 TO nNumClientCerts - 1
nSuccess := oConnectedSock:GetRcvdClientCert(i, oClientCert)
IF (nSuccess == 0)
? oConnectedSock:LastErrorText
oSocket:destroy()
oCert:destroy()
oConnectedSock:destroy()
oClientCert:destroy()
RETURN
ENDIF
? "Client certificate subject: " + oClientCert:SubjectCN
NEXT
oSocket:destroy()
oCert:destroy()
oConnectedSock:destroy()
oClientCert:destroy()