Unicode C++
Unicode 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 Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkDateTimeW.h>
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
void ChilkatSample(void)
{
bool success = false;
// 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
CkStringBuilderW tsId;
tsId.Append(L"TS-");
tsId.AppendRandom(16,L"hex");
// STR-037E78514E9B9132CB16817563559614
CkStringBuilderW strId;
strId.Append(L"STR-");
strId.AppendRandom(16,L"hex");
// KI-037E78514E9B9132CB16817563559583
CkStringBuilderW keyInfoId;
keyInfoId.Append(L"KI-");
keyInfoId.AppendRandom(16,L"hex");
// Create a date/time for the current time with this format: 2023-04-17T18:32:35.913Z
CkDateTimeW dt;
dt.SetFromCurrentSystemTime();
CkStringBuilderW sbNow;
sbNow.Append(dt.getAsTimestamp(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
int n = sbNow.Replace(L"Z",L".000Z");
CkStringBuilderW sbNowPlusOneHour;
dt.AddSeconds(3600);
sbNowPlusOneHour.Append(dt.getAsTimestamp(false));
n = sbNowPlusOneHour.Replace(L"Z",L".000Z");
CkXmlW xmlToSign;
xmlToSign.put_Tag(L"soapenv:Envelope");
xmlToSign.AddAttribute(L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
xmlToSign.AddAttribute(L"xmlns:v1",L"http://customs.govt.nz/jbms/msggate/reqresp/v1");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security",true,L"soapenv:mustUnderstand",L"1");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security",true,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security",true,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsu:Timestamp",true,L"wsu:Id",tsId.getAsString());
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",sbNow.getAsString());
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",sbNowPlusOneHour.getAsString());
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"wsu:Id",L"id-8");
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xmlToSign.UpdateChildContent(L"soapenv:Body|v1:RequestResponse|v1:Submitter",L"TEST1234");
xmlToSign.UpdateChildContent(L"soapenv:Body|v1:RequestResponse|v1:MailboxMsgId",L"999999");
CkXmlDSigGenW gen;
gen.put_SigLocation(L"soapenv:Envelope|soapenv:Header|wsse:Security");
gen.put_SigLocationMod(0);
gen.put_SigId(L"SIG-037E78514E9B9132CB16817563559695");
gen.put_SigNamespacePrefix(L"ds");
gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
gen.put_SignedInfoPrefixList(L"soapenv v1");
gen.put_IncNamespacePrefix(L"ec");
gen.put_IncNamespaceUri(L"http://www.w3.org/2001/10/xml-exc-c14n#");
gen.put_SignedInfoCanonAlg(L"EXCL_C14N");
gen.put_SignedInfoDigestMethod(L"sha256");
// Set the KeyInfoId before adding references..
gen.put_KeyInfoId(keyInfoId.getAsString());
// -------- Reference 1 --------
CkXmlW xml1;
xml1.put_Tag(L"ds:Transforms");
xml1.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
xml1.UpdateAttrAt(L"ds:Transform|ec:InclusiveNamespaces",true,L"PrefixList",L"wsse soapenv v1");
xml1.UpdateAttrAt(L"ds:Transform|ec:InclusiveNamespaces",true,L"xmlns:ec",L"http://www.w3.org/2001/10/xml-exc-c14n#");
gen.AddSameDocRef2(tsId.getAsString(),L"sha256",xml1,L"");
// -------- Reference 2 --------
CkXmlW xml2;
xml2.put_Tag(L"ds:Transforms");
xml2.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
xml2.UpdateAttrAt(L"ds:Transform|ec:InclusiveNamespaces",true,L"PrefixList",L"v1");
xml2.UpdateAttrAt(L"ds:Transform|ec:InclusiveNamespaces",true,L"xmlns:ec",L"http://www.w3.org/2001/10/xml-exc-c14n#");
gen.AddSameDocRef2(L"id-8",L"sha256",xml2,L"");
// Provide a certificate + private key. (PFX password is test123)
CkCertW cert;
success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123");
if (success != true) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
gen.SetX509Cert(cert,true);
gen.put_KeyInfoType(L"Custom");
// Create the custom KeyInfo XML..
CkXmlW xmlCustomKeyInfo;
xmlCustomKeyInfo.put_Tag(L"wsse:SecurityTokenReference");
xmlCustomKeyInfo.AddAttribute(L"wsu:Id",strId.getAsString());
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:KeyIdentifier",true,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:KeyIdentifier",true,L"ValueType",L"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.put_UncommonOptions(L"Base64CertNoCRLF");
xmlCustomKeyInfo.UpdateChildContent(L"wsse:KeyIdentifier",cert.getEncoded());
xmlCustomKeyInfo.put_EmitXmlDecl(false);
gen.put_CustomKeyInfoXml(xmlCustomKeyInfo.getXml());
// Load XML to be signed...
CkStringBuilderW sbXml;
xmlToSign.GetXmlSb(sbXml);
gen.put_Behaviors(L"IndentedSignature");
// Sign the XML...
gen.put_VerboseLogging(true);
success = gen.CreateXmlDSigSb(sbXml);
if (success != true) {
wprintf(L"%s\n",gen.lastErrorText());
return;
}
// Save the signed XML to a file.
success = sbXml.WriteFile(L"c:/temp/qa_output/signedXml.xml",L"utf-8",false);
wprintf(L"%s\n",sbXml.getAsString());
}