Sample code for 30+ languages & platforms
Unicode C

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkXmlDSigW.h>
#include <C_CkStringArrayW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbXml;
    HCkXmlDSigW dsig;
    int i;
    HCkStringArrayW saCerts;
    HCkCertW cert;
    BOOL bVerifyReferenceDigests;
    BOOL bVerified;
    int j;

    success = FALSE;

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

    sbXml = CkStringBuilderW_Create();

    // Load XML containing one or more signatures.
    success = CkStringBuilderW_LoadFile(sbXml,L"qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml",L"utf-8");
    if (success == FALSE) {
        wprintf(L"Failed to load the XML file..\n");
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    dsig = CkXmlDSigW_Create();

    // First load the XML containing the signatures to be verified.
    // Note that this particular Signature already contains the RSA public key that will be used
    // for verification.
    success = CkXmlDSigW_LoadSignatureSb(dsig,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(dsig));
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(dsig);
        return;
    }

    // For each signature, verify and also get the certificate(s) contained within each Signature.
    i = 0;
    saCerts = CkStringArrayW_Create();
    cert = CkCertW_Create();

    wprintf(L"numSignatures = %d\n",CkXmlDSigW_getNumSignatures(dsig));

    while (i < CkXmlDSigW_getNumSignatures(dsig)) {
        // Select the Nth signature by setting the Selector property.
        CkXmlDSigW_putSelector(dsig,i);

        bVerifyReferenceDigests = TRUE;
        bVerified = CkXmlDSigW_VerifySignature(dsig,bVerifyReferenceDigests);
        wprintf(L"Signature %d verified = %d\n",i + 1,bVerified);

        // Get the certificates embedded in this signature.
        CkStringArrayW_Clear(saCerts);
        success = CkXmlDSigW_GetCerts(dsig,saCerts);
        if (success == TRUE) {
            j = 0;
            while (j < CkStringArrayW_getCount(saCerts)) {
                success = CkCertW_LoadFromBase64(cert,CkStringArrayW_getString(saCerts,j));
                if (success == TRUE) {
                    wprintf(L"    %s\n",CkCertW_subjectDN(cert));
                }

                j = j + 1;
            }

        }

        i = i + 1;
    }



    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigW_Dispose(dsig);
    CkStringArrayW_Dispose(saCerts);
    CkCertW_Dispose(cert);

    }