Sample code for 30+ languages & platforms
Android™

Get a Received Client Certificate (Server Side)

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Use NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.

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;

    CkSocket socket = new CkSocket();

    //  The file path is relative to the application's current working directory.  An absolute path
    //  may also be used.  Supply the path appropriate to your own environment.
    //  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/server.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    success = socket.InitSslServer(cert);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    success = socket.BindAndListen(5000,25);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    //  Accept a client connection.  The accepted connection carries any client certificate presented
    //  during the TLS handshake.
    CkSocket connectedSock = new CkSocket();
    success = socket.AcceptNext(20000,connectedSock);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    //  Copy each received client certificate and inspect it.
    int numClientCerts = connectedSock.get_NumReceivedClientCerts();
    CkCert clientCert = new CkCert();
    int i;
    for (i = 0; i <= numClientCerts - 1; i++) {
        success = connectedSock.GetRcvdClientCert(i,clientCert);
        if (success == false) {
            Log.i(TAG, connectedSock.lastErrorText());
            return;
            }

        Log.i(TAG, "Client certificate subject: " + clientCert.subjectCN());
        }


  }

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