Rust Requires Chilkat v11.0.0+
Rust
Decrypt a SAML Response
See more Encryption Examples
Demonstrates how to decrypt a SAML response.Chilkat Rust Downloads
let mut success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example decrypts this SAML response:
// <?xml version="1.0" encoding="UTF-8" ?>
// <saml2p:Response Destination="https://deskflow-asp2.com/ubc/ubcdfe.dll/cwlacs" ID="_e4585eaeedbcaf7c24dff7f1ee2499f5" IssueInstant="2018-10-11T17:46:20.727Z" Version="2.0" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol">
// <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://authentication.stg.id.ubc.ca</saml2:Issuer>
// <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/2001/04/xmldsig-more#rsa-sha256"/>
// <ds:Reference URI="#_e4585eaeedbcaf7c24dff7f1ee2499f5">
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>1ui22tqFyYEOoWI19CMwz4n+ynxNjLDGdTeRMdi60EU=</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue>ROg7FXV6vsp8socVhdo76/i7cRHGGKIveAiScKdujZT0QrHVqIvvbZ/RnwvEMJ9H9i/kJFAQA171
// Eo2kDjSdvNFQ/YcKaJUwMtAwT05yVatGV42RZKEf7ME+vpcCTR1LWZdrhat1FWCg1MNQwNWB0EL5
// fEP2a4jAcSTB8tFbjTAHsv7IWC39E5RVv99mACYXLa7iGZLtORANZxgYu5qQgmH6pUkI6Z1cpmf+
// m9mIjKM6LF0EvLfWOBWL6udZ+GsHPOLjVTJg+1S0xb9FQCYDVW1QhbjSS0icKHKTNNbrsaxllVDY
// m4q27YQjRh+XxugPgvsZ61Pxlto8Jbg+6jUlMQ==</ds:SignatureValue>
// <ds:KeyInfo>
// <ds:X509Data>
// <ds:X509Certificate>MIIDTTCCAjWgAwIBAgIVAJccYyIV6wly8XyddumpgnHMJ2JLMA0GCSqGSIb3DQEBCwUAMCcxJTAj
// BgNVBAMMHGF1dGhlbnRpY2F0aW9uLnN0Zy5pZC51YmMuY2EwHhcNMTcwMzAxMTk1NDM0WhcNMzcw
// ...
// xUuh6HuHKIwQqHBz7udxbH3Zbb6jXGDJjiDHt1LRJ8xbVisFIcDlIwsGQQi0HeEJfx4P</ds:X509Certificate>
// </ds:X509Data>
// </ds:KeyInfo>
// </ds:Signature>
// <saml2p:Status>
// <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
// </saml2p:Status>
// <saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
// <xenc:EncryptedData Id="_314d80b9cf02d8eda8d686a6ffd626cf" Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
// <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"/>
// <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
// <xenc:EncryptedKey Id="_d7b6da6fb59a627ebb4a96928441ab79" Recipient="https://ubcdfe.deskflow-asp2.com" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
// <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
// </xenc:EncryptionMethod>
// <ds:KeyInfo>
// <ds:X509Data>
// <ds:X509Certificate>MIICuzCCAiQCCQD3bpigRnKMSzANBgkqhkiG9w0BAQsFADCBoTELMAkGA1UEBhMCQ0ExEDAOBgNV
// BAgMB09udGFyaW8xEDAOBgNVBAcMB1Rvcm9udG8xJjAkBgNVBAoMHVRhY3RpY2FsIEJ1c2luZXNz
// ...
// kVRcHd1UK3q7G8FoykWjdQz/0EoMTfEZ+Md56mLOe48eMUZV2ONZuL1kDCEKw1UwkaDQI4Pf8pzx
// 82b9rgw9wBDtvu5eFPlUGEGIBw==</ds:X509Certificate>
// </ds:X509Data>
// </ds:KeyInfo>
// <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
// <xenc:CipherValue>BNHfUOpgdPE5BgpN2VIZIDthMAv1rxk91qVnWyCZOG9bmUKChJtTUqMpndot7VJwYuyKFshkAdnT
// D79KGdlSA1xHKcVeZXXzDWglqSyYjzhDCsyOhPaI4HelMFgCLwyFz89uEpUpqlvfl8ol3Am/XnzQ
// Vp7V7oS76hocjUI51Qs=</xenc:CipherValue>
// </xenc:CipherData>
// </xenc:EncryptedKey>
// </ds:KeyInfo>
// <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
// <xenc:CipherValue>R6l7tmbnXrOfBgB8lA3KnwLYsLH5ZO5omQ7Hp5K05atzw2o55xmCXVMYhNneFxMtxUh6raEyHeZX
// PTZNgWrvdqc4GYND/R7MhRrJzk9OAq1WyoOXwbtRpwNDwWA4N2IuprPQJbvjVxaw/PesZMZwZqlp
// ...
// zm9zAxahyu8Ooe8M4r3HN2cY0JxxxkZtDiulbnyA+rRtXfBRJtangvFQ4iFAnzM/Yg9hMyW9jcu0
// S7FzuRB9ONMxi+nh0IFWgqp+</xenc:CipherValue>
// </xenc:CipherData>
// </xenc:EncryptedData>
// </saml2:EncryptedAssertion>
// </saml2p:Response>
// The sample encrypted SAML response and RSA private key are available online:
let http = chilkat::Http::new();
let sb_saml_response = chilkat::StringBuilder::new();
let sb_private_key_pem = chilkat::StringBuilder::new();
success = http.quick_get_sb("https://chilkatdownload.com/data/samlresponse.xml", &sb_saml_response).is_ok();
if success {
success = http.quick_get_sb("https://chilkatdownload.com/data/samlresponse_privkey.pem", &sb_private_key_pem).is_ok();
}
if !success {
println!("{}", http.last_error_text());
return;
}
let xml = chilkat::Xml::new();
let _ = xml.load_sb(&sb_saml_response, true);
// Load the RSA private key..
let privkey = chilkat::PrivateKey::new();
if privkey.load_pem(&sb_private_key_pem.get_as_string().unwrap_or_default()).is_err() {
println!("{}", privkey.last_error_text());
return;
}
// Prepare an RSA object w/ the private key...
let rsa = chilkat::Rsa::new();
if rsa.use_private_key(&privkey).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// RSA will be used to decrypt the xenc:EncryptedKey
// The bytes to be decrypted are in xenc:CipherValue (in base64 format)
let Ok(encrypted_aes_key) = xml.get_child_content("saml2:EncryptedAssertion|xenc:EncryptedData|ds:KeyInfo|xenc:EncryptedKey|xenc:CipherData|xenc:CipherValue") else {
println!("Encrypted AES key not found.");
return;
};
println!("Encrypted AES key (base64) = {}", encrypted_aes_key);
let bd_aes_key = chilkat::BinData::new();
let _ = bd_aes_key.append_encoded(&encrypted_aes_key, "base64");
let sb_rsa_alg = chilkat::StringBuilder::new();
let _ = sb_rsa_alg.append(&xml.chilkat_path("saml2:EncryptedAssertion|xenc:EncryptedData|ds:KeyInfo|xenc:EncryptedKey|xenc:EncryptionMethod|(Algorithm)").unwrap_or_default());
println!("sbRsaAlg contains: {}", sb_rsa_alg.get_as_string().unwrap_or_default());
if sb_rsa_alg.contains("rsa-oaep", true) {
rsa.set_pkcs_padding(false);
}
if rsa.decrypt_bd(&bd_aes_key, true).is_err() {
println!("{}", rsa.last_error_text());
return;
}
println!("Decrypted AES key (hex) = {}", bd_aes_key.get_encoded("hex").unwrap_or_default());
// Get the encrypted XML (in base64) to be decrypted w/ the AES key.
let Ok(encrypted64) = xml.get_child_content("saml2:EncryptedAssertion|xenc:EncryptedData|xenc:CipherData|xenc:CipherValue") else {
println!("Encrypted data not found.");
return;
};
let bd_encrypted = chilkat::BinData::new();
let _ = bd_encrypted.append_encoded(&encrypted64, "base64");
// Get the symmetric algorithm: "http://www.w3.org/2001/04/xmlenc#aes128-cbc"
// and set the symmetric decrypt properties.
let crypt = chilkat::Crypt2::new();
let sb_alg = chilkat::StringBuilder::new();
let _ = sb_alg.append(&xml.chilkat_path("saml2:EncryptedAssertion|xenc:EncryptedData|xenc:EncryptionMethod|(Algorithm)").unwrap_or_default());
if sb_alg.contains("aes128-cbc", true) {
crypt.set_crypt_algorithm("aes");
crypt.set_key_length(128);
crypt.set_cipher_mode("cbc");
// The 1st 16 bytes of the encrypted data are the AES IV.
crypt.set_encoded_iv(&bd_encrypted.get_encoded_chunk(0, 16, "hex").unwrap_or_default(), "hex");
let _ = bd_encrypted.remove_chunk(0, 16);
}
// Other algorithms, key lengths, etc, can be supported by checking for different Algorithm attribute values..
crypt.set_encoded_key(&bd_aes_key.get_encoded("hex").unwrap_or_default(), "hex");
// AES decrypt...
if crypt.decrypt_bd(&bd_encrypted).is_err() {
println!("{}", crypt.last_error_text());
return;
}
// Get the decrypted XML
let decrypted_xml = bd_encrypted.get_string("utf-8").unwrap_or_default();
println!("Decrypted XML:");
println!("{}", decrypted_xml);
// The decrypted XML looks like this:
// <saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_226e565c548db7986d165d7d969b48b4" IssueInstant="2018-10-11T17:46:20.727Z" Version="2.0">
// ...
// ...
// ...
// </saml2:Assertion>
let xml_assertion = chilkat::Xml::new();
let _ = xml_assertion.load_xml(&decrypted_xml);
// Replace the saml2:EncryptedAssertion XML subtree with the saml2:Assertion XML.
let xml_encrypted_assertion = xml.find_child("saml2:EncryptedAssertion").unwrap();
let _ = xml_encrypted_assertion.swap_tree(&xml_assertion);
// The decrypted XML assertion has now replaced the encrypted XML assertion.
// Examine the fully decrypted XML document:
println!("Full XML SAML document with decrypted assertion:");
println!("{}", xml.get_xml().unwrap_or_default());