Sample code for 30+ languages & platforms
Rust

Signed SOAP for www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/ using BinarySecurityToken

See more XML Digital Signatures Examples

Creates a signed SOAP request for www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/ using a BinarySecurityToken.

Chilkat Rust Downloads

Rust

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

// Create the following pre-signed SOAP XML:
// Note: We'll be constructing a placeholder for the wseBinarySecurityToken in the soapenv:Header.
// Chilkat will automatically replace the "BinarySecurityToken_Base64Binary_Content" with actual data when signing the XML.

// However: Your application should insert the actual desired timestamp values for wsu:Created and wsuExpires
// Typically this is the current system date/time and a few minutes from the current system date/time.
// The example below shows a 5 minute window.  We'll write code below to insert the current date/time and the current date/time + 5 minutes..

// <?xml version="1.0" encoding="utf-8"?>
// <soapenv:Envelope 
//   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
//   xmlns:scat="www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/"
//   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"
//   xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
//   xmlns:exc14n="http://www.w3.org/2001/10/xml-exc-c14n#">
//     <soapenv:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//         <wsse:Security soapenv:mustUnderstand="1">
//             <wsse:BinarySecurityToken 
//                   ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
//                   EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
//                   wsu:Id="X509Token">BinarySecurityToken_Base64Binary_Content</wsse:BinarySecurityToken>
//             <wsu:Timestamp wsu:Id="_1">
//                 <wsu:Created>2020-03-20T18:08:19Z</wsu:Created>
//                 <wsu:Expires>2020-03-20T18:13:19Z</wsu:Expires>
//             </wsu:Timestamp>
//         </wsse:Security>
//         <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
//     </soapenv:Header>
//     <soapenv:Body>
//         <scat:getRuoliStruttureOperatore>
//             <scat:codFiscaleOperatore>CLDxxxxxxxxxxxxL</scat:codFiscaleOperatore>
//         </scat:getRuoliStruttureOperatore>
//     </soapenv:Body>
// </soapenv:Envelope>

let _ = true;
// Create the XML to be signed...
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:scat", "www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/");
let _ = xml_to_sign.add_attribute("xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
let _ = xml_to_sign.add_attribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
let _ = xml_to_sign.add_attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
let _ = xml_to_sign.add_attribute("xmlns:exc14n", "http://www.w3.org/2001/10/xml-exc-c14n#");
let _ = xml_to_sign.update_attr_at("soapenv:Header", 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", true, "soapenv:mustUnderstand", "1");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "X509Token");
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", "BinarySecurityToken_Base64Binary_Content");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", "_1");

// Insert a 5-minute timestampe window.
let dt_now = chilkat::DateTime::new();
let _ = dt_now.set_from_current_system_time();
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", &dt_now.get_as_timestamp(false).unwrap_or_default());
// Add 5 minutes to the time.
let _ = dt_now.add_seconds(300);
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", &dt_now.get_as_timestamp(false).unwrap_or_default());
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security[1]", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
xml_to_sign.update_child_content("soapenv:Body|scat:getRuoliStruttureOperatore|scat:codFiscaleOperatore", "CLDxxxxxxxxxxxxL");

println!("XML to sign:");
println!("{}", xml_to_sign.get_xml().unwrap_or_default());

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

gen_.set_sig_location("soapenv:Envelope|soapenv:Header|wsse:Security");
gen_.set_sig_location_mod(0);
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_signed_info_canon_alg("EXCL_C14N");
gen_.set_signed_info_digest_method("sha1");

// Set the KeyInfoId before adding references..
gen_.set_key_info_id("X509KeyId");

// -------- Reference 1 --------
let _ = gen_.add_same_doc_ref("_1", "sha1", "EXCL_C14N", "", "");

// 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", "X509TokenReference");
let _ = xml_custom_key_info.update_attr_at("wsse:Reference", true, "URI", "#X509Token");
let _ = xml_custom_key_info.update_attr_at("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");

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);

// Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert.
let bd_cert = chilkat::BinData::new();
let _ = cert.export_cert_der_bd(&bd_cert);
let sb_cert64 = chilkat::StringBuilder::new();
let _ = bd_cert.get_encoded_sb("base64", &sb_cert64);

let n_replaced = sb_xml.replace("BinarySecurityToken_Base64Binary_Content", &sb_cert64.get_as_string().unwrap_or_default());

gen_.set_behaviors("IndentedSignature");

// Sign the XML...
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("qa_output/signedXml.xml", "utf-8", false).is_ok();

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

// Produces the following signed XML:
// Note: The following has been edited for readability.  Therefore, what you see here will not actually validate because it's been modified.

// <?xml version="1.0" encoding="utf-8"?>
// <soapenv:Envelope 
//   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
//   xmlns:scat="www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/" 
//   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"
//   xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:exc14n="http://www.w3.org/2001/10/xml-exc-c14n#">
//     <soapenv:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//         <wsse:Security soapenv:mustUnderstand="1">
//             <wsse:BinarySecurityToken ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" wsu:Id="X509Token">MIIFNTCCBB2gAwIBAgIQHozVnBl1lTsusAh26u6WZTANBgkqhkiG9w0BAQsFADCB
// lzELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G
// A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxPTA7BgNV
// BAMTNENPTU9ETyBSU0EgQ2xpZW50IEF1dGhlbnRpY2F0aW9uIGFuZCBTZWN1cmUg
// RW1haWwgQ0EwHhcNMTcxMjE0MDAwMDAwWhcNMTgxMjE0MjM1OTU5WjAmMSQwIgYJ
// KoZIhvcNAQkBFhVhc2Rhc2Rhc2Rhc2RkQGJ5b20uZGUwggEiMA0GCSqGSIb3DQEB
// AQUAA4IBDwAwggEKAoIBAQC96oQe50EDoiuJVITeKJzy6GzVq74cEa14eFypjMNb
// YVyedfCI6cn9pVClLqL7dlwxFGCRA62bbNE9woKLBT3SO1IvgoFDVIrbJm+84GEo
// qQReZe8HpZnF06d3DWwhUjjcuO1z2yliGdSymSee8/1OaztxEAEOWaZR+4MkfSNc
// AzzjGtKcKjVMSdiiO0JGAG/IXEwzlulfkF8zVdqDGkQTQesvT4WBys4BYwTDI64d
// sM7rcC9vwuK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Ad2F3YIymZKsRVdrU6+h0gXMnmtpX8T+SDV6M72PP2/ZzF6gVVItyyrIScK1J8
// mcEaR5GGkLBk1s9qQM9esp3FRlACVeb1Qlytr4vgc5FlCqn0rMtjlF4=
// </wsse:BinarySecurityToken>
//             <wsu:Timestamp wsu:Id="_1">
//                 <wsu:Created>2020-03-26T17:19:50Z</wsu:Created>
//                 <wsu:Expires>2020-03-26T17:24:50Z</wsu:Expires>
//             </wsu:Timestamp>
//         </wsse:Security>
//         <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
//   <ds:SignedInfo>
//     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//     <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
//     <ds:Reference URI="#_1">
//       <ds:Transforms>
//         <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//       </ds:Transforms>
//       <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
//       <ds:DigestValue>cwrTw7wFZqNc50NxE//UT11qZCY=</ds:DigestValue>
//     </ds:Reference>
//   </ds:SignedInfo>
//   <ds:SignatureValue>s2UmYNVDz9dm3eU ... 619qSZqw==</ds:SignatureValue>
//   <ds:KeyInfo Id="X509KeyId"><wsse:SecurityTokenReference wsu:Id="X509TokenReference">
//     <wsse:Reference URI="#X509Token" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
// </wsse:SecurityTokenReference>
//   </ds:KeyInfo>
// </ds:Signature></wsse:Security>
//     </soapenv:Header>
//     <soapenv:Body>
//         <scat:getRuoliStruttureOperatore>
//             <scat:codFiscaleOperatore>CLDxxxxxxxxxxxxL</scat:codFiscaleOperatore>
//         </scat:getRuoliStruttureOperatore>
//     </soapenv:Body>
// </soapenv:Envelope>

// ----------------------------------------
// Verify the signatures we just produced...
let verifier = chilkat::XmlDSig::new();
if verifier.load_signature_sb(&sb_xml).is_err() {
    println!("{}", verifier.last_error_text());
    return;
}

let num_sigs = verifier.num_signatures();
let mut verify_idx = 0;
while verify_idx < num_sigs {
    verifier.set_selector(verify_idx);
    if verifier.verify_signature(true).is_err() {
        println!("{}", verifier.last_error_text());
        return;
    }

    verify_idx = verify_idx + 1;
}

println!("All signatures were successfully verified.");