deltachat/
key.rs

1//! Cryptographic key module.
2
3use std::collections::BTreeMap;
4use std::fmt;
5use std::io::Cursor;
6
7use anyhow::{bail, ensure, Context as _, Result};
8use base64::Engine as _;
9use deltachat_contact_tools::EmailAddress;
10use pgp::composed::Deserializable;
11pub use pgp::composed::{SignedPublicKey, SignedSecretKey};
12use pgp::ser::Serialize;
13use pgp::types::{KeyDetails, KeyId, Password};
14use rand::thread_rng;
15use tokio::runtime::Handle;
16
17use crate::context::Context;
18use crate::log::{info, LogExt};
19use crate::pgp::KeyPair;
20use crate::tools::{self, time_elapsed};
21
22/// Convenience trait for working with keys.
23///
24/// This trait is implemented for rPGP's [SignedPublicKey] and
25/// [SignedSecretKey] types and makes working with them a little
26/// easier in the deltachat world.
27pub(crate) trait DcKey: Serialize + Deserializable + Clone {
28    /// Create a key from some bytes.
29    fn from_slice(bytes: &[u8]) -> Result<Self> {
30        let res = <Self as Deserializable>::from_bytes(Cursor::new(bytes));
31        if let Ok(res) = res {
32            return Ok(res);
33        }
34
35        // Workaround for keys imported using
36        // Delta Chat core < 1.0.0.
37        // Old Delta Chat core had a bug
38        // that resulted in treating CRC24 checksum
39        // as part of the key when reading ASCII Armor.
40        // Some users that started using Delta Chat in 2019
41        // have such corrupted keys with garbage bytes at the end.
42        //
43        // Garbage is at least 3 bytes long
44        // and may be longer due to padding
45        // at the end of the real key data
46        // and importing the key multiple times.
47        //
48        // If removing 10 bytes is not enough,
49        // the key is likely actually corrupted.
50        for garbage_bytes in 3..std::cmp::min(bytes.len(), 10) {
51            let res = <Self as Deserializable>::from_bytes(Cursor::new(
52                bytes
53                    .get(..bytes.len().saturating_sub(garbage_bytes))
54                    .unwrap_or_default(),
55            ));
56            if let Ok(res) = res {
57                return Ok(res);
58            }
59        }
60
61        // Removing garbage bytes did not help, return the error.
62        Ok(res?)
63    }
64
65    /// Create a key from a base64 string.
66    fn from_base64(data: &str) -> Result<Self> {
67        // strip newlines and other whitespace
68        let cleaned: String = data.split_whitespace().collect();
69        let bytes = base64::engine::general_purpose::STANDARD.decode(cleaned.as_bytes())?;
70        Self::from_slice(&bytes)
71    }
72
73    /// Create a key from an ASCII-armored string.
74    ///
75    /// Returns the key and a map of any headers which might have been set in
76    /// the ASCII-armored representation.
77    fn from_asc(data: &str) -> Result<(Self, BTreeMap<String, String>)> {
78        let bytes = data.as_bytes();
79        let res = Self::from_armor_single(Cursor::new(bytes));
80        let (key, headers) = match res {
81            Err(pgp::errors::Error::NoMatchingPacket { .. }) => match Self::is_private() {
82                true => bail!("No private key packet found"),
83                false => bail!("No public key packet found"),
84            },
85            _ => res.context("rPGP error")?,
86        };
87        let headers = headers
88            .into_iter()
89            .map(|(key, values)| {
90                (
91                    key.trim().to_lowercase(),
92                    values
93                        .last()
94                        .map_or_else(String::new, |s| s.trim().to_string()),
95                )
96            })
97            .collect();
98        Ok((key, headers))
99    }
100
101    /// Serialise the key as bytes.
102    fn to_bytes(&self) -> Vec<u8> {
103        // Not using Serialize::to_bytes() to make clear *why* it is
104        // safe to ignore this error.
105        // Because we write to a Vec<u8> the io::Write impls never
106        // fail and we can hide this error.
107        let mut buf = Vec::new();
108        self.to_writer(&mut buf).unwrap();
109        buf
110    }
111
112    /// Serialise the key to a base64 string.
113    fn to_base64(&self) -> String {
114        base64::engine::general_purpose::STANDARD.encode(DcKey::to_bytes(self))
115    }
116
117    /// Serialise the key to ASCII-armored representation.
118    ///
119    /// Each header line must be terminated by `\r\n`.  Only allows setting one
120    /// header as a simplification since that's the only way it's used so far.
121    // Since .to_armored_string() are actual methods on SignedPublicKey and
122    // SignedSecretKey we can not generically implement this.
123    fn to_asc(&self, header: Option<(&str, &str)>) -> String;
124
125    /// The fingerprint for the key.
126    fn dc_fingerprint(&self) -> Fingerprint;
127
128    fn is_private() -> bool;
129    fn key_id(&self) -> KeyId;
130}
131
132/// Attempts to load own public key.
133///
134/// Returns `None` if no key is generated yet.
135pub(crate) async fn load_self_public_key_opt(context: &Context) -> Result<Option<SignedPublicKey>> {
136    let Some(public_key_bytes) = context
137        .sql
138        .query_row_optional(
139            "SELECT public_key
140             FROM keypairs
141             WHERE id=(SELECT value FROM config WHERE keyname='key_id')",
142            (),
143            |row| {
144                let bytes: Vec<u8> = row.get(0)?;
145                Ok(bytes)
146            },
147        )
148        .await?
149    else {
150        return Ok(None);
151    };
152    let public_key = SignedPublicKey::from_slice(&public_key_bytes)?;
153    Ok(Some(public_key))
154}
155
156/// Loads own public key.
157///
158/// If no key is generated yet, generates a new one.
159pub(crate) async fn load_self_public_key(context: &Context) -> Result<SignedPublicKey> {
160    match load_self_public_key_opt(context).await? {
161        Some(public_key) => Ok(public_key),
162        None => {
163            let keypair = generate_keypair(context).await?;
164            Ok(keypair.public)
165        }
166    }
167}
168
169/// Returns our own public keyring.
170pub(crate) async fn load_self_public_keyring(context: &Context) -> Result<Vec<SignedPublicKey>> {
171    let keys = context
172        .sql
173        .query_map(
174            r#"SELECT public_key
175               FROM keypairs
176               ORDER BY id=(SELECT value FROM config WHERE keyname='key_id') DESC"#,
177            (),
178            |row| row.get::<_, Vec<u8>>(0),
179            |keys| keys.collect::<Result<Vec<_>, _>>().map_err(Into::into),
180        )
181        .await?
182        .into_iter()
183        .filter_map(|bytes| SignedPublicKey::from_slice(&bytes).log_err(context).ok())
184        .collect();
185    Ok(keys)
186}
187
188/// Returns own public key fingerprint in (not human-readable) hex representation.
189/// This is the fingerprint format that is used in the database.
190///
191/// If no key is generated yet, generates a new one.
192///
193/// For performance reasons, the fingerprint is cached after the first invocation.
194pub(crate) async fn self_fingerprint(context: &Context) -> Result<&str> {
195    if let Some(fp) = context.self_fingerprint.get() {
196        Ok(fp)
197    } else {
198        let fp = load_self_public_key(context).await?.dc_fingerprint().hex();
199        Ok(context.self_fingerprint.get_or_init(|| fp))
200    }
201}
202
203/// Returns own public key fingerprint in (not human-readable) hex representation.
204/// This is the fingerprint format that is used in the database.
205///
206/// Returns `None` if no key is generated yet.
207///
208/// For performance reasons, the fingerprint is cached after the first invocation.
209pub(crate) async fn self_fingerprint_opt(context: &Context) -> Result<Option<&str>> {
210    if let Some(fp) = context.self_fingerprint.get() {
211        Ok(Some(fp))
212    } else if let Some(key) = load_self_public_key_opt(context).await? {
213        let fp = key.dc_fingerprint().hex();
214        Ok(Some(context.self_fingerprint.get_or_init(|| fp)))
215    } else {
216        Ok(None)
217    }
218}
219
220pub(crate) async fn load_self_secret_key(context: &Context) -> Result<SignedSecretKey> {
221    let private_key = context
222        .sql
223        .query_row_optional(
224            "SELECT private_key
225             FROM keypairs
226             WHERE id=(SELECT value FROM config WHERE keyname='key_id')",
227            (),
228            |row| {
229                let bytes: Vec<u8> = row.get(0)?;
230                Ok(bytes)
231            },
232        )
233        .await?;
234    match private_key {
235        Some(bytes) => SignedSecretKey::from_slice(&bytes),
236        None => {
237            let keypair = generate_keypair(context).await?;
238            Ok(keypair.secret)
239        }
240    }
241}
242
243pub(crate) async fn load_self_secret_keyring(context: &Context) -> Result<Vec<SignedSecretKey>> {
244    let keys = context
245        .sql
246        .query_map(
247            r#"SELECT private_key
248               FROM keypairs
249               ORDER BY id=(SELECT value FROM config WHERE keyname='key_id') DESC"#,
250            (),
251            |row| row.get::<_, Vec<u8>>(0),
252            |keys| keys.collect::<Result<Vec<_>, _>>().map_err(Into::into),
253        )
254        .await?
255        .into_iter()
256        .filter_map(|bytes| SignedSecretKey::from_slice(&bytes).log_err(context).ok())
257        .collect();
258    Ok(keys)
259}
260
261impl DcKey for SignedPublicKey {
262    fn to_asc(&self, header: Option<(&str, &str)>) -> String {
263        // Not using .to_armored_string() to make clear *why* it is
264        // safe to ignore this error.
265        // Because we write to a Vec<u8> the io::Write impls never
266        // fail and we can hide this error.
267        let headers =
268            header.map(|(key, value)| BTreeMap::from([(key.to_string(), vec![value.to_string()])]));
269        let mut buf = Vec::new();
270        self.to_armored_writer(&mut buf, headers.as_ref().into())
271            .unwrap_or_default();
272        std::string::String::from_utf8(buf).unwrap_or_default()
273    }
274
275    fn is_private() -> bool {
276        false
277    }
278
279    fn dc_fingerprint(&self) -> Fingerprint {
280        self.fingerprint().into()
281    }
282
283    fn key_id(&self) -> KeyId {
284        KeyDetails::key_id(self)
285    }
286}
287
288impl DcKey for SignedSecretKey {
289    fn to_asc(&self, header: Option<(&str, &str)>) -> String {
290        // Not using .to_armored_string() to make clear *why* it is
291        // safe to do these unwraps.
292        // Because we write to a Vec<u8> the io::Write impls never
293        // fail and we can hide this error.  The string is always ASCII.
294        let headers =
295            header.map(|(key, value)| BTreeMap::from([(key.to_string(), vec![value.to_string()])]));
296        let mut buf = Vec::new();
297        self.to_armored_writer(&mut buf, headers.as_ref().into())
298            .unwrap_or_default();
299        std::string::String::from_utf8(buf).unwrap_or_default()
300    }
301
302    fn is_private() -> bool {
303        true
304    }
305
306    fn dc_fingerprint(&self) -> Fingerprint {
307        self.fingerprint().into()
308    }
309
310    fn key_id(&self) -> KeyId {
311        KeyDetails::key_id(&**self)
312    }
313}
314
315/// Deltachat extension trait for secret keys.
316///
317/// Provides some convenience wrappers only applicable to [SignedSecretKey].
318pub(crate) trait DcSecretKey {
319    /// Create a public key from a private one.
320    fn split_public_key(&self) -> Result<SignedPublicKey>;
321}
322
323impl DcSecretKey for SignedSecretKey {
324    fn split_public_key(&self) -> Result<SignedPublicKey> {
325        self.verify()?;
326        let unsigned_pubkey = self.public_key();
327        let mut rng = thread_rng();
328        let signed_pubkey = unsigned_pubkey.sign(
329            &mut rng,
330            &self.primary_key,
331            self.primary_key.public_key(),
332            &Password::empty(),
333        )?;
334        Ok(signed_pubkey)
335    }
336}
337
338async fn generate_keypair(context: &Context) -> Result<KeyPair> {
339    let addr = context.get_primary_self_addr().await?;
340    let addr = EmailAddress::new(&addr)?;
341    let _guard = context.generating_key_mutex.lock().await;
342
343    // Check if the key appeared while we were waiting on the lock.
344    match load_keypair(context).await? {
345        Some(key_pair) => Ok(key_pair),
346        None => {
347            let start = tools::Time::now();
348            info!(context, "Generating keypair.");
349            let keypair = Handle::current()
350                .spawn_blocking(move || crate::pgp::create_keypair(addr))
351                .await??;
352
353            store_self_keypair(context, &keypair).await?;
354            info!(
355                context,
356                "Keypair generated in {:.3}s.",
357                time_elapsed(&start).as_secs(),
358            );
359            Ok(keypair)
360        }
361    }
362}
363
364pub(crate) async fn load_keypair(context: &Context) -> Result<Option<KeyPair>> {
365    let res = context
366        .sql
367        .query_row_optional(
368            "SELECT public_key, private_key
369             FROM keypairs
370             WHERE id=(SELECT value FROM config WHERE keyname='key_id')",
371            (),
372            |row| {
373                let pub_bytes: Vec<u8> = row.get(0)?;
374                let sec_bytes: Vec<u8> = row.get(1)?;
375                Ok((pub_bytes, sec_bytes))
376            },
377        )
378        .await?;
379
380    Ok(if let Some((pub_bytes, sec_bytes)) = res {
381        Some(KeyPair {
382            public: SignedPublicKey::from_slice(&pub_bytes)?,
383            secret: SignedSecretKey::from_slice(&sec_bytes)?,
384        })
385    } else {
386        None
387    })
388}
389
390/// Store the keypair as an owned keypair for addr in the database.
391///
392/// This will save the keypair as keys for the given address.  The
393/// "self" here refers to the fact that this DC instance owns the
394/// keypair.  Usually `addr` will be [Config::ConfiguredAddr].
395///
396/// If either the public or private keys are already present in the
397/// database, this entry will be removed first regardless of the
398/// address associated with it.  Practically this means saving the
399/// same key again overwrites it.
400///
401/// [Config::ConfiguredAddr]: crate::config::Config::ConfiguredAddr
402pub(crate) async fn store_self_keypair(context: &Context, keypair: &KeyPair) -> Result<()> {
403    let mut config_cache_lock = context.sql.config_cache.write().await;
404    let new_key_id = context
405        .sql
406        .transaction(|transaction| {
407            let public_key = DcKey::to_bytes(&keypair.public);
408            let secret_key = DcKey::to_bytes(&keypair.secret);
409
410            // private_key and public_key columns
411            // are UNIQUE since migration 107,
412            // so this fails if we already have this key.
413            transaction
414                .execute(
415                    "INSERT INTO keypairs (public_key, private_key)
416                     VALUES (?,?)",
417                    (&public_key, &secret_key),
418                )
419                .context("Failed to insert keypair")?;
420
421            let new_key_id = transaction.last_insert_rowid();
422
423            // This will fail if we already have `key_id`.
424            //
425            // Setting default key is only possible if we don't
426            // have a key already.
427            transaction.execute(
428                "INSERT INTO config (keyname, value) VALUES ('key_id', ?)",
429                (new_key_id,),
430            )?;
431            Ok(Some(new_key_id))
432        })
433        .await?;
434
435    if let Some(new_key_id) = new_key_id {
436        // Update config cache if transaction succeeded and changed current default key.
437        config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string()));
438    }
439
440    Ok(())
441}
442
443/// Saves a keypair as the default keys.
444///
445/// This API is used for testing purposes
446/// to avoid generating the key in tests.
447/// Use import/export APIs instead.
448pub async fn preconfigure_keypair(context: &Context, secret_data: &str) -> Result<()> {
449    let secret = SignedSecretKey::from_asc(secret_data)?.0;
450    let public = secret.split_public_key()?;
451    let keypair = KeyPair { public, secret };
452    store_self_keypair(context, &keypair).await?;
453    Ok(())
454}
455
456/// A key fingerprint
457#[derive(Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
458pub struct Fingerprint(Vec<u8>);
459
460impl Fingerprint {
461    /// Creates new 160-bit (20 bytes) fingerprint.
462    pub fn new(v: Vec<u8>) -> Fingerprint {
463        debug_assert_eq!(v.len(), 20);
464        Fingerprint(v)
465    }
466
467    /// Make a hex string from the fingerprint.
468    ///
469    /// Use [std::fmt::Display] or [ToString::to_string] to get a
470    /// human-readable formatted string.
471    pub fn hex(&self) -> String {
472        hex::encode_upper(&self.0)
473    }
474}
475
476impl From<pgp::types::Fingerprint> for Fingerprint {
477    fn from(fingerprint: pgp::types::Fingerprint) -> Fingerprint {
478        Self::new(fingerprint.as_bytes().into())
479    }
480}
481
482impl fmt::Debug for Fingerprint {
483    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484        f.debug_struct("Fingerprint")
485            .field("hex", &self.hex())
486            .finish()
487    }
488}
489
490/// Make a human-readable fingerprint.
491impl fmt::Display for Fingerprint {
492    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493        // Split key into chunks of 4 with space and newline at 20 chars
494        for (i, c) in self.hex().chars().enumerate() {
495            if i > 0 && i % 20 == 0 {
496                writeln!(f)?;
497            } else if i > 0 && i % 4 == 0 {
498                write!(f, " ")?;
499            }
500            write!(f, "{c}")?;
501        }
502        Ok(())
503    }
504}
505
506/// Parse a human-readable or otherwise formatted fingerprint.
507impl std::str::FromStr for Fingerprint {
508    type Err = anyhow::Error;
509
510    fn from_str(input: &str) -> Result<Self> {
511        let hex_repr: String = input
512            .to_uppercase()
513            .chars()
514            .filter(|&c| c.is_ascii_hexdigit())
515            .collect();
516        let v: Vec<u8> = hex::decode(&hex_repr)?;
517        ensure!(v.len() == 20, "wrong fingerprint length: {}", hex_repr);
518        let fp = Fingerprint::new(v);
519        Ok(fp)
520    }
521}
522
523#[cfg(test)]
524mod tests {
525    use std::sync::{Arc, LazyLock};
526
527    use super::*;
528    use crate::config::Config;
529    use crate::test_utils::{alice_keypair, TestContext};
530
531    static KEYPAIR: LazyLock<KeyPair> = LazyLock::new(alice_keypair);
532
533    #[test]
534    fn test_from_armored_string() {
535        let (private_key, _) = SignedSecretKey::from_asc(
536            "-----BEGIN PGP PRIVATE KEY BLOCK-----
537
538xcLYBF0fgz4BCADnRUV52V4xhSsU56ZaAn3+3oG86MZhXy4X8w14WZZDf0VJGeTh
539oTtVwiw9rVN8FiUELqpO2CS2OwS9mAGMJmGIt78bvIy2EHIAjUilqakmb0ChJxC+
540ilSowab9slSdOgzQI1fzo+VZkhtczvRBq31cW8G05tuiLsnDSSS+sSH/GkvJqpzB
541BWu6tSrMzth58KBM2XwWmozpLzy6wlrUBOYT8J79UVvs81O/DhXpVYYOWj2h4n3O
54260qtK7SJBCjG7vGc2Ef8amsrjTDwUii0QQcF+BJN3ZuCI5AdOTpI39QuCDuD9UH2
543NOKI+jYPQ4KB8pA1aYXBZzYyjuwCHzryXXsXABEBAAEAB/0VkYBJPNxsAd9is7fv
5447QuTGW1AEPVvX1ENKr2226QH53auupt972t5NAKsPd3rVKVfHnsDn2TNGfP3OpXq
545XCn8diZ8j7kPwbjgFE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Usx1c+bH1EV7CmJuuY4+G
578JKz0HMEEANLLWy/9enOvSpznYIUdtXxNG6evRHClkf7jZimM/VrAc4ICW4hqICK3
579k5VMcRxVOa9hKZgg8vLfO8BRPRUB6Bc3SrK2jCKSli0FbtliNZS/lUBO1A7HRtY6
5803coYUJBKqzmObLkh4C3RFQ5n/I6cJEvD7u9jzgpW71HtdI64NQvJBAC+88Q5irPg
58107UZH9by8EVsCij8NFzChGmysHHGqeAMVVuI+rOqDqBsQA1n2aqxQ1uz5NZ9+ztu
582Dn13hMEm8U2a9MtZdBhwlJrso3RzRf570V3E6qfdFqrQLoHDdRGRS9DMcUgMayo3
583Hod6MFYzFVmbrmc822KmhaS3lBzLVpgkmEeJwsB2BBgBCAAgBQJdH4NfAhsMFiEE
584aeHEHjiV97rB+YeLMKMg0aJs7GIACgkQMKMg0aJs7GLItQgAqKF63+HwAsjoPMBv
585T9RdKdCaYV0MvxZyc7eM2pSk8cyfj6IPnxD8DPT699SMIzBfsrdGcfDYYgSODHL+
586XsV31J215HfYBh/Nkru8fawiVxr+sJG2IDAeA9SBjsDCogfzW4PwLXgTXRqNFLVr
587fK6hf6wpF56STV2U2D60b9xJeSAbBWlZFzCCQw3mPtGf/EGMHFxnJUE7MLEaaTEf
588V2Fclh+G0sWp7F2ZS3nt0vX1hYG8TMIzM8Bj2eMsdXATOji9ST7EUxk/BpFax86D
589i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
5907yPJeQ==
591=KZk/
592-----END PGP PRIVATE KEY BLOCK-----",
593        )
594        .expect("failed to decode");
595        let binary = DcKey::to_bytes(&private_key);
596        SignedSecretKey::from_slice(&binary).expect("invalid private key");
597    }
598
599    #[test]
600    fn test_asc_roundtrip() {
601        let key = KEYPAIR.public.clone();
602        let asc = key.to_asc(Some(("spam", "ham")));
603        let (key2, hdrs) = SignedPublicKey::from_asc(&asc).unwrap();
604        assert_eq!(key, key2);
605        assert_eq!(hdrs.len(), 1);
606        assert_eq!(hdrs.get("spam"), Some(&String::from("ham")));
607
608        let key = KEYPAIR.secret.clone();
609        let asc = key.to_asc(Some(("spam", "ham")));
610        let (key2, hdrs) = SignedSecretKey::from_asc(&asc).unwrap();
611        assert_eq!(key, key2);
612        assert_eq!(hdrs.len(), 1);
613        assert_eq!(hdrs.get("spam"), Some(&String::from("ham")));
614    }
615
616    #[test]
617    fn test_from_slice_roundtrip() {
618        let public_key = KEYPAIR.public.clone();
619        let private_key = KEYPAIR.secret.clone();
620
621        let binary = DcKey::to_bytes(&public_key);
622        let public_key2 = SignedPublicKey::from_slice(&binary).expect("invalid public key");
623        assert_eq!(public_key, public_key2);
624
625        let binary = DcKey::to_bytes(&private_key);
626        let private_key2 = SignedSecretKey::from_slice(&binary).expect("invalid private key");
627        assert_eq!(private_key, private_key2);
628    }
629
630    #[test]
631    fn test_from_slice_bad_data() {
632        let mut bad_data: [u8; 4096] = [0; 4096];
633        for (i, v) in bad_data.iter_mut().enumerate() {
634            *v = (i & 0xff) as u8;
635        }
636        for j in 0..(4096 / 40) {
637            let slice = &bad_data.get(j..j + 4096 / 2 + j).unwrap();
638            assert!(SignedPublicKey::from_slice(slice).is_err());
639            assert!(SignedSecretKey::from_slice(slice).is_err());
640        }
641    }
642
643    /// Tests workaround for Delta Chat core < 1.0.0
644    /// which parsed CRC24 at the end of ASCII Armor
645    /// as the part of the key.
646    /// Depending on the alignment and the number of
647    /// `=` characters at the end of the key,
648    /// this resulted in various number of garbage
649    /// octets at the end of the key, starting from 3 octets,
650    /// but possibly 4 or 5 and maybe more octets
651    /// if the key is imported or transferred
652    /// using Autocrypt Setup Message multiple times.
653    #[test]
654    fn test_ignore_trailing_garbage() {
655        // Test several variants of garbage.
656        for garbage in [
657            b"\x02\xfc\xaa\x38\x4b\x5c".as_slice(),
658            b"\x02\xfc\xaa".as_slice(),
659            b"\x01\x02\x03\x04\x05".as_slice(),
660        ] {
661            let private_key = KEYPAIR.secret.clone();
662
663            let mut binary = DcKey::to_bytes(&private_key);
664            binary.extend(garbage);
665
666            let private_key2 =
667                SignedSecretKey::from_slice(&binary).expect("Failed to ignore garbage");
668
669            assert_eq!(private_key.dc_fingerprint(), private_key2.dc_fingerprint());
670        }
671    }
672
673    #[test]
674    fn test_base64_roundtrip() {
675        let key = KEYPAIR.public.clone();
676        let base64 = key.to_base64();
677        let key2 = SignedPublicKey::from_base64(&base64).unwrap();
678        assert_eq!(key, key2);
679    }
680
681    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
682    async fn test_load_self_generate_public() {
683        let t = TestContext::new().await;
684        t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
685            .await
686            .unwrap();
687        let key = load_self_public_key(&t).await;
688        assert!(key.is_ok());
689    }
690
691    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
692    async fn test_load_self_generate_secret() {
693        let t = TestContext::new().await;
694        t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
695            .await
696            .unwrap();
697        let key = load_self_secret_key(&t).await;
698        assert!(key.is_ok());
699    }
700
701    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
702    async fn test_load_self_generate_concurrent() {
703        use std::thread;
704
705        let t = TestContext::new().await;
706        t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
707            .await
708            .unwrap();
709        let thr0 = {
710            let ctx = t.clone();
711            thread::spawn(move || {
712                tokio::runtime::Runtime::new()
713                    .unwrap()
714                    .block_on(load_self_public_key(&ctx))
715            })
716        };
717        let thr1 = {
718            let ctx = t.clone();
719            thread::spawn(move || {
720                tokio::runtime::Runtime::new()
721                    .unwrap()
722                    .block_on(load_self_public_key(&ctx))
723            })
724        };
725        let res0 = thr0.join().unwrap();
726        let res1 = thr1.join().unwrap();
727        assert_eq!(res0.unwrap(), res1.unwrap());
728    }
729
730    #[test]
731    fn test_split_key() {
732        let pubkey = KEYPAIR.secret.split_public_key().unwrap();
733        assert_eq!(pubkey.primary_key, KEYPAIR.public.primary_key);
734    }
735
736    /// Tests that setting a default key second time is not allowed.
737    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
738    async fn test_save_self_key_twice() {
739        // Saving the same key twice should result in only one row in
740        // the keypairs table.
741        let t = TestContext::new().await;
742        let ctx = Arc::new(t);
743
744        let nrows = || async {
745            ctx.sql
746                .count("SELECT COUNT(*) FROM keypairs;", ())
747                .await
748                .unwrap()
749        };
750        assert_eq!(nrows().await, 0);
751        store_self_keypair(&ctx, &KEYPAIR).await.unwrap();
752        assert_eq!(nrows().await, 1);
753
754        // Saving a second key fails.
755        let res = store_self_keypair(&ctx, &KEYPAIR).await;
756        assert!(res.is_err());
757
758        assert_eq!(nrows().await, 1);
759    }
760
761    #[test]
762    fn test_fingerprint_from_str() {
763        let res = Fingerprint::new(vec![
764            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
765        ]);
766
767        let fp: Fingerprint = "0102030405060708090A0B0c0d0e0F1011121314".parse().unwrap();
768        assert_eq!(fp, res);
769
770        let fp: Fingerprint = "zzzz 0102 0304 0506\n0708090a0b0c0D0E0F1011121314 yyy"
771            .parse()
772            .unwrap();
773        assert_eq!(fp, res);
774
775        assert!("1".parse::<Fingerprint>().is_err());
776    }
777
778    #[test]
779    fn test_fingerprint_hex() {
780        let fp = Fingerprint::new(vec![
781            1, 2, 4, 8, 16, 32, 64, 128, 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
782        ]);
783        assert_eq!(fp.hex(), "0102040810204080FF0A0B0C0D0E0F1011121314");
784    }
785
786    #[test]
787    fn test_fingerprint_to_string() {
788        let fp = Fingerprint::new(vec![
789            1, 2, 4, 8, 16, 32, 64, 128, 255, 1, 2, 4, 8, 16, 32, 64, 128, 255, 19, 20,
790        ]);
791        assert_eq!(
792            fp.to_string(),
793            "0102 0408 1020 4080 FF01\n0204 0810 2040 80FF 1314"
794        );
795    }
796}