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++) XML-DSig Add Object Reference with Transforms Specified ExplicitlyDemonstrates how to use the new AddObjectRef2 method to explicitly specify the XML Transforms fragment.
#include <CkXmlW.h> #include <CkXmlDSigGenW.h> #include <CkCertW.h> #include <CkStringBuilderW.h> #include <CkXmlDSigW.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; // Build the following XML to be signed: // <?xml version="1.0" encoding="utf-8"?> // <InitUpload xmlns="http://e-dokumenty.mf.gov.pl"> // <DocumentType>JPK</DocumentType> // <Version>01.02.01.20160617</Version> // <EncryptionKey algorithm="RSA" encoding="Base64" mode="ECB" padding="PKCS#1">xxxx</EncryptionKey> // <DocumentList> // <Document> // <FormCode schemaVersion="1-1" systemCode="JPK_VAT (3)">JPK_VAT</FormCode> // <FileName>JPK_VAT_3_v1-1_20181201.xml</FileName> // <ContentLength>8736</ContentLength> // <HashValue algorithm="SHA-256" encoding="Base64">JEDI1pItwh6dj/Xx1uts/x61qnjZ4DLHpkZMhmf1oKQ=</HashValue> // <FileSignatureList filesNumber="1"> // <Packaging> // <SplitZip mode="zip" type="split"/> // </Packaging> // <Encryption> // <AES block="16" mode="CBC" padding="PKCS#7" size="256"> // <IV bytes="16" encoding="Base64">z64oN9zXHt1+S3XACRSCYw==</IV> // </AES> // </Encryption> // <FileSignature> // <OrdinalNumber>1</OrdinalNumber> // <FileName>JPK_VAT_3_v1-1_20181201-000.xml.zip.aes</FileName> // <ContentLength>16</ContentLength> // <HashValue algorithm="MD5" encoding="Base64">5MX0q1935fvMjLFV7E1yDw==</HashValue> // </FileSignature> // </FileSignatureList> // </Document> // </DocumentList> // </InitUpload> // Use this online tool to generate code from sample XML: // Generate Code to Create XML CkXmlW xmlToSign; xmlToSign.put_Tag(L"InitUpload"); xmlToSign.AddAttribute(L"xmlns",L"http://e-dokumenty.mf.gov.pl"); xmlToSign.UpdateChildContent(L"DocumentType",L"JPK"); xmlToSign.UpdateChildContent(L"Version",L"01.02.01.20160617"); xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"algorithm",L"RSA"); xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"encoding",L"Base64"); xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"mode",L"ECB"); xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"padding",L"PKCS#1"); xmlToSign.UpdateChildContent(L"EncryptionKey",L"xxxx"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"schemaVersion",L"1-1"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"systemCode",L"JPK_VAT (3)"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FormCode",L"JPK_VAT"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileName",L"JPK_VAT_3_v1-1_20181201.xml"); xmlToSign.UpdateChildContent(L"DocumentList|Document|ContentLength",L"8736"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"algorithm",L"SHA-256"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"encoding",L"Base64"); xmlToSign.UpdateChildContent(L"DocumentList|Document|HashValue",L"JEDI1pItwh6dj/Xx1uts/x61qnjZ4DLHpkZMhmf1oKQ="); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList",true,L"filesNumber",L"1"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"mode",L"zip"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"type",L"split"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"block",L"16"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"mode",L"CBC"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"padding",L"PKCS#7"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"size",L"256"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"bytes",L"16"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"encoding",L"Base64"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",L"z64oN9zXHt1+S3XACRSCYw=="); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber",L"1"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|FileName",L"JPK_VAT_3_v1-1_20181201-000.xml.zip.aes"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",L"16"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"algorithm",L"MD5"); xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"encoding",L"Base64"); xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",L"5MX0q1935fvMjLFV7E1yDw=="); CkXmlDSigGenW gen; gen.put_SigLocation(L"InitUpload"); gen.put_SigLocationMod(0); gen.put_SigId(L"id-1234"); gen.put_SigNamespacePrefix(L"ds"); gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#"); gen.put_SignedInfoCanonAlg(L"EXCL_C14N"); gen.put_SignedInfoDigestMethod(L"sha256"); // Create an Object to be added to the Signature. // <xades:QualifyingProperties Target="#id-1234" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"> // <xades:SignedProperties Id="xades-id-1234"> // <xades:SignedSignatureProperties> // <xades:SigningTime>TO BE GENERATED BY CHILKAT</xades:SigningTime> // <xades:SigningCertificate> // <xades:Cert> // <xades:CertDigest> // <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> // <ds:DigestValue>TO BE GENERATED BY CHILKAT</ds:DigestValue> // </xades:CertDigest> // <xades:IssuerSerial> // <ds:X509IssuerName>TO BE GENERATED BY CHILKAT</ds:X509IssuerName> // <ds:X509SerialNumber>TO BE GENERATED BY CHILKAT</ds:X509SerialNumber> // </xades:IssuerSerial> // </xades:Cert> // </xades:SigningCertificate> // </xades:SignedSignatureProperties> // <xades:SignedDataObjectProperties> // <xades:DataObjectFormat ObjectReference="#r-id-1"> // <xades:MimeType>text/xml</xades:MimeType> // </xades:DataObjectFormat> // </xades:SignedDataObjectProperties> // </xades:SignedProperties> // </xades:QualifyingProperties> CkXmlW object1; object1.put_Tag(L"xades:QualifyingProperties"); object1.AddAttribute(L"Target",L"#id-1234"); object1.AddAttribute(L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#"); object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"xades-id-1234"); // Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1 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/2000/09/xmldsig#sha1"); 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"); object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",true,L"ObjectReference",L"#r-id-1"); object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"text/xml"); gen.AddObject(L"",object1.getXml(),L"",L""); // -------- Reference 1 -------- // Build the following Transforms fragment: // <ds:Transforms> // <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> // <ds:XPath>not(ancestor-or-self::ds:Signature)</ds:XPath> // </ds:Transform> // <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> // </ds:Transforms> CkXmlW xml1; xml1.put_Tag(L"ds:Transforms"); xml1.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/TR/1999/REC-xpath-19991116"); xml1.UpdateChildContent(L"ds:Transform|ds:XPath",L"not(ancestor-or-self::ds:Signature)"); xml1.UpdateAttrAt(L"ds:Transform[1]",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); gen.AddSameDocRef2(L"",L"sha256",xml1,L""); gen.SetRefIdAttr(L"",L"r-id-1"); // -------- Reference 2 -------- // Build the following Transforms fragment: // <ds:Transforms> // <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> // </ds:Transforms> CkXmlW xml2; xml2.put_Tag(L"ds:Transforms"); xml2.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); gen.AddObjectRef2(L"xades-id-1234",L"sha256",xml2,L"http://uri.etsi.org/01903#SignedProperties"); // 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"); // Load XML to be signed... CkStringBuilderW sbXml; xmlToSign.GetXmlSb(sbXml); gen.put_Behaviors(L"IndentedSignature"); // 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"qa_output/signedXml.xml",L"utf-8",false); wprintf(L"%s\n",sbXml.getAsString()); // ---------------------------------------- // Verify the signatures we just produced... CkXmlDSigW verifier; success = verifier.LoadSignatureSb(sbXml); if (success != true) { wprintf(L"%s\n",verifier.lastErrorText()); return; } int numSigs = verifier.get_NumSignatures(); int verifyIdx = 0; while (verifyIdx < numSigs) { verifier.put_Selector(verifyIdx); bool verified = verifier.VerifySignature(true); if (verified != true) { wprintf(L"%s\n",verifier.lastErrorText()); return; } verifyIdx = verifyIdx + 1; } wprintf(L"All signatures were successfully verified.\n"); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.