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

PHP
<?php

use Chilkat2\Cert;
use Chilkat2\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 (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$numCerts = $certStore->NumCertificates;
echo 'numCerts = ', $numCerts, "\n";

$cert = new Cert();
$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);
    echo $cert->SubjectDN, "\n";
    echo $cert->SubjectCN, "\n";
    echo $cert->SerialNumber, "\n";
    if ($cert->IsRsa()) {
        echo "key type is RSA\n";
    }

    if ($cert->IsEcdsa()) {
        echo "key type is ECDSA\n";
    }

    echo 'has private key: ', ($cert->HasPrivateKey() ? 'true' : 'false'), "\n";
    echo "----\n";
    $i = $i + 1;
}

$certStore->CloseCertStore();