1use std::collections::HashSet;
4
5use anyhow::Result;
6use mailparse::ParsedMail;
7
8use crate::key::{Fingerprint, SignedPublicKey, SignedSecretKey};
9use crate::pgp;
10
11pub fn try_decrypt<'a>(
15 mail: &'a ParsedMail<'a>,
16 private_keyring: &'a [SignedSecretKey],
17) -> Result<Option<::pgp::composed::Message<'static>>> {
18 let Some(encrypted_data_part) = get_encrypted_mime(mail) else {
19 return Ok(None);
20 };
21
22 let data = encrypted_data_part.get_body_raw()?;
23 let msg = pgp::pk_decrypt(data, private_keyring)?;
24
25 Ok(Some(msg))
26}
27
28pub(crate) fn get_encrypted_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> {
30 get_autocrypt_mime(mail)
31 .or_else(|| get_mixed_up_mime(mail))
32 .or_else(|| get_attachment_mime(mail))
33}
34
35fn get_mixed_up_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> {
53 if mail.ctype.mimetype != "multipart/mixed" {
54 return None;
55 }
56 if let [first_part, second_part, third_part] = &mail.subparts[..] {
57 if first_part.ctype.mimetype == "text/plain"
58 && second_part.ctype.mimetype == "application/pgp-encrypted"
59 && third_part.ctype.mimetype == "application/octet-stream"
60 {
61 Some(third_part)
62 } else {
63 None
64 }
65 } else {
66 None
67 }
68}
69
70fn get_attachment_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> {
78 if mail.ctype.mimetype != "multipart/mixed" {
79 return None;
80 }
81 if let [first_part, second_part] = &mail.subparts[..] {
82 if first_part.ctype.mimetype == "text/plain"
83 && second_part.ctype.mimetype == "multipart/encrypted"
84 {
85 get_autocrypt_mime(second_part)
86 } else {
87 None
88 }
89 } else {
90 None
91 }
92}
93
94fn get_autocrypt_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail<'b>> {
98 if mail.ctype.mimetype != "multipart/encrypted" {
99 return None;
100 }
101 if let [first_part, second_part] = &mail.subparts[..] {
102 if first_part.ctype.mimetype == "application/pgp-encrypted"
103 && second_part.ctype.mimetype == "application/octet-stream"
104 {
105 Some(second_part)
106 } else {
107 None
108 }
109 } else {
110 None
111 }
112}
113
114pub(crate) fn validate_detached_signature<'a, 'b>(
121 mail: &'a ParsedMail<'b>,
122 public_keyring_for_validate: &[SignedPublicKey],
123) -> Option<(&'a ParsedMail<'b>, HashSet<Fingerprint>)> {
124 if mail.ctype.mimetype != "multipart/signed" {
125 return None;
126 }
127
128 if let [first_part, second_part] = &mail.subparts[..] {
129 let content = first_part.raw_bytes;
131 let ret_valid_signatures = match second_part.get_body_raw() {
132 Ok(signature) => pgp::pk_validate(content, &signature, public_keyring_for_validate)
133 .unwrap_or_default(),
134 Err(_) => Default::default(),
135 };
136 Some((first_part, ret_valid_signatures))
137 } else {
138 None
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145 use crate::receive_imf::receive_imf;
146 use crate::test_utils::TestContext;
147
148 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
149 async fn test_mixed_up_mime() -> Result<()> {
150 let mixed_up_mime = include_bytes!("../test-data/message/protonmail-mixed-up.eml");
154 let mail = mailparse::parse_mail(mixed_up_mime)?;
155 assert!(get_autocrypt_mime(&mail).is_none());
156 assert!(get_mixed_up_mime(&mail).is_some());
157 assert!(get_attachment_mime(&mail).is_none());
158
159 let repaired_mime = include_bytes!("../test-data/message/protonmail-repaired.eml");
165 let mail = mailparse::parse_mail(repaired_mime)?;
166 assert!(get_autocrypt_mime(&mail).is_some());
167 assert!(get_mixed_up_mime(&mail).is_none());
168 assert!(get_attachment_mime(&mail).is_none());
169
170 let attachment_mime = include_bytes!("../test-data/message/google-workspace-mixed-up.eml");
173 let mail = mailparse::parse_mail(attachment_mime)?;
174 assert!(get_autocrypt_mime(&mail).is_none());
175 assert!(get_mixed_up_mime(&mail).is_none());
176 assert!(get_attachment_mime(&mail).is_some());
177
178 let bob = TestContext::new_bob().await;
179 receive_imf(&bob, attachment_mime, false).await?;
180 let msg = bob.get_last_msg().await;
181 assert_eq!(msg.text, "Hello from Thunderbird!");
182
183 Ok(())
184 }
185
186 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
187 async fn test_mixed_up_mime_long() -> Result<()> {
188 let mixed_up_mime = include_bytes!("../test-data/message/mixed-up-long.eml");
191 let bob = TestContext::new_bob().await;
192 receive_imf(&bob, mixed_up_mime, false).await?;
193 let msg = bob.get_last_msg().await;
194 assert!(!msg.get_text().is_empty());
195 assert!(msg.has_html());
196 assert!(msg.id.get_html(&bob).await?.unwrap().len() > 40000);
197 Ok(())
198 }
199}