Sample code for 30+ languages & platforms
Xbase++

Use HTTPS Client Certificate from .cer and .key Files

See more HTTP Examples

Demonstrates how to load a cert + private key from .cer and .key (base64) files and use it for mutual TLS authentication (client-side certificate).

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oCert
LOCAL oPrivKey
LOCAL oBd

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

oCert := CreateObject("Chilkat.Cert")
oPrivKey := CreateObject("Chilkat.PrivateKey")

//  Load any type of certificate (.cer, .p7b, .pem, etc.) by calling LoadFromFile.
nSuccess := oCert:LoadFromFile("qa_data/certs/sample_cert_a.cer")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oHttp:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

//  Load the private key.
oBd := CreateObject("Chilkat.BinData")
nSuccess := oBd:LoadFile("qa_data/certs/sample_key_a.key")
nSuccess := oPrivKey:LoadAnyFormat(oBd, "privateKeyPasswordIfNecessary")
IF (nSuccess != 1)
    ? oPrivKey:LastErrorText
    oHttp:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  Associate the private key with the cert.
nSuccess := oCert:SetPrivateKey(oPrivKey)
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oHttp:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  Set the certificate to be used for mutual TLS authentication
//  (i.e. sets the client-side certificate for two-way TLS authentication)
nSuccess := oHttp:SetSslClientCert(oCert)
IF (nSuccess != 1)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  At this point, the HTTP object instance is setup with the client-side cert, and any SSL/TLS
//  connection will automatically use it if the server demands a client-side cert.

oHttp:destroy()
oCert:destroy()
oPrivKey:destroy()
oBd:destroy()