Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
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
Misc
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
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) UBL XAdES Enveloped Signature

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoXml.h>
#import <CkoXmlDSigGen.h>
#import <CkoCert.h>
#import <CkoStringBuilder.h>
#import <CkoXmlDSig.h>

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

BOOL success = YES;

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

CkoXml *xmlToSign = [[CkoXml alloc] init];
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 UpdateAttrAt: @"ext:UBLExtensions|ext:UBLExtension" autoCreate: YES attrName: @"xmlns:sac" attrValue: @"urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2"];
[xmlToSign UpdateAttrAt: @"ext:UBLExtensions|ext:UBLExtension" autoCreate: YES 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 UpdateAttrAt: @"cbc:Note" autoCreate: YES attrName: @"languageID" attrValue: @"en"];
[xmlToSign UpdateChildContent: @"cbc:Note" value: @"Ordered in our booth at the convention."];
[xmlToSign UpdateAttrAt: @"cbc:DocumentCurrencyCode" autoCreate: YES attrName: @"listAgencyID" attrValue: @"6"];
[xmlToSign UpdateAttrAt: @"cbc:DocumentCurrencyCode" autoCreate: YES 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 UpdateAttrAt: @"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject" autoCreate: YES 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 UpdateAttrAt: @"cac:TaxTotal|cbc:TaxAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:TaxTotal|cbc:TaxAmount" value: @"25.00"];
[xmlToSign UpdateAttrAt: @"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount" value: @"100.00"];
[xmlToSign UpdateAttrAt: @"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount" autoCreate: YES 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 UpdateAttrAt: @"cac:LegalMonetaryTotal|cbc:LineExtensionAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:LegalMonetaryTotal|cbc:LineExtensionAmount" value: @"100.00"];
[xmlToSign UpdateAttrAt: @"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount" value: @"100.00"];
[xmlToSign UpdateAttrAt: @"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount" value: @"125.00"];
[xmlToSign UpdateAttrAt: @"cac:LegalMonetaryTotal|cbc:PayableAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:LegalMonetaryTotal|cbc:PayableAmount" value: @"125.00"];
[xmlToSign UpdateChildContent: @"cac:CreditNoteLine|cbc:ID" value: @"1"];
[xmlToSign UpdateAttrAt: @"cac:CreditNoteLine|cbc:CreditedQuantity" autoCreate: YES attrName: @"unitCode" attrValue: @"H87"];
[xmlToSign UpdateChildContent: @"cac:CreditNoteLine|cbc:CreditedQuantity" value: @"1.000"];
[xmlToSign UpdateAttrAt: @"cac:CreditNoteLine|cbc:LineExtensionAmount" autoCreate: YES 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 UpdateAttrAt: @"cac:CreditNoteLine|cac:Price|cbc:PriceAmount" autoCreate: YES attrName: @"currencyID" attrValue: @"HRK"];
[xmlToSign UpdateChildContent: @"cac:CreditNoteLine|cac:Price|cbc:PriceAmount" value: @"100.000000"];
[xmlToSign UpdateAttrAt: @"cac:CreditNoteLine|cac:Price|cbc:BaseQuantity" autoCreate: YES 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.

CkoXmlDSigGen *gen = [[CkoXmlDSigGen alloc] init];

gen.SigLocation = @"CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation";
gen.SigLocationMod = [NSNumber numberWithInt: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.
CkoXml *object1 = [[CkoXml alloc] init];
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 UpdateAttrAt: @"xades:SignedProperties" autoCreate: YES attrName: @"Id" attrValue: @"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc"];
[object1 UpdateChildContent: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime" value: @"TO BE GENERATED BY CHILKAT"];
// Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1
[object1 UpdateAttrAt: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod" autoCreate: YES 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 UpdateAttrAt: @"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat" autoCreate: YES 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)
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/cert_test123.pfx" password: @"test123"];
if (success != YES) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

[gen SetX509Cert: cert usePrivateKey: YES];

gen.KeyInfoType = @"X509Data+KeyValue";
gen.X509Type = @"IssuerSerial,SubjectName,SKI,Certificate";

// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
[xmlToSign GetXmlSb: 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 != YES) {
    NSLog(@"%@",gen.LastErrorText);
    return;
}

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

// Save the signed XML to a file.
success = [sbXml WriteFile: @"qa_output/signedXml.xml" charset: @"utf-8" emitBom: NO];

NSLog(@"%@",[sbXml GetAsString]);

// ----------------------------------------
// Verify the signature we just produced...
CkoXmlDSig *verifier = [[CkoXmlDSig alloc] init];
success = [verifier LoadSignatureSb: sbXml];
if (success != YES) {
    NSLog(@"%@",verifier.LastErrorText);
    return;
}

// We should have just one signature..
int numSigs = [verifier.NumSignatures intValue];
int verifyIdx = 0;
while (verifyIdx < numSigs) {
    verifier.Selector = [NSNumber numberWithInt: verifyIdx];
    BOOL verified = [verifier VerifySignature: YES];
    if (verified != YES) {
        NSLog(@"%@",verifier.LastErrorText);
        return;
    }

    verifyIdx = verifyIdx + 1;
}

NSLog(@"%@",@"The signature was successfully verified.");
 

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