Skip to content

Commit

Permalink
Prefer encryption for the peer if the message is encrypted or signed …
Browse files Browse the repository at this point in the history
…with the known key (#3844)

Note that if the message is encrypted, we don't check whether it's signed with an attached key
currently, otherwise a massive refactoring of the code is needed because for encrypted messages a
signature is checked and discarded first now.
  • Loading branch information
iequidoo committed Dec 19, 2022
1 parent 03c273e commit a3fe105
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 235 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# This directory contains email messages verbatim, and changing CRLF to
# LF will corrupt them.
test-data/* text=false
test-data/** text=false

# binary files should be detected by git, however, to be sure, you can add them here explicitly
*.png binary
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Fix cargo clippy and doc errors after Rust update to 1.66 #3850
- Don't send GroupNameChanged message if the group name doesn't change in terms of
improve_single_line_input() #3852
- Prefer encryption for the peer if the message is encrypted or signed with the known key #3849


## 1.103.0
Expand Down
8 changes: 4 additions & 4 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn try_decrypt(
decryption_info: &DecryptionInfo,
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> {
// Possibly perform decryption
let public_keyring_for_validate = keyring_from_peerstate(&decryption_info.peerstate);
let public_keyring_for_validate = keyring_from_peerstate(decryption_info.peerstate.as_ref());

let encrypted_data_part = match get_autocrypt_mime(mail)
.or_else(|| get_mixed_up_mime(mail))
Expand Down Expand Up @@ -251,7 +251,7 @@ fn has_decrypted_pgp_armor(input: &[u8]) -> bool {
///
/// Returns `None` if the part is not a Multipart/Signed part, otherwise retruns the set of key
/// fingerprints for which there is a valid signature.
fn validate_detached_signature(
pub(crate) fn validate_detached_signature(
mail: &ParsedMail<'_>,
public_keyring_for_validate: &Keyring<SignedPublicKey>,
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> {
Expand All @@ -272,9 +272,9 @@ fn validate_detached_signature(
}
}

fn keyring_from_peerstate(peerstate: &Option<Peerstate>) -> Keyring<SignedPublicKey> {
pub(crate) fn keyring_from_peerstate(peerstate: Option<&Peerstate>) -> Keyring<SignedPublicKey> {
let mut public_keyring_for_validate: Keyring<SignedPublicKey> = Keyring::new();
if let Some(ref peerstate) = *peerstate {
if let Some(peerstate) = peerstate {
if let Some(key) = &peerstate.public_key {
public_keyring_for_validate.add(key.clone());
} else if let Some(key) = &peerstate.gossip_key {
Expand Down
247 changes: 160 additions & 87 deletions src/mimeparser.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/peerstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ mod tests {
verified_key_fingerprint: None,
fingerprint_changed: false,
};
assert_eq!(peerstate.prefer_encrypt, EncryptPreference::NoPreference);

peerstate.apply_header(&header, 100);
assert_eq!(peerstate.prefer_encrypt, EncryptPreference::Mutual);
Expand Down
6 changes: 6 additions & 0 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5359,7 +5359,13 @@ Reply from different address

let raw = include_bytes!("../test-data/message/thunderbird_with_autocrypt_unencrypted.eml");
receive_imf(&t, raw, false).await?;
let peerstate = Peerstate::from_addr(&t, "[email protected]")
.await?
.unwrap();
assert_eq!(peerstate.prefer_encrypt, EncryptPreference::Mutual);

let raw = include_bytes!("../test-data/message/thunderbird_signed_unencrypted.eml");
receive_imf(&t, raw, false).await?;
let peerstate = Peerstate::from_addr(&t, "[email protected]")
.await?
.unwrap();
Expand Down
56 changes: 56 additions & 0 deletions test-data/message/thunderbird_signed_unencrypted.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
From - Thu, 15 Dec 2022 14:45:17 GMT
X-Mozilla-Status: 0801
X-Mozilla-Status2: 00000000
Message-ID: <[email protected]>
Date: Thu, 15 Dec 2022 11:45:16 -0300
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.5.1
Content-Language: en-US
To: [email protected]
From: Alice <[email protected]>
Subject: test message 15:53
X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0;
attachmentreminder=0; deliveryformat=0
X-Identity-Key: id3
Fcc: imap://alice%[email protected]/Sent
Content-Type: multipart/signed; micalg=pgp-sha256;
protocol="application/pgp-signature";
boundary="------------iX39J1p7DOgblwacjo0e7jX7"

This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
--------------iX39J1p7DOgblwacjo0e7jX7
Content-Type: multipart/mixed; boundary="------------WD4DG7TcI4p4lbzyM4toRaDw";
protected-headers="v1"
From: Alice <[email protected]>
To: [email protected]
Message-ID: <[email protected]>
Subject: test message 15:53
--------------WD4DG7TcI4p4lbzyM4toRaDw
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: base64
DQo=
--------------WD4DG7TcI4p4lbzyM4toRaDw--

--------------iX39J1p7DOgblwacjo0e7jX7
Content-Type: application/pgp-signature; name="OpenPGP_signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="OpenPGP_signature"
-----BEGIN PGP SIGNATURE-----

wsD5BAABCAAjFiEEFKs/ZfwnS721+naMJfAHJFnkeuIFAmObMvwFAwAAAAAACgkQJfAHJFnkeuLM
TgwAnAADX93HE5vXmuBcAbRN2HKIwMzBtRtUF4FNPKchffUvvhSNpHkW2jW7A4hOHNgVSDQdqIUn
+62NgkaKrT1bZqozOZNHXMECHtKBwXWTkIAVqcBdvscCztVIgGby56OPnzZ5y09BsRaqqE5AhDgN
wGCLa6ipu5FYSF6+KzdO0GIPMY5aGRgVhtl4N01v4S3+r/Yu60MkN87nd15Eaqsrs60P9RmKJTt4
hDie35kKvHnPzLNs8+xLfqPuO/P7ZbPQgkgCwMAMsMDRUYOv+k5c/bL3PKiOENuDpQ7dkKJ2OzSn
nTcg8qhDf17vWe26C/QBhFiGEsrHNBQ1KW5by+cqjIUBJgXElFnPl35S5L3fn6JHZLcz6q+wQuJu
vGT1mJuP//jLFkMHSexukFIVXzn41rWPLd05rBqMgwRcOHMIyzE9zaO1aa8MF2TirPaZ5lH9rx/y
9DCU/d2sqbbYt8TGqj4hM3pqg5K22eq4KT1W7y8+28I5QfjZumLLrHBdYTnR
=6JTB
-----END PGP SIGNATURE-----

--------------iX39J1p7DOgblwacjo0e7jX7--
Loading

0 comments on commit a3fe105

Please sign in to comment.