Sample code for 30+ languages & platforms
Java

Apple Keychain - List Certs on Smartcards and USB Tokens

See more Apple Keychain Examples

Iterates over the certificatse on connected smartcards and USB tokens via the Apple Keychain.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkCertStore certStore = new CkCertStore();

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    success = certStore.OpenSmartcard("");
    if (success == false) {
        System.out.println(certStore.lastErrorText());
        return;
        }

    int numCerts = certStore.get_NumCertificates();
    System.out.println("numCerts = " + numCerts);

    CkCert cert = new CkCert();
    int i = 0;
    while (i < numCerts) {
        // Note: Chilkat also gets the associated private key if it exists.
        // You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
        certStore.GetCert(i,cert);
        System.out.println(cert.subjectDN());
        System.out.println(cert.subjectCN());
        System.out.println(cert.serialNumber());
        if (cert.IsRsa() == true) {
            System.out.println("key type is RSA");
            }

        if (cert.IsEcdsa() == true) {
            System.out.println("key type is ECDSA");
            }

        System.out.println("has private key: " + cert.HasPrivateKey());
        System.out.println("----");
        i = i+1;
        }

    certStore.CloseCertStore();
  }
}