Rust Requires Chilkat v11.0.0+
Rust
Decrypt S/MIME and Verify S/MIME Signatures
See more MIME Examples
_LANGUAGE_ example to decrypt S/MIME and verify S/MIME signatures. The S/MIME is unwrapped to get the original MIME prior to signing/encrypting.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let mime = chilkat::Mime::new();
// Load an S/MIME message from a file:
if mime.load_mime_file("signedMime.txt").is_err() {
println!("{}", mime.last_error_text());
return;
}
// The ContainsEncryptedParts/ContainsSignedParts methods
// can be called to determine if the MIME is encrypted and/or signed:
let is_encrypted = mime.contains_encrypted_parts();
let is_signed = mime.contains_signed_parts();
// We don't want the "unwrap extras". You'll see what those
// are in a few moments...
mime.set_unwrap_extras(false);
// To verify the signature, call UnwrapSecurity. This will
// verify the signature(s) and decrypt the S/MIME and restore
// the MIME to the unsigned/unencrypted state.
// The results of what was found are present in the MIME object's
// properties, as well as extra header fields that are added
// to the unwrapped MIME.
if mime.unwrap_security().is_err() {
// UnwrapSecurity returns true if all signatures were
// verified and all parts decrypted.
println!("{}", mime.last_error_text());
return;
}
// The mime.UnwrapExtras property controls whether or not
// these additional fields are added to the unwrapped MIME:
//
// X-NumPartsSigned: 1
// X-SignaturesValid: yes
// X-NumPartsEncrypted: 1
// X-Decrypted: no
//
// The X-NumPartsSigned/X-SignaturesValid headers are added
// if the MIME was signed.
//
// The X-NumPartsEncrypted/X-Decrypted headers are added
// if the MIME was encrypted.
//
// If the MIME was signed, get the certificate used for signing.
if is_signed {
let signer_cert = chilkat::Cert::new();
// The NumSignerCerts property indicates how many certificates
// were used for signing. This example will assume the value is 1.
// To get the 1st signer cert, call LastSignerCert with an index of 0:
if mime.last_signer_cert(0, &signer_cert).is_ok() {
println!("**** Signer Cert: {}", signer_cert.subject_cn());
}
}
// If the MIME was encrypted, get the certificate used for decryption
if is_encrypted {
// The NumDecryptCerts property indicates how many certificates
// were used for decrypting. This example will assume the value is 1.
// To get the 1st decrypt cert, call LastDecryptCert with an index of 0:
let decrypt_cert = chilkat::Cert::new();
if mime.last_decrypt_cert(0, &decrypt_cert).is_ok() {
println!("**** Decrypt Cert: {}", decrypt_cert.subject_cn());
}
}
// Display the unwrapped MIME:
println!("{}", mime.get_mime().unwrap_or_default());
// Save the unwrapped MIME to a file:
let _ = mime.save_mime("unwrappedMime.txt").is_ok();