Sample code for 30+ languages & platforms
Android™

SSH Authentication using X.509 Certificates

See more SSH Examples

Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509 certificate. The certificate and key are loaded from a .pfx, the private key is exported as PEM, and that key is used for public-key authentication. See X.509v3 Certificates for SSH Authentication for more information.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: This is not X.509 certificate authentication in the SSH-protocol sense — the server still performs ordinary public-key authentication. The certificate is simply a convenient container for a key pair your organization already manages through its PKI, so the same credential issued for other purposes can be reused for SSH. What the server needs installed is the corresponding public key, exactly as with any other key. Chilkat can also load such certificates from smart cards and USB tokens rather than a file.

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;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509
    //  certificate.

    CkSsh ssh = new CkSsh();

    String hostname = "ssh.example.com";
    int port = 22;
    success = ssh.Connect(hostname,port);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Load the certificate and its private key from a .pfx file.  The PFX password should come
    //  from a secure source rather than being hard-coded.
    //  Note: Chilkat can load certificates and private keys from many sources, including smart
    //  cards and USB tokens (HSMs).
    String pfxPassword = "myPfxPassword";
    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/pfx/example.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    //  Get the certificate's private key as PEM, to be used for SSH authentication.  The
    //  corresponding public key is installed on the server.
    String privKeyPem = cert.getPrivateKeyPem();
    if (cert.get_LastMethodSuccess() == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    CkSshKey key = new CkSshKey();
    success = key.FromOpenSshPrivateKey(privKeyPem);
    if (success == false) {
        Log.i(TAG, key.lastErrorText());
        return;
        }

    success = ssh.AuthenticatePk("mySshLogin",key);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    Log.i(TAG, "Public-key authentication successful.");

    ssh.Disconnect();

  }

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