Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

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

let _ = 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
let ts_id = chilkat::StringBuilder::new();
let _ = ts_id.append("TS-");
let _ = ts_id.append_random(16, "hex");

// STR-037E78514E9B9132CB16817563559614
let str_id = chilkat::StringBuilder::new();
let _ = str_id.append("STR-");
let _ = str_id.append_random(16, "hex");

// KI-037E78514E9B9132CB16817563559583
let key_info_id = chilkat::StringBuilder::new();
let _ = key_info_id.append("KI-");
let _ = key_info_id.append_random(16, "hex");

// Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
let dt = chilkat::DateTime::new();
let _ = dt.set_from_current_system_time();

let sb_now = chilkat::StringBuilder::new();
let _ = sb_now.append(&dt.get_as_timestamp(false).unwrap_or_default());
// 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
let mut n = sb_now.replace("Z", ".000Z");

let sb_now_plus_one_hour = chilkat::StringBuilder::new();
let _ = dt.add_seconds(3600);
let _ = sb_now_plus_one_hour.append(&dt.get_as_timestamp(false).unwrap_or_default());
n = sb_now_plus_one_hour.replace("Z", ".000Z");

let xml_to_sign = chilkat::Xml::new();
xml_to_sign.set_tag("soapenv:Envelope");
let _ = xml_to_sign.add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
let _ = xml_to_sign.add_attribute("xmlns:v1", "http://customs.govt.nz/jbms/msggate/reqresp/v1");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security", true, "soapenv:mustUnderstand", "1");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", &ts_id.get_as_string().unwrap_or_default());
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", &sb_now.get_as_string().unwrap_or_default());
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", &sb_now_plus_one_hour.get_as_string().unwrap_or_default());
let _ = xml_to_sign.update_attr_at("soapenv:Body", true, "wsu:Id", "id-8");
let _ = xml_to_sign.update_attr_at("soapenv:Body", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xml_to_sign.update_child_content("soapenv:Body|v1:RequestResponse|v1:Submitter", "TEST1234");
xml_to_sign.update_child_content("soapenv:Body|v1:RequestResponse|v1:MailboxMsgId", "999999");

let gen_ = chilkat::XmlDSigGen::new();

gen_.set_sig_location("soapenv:Envelope|soapenv:Header|wsse:Security");
gen_.set_sig_location_mod(0);
gen_.set_sig_id("SIG-037E78514E9B9132CB16817563559695");
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_signed_info_prefix_list("soapenv v1");
gen_.set_inc_namespace_prefix("ec");
gen_.set_inc_namespace_uri("http://www.w3.org/2001/10/xml-exc-c14n#");
gen_.set_signed_info_canon_alg("EXCL_C14N");
gen_.set_signed_info_digest_method("sha256");

// Set the KeyInfoId before adding references..
gen_.set_key_info_id(&key_info_id.get_as_string().unwrap_or_default());

// -------- Reference 1 --------
let xml1 = chilkat::Xml::new();
xml1.set_tag("ds:Transforms");
let _ = xml1.update_attr_at("ds:Transform", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = xml1.update_attr_at("ds:Transform|ec:InclusiveNamespaces", true, "PrefixList", "wsse soapenv v1");
let _ = xml1.update_attr_at("ds:Transform|ec:InclusiveNamespaces", true, "xmlns:ec", "http://www.w3.org/2001/10/xml-exc-c14n#");

let _ = gen_.add_same_doc_ref2(&ts_id.get_as_string().unwrap_or_default(), "sha256", &xml1, "");

// -------- Reference 2 --------
let xml2 = chilkat::Xml::new();
xml2.set_tag("ds:Transforms");
let _ = xml2.update_attr_at("ds:Transform", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = xml2.update_attr_at("ds:Transform|ec:InclusiveNamespaces", true, "PrefixList", "v1");
let _ = xml2.update_attr_at("ds:Transform|ec:InclusiveNamespaces", true, "xmlns:ec", "http://www.w3.org/2001/10/xml-exc-c14n#");

let _ = gen_.add_same_doc_ref2("id-8", "sha256", &xml2, "");

// Provide a certificate + private key. (PFX password is test123)
let cert = chilkat::Cert::new();
if cert.load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

let _ = gen_.set_x509_cert(&cert, true);

gen_.set_key_info_type("Custom");

// Create the custom KeyInfo XML..
let xml_custom_key_info = chilkat::Xml::new();
xml_custom_key_info.set_tag("wsse:SecurityTokenReference");
let _ = xml_custom_key_info.add_attribute("wsu:Id", &str_id.get_as_string().unwrap_or_default());
let _ = xml_custom_key_info.update_attr_at("wsse:KeyIdentifier", true, "EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
let _ = xml_custom_key_info.update_attr_at("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
cert.set_uncommon_options("Base64CertNoCRLF");
xml_custom_key_info.update_child_content("wsse:KeyIdentifier", &cert.get_encoded().unwrap_or_default());

xml_custom_key_info.set_emit_xml_decl(false);
gen_.set_custom_key_info_xml(&xml_custom_key_info.get_xml().unwrap_or_default());

// Load XML to be signed...
let sb_xml = chilkat::StringBuilder::new();
let _ = xml_to_sign.get_xml_sb(&sb_xml);

gen_.set_behaviors("IndentedSignature");

// Sign the XML...
gen_.set_verbose_logging(true);
if gen_.create_xml_d_sig_sb(&sb_xml).is_err() {
    println!("{}", gen_.last_error_text());
    return;
}

// Save the signed XML to a file.
let _ = sb_xml.write_file("c:/temp/qa_output/signedXml.xml", "utf-8", false).is_ok();

println!("{}", sb_xml.get_as_string().unwrap_or_default());