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
(Swift 3,4,5...) 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.
func chilkatTest() { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. var success: Bool = true // 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> let xmlToSign = CkoXml()! xmlToSign.tag = "es:Dossier" xmlToSign.addAttribute("xmlns:xsi", value: "http://www.w3.org/2001/XMLSchema-instance") xmlToSign.addAttribute("xmlns:ds", value: "http://www.w3.org/2000/09/xmldsig#") xmlToSign.addAttribute("xmlns", value: "http://uri.etsi.org/01903/v1.3.2#") xmlToSign.addAttribute("xmlns:es", value: "https://www.microsec.hu/ds/e-szigno30#") xmlToSign.addAttribute("xsi:schemaLocation", value: "https://www.microsec.hu/ds/e-szigno30# https://www.microsec.hu/ds/e-szigno30.xsd") xmlToSign.updateAttr(at: "es:DossierProfile", autoCreate: true, attrName: "Id", attrValue: "PObject0") xmlToSign.updateAttr(at: "es:DossierProfile", autoCreate: true, attrName: "OBJREF", attrValue: "Object0") xmlToSign.updateChildContent("es:DossierProfile|es:Title", value: "e-akta.es3") xmlToSign.updateChildContent("es:DossierProfile|es:E-category", value: "electronic dossier") xmlToSign.updateChildContent("es:DossierProfile|es:CreationDate", value: "2022-12-02T07:55:16Z") xmlToSign.updateAttr(at: "es:Documents", autoCreate: true, attrName: "Id", attrValue: "Object0") let gen = CkoXmlDSigGen()! gen.sigLocation = "es:Dossier" gen.sigLocationMod = 0 gen.sigId = "S9fe8096e-2cac-415d-9222-f6cf2ecb314b" gen.sigValueId = "VS9fe8096e-2cac-415d-9222-f6cf2ecb314b" gen.signedInfoId = "SIS9fe8096e-2cac-415d-9222-f6cf2ecb314b" gen.signedInfoCanonAlg = "EXCL_C14N" gen.signedInfoDigestMethod = "sha256" // Set the KeyInfoId before adding references.. gen.keyInfoId = "KS9fe8096e-2cac-415d-9222-f6cf2ecb314b" // Create an Object to be added to the Signature. let object1 = CkoXml()! object1.tag = "es:SignatureProfile" object1.addAttribute("Id", value: "PS9fe8096e-2cac-415d-9222-f6cf2ecb314b") object1.addAttribute("OBJREF", value: "Object0") object1.addAttribute("SIGREF", value: "S9fe8096e-2cac-415d-9222-f6cf2ecb314b") object1.addAttribute("SIGREFLIST", value: "#Object0 #PS9fe8096e-2cac-415d-9222-f6cf2ecb314b #PObject0 #XS9fe8096e-2cac-415d-9222-f6cf2ecb314b") object1.updateChildContent("es:SignerName", value: "EC Minsített-Tesztel Péterke") object1.updateChildContent("es:SDPresented", value: "false") object1.updateChildContent("es:Type", value: "signature") object1.updateAttr(at: "es:Generator|es:Program", autoCreate: true, attrName: "name", attrValue: "e-Szigno") object1.updateAttr(at: "es:Generator|es:Program", autoCreate: true, attrName: "version", attrValue: "3.3.6.8") object1.updateAttr(at: "es:Generator|es:Device", autoCreate: true, attrName: "name", attrValue: "OpenSSL 1.1.1n 15 Mar 2022") object1.updateAttr(at: "es:Generator|es:Device", autoCreate: true, attrName: "type", attrValue: "") gen.addObject("O1S9fe8096e-2cac-415d-9222-f6cf2ecb314b", content: object1.getXml(), mimeType: "", encoding: "") // Create an Object to be added to the Signature. let object2 = CkoXml()! object2.tag = "QualifyingProperties" object2.addAttribute("Target", value: "#S9fe8096e-2cac-415d-9222-f6cf2ecb314b") object2.addAttribute("Id", value: "QPS9fe8096e-2cac-415d-9222-f6cf2ecb314b") object2.updateAttr(at: "SignedProperties", autoCreate: true, attrName: "Id", attrValue: "XS9fe8096e-2cac-415d-9222-f6cf2ecb314b") object2.updateChildContent("SignedProperties|SignedSignatureProperties|SigningTime", value: "TO BE GENERATED BY CHILKAT") object2.updateAttr(at: "SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestMethod", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2001/04/xmlenc#sha256") object2.updateChildContent("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|CertDigest|ds:DigestValue", value: "TO BE GENERATED BY CHILKAT") object2.updateChildContent("SignedProperties|SignedSignatureProperties|SigningCertificateV2|Cert|IssuerSerialV2", value: "TO BE GENERATED BY CHILKAT") object2.updateChildContent("SignedProperties|SignedSignatureProperties|SignaturePolicyIdentifier|SignaturePolicyImplied", value: "") object2.updateChildContent("SignedProperties|SignedSignatureProperties|SignerRoleV2|ClaimedRoles|ClaimedRole", value: "tesztel") // Here we have the EncapsulatedTimestamp found in the unsigned signature properties. object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp", autoCreate: true, attrName: "Id", attrValue: "T72cb4961-4326-4319-857a-7cf55e7ef899") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|ds:CanonicalizationMethod", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2001/10/xml-exc-c14n#") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp", autoCreate: true, attrName: "Id", attrValue: "ET72cb4961-4326-4319-857a-7cf55e7ef899") object2.updateChildContent("UnsignedProperties|UnsignedSignatureProperties|SignatureTimeStamp|EncapsulatedTimeStamp", value: "TO BE GENERATED BY CHILKAT") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|TimeStampValidationData", autoCreate: true, attrName: "xmlns", attrValue: "http://uri.etsi.org/01903/v1.4.1#") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|CertificateValues", autoCreate: true, attrName: "Id", attrValue: "CV18c7702d-d45b-44bc-853a-a720f41053cd") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate", autoCreate: true, attrName: "Id", attrValue: "EC42db04c8-1422-407b-8c42-189353a55268") object2.updateChildContent("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate", value: "BASE64_CONTENT") object2.updateAttr(at: "UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]", autoCreate: true, attrName: "Id", attrValue: "EC04728b44-a32c-46c1-b9bb-85b1f6b3c7d3") object2.updateChildContent("UnsignedProperties|UnsignedSignatureProperties|CertificateValues|EncapsulatedX509Certificate[1]", value: "BASE64_CONTENT") gen.addObject("O2S9fe8096e-2cac-415d-9222-f6cf2ecb314b", content: object2.getXml(), mimeType: "", encoding: "") // -------- Reference 1 -------- gen.addSameDocRef("Object0", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "") gen.setRefIdAttr("Object0", value: "Re1f816c4-7898-4544-9b41-f4156dc0c528") // -------- Reference 2 -------- gen.addObjectRef("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "") gen.setRefIdAttr("PS9fe8096e-2cac-415d-9222-f6cf2ecb314b", value: "Ra873b616-e568-4c38-ae94-27fbff67cc43") // -------- Reference 3 -------- gen.addSameDocRef("PObject0", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "") gen.setRefIdAttr("PObject0", value: "Ra5d85948-5d6a-4914-8c32-242f5d6d9e81") // -------- Reference 4 -------- gen.addObjectRef("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "http://uri.etsi.org/01903#SignedProperties") gen.setRefIdAttr("XS9fe8096e-2cac-415d-9222-f6cf2ecb314b", value: "Ra7412a43-dc05-4e0a-ac84-e9a070214757") // Provide a certificate + private key. (PFX password is test123) let cert = CkoCert()! success = cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", password: "test123") if success != true { print("\(cert.lastErrorText!)") return } gen.setX509Cert(cert, usePrivateKey: true) gen.keyInfoType = "X509Data" gen.x509Type = "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: // ------------------------------------------------------------------------------------------- let jsonTsa = CkoJsonObject()! jsonTsa.update("timestampToken.tsaUrl", value: "http://timestamp.digicert.com") jsonTsa.updateBool("timestampToken.requestTsaCert", value: true) gen.setTsa(jsonTsa) // Load XML to be signed... let sbXml = CkoStringBuilder()! xmlToSign.getSb(sbXml) gen.behaviors = "IndentedSignature,OmitAlreadyDefinedSigNamespace" // Sign the XML... success = gen.createXmlDSigSb(sbXml) if success != true { print("\(gen.lastErrorText!)") return } // ----------------------------------------------- // Save the signed XML to a file. success = sbXml.writeFile("c:/temp/qa_output/signedXml.xml", charset: "utf-8", emitBom: false) print("\(sbXml.getAsString()!)") // ---------------------------------------- // Verify the signatures we just produced... let verifier = CkoXmlDSig()! success = verifier.loadSignatureSb(sbXml) if success != true { print("\(verifier.lastErrorText!)") return } var numSigs: Int = verifier.numSignatures.intValue var verifyIdx: Int = 0 while verifyIdx < numSigs { verifier.selector = verifyIdx var verified: Bool = verifier.verifySignature(true) if verified != true { print("\(verifier.lastErrorText!)") return } verifyIdx = verifyIdx + 1 } print("All signatures were successfully verified.") } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.