Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

let cert_store = chilkat::CertStore::new();

// On MacOS and iOS, the OpenSmartcard method opens the Keychain.
// The argument passed to OpenSmartcard is ignored.
if cert_store.open_smartcard("").is_err() {
    println!("{}", cert_store.last_error_text());
    return;
}

let num_certs = cert_store.num_certificates();
println!("numCerts = {}", num_certs);

let cert = chilkat::Cert::new();
let mut i = 0;
while i < num_certs {
    // 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.
    let _ = cert_store.get_cert(i, &cert);
    println!("{}", cert.subject_dn());
    println!("{}", cert.subject_cn());
    println!("{}", cert.serial_number());
    if cert.is_rsa() {
        println!("key type is RSA");
    }

    if cert.is_ecdsa() {
        println!("key type is ECDSA");
    }

    println!("has private key: {}", cert.has_private_key());
    println!("----");
    i = i + 1;
}

let _ = cert_store.close_cert_store();