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
(DataFlex) Signed Zip as Base64 with XAdES-BESThis example is to help companies implement a solution for sending XAdES-BES to the Polish government for reporting purposes. Specifically: Przed podpisaniem deklaracja zbiorcza (PIT-11Z, PIT-8CZ, PIT-40Z, PIT-RZ) musi zostać The example demonstrates the following:
This example will also show the reverse:
Use ChilkatAx-win32.pkg Procedure Test Variant vSbXml ToZip Handle hoSbXmlToZip Boolean iSuccess Handle hoZip Variant vEntry Handle hoEntry Variant vBdZip Handle hoBdZip Handle hoGen Handle hoObject1 Variant vCert Handle hoCert Variant vSbXml Handle hoSbXml Handle hoVerifier Boolean iVerified Handle hoXml String sStrZipBase64 String sOrigXml String sTemp1 Integer iTemp1 Boolean bTemp1 // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Zip the PIT-11Z.xml to create PIT-11Z.zip (not as a .zip file, but in-memory). Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXmlToZip If (Not(IsComObjectCreated(hoSbXmlToZip))) Begin Send CreateComObject of hoSbXmlToZip End Get ComLoadFile Of hoSbXmlToZip "qa_data/xml/PIT-11Z.xml" "utf-8" To iSuccess If (iSuccess <> True) Begin Showln "Failed to load the XML to be zipped." Procedure_Return End Get Create (RefClass(cComChilkatZip)) To hoZip If (Not(IsComObjectCreated(hoZip))) Begin Send CreateComObject of hoZip End // Initialize the zip object. No file is created in this call. // It should always return True. Get ComNewZip Of hoZip "PIT-11Z.zip" To iSuccess // Add the XML to be zipped. Get ComGetAsString Of hoSbXmlToZip To sTemp1 Get ComAppendString Of hoZip "PIT-11Z.xml" sTemp1 To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Send Destroy of hoEntry // Write the zip to a BinData object. Get Create (RefClass(cComChilkatBinData)) To hoBdZip If (Not(IsComObjectCreated(hoBdZip))) Begin Send CreateComObject of hoBdZip End Get pvComObject of hoBdZip to vBdZip Get ComWriteBd Of hoZip vBdZip To iSuccess // The contents of the bdZip will be retrieved in base64 format when needed below.. Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen If (Not(IsComObjectCreated(hoGen))) Begin Send CreateComObject of hoGen End Set ComSigLocation Of hoGen To "" Set ComSigLocationMod Of hoGen To 0 Set ComSigId Of hoGen To "Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19" Set ComSigNamespacePrefix Of hoGen To "ds" Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#" Set ComSigValueId Of hoGen To "SignatureValue_2a8df7f8-b958-40cc-83f6-edb53b837347_52" Set ComSignedInfoId Of hoGen To "SignedInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_41" Set ComSignedInfoCanonAlg Of hoGen To "C14N" Set ComSignedInfoDigestMethod Of hoGen To "sha1" // Set the KeyInfoId before adding references.. Set ComKeyInfoId Of hoGen To "KeyInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_24" // Create an Object to be added to the Signature. Get Create (RefClass(cComChilkatXml)) To hoObject1 If (Not(IsComObjectCreated(hoObject1))) Begin Send CreateComObject of hoObject1 End Set ComTag Of hoObject1 To "xades:QualifyingProperties" Get ComAddAttribute Of hoObject1 "xmlns:xades" "http://uri.etsi.org/01903/v1.3.2#" To iSuccess Get ComAddAttribute Of hoObject1 "Id" "QualifyingProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_43" To iSuccess Get ComAddAttribute Of hoObject1 "Target" "#Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19" To iSuccess Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties" True "Id" "SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e" To iSuccess Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties" True "Id" "SignedSignatureProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_0a" To iSuccess // Chilkat will replace the strings "TO BE GENERATED BY CHILKAT" with actual values when the signature is created. Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime" "TO BE GENERATED BY CHILKAT" // 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 Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod" True "Algorithm" "http://www.w3.org/2000/09/xmldsig#sha1" To iSuccess Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue" "TO BE GENERATED BY CHILKAT" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName" "TO BE GENERATED BY CHILKAT" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber" "TO BE GENERATED BY CHILKAT" Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties" True "Id" "SignedDataObjectProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4b" To iSuccess Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat" True "ObjectReference" "#Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27" To iSuccess Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description" "MIME-Version: 1.0" + (character(13)) + (character(10)) + "Content-Type: application/zip" + (character(13)) + (character(10)) + "Content-Transfer-Encoding: binary" + (character(13)) + (character(10)) + "Content-Disposition: filename="PIT-11Z.zip"" Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier" True "Qualifier" "OIDAsURI" To iSuccess Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier" "http://www.certum.pl/OIDAsURI/signedFile/1.2.616.1.113527.3.1.1.3.1" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Description" "Opis formatu dokumentu oraz jego pelna nazwa" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:DocumentationReferences|xades:DocumentationReference" "http://www.certum.pl/OIDAsURI/signedFile.pdf" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType" "application/zip" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Identifier" "http://uri.etsi.org/01903/v1.2.2#ProofOfApproval" Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:AllSignedDataObjects" "" Get ComUpdateAttrAt Of hoObject1 "xades:UnsignedProperties" True "Id" "UnsignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_55" To iSuccess // Emit XML in compact (single-line) format to avoid whitespace problems. Set ComEmitCompact Of hoObject1 To True Get ComGetXml Of hoObject1 To sTemp1 Get ComAddObject Of hoGen "" sTemp1 "" "" To iSuccess // Create an Object to be added to the Signature. // This is where we add the base64 representation of the PIT-11Z.zip Get ComGetEncoded Of hoBdZip "base64" To sTemp1 Get ComAddObject Of hoGen "Object1_2a8df7f8-b958-40cc-83f6-edb53b837347" sTemp1 "" "http://www.w3.org/2000/09/xmldsig#base64" To iSuccess // -------- Reference 1 -------- Get ComAddObjectRef Of hoGen "Object1_2a8df7f8-b958-40cc-83f6-edb53b837347" "sha1" "C14N_WithComments" "" "" To iSuccess Get ComSetRefIdAttr Of hoGen "Object1_2a8df7f8-b958-40cc-83f6-edb53b837347" "Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27" To iSuccess // -------- Reference 2 -------- Get ComAddObjectRef Of hoGen "SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e" "sha1" "" "" "http://uri.etsi.org/01903#SignedProperties" To iSuccess Get ComSetRefIdAttr Of hoGen "SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e" "SignedProperties-Reference_2a8df7f8-b958-40cc-83f6-edb53b837347_28" To iSuccess // Provide a certificate + private key. (PFX password is test123) Get Create (RefClass(cComChilkatCert)) To hoCert If (Not(IsComObjectCreated(hoCert))) Begin Send CreateComObject of hoCert End Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoCert To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoCert to vCert Get ComSetX509Cert Of hoGen vCert True To iSuccess Set ComKeyInfoType Of hoGen To "X509Data" Set ComX509Type Of hoGen To "Certificate" // This will be an enveloping signature where the Signature element // is the XML document root, the signed data is contained within Object // tag(s) within the Signature. // Therefore, pass an empty sbXml to CreateXmlDsigSb. Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml If (Not(IsComObjectCreated(hoSbXml))) Begin Send CreateComObject of hoSbXml End // The Polish government's XmlDSig implementation requires that we reproduce an attribute-sorting error. // (This is an error in the XML canonicalization that is not noticed when both the signature-creation code and signature-verification code use // the same XML canonicalization implementation w/ the bug.) Set ComBehaviors Of hoGen To "AttributeSortingBug,CompactSignedXml" // Sign the XML... Get pvComObject of hoSbXml to vSbXml Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoGen To sTemp1 Showln sTemp1 Procedure_Return End // ----------------------------------------------- // Save the signed XML to a file. Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" False To iSuccess Get ComGetAsString Of hoSbXml To sTemp1 Showln sTemp1 // ---------------------------------------- // Verify the signature we just produced... Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier If (Not(IsComObjectCreated(hoVerifier))) Begin Send CreateComObject of hoVerifier End Get pvComObject of hoSbXml to vSbXml Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Get ComVerifySignature Of hoVerifier True To iVerified If (iVerified <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Showln "This signature was successfully verified." // ------------------------------------ // Finally, let's extract the .zip from the signed XML, and then unzip the original PIT-11Z.xml from the in-memory zip. Get Create (RefClass(cComChilkatXml)) To hoXml If (Not(IsComObjectCreated(hoXml))) Begin Send CreateComObject of hoXml End Get pvComObject of hoSbXml to vSbXml Get ComLoadSb Of hoXml vSbXml True To iSuccess // The base64 image of the PIT-11Z.zip is in the 2nd ds:Object child of the ds:Signature (the ds:Signature is the root element of the signed XML). // (ds:Object[0] would be the 1st ds:Object child. Index 1 is the 2nd ds:Object child.) Get ComGetChildContent Of hoXml "ds:Object[1]" To sStrZipBase64 Get ComClear Of hoBdZip To iSuccess Get ComAppendEncoded Of hoBdZip sStrZipBase64 "base64" To iSuccess Get ComNumBytes Of hoBdZip To iTemp1 If (iTemp1 = 0) Begin Showln "Something went wrong.. we dont' have any data.." Procedure_Return End Get pvComObject of hoBdZip to vBdZip Get ComOpenBd Of hoZip vBdZip To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // Get the 1st file in the zip, which should be the PIT-11Z.xml Get ComGetEntryByIndex Of hoZip 0 To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Get ComLastMethodSuccess Of hoZip To bTemp1 If (bTemp1 <> True) Begin Showln "Zip contains no files..." Procedure_Return End // Get the XML: Get ComUnzipToString Of hoEntry 0 "utf-8" To sOrigXml Showln "Original XML extracted from base64 zip:" Showln sOrigXml Send Destroy of hoEntry End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.