deltachat/
securejoin.rs

1//! Implementation of [SecureJoin protocols](https://securejoin.delta.chat/).
2
3use anyhow::{Context as _, Error, Result, bail, ensure};
4use deltachat_contact_tools::ContactAddress;
5use percent_encoding::{AsciiSet, utf8_percent_encode};
6
7use crate::chat::{
8    self, Chat, ChatId, ChatIdBlocked, add_info_msg, get_chat_id_by_grpid, load_broadcast_secret,
9};
10use crate::config::Config;
11use crate::constants::{
12    BROADCAST_INCOMPATIBILITY_MSG, Blocked, Chattype, NON_ALPHANUMERIC_WITHOUT_DOT,
13};
14use crate::contact::mark_contact_id_as_verified;
15use crate::contact::{Contact, ContactId, Origin};
16use crate::context::Context;
17use crate::e2ee::ensure_secret_key_exists;
18use crate::events::EventType;
19use crate::headerdef::HeaderDef;
20use crate::key::{DcKey, Fingerprint, load_self_public_key};
21use crate::log::LogExt as _;
22use crate::log::warn;
23use crate::message::{Message, Viewtype};
24use crate::mimeparser::{MimeMessage, SystemMessage};
25use crate::param::Param;
26use crate::qr::check_qr;
27use crate::securejoin::bob::JoinerProgress;
28use crate::sync::Sync::*;
29use crate::tools::{create_id, time};
30use crate::{SecurejoinSource, stats};
31use crate::{SecurejoinUiPath, token};
32
33mod bob;
34mod qrinvite;
35
36pub(crate) use qrinvite::QrInvite;
37
38use crate::token::Namespace;
39
40/// Only new QR codes cause a verification on Alice's side.
41/// When a QR code is too old, it is assumed that there was no direct QR scan,
42/// and that the QR code was potentially published on a website,
43/// so, Alice doesn't mark Bob as verified.
44// TODO For backwards compatibility reasons, this is still using a rather large value.
45// Set this to a lower value (e.g. 10 minutes)
46// when Delta Chat v2.22.0 is sufficiently rolled out
47const VERIFICATION_TIMEOUT_SECONDS: i64 = 7 * 24 * 3600;
48
49const DISALLOWED_CHARACTERS: &AsciiSet = &NON_ALPHANUMERIC_WITHOUT_DOT.remove(b'_');
50
51fn inviter_progress(
52    context: &Context,
53    contact_id: ContactId,
54    chat_id: ChatId,
55    chat_type: Chattype,
56) -> Result<()> {
57    // No other values are used.
58    let progress = 1000;
59    context.emit_event(EventType::SecurejoinInviterProgress {
60        contact_id,
61        chat_id,
62        chat_type,
63        progress,
64    });
65
66    Ok(())
67}
68
69/// Shorten name to max. `length` characters.
70/// This is to not make QR codes or invite links arbitrary long.
71fn shorten_name(name: &str, length: usize) -> String {
72    if name.chars().count() > length {
73        // We use _ rather than ... to avoid dots at the end of the URL, which would confuse linkifiers
74        format!(
75            "{}_",
76            &name
77                .chars()
78                .take(length.saturating_sub(1))
79                .collect::<String>()
80        )
81    } else {
82        name.to_string()
83    }
84}
85
86/// Generates a Secure Join QR code.
87///
88/// With `chat` set to `None` this generates a setup-contact QR code, with `chat` set to a
89/// [`ChatId`] generates a join-group/join-broadcast-channel QR code for the given chat.
90pub async fn get_securejoin_qr(context: &Context, chat: Option<ChatId>) -> Result<String> {
91    /*=======================================================
92    ====             Alice - the inviter side            ====
93    ====   Step 1 in "Setup verified contact" protocol   ====
94    =======================================================*/
95
96    ensure_secret_key_exists(context).await.ok();
97
98    let chat = match chat {
99        Some(id) => {
100            let chat = Chat::load_from_db(context, id).await?;
101            ensure!(
102                chat.typ == Chattype::Group || chat.typ == Chattype::OutBroadcast,
103                "Can't generate SecureJoin QR code for chat {id} of type {}",
104                chat.typ
105            );
106            if chat.grpid.is_empty() {
107                let err = format!("Can't generate QR code, chat {id} is a email thread");
108                error!(context, "get_securejoin_qr: {}.", err);
109                bail!(err);
110            }
111            if chat.typ == Chattype::OutBroadcast {
112                // If the user created the broadcast before updating Delta Chat,
113                // then the secret will be missing, and the user needs to recreate the broadcast:
114                if load_broadcast_secret(context, chat.id).await?.is_none() {
115                    error!(
116                        context,
117                        "Not creating securejoin QR for old broadcast {}, see chat for more info.",
118                        chat.id,
119                    );
120                    let text = BROADCAST_INCOMPATIBILITY_MSG;
121                    add_info_msg(context, chat.id, text, time()).await?;
122                    bail!(text.to_string());
123                }
124            }
125            Some(chat)
126        }
127        None => None,
128    };
129    let grpid = chat.as_ref().map(|c| c.grpid.as_str());
130    let sync_token = token::lookup(context, Namespace::InviteNumber, grpid)
131        .await?
132        .is_none();
133    // Invite number is used to request the inviter key.
134    let invitenumber = token::lookup_or_new(context, Namespace::InviteNumber, grpid).await?;
135
136    // Auth token is used to verify the key-contact
137    // if the token is not old
138    // and add the contact to the group
139    // if there is an associated group ID.
140    //
141    // We always generate a new auth token
142    // because auth tokens "expire"
143    // and can only be used to join groups
144    // without verification afterwards.
145    let auth = create_id();
146    token::save(context, Namespace::Auth, grpid, &auth, time()).await?;
147
148    let fingerprint = get_self_fingerprint(context).await?.hex();
149
150    let self_addr = context.get_primary_self_addr().await?;
151    let self_addr_urlencoded = utf8_percent_encode(&self_addr, DISALLOWED_CHARACTERS).to_string();
152
153    let self_name = context
154        .get_config(Config::Displayname)
155        .await?
156        .unwrap_or_default();
157
158    let qr = if let Some(chat) = chat {
159        if sync_token {
160            context
161                .sync_qr_code_tokens(Some(chat.grpid.as_str()))
162                .await?;
163            context.scheduler.interrupt_inbox().await;
164        }
165
166        let chat_name = chat.get_name();
167        let chat_name_shortened = shorten_name(chat_name, 25);
168        let chat_name_urlencoded = utf8_percent_encode(&chat_name_shortened, DISALLOWED_CHARACTERS)
169            .to_string()
170            .replace("%20", "+");
171        let grpid = &chat.grpid;
172
173        let self_name_shortened = shorten_name(&self_name, 16);
174        let self_name_urlencoded = utf8_percent_encode(&self_name_shortened, DISALLOWED_CHARACTERS)
175            .to_string()
176            .replace("%20", "+");
177
178        if chat.typ == Chattype::OutBroadcast {
179            // For historic reansons, broadcasts currently use j instead of i for the invitenumber.
180            format!(
181                "https://i.delta.chat/#{fingerprint}&x={grpid}&j={invitenumber}&s={auth}&a={self_addr_urlencoded}&n={self_name_urlencoded}&b={chat_name_urlencoded}",
182            )
183        } else {
184            format!(
185                "https://i.delta.chat/#{fingerprint}&x={grpid}&i={invitenumber}&s={auth}&a={self_addr_urlencoded}&n={self_name_urlencoded}&g={chat_name_urlencoded}",
186            )
187        }
188    } else {
189        let self_name_shortened = shorten_name(&self_name, 25);
190        let self_name_urlencoded = utf8_percent_encode(&self_name_shortened, DISALLOWED_CHARACTERS)
191            .to_string()
192            .replace("%20", "+");
193        if sync_token {
194            context.sync_qr_code_tokens(None).await?;
195            context.scheduler.interrupt_inbox().await;
196        }
197        format!(
198            "https://i.delta.chat/#{fingerprint}&i={invitenumber}&s={auth}&a={self_addr_urlencoded}&n={self_name_urlencoded}",
199        )
200    };
201
202    info!(context, "Generated QR code.");
203    Ok(qr)
204}
205
206async fn get_self_fingerprint(context: &Context) -> Result<Fingerprint> {
207    let key = load_self_public_key(context)
208        .await
209        .context("Failed to load key")?;
210    Ok(key.dc_fingerprint())
211}
212
213/// Take a scanned QR-code and do the setup-contact/join-group/invite handshake.
214///
215/// This is the start of the process for the joiner.  See the module and ffi documentation
216/// for more details.
217///
218/// The function returns immediately and the handshake will run in background.
219pub async fn join_securejoin(context: &Context, qr: &str) -> Result<ChatId> {
220    join_securejoin_with_ux_info(context, qr, None, None).await
221}
222
223/// Take a scanned QR-code and do the setup-contact/join-group/invite handshake.
224///
225/// This is the start of the process for the joiner.  See the module and ffi documentation
226/// for more details.
227///
228/// The function returns immediately and the handshake will run in background.
229///
230/// **source** and **uipath** are for statistics-sending,
231/// if the user enabled it in the settings;
232/// if you don't have statistics-sending implemented, just pass `None` here.
233pub async fn join_securejoin_with_ux_info(
234    context: &Context,
235    qr: &str,
236    source: Option<SecurejoinSource>,
237    uipath: Option<SecurejoinUiPath>,
238) -> Result<ChatId> {
239    let res = securejoin(context, qr).await.map_err(|err| {
240        warn!(context, "Fatal joiner error: {:#}", err);
241        // The user just scanned this QR code so has context on what failed.
242        error!(context, "QR process failed");
243        err
244    })?;
245
246    stats::count_securejoin_ux_info(context, source, uipath)
247        .await
248        .log_err(context)
249        .ok();
250
251    Ok(res)
252}
253
254async fn securejoin(context: &Context, qr: &str) -> Result<ChatId> {
255    /*========================================================
256    ====             Bob - the joiner's side             =====
257    ====   Step 2 in "Setup verified contact" protocol   =====
258    ========================================================*/
259
260    info!(context, "Requesting secure-join ...",);
261    let qr_scan = check_qr(context, qr).await?;
262
263    let invite = QrInvite::try_from(qr_scan)?;
264
265    stats::count_securejoin_invite(context, &invite)
266        .await
267        .log_err(context)
268        .ok();
269
270    bob::start_protocol(context, invite).await
271}
272
273/// Send handshake message from Alice's device.
274async fn send_alice_handshake_msg(
275    context: &Context,
276    contact_id: ContactId,
277    step: &str,
278) -> Result<()> {
279    let mut msg = Message {
280        viewtype: Viewtype::Text,
281        text: format!("Secure-Join: {step}"),
282        hidden: true,
283        ..Default::default()
284    };
285    msg.param.set_cmd(SystemMessage::SecurejoinMessage);
286    msg.param.set(Param::Arg, step);
287    msg.param.set_int(Param::GuaranteeE2ee, 1);
288    chat::send_msg(
289        context,
290        ChatIdBlocked::get_for_contact(context, contact_id, Blocked::Yes)
291            .await?
292            .id,
293        &mut msg,
294    )
295    .await?;
296    Ok(())
297}
298
299/// Get an unblocked chat that can be used for info messages.
300async fn info_chat_id(context: &Context, contact_id: ContactId) -> Result<ChatId> {
301    let chat_id_blocked = ChatIdBlocked::get_for_contact(context, contact_id, Blocked::Not).await?;
302    Ok(chat_id_blocked.id)
303}
304
305/// Checks fingerprint and marks the contact as verified
306/// if fingerprint matches.
307async fn verify_sender_by_fingerprint(
308    context: &Context,
309    fingerprint: &Fingerprint,
310    contact_id: ContactId,
311) -> Result<bool> {
312    let contact = Contact::get_by_id(context, contact_id).await?;
313    let is_verified = contact.fingerprint().is_some_and(|fp| &fp == fingerprint);
314    if is_verified {
315        mark_contact_id_as_verified(context, contact_id, Some(ContactId::SELF)).await?;
316    }
317    Ok(is_verified)
318}
319
320/// What to do with a Secure-Join handshake message after it was handled.
321///
322/// This status is returned to [`receive_imf_inner`] which will use it to decide what to do
323/// next with this incoming setup-contact/secure-join handshake message.
324///
325/// [`receive_imf_inner`]: crate::receive_imf::receive_imf_inner
326#[derive(Debug, PartialEq, Eq)]
327pub(crate) enum HandshakeMessage {
328    /// The message has been fully handled and should be removed/delete.
329    ///
330    /// This removes the message both locally and on the IMAP server.
331    Done,
332    /// The message should be ignored/hidden, but not removed/deleted.
333    ///
334    /// This leaves it on the IMAP server.  It means other devices on this account can
335    /// receive and potentially process this message as well.  This is useful for example
336    /// when the other device is running the protocol and has the relevant QR-code
337    /// information while this device does not have the joiner state.
338    Ignore,
339    /// The message should be further processed by incoming message handling.
340    ///
341    /// This may for example result in a group being created if it is a message which added
342    /// us to a group (a `vg-member-added` message).
343    Propagate,
344}
345
346/// Handle incoming secure-join handshake.
347///
348/// This function will update the securejoin state in the database as the protocol
349/// progresses.
350///
351/// A message which results in [`Err`] will be hidden from the user but not deleted, it may
352/// be a valid message for something else we are not aware off.  E.g. it could be part of a
353/// handshake performed by another DC app on the same account.
354///
355/// When `handle_securejoin_handshake()` is called, the message is not yet filed in the
356/// database; this is done by `receive_imf()` later on as needed.
357pub(crate) async fn handle_securejoin_handshake(
358    context: &Context,
359    mime_message: &mut MimeMessage,
360    contact_id: ContactId,
361) -> Result<HandshakeMessage> {
362    if contact_id.is_special() {
363        return Err(Error::msg("Can not be called with special contact ID"));
364    }
365    let step = mime_message
366        .get_header(HeaderDef::SecureJoin)
367        .context("Not a Secure-Join message")?;
368
369    info!(context, "Received secure-join message {step:?}.");
370
371    // Opportunistically protect against a theoretical 'surreptitious forwarding' attack:
372    // If Eve obtains a QR code from Alice and starts a securejoin with her,
373    // and also lets Bob scan a manipulated QR code,
374    // she could reencrypt the v*-request-with-auth message to Bob while maintaining the signature,
375    // and Bob would regard the message as valid.
376    //
377    // This attack is not actually relevant in any threat model,
378    // because if Eve can see Alice's QR code and have Bob scan a manipulated QR code,
379    // she can just do a classical MitM attack.
380    //
381    // Protecting all messages sent by Delta Chat against 'surreptitious forwarding'
382    // by checking the 'intended recipient fingerprint'
383    // will improve security (completely unrelated to the securejoin protocol)
384    // and is something we want to do in the future:
385    // https://www.rfc-editor.org/rfc/rfc9580.html#name-surreptitious-forwarding
386    if !matches!(step, "vg-request" | "vc-request") {
387        let mut self_found = false;
388        let self_fingerprint = load_self_public_key(context).await?.dc_fingerprint();
389        for (addr, key) in &mime_message.gossiped_keys {
390            if key.public_key.dc_fingerprint() == self_fingerprint
391                && context.is_self_addr(addr).await?
392            {
393                self_found = true;
394                break;
395            }
396        }
397        if !self_found {
398            // This message isn't intended for us. Possibly the peer doesn't own the key which the
399            // message is signed with but forwarded someone's message to us.
400            warn!(context, "Step {step}: No self addr+pubkey gossip found.");
401            return Ok(HandshakeMessage::Ignore);
402        }
403    }
404
405    match step {
406        "vg-request" | "vc-request" => {
407            /*=======================================================
408            ====             Alice - the inviter side            ====
409            ====   Step 3 in "Setup verified contact" protocol   ====
410            =======================================================*/
411
412            // this message may be unencrypted (Bob, the joiner and the sender, might not have Alice's key yet)
413            // it just ensures, we have Bobs key now. If we do _not_ have the key because eg. MitM has removed it,
414            // send_message() will fail with the error "End-to-end-encryption unavailable unexpectedly.", so, there is no additional check needed here.
415            // verify that the `Secure-Join-Invitenumber:`-header matches invitenumber written to the QR code
416            let invitenumber = match mime_message.get_header(HeaderDef::SecureJoinInvitenumber) {
417                Some(n) => n,
418                None => {
419                    warn!(context, "Secure-join denied (invitenumber missing)");
420                    return Ok(HandshakeMessage::Ignore);
421                }
422            };
423            if !token::exists(context, token::Namespace::InviteNumber, invitenumber).await? {
424                warn!(context, "Secure-join denied (bad invitenumber).");
425                return Ok(HandshakeMessage::Ignore);
426            }
427
428            let from_addr = ContactAddress::new(&mime_message.from.addr)?;
429            let autocrypt_fingerprint = mime_message.autocrypt_fingerprint.as_deref().unwrap_or("");
430            let (autocrypt_contact_id, _) = Contact::add_or_lookup_ex(
431                context,
432                "",
433                &from_addr,
434                autocrypt_fingerprint,
435                Origin::IncomingUnknownFrom,
436            )
437            .await?;
438
439            // Alice -> Bob
440            send_alice_handshake_msg(
441                context,
442                autocrypt_contact_id,
443                &format!("{}-auth-required", &step.get(..2).unwrap_or_default()),
444            )
445            .await
446            .context("failed sending auth-required handshake message")?;
447            Ok(HandshakeMessage::Done)
448        }
449        "vg-auth-required" | "vc-auth-required" => {
450            /*========================================================
451            ====             Bob - the joiner's side             =====
452            ====   Step 4 in "Setup verified contact" protocol   =====
453            ========================================================*/
454            bob::handle_auth_required(context, mime_message).await
455        }
456        "vg-request-with-auth" | "vc-request-with-auth" => {
457            /*==========================================================
458            ====              Alice - the inviter side              ====
459            ====   Steps 5+6 in "Setup verified contact" protocol   ====
460            ====  Step 6 in "Out-of-band verified groups" protocol  ====
461            ==========================================================*/
462
463            // verify that Secure-Join-Fingerprint:-header matches the fingerprint of Bob
464            let Some(fp) = mime_message.get_header(HeaderDef::SecureJoinFingerprint) else {
465                warn!(
466                    context,
467                    "Ignoring {step} message because fingerprint is not provided."
468                );
469                return Ok(HandshakeMessage::Ignore);
470            };
471            let fingerprint: Fingerprint = fp.parse()?;
472            if !encrypted_and_signed(context, mime_message, &fingerprint) {
473                warn!(
474                    context,
475                    "Ignoring {step} message because the message is not encrypted."
476                );
477                return Ok(HandshakeMessage::Ignore);
478            }
479            // verify that the `Secure-Join-Auth:`-header matches the secret written to the QR code
480            let Some(auth) = mime_message.get_header(HeaderDef::SecureJoinAuth) else {
481                warn!(
482                    context,
483                    "Ignoring {step} message because of missing auth code."
484                );
485                return Ok(HandshakeMessage::Ignore);
486            };
487            let Some((grpid, timestamp)) = context
488                .sql
489                .query_row_optional(
490                    "SELECT foreign_key, timestamp FROM tokens WHERE namespc=? AND token=?",
491                    (Namespace::Auth, auth),
492                    |row| {
493                        let foreign_key: String = row.get(0)?;
494                        let timestamp: i64 = row.get(1)?;
495                        Ok((foreign_key, timestamp))
496                    },
497                )
498                .await?
499            else {
500                warn!(
501                    context,
502                    "Ignoring {step} message because of invalid auth code."
503                );
504                return Ok(HandshakeMessage::Ignore);
505            };
506            let joining_chat_id = match grpid.as_str() {
507                "" => None,
508                id => {
509                    let Some((chat_id, ..)) = get_chat_id_by_grpid(context, id).await? else {
510                        warn!(context, "Ignoring {step} message: unknown grpid {id}.",);
511                        return Ok(HandshakeMessage::Ignore);
512                    };
513                    Some(chat_id)
514                }
515            };
516
517            let sender_contact = Contact::get_by_id(context, contact_id).await?;
518            if sender_contact
519                .fingerprint()
520                .is_none_or(|fp| fp != fingerprint)
521            {
522                warn!(
523                    context,
524                    "Ignoring {step} message because of fingerprint mismatch."
525                );
526                return Ok(HandshakeMessage::Ignore);
527            }
528            info!(context, "Fingerprint verified via Auth code.",);
529
530            // Mark the contact as verified if auth code is less than VERIFICATION_TIMEOUT_SECONDS seconds old.
531            if time() < timestamp + VERIFICATION_TIMEOUT_SECONDS {
532                mark_contact_id_as_verified(context, contact_id, Some(ContactId::SELF)).await?;
533            }
534            contact_id.regossip_keys(context).await?;
535            ContactId::scaleup_origin(context, &[contact_id], Origin::SecurejoinInvited).await?;
536            // for setup-contact, make Alice's one-to-one chat with Bob visible
537            // (secure-join-information are shown in the group chat)
538            if grpid.is_empty() {
539                ChatId::create_for_contact(context, contact_id).await?;
540            }
541            context.emit_event(EventType::ContactsChanged(Some(contact_id)));
542            if let Some(joining_chat_id) = joining_chat_id {
543                // Join group.
544                chat::add_contact_to_chat_ex(context, Nosync, joining_chat_id, contact_id, true)
545                    .await?;
546                let chat = Chat::load_from_db(context, joining_chat_id).await?;
547
548                inviter_progress(context, contact_id, joining_chat_id, chat.typ)?;
549                // IMAP-delete the message to avoid handling it by another device and adding the
550                // member twice. Another device will know the member's key from Autocrypt-Gossip.
551                Ok(HandshakeMessage::Done)
552            } else {
553                let chat_id = info_chat_id(context, contact_id).await?;
554                // Setup verified contact.
555                send_alice_handshake_msg(context, contact_id, "vc-contact-confirm")
556                    .await
557                    .context("failed sending vc-contact-confirm message")?;
558
559                inviter_progress(context, contact_id, chat_id, Chattype::Single)?;
560                Ok(HandshakeMessage::Ignore) // "Done" would delete the message and break multi-device (the key from Autocrypt-header is needed)
561            }
562        }
563        /*=======================================================
564        ====             Bob - the joiner's side             ====
565        ====   Step 7 in "Setup verified contact" protocol   ====
566        =======================================================*/
567        "vc-contact-confirm" => {
568            context.emit_event(EventType::SecurejoinJoinerProgress {
569                contact_id,
570                progress: JoinerProgress::Succeeded.to_usize(),
571            });
572            Ok(HandshakeMessage::Ignore)
573        }
574        "vg-member-added" => {
575            let Some(member_added) = mime_message.get_header(HeaderDef::ChatGroupMemberAdded)
576            else {
577                warn!(
578                    context,
579                    "vg-member-added without Chat-Group-Member-Added header."
580                );
581                return Ok(HandshakeMessage::Propagate);
582            };
583            if !context.is_self_addr(member_added).await? {
584                info!(
585                    context,
586                    "Member {member_added} added by unrelated SecureJoin process."
587                );
588                return Ok(HandshakeMessage::Propagate);
589            }
590
591            context.emit_event(EventType::SecurejoinJoinerProgress {
592                contact_id,
593                progress: JoinerProgress::Succeeded.to_usize(),
594            });
595            Ok(HandshakeMessage::Propagate)
596        }
597
598        "vg-member-added-received" | "vc-contact-confirm-received" => {
599            // Deprecated steps, delete them immediately.
600            Ok(HandshakeMessage::Done)
601        }
602        _ => {
603            warn!(context, "invalid step: {}", step);
604            Ok(HandshakeMessage::Ignore)
605        }
606    }
607}
608
609/// Observe self-sent Securejoin message.
610///
611/// In a multi-device-setup, there may be other devices that "see" the handshake messages.
612/// If we see self-sent messages encrypted+signed correctly with our key,
613/// we can make some conclusions of it.
614///
615/// If we see self-sent {vc,vg}-request-with-auth,
616/// we know that we are Bob (joiner-observer)
617/// that just marked peer (Alice) as verified
618/// either after receiving {vc,vg}-auth-required
619/// or immediately after scanning the QR-code
620/// if the key was already known.
621///
622/// If we see self-sent vc-contact-confirm or vg-member-added message,
623/// we know that we are Alice (inviter-observer)
624/// that just marked peer (Bob) as verified
625/// in response to correct vc-request-with-auth message.
626pub(crate) async fn observe_securejoin_on_other_device(
627    context: &Context,
628    mime_message: &MimeMessage,
629    contact_id: ContactId,
630) -> Result<HandshakeMessage> {
631    if contact_id.is_special() {
632        return Err(Error::msg("Can not be called with special contact ID"));
633    }
634    let step = mime_message
635        .get_header(HeaderDef::SecureJoin)
636        .context("Not a Secure-Join message")?;
637    info!(context, "Observing secure-join message {step:?}.");
638
639    if !matches!(
640        step,
641        "vg-request-with-auth" | "vc-request-with-auth" | "vg-member-added" | "vc-contact-confirm"
642    ) {
643        return Ok(HandshakeMessage::Ignore);
644    };
645
646    if !encrypted_and_signed(context, mime_message, &get_self_fingerprint(context).await?) {
647        warn!(
648            context,
649            "Observed SecureJoin message is not encrypted correctly."
650        );
651        return Ok(HandshakeMessage::Ignore);
652    }
653
654    let contact = Contact::get_by_id(context, contact_id).await?;
655    let addr = contact.get_addr().to_lowercase();
656
657    let Some(key) = mime_message.gossiped_keys.get(&addr) else {
658        warn!(context, "No gossip header for {addr} at step {step}.");
659        return Ok(HandshakeMessage::Ignore);
660    };
661
662    let Some(contact_fingerprint) = contact.fingerprint() else {
663        // Not a key-contact, should not happen.
664        warn!(context, "Contact does not have a fingerprint.");
665        return Ok(HandshakeMessage::Ignore);
666    };
667
668    if key.public_key.dc_fingerprint() != contact_fingerprint {
669        // Fingerprint does not match, ignore.
670        warn!(context, "Fingerprint does not match.");
671        return Ok(HandshakeMessage::Ignore);
672    }
673
674    mark_contact_id_as_verified(context, contact_id, Some(ContactId::SELF)).await?;
675
676    if step == "vg-member-added" || step == "vc-contact-confirm" {
677        let chat_type = if mime_message
678            .get_header(HeaderDef::ChatGroupMemberAdded)
679            .is_none()
680        {
681            Chattype::Single
682        } else if mime_message.get_header(HeaderDef::ListId).is_some() {
683            Chattype::OutBroadcast
684        } else {
685            Chattype::Group
686        };
687
688        // We don't know the chat ID
689        // as we may not know about the group yet.
690        //
691        // Event is mostly used for bots
692        // which only have a single device
693        // and tests which don't care about the chat ID,
694        // so we pass invalid chat ID here.
695        let chat_id = ChatId::new(0);
696        inviter_progress(context, contact_id, chat_id, chat_type)?;
697    }
698
699    if step == "vg-request-with-auth" || step == "vc-request-with-auth" {
700        // This actually reflects what happens on the first device (which does the secure
701        // join) and causes a subsequent "vg-member-added" message to create an unblocked
702        // verified group.
703        ChatId::create_for_contact_with_blocked(context, contact_id, Blocked::Not).await?;
704    }
705
706    if step == "vg-member-added" {
707        Ok(HandshakeMessage::Propagate)
708    } else {
709        Ok(HandshakeMessage::Ignore)
710    }
711}
712
713/* ******************************************************************************
714 * Tools: Misc.
715 ******************************************************************************/
716
717fn encrypted_and_signed(
718    context: &Context,
719    mimeparser: &MimeMessage,
720    expected_fingerprint: &Fingerprint,
721) -> bool {
722    if let Some(signature) = mimeparser.signature.as_ref() {
723        if signature == expected_fingerprint {
724            true
725        } else {
726            warn!(
727                context,
728                "Message does not match expected fingerprint {expected_fingerprint}.",
729            );
730            false
731        }
732    } else {
733        warn!(context, "Message not encrypted.",);
734        false
735    }
736}
737
738#[cfg(test)]
739mod securejoin_tests;