Sample code for 30+ languages & platforms
Java Requires Chilkat v10.1.2+

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertStore;
import com.chilkatsoft.chilkat2.ChilkatException;

public class ChilkatExample {

    public static void main(String[] args) {
        CertStore certStore = new CertStore();

        // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
        // The argument passed to OpenSmartcard is ignored.
        try {
            certStore.openSmartcard("");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        Cert cert = new Cert();
        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.getSubjectDN());
            System.out.println(cert.getSubjectCN());
            System.out.println(cert.getSerialNumber());
            if (cert.isRsa()) {
                System.out.println("key type is RSA");
            }

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

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

        certStore.closeCertStore();
    }
}