Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Swift 3,4,5... Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Swift 3,4,5...) UBL XAdES Enveloped Signature

Demonstrates how to create a UBL XAdES enveloped signature.

Note: This example requires Chilkat v9.5.0.80 or greater.

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

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

    var success: Bool = true

    // 
    // The following code creates the XML document to be signed.
    // (It is also possible to simply load the XML into the Chilkat XML object by calling the LoadXml method.)

    // A sample (already signed) of this XML is available here:  External link: credit-note-en16931-xades-signed.xml

    // Also, you may use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    let xmlToSign = CkoXml()!
    xmlToSign.tag = "CreditNote"
    xmlToSign.addAttribute("xmlns", value: "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2")
    xmlToSign.addAttribute("xmlns:cac", value: "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
    xmlToSign.addAttribute("xmlns:cbc", value: "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
    xmlToSign.addAttribute("xmlns:ext", value: "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
    xmlToSign.updateAttr(at: "ext:UBLExtensions|ext:UBLExtension", autoCreate: true, attrName: "xmlns:sac", attrValue: "urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2")
    xmlToSign.updateAttr(at: "ext:UBLExtensions|ext:UBLExtension", autoCreate: true, attrName: "xmlns:sig", attrValue: "urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2")
    xmlToSign.updateChildContent("ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation", value: "")
    xmlToSign.updateChildContent("cbc:CustomizationID", value: "urn:cen.eu:en16931:2017")
    xmlToSign.updateChildContent("cbc:ProfileID", value: "P1")
    xmlToSign.updateChildContent("cbc:ID", value: "c2ad1540-cf12-4e83-b47c-906aac70242e")
    xmlToSign.updateChildContent("cbc:IssueDate", value: "2009-12-15")
    xmlToSign.updateAttr(at: "cbc:Note", autoCreate: true, attrName: "languageID", attrValue: "en")
    xmlToSign.updateChildContent("cbc:Note", value: "Ordered in our booth at the convention.")
    xmlToSign.updateAttr(at: "cbc:DocumentCurrencyCode", autoCreate: true, attrName: "listAgencyID", attrValue: "6")
    xmlToSign.updateAttr(at: "cbc:DocumentCurrencyCode", autoCreate: true, attrName: "listID", attrValue: "ISO 4217 Alpha")
    xmlToSign.updateChildContent("cbc:DocumentCurrencyCode", value: "HRK")
    xmlToSign.updateChildContent("cbc:AccountingCost", value: "Project cost code 123")
    xmlToSign.updateChildContent("cac:InvoicePeriod|cbc:StartDate", value: "2009-11-01")
    xmlToSign.updateChildContent("cac:InvoicePeriod|cbc:EndDate", value: "2009-11-30")
    xmlToSign.updateChildContent("cac:OrderReference|cbc:ID", value: "123")
    xmlToSign.updateChildContent("cac:ContractDocumentReference|cbc:ID", value: "Contract321")
    xmlToSign.updateChildContent("cac:ContractDocumentReference|cbc:DocumentType", value: "Framework agreement")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference|cbc:ID", value: "Doc1")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference|cbc:DocumentType", value: "Timesheet")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI", value: "http://www.suppliersite.eu/sheet001.html")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference[1]|cbc:ID", value: "Doc2")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference[1]|cbc:DocumentType", value: "Drawing")
    xmlToSign.updateAttr(at: "cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject", autoCreate: true, attrName: "mimeCode", attrValue: "application/pdf")
    xmlToSign.updateChildContent("cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject", value: "UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID", value: "9934:18683136487::HR99:276")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName", value: "KATANCICEVA")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName", value: "ZAGREB")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone", value: "10000")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode", value: "HR")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID", value: "HR18683136487")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID", value: "FRE")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName", value: "MINISTARSTVO FINANCIJA")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name", value: "JURAJ MARKOVIC")
    xmlToSign.updateChildContent("cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail", value: "juraj.markovic@fina.hr")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID", value: "9934:49811265576::HR99:NOVO1")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName", value: "GETALDICEVA 4")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName", value: "ZAGREB")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone", value: "10000")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode", value: "HR")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID", value: "HR49811265576")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID", value: "VAT")
    xmlToSign.updateChildContent("cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName", value: "NOVO1")
    xmlToSign.updateChildContent("cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode", value: "HR")
    xmlToSign.updateChildContent("cac:PaymentMeans|cbc:PaymentMeansCode", value: "30")
    xmlToSign.updateChildContent("cac:PaymentMeans|cbc:InstructionNote", value: "Neki opis placanja")
    xmlToSign.updateChildContent("cac:PaymentMeans|cbc:PaymentID", value: "HR00 12456")
    xmlToSign.updateChildContent("cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID", value: "HR1210010051863000160")
    xmlToSign.updateChildContent("cac:PaymentTerms|cbc:Note", value: "Neki uvjeti placanja")
    xmlToSign.updateAttr(at: "cac:TaxTotal|cbc:TaxAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:TaxTotal|cbc:TaxAmount", value: "25.00")
    xmlToSign.updateAttr(at: "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount", value: "100.00")
    xmlToSign.updateAttr(at: "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount", value: "25.00")
    xmlToSign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID", value: "S")
    xmlToSign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent", value: "25")
    xmlToSign.updateChildContent("cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID", value: "VAT")
    xmlToSign.updateAttr(at: "cac:LegalMonetaryTotal|cbc:LineExtensionAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:LegalMonetaryTotal|cbc:LineExtensionAmount", value: "100.00")
    xmlToSign.updateAttr(at: "cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount", value: "100.00")
    xmlToSign.updateAttr(at: "cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount", value: "125.00")
    xmlToSign.updateAttr(at: "cac:LegalMonetaryTotal|cbc:PayableAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:LegalMonetaryTotal|cbc:PayableAmount", value: "125.00")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cbc:ID", value: "1")
    xmlToSign.updateAttr(at: "cac:CreditNoteLine|cbc:CreditedQuantity", autoCreate: true, attrName: "unitCode", attrValue: "H87")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cbc:CreditedQuantity", value: "1.000")
    xmlToSign.updateAttr(at: "cac:CreditNoteLine|cbc:LineExtensionAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cbc:LineExtensionAmount", value: "100.00")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cbc:Description", value: "Neki detaljniji opis proizvoda ide ovdje")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cbc:Name", value: "Neki proizvod")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode", value: "HR")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID", value: "S")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent", value: "25")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID", value: "VAT")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name", value: "Boja")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value", value: "Plava")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name", value: "Masa")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value", value: "1,2 Kg")
    xmlToSign.updateAttr(at: "cac:CreditNoteLine|cac:Price|cbc:PriceAmount", autoCreate: true, attrName: "currencyID", attrValue: "HRK")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Price|cbc:PriceAmount", value: "100.000000")
    xmlToSign.updateAttr(at: "cac:CreditNoteLine|cac:Price|cbc:BaseQuantity", autoCreate: true, attrName: "unitCode", attrValue: "H87")
    xmlToSign.updateChildContent("cac:CreditNoteLine|cac:Price|cbc:BaseQuantity", value: "1.000")

    // Use this online tool to generate XAdES code from a sample signed XML: 
    // Generate Code to Creaet XAdES from Sample XAdES

    // The following code was generated by the online tool.

    let gen = CkoXmlDSigGen()!

    gen.sigLocation = "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"
    gen.sigLocationMod = 0
    gen.sigId = "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"
    gen.sigNamespacePrefix = ""
    gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
    gen.signedInfoCanonAlg = "C14N"
    gen.signedInfoDigestMethod = "sha256"

    // Create an Object to be added to the Signature.
    let object1 = CkoXml()!
    object1.tag = "xades:QualifyingProperties"
    object1.addAttribute("xmlns:ds", value: "http://www.w3.org/2000/09/xmldsig#")
    object1.addAttribute("xmlns:xades", value: "http://uri.etsi.org/01903/v1.3.2#")
    object1.addAttribute("Target", value: "#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69")
    object1.updateAttr(at: "xades:SignedProperties", autoCreate: true, attrName: "Id", attrValue: "SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc")
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", value: "TO BE GENERATED BY CHILKAT")
    object1.updateAttr(at: "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2000/09/xmldsig#sha1")
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue", value: "TO BE GENERATED BY CHILKAT")
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName", value: "TO BE GENERATED BY CHILKAT")
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber", value: "TO BE GENERATED BY CHILKAT")
    object1.updateAttr(at: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", autoCreate: true, attrName: "ObjectReference", attrValue: "")
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", value: "document")
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", value: "application/xml")
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", value: "UTF-8")

    gen.addObject("", content: object1.getXml(), mimeType: "", encoding: "")

    // -------- Reference 1 --------
    gen.addSameDocRef("", digestMethod: "sha256", canonMethod: "", prefixList: "", refType: "")

    // -------- Reference 2 --------
    gen.addObjectRef("SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc", digestMethod: "sha256", canonMethod: "", prefixList: "", refType: "http://uri.etsi.org/01903#SignedProperties")

    // 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+KeyValue"
    gen.x509Type = "IssuerSerial,SubjectName,SKI,Certificate"

    // Load XML to be signed...
    let sbXml = CkoStringBuilder()!
    xmlToSign.getSb(sbXml)

    // Adding the "UBLDocumentSignatures" behavior causes the following Transform to be used for the 
    // first Reference:   
    //   <Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"><XPath>count(ancestor-or-self::sig:UBLDocumentSignatures | here()/ancestor::sig:UBLDocumentSignatures[1]) &gt; count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform>
    gen.behaviors = "IndentedSignature,UBLDocumentSignatures"

    // Sign the XML...
    success = gen.createXmlDSigSb(sbXml)
    if success != true {
        print("\(gen.lastErrorText!)")
        return
    }

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

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

    print("\(sbXml.getAsString()!)")

    // ----------------------------------------
    // Verify the signature we just produced...
    let verifier = CkoXmlDSig()!
    success = verifier.loadSignatureSb(sbXml)
    if success != true {
        print("\(verifier.lastErrorText!)")
        return
    }

    // We should have just one signature..
    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("The signature was successfully verified.")

}

 

© 2000-2021 Chilkat Software, Inc. All Rights Reserved.