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) Sign SOAP XML for New Zealand Customs Service

See more XAdES Examples

Demonstrates how to create an XAdES signed SOAP XML pertaining to the New Zealand Customs Service.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

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

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

BOOL success = YES;

// Create the following XML to be signed:

// <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
//     xmlns:v1="http://customs.govt.nz/jbms/msggate/reqresp/v1">
//     <soapenv:Header>
//         <wsse:Security soapenv:mustUnderstand="1"
//             xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
//             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//             <wsu:Timestamp wsu:Id="TS-037E78514E9B9132CB16817563559151">
//                 <wsu:Created>2023-04-17T18:32:35.913Z</wsu:Created>
//                 <wsu:Expires>2023-04-17T19:32:35.913Z</wsu:Expires>
//             </wsu:Timestamp>
//         </wsse:Security>
//     </soapenv:Header>
//     <soapenv:Body wsu:Id="id-8"
//         xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//         <v1:RequestResponse>
//             <v1:Submitter>TEST1234</v1:Submitter>
//             <v1:MailboxMsgId>999999</v1:MailboxMsgId>
//         </v1:RequestResponse>
//     </soapenv:Body>
// </soapenv:Envelope>

// Create a random ID like this: TS-037E78514E9B9132CB16817563559151
CkoStringBuilder *tsId = [[CkoStringBuilder alloc] init];
[tsId Append: @"TS-"];
[tsId AppendRandom: [NSNumber numberWithInt: 16] encoding: @"hex"];

// STR-037E78514E9B9132CB16817563559614
CkoStringBuilder *strId = [[CkoStringBuilder alloc] init];
[strId Append: @"STR-"];
[strId AppendRandom: [NSNumber numberWithInt: 16] encoding: @"hex"];

// KI-037E78514E9B9132CB16817563559583
CkoStringBuilder *keyInfoId = [[CkoStringBuilder alloc] init];
[keyInfoId Append: @"KI-"];
[keyInfoId AppendRandom: [NSNumber numberWithInt: 16] encoding: @"hex"];

// Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
CkoDateTime *dt = [[CkoDateTime alloc] init];
[dt SetFromCurrentSystemTime];

CkoStringBuilder *sbNow = [[CkoStringBuilder alloc] init];
[sbNow Append: [dt GetAsTimestamp: NO]];
// If we really need the milliseconds, we can replace the "Z" with ".000Z"
// The server will also likely accept a timestamp without milliseconds, such as 2023-04-17T18:32:35Z
int n = [[sbNow Replace: @"Z" replacement: @".000Z"] intValue];

CkoStringBuilder *sbNowPlusOneHour = [[CkoStringBuilder alloc] init];
[dt AddSeconds: [NSNumber numberWithInt: 3600]];
[sbNowPlusOneHour Append: [dt GetAsTimestamp: NO]];
n = [[sbNowPlusOneHour Replace: @"Z" replacement: @".000Z"] intValue];

CkoXml *xmlToSign = [[CkoXml alloc] init];
xmlToSign.Tag = @"soapenv:Envelope";
[xmlToSign AddAttribute: @"xmlns:soapenv" value: @"http://schemas.xmlsoap.org/soap/envelope/"];
[xmlToSign AddAttribute: @"xmlns:v1" value: @"http://customs.govt.nz/jbms/msggate/reqresp/v1"];
[xmlToSign UpdateAttrAt: @"soapenv:Header|wsse:Security" autoCreate: YES attrName: @"soapenv:mustUnderstand" attrValue: @"1"];
[xmlToSign UpdateAttrAt: @"soapenv:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsse" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"];
[xmlToSign UpdateAttrAt: @"soapenv:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xmlToSign UpdateAttrAt: @"soapenv:Header|wsse:Security|wsu:Timestamp" autoCreate: YES attrName: @"wsu:Id" attrValue: [tsId GetAsString]];
[xmlToSign UpdateChildContent: @"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created" value: [sbNow GetAsString]];
[xmlToSign UpdateChildContent: @"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires" value: [sbNowPlusOneHour GetAsString]];
[xmlToSign UpdateAttrAt: @"soapenv:Body" autoCreate: YES attrName: @"wsu:Id" attrValue: @"id-8"];
[xmlToSign UpdateAttrAt: @"soapenv:Body" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xmlToSign UpdateChildContent: @"soapenv:Body|v1:RequestResponse|v1:Submitter" value: @"TEST1234"];
[xmlToSign UpdateChildContent: @"soapenv:Body|v1:RequestResponse|v1:MailboxMsgId" value: @"999999"];

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

gen.SigLocation = @"soapenv:Envelope|soapenv:Header|wsse:Security";
gen.SigLocationMod = [NSNumber numberWithInt:0];
gen.SigId = @"SIG-037E78514E9B9132CB16817563559695";
gen.SigNamespacePrefix = @"ds";
gen.SigNamespaceUri = @"http://www.w3.org/2000/09/xmldsig#";
gen.SignedInfoPrefixList = @"soapenv v1";
gen.IncNamespacePrefix = @"ec";
gen.IncNamespaceUri = @"http://www.w3.org/2001/10/xml-exc-c14n#";
gen.SignedInfoCanonAlg = @"EXCL_C14N";
gen.SignedInfoDigestMethod = @"sha256";

// Set the KeyInfoId before adding references..
gen.KeyInfoId = [keyInfoId GetAsString];

// -------- Reference 1 --------
CkoXml *xml1 = [[CkoXml alloc] init];
xml1.Tag = @"ds:Transforms";
[xml1 UpdateAttrAt: @"ds:Transform" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];
[xml1 UpdateAttrAt: @"ds:Transform|ec:InclusiveNamespaces" autoCreate: YES attrName: @"PrefixList" attrValue: @"wsse soapenv v1"];
[xml1 UpdateAttrAt: @"ds:Transform|ec:InclusiveNamespaces" autoCreate: YES attrName: @"xmlns:ec" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];

[gen AddSameDocRef2: [tsId GetAsString] digestMethod: @"sha256" transforms: xml1 refType: @""];

// -------- Reference 2 --------
CkoXml *xml2 = [[CkoXml alloc] init];
xml2.Tag = @"ds:Transforms";
[xml2 UpdateAttrAt: @"ds:Transform" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];
[xml2 UpdateAttrAt: @"ds:Transform|ec:InclusiveNamespaces" autoCreate: YES attrName: @"PrefixList" attrValue: @"v1"];
[xml2 UpdateAttrAt: @"ds:Transform|ec:InclusiveNamespaces" autoCreate: YES attrName: @"xmlns:ec" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];

[gen AddSameDocRef2: @"id-8" digestMethod: @"sha256" transforms: xml2 refType: @""];

// 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 = @"Custom";

// Create the custom KeyInfo XML..
CkoXml *xmlCustomKeyInfo = [[CkoXml alloc] init];
xmlCustomKeyInfo.Tag = @"wsse:SecurityTokenReference";
[xmlCustomKeyInfo AddAttribute: @"wsu:Id" value: [strId GetAsString]];
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:KeyIdentifier" autoCreate: YES attrName: @"EncodingType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"];
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:KeyIdentifier" autoCreate: YES attrName: @"ValueType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"];
// Insert the single-line base64 of the signing certificate's DER
cert.UncommonOptions = @"Base64CertNoCRLF";
[xmlCustomKeyInfo UpdateChildContent: @"wsse:KeyIdentifier" value: [cert GetEncoded]];

xmlCustomKeyInfo.EmitXmlDecl = NO;
gen.CustomKeyInfoXml = [xmlCustomKeyInfo GetXml];

// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
[xmlToSign GetXmlSb: sbXml];

gen.Behaviors = @"IndentedSignature";

// Sign the XML...
gen.VerboseLogging = YES;
success = [gen CreateXmlDSigSb: sbXml];
if (success != YES) {
    NSLog(@"%@",gen.LastErrorText);
    return;
}

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

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

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