Sample code for 30+ languages & platforms
Go

XAdES using TSA Requiring Client Certificate

See more XML Digital Signatures Examples

Demonstrates 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/

Chilkat Go Downloads

Go
    success := false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    success = true

    // 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.)
    sbXml := chilkat.NewStringBuilder()
    success = sbXml.LoadFile("xmlToSign.xml","utf-8")

    gen := chilkat.NewXmlDSigGen()

    gen.SetSigLocation("TransakcniLogSystemu")
    gen.SetSigLocationMod(0)
    gen.SetSigId("SignatureID-Signature1")
    gen.SetSigNamespacePrefix("ds")
    gen.SetSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#")
    gen.SetSignedInfoCanonAlg("C14N")
    gen.SetSignedInfoDigestMethod("sha256")

    // Set the KeyInfoId before adding references..
    gen.SetKeyInfoId("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.

    object1 := chilkat.NewXml()
    object1.SetTag("xades:QualifyingProperties")
    object1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#")
    object1.AddAttribute("Target","#Signature1")

    object1.UpdateAttrAt("xades:SignedProperties",true,"Id","SignedProperties-Signature-Signature1")
    object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT")
    object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",true,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256")
    object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT")
    object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2","TO BE GENERATED BY CHILKAT")

    // The EncapsulatedTimestamp will be included in the unsigned properties.
    object1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",true,"Id","signature-timestamp-5561-8212-3316-5191")
    object1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",true,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
    object1.UpdateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",true,"Encoding","http://uri.etsi.org/01903/v1.2.2#DER")
    object1.UpdateChildContent("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp","TO BE GENERATED BY CHILKAT")

    gen.AddObject("XadesObjectId-Signature1",*object1.GetXml(),"","")

    // -------- Reference 1 --------
    gen.AddObjectRef("SignedProperties-Signature-Signature1","sha256","EXCL_C14N","","http://uri.etsi.org/01903#SignedProperties")

    // -------- Reference 2 --------
    gen.AddSameDocRef("KeyInfoId-Signature-Signature1","sha256","EXCL_C14N","","")
    gen.SetRefIdAttr("KeyInfoId-Signature-Signature1","ReferenceKeyInfo")

    // -------- Reference 3 --------
    gen.AddSameDocRef("","sha256","EXCL_C14N","","")
    gen.SetRefIdAttr("","Reference-Signature1")

    // Provide a certificate + private key. (PFX password is test123)
    cert := chilkat.NewCert()
    success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
    if success != true {
        fmt.Println(cert.LastErrorText())
        sbXml.DisposeStringBuilder()
        gen.DisposeXmlDSigGen()
        object1.DisposeXml()
        cert.DisposeCert()
        return
    }

    gen.SetX509Cert(cert,true)

    gen.SetKeyInfoType("X509Data")
    gen.SetX509Type("Certificate")

    gen.SetBehaviors("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:
    // -------------------------------------------------------------------------------------------

    jsonTsa := chilkat.NewJsonObject()
    jsonTsa.UpdateString("timestampToken.tsaUrl","https://www3.postsignum.cz/TSS/TSS_crt/")
    jsonTsa.UpdateBool("timestampToken.requestTsaCert",true)
    gen.SetTsa(jsonTsa)

    // -------------------------------------------------------------------------------------------
    // 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.
    // -------------------------------------------------------------------------------------------
    http := chilkat.NewHttp()
    success = http.SetSslClientCertPfx("/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx","pfxPassword")
    if success != true {
        fmt.Println(http.LastErrorText())
        sbXml.DisposeStringBuilder()
        gen.DisposeXmlDSigGen()
        object1.DisposeXml()
        cert.DisposeCert()
        jsonTsa.DisposeJsonObject()
        http.DisposeHttp()
        return
    }

    // Tell the XmlDSigGen object to use the above HTTP object for TSA communications.
    gen.SetHttpObj(http)

    // Sign the XML...
    success = gen.CreateXmlDSigSb(sbXml)
    if success != true {
        fmt.Println(gen.LastErrorText())
        sbXml.DisposeStringBuilder()
        gen.DisposeXmlDSigGen()
        object1.DisposeXml()
        cert.DisposeCert()
        jsonTsa.DisposeJsonObject()
        http.DisposeHttp()
        return
    }

    // -----------------------------------------------

    // Save the signed XML to a file.
    success = sbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",false)

    fmt.Println(*sbXml.GetAsString())

    sbXml.DisposeStringBuilder()
    gen.DisposeXmlDSigGen()
    object1.DisposeXml()
    cert.DisposeCert()
    jsonTsa.DisposeJsonObject()
    http.DisposeHttp()