Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) XAdES using TSA Requiring Client CertificateSee more XML Digital Signatures ExamplesDemonstrates how to create an XMLDSig (XAdES) signed document which includes an EncapsulatedTimestamp using a TSA (TimeStamp Authority) server requiring client certificate authentication. One such TSA is https://www3.postsignum.cz/TSS/TSS_crt/
#include <CkStringBuilderW.h> #include <CkXmlDSigGenW.h> #include <CkXmlW.h> #include <CkCertW.h> #include <CkJsonObjectW.h> #include <CkHttpW.h> void ChilkatSample(void) { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. bool success = true; // Load the XML to be signed. For example, the XML to be signed might contain something like this: // <?xml version="1.0" encoding="utf-8"?> // <TransakcniLogSystemu xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nsess.public.cz/erms_trans/v_01_01" Id="Signature1"> // <TransLogInfo> // <Identifikator>XYZ ABC</Identifikator> // <DatumVzniku>2022-12-20T14:39:02.3625922+01:00</DatumVzniku> // <DatumCasOd>2022-12-20T14:26:26.88</DatumCasOd> // <DatumCasDo>2022-12-20T14:39:02.287</DatumCasDo> // <Software>XYZ</Software> // <VerzeSoftware>2.0.19.32</VerzeSoftware> // </TransLogInfo> // <Udalosti> // <Udalost> // <Poradi>1</Poradi> // ... // Load the XML to be signed from a file. // (XML can be loaded from other source, such as a string variable.) CkStringBuilderW sbXml; success = sbXml.LoadFile(L"xmlToSign.xml",L"utf-8"); CkXmlDSigGenW gen; gen.put_SigLocation(L"TransakcniLogSystemu"); gen.put_SigLocationMod(0); gen.put_SigId(L"SignatureID-Signature1"); gen.put_SigNamespacePrefix(L"ds"); gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#"); gen.put_SignedInfoCanonAlg(L"C14N"); gen.put_SignedInfoDigestMethod(L"sha256"); // Set the KeyInfoId before adding references.. gen.put_KeyInfoId(L"KeyInfoId-Signature-Signature1"); // Create an Object to be added to the Signature. // Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing. // The EncapsulatedTimestamp will be automatically generated. CkXmlW object1; object1.put_Tag(L"xades:QualifyingProperties"); object1.AddAttribute(L"Target",L"#Signature1"); object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"SignedProperties-Signature-Signature1"); object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT"); object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|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:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT"); object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName",L"TO BE GENERATED BY CHILKAT"); object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber",L"TO BE GENERATED BY CHILKAT"); // The EncapsulatedTimestamp will be included in the unsigned properties. object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",true,L"Id",L"signature-timestamp-5561-8212-3316-5191"); object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",true,L"Encoding",L"http://uri.etsi.org/01903/v1.2.2#DER"); object1.UpdateChildContent(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",L"TO BE GENERATED BY CHILKAT"); gen.AddObject(L"XadesObjectId-Signature1",object1.getXml(),L"",L""); // -------- Reference 1 -------- gen.AddObjectRef(L"SignedProperties-Signature-Signature1",L"sha256",L"EXCL_C14N",L"",L"http://uri.etsi.org/01903#SignedProperties"); // -------- Reference 2 -------- gen.AddSameDocRef(L"KeyInfoId-Signature-Signature1",L"sha256",L"EXCL_C14N",L"",L""); gen.SetRefIdAttr(L"KeyInfoId-Signature-Signature1",L"ReferenceKeyInfo"); // -------- Reference 3 -------- gen.AddSameDocRef(L"",L"sha256",L"EXCL_C14N",L"",L""); gen.SetRefIdAttr(L"",L"Reference-Signature1"); // Provide a certificate + private key. (PFX password is test123) CkCertW cert; success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != true) { wprintf(L"%s\n",cert.lastErrorText()); return; } gen.SetX509Cert(cert,true); gen.put_KeyInfoType(L"X509Data"); gen.put_X509Type(L"Certificate"); gen.put_Behaviors(L"IndentedSignature"); // ------------------------------------------------------------------------------------------- // To have the EncapsulatedTimeStamp automatically added... // 1) Add the <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp> // to the unsigned properties. (This was accomplished in the above code.) // 2) Specify the TSA URL (Timestamping Authority URL). // Here we specify the TSA URL: // ------------------------------------------------------------------------------------------- CkJsonObjectW jsonTsa; jsonTsa.UpdateString(L"timestampToken.tsaUrl",L"https://www3.postsignum.cz/TSS/TSS_crt/"); jsonTsa.UpdateBool(L"timestampToken.requestTsaCert",true); gen.SetTsa(jsonTsa); // ------------------------------------------------------------------------------------------- // In this case, the TSA requires client certificate authentication. // To provide your client certificate, the application will instantiate a Chilkat HTTP object, // then set it up with a SSL/TLS client certificate, and then tell the XmlDSigGen object // to use the HTTP object for connections to the TSA server. // ------------------------------------------------------------------------------------------- CkHttpW http; success = http.SetSslClientCertPfx(L"/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx",L"pfxPassword"); if (success != true) { wprintf(L"%s\n",http.lastErrorText()); return; } // Tell the XmlDSigGen object to use the above HTTP object for TSA communications. gen.SetHttpObj(http); // Sign the XML... success = gen.CreateXmlDSigSb(sbXml); if (success != true) { wprintf(L"%s\n",gen.lastErrorText()); return; } // ----------------------------------------------- // Save the signed XML to a file. success = sbXml.WriteFile(L"c:/temp/qa_output/signedXml.xml",L"utf-8",false); wprintf(L"%s\n",sbXml.getAsString()); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.