Skip to content

Commit c627d2f

Browse files
committed
refactor: remove has_decrypted_pgp_armor()
Explicit check for `-----BEGIN PGP MESSAGE-----` is unnecessary and not sufficient to ensure that the message is valid. We have already checked the MIME type, so ASCII-armored OpenPGP message should be inside. If it's not, decryption will fail anyway.
1 parent 429c14a commit c627d2f

File tree

1 file changed

+5
-54
lines changed

1 file changed

+5
-54
lines changed

src/decrypt.rs

+5-54
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub fn try_decrypt(
3131
return Ok(None);
3232
};
3333

34-
decrypt_part(
35-
encrypted_data_part,
36-
private_keyring,
37-
public_keyring_for_validate,
38-
)
34+
let data = encrypted_data_part.get_body_raw()?;
35+
36+
let (plain, ret_valid_signatures) =
37+
pgp::pk_decrypt(data, private_keyring, public_keyring_for_validate)?;
38+
Ok(Some((plain, ret_valid_signatures)))
3939
}
4040

4141
pub(crate) async fn prepare_decryption(
@@ -204,37 +204,6 @@ fn get_autocrypt_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail
204204
}
205205
}
206206

207-
/// Returns Ok(None) if nothing encrypted was found.
208-
fn decrypt_part(
209-
mail: &ParsedMail<'_>,
210-
private_keyring: &[SignedSecretKey],
211-
public_keyring_for_validate: &[SignedPublicKey],
212-
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> {
213-
let data = mail.get_body_raw()?;
214-
215-
if has_decrypted_pgp_armor(&data) {
216-
let (plain, ret_valid_signatures) =
217-
pgp::pk_decrypt(data, private_keyring, public_keyring_for_validate)?;
218-
return Ok(Some((plain, ret_valid_signatures)));
219-
}
220-
221-
Ok(None)
222-
}
223-
224-
#[allow(clippy::indexing_slicing)]
225-
fn has_decrypted_pgp_armor(input: &[u8]) -> bool {
226-
if let Some(index) = input.iter().position(|b| *b > b' ') {
227-
if input.len() - index > 26 {
228-
let start = index;
229-
let end = start + 27;
230-
231-
return &input[start..end] == b"-----BEGIN PGP MESSAGE-----";
232-
}
233-
}
234-
235-
false
236-
}
237-
238207
/// Validates signatures of Multipart/Signed message part, as defined in RFC 1847.
239208
///
240209
/// Returns the signed part and the set of key
@@ -346,24 +315,6 @@ mod tests {
346315
use crate::receive_imf::receive_imf;
347316
use crate::test_utils::TestContext;
348317

349-
#[test]
350-
fn test_has_decrypted_pgp_armor() {
351-
let data = b" -----BEGIN PGP MESSAGE-----";
352-
assert_eq!(has_decrypted_pgp_armor(data), true);
353-
354-
let data = b" \n-----BEGIN PGP MESSAGE-----";
355-
assert_eq!(has_decrypted_pgp_armor(data), true);
356-
357-
let data = b" -----BEGIN PGP MESSAGE---";
358-
assert_eq!(has_decrypted_pgp_armor(data), false);
359-
360-
let data = b" -----BEGIN PGP MESSAGE-----";
361-
assert_eq!(has_decrypted_pgp_armor(data), true);
362-
363-
let data = b"blas";
364-
assert_eq!(has_decrypted_pgp_armor(data), false);
365-
}
366-
367318
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
368319
async fn test_mixed_up_mime() -> Result<()> {
369320
// "Mixed Up" mail as received when sending an encrypted

0 commit comments

Comments
 (0)