Sample code for 30+ languages & platforms
Unicode C

Verify a .p7m and get Algorithm Information

See more Digital Signatures Examples

Demonstrates how to verify a .p7m and then examine the algorithms used by the signature.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    const wchar_t *inFile;
    const wchar_t *outFile;
    HCkJsonObjectW json;
    int i;
    int count_i;
    const wchar_t *strVal;
    const wchar_t *certSerialNumber;
    const wchar_t *certIssuerCN;
    const wchar_t *certIssuerDN;
    const wchar_t *certDigestAlgOid;
    const wchar_t *certDigestAlgName;
    const wchar_t *contentType;
    const wchar_t *signingTime;
    const wchar_t *messageDigest;
    const wchar_t *signingAlgOid;
    const wchar_t *signerDigest;
    int j;
    int count_j;
    const wchar_t *oid;
    const wchar_t *oidName;
    const wchar_t *der;

    success = FALSE;

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

    crypt = CkCrypt2W_Create();

    inFile = L"qa_data/p7m/brainpoolP256r1.p7m";
    outFile = L"qa_output/something.dat";

    // Verify and extract the signed data.
    success = CkCrypt2W_VerifyP7M(crypt,inFile,outFile);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkCrypt2W_Dispose(crypt);
        return;
    }

    // Examine details about the signature(s)
    json = CkJsonObjectW_Create();
    CkCrypt2W_GetLastJsonData(crypt,json);
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Sample output

    // {
    //   "pkcs7": {
    //     "verify": {
    //       "digestAlgorithms": [
    //         "sha256"
    //       ],
    //       "signerInfo": [
    //         {
    //           "cert": {
    //             "serialNumber": "FFFFE552B302FFFFFF1E34C3ACEB2FFFF",
    //             "issuerCN": "The common name of the cert...",
    //             "issuerDN": "",
    //             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    //             "digestAlgName": "SHA256"
    //           },
    //           "contentType": "1.2.840.113549.1.7.1",
    //           "signingTime": "190409140500Z",
    //           "messageDigest": "lQe9If7vZKFf/NlSYu5Esmlw3phVK/RFsbbb1uH73t8=",
    //           "signingAlgOid": "1.2.840.10045.4.3.2",
    //           "signerDigest": "lQe9If7vZKFf/NlSYu5Esmlw3phVK/RFsbbb1uH73t8=",
    //           "authAttr": [
    //             {
    //               "oid": "1.2.840.113549.1.9.3",
    //               "oidName": "contentType"
    //             },
    //             {
    //               "oid": "1.2.840.113549.1.9.5",
    //               "oidName": "signingTime"
    //             },
    //             {
    //               "oid": "1.2.840.113549.1.9.52",
    //               "oidName": "1.2.840.113549.1.9.52",
    //               "der": "MBs ... AwI="
    //             },
    //             {
    //               "oid": "1.2.840.113549.1.9.4",
    //               "oidName": "messageDigest"
    //             },
    //             {
    //               "oid": "1.2.840.113549.1.9.16.2.47",
    //               "oidName": "signingCertificateV2",
    //               "der": "MCYw .. 7PlQ=="
    //             },
    //             {
    //               "oid": "1.2.840.113549.1.9.20",
    //               "oidName": "1.2.840.113549.1.9.20"
    //             }
    //           ]
    //         }
    //       ]
    //     }
    //   }
    // }

    // Code for parsing the above JSON...

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"pkcs7.verify.digestAlgorithms");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        strVal = CkJsonObjectW_stringOf(json,L"pkcs7.verify.digestAlgorithms[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"pkcs7.verify.signerInfo");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        certSerialNumber = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].cert.serialNumber");
        certIssuerCN = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].cert.issuerCN");
        certIssuerDN = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].cert.issuerDN");
        certDigestAlgOid = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].cert.digestAlgOid");
        certDigestAlgName = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].cert.digestAlgName");
        contentType = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].contentType");
        signingTime = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].signingTime");
        messageDigest = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].messageDigest");
        signingAlgOid = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].signingAlgOid");
        signerDigest = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].signerDigest");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"pkcs7.verify.signerInfo[i].authAttr");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            oid = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].authAttr[j].oid");
            oidName = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].authAttr[j].oidName");
            der = CkJsonObjectW_stringOf(json,L"pkcs7.verify.signerInfo[i].authAttr[j].der");
            j = j + 1;
        }

        i = i + 1;
    }

    wprintf(L"Success!\n");


    CkCrypt2W_Dispose(crypt);
    CkJsonObjectW_Dispose(json);

    }