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
(Visual FoxPro) XAdES using TSA Requiring Client CertificateSee more XML Digital Signatures ExamplesDemonstrates how to create an XMLDSig (XAdES) signed document which includes an EncapsulatedTimestamp using a TSA (TimeStamp Authority) server requiring client certificate authentication. One such TSA is https://www3.postsignum.cz/TSS/TSS_crt/
LOCAL lnSuccess LOCAL loSbXml LOCAL loGen LOCAL loObject1 LOCAL loCert LOCAL loJsonTsa LOCAL loHttp * This example requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. lnSuccess = 1 * Load the XML to be signed. For example, the XML to be signed might contain something like this: * <?xml version="1.0" encoding="utf-8"?> * <TransakcniLogSystemu xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nsess.public.cz/erms_trans/v_01_01" Id="Signature1"> * <TransLogInfo> * <Identifikator>XYZ ABC</Identifikator> * <DatumVzniku>2022-12-20T14:39:02.3625922+01:00</DatumVzniku> * <DatumCasOd>2022-12-20T14:26:26.88</DatumCasOd> * <DatumCasDo>2022-12-20T14:39:02.287</DatumCasDo> * <Software>XYZ</Software> * <VerzeSoftware>2.0.19.32</VerzeSoftware> * </TransLogInfo> * <Udalosti> * <Udalost> * <Poradi>1</Poradi> * ... * Load the XML to be signed from a file. * (XML can be loaded from other source, such as a string variable.) * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSbXml = CreateObject('Chilkat.StringBuilder') lnSuccess = loSbXml.LoadFile("xmlToSign.xml","utf-8") * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.XmlDSigGen') loGen = CreateObject('Chilkat.XmlDSigGen') loGen.SigLocation = "TransakcniLogSystemu" loGen.SigLocationMod = 0 loGen.SigId = "SignatureID-Signature1" loGen.SigNamespacePrefix = "ds" loGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" loGen.SignedInfoCanonAlg = "C14N" loGen.SignedInfoDigestMethod = "sha256" * Set the KeyInfoId before adding references.. loGen.KeyInfoId = "KeyInfoId-Signature-Signature1" * Create an Object to be added to the Signature. * Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing. * The EncapsulatedTimestamp will be automatically generated. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Xml') loObject1 = CreateObject('Chilkat.Xml') loObject1.Tag = "xades:QualifyingProperties" loObject1.AddAttribute("Target","#Signature1") loObject1.UpdateAttrAt("xades:SignedProperties",1,"Id","SignedProperties-Signature-Signature1") loObject1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT") loObject1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256") loObject1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT") loObject1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT") loObject1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT") * The EncapsulatedTimestamp will be included in the unsigned properties. loObject1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",1,"Id","signature-timestamp-5561-8212-3316-5191") loObject1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") loObject1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",1,"Encoding","http://uri.etsi.org/01903/v1.2.2#DER") loObject1.UpdateChildContent("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp","TO BE GENERATED BY CHILKAT") loGen.AddObject("XadesObjectId-Signature1",loObject1.GetXml(),"","") * -------- Reference 1 -------- loGen.AddObjectRef("SignedProperties-Signature-Signature1","sha256","EXCL_C14N","","http://uri.etsi.org/01903#SignedProperties") * -------- Reference 2 -------- loGen.AddSameDocRef("KeyInfoId-Signature-Signature1","sha256","EXCL_C14N","","") loGen.SetRefIdAttr("KeyInfoId-Signature-Signature1","ReferenceKeyInfo") * -------- Reference 3 -------- loGen.AddSameDocRef("","sha256","EXCL_C14N","","") loGen.SetRefIdAttr("","Reference-Signature1") * Provide a certificate + private key. (PFX password is test123) * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Cert') loCert = CreateObject('Chilkat.Cert') lnSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") IF (lnSuccess <> 1) THEN ? loCert.LastErrorText RELEASE loSbXml RELEASE loGen RELEASE loObject1 RELEASE loCert CANCEL ENDIF loGen.SetX509Cert(loCert,1) loGen.KeyInfoType = "X509Data" loGen.X509Type = "Certificate" loGen.Behaviors = "IndentedSignature" * ------------------------------------------------------------------------------------------- * To have the EncapsulatedTimeStamp automatically added... * 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. (This was accomplished in the above code.) * 2) Specify the TSA URL (Timestamping Authority URL). * Here we specify the TSA URL: * ------------------------------------------------------------------------------------------- * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJsonTsa = CreateObject('Chilkat.JsonObject') loJsonTsa.UpdateString("timestampToken.tsaUrl","https://www3.postsignum.cz/TSS/TSS_crt/") loJsonTsa.UpdateBool("timestampToken.requestTsaCert",1) loGen.SetTsa(loJsonTsa) * ------------------------------------------------------------------------------------------- * In this case, the TSA requires client certificate authentication. * To provide your client certificate, the application will instantiate a Chilkat HTTP object, * then set it up with a SSL/TLS client certificate, and then tell the XmlDSigGen object * to use the HTTP object for connections to the TSA server. * ------------------------------------------------------------------------------------------- * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Http') loHttp = CreateObject('Chilkat.Http') lnSuccess = loHttp.SetSslClientCertPfx("/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx","pfxPassword") IF (lnSuccess <> 1) THEN ? loHttp.LastErrorText RELEASE loSbXml RELEASE loGen RELEASE loObject1 RELEASE loCert RELEASE loJsonTsa RELEASE loHttp CANCEL ENDIF * Tell the XmlDSigGen object to use the above HTTP object for TSA communications. loGen.SetHttpObj(loHttp) * Sign the XML... lnSuccess = loGen.CreateXmlDSigSb(loSbXml) IF (lnSuccess <> 1) THEN ? loGen.LastErrorText RELEASE loSbXml RELEASE loGen RELEASE loObject1 RELEASE loCert RELEASE loJsonTsa RELEASE loHttp CANCEL ENDIF * ----------------------------------------------- * Save the signed XML to a file. lnSuccess = loSbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",0) ? loSbXml.GetAsString() RELEASE loSbXml RELEASE loGen RELEASE loObject1 RELEASE loCert RELEASE loJsonTsa RELEASE loHttp |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.