|
|
|
|
|
|
|
|
use std::collections::HashSet; |
|
|
use std::str::FromStr; |
|
|
|
|
|
use anyhow::Result; |
|
|
use deltachat_contact_tools::addr_cmp; |
|
|
use mailparse::ParsedMail; |
|
|
|
|
|
use crate::aheader::Aheader; |
|
|
use crate::authres::handle_authres; |
|
|
use crate::authres::{self, DkimResults}; |
|
|
use crate::context::Context; |
|
|
use crate::headerdef::{HeaderDef, HeaderDefMap}; |
|
|
use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey}; |
|
|
use crate::peerstate::Peerstate; |
|
|
use crate::pgp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_decrypt( |
|
|
mail: &ParsedMail<'_>, |
|
|
private_keyring: &[SignedSecretKey], |
|
|
public_keyring_for_validate: &[SignedPublicKey], |
|
|
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> { |
|
|
let Some(encrypted_data_part) = get_encrypted_mime(mail) else { |
|
|
return Ok(None); |
|
|
}; |
|
|
|
|
|
decrypt_part( |
|
|
encrypted_data_part, |
|
|
private_keyring, |
|
|
public_keyring_for_validate, |
|
|
) |
|
|
} |
|
|
|
|
|
pub(crate) async fn prepare_decryption( |
|
|
context: &Context, |
|
|
mail: &ParsedMail<'_>, |
|
|
from: &str, |
|
|
message_time: i64, |
|
|
) -> Result<DecryptionInfo> { |
|
|
if mail.headers.get_header(HeaderDef::ListPost).is_some() { |
|
|
if mail.headers.get_header(HeaderDef::Autocrypt).is_some() { |
|
|
info!( |
|
|
context, |
|
|
"Ignoring autocrypt header since this is a mailing list message. \ |
|
|
NOTE: For privacy reasons, the mailing list software should remove Autocrypt headers." |
|
|
); |
|
|
} |
|
|
return Ok(DecryptionInfo { |
|
|
from: from.to_string(), |
|
|
autocrypt_header: None, |
|
|
peerstate: None, |
|
|
message_time, |
|
|
dkim_results: DkimResults { dkim_passed: false }, |
|
|
}); |
|
|
} |
|
|
|
|
|
let autocrypt_header = if context.is_self_addr(from).await? { |
|
|
None |
|
|
} else if let Some(aheader_value) = mail.headers.get_header_value(HeaderDef::Autocrypt) { |
|
|
match Aheader::from_str(&aheader_value) { |
|
|
Ok(header) if addr_cmp(&header.addr, from) => Some(header), |
|
|
Ok(header) => { |
|
|
warn!( |
|
|
context, |
|
|
"Autocrypt header address {:?} is not {:?}.", header.addr, from |
|
|
); |
|
|
None |
|
|
} |
|
|
Err(err) => { |
|
|
warn!(context, "Failed to parse Autocrypt header: {:#}.", err); |
|
|
None |
|
|
} |
|
|
} |
|
|
} else { |
|
|
None |
|
|
}; |
|
|
|
|
|
let dkim_results = handle_authres(context, mail, from).await?; |
|
|
let allow_aeap = get_encrypted_mime(mail).is_some(); |
|
|
let peerstate = get_autocrypt_peerstate( |
|
|
context, |
|
|
from, |
|
|
autocrypt_header.as_ref(), |
|
|
message_time, |
|
|
allow_aeap, |
|
|
) |
|
|
.await?; |
|
|
|
|
|
Ok(DecryptionInfo { |
|
|
from: from.to_string(), |
|
|
autocrypt_header, |
|
|
peerstate, |
|
|
message_time, |
|
|
dkim_results, |
|
|
}) |
|
|
} |
|
|
|
|
|
#[derive(Debug)] |
|
|
pub struct DecryptionInfo { |
|
|
|
|
|
|
|
|
pub from: String, |
|
|
pub autocrypt_header: Option<Aheader>, |
|
|
|
|
|
pub peerstate: Option<Peerstate>, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub message_time: i64, |
|
|
pub(crate) dkim_results: authres::DkimResults, |
|
|
} |
|
|
|
|
|
|
|
|
fn get_encrypted_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> { |
|
|
get_autocrypt_mime(mail) |
|
|
.or_else(|| get_mixed_up_mime(mail)) |
|
|
.or_else(|| get_attachment_mime(mail)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_mixed_up_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> { |
|
|
if mail.ctype.mimetype != "multipart/mixed" { |
|
|
return None; |
|
|
} |
|
|
if let [first_part, second_part, third_part] = &mail.subparts[..] { |
|
|
if first_part.ctype.mimetype == "text/plain" |
|
|
&& second_part.ctype.mimetype == "application/pgp-encrypted" |
|
|
&& third_part.ctype.mimetype == "application/octet-stream" |
|
|
{ |
|
|
Some(third_part) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_attachment_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> { |
|
|
if mail.ctype.mimetype != "multipart/mixed" { |
|
|
return None; |
|
|
} |
|
|
if let [first_part, second_part] = &mail.subparts[..] { |
|
|
if first_part.ctype.mimetype == "text/plain" |
|
|
&& second_part.ctype.mimetype == "multipart/encrypted" |
|
|
{ |
|
|
get_autocrypt_mime(second_part) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_autocrypt_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> { |
|
|
if mail.ctype.mimetype != "multipart/encrypted" { |
|
|
return None; |
|
|
} |
|
|
if let [first_part, second_part] = &mail.subparts[..] { |
|
|
if first_part.ctype.mimetype == "application/pgp-encrypted" |
|
|
&& second_part.ctype.mimetype == "application/octet-stream" |
|
|
{ |
|
|
Some(second_part) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fn decrypt_part( |
|
|
mail: &ParsedMail<'_>, |
|
|
private_keyring: &[SignedSecretKey], |
|
|
public_keyring_for_validate: &[SignedPublicKey], |
|
|
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> { |
|
|
let data = mail.get_body_raw()?; |
|
|
|
|
|
if has_decrypted_pgp_armor(&data) { |
|
|
let (plain, ret_valid_signatures) = |
|
|
pgp::pk_decrypt(data, private_keyring, public_keyring_for_validate)?; |
|
|
return Ok(Some((plain, ret_valid_signatures))); |
|
|
} |
|
|
|
|
|
Ok(None) |
|
|
} |
|
|
|
|
|
#[allow(clippy::indexing_slicing)] |
|
|
fn has_decrypted_pgp_armor(input: &[u8]) -> bool { |
|
|
if let Some(index) = input.iter().position(|b| *b > b' ') { |
|
|
if input.len() - index > 26 { |
|
|
let start = index; |
|
|
let end = start + 27; |
|
|
|
|
|
return &input[start..end] == b"-----BEGIN PGP MESSAGE-----"; |
|
|
} |
|
|
} |
|
|
|
|
|
false |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn validate_detached_signature<'a, 'b>( |
|
|
mail: &'a ParsedMail<'b>, |
|
|
public_keyring_for_validate: &[SignedPublicKey], |
|
|
) -> Option<(&'a ParsedMail<'b>, HashSet<Fingerprint>)> { |
|
|
if mail.ctype.mimetype != "multipart/signed" { |
|
|
return None; |
|
|
} |
|
|
|
|
|
if let [first_part, second_part] = &mail.subparts[..] { |
|
|
|
|
|
let content = first_part.raw_bytes; |
|
|
let ret_valid_signatures = match second_part.get_body_raw() { |
|
|
Ok(signature) => pgp::pk_validate(content, &signature, public_keyring_for_validate) |
|
|
.unwrap_or_default(), |
|
|
Err(_) => Default::default(), |
|
|
}; |
|
|
Some((first_part, ret_valid_signatures)) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) fn keyring_from_peerstate(peerstate: Option<&Peerstate>) -> Vec<SignedPublicKey> { |
|
|
let mut public_keyring_for_validate = Vec::new(); |
|
|
if let Some(peerstate) = peerstate { |
|
|
if let Some(key) = &peerstate.public_key { |
|
|
public_keyring_for_validate.push(key.clone()); |
|
|
} else if let Some(key) = &peerstate.gossip_key { |
|
|
public_keyring_for_validate.push(key.clone()); |
|
|
} |
|
|
} |
|
|
public_keyring_for_validate |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn get_autocrypt_peerstate( |
|
|
context: &Context, |
|
|
from: &str, |
|
|
autocrypt_header: Option<&Aheader>, |
|
|
message_time: i64, |
|
|
allow_aeap: bool, |
|
|
) -> Result<Option<Peerstate>> { |
|
|
let allow_change = !context.is_self_addr(from).await?; |
|
|
let mut peerstate; |
|
|
|
|
|
|
|
|
if let Some(header) = autocrypt_header { |
|
|
if allow_aeap { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
peerstate = Peerstate::from_verified_fingerprint_or_addr( |
|
|
context, |
|
|
&header.public_key.fingerprint(), |
|
|
from, |
|
|
) |
|
|
.await?; |
|
|
} else { |
|
|
peerstate = Peerstate::from_addr(context, from).await?; |
|
|
} |
|
|
|
|
|
if let Some(ref mut peerstate) = peerstate { |
|
|
if addr_cmp(&peerstate.addr, from) { |
|
|
if allow_change { |
|
|
peerstate.apply_header(header, message_time); |
|
|
peerstate.save_to_db(&context.sql).await?; |
|
|
} else { |
|
|
info!( |
|
|
context, |
|
|
"Refusing to update existing peerstate of {}", &peerstate.addr |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
let p = Peerstate::from_header(header, message_time); |
|
|
p.save_to_db(&context.sql).await?; |
|
|
peerstate = Some(p); |
|
|
} |
|
|
} else { |
|
|
peerstate = Peerstate::from_addr(context, from).await?; |
|
|
} |
|
|
|
|
|
Ok(peerstate) |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use super::*; |
|
|
use crate::receive_imf::receive_imf; |
|
|
use crate::test_utils::TestContext; |
|
|
|
|
|
#[test] |
|
|
fn test_has_decrypted_pgp_armor() { |
|
|
let data = b" -----BEGIN PGP MESSAGE-----"; |
|
|
assert_eq!(has_decrypted_pgp_armor(data), true); |
|
|
|
|
|
let data = b" \n-----BEGIN PGP MESSAGE-----"; |
|
|
assert_eq!(has_decrypted_pgp_armor(data), true); |
|
|
|
|
|
let data = b" -----BEGIN PGP MESSAGE---"; |
|
|
assert_eq!(has_decrypted_pgp_armor(data), false); |
|
|
|
|
|
let data = b" -----BEGIN PGP MESSAGE-----"; |
|
|
assert_eq!(has_decrypted_pgp_armor(data), true); |
|
|
|
|
|
let data = b"blas"; |
|
|
assert_eq!(has_decrypted_pgp_armor(data), false); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_mixed_up_mime() -> Result<()> { |
|
|
|
|
|
|
|
|
|
|
|
let mixed_up_mime = include_bytes!("../test-data/message/protonmail-mixed-up.eml"); |
|
|
let mail = mailparse::parse_mail(mixed_up_mime)?; |
|
|
assert!(get_autocrypt_mime(&mail).is_none()); |
|
|
assert!(get_mixed_up_mime(&mail).is_some()); |
|
|
assert!(get_attachment_mime(&mail).is_none()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let repaired_mime = include_bytes!("../test-data/message/protonmail-repaired.eml"); |
|
|
let mail = mailparse::parse_mail(repaired_mime)?; |
|
|
assert!(get_autocrypt_mime(&mail).is_some()); |
|
|
assert!(get_mixed_up_mime(&mail).is_none()); |
|
|
assert!(get_attachment_mime(&mail).is_none()); |
|
|
|
|
|
|
|
|
|
|
|
let attachment_mime = include_bytes!("../test-data/message/google-workspace-mixed-up.eml"); |
|
|
let mail = mailparse::parse_mail(attachment_mime)?; |
|
|
assert!(get_autocrypt_mime(&mail).is_none()); |
|
|
assert!(get_mixed_up_mime(&mail).is_none()); |
|
|
assert!(get_attachment_mime(&mail).is_some()); |
|
|
|
|
|
let bob = TestContext::new_bob().await; |
|
|
receive_imf(&bob, attachment_mime, false).await?; |
|
|
let msg = bob.get_last_msg().await; |
|
|
assert_eq!(msg.text, "Hello from Thunderbird!"); |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_mixed_up_mime_long() -> Result<()> { |
|
|
|
|
|
|
|
|
let mixed_up_mime = include_bytes!("../test-data/message/mixed-up-long.eml"); |
|
|
let bob = TestContext::new_bob().await; |
|
|
receive_imf(&bob, mixed_up_mime, false).await?; |
|
|
let msg = bob.get_last_msg().await; |
|
|
assert!(!msg.get_text().is_empty()); |
|
|
assert!(msg.has_html()); |
|
|
assert!(msg.id.get_html(&bob).await?.unwrap().len() > 40000); |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|