deltachat/imex/
key_transfer.rs

1//! # Key transfer via Autocrypt Setup Message.
2use rand::{thread_rng, Rng};
3
4use anyhow::{bail, ensure, Result};
5
6use crate::blob::BlobObject;
7use crate::chat::{self, ChatId};
8use crate::config::Config;
9use crate::constants::{ASM_BODY, ASM_SUBJECT};
10use crate::contact::ContactId;
11use crate::context::Context;
12use crate::imex::set_self_key;
13use crate::key::{load_self_secret_key, DcKey};
14use crate::message::{Message, MsgId, Viewtype};
15use crate::mimeparser::SystemMessage;
16use crate::param::Param;
17use crate::pgp;
18use crate::tools::open_file_std;
19
20/// Initiates key transfer via Autocrypt Setup Message.
21///
22/// Returns setup code.
23pub async fn initiate_key_transfer(context: &Context) -> Result<String> {
24    let setup_code = create_setup_code(context);
25    /* this may require a keypair to be created. this may take a second ... */
26    let setup_file_content = render_setup_file(context, &setup_code).await?;
27    /* encrypting may also take a while ... */
28    let setup_file_blob = BlobObject::create_and_deduplicate_from_bytes(
29        context,
30        setup_file_content.as_bytes(),
31        "autocrypt-setup-message.html",
32    )?;
33
34    let chat_id = ChatId::create_for_contact(context, ContactId::SELF).await?;
35    let mut msg = Message::new(Viewtype::File);
36    msg.param.set(Param::File, setup_file_blob.as_name());
37    msg.param
38        .set(Param::Filename, "autocrypt-setup-message.html");
39    msg.subject = ASM_SUBJECT.to_owned();
40    msg.param
41        .set(Param::MimeType, "application/autocrypt-setup");
42    msg.param.set_cmd(SystemMessage::AutocryptSetupMessage);
43    msg.force_plaintext();
44    msg.param.set_int(Param::SkipAutocrypt, 1);
45
46    // Enable BCC-self, because transferring a key
47    // means we have a multi-device setup.
48    context.set_config_bool(Config::BccSelf, true).await?;
49
50    chat::send_msg(context, chat_id, &mut msg).await?;
51    Ok(setup_code)
52}
53
54/// Continue key transfer via Autocrypt Setup Message.
55///
56/// `msg_id` is the ID of the received Autocrypt Setup Message.
57/// `setup_code` is the code entered by the user.
58pub async fn continue_key_transfer(
59    context: &Context,
60    msg_id: MsgId,
61    setup_code: &str,
62) -> Result<()> {
63    ensure!(!msg_id.is_special(), "wrong id");
64
65    let msg = Message::load_from_db(context, msg_id).await?;
66    ensure!(
67        msg.is_setupmessage(),
68        "Message is no Autocrypt Setup Message."
69    );
70
71    if let Some(filename) = msg.get_file(context) {
72        let file = open_file_std(context, filename)?;
73        let sc = normalize_setup_code(setup_code);
74        let armored_key = decrypt_setup_file(&sc, file).await?;
75        set_self_key(context, &armored_key).await?;
76        context.set_config_bool(Config::BccSelf, true).await?;
77
78        Ok(())
79    } else {
80        bail!("Message is no Autocrypt Setup Message.");
81    }
82}
83
84/// Renders HTML body of a setup file message.
85///
86/// The `passphrase` must be at least 2 characters long.
87pub async fn render_setup_file(context: &Context, passphrase: &str) -> Result<String> {
88    let passphrase_begin = if let Some(passphrase_begin) = passphrase.get(..2) {
89        passphrase_begin
90    } else {
91        bail!("Passphrase must be at least 2 chars long.");
92    };
93    let private_key = load_self_secret_key(context).await?;
94    let ac_headers = match context.get_config_bool(Config::E2eeEnabled).await? {
95        false => None,
96        true => Some(("Autocrypt-Prefer-Encrypt", "mutual")),
97    };
98    let private_key_asc = private_key.to_asc(ac_headers);
99    let encr = pgp::symm_encrypt(passphrase, private_key_asc.as_bytes())
100        .await?
101        .replace('\n', "\r\n");
102
103    let replacement = format!(
104        concat!(
105            "-----BEGIN PGP MESSAGE-----\r\n",
106            "Passphrase-Format: numeric9x4\r\n",
107            "Passphrase-Begin: {}"
108        ),
109        passphrase_begin
110    );
111    let pgp_msg = encr.replace("-----BEGIN PGP MESSAGE-----", &replacement);
112
113    let msg_subj = ASM_SUBJECT;
114    let msg_body = ASM_BODY.to_string();
115    let msg_body_html = msg_body.replace('\r', "").replace('\n', "<br>");
116    Ok(format!(
117        concat!(
118            "<!DOCTYPE html>\r\n",
119            "<html>\r\n",
120            "  <head>\r\n",
121            "    <title>{}</title>\r\n",
122            "  </head>\r\n",
123            "  <body>\r\n",
124            "    <h1>{}</h1>\r\n",
125            "    <p>{}</p>\r\n",
126            "    <pre>\r\n{}\r\n</pre>\r\n",
127            "  </body>\r\n",
128            "</html>\r\n"
129        ),
130        msg_subj, msg_subj, msg_body_html, pgp_msg
131    ))
132}
133
134/// Creates a new setup code for Autocrypt Setup Message.
135fn create_setup_code(_context: &Context) -> String {
136    let mut random_val: u16;
137    let mut rng = thread_rng();
138    let mut ret = String::new();
139
140    for i in 0..9 {
141        loop {
142            random_val = rng.gen();
143            if random_val as usize <= 60000 {
144                break;
145            }
146        }
147        random_val = (random_val as usize % 10000) as u16;
148        ret += &format!(
149            "{}{:04}",
150            if 0 != i { "-" } else { "" },
151            random_val as usize
152        );
153    }
154
155    ret
156}
157
158async fn decrypt_setup_file<T: std::io::Read + std::io::Seek>(
159    passphrase: &str,
160    file: T,
161) -> Result<String> {
162    let plain_bytes = pgp::symm_decrypt(passphrase, file).await?;
163    let plain_text = std::string::String::from_utf8(plain_bytes)?;
164
165    Ok(plain_text)
166}
167
168fn normalize_setup_code(s: &str) -> String {
169    let mut out = String::new();
170    for c in s.chars() {
171        if c.is_ascii_digit() {
172            out.push(c);
173            if let 4 | 9 | 14 | 19 | 24 | 29 | 34 | 39 = out.len() {
174                out += "-"
175            }
176        }
177    }
178    out
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    use crate::pgp::{split_armored_data, HEADER_AUTOCRYPT, HEADER_SETUPCODE};
186    use crate::receive_imf::receive_imf;
187    use crate::test_utils::{TestContext, TestContextManager};
188    use ::pgp::armor::BlockType;
189
190    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
191    async fn test_render_setup_file() {
192        let t = TestContext::new_alice().await;
193        let msg = render_setup_file(&t, "hello").await.unwrap();
194        println!("{}", &msg);
195        // Check some substrings, indicating things got substituted.
196        assert!(msg.contains("<title>Autocrypt Setup Message</title"));
197        assert!(msg.contains("<h1>Autocrypt Setup Message</h1>"));
198        assert!(msg.contains("<p>This is the Autocrypt Setup Message used to"));
199        assert!(msg.contains("-----BEGIN PGP MESSAGE-----\r\n"));
200        assert!(msg.contains("Passphrase-Format: numeric9x4\r\n"));
201        assert!(msg.contains("Passphrase-Begin: he\r\n"));
202        assert!(msg.contains("-----END PGP MESSAGE-----\r\n"));
203
204        for line in msg.rsplit_terminator('\n') {
205            assert!(line.ends_with('\r'));
206        }
207    }
208
209    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
210    async fn test_render_setup_file_newline_replace() {
211        let t = TestContext::new_alice().await;
212        let msg = render_setup_file(&t, "pw").await.unwrap();
213        println!("{}", &msg);
214        assert!(msg.contains("<p>This is the Autocrypt Setup Message used to transfer your end-to-end setup between clients.<br>"));
215    }
216
217    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
218    async fn test_create_setup_code() {
219        let t = TestContext::new().await;
220        let setupcode = create_setup_code(&t);
221        assert_eq!(setupcode.len(), 44);
222        assert_eq!(setupcode.chars().nth(4).unwrap(), '-');
223        assert_eq!(setupcode.chars().nth(9).unwrap(), '-');
224        assert_eq!(setupcode.chars().nth(14).unwrap(), '-');
225        assert_eq!(setupcode.chars().nth(19).unwrap(), '-');
226        assert_eq!(setupcode.chars().nth(24).unwrap(), '-');
227        assert_eq!(setupcode.chars().nth(29).unwrap(), '-');
228        assert_eq!(setupcode.chars().nth(34).unwrap(), '-');
229        assert_eq!(setupcode.chars().nth(39).unwrap(), '-');
230    }
231
232    #[test]
233    fn test_normalize_setup_code() {
234        let norm = normalize_setup_code("123422343234423452346234723482349234");
235        assert_eq!(norm, "1234-2234-3234-4234-5234-6234-7234-8234-9234");
236
237        let norm =
238            normalize_setup_code("\t1 2 3422343234- foo bar-- 423-45 2 34 6234723482349234      ");
239        assert_eq!(norm, "1234-2234-3234-4234-5234-6234-7234-8234-9234");
240    }
241
242    /* S_EM_SETUPFILE is a AES-256 symm. encrypted setup message created by Enigmail
243    with an "encrypted session key", see RFC 4880.  The code is in S_EM_SETUPCODE */
244    const S_EM_SETUPCODE: &str = "1742-0185-6197-1303-7016-8412-3581-4441-0597";
245    const S_EM_SETUPFILE: &str = include_str!("../../test-data/message/stress.txt");
246
247    // Autocrypt Setup Message payload "encrypted" with plaintext algorithm.
248    const S_PLAINTEXT_SETUPFILE: &str =
249        include_str!("../../test-data/message/plaintext-autocrypt-setup.txt");
250
251    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
252    async fn test_split_and_decrypt() {
253        let buf_1 = S_EM_SETUPFILE.as_bytes().to_vec();
254        let (typ, headers, base64) = split_armored_data(&buf_1).unwrap();
255        assert_eq!(typ, BlockType::Message);
256        assert!(S_EM_SETUPCODE.starts_with(headers.get(HEADER_SETUPCODE).unwrap()));
257        assert!(!headers.contains_key(HEADER_AUTOCRYPT));
258
259        assert!(!base64.is_empty());
260
261        let setup_file = S_EM_SETUPFILE.to_string();
262        let decrypted =
263            decrypt_setup_file(S_EM_SETUPCODE, std::io::Cursor::new(setup_file.as_bytes()))
264                .await
265                .unwrap();
266
267        let (typ, headers, _base64) = split_armored_data(decrypted.as_bytes()).unwrap();
268
269        assert_eq!(typ, BlockType::PrivateKey);
270        assert_eq!(headers.get(HEADER_AUTOCRYPT), Some(&"mutual".to_string()));
271        assert!(!headers.contains_key(HEADER_SETUPCODE));
272    }
273
274    /// Tests that Autocrypt Setup Message encrypted with "plaintext" algorithm cannot be
275    /// decrypted.
276    ///
277    /// According to <https://datatracker.ietf.org/doc/html/rfc4880#section-13.4>
278    /// "Implementations MUST NOT use plaintext in Symmetrically Encrypted Data packets".
279    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
280    async fn test_decrypt_plaintext_autocrypt_setup_message() {
281        let setup_file = S_PLAINTEXT_SETUPFILE.to_string();
282        let incorrect_setupcode = "0000-0000-0000-0000-0000-0000-0000-0000-0000";
283        assert!(decrypt_setup_file(
284            incorrect_setupcode,
285            std::io::Cursor::new(setup_file.as_bytes()),
286        )
287        .await
288        .is_err());
289    }
290
291    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
292    async fn test_key_transfer() -> Result<()> {
293        let alice = TestContext::new_alice().await;
294
295        alice.set_config(Config::BccSelf, Some("0")).await?;
296        let setup_code = initiate_key_transfer(&alice).await?;
297
298        // Test that sending Autocrypt Setup Message enables `bcc_self`.
299        assert_eq!(alice.get_config_bool(Config::BccSelf).await?, true);
300
301        // Get Autocrypt Setup Message.
302        let sent = alice.pop_sent_msg().await;
303
304        // Alice sets up a second device.
305        let alice2 = TestContext::new().await;
306        alice2.set_name("alice2");
307        alice2.configure_addr("alice@example.org").await;
308        alice2.recv_msg(&sent).await;
309        let msg = alice2.get_last_msg().await;
310        assert!(msg.is_setupmessage());
311        assert_eq!(
312            crate::key::load_self_secret_keyring(&alice2).await?.len(),
313            0
314        );
315
316        // Transfer the key.
317        alice2.set_config(Config::BccSelf, Some("0")).await?;
318        continue_key_transfer(&alice2, msg.id, &setup_code).await?;
319        assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true);
320        assert_eq!(
321            crate::key::load_self_secret_keyring(&alice2).await?.len(),
322            1
323        );
324
325        // Alice sends a message to self from the new device.
326        let sent = alice2.send_text(msg.chat_id, "Test").await;
327        let rcvd_msg = alice.recv_msg(&sent).await;
328        assert_eq!(rcvd_msg.get_text(), "Test");
329
330        Ok(())
331    }
332
333    /// Tests that Autocrypt Setup Messages is only clickable if it is self-sent.
334    /// This prevents Bob from tricking Alice into changing the key
335    /// by sending her an Autocrypt Setup Message as long as Alice's server
336    /// does not allow to forge the `From:` header.
337    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
338    async fn test_key_transfer_non_self_sent() -> Result<()> {
339        let mut tcm = TestContextManager::new();
340        let alice = tcm.alice().await;
341        let bob = tcm.bob().await;
342
343        let _setup_code = initiate_key_transfer(&alice).await?;
344
345        // Get Autocrypt Setup Message.
346        let sent = alice.pop_sent_msg().await;
347
348        let rcvd = bob.recv_msg(&sent).await;
349        assert!(!rcvd.is_setupmessage());
350
351        Ok(())
352    }
353
354    /// Tests reception of Autocrypt Setup Message from K-9 6.802.
355    ///
356    /// Unlike Autocrypt Setup Message sent by Delta Chat,
357    /// this message does not contain `Autocrypt-Prefer-Encrypt` header.
358    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
359    async fn test_key_transfer_k_9() -> Result<()> {
360        let t = &TestContext::new().await;
361        t.configure_addr("autocrypt@nine.testrun.org").await;
362
363        let raw = include_bytes!("../../test-data/message/k-9-autocrypt-setup-message.eml");
364        let received = receive_imf(t, raw, false).await?.unwrap();
365
366        let setup_code = "0655-9868-8252-5455-4232-5158-1237-5333-2638";
367        continue_key_transfer(t, *received.msg_ids.last().unwrap(), setup_code).await?;
368
369        Ok(())
370    }
371}