Sample code for 30+ languages & platforms
Unicode C

SII Chile - FRMT Signature Computation and Add to XML

See more XML Digital Signatures Examples

Compute the FRMT signature and add to the XML. This is the RSA signature of the SHA-1 digest of the "flattened" DD element.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkRsaW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    HCkXmlW ddXml;
    HCkStringBuilderW sbFlattened;
    HCkPrivateKeyW privKey;
    HCkRsaW rsa;
    const wchar_t *sig;

    success = FALSE;

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

    // Also see:  Compute the FRMA Signature and Add to XML

    xml = CkXmlW_Create();

    // Load the unsigned XML that contains the following:

    // <DTE version="1.0">
    //   <Documento ID="F60T33">
    //         <TED version="1.0">
    //             <DD>
    // 		...
    //                 <CAF version="1.0">
    //                     <DA>
    // 			...
    //                     </DA>
    //                     <FRMA algoritmo="SHA1withRSA">...</FRMA>
    //                 </CAF>
    //                 ...
    //             </DD>
    //             ... The FRMT will be added here in another example ...
    //         </TED>
    //   </Documento>
    // </DTE>

    success = CkXmlW_LoadXmlFile(xml,L"qa_data/xml_dsig/sii_cl/test_1.xml");
    if (success == FALSE) {
        wprintf(L"Failed to load initial XML file.\n");
        CkXmlW_Dispose(xml);
        return;
    }

    // Get a reference to the "DD" element
    ddXml = CkXmlW_FindChild(xml,L"Documento|TED|DD");
    if (CkXmlW_getLastMethodSuccess(xml) == FALSE) {
        wprintf(L"Failed to find DD element\n");
        CkXmlW_Dispose(xml);
        return;
    }

    //  We need to get the "flattened" DD XML where:
    //    - No whitespace between elements.
    //    - The 5 pre-defined entities are converted.
    //    - The text is encoded in the ISO-8859-1 character set (Latin-1), 
    sbFlattened = CkStringBuilderW_Create();
    CkXmlW_putEmitCompact(ddXml,TRUE);
    CkXmlW_putEmitXmlDecl(ddXml,FALSE);
    CkXmlW_GetXmlSb(ddXml,sbFlattened);

    // Compute the SHA-1 message digest of the iso-8859-1 byte representation, 
    // and sign it with our RSA private key, getting the result in base64 format.

    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadAnyFormatFile(privKey,L"qa_data/rsa/rsaPrivKey_pkcs8.pem",L"");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkXmlW_Dispose(xml);
        CkStringBuilderW_Dispose(sbFlattened);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    rsa = CkRsaW_Create();
    CkRsaW_UsePrivateKey(rsa,privKey);

    CkRsaW_putEncodingMode(rsa,L"base64");
    CkRsaW_putCharset(rsa,L"iso-8859-1");
    sig = CkRsaW_signStringENC(rsa,CkStringBuilderW_getAsString(sbFlattened),L"sha1");

    // Add the FRMT signature element to the XML.
    CkXmlW_UpdateChildContent(xml,L"Documento|TED|FRMT",sig);
    CkXmlW_UpdateAttrAt(xml,L"Documento|TED|FRMT",TRUE,L"algoritmo",L"SHA1withRSA");

    CkXmlW_Dispose(ddXml);

    // See what we have:
    CkXmlW_putEmitCompact(xml,FALSE);
    CkXmlW_putEmitXmlDecl(xml,TRUE);
    wprintf(L"%s\n",CkXmlW_getXml(xml));


    CkXmlW_Dispose(xml);
    CkStringBuilderW_Dispose(sbFlattened);
    CkPrivateKeyW_Dispose(privKey);
    CkRsaW_Dispose(rsa);

    }