mirror of
https://github.com/chatmail/core.git
synced 2026-05-20 07:16:31 +03:00
refactor: make min_verified a boolean
We either need a securejoin or autocrypt key, there are no intermediate states.
This commit is contained in:
@@ -34,7 +34,7 @@ use crate::message::{self, Message, MessageState, MsgId, Viewtype};
|
|||||||
use crate::mimefactory::MimeFactory;
|
use crate::mimefactory::MimeFactory;
|
||||||
use crate::mimeparser::SystemMessage;
|
use crate::mimeparser::SystemMessage;
|
||||||
use crate::param::{Param, Params};
|
use crate::param::{Param, Params};
|
||||||
use crate::peerstate::{Peerstate, PeerstateVerifiedStatus};
|
use crate::peerstate::Peerstate;
|
||||||
use crate::receive_imf::ReceivedMsg;
|
use crate::receive_imf::ReceivedMsg;
|
||||||
use crate::smtp::send_msg_to_smtp;
|
use crate::smtp::send_msg_to_smtp;
|
||||||
use crate::sql;
|
use crate::sql;
|
||||||
@@ -1202,11 +1202,7 @@ impl ChatId {
|
|||||||
let peerstate = Peerstate::from_addr(context, addr).await?;
|
let peerstate = Peerstate::from_addr(context, addr).await?;
|
||||||
|
|
||||||
match peerstate
|
match peerstate
|
||||||
.filter(|peerstate| {
|
.filter(|peerstate| peerstate.peek_key(false).is_some())
|
||||||
peerstate
|
|
||||||
.peek_key(PeerstateVerifiedStatus::Unverified)
|
|
||||||
.is_some()
|
|
||||||
})
|
|
||||||
.map(|peerstate| peerstate.prefer_encrypt)
|
.map(|peerstate| peerstate.prefer_encrypt)
|
||||||
{
|
{
|
||||||
Some(EncryptPreference::Mutual) => ret_mutual += &format!("{addr}\n"),
|
Some(EncryptPreference::Mutual) => ret_mutual += &format!("{addr}\n"),
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ use crate::login_param::LoginParam;
|
|||||||
use crate::message::MessageState;
|
use crate::message::MessageState;
|
||||||
use crate::mimeparser::AvatarAction;
|
use crate::mimeparser::AvatarAction;
|
||||||
use crate::param::{Param, Params};
|
use crate::param::{Param, Params};
|
||||||
use crate::peerstate::{Peerstate, PeerstateVerifiedStatus};
|
use crate::peerstate::Peerstate;
|
||||||
use crate::sql::{self, params_iter};
|
use crate::sql::{self, params_iter};
|
||||||
use crate::sync::{self, Sync::*, SyncData};
|
use crate::sync::{self, Sync::*, SyncData};
|
||||||
use crate::tools::{
|
use crate::tools::{
|
||||||
@@ -1037,11 +1037,9 @@ impl Contact {
|
|||||||
let loginparam = LoginParam::load_configured_params(context).await?;
|
let loginparam = LoginParam::load_configured_params(context).await?;
|
||||||
let peerstate = Peerstate::from_addr(context, &contact.addr).await?;
|
let peerstate = Peerstate::from_addr(context, &contact.addr).await?;
|
||||||
|
|
||||||
if let Some(peerstate) = peerstate.filter(|peerstate| {
|
if let Some(peerstate) =
|
||||||
peerstate
|
peerstate.filter(|peerstate| peerstate.peek_key(false).is_some())
|
||||||
.peek_key(PeerstateVerifiedStatus::Unverified)
|
{
|
||||||
.is_some()
|
|
||||||
}) {
|
|
||||||
let stock_message = match peerstate.prefer_encrypt {
|
let stock_message = match peerstate.prefer_encrypt {
|
||||||
EncryptPreference::Mutual => stock_str::e2e_preferred(context).await,
|
EncryptPreference::Mutual => stock_str::e2e_preferred(context).await,
|
||||||
EncryptPreference::NoPreference => stock_str::e2e_available(context).await,
|
EncryptPreference::NoPreference => stock_str::e2e_available(context).await,
|
||||||
@@ -1056,11 +1054,11 @@ impl Contact {
|
|||||||
.fingerprint()
|
.fingerprint()
|
||||||
.to_string();
|
.to_string();
|
||||||
let fingerprint_other_verified = peerstate
|
let fingerprint_other_verified = peerstate
|
||||||
.peek_key(PeerstateVerifiedStatus::BidirectVerified)
|
.peek_key(true)
|
||||||
.map(|k| k.fingerprint().to_string())
|
.map(|k| k.fingerprint().to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let fingerprint_other_unverified = peerstate
|
let fingerprint_other_unverified = peerstate
|
||||||
.peek_key(PeerstateVerifiedStatus::Unverified)
|
.peek_key(false)
|
||||||
.map(|k| k.fingerprint().to_string())
|
.map(|k| k.fingerprint().to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
if loginparam.addr < peerstate.addr {
|
if loginparam.addr < peerstate.addr {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use crate::aheader::{Aheader, EncryptPreference};
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::key::{load_self_public_key, load_self_secret_key, SignedPublicKey};
|
use crate::key::{load_self_public_key, load_self_secret_key, SignedPublicKey};
|
||||||
use crate::peerstate::{Peerstate, PeerstateVerifiedStatus};
|
use crate::peerstate::Peerstate;
|
||||||
use crate::pgp;
|
use crate::pgp;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -94,7 +94,7 @@ impl EncryptHelper {
|
|||||||
pub async fn encrypt(
|
pub async fn encrypt(
|
||||||
self,
|
self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
min_verified: PeerstateVerifiedStatus,
|
min_verified: bool,
|
||||||
mail_to_encrypt: lettre_email::PartBuilder,
|
mail_to_encrypt: lettre_email::PartBuilder,
|
||||||
peerstates: Vec<(Option<Peerstate>, &str)>,
|
peerstates: Vec<(Option<Peerstate>, &str)>,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
@@ -118,7 +118,7 @@ impl EncryptHelper {
|
|||||||
|
|
||||||
// Encrypt to secondary verified keys
|
// Encrypt to secondary verified keys
|
||||||
// if we also encrypt to the introducer ("verifier") of the key.
|
// if we also encrypt to the introducer ("verifier") of the key.
|
||||||
if min_verified == PeerstateVerifiedStatus::BidirectVerified {
|
if min_verified {
|
||||||
for (peerstate, _addr) in peerstates {
|
for (peerstate, _addr) in peerstates {
|
||||||
if let Some(peerstate) = peerstate {
|
if let Some(peerstate) = peerstate {
|
||||||
if let (Some(key), Some(verifier)) = (
|
if let (Some(key), Some(verifier)) = (
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ use crate::location;
|
|||||||
use crate::message::{self, Message, MsgId, Viewtype};
|
use crate::message::{self, Message, MsgId, Viewtype};
|
||||||
use crate::mimeparser::SystemMessage;
|
use crate::mimeparser::SystemMessage;
|
||||||
use crate::param::Param;
|
use crate::param::Param;
|
||||||
use crate::peerstate::{Peerstate, PeerstateVerifiedStatus};
|
use crate::peerstate::Peerstate;
|
||||||
use crate::simplify::escape_message_footer_marks;
|
use crate::simplify::escape_message_footer_marks;
|
||||||
use crate::stock_str;
|
use crate::stock_str;
|
||||||
use crate::tools::IsNoneOrEmpty;
|
use crate::tools::IsNoneOrEmpty;
|
||||||
@@ -312,7 +312,7 @@ impl<'a> MimeFactory<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn min_verified(&self) -> PeerstateVerifiedStatus {
|
fn min_verified(&self) -> bool {
|
||||||
match &self.loaded {
|
match &self.loaded {
|
||||||
Loaded::Message { chat } => {
|
Loaded::Message { chat } => {
|
||||||
if chat.is_protected() {
|
if chat.is_protected() {
|
||||||
@@ -321,15 +321,15 @@ impl<'a> MimeFactory<'a> {
|
|||||||
// In order to do this, it is necessary that they can be sent
|
// In order to do this, it is necessary that they can be sent
|
||||||
// to a key that is not yet verified.
|
// to a key that is not yet verified.
|
||||||
// This has to work independently of whether the chat is protected right now.
|
// This has to work independently of whether the chat is protected right now.
|
||||||
PeerstateVerifiedStatus::Unverified
|
false
|
||||||
} else {
|
} else {
|
||||||
PeerstateVerifiedStatus::BidirectVerified
|
true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PeerstateVerifiedStatus::Unverified
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loaded::Mdn { .. } => PeerstateVerifiedStatus::Unverified,
|
Loaded::Mdn { .. } => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -373,7 +373,7 @@ impl Peerstate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the contents of the `Autocrypt-Gossip` header for outgoing messages.
|
/// Returns the contents of the `Autocrypt-Gossip` header for outgoing messages.
|
||||||
pub fn render_gossip_header(&self, min_verified: PeerstateVerifiedStatus) -> Option<String> {
|
pub fn render_gossip_header(&self, min_verified: bool) -> Option<String> {
|
||||||
if let Some(key) = self.peek_key(min_verified) {
|
if let Some(key) = self.peek_key(min_verified) {
|
||||||
let header = Aheader::new(
|
let header = Aheader::new(
|
||||||
self.addr.clone(),
|
self.addr.clone(),
|
||||||
@@ -397,12 +397,11 @@ impl Peerstate {
|
|||||||
/// Converts the peerstate into the contact public key.
|
/// Converts the peerstate into the contact public key.
|
||||||
///
|
///
|
||||||
/// Similar to [`Self::peek_key`], but consumes the peerstate and returns owned key.
|
/// Similar to [`Self::peek_key`], but consumes the peerstate and returns owned key.
|
||||||
pub fn take_key(mut self, min_verified: PeerstateVerifiedStatus) -> Option<SignedPublicKey> {
|
pub fn take_key(mut self, min_verified: bool) -> Option<SignedPublicKey> {
|
||||||
match min_verified {
|
if min_verified {
|
||||||
PeerstateVerifiedStatus::BidirectVerified => self.verified_key.take(),
|
self.verified_key.take()
|
||||||
PeerstateVerifiedStatus::Unverified => {
|
} else {
|
||||||
self.public_key.take().or_else(|| self.gossip_key.take())
|
self.public_key.take().or_else(|| self.gossip_key.take())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -415,25 +414,24 @@ impl Peerstate {
|
|||||||
/// Returned key is suitable for sending in `Autocrypt-Gossip` header.
|
/// Returned key is suitable for sending in `Autocrypt-Gossip` header.
|
||||||
///
|
///
|
||||||
/// Returns `None` if there is no suitable public key.
|
/// Returns `None` if there is no suitable public key.
|
||||||
pub fn peek_key(&self, min_verified: PeerstateVerifiedStatus) -> Option<&SignedPublicKey> {
|
pub fn peek_key(&self, min_verified: bool) -> Option<&SignedPublicKey> {
|
||||||
match min_verified {
|
if min_verified {
|
||||||
PeerstateVerifiedStatus::BidirectVerified => self.verified_key.as_ref(),
|
self.verified_key.as_ref()
|
||||||
PeerstateVerifiedStatus::Unverified => {
|
} else {
|
||||||
self.public_key.as_ref().or(self.gossip_key.as_ref())
|
self.public_key.as_ref().or(self.gossip_key.as_ref())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the contact's public key fingerprint.
|
/// Returns a reference to the contact's public key fingerprint.
|
||||||
///
|
///
|
||||||
/// Similar to [`Self::peek_key`], but returns the fingerprint instead of the key.
|
/// Similar to [`Self::peek_key`], but returns the fingerprint instead of the key.
|
||||||
fn peek_key_fingerprint(&self, min_verified: PeerstateVerifiedStatus) -> Option<&Fingerprint> {
|
fn peek_key_fingerprint(&self, min_verified: bool) -> Option<&Fingerprint> {
|
||||||
match min_verified {
|
if min_verified {
|
||||||
PeerstateVerifiedStatus::BidirectVerified => self.verified_key_fingerprint.as_ref(),
|
self.verified_key_fingerprint.as_ref()
|
||||||
PeerstateVerifiedStatus::Unverified => self
|
} else {
|
||||||
.public_key_fingerprint
|
self.public_key_fingerprint
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.or(self.gossip_key_fingerprint.as_ref()),
|
.or(self.gossip_key_fingerprint.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,10 +441,9 @@ impl Peerstate {
|
|||||||
/// Note that verified groups always use the verified key no matter if the
|
/// Note that verified groups always use the verified key no matter if the
|
||||||
/// opportunistic key matches or not.
|
/// opportunistic key matches or not.
|
||||||
pub(crate) fn is_using_verified_key(&self) -> bool {
|
pub(crate) fn is_using_verified_key(&self) -> bool {
|
||||||
let verified = self.peek_key_fingerprint(PeerstateVerifiedStatus::BidirectVerified);
|
let verified = self.peek_key_fingerprint(true);
|
||||||
|
|
||||||
verified.is_some()
|
verified.is_some() && verified == self.peek_key_fingerprint(false)
|
||||||
&& verified == self.peek_key_fingerprint(PeerstateVerifiedStatus::Unverified)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set this peerstate to verified
|
/// Set this peerstate to verified
|
||||||
|
|||||||
Reference in New Issue
Block a user