diff --git a/src/aheader.rs b/src/aheader.rs index 9899c1978..ac9a54f60 100644 --- a/src/aheader.rs +++ b/src/aheader.rs @@ -4,11 +4,9 @@ use anyhow::{bail, Context as _, Error, Result}; use std::collections::BTreeMap; +use std::fmt; use std::str::FromStr; -use std::{fmt, str}; -use crate::contact::addr_cmp; -use crate::headerdef::{HeaderDef, HeaderDefMap}; use crate::key::{DcKey, SignedPublicKey}; /// Possible values for encryption preference @@ -36,7 +34,7 @@ impl fmt::Display for EncryptPreference { } } -impl str::FromStr for EncryptPreference { +impl FromStr for EncryptPreference { type Err = Error; fn from_str(s: &str) -> Result { @@ -69,29 +67,6 @@ impl Aheader { prefer_encrypt, } } - - /// Tries to parse Autocrypt header. - /// - /// If there is none, returns None. If the header is present but cannot be parsed, returns an - /// error. - pub fn from_headers( - wanted_from: &str, - headers: &[mailparse::MailHeader<'_>], - ) -> Result> { - if let Some(value) = headers.get_header_value(HeaderDef::Autocrypt) { - let header = Self::from_str(&value)?; - if !addr_cmp(&header.addr, wanted_from) { - bail!( - "Autocrypt header address {:?} is not {:?}", - header.addr, - wanted_from - ); - } - Ok(Some(header)) - } else { - Ok(None) - } - } } impl fmt::Display for Aheader { @@ -118,7 +93,7 @@ impl fmt::Display for Aheader { } } -impl str::FromStr for Aheader { +impl FromStr for Aheader { type Err = Error; fn from_str(s: &str) -> Result { diff --git a/src/decrypt.rs b/src/decrypt.rs index 68bcba737..b7f831ab0 100644 --- a/src/decrypt.rs +++ b/src/decrypt.rs @@ -1,6 +1,7 @@ //! End-to-end decryption support. use std::collections::HashSet; +use std::str::FromStr; use anyhow::Result; use mailparse::ParsedMail; @@ -13,7 +14,6 @@ use crate::context::Context; use crate::headerdef::{HeaderDef, HeaderDefMap}; use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey}; use crate::keyring::Keyring; -use crate::log::LogExt; use crate::peerstate::Peerstate; use crate::pgp; @@ -72,9 +72,25 @@ pub(crate) async fn prepare_decryption( }); } - let autocrypt_header = Aheader::from_headers(from, &mail.headers) - .ok_or_log_msg(context, "Failed to parse Autocrypt header") - .flatten(); + let autocrypt_header = + if let Some(autocrypt_header_value) = mail.headers.get_header_value(HeaderDef::Autocrypt) { + match Aheader::from_str(&autocrypt_header_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, message_time).await?;