Sample code for 30+ languages & platforms
C

Load P7B and List Certificates

Demonstrates how to load a .p7b containing certificates and accesses each individual certificate.

Chilkat C Downloads

C
#include <C_CkPem.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPem pem;
    int i;
    int numCerts;
    HCkCert cert;

    success = FALSE;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    success = FALSE;

    pem = CkPem_Create();

    // Load the .p7b from a file.
    success = CkPem_LoadP7bFile(pem,"/Users/chilkat/testData/p7b/myCerts.p7b");
    if (success != TRUE) {
        printf("%s\n",CkPem_lastErrorText(pem));
        CkPem_Dispose(pem);
        return;
    }

    numCerts = CkPem_getNumCerts(pem);
    if (numCerts == 0) {
        printf("%s\n",("Error: Expected the .p7b to contain certificates."));
        CkPem_Dispose(pem);
        return;
    }

    // Access each certificate and show the DN (Distinguished Name)
    for (i = 1; i <= numCerts; i++) {
        cert = CkPem_GetCert(pem,i - 1);
        printf("%d: %s\n",i,CkCert_subjectDN(cert));
        CkCert_Dispose(cert);
    }



    CkPem_Dispose(pem);

    }