Skip to content

Commit

Permalink
refactor: remove has_decrypted_pgp_armor()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
link2xt committed Nov 3, 2024
1 parent 429c14a commit c627d2f
Showing 1 changed file with 5 additions and 54 deletions.
59 changes: 5 additions & 54 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub fn try_decrypt(
return Ok(None);
};

decrypt_part(
encrypted_data_part,
private_keyring,
public_keyring_for_validate,
)
let data = encrypted_data_part.get_body_raw()?;

let (plain, ret_valid_signatures) =
pgp::pk_decrypt(data, private_keyring, public_keyring_for_validate)?;
Ok(Some((plain, ret_valid_signatures)))
}

pub(crate) async fn prepare_decryption(
Expand Down Expand Up @@ -204,37 +204,6 @@ fn get_autocrypt_mime<'a, 'b>(mail: &'a ParsedMail<'b>) -> Option<&'a ParsedMail
}
}

/// Returns Ok(None) if nothing encrypted was found.
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
}

/// Validates signatures of Multipart/Signed message part, as defined in RFC 1847.
///
/// Returns the signed part and the set of key
Expand Down Expand Up @@ -346,24 +315,6 @@ mod tests {
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<()> {
// "Mixed Up" mail as received when sending an encrypted
Expand Down

0 comments on commit c627d2f

Please sign in to comment.