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

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
Cloud Signature CSC
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

 

 

 

(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 C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkStringBuilder.h>
#include <C_CkDateTime.h>
#include <C_CkXml.h>
#include <C_CkXmlDSigGen.h>
#include <C_CkCert.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder tsId;
    HCkStringBuilder strId;
    HCkStringBuilder keyInfoId;
    HCkDateTime dt;
    HCkStringBuilder sbNow;
    int n;
    HCkStringBuilder sbNowPlusOneHour;
    HCkXml xmlToSign;
    HCkXmlDSigGen gen;
    HCkXml xml1;
    HCkXml xml2;
    HCkCert cert;
    HCkXml xmlCustomKeyInfo;
    HCkStringBuilder sbXml;

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

    success = TRUE;

    // 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
    tsId = CkStringBuilder_Create();
    CkStringBuilder_Append(tsId,"TS-");
    CkStringBuilder_AppendRandom(tsId,16,"hex");

    // STR-037E78514E9B9132CB16817563559614
    strId = CkStringBuilder_Create();
    CkStringBuilder_Append(strId,"STR-");
    CkStringBuilder_AppendRandom(strId,16,"hex");

    // KI-037E78514E9B9132CB16817563559583
    keyInfoId = CkStringBuilder_Create();
    CkStringBuilder_Append(keyInfoId,"KI-");
    CkStringBuilder_AppendRandom(keyInfoId,16,"hex");

    // Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
    dt = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(dt);

    sbNow = CkStringBuilder_Create();
    CkStringBuilder_Append(sbNow,CkDateTime_getAsTimestamp(dt,FALSE));
    // 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
    n = CkStringBuilder_Replace(sbNow,"Z",".000Z");

    sbNowPlusOneHour = CkStringBuilder_Create();
    CkDateTime_AddSeconds(dt,3600);
    CkStringBuilder_Append(sbNowPlusOneHour,CkDateTime_getAsTimestamp(dt,FALSE));
    n = CkStringBuilder_Replace(sbNowPlusOneHour,"Z",".000Z");

    xmlToSign = CkXml_Create();
    CkXml_putTag(xmlToSign,"soapenv:Envelope");
    CkXml_AddAttribute(xmlToSign,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_AddAttribute(xmlToSign,"xmlns:v1","http://customs.govt.nz/jbms/msggate/reqresp/v1");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"soapenv:mustUnderstand","1");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp",TRUE,"wsu:Id",CkStringBuilder_getAsString(tsId));
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkStringBuilder_getAsString(sbNow));
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkStringBuilder_getAsString(sbNowPlusOneHour));
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Body",TRUE,"wsu:Id","id-8");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Body",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Body|v1:RequestResponse|v1:Submitter","TEST1234");
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Body|v1:RequestResponse|v1:MailboxMsgId","999999");

    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"soapenv:Envelope|soapenv:Header|wsse:Security");
    CkXmlDSigGen_putSigLocationMod(gen,0);
    CkXmlDSigGen_putSigId(gen,"SIG-037E78514E9B9132CB16817563559695");
    CkXmlDSigGen_putSigNamespacePrefix(gen,"ds");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSignedInfoPrefixList(gen,"soapenv v1");
    CkXmlDSigGen_putIncNamespacePrefix(gen,"ec");
    CkXmlDSigGen_putIncNamespaceUri(gen,"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha256");

    // Set the KeyInfoId before adding references..
    CkXmlDSigGen_putKeyInfoId(gen,CkStringBuilder_getAsString(keyInfoId));

    // -------- Reference 1 --------
    xml1 = CkXml_Create();
    CkXml_putTag(xml1,"ds:Transforms");
    CkXml_UpdateAttrAt(xml1,"ds:Transform",TRUE,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXml_UpdateAttrAt(xml1,"ds:Transform|ec:InclusiveNamespaces",TRUE,"PrefixList","wsse soapenv v1");
    CkXml_UpdateAttrAt(xml1,"ds:Transform|ec:InclusiveNamespaces",TRUE,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGen_AddSameDocRef2(gen,CkStringBuilder_getAsString(tsId),"sha256",xml1,"");

    // -------- Reference 2 --------
    xml2 = CkXml_Create();
    CkXml_putTag(xml2,"ds:Transforms");
    CkXml_UpdateAttrAt(xml2,"ds:Transform",TRUE,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXml_UpdateAttrAt(xml2,"ds:Transform|ec:InclusiveNamespaces",TRUE,"PrefixList","v1");
    CkXml_UpdateAttrAt(xml2,"ds:Transform|ec:InclusiveNamespaces",TRUE,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGen_AddSameDocRef2(gen,"id-8","sha256",xml2,"");

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
    if (success != TRUE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkStringBuilder_Dispose(tsId);
        CkStringBuilder_Dispose(strId);
        CkStringBuilder_Dispose(keyInfoId);
        CkDateTime_Dispose(dt);
        CkStringBuilder_Dispose(sbNow);
        CkStringBuilder_Dispose(sbNowPlusOneHour);
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(xml1);
        CkXml_Dispose(xml2);
        CkCert_Dispose(cert);
        return;
    }

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"Custom");

    // Create the custom KeyInfo XML..
    xmlCustomKeyInfo = CkXml_Create();
    CkXml_putTag(xmlCustomKeyInfo,"wsse:SecurityTokenReference");
    CkXml_AddAttribute(xmlCustomKeyInfo,"wsu:Id",CkStringBuilder_getAsString(strId));
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:KeyIdentifier",TRUE,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:KeyIdentifier",TRUE,"ValueType","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
    CkCert_putUncommonOptions(cert,"Base64CertNoCRLF");
    CkXml_UpdateChildContent(xmlCustomKeyInfo,"wsse:KeyIdentifier",CkCert_getEncoded(cert));

    CkXml_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
    CkXmlDSigGen_putCustomKeyInfoXml(gen,CkXml_getXml(xmlCustomKeyInfo));

    // Load XML to be signed...
    sbXml = CkStringBuilder_Create();
    CkXml_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");

    // Sign the XML...
    CkXmlDSigGen_putVerboseLogging(gen,TRUE);
    success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
        CkStringBuilder_Dispose(tsId);
        CkStringBuilder_Dispose(strId);
        CkStringBuilder_Dispose(keyInfoId);
        CkDateTime_Dispose(dt);
        CkStringBuilder_Dispose(sbNow);
        CkStringBuilder_Dispose(sbNowPlusOneHour);
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(xml1);
        CkXml_Dispose(xml2);
        CkCert_Dispose(cert);
        CkXml_Dispose(xmlCustomKeyInfo);
        CkStringBuilder_Dispose(sbXml);
        return;
    }

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

    printf("%s\n",CkStringBuilder_getAsString(sbXml));


    CkStringBuilder_Dispose(tsId);
    CkStringBuilder_Dispose(strId);
    CkStringBuilder_Dispose(keyInfoId);
    CkDateTime_Dispose(dt);
    CkStringBuilder_Dispose(sbNow);
    CkStringBuilder_Dispose(sbNowPlusOneHour);
    CkXml_Dispose(xmlToSign);
    CkXmlDSigGen_Dispose(gen);
    CkXml_Dispose(xml1);
    CkXml_Dispose(xml2);
    CkCert_Dispose(cert);
    CkXml_Dispose(xmlCustomKeyInfo);
    CkStringBuilder_Dispose(sbXml);

    }

 

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