Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    String sPfxPassword
    Variant vCert
    Handle hoCert
    String sTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "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.
    Set ComPassword Of hoFtp To "myPassword"

    //  Use explicit FTPS (AUTH TLS) on the standard FTP port.
    Set ComAuthTls Of hoFtp To True

    //  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.
    Move "myPfxPassword" To sPfxPassword
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/client.pfx" sPfxPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the client certificate before connecting.
    Get pvComObject of hoCert to vCert
    Get ComSetSslClientCert Of hoFtp vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connected with a client certificate."

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure