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) Create XAdES-T Signed XMLSee more XAdES ExamplesThis example signs XML using the XAdES-T profile. XAdES-T is a profile within the XAdES standard that adds support for secure timestamping of signatures. Secure timestamping involves adding a timestamp to the signature, indicating the exact time when the signature was applied. Timestamping enhances the long-term validity of signatures by providing evidence that the signature existed at a specific point in time, even if the signer's certificate has expired or been revoked. XAdES-T signatures include elements for embedding timestamp data within the XML signature, along with information about the timestamp authority and the timestamp verification process. XAdES-T signatures are suitable for scenarios where long-term validity and integrity of signatures are essential, such as in legal and regulatory contexts where archived documents may need to be validated years or decades later.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoXmlToSign Handle hoGen Handle hoObject1 Handle hoObject2 Variant vCert Handle hoCert Variant vJsonTsa Handle hoJsonTsa Variant vSbXml Handle hoSbXml Handle hoVerifier Integer iNumSigs Integer iVerifyIdx Boolean iVerified String sTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Move True To iSuccess // Create the XML to be signed... // Use this online tool to generate code from sample XML: // Generate Code to Create XML // <?xml version="1.0" encoding="UTF-8"?> // <es:Dossier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://uri.etsi.org/01903/v1.3.2#" xmlns:es="https://www.microsec.hu/ds/e-szigno30#" xsi:schemaLocation="https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd"> // <es:DossierProfile Id="PObject0" OBJREF="Object0"> // <es:Title>e-akta.es3</es:Title> // <es:E-category>electronic dossier</es:E-category> // <es:CreationDate>2022-12-02T07:55:16Z</es:CreationDate> // </es:DossierProfile> // <es:Documents Id="Object0"/> // </es:Dossier> Get Create (RefClass(cComChilkatXml)) To hoXmlToSign If (Not(IsComObjectCreated(hoXmlToSign))) Begin Send CreateComObject of hoXmlToSign End Set ComTag Of hoXmlToSign To "es:Dossier" Get ComAddAttribute Of hoXmlToSign "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess Get ComAddAttribute Of hoXmlToSign "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#" To iSuccess Get ComAddAttribute Of hoXmlToSign "xmlns" "http://uri.etsi.org/01903/v1.3.2#" To iSuccess Get ComAddAttribute Of hoXmlToSign "xmlns:es" "https://www.microsec.hu/ds/e-szigno30#" To iSuccess Get ComAddAttribute Of hoXmlToSign "xsi:schemaLocation" "https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "es:DossierProfile" True "Id" "PObject0" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "es:DossierProfile" True "OBJREF" "Object0" To iSuccess Send ComUpdateChildContent To hoXmlToSign "es:DossierProfile|es:Title" "e-akta.es3" Send ComUpdateChildContent To hoXmlToSign "es:DossierProfile|es:E-category" "electronic dossier" Send ComUpdateChildContent To hoXmlToSign "es:DossierProfile|es:CreationDate" "2022-12-02T07:55:16Z" Get ComUpdateAttrAt Of hoXmlToSign "es:Documents" True "Id" "Object0" To iSuccess Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen If (Not(IsComObjectCreated(hoGen))) Begin Send CreateComObject of hoGen End Set ComSigLocation Of hoGen To "es:Dossier" Set ComSigLocationMod Of hoGen To 0 Set ComSigId Of hoGen To "S9fe8096e-2cac-415d-9222-f6cf2ecb314b" Set ComSigValueId Of hoGen To "VS9fe8096e-2cac-415d-9222-f6cf2ecb314b" Set ComSignedInfoId Of hoGen To "SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b" Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N" Set ComSignedInfoDigestMethod Of hoGen To "sha256" // Set the KeyInfoId before adding references.. Set ComKeyInfoId Of hoGen To "KS9fe8096e-2cac-415d-9222-f6cf2ecb314b" // 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 "es:SignatureProfile" Get ComAddAttribute Of hoObject1 "Id" "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Get ComAddAttribute Of hoObject1 "OBJREF" "Object0" To iSuccess Get ComAddAttribute Of hoObject1 "SIGREF" "S9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Get ComAddAttribute Of hoObject1 "SIGREFLIST" "#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Send ComUpdateChildContent To hoObject1 "es:SignerName" "EC Minősített-Tesztelő Péterke" Send ComUpdateChildContent To hoObject1 "es:SDPresented" "false" Send ComUpdateChildContent To hoObject1 "es:Type" "signature" Get ComUpdateAttrAt Of hoObject1 "es:Generator|es:Program" True "name" "e-Szigno" To iSuccess Get ComUpdateAttrAt Of hoObject1 "es:Generator|es:Program" True "version" "3.3.6.8" To iSuccess Get ComUpdateAttrAt Of hoObject1 "es:Generator|es:Device" True "name" "OpenSSL 1.1.1n 15 Mar 2022" To iSuccess Get ComUpdateAttrAt Of hoObject1 "es:Generator|es:Device" True "type" "" To iSuccess Get ComGetXml Of hoObject1 To sTemp1 Get ComAddObject Of hoGen "O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b" sTemp1 "" "" To iSuccess // Create an Object to be added to the Signature. Get Create (RefClass(cComChilkatXml)) To hoObject2 If (Not(IsComObjectCreated(hoObject2))) Begin Send CreateComObject of hoObject2 End Set ComTag Of hoObject2 To "QualifyingProperties" Get ComAddAttribute Of hoObject2 "Target" "#S9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Get ComAddAttribute Of hoObject2 "Id" "QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Get ComUpdateAttrAt Of hoObject2 "SignedProperties" True "Id" "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" To iSuccess Send ComUpdateChildContent To hoObject2 "SignedProperties|SignedSignatureProperties|SigningTime" "TO BE GENERATED BY CHILKAT" Get ComUpdateAttrAt Of hoObject2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod" True "Algorithm" "http://www.w3.org/2001/04/xmlenc#sha256" To iSuccess Send ComUpdateChildContent To hoObject2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue" "TO BE GENERATED BY CHILKAT" Send ComUpdateChildContent To hoObject2 "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2" "TO BE GENERATED BY CHILKAT" Send ComUpdateChildContent To hoObject2 "SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied" "" Send ComUpdateChildContent To hoObject2 "SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole" "tesztelő" // Here we have the EncapsulatedTimestamp found in the unsigned signature properties. Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp" True "Id" "T72cb4961-4326-4319-857a-7cf55e7ef899" To iSuccess Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod" True "Algorithm" "http://www.w3.org/2001/10/xml-exc-c14n#" To iSuccess Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp" True "Id" "ET72cb4961-4326-4319-857a-7cf55e7ef899" To iSuccess Send ComUpdateChildContent To hoObject2 "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp" "TO BE GENERATED BY CHILKAT" Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData" True "xmlns" "http://uri.etsi.org/01903/v1.4.1#" To iSuccess Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues" True "Id" "CV18c7702d-d45b-44bc-853a-a720f41053cd" To iSuccess Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate" True "Id" "EC42db04c8-1422-407b-8c42-189353a55268" To iSuccess Send ComUpdateChildContent To hoObject2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate" "BASE64_CONTENT" Get ComUpdateAttrAt Of hoObject2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]" True "Id" "EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3" To iSuccess Send ComUpdateChildContent To hoObject2 "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]" "BASE64_CONTENT" Get ComGetXml Of hoObject2 To sTemp1 Get ComAddObject Of hoGen "O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b" sTemp1 "" "" To iSuccess // -------- Reference 1 -------- Get ComAddSameDocRef Of hoGen "Object0" "sha256" "EXCL_C14N" "" "" To iSuccess Get ComSetRefIdAttr Of hoGen "Object0" "Re1f816c4-7898-4544-9b41-f4156dc0c528" To iSuccess // -------- Reference 2 -------- Get ComAddObjectRef Of hoGen "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "sha256" "EXCL_C14N" "" "" To iSuccess Get ComSetRefIdAttr Of hoGen "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "Ra873b616-e568-4c38-ae94-27fbff67cc43" To iSuccess // -------- Reference 3 -------- Get ComAddSameDocRef Of hoGen "PObject0" "sha256" "EXCL_C14N" "" "" To iSuccess Get ComSetRefIdAttr Of hoGen "PObject0" "Ra5d85948-5d6a-4914-8c32-242f5d6d9e81" To iSuccess // -------- Reference 4 -------- Get ComAddObjectRef Of hoGen "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "sha256" "EXCL_C14N" "" "http://uri.etsi.org/01903#SignedProperties" To iSuccess Get ComSetRefIdAttr Of hoGen "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b" "Ra7412a43-dc05-4e0a-ac84-e9a070214757" 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" // ------------------------------------------------------------------------------------------- // To have the EncapsulatedTimeStamp automatically added, we only need to do 2 things. // 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. // 2) Specify the TSA URL (Timestamping Authority URL). // Here we specify the TSA URL: // ------------------------------------------------------------------------------------------- Get Create (RefClass(cComChilkatJsonObject)) To hoJsonTsa If (Not(IsComObjectCreated(hoJsonTsa))) Begin Send CreateComObject of hoJsonTsa End Get ComUpdateString Of hoJsonTsa "timestampToken.tsaUrl" "http://timestamp.digicert.com" To iSuccess Get ComUpdateBool Of hoJsonTsa "timestampToken.requestTsaCert" True To iSuccess Get pvComObject of hoJsonTsa to vJsonTsa Get ComSetTsa Of hoGen vJsonTsa To iSuccess // Load XML to be signed... Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml If (Not(IsComObjectCreated(hoSbXml))) Begin Send CreateComObject of hoSbXml End Get pvComObject of hoSbXml to vSbXml Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess Set ComBehaviors Of hoGen To "IndentedSignature,OmitAlreadyDefinedSigNamespace" // 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 "c:/temp/qa_output/signedXml.xml" "utf-8" False To iSuccess Get ComGetAsString Of hoSbXml To sTemp1 Showln sTemp1 // ---------------------------------------- // Verify the signatures 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 ComNumSignatures Of hoVerifier To iNumSigs Move 0 To iVerifyIdx While (iVerifyIdx < iNumSigs) Set ComSelector Of hoVerifier To iVerifyIdx Get ComVerifySignature Of hoVerifier True To iVerified If (iVerified <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Move (iVerifyIdx + 1) To iVerifyIdx Loop Showln "All signatures were successfully verified." End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.