fix: do not compress SecureJoin messages

This commit is contained in:
link2xt
2024-02-09 11:14:06 +00:00
parent 3c4c701f9b
commit b970ebe67a
3 changed files with 31 additions and 16 deletions

View File

@@ -95,6 +95,7 @@ impl EncryptHelper {
verified: bool, verified: bool,
mail_to_encrypt: lettre_email::PartBuilder, mail_to_encrypt: lettre_email::PartBuilder,
peerstates: Vec<(Option<Peerstate>, String)>, peerstates: Vec<(Option<Peerstate>, String)>,
compress: bool,
) -> Result<String> { ) -> Result<String> {
let mut keyring: Vec<SignedPublicKey> = Vec::new(); let mut keyring: Vec<SignedPublicKey> = Vec::new();
@@ -135,7 +136,7 @@ impl EncryptHelper {
let raw_message = mail_to_encrypt.build().as_string().into_bytes(); let raw_message = mail_to_encrypt.build().as_string().into_bytes();
let ctext = pgp::pk_encrypt(&raw_message, keyring, Some(sign_key)).await?; let ctext = pgp::pk_encrypt(&raw_message, keyring, Some(sign_key), compress).await?;
Ok(ctext) Ok(ctext)
} }

View File

@@ -734,8 +734,12 @@ impl<'a> MimeFactory<'a> {
); );
} }
// Disable compression for SecureJoin to ensure
// there are no compression side channels
// leaking information about the tokens.
let compress = self.msg.param.get_cmd() != SystemMessage::SecurejoinMessage;
let encrypted = encrypt_helper let encrypted = encrypt_helper
.encrypt(context, verified, message, peerstates) .encrypt(context, verified, message, peerstates, compress)
.await?; .await?;
outer_message outer_message

View File

@@ -236,6 +236,7 @@ pub async fn pk_encrypt(
plain: &[u8], plain: &[u8],
public_keys_for_encryption: Vec<SignedPublicKey>, public_keys_for_encryption: Vec<SignedPublicKey>,
private_key_for_signing: Option<SignedSecretKey>, private_key_for_signing: Option<SignedSecretKey>,
compress: bool,
) -> Result<String> { ) -> Result<String> {
let lit_msg = Message::new_literal_bytes("", plain); let lit_msg = Message::new_literal_bytes("", plain);
@@ -249,20 +250,19 @@ pub async fn pk_encrypt(
let mut rng = thread_rng(); let mut rng = thread_rng();
// TODO: measure time
let encrypted_msg = if let Some(ref skey) = private_key_for_signing { let encrypted_msg = if let Some(ref skey) = private_key_for_signing {
lit_msg let signed_msg = lit_msg.sign(skey, || "".into(), HASH_ALGORITHM)?;
.sign(skey, || "".into(), HASH_ALGORITHM) let compressed_msg = if compress {
.and_then(|msg| msg.compress(CompressionAlgorithm::ZLIB)) signed_msg.compress(CompressionAlgorithm::ZLIB)?
.and_then(|msg| { } else {
msg.encrypt_to_keys(&mut rng, SYMMETRIC_KEY_ALGORITHM, &pkeys_refs) signed_msg
}) };
compressed_msg.encrypt_to_keys(&mut rng, SYMMETRIC_KEY_ALGORITHM, &pkeys_refs)?
} else { } else {
lit_msg.encrypt_to_keys(&mut rng, SYMMETRIC_KEY_ALGORITHM, &pkeys_refs) lit_msg.encrypt_to_keys(&mut rng, SYMMETRIC_KEY_ALGORITHM, &pkeys_refs)?
}; };
let msg = encrypted_msg?; let encoded_msg = encrypted_msg.to_armored_string(None)?;
let encoded_msg = msg.to_armored_string(None)?;
Ok(encoded_msg) Ok(encoded_msg)
}) })
@@ -484,10 +484,16 @@ mod tests {
CTEXT_SIGNED CTEXT_SIGNED
.get_or_init(|| async { .get_or_init(|| async {
let keyring = vec![KEYS.alice_public.clone(), KEYS.bob_public.clone()]; let keyring = vec![KEYS.alice_public.clone(), KEYS.bob_public.clone()];
let compress = true;
pk_encrypt(CLEARTEXT, keyring, Some(KEYS.alice_secret.clone())) pk_encrypt(
.await CLEARTEXT,
.unwrap() keyring,
Some(KEYS.alice_secret.clone()),
compress,
)
.await
.unwrap()
}) })
.await .await
} }
@@ -497,7 +503,11 @@ mod tests {
CTEXT_UNSIGNED CTEXT_UNSIGNED
.get_or_init(|| async { .get_or_init(|| async {
let keyring = vec![KEYS.alice_public.clone(), KEYS.bob_public.clone()]; let keyring = vec![KEYS.alice_public.clone(), KEYS.bob_public.clone()];
pk_encrypt(CLEARTEXT, keyring, None).await.unwrap() let compress = true;
pk_encrypt(CLEARTEXT, keyring, None, compress)
.await
.unwrap()
}) })
.await .await
} }