Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Socket
string ls_PfxPassword
oleobject loo_Cert
oleobject loo_ConnectedSock
integer li_NumClientCerts
oleobject loo_ClientCert
integer i

li_Success = 0

loo_Socket = create oleobject
li_rc = loo_Socket.ConnectToNewObject("Chilkat.Socket")
if li_rc < 0 then
    destroy loo_Socket
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  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.
ls_PfxPassword = "myPfxPassword"
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/server.pfx",ls_PfxPassword)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Socket
    destroy loo_Cert
    return
end if

li_Success = loo_Socket.InitSslServer(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    destroy loo_Cert
    return
end if

li_Success = loo_Socket.BindAndListen(5000,25)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    destroy loo_Cert
    return
end if

//  Accept a client connection.  The accepted connection carries any client certificate presented
//  during the TLS handshake.
loo_ConnectedSock = create oleobject
li_rc = loo_ConnectedSock.ConnectToNewObject("Chilkat.Socket")

li_Success = loo_Socket.AcceptNext(20000,loo_ConnectedSock)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    destroy loo_Cert
    destroy loo_ConnectedSock
    return
end if

//  Copy each received client certificate and inspect it.
li_NumClientCerts = loo_ConnectedSock.NumReceivedClientCerts
loo_ClientCert = create oleobject
li_rc = loo_ClientCert.ConnectToNewObject("Chilkat.Cert")

for i = 0 to li_NumClientCerts - 1
    li_Success = loo_ConnectedSock.GetRcvdClientCert(i,loo_ClientCert)
    if li_Success = 0 then
        Write-Debug loo_ConnectedSock.LastErrorText
        destroy loo_Socket
        destroy loo_Cert
        destroy loo_ConnectedSock
        destroy loo_ClientCert
        return
    end if

    Write-Debug "Client certificate subject: " + loo_ClientCert.SubjectCN
next


destroy loo_Socket
destroy loo_Cert
destroy loo_ConnectedSock
destroy loo_ClientCert