refactor: resultify get_self_fingerprint()

This commit is contained in:
link2xt
2024-10-21 11:46:25 +00:00
parent c5cadd9991
commit aa71fbe04c
2 changed files with 18 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
//! Implementation of [SecureJoin protocols](https://securejoin.delta.chat/). //! Implementation of [SecureJoin protocols](https://securejoin.delta.chat/).
use anyhow::{bail, ensure, Context as _, Error, Result}; use anyhow::{ensure, Context as _, Error, Result};
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC}; use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use crate::aheader::EncryptPreference; use crate::aheader::EncryptPreference;
@@ -89,12 +89,7 @@ pub async fn get_securejoin_qr(context: &Context, group: Option<ChatId>) -> Resu
.await? .await?
.unwrap_or_default(); .unwrap_or_default();
let fingerprint: Fingerprint = match get_self_fingerprint(context).await { let fingerprint = get_self_fingerprint(context).await?;
Some(fp) => fp,
None => {
bail!("No fingerprint, cannot generate QR code.");
}
};
let self_addr_urlencoded = let self_addr_urlencoded =
utf8_percent_encode(&self_addr, NON_ALPHANUMERIC_WITHOUT_DOT).to_string(); utf8_percent_encode(&self_addr, NON_ALPHANUMERIC_WITHOUT_DOT).to_string();
@@ -140,14 +135,11 @@ pub async fn get_securejoin_qr(context: &Context, group: Option<ChatId>) -> Resu
Ok(qr) Ok(qr)
} }
async fn get_self_fingerprint(context: &Context) -> Option<Fingerprint> { async fn get_self_fingerprint(context: &Context) -> Result<Fingerprint> {
match load_self_public_key(context).await { let key = load_self_public_key(context)
Ok(key) => Some(key.fingerprint()), .await
Err(_) => { .context("Failed to load key")?;
warn!(context, "get_self_fingerprint(): failed to load key"); Ok(key.fingerprint())
None
}
}
} }
/// Take a scanned QR-code and do the setup-contact/join-group/invite handshake. /// Take a scanned QR-code and do the setup-contact/join-group/invite handshake.
@@ -388,7 +380,7 @@ pub(crate) async fn handle_securejoin_handshake(
return Ok(HandshakeMessage::Ignore); return Ok(HandshakeMessage::Ignore);
}; };
let fingerprint: Fingerprint = fp.parse()?; let fingerprint: Fingerprint = fp.parse()?;
if !encrypted_and_signed(context, mime_message, Some(&fingerprint)) { if !encrypted_and_signed(context, mime_message, &fingerprint) {
warn!( warn!(
context, context,
"Ignoring {step} message because the message is not encrypted." "Ignoring {step} message because the message is not encrypted."
@@ -577,11 +569,7 @@ pub(crate) async fn observe_securejoin_on_other_device(
return Ok(HandshakeMessage::Ignore); return Ok(HandshakeMessage::Ignore);
}; };
if !encrypted_and_signed( if !encrypted_and_signed(context, mime_message, &get_self_fingerprint(context).await?) {
context,
mime_message,
get_self_fingerprint(context).await.as_ref(),
) {
could_not_establish_secure_connection( could_not_establish_secure_connection(
context, context,
contact_id, contact_id,
@@ -740,24 +728,19 @@ async fn mark_peer_as_verified(
fn encrypted_and_signed( fn encrypted_and_signed(
context: &Context, context: &Context,
mimeparser: &MimeMessage, mimeparser: &MimeMessage,
expected_fingerprint: Option<&Fingerprint>, expected_fingerprint: &Fingerprint,
) -> bool { ) -> bool {
if !mimeparser.was_encrypted() { if !mimeparser.was_encrypted() {
warn!(context, "Message not encrypted.",); warn!(context, "Message not encrypted.",);
false false
} else if let Some(expected_fingerprint) = expected_fingerprint { } else if !mimeparser.signatures.contains(expected_fingerprint) {
if !mimeparser.signatures.contains(expected_fingerprint) { warn!(
warn!( context,
context, "Message does not match expected fingerprint {}.", expected_fingerprint,
"Message does not match expected fingerprint {}.", expected_fingerprint, );
);
false
} else {
true
}
} else {
warn!(context, "Fingerprint for comparison missing.");
false false
} else {
true
} }
} }

View File

@@ -270,7 +270,7 @@ impl BobState {
context, context,
"Bob Step 4 - handling {{vc,vg}}-auth-required message." "Bob Step 4 - handling {{vc,vg}}-auth-required message."
); );
if !encrypted_and_signed(context, mime_message, Some(self.invite.fingerprint())) { if !encrypted_and_signed(context, mime_message, self.invite.fingerprint()) {
let reason = if mime_message.was_encrypted() { let reason = if mime_message.was_encrypted() {
"Valid signature missing" "Valid signature missing"
} else { } else {