Android™
Android™
Get and Inspect the FTPS Server Certificate Before Login
See more FTP Examples
Demonstrates the Chilkat Ftp2.GetServerCert method, which loads the leaf certificate presented on the connected FTPS control channel into a Cert object. The only argument is the Cert; an active TLS control connection is required. This example uses ConnectOnly followed by LoginAfterConnectOnly so the certificate can be inspected before any credentials are sent.
Background: Chilkat validates the server certificate during the TLS handshake, but an application often needs to apply its own check — pinning a fingerprint, matching a subject, or enforcing an internal policy — and the safe moment to do that is before revealing the login. Splitting the handshake with
ConnectOnly establishes TLS and makes the certificate available via GetServerCert, while deferring USER and PASS to LoginAfterConnectOnly. If the certificate fails inspection you simply disconnect, and an untrusted or impersonating server never receives the credentials. For declarative pinning without writing inspection code, SetSslCertRequirement lets you require certificate-field values up front.Chilkat Android™ Downloads
// 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.GetServerCert method, which loads the leaf certificate presented on the
// connected FTPS control channel into a Cert object. The only argument is the Cert. It requires
// an active TLS control connection.
//
// This example uses ConnectOnly + LoginAfterConnectOnly so the server certificate can be
// inspected and validated BEFORE any credentials are sent.
CkFtp2 ftp = new CkFtp2();
ftp.put_Hostname("ftp.example.com");
// Use explicit FTPS (AUTH TLS) on the standard FTP port.
ftp.put_AuthTls(true);
// Establish the connection and TLS layer and receive the greeting, but do NOT send USER/PASS.
success = ftp.ConnectOnly();
if (success == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
// The TLS control channel is now up. Retrieve the server's certificate for inspection.
CkCert cert = new CkCert();
success = ftp.GetServerCert(cert);
if (success == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
Log.i(TAG, "Server certificate subject: " + cert.subjectCN());
Log.i(TAG, "Issued by: " + cert.issuerCN());
// Perform any application-specific validation of the server certificate here -- for example,
// compare the fingerprint or subject against an expected value. Only proceed to authenticate if
// the certificate is acceptable. Because no credentials have been sent yet, an untrusted server
// never sees the username or password.
//
// if (certificate is not acceptable) { return; }
// The certificate passed inspection, so now provide credentials and authenticate.
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");
success = ftp.LoginAfterConnectOnly();
if (success == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
Log.i(TAG, "Authenticated after verifying the server 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."
}
}