Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoCertStore.h>
#import <CkoCert.h>

BOOL success = NO;

CkoCertStore *certStore = [[CkoCertStore alloc] init];

// On MacOS and iOS, the OpenSmartcard method opens the Keychain.
// The argument passed to OpenSmartcard is ignored.
success = [certStore OpenSmartcard: @""];
if (success == NO) {
    NSLog(@"%@",certStore.LastErrorText);
    return;
}

int numCerts = [certStore.NumCertificates intValue];
NSLog(@"%@%d",@"numCerts = ",numCerts);

CkoCert *cert = [[CkoCert alloc] init];
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: [NSNumber numberWithInt: i] cert: cert];
    NSLog(@"%@",cert.SubjectDN);
    NSLog(@"%@",cert.SubjectCN);
    NSLog(@"%@",cert.SerialNumber);
    if ([cert IsRsa] == YES) {
        NSLog(@"%@",@"key type is RSA");
    }

    if ([cert IsEcdsa] == YES) {
        NSLog(@"%@",@"key type is ECDSA");
    }

    NSLog(@"%@%d",@"has private key: ",[cert HasPrivateKey]);
    NSLog(@"%@",@"----");
    i = i + 1;
}

[certStore CloseCertStore];