Sample code for 30+ languages & platforms
Unicode C

Sign Bolivia Invoice XML

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature (XmlDSig) for a Bolivia invoice (factura).

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    HCkXmlDSigGenW gen;
    HCkCertW cert;
    HCkStringBuilderW sbXml;
    HCkXmlDSigW verifier;
    int numSigs;
    int verifyIdx;
    BOOL verified;

    success = FALSE;

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

    success = TRUE;

    //  Create the following XML to be signed:

    //  <?xml version="1.0" encoding="UTF-8"?>
    //  <facturaElectronicaCompraVenta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
    //      <cabecera>
    //          <nitEmisor>99999999</nitEmisor>
    //          <razonSocialEmisor>Abc Xyz</razonSocialEmisor>
    //          <municipio>Cochabamba - Cochabamba</municipio>
    //          <telefono>4444444</telefono>
    //          <numeroFactura>417</numeroFactura>
    //          <cuf>ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA</cuf>
    //          <cufd>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</cufd>
    //          <codigoSucursal>0</codigoSucursal>
    //          <direccion>aaa aaa aaa</direccion>
    //          <codigoPuntoVenta>1</codigoPuntoVenta>
    //          <fechaEmision>2023-04-13T22:43:22.417</fechaEmision>
    //          <nombreRazonSocial>xyz</nombreRazonSocial>
    //          <codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
    //          <numeroDocumento>1234567</numeroDocumento>
    //             <complemento xsi:nil="true"/>
    //          <codigoCliente>1234567</codigoCliente>
    //          <codigoMetodoPago>1</codigoMetodoPago>
    //             <numeroTarjeta xsi:nil="true"/>
    //          <montoTotal>22.00</montoTotal>
    //          <montoTotalSujetoIva>22.00</montoTotalSujetoIva>
    //          <codigoMoneda>1</codigoMoneda>
    //          <tipoCambio>1.00</tipoCambio>
    //          <montoTotalMoneda>22.00</montoTotalMoneda>
    //             <montoGiftCard xsi:nil="true"/>
    //          <descuentoAdicional>0.00</descuentoAdicional>
    //             <codigoExcepcion>1</codigoExcepcion>
    //             <cafc xsi:nil="true"/>
    //          <leyenda>Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.</leyenda>
    //          <usuario>nromero</usuario>
    //          <codigoDocumentoSector>1</codigoDocumentoSector>
    //      </cabecera>
    //      <detalle>
    //          <actividadEconomica>620100</actividadEconomica>
    //          <codigoProductoSin>123456</codigoProductoSin>
    //          <codigoProducto>1</codigoProducto>
    //          <descripcion>Economicos</descripcion>
    //          <cantidad>1.00</cantidad>
    //          <unidadMedida>58</unidadMedida>
    //          <precioUnitario>22.00</precioUnitario>
    //          <montoDescuento>0.00</montoDescuento>
    //          <subTotal>22.00</subTotal>
    //             <numeroSerie xsi:nil="true"/>
    //             <numeroImei xsi:nil="true"/>
    //      </detalle>
    //  </facturaElectronicaCompraVenta>

    //  Use this online tool to generate code from sample XML: 
    //  Generate Code to Create XML

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"facturaElectronicaCompraVenta");
    CkXmlW_AddAttribute(xml,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    CkXmlW_AddAttribute(xml,L"xsi:noNamespaceSchemaLocation",L"facturaElectronicaCompraVenta.xsd");
    CkXmlW_UpdateChildContent(xml,L"cabecera|nitEmisor",L"99999999");
    CkXmlW_UpdateChildContent(xml,L"cabecera|razonSocialEmisor",L"Abc Xyz");
    CkXmlW_UpdateChildContent(xml,L"cabecera|municipio",L"Cochabamba - Cochabamba");
    CkXmlW_UpdateChildContent(xml,L"cabecera|telefono",L"4444444");
    CkXmlW_UpdateChildContent(xml,L"cabecera|numeroFactura",L"417");
    CkXmlW_UpdateChildContent(xml,L"cabecera|cuf",L"ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA");
    CkXmlW_UpdateChildContent(xml,L"cabecera|cufd",L"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoSucursal",L"0");
    CkXmlW_UpdateChildContent(xml,L"cabecera|direccion",L"aaa aaa aaa");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoPuntoVenta",L"1");
    CkXmlW_UpdateChildContent(xml,L"cabecera|fechaEmision",L"2023-04-13T22:43:22.417");
    CkXmlW_UpdateChildContent(xml,L"cabecera|nombreRazonSocial",L"xyz");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoTipoDocumentoIdentidad",L"1");
    CkXmlW_UpdateChildContent(xml,L"cabecera|numeroDocumento",L"1234567");
    CkXmlW_UpdateAttrAt(xml,L"cabecera|complemento",TRUE,L"xsi:nil",L"true");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoCliente",L"1234567");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoMetodoPago",L"1");
    CkXmlW_UpdateAttrAt(xml,L"cabecera|numeroTarjeta",TRUE,L"xsi:nil",L"true");
    CkXmlW_UpdateChildContent(xml,L"cabecera|montoTotal",L"22.00");
    CkXmlW_UpdateChildContent(xml,L"cabecera|montoTotalSujetoIva",L"22.00");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoMoneda",L"1");
    CkXmlW_UpdateChildContent(xml,L"cabecera|tipoCambio",L"1.00");
    CkXmlW_UpdateChildContent(xml,L"cabecera|montoTotalMoneda",L"22.00");
    CkXmlW_UpdateAttrAt(xml,L"cabecera|montoGiftCard",TRUE,L"xsi:nil",L"true");
    CkXmlW_UpdateChildContent(xml,L"cabecera|descuentoAdicional",L"0.00");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoExcepcion",L"1");
    CkXmlW_UpdateAttrAt(xml,L"cabecera|cafc",TRUE,L"xsi:nil",L"true");
    CkXmlW_UpdateChildContent(xml,L"cabecera|leyenda",L"Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.");
    CkXmlW_UpdateChildContent(xml,L"cabecera|usuario",L"nromero");
    CkXmlW_UpdateChildContent(xml,L"cabecera|codigoDocumentoSector",L"1");
    CkXmlW_UpdateChildContent(xml,L"detalle|actividadEconomica",L"620100");
    CkXmlW_UpdateChildContent(xml,L"detalle|codigoProductoSin",L"123456");
    CkXmlW_UpdateChildContent(xml,L"detalle|codigoProducto",L"1");
    CkXmlW_UpdateChildContent(xml,L"detalle|descripcion",L"Economicos");
    CkXmlW_UpdateChildContent(xml,L"detalle|cantidad",L"1.00");
    CkXmlW_UpdateChildContent(xml,L"detalle|unidadMedida",L"58");
    CkXmlW_UpdateChildContent(xml,L"detalle|precioUnitario",L"22.00");
    CkXmlW_UpdateChildContent(xml,L"detalle|montoDescuento",L"0.00");
    CkXmlW_UpdateChildContent(xml,L"detalle|subTotal",L"22.00");
    CkXmlW_UpdateAttrAt(xml,L"detalle|numeroSerie",TRUE,L"xsi:nil",L"true");
    CkXmlW_UpdateAttrAt(xml,L"detalle|numeroImei",TRUE,L"xsi:nil",L"true");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"facturaElectronicaCompraVenta");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"C14N_WithComments",L"",L"");

    //  Provide your certificate + private key. (PFX password is test123)
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlW_Dispose(xml);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    CkXmlDSigGenW_putBehaviors(gen,L"EnvelopedTransformFirst");

    //  Load XML to be signed...
    sbXml = CkStringBuilderW_Create();
    CkXmlW_putEmitCompact(xml,TRUE);
    CkXmlW_GetXmlSb(xml,sbXml);

    //  Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xml);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    //  -----------------------------------------------

    //  Save the signed XML to a file.
    success = CkStringBuilderW_WriteFile(sbXml,L"qa_output/signedXml.xml",L"utf-8",TRUE);

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

    //  ----------------------------------------
    //  Verify the signatures we just produced...
    verifier = CkXmlDSigW_Create();
    success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
        CkXmlW_Dispose(xml);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    numSigs = CkXmlDSigW_getNumSignatures(verifier);
    verifyIdx = 0;
    while (verifyIdx < numSigs) {
        CkXmlDSigW_putSelector(verifier,verifyIdx);
        verified = CkXmlDSigW_VerifySignature(verifier,TRUE);
        if (verified != TRUE) {
            wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
            CkXmlW_Dispose(xml);
            CkXmlDSigGenW_Dispose(gen);
            CkCertW_Dispose(cert);
            CkStringBuilderW_Dispose(sbXml);
            CkXmlDSigW_Dispose(verifier);
            return;
        }

        verifyIdx = verifyIdx + 1;
    }

    wprintf(L"All signatures were successfully verified.\n");


    CkXmlW_Dispose(xml);
    CkXmlDSigGenW_Dispose(gen);
    CkCertW_Dispose(cert);
    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigW_Dispose(verifier);

    }