Sample code for 30+ languages & platforms
PureBasic

FTPS with Mutual TLS Authentication (TLS Client Certificate)

See more FTP Examples

Demonstrates how to do mutual TLS authentication (using a client certificate from a .pfx/.p12).

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ftp.i = CkFtp2::ckCreate()
    If ftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkPort(ftp, 21)

    ; If using implicit TLS, you probably want port 990..
    CkFtp2::setCkPort(ftp, 990)

    ; Set this to 0 for implicit TLS, otherwise set to 1 for explicit TLS (where port is typically 21).
    CkFtp2::setCkAuthTls(ftp, 0)

    ; Set this to 1 for implicit TLS, otherwise set to 0.
    CkFtp2::setCkSsl(ftp, 1)

    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/example.pfx","pfx_password")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Use this certificate for our TLS mutually authenticated connection:
    success = CkFtp2::ckSetSslClientCert(ftp,cert)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Establish the TLS connection with the FTP server.
    success = CkFtp2::ckConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; If a login is required, then login with the FTP account login/password.
    CkFtp2::setCkUsername(ftp, "myLogin")
    CkFtp2::setCkPassword(ftp, "myPassword")
    success = CkFtp2::ckLoginAfterConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Do whatever you're doing to do ...
    ; upload files, download files, etc...

    ; .....
    ; .....

    success = CkFtp2::ckDisconnect(ftp)


    CkFtp2::ckDispose(ftp)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure