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

Lua
local chilkat = require("chilkat")

local certStore = chilkat.CertStore.new()

-- On MacOS and iOS, the OpenSmartcard method opens the Keychain.
-- The argument passed to OpenSmartcard is ignored.
local success = certStore:open_smartcard("")
if not success then
    print(certStore.last_error_text)
    return
end

local numCerts = certStore.num_certificates
print("numCerts = " .. numCerts)

local cert = chilkat.Cert.new()
local i = 0
while i < numCerts do
    -- 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:get_cert(i, cert)
    print(cert.subject_dn)
    print(cert.subject_cn)
    print(cert.serial_number)
    if cert:is_rsa() then
        print("key type is RSA")
    end

    if cert:is_ecdsa() then
        print("key type is ECDSA")
    end

    print("has private key: " .. tostring(cert:has_private_key()))
    print("----")
    i = i + 1
end

certStore:close_cert_store()