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 <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; HCkXmlW xml1; HCkXmlW xml2; HCkCertW cert; HCkStringBuilderW sbXml; HCkXmlDSigW verifier; int numSigs; int verifyIdx; BOOL verified; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. 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 xmlToSign = CkXmlW_Create(); CkXmlW_putTag(xmlToSign,L"InitUpload"); CkXmlW_AddAttribute(xmlToSign,L"xmlns",L"http://e-dokumenty.mf.gov.pl"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentType",L"JPK"); CkXmlW_UpdateChildContent(xmlToSign,L"Version",L"01.02.01.20160617"); CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"algorithm",L"RSA"); CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"encoding",L"Base64"); CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"mode",L"ECB"); CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"padding",L"PKCS#1"); CkXmlW_UpdateChildContent(xmlToSign,L"EncryptionKey",L"xxxx"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FormCode",TRUE,L"schemaVersion",L"1-1"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FormCode",TRUE,L"systemCode",L"JPK_VAT (3)"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FormCode",L"JPK_VAT"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileName",L"JPK_VAT_3_v1-1_20181201.xml"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|ContentLength",L"8736"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|HashValue",TRUE,L"algorithm",L"SHA-256"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|HashValue",TRUE,L"encoding",L"Base64"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|HashValue",L"JEDI1pItwh6dj/Xx1uts/x61qnjZ4DLHpkZMhmf1oKQ="); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList",TRUE,L"filesNumber",L"1"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",TRUE,L"mode",L"zip"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",TRUE,L"type",L"split"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"block",L"16"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"mode",L"CBC"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"padding",L"PKCS#7"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"size",L"256"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",TRUE,L"bytes",L"16"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",TRUE,L"encoding",L"Base64"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",L"z64oN9zXHt1+S3XACRSCYw=="); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber",L"1"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|FileName",L"JPK_VAT_3_v1-1_20181201-000.xml.zip.aes"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",L"16"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",TRUE,L"algorithm",L"MD5"); CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",TRUE,L"encoding",L"Base64"); CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",L"5MX0q1935fvMjLFV7E1yDw=="); gen = CkXmlDSigGenW_Create(); CkXmlDSigGenW_putSigLocation(gen,L"InitUpload"); CkXmlDSigGenW_putSigLocationMod(gen,0); CkXmlDSigGenW_putSigId(gen,L"id-1234"); CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds"); CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#"); CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N"); CkXmlDSigGenW_putSignedInfoDigestMethod(gen,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> 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"Target",L"#id-1234"); CkXmlW_UpdateAttrAt(object1,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 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/2000/09/xmldsig#sha1"); 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"); CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",TRUE,L"ObjectReference",L"#r-id-1"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"text/xml"); CkXmlDSigGenW_AddObject(gen,L"",CkXmlW_getXml(object1),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> xml1 = CkXmlW_Create(); CkXmlW_putTag(xml1,L"ds:Transforms"); CkXmlW_UpdateAttrAt(xml1,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/TR/1999/REC-xpath-19991116"); CkXmlW_UpdateChildContent(xml1,L"ds:Transform|ds:XPath",L"not(ancestor-or-self::ds:Signature)"); CkXmlW_UpdateAttrAt(xml1,L"ds:Transform[1]",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); CkXmlDSigGenW_AddSameDocRef2(gen,L"",L"sha256",xml1,L""); CkXmlDSigGenW_SetRefIdAttr(gen,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> xml2 = CkXmlW_Create(); CkXmlW_putTag(xml2,L"ds:Transforms"); CkXmlW_UpdateAttrAt(xml2,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); CkXmlDSigGenW_AddObjectRef2(gen,L"xades-id-1234",L"sha256",xml2,L"http://uri.etsi.org/01903#SignedProperties"); // Provide a certificate + private key. (PFX password is test123) cert = CkCertW_Create(); success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); 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"); // Sign the XML... success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); 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 signatures we just produced... verifier = CkXmlDSigW_Create(); success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); return; } numSigs = CkXmlDSigW_getNumSignatures(verifier); verifyIdx = 0; while (verifyIdx < numSigs) { CkXmlDSigW_putSelector(verifier,verifyIdx); verified = CkXmlDSigW_VerifySignature(verifier,TRUE); if (verified != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); return; } verifyIdx = verifyIdx + 1; } wprintf(L"All signatures were successfully verified.\n"); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkXmlW_Dispose(xml1); CkXmlW_Dispose(xml2); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.