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
(PureBasic) 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.
IncludeFile "CkJsonObject.pb" IncludeFile "CkXmlDSig.pb" IncludeFile "CkXml.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkCert.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i = 1 ; 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> xmlToSign.i = CkXml::ckCreate() If xmlToSign.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlToSign, "es:Dossier") CkXml::ckAddAttribute(xmlToSign,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") CkXml::ckAddAttribute(xmlToSign,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#") CkXml::ckAddAttribute(xmlToSign,"xmlns","http://uri.etsi.org/01903/v1.3.2#") CkXml::ckAddAttribute(xmlToSign,"xmlns:es","https://www.microsec.hu/ds/e-szigno30#") CkXml::ckAddAttribute(xmlToSign,"xsi:schemaLocation","https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd") CkXml::ckUpdateAttrAt(xmlToSign,"es:DossierProfile",1,"Id","PObject0") CkXml::ckUpdateAttrAt(xmlToSign,"es:DossierProfile",1,"OBJREF","Object0") CkXml::ckUpdateChildContent(xmlToSign,"es:DossierProfile|es:Title","e-akta.es3") CkXml::ckUpdateChildContent(xmlToSign,"es:DossierProfile|es:E-category","electronic dossier") CkXml::ckUpdateChildContent(xmlToSign,"es:DossierProfile|es:CreationDate","2022-12-02T07:55:16Z") CkXml::ckUpdateAttrAt(xmlToSign,"es:Documents",1,"Id","Object0") gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkSigLocation(gen, "es:Dossier") CkXmlDSigGen::setCkSigLocationMod(gen, 0) CkXmlDSigGen::setCkSigId(gen, "S9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXmlDSigGen::setCkSigValueId(gen, "VS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXmlDSigGen::setCkSignedInfoId(gen, "SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "EXCL_C14N") CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256") ; Set the KeyInfoId before adding references.. CkXmlDSigGen::setCkKeyInfoId(gen, "KS9fe8096e-2cac-415d-9222-f6cf2ecb314b") ; Create an Object to be added to the Signature. object1.i = CkXml::ckCreate() If object1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(object1, "es:SignatureProfile") CkXml::ckAddAttribute(object1,"Id","PS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckAddAttribute(object1,"OBJREF","Object0") CkXml::ckAddAttribute(object1,"SIGREF","S9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckAddAttribute(object1,"SIGREFLIST","#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckUpdateChildContent(object1,"es:SignerName","EC Minsített-Tesztel Péterke") CkXml::ckUpdateChildContent(object1,"es:SDPresented","false") CkXml::ckUpdateChildContent(object1,"es:Type","signature") CkXml::ckUpdateAttrAt(object1,"es:Generator|es:Program",1,"name","e-Szigno") CkXml::ckUpdateAttrAt(object1,"es:Generator|es:Program",1,"version","3.3.6.8") CkXml::ckUpdateAttrAt(object1,"es:Generator|es:Device",1,"name","OpenSSL 1.1.1n 15 Mar 2022") CkXml::ckUpdateAttrAt(object1,"es:Generator|es:Device",1,"type","") CkXmlDSigGen::ckAddObject(gen,"O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b",CkXml::ckGetXml(object1),"","") ; Create an Object to be added to the Signature. object2.i = CkXml::ckCreate() If object2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(object2, "QualifyingProperties") CkXml::ckAddAttribute(object2,"Target","#S9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckAddAttribute(object2,"Id","QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckUpdateAttrAt(object2,"SignedProperties",1,"Id","XS9fe8096e-2cac-415d-9222-f6cf2ecb314b") CkXml::ckUpdateChildContent(object2,"SignedProperties|SignedSignatureProperties|SigningTime","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateAttrAt(object2,"SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256") CkXml::ckUpdateChildContent(object2,"SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object2,"SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object2,"SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied","") CkXml::ckUpdateChildContent(object2,"SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole","tesztel") ; Here we have the EncapsulatedTimestamp found in the unsigned signature properties. CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp",1,"Id","T72cb4961-4326-4319-857a-7cf55e7ef899") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp",1,"Id","ET72cb4961-4326-4319-857a-7cf55e7ef899") CkXml::ckUpdateChildContent(object2,"UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData",1,"xmlns","http://uri.etsi.org/01903/v1.4.1#") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|CertificateValues",1,"Id","CV18c7702d-d45b-44bc-853a-a720f41053cd") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate",1,"Id","EC42db04c8-1422-407b-8c42-189353a55268") CkXml::ckUpdateChildContent(object2,"UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate","BASE64_CONTENT") CkXml::ckUpdateAttrAt(object2,"UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]",1,"Id","EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3") CkXml::ckUpdateChildContent(object2,"UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]","BASE64_CONTENT") CkXmlDSigGen::ckAddObject(gen,"O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b",CkXml::ckGetXml(object2),"","") ; -------- Reference 1 -------- CkXmlDSigGen::ckAddSameDocRef(gen,"Object0","sha256","EXCL_C14N","","") CkXmlDSigGen::ckSetRefIdAttr(gen,"Object0","Re1f816c4-7898-4544-9b41-f4156dc0c528") ; -------- Reference 2 -------- CkXmlDSigGen::ckAddObjectRef(gen,"PS9fe8096e-2cac-415d-9222-f6cf2ecb314b","sha256","EXCL_C14N","","") CkXmlDSigGen::ckSetRefIdAttr(gen,"PS9fe8096e-2cac-415d-9222-f6cf2ecb314b","Ra873b616-e568-4c38-ae94-27fbff67cc43") ; -------- Reference 3 -------- CkXmlDSigGen::ckAddSameDocRef(gen,"PObject0","sha256","EXCL_C14N","","") CkXmlDSigGen::ckSetRefIdAttr(gen,"PObject0","Ra5d85948-5d6a-4914-8c32-242f5d6d9e81") ; -------- Reference 4 -------- CkXmlDSigGen::ckAddObjectRef(gen,"XS9fe8096e-2cac-415d-9222-f6cf2ecb314b","sha256","EXCL_C14N","","http://uri.etsi.org/01903#SignedProperties") CkXmlDSigGen::ckSetRefIdAttr(gen,"XS9fe8096e-2cac-415d-9222-f6cf2ecb314b","Ra7412a43-dc05-4e0a-ac84-e9a070214757") ; Provide a certificate + private key. (PFX password is test123) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(object2) CkCert::ckDispose(cert) ProcedureReturn EndIf CkXmlDSigGen::ckSetX509Cert(gen,cert,1) CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data") CkXmlDSigGen::setCkX509Type(gen, "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: ; ------------------------------------------------------------------------------------------- jsonTsa.i = CkJsonObject::ckCreate() If jsonTsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(jsonTsa,"timestampToken.tsaUrl","http://timestamp.digicert.com") CkJsonObject::ckUpdateBool(jsonTsa,"timestampToken.requestTsaCert",1) CkXmlDSigGen::ckSetTsa(gen,jsonTsa) ; Load XML to be signed... sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckGetXmlSb(xmlToSign,sbXml) CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature,OmitAlreadyDefinedSigNamespace") ; Sign the XML... success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(object2) CkCert::ckDispose(cert) CkJsonObject::ckDispose(jsonTsa) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; ----------------------------------------------- ; Save the signed XML to a file. success = CkStringBuilder::ckWriteFile(sbXml,"c:/temp/qa_output/signedXml.xml","utf-8",0) Debug CkStringBuilder::ckGetAsString(sbXml) ; ---------------------------------------- ; Verify the signatures we just produced... verifier.i = CkXmlDSig::ckCreate() If verifier.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml) If success <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(object2) CkCert::ckDispose(cert) CkJsonObject::ckDispose(jsonTsa) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf numSigs.i = CkXmlDSig::ckNumSignatures(verifier) verifyIdx.i = 0 While verifyIdx < numSigs CkXmlDSig::setCkSelector(verifier, verifyIdx) verified.i = CkXmlDSig::ckVerifySignature(verifier,1) If verified <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(object2) CkCert::ckDispose(cert) CkJsonObject::ckDispose(jsonTsa) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf verifyIdx = verifyIdx + 1 Wend Debug "All signatures were successfully verified." CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(object2) CkCert::ckDispose(cert) CkJsonObject::ckDispose(jsonTsa) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.