Sample code for 30+ languages & platforms
Android™

Verify and Pin the FTPS Server Certificate

See more FTP Examples

Demonstrates the certificate-verification properties RequireSslCertVerify, TlsPinSet, and the read-only SslServerCertVerified.

Background: By default a TLS connection is not rejected merely because certificate-chain verification fails, so RequireSslCertVerify = true is the important hardening step — it makes an expired, unsigned, or untrusted certificate abort the connection. TlsPinSet adds public-key pinning on top: the handshake fails unless the server's SPKI fingerprint matches one you configured, defending against a mis-issued certificate that would otherwise validate. Pinning supplements verification rather than replacing it. SslServerCertVerified reports the outcome.

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;

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("myFtpLogin");

    ftp.put_AuthTls(true);

    //  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");

    //  Reject the connection if the server certificate cannot be verified (expired, bad signature,
    //  untrusted chain, etc.).  The default is false, which does not reject on verification
    //  failure -- set true for security.
    ftp.put_RequireSslCertVerify(true);

    //  Optionally pin the server's public key.  If none of the configured SPKI fingerprints matches,
    //  the TLS handshake fails.  Pinning supplements normal verification; it does not replace it.
    ftp.put_TlsPinSet("sha256//YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=");

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

    //  SslServerCertVerified reports whether the certificate chain was successfully verified.
    if (ftp.get_SslServerCertVerified()) {
        Log.i(TAG, "The server certificate was verified.");
        }

    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."
  }
}