Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

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

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("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.
    ftp.put_Password("myPassword");

    //  Use explicit FTPS (AUTH TLS) on the standard FTP port.
    ftp.put_AuthTls(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.
    String pfxPassword = "myPfxPassword";
    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/client.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    //  Provide the client certificate before connecting.
    success = ftp.SetSslClientCert(cert);
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    success = ftp.Connect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Connected with a client certificate.");

    success = ftp.Disconnect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}