Unicode C
Unicode C
SII Chile - FRMA Signature Computation and Add to XML
See more XML Digital Signatures Examples
Compute the FRMA signature of a <DA> element enclosed inside a <CAF> element of the XML to be signed.Chilkat Unicode C Downloads
#include <C_CkXmlW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkRsaW.h>
void ChilkatSample(void)
{
BOOL success;
HCkXmlW xml;
HCkXmlW daXml;
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 FRMT 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>
// ... The FRMA will be added here ...
// </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_0.xml");
if (success == FALSE) {
wprintf(L"Failed to load initial XML file.\n");
CkXmlW_Dispose(xml);
return;
}
// Get a reference to the "DA" element
daXml = CkXmlW_FindChild(xml,L"Documento|TED|DD|CAF|DA");
if (CkXmlW_getLastMethodSuccess(xml) == FALSE) {
wprintf(L"Failed to find DA element\n");
CkXmlW_Dispose(xml);
return;
}
// We need to get the "flattened" DA 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(daXml,TRUE);
CkXmlW_putEmitXmlDecl(daXml,FALSE);
CkXmlW_GetXmlSb(daXml,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 FRMA signature element to the XML.
CkXmlW_UpdateChildContent(xml,L"Documento|TED|DD|CAF|FRMA",sig);
CkXmlW_UpdateAttrAt(xml,L"Documento|TED|DD|CAF|FRMA",TRUE,L"algoritmo",L"SHA1withRSA");
CkXmlW_Dispose(daXml);
// See what we have:
CkXmlW_putEmitCompact(xml,FALSE);
CkXmlW_putEmitXmlDecl(xml,TRUE);
wprintf(L"%s\n",CkXmlW_getXml(xml));
CkXmlW_SaveXml(xml,L"qa_data/xml_dsig/sii_cl/test_1.xml");
CkXmlW_Dispose(xml);
CkStringBuilderW_Dispose(sbFlattened);
CkPrivateKeyW_Dispose(privKey);
CkRsaW_Dispose(rsa);
}