Sample code for 30+ languages & platforms
Unicode C++

Create XAdES using Smart Card or USB Token

See more XAdES Examples

Demonstrates how to create an XAdES signed XML document using a certificate located on a smartcard or USB token.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkXmlDSigW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // Load the XML to be signed.
    CkXmlW xmlToSign;
    success = xmlToSign.LoadXmlFile(L"qa_data/fattura_electronica/docToSign.xml");
    if (success == false) {
        wprintf(L"%s\n",xmlToSign.lastErrorText());
        return;
    }

    CkXmlDSigGenW gen;

    gen.put_SigLocation(L"p:FatturaElettronica");
    gen.put_SigId(L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
    gen.put_SigNamespacePrefix(L"ds");
    gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
    gen.put_SigValueId(L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue");
    gen.put_SignedInfoCanonAlg(L"C14N");
    gen.put_SignedInfoDigestMethod(L"sha256");

    // Create an Object to be added to the Signature.
    // Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
    // when the XML is signed.
    CkXmlW object1;
    object1.put_Tag(L"xades:QualifyingProperties");
    object1.AddAttribute(L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
    object1.AddAttribute(L"xmlns:xades141",L"http://uri.etsi.org/01903/v1.4.1#");
    object1.AddAttribute(L"Target",L"#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
    object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",true,L"Algorithm",L"http://www.w3.org/2001/04/xmlenc#sha256");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");

    gen.AddObject(L"",object1.getXml(),L"",L"");

    // -------- Reference 1 --------
    gen.put_KeyInfoId(L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo");
    gen.AddSameDocRef(L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo",L"sha256",L"",L"",L"");

    // -------- Reference 2 --------
    gen.AddSameDocRef(L"",L"sha256",L"",L"",L"");
    gen.SetRefIdAttr(L"",L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0");

    // -------- Reference 3 --------
    gen.AddObjectRef(L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops",L"sha256",L"",L"",L"http://uri.etsi.org/01903#SignedProperties");

    // ----------------------------------------------------------------
    //  Load a certificate that has been pre-installed on the Windows system
    //  This includes certificates on smartcards and USB tokens
    CkCertW cert;

    //  You may provide the PIN here..
    cert.put_SmartCardPin(L"000000");

    // Load the certificate on the smartcard currently in the reader (or on the USB token).
    // Pass an empty string to allow Chilkat to automatically choose the CSP (Cryptographi Service Provider).
    // See Load Certificate on Smartcard for information about explicitly selecting a particular CSP.
    success = cert.LoadFromSmartcard(L"");
    if (success == false) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    gen.SetX509Cert(cert,true);
    gen.put_KeyInfoType(L"X509Data");
    gen.put_X509Type(L"Certificate");

    // Load XML to be signed...
    CkStringBuilderW sbXml;
    xmlToSign.GetXmlSb(sbXml);

    gen.put_Behaviors(L"IndentedSignature,ForceAddEnvelopedSignatureTransform");

    // Sign the XML...
    success = gen.CreateXmlDSigSb(sbXml);
    if (success == false) {
        wprintf(L"%s\n",gen.lastErrorText());
        return;
    }

    // Save the signed XMl to a file.
    success = sbXml.WriteFile(L"qa_output/signedXml.xml",L"utf-8",false);

    wprintf(L"%s\n",sbXml.getAsString());

    // ----------------------------------------
    // Verify the signature we just produced...
    CkXmlDSigW verifier;
    success = verifier.LoadSignatureSb(sbXml);
    if (success == false) {
        wprintf(L"%s\n",verifier.lastErrorText());
        return;
    }

    bool verified = verifier.VerifySignature(true);
    if (verified != true) {
        wprintf(L"%s\n",verifier.lastErrorText());
        return;
    }

    wprintf(L"This signature was successfully verified.\n");
    }