Unicode C
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
#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 xmlToSign;
HCkXmlDSigGenW gen;
HCkXmlW object1;
HCkCertW cert;
HCkStringBuilderW sbXml;
HCkXmlDSigW verifier;
BOOL verified;
success = FALSE;
// Load the XML to be signed.
xmlToSign = CkXmlW_Create();
success = CkXmlW_LoadXmlFile(xmlToSign,L"qa_data/fattura_electronica/docToSign.xml");
if (success == FALSE) {
wprintf(L"%s\n",CkXmlW_lastErrorText(xmlToSign));
CkXmlW_Dispose(xmlToSign);
return;
}
gen = CkXmlDSigGenW_Create();
CkXmlDSigGenW_putSigLocation(gen,L"p:FatturaElettronica");
CkXmlDSigGenW_putSigId(gen,L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGenW_putSigValueId(gen,L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue");
CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N");
CkXmlDSigGenW_putSignedInfoDigestMethod(gen,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.
object1 = CkXmlW_Create();
CkXmlW_putTag(object1,L"xades:QualifyingProperties");
CkXmlW_AddAttribute(object1,L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
CkXmlW_AddAttribute(object1,L"xmlns:xades141",L"http://uri.etsi.org/01903/v1.4.1#");
CkXmlW_AddAttribute(object1,L"Target",L"#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
CkXmlW_UpdateAttrAt(object1,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");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");
CkXmlDSigGenW_AddObject(gen,L"",CkXmlW_getXml(object1),L"",L"");
// -------- Reference 1 --------
CkXmlDSigGenW_putKeyInfoId(gen,L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo");
CkXmlDSigGenW_AddSameDocRef(gen,L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo",L"sha256",L"",L"",L"");
// -------- Reference 2 --------
CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"",L"",L"");
CkXmlDSigGenW_SetRefIdAttr(gen,L"",L"xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0");
// -------- Reference 3 --------
CkXmlDSigGenW_AddObjectRef(gen,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
cert = CkCertW_Create();
// You may provide the PIN here..
CkCertW_putSmartCardPin(cert,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 = CkCertW_LoadFromSmartcard(cert,L"");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkCertW_Dispose(cert);
return;
}
CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
CkXmlDSigGenW_putX509Type(gen,L"Certificate");
// Load XML to be signed...
sbXml = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xmlToSign,sbXml);
CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature,ForceAddEnvelopedSignatureTransform");
// Sign the XML...
success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
if (success == FALSE) {
wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
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",FALSE);
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));
// ----------------------------------------
// Verify the signature we just produced...
verifier = CkXmlDSigW_Create();
success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
if (success == FALSE) {
wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
verified = CkXmlDSigW_VerifySignature(verifier,TRUE);
if (verified != TRUE) {
wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
wprintf(L"This signature was successfully verified.\n");
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
}