Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v10.1.2+

SSL Client Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server using a client-side certificate, send a simple message, receive a simple response, and disconnect.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL oCertStore
LOCAL oJsonCN
LOCAL oCert
LOCAL nSsl
LOCAL nMaxWaitMillisec
LOCAL cSslServerHost
LOCAL nSslServerPort
LOCAL cReceivedMsg

nSuccess := 0

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

oSocket := CreateObject("Chilkat.Socket")

//  Create an instance of a certificate store object, load a PFX file,
//  locate the certificate we need, and use it for signing.
//  (a PFX file may contain more than one certificate.)
oCertStore := CreateObject("Chilkat.CertStore")
//  The 1st argument is the filename, the 2nd arg is the 
//  PFX file's password:
nSuccess := oCertStore:LoadPfxFile("chilkat_secret.pfx", "secret")
IF (nSuccess != 1)
    ? oCertStore:LastErrorText
    oSocket:destroy()
    oCertStore:destroy()
    RETURN
ENDIF

//  Find the certificate by the subject common name:
oJsonCN := CreateObject("Chilkat.JsonObject")
oJsonCN:UpdateString("CN", "cert common name")

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCertStore:FindCert(oJsonCN, oCert)
IF (nSuccess == 0)
    ? oCertStore:LastErrorText
    oSocket:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

nSuccess := oSocket:SetSslClientCert(oCert)

nSsl := 1
nMaxWaitMillisec := 20000

//  The SSL server hostname may be an IP address, a domain name,
//  or "localhost".  You'll need to change this:

cSslServerHost := "123.123.88.88"
nSslServerPort := 8123

//  Connect to the SSL server:
nSuccess := oSocket:Connect(cSslServerHost, nSslServerPort, nSsl, nMaxWaitMillisec)
IF (nSuccess != 1)
    ? oSocket:LastErrorText
    oSocket:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Set maximum timeouts for reading an writing (in millisec)
oSocket:MaxReadIdleMs := 20000
oSocket:MaxSendIdleMs := 20000

//  Send a "Hello Server! -EOM-" message:
nSuccess := oSocket:SendString("Hello Server! -EOM-")
IF (nSuccess != 1)
    ? oSocket:LastErrorText
    oSocket:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  The server (in this example) is going to send a "Hello Client! -EOM-" 
//  message.  Read it:
cReceivedMsg := oSocket:ReceiveUntilMatch("-EOM-")
IF (oSocket:LastMethodSuccess != 1)
    ? oSocket:LastErrorText
    oSocket:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Close the connection with the server
//  Wait a max of 20 seconds (20000 millsec)
nSuccess := oSocket:Close(20000)

? cReceivedMsg

oSocket:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()