Sample code for 30+ languages & platforms
Unicode C

Examine KeyInfo Certificate in XML Signature

See more XML Digital Signatures Examples

This example loads signed XML and gets the signing certificate, assuming the certificate is contained in X509Certificate within the KeyInfo.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlDSigW dsig;
    HCkStringBuilderW sbXml;
    HCkXmlW xmlKeyInfo;
    const wchar_t *certBase64;
    HCkCertW cert;

    success = FALSE;

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

    dsig = CkXmlDSigW_Create();
    sbXml = CkStringBuilderW_Create();

    success = CkStringBuilderW_LoadFile(sbXml,L"c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml",L"utf-8");
    if (success != TRUE) {
        wprintf(L"Failed to load XML file.\n");
        CkXmlDSigW_Dispose(dsig);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    success = CkXmlDSigW_LoadSignatureSb(dsig,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(dsig));
        CkXmlDSigW_Dispose(dsig);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Get the KeyInfo XML.
    xmlKeyInfo = CkXmlDSigW_GetKeyInfo(dsig);
    if (CkXmlDSigW_getLastMethodSuccess(dsig) != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(dsig));
        CkXmlDSigW_Dispose(dsig);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    wprintf(L"%s\n",CkXmlW_getXml(xmlKeyInfo));
    wprintf(L"----\n");

    // Assuming the X509Certificate is in the KeyInfo, it will look like this:

    //   <ds:KeyInfo Id="...">
    //     <ds:KeyValue>
    //     ...  
    //     <ds:X509Data>
    //       <ds:X509Certificate>MIIHAz...</ds:X509Certificate>
    //     </ds:X509Data>
    //   </ds:KeyInfo>
    certBase64 = CkXmlW_getChildContent(xmlKeyInfo,L"*:X509Data|*:X509Certificate");
    if (CkXmlW_getLastMethodSuccess(xmlKeyInfo) != TRUE) {
        wprintf(L"No X509Certificate found in the KeyInfo.\n");
        CkXmlDSigW_Dispose(dsig);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Load a certificate object w/ the base64.
    cert = CkCertW_Create();
    success = CkCertW_LoadFromBase64(cert,certBase64);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlDSigW_Dispose(dsig);
        CkStringBuilderW_Dispose(sbXml);
        CkCertW_Dispose(cert);
        return;
    }

    // Examine the cert..
    wprintf(L"SubjectDN: %s\n",CkCertW_subjectDN(cert));
    wprintf(L"IssuerDN: %s\n",CkCertW_issuerDN(cert));
    wprintf(L"SerialNumber as Decimal: %s\n",CkCertW_serialDecimal(cert));

    CkXmlW_Dispose(xmlKeyInfo);


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

    }