Sample code for 30+ languages & platforms
Ruby

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 Ruby Downloads

Ruby
require 'chilkat'

success = false

certStore = Chilkat::CkCertStore.new()

# On MacOS and iOS, the OpenSmartcard method opens the Keychain.
# The argument passed to OpenSmartcard is ignored.
success = certStore.OpenSmartcard("")
if (success == false)
    print certStore.lastErrorText() + "\n";
    exit
end

numCerts = certStore.get_NumCertificates()
print "numCerts = " + numCerts.to_s() + "\n";

cert = Chilkat::CkCert.new()
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)
    print cert.subjectDN() + "\n";
    print cert.subjectCN() + "\n";
    print cert.serialNumber() + "\n";
    if (cert.IsRsa() == true)
        print "key type is RSA" + "\n";
    end

    if (cert.IsEcdsa() == true)
        print "key type is ECDSA" + "\n";
    end

    print "has private key: " + cert.HasPrivateKey().to_s() + "\n";
    print "----" + "\n";
    i = i + 1
end

certStore.CloseCertStore()