Sample code for 30+ languages & platforms
PureBasic

Set an FTPS Client Certificate (Cert object)

See more FTP Examples

Demonstrates the Chilkat Ftp2.SetSslClientCert method, which configures a client certificate for FTPS servers that request mutual TLS authentication. The only argument is a Cert that must have access to its private key.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Some high-security FTPS deployments require the client to present its own certificate during the TLS handshake — mutual TLS — as a second factor alongside the username and password. Because that happens while connecting, the certificate must be set before Connect. The Cert must carry its private key, since the client proves possession of it during the handshake.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.SetSslClientCert method, which configures a client certificate for FTPS
    ;  servers that request mutual TLS authentication.  The only argument is a Cert object that must
    ;  have access to its private key.

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

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkUsername(ftp, "myFtpLogin")

    ;  Normally you would not hard-code the password in source.  You should instead obtain it
    ;  from an interactive prompt, environment variable, or a secrets vault.
    CkFtp2::setCkPassword(ftp, "myPassword")

    ;  Use explicit FTPS (AUTH TLS) on the standard FTP port.
    CkFtp2::setCkAuthTls(ftp, 1)

    ;  Load the client certificate (with its private key) from a PFX file.
    ;  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/client.pfx",pfxPassword)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ;  Provide the client certificate before connecting.
    success = CkFtp2::ckSetSslClientCert(ftp,cert)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    success = CkFtp2::ckConnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Connected with a client certificate."

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf



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


    ProcedureReturn
EndProcedure