PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSocket.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 0
socket.i = CkSocket::ckCreate()
If socket.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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.s = "myPfxPassword"
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkCert::ckLoadPfxFile(cert,"qa_data/server.pfx",pfxPassword)
If success = 0
Debug CkCert::ckLastErrorText(cert)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
success = CkSocket::ckInitSslServer(socket,cert)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
success = CkSocket::ckBindAndListen(socket,5000,25)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; Accept a client connection. The accepted connection carries any client certificate presented
; during the TLS handshake.
connectedSock.i = CkSocket::ckCreate()
If connectedSock.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkSocket::ckAcceptNext(socket,20000,connectedSock)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
CkSocket::ckDispose(connectedSock)
ProcedureReturn
EndIf
; Copy each received client certificate and inspect it.
numClientCerts.i = CkSocket::ckNumReceivedClientCerts(connectedSock)
clientCert.i = CkCert::ckCreate()
If clientCert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i
For i = 0 To numClientCerts - 1
success = CkSocket::ckGetRcvdClientCert(connectedSock,i,clientCert)
If success = 0
Debug CkSocket::ckLastErrorText(connectedSock)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
CkSocket::ckDispose(connectedSock)
CkCert::ckDispose(clientCert)
ProcedureReturn
EndIf
Debug "Client certificate subject: " + CkCert::ckSubjectCN(clientCert)
Next
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
CkSocket::ckDispose(connectedSock)
CkCert::ckDispose(clientCert)
ProcedureReturn
EndProcedure