Skip to content

Commit a3fe105

Browse files
committed
Prefer encryption for the peer if the message is encrypted or signed 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.
1 parent 03c273e commit a3fe105

8 files changed

+370
-235
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

99
# binary files should be detected by git, however, to be sure, you can add them here explicitly
1010
*.png binary

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Fix cargo clippy and doc errors after Rust update to 1.66 #3850
2020
- Don't send GroupNameChanged message if the group name doesn't change in terms of
2121
improve_single_line_input() #3852
22+
- Prefer encryption for the peer if the message is encrypted or signed with the known key #3849
2223

2324

2425
## 1.103.0

src/decrypt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub async fn try_decrypt(
3131
decryption_info: &DecryptionInfo,
3232
) -> Result<Option<(Vec<u8>, HashSet<Fingerprint>)>> {
3333
// Possibly perform decryption
34-
let public_keyring_for_validate = keyring_from_peerstate(&decryption_info.peerstate);
34+
let public_keyring_for_validate = keyring_from_peerstate(decryption_info.peerstate.as_ref());
3535

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

275-
fn keyring_from_peerstate(peerstate: &Option<Peerstate>) -> Keyring<SignedPublicKey> {
275+
pub(crate) fn keyring_from_peerstate(peerstate: Option<&Peerstate>) -> Keyring<SignedPublicKey> {
276276
let mut public_keyring_for_validate: Keyring<SignedPublicKey> = Keyring::new();
277-
if let Some(ref peerstate) = *peerstate {
277+
if let Some(peerstate) = peerstate {
278278
if let Some(key) = &peerstate.public_key {
279279
public_keyring_for_validate.add(key.clone());
280280
} else if let Some(key) = &peerstate.gossip_key {

src/mimeparser.rs

Lines changed: 160 additions & 87 deletions
Large diffs are not rendered by default.

src/peerstate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,6 @@ mod tests {
806806
verified_key_fingerprint: None,
807807
fingerprint_changed: false,
808808
};
809-
assert_eq!(peerstate.prefer_encrypt, EncryptPreference::NoPreference);
810809

811810
peerstate.apply_header(&header, 100);
812811
assert_eq!(peerstate.prefer_encrypt, EncryptPreference::Mutual);

src/receive_imf.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5359,7 +5359,13 @@ Reply from different address
53595359

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

5367+
let raw = include_bytes!("../test-data/message/thunderbird_signed_unencrypted.eml");
5368+
receive_imf(&t, raw, false).await?;
53635369
let peerstate = Peerstate::from_addr(&t, "[email protected]")
53645370
.await?
53655371
.unwrap();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
From - Thu, 15 Dec 2022 14:45:17 GMT
2+
X-Mozilla-Status: 0801
3+
X-Mozilla-Status2: 00000000
4+
Message-ID: <[email protected]>
5+
Date: Thu, 15 Dec 2022 11:45:16 -0300
6+
MIME-Version: 1.0
7+
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
8+
Thunderbird/102.5.1
9+
Content-Language: en-US
10+
11+
From: Alice <[email protected]>
12+
Subject: test message 15:53
13+
X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0;
14+
attachmentreminder=0; deliveryformat=0
15+
X-Identity-Key: id3
16+
Fcc: imap://alice%[email protected]/Sent
17+
Content-Type: multipart/signed; micalg=pgp-sha256;
18+
protocol="application/pgp-signature";
19+
boundary="------------iX39J1p7DOgblwacjo0e7jX7"
20+
21+
This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
22+
--------------iX39J1p7DOgblwacjo0e7jX7
23+
Content-Type: multipart/mixed; boundary="------------WD4DG7TcI4p4lbzyM4toRaDw";
24+
protected-headers="v1"
25+
From: Alice <[email protected]>
26+
27+
Message-ID: <[email protected]>
28+
Subject: test message 15:53
29+
30+
--------------WD4DG7TcI4p4lbzyM4toRaDw
31+
Content-Type: text/plain; charset=UTF-8; format=flowed
32+
Content-Transfer-Encoding: base64
33+
34+
DQo=
35+
36+
--------------WD4DG7TcI4p4lbzyM4toRaDw--
37+
38+
--------------iX39J1p7DOgblwacjo0e7jX7
39+
Content-Type: application/pgp-signature; name="OpenPGP_signature.asc"
40+
Content-Description: OpenPGP digital signature
41+
Content-Disposition: attachment; filename="OpenPGP_signature"
42+
43+
-----BEGIN PGP SIGNATURE-----
44+
45+
wsD5BAABCAAjFiEEFKs/ZfwnS721+naMJfAHJFnkeuIFAmObMvwFAwAAAAAACgkQJfAHJFnkeuLM
46+
TgwAnAADX93HE5vXmuBcAbRN2HKIwMzBtRtUF4FNPKchffUvvhSNpHkW2jW7A4hOHNgVSDQdqIUn
47+
+62NgkaKrT1bZqozOZNHXMECHtKBwXWTkIAVqcBdvscCztVIgGby56OPnzZ5y09BsRaqqE5AhDgN
48+
wGCLa6ipu5FYSF6+KzdO0GIPMY5aGRgVhtl4N01v4S3+r/Yu60MkN87nd15Eaqsrs60P9RmKJTt4
49+
hDie35kKvHnPzLNs8+xLfqPuO/P7ZbPQgkgCwMAMsMDRUYOv+k5c/bL3PKiOENuDpQ7dkKJ2OzSn
50+
nTcg8qhDf17vWe26C/QBhFiGEsrHNBQ1KW5by+cqjIUBJgXElFnPl35S5L3fn6JHZLcz6q+wQuJu
51+
vGT1mJuP//jLFkMHSexukFIVXzn41rWPLd05rBqMgwRcOHMIyzE9zaO1aa8MF2TirPaZ5lH9rx/y
52+
9DCU/d2sqbbYt8TGqj4hM3pqg5K22eq4KT1W7y8+28I5QfjZumLLrHBdYTnR
53+
=6JTB
54+
-----END PGP SIGNATURE-----
55+
56+
--------------iX39J1p7DOgblwacjo0e7jX7--

0 commit comments

Comments
 (0)