Sample code for 30+ languages & platforms
PureBasic

Advertise Acceptable Client CA Names for Mutual TLS

See more Socket/SSL/TLS Examples

Demonstrates Socket.AddSslAcceptableClientCaDn, which adds a certificate-authority distinguished name to the list a TLS server advertises when requesting a client certificate for mutual TLS.

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. Call this once for each acceptable CA distinguished name, and add all names before calling InitSslServer. The server then requests a client certificate and advertises the configured acceptable CAs.

Chilkat PureBasic Downloads

PureBasic
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

    ;  For mutual TLS, advertise the acceptable client-certificate CA distinguished names BEFORE
    ;  initializing server-side TLS.  Call once for each acceptable CA distinguished name.
    success = CkSocket::ckAddSslAcceptableClientCaDn(socket,"CN=Example Root CA, O=Example, C=US")
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    success = CkSocket::ckAddSslAcceptableClientCaDn(socket,"CN=Example Intermediate CA, O=Example, C=US")
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        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

    ;  Initialize server-side TLS.  The server will now request a client certificate and advertise the
    ;  acceptable CA distinguished names added above.
    success = CkSocket::ckInitSslServer(socket,cert)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Server-side TLS configured to request client certificates."


    CkSocket::ckDispose(socket)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure