Sample code for 30+ languages & platforms
B4X

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

B4X
Dim success As Boolean = False

Dim socket As ChilkatSocket
socket.Initialize("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.
Dim pfxPassword As String = "myPfxPassword"
Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadPfxFile("qa_data/server.pfx", pfxPassword)
If success = False Then
    Log(cert.LastErrorText)
    Return
End If


success = socket.InitSslServer(cert)
If success = False Then
    Log(socket.LastErrorText)
    Return
End If

success = socket.BindAndListen(5000, 25)
If success = False Then
    Log(socket.LastErrorText)
    Return
End If


'  Accept a client connection.  The accepted connection carries any client certificate presented
'  during the TLS handshake.
Dim connectedSock As ChilkatSocket
connectedSock.Initialize("connectedSock")
success = socket.AcceptNext(20000, connectedSock)
If success = False Then
    Log(socket.LastErrorText)
    Return
End If


'  Copy each received client certificate and inspect it.
Dim numClientCerts As Int = connectedSock.NumReceivedClientCerts
Dim clientCert As ChilkatCert
clientCert.Initialize("clientCert")
Dim i As Int
For i = 0 To numClientCerts - 1
    success = connectedSock.GetRcvdClientCert(i, clientCert)
    If success = False Then
        Log(connectedSock.LastErrorText)
        Return
    End If

    Log("Client certificate subject: " & clientCert.SubjectCN)
Next