mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
The user-visible change here is that it allows the FFI API to save keys in the database for a context. This is primarily intended for testing purposes as it allows you to get a key without having to generate it. Internally the most important change is to start using the SignedPublicKey and SignedPrivateKey types from rpgp instead of wrapping them into a single Key object. This allows APIs to be specific about which they want instead of having to do runtime checks like .is_public() or so. This means some of the functionality of the Key impl now needs to be a trait. A thid API change is to introduce the KeyPair struct, which binds together the email address, public and private key for a keypair. All these changes result in a bunch of cleanups, though more more should be done to completely replace the Key type with the SignedPublicKye/SignedPrivateKey + traits. But this change is large enough already. Testing-wise this adds two new keys which can be loaded from disk and and avoids a few more key-generating tests. The encrypt/decrypt tests are moved from the stress tests into the pgp tests and split up.
46 lines
1.0 KiB
Rust
46 lines
1.0 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use crate::constants::KeyType;
|
|
use crate::context::Context;
|
|
use crate::key::Key;
|
|
use crate::sql::Sql;
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
pub struct Keyring<'a> {
|
|
keys: Vec<Cow<'a, Key>>,
|
|
}
|
|
|
|
impl<'a> Keyring<'a> {
|
|
pub fn add_owned(&mut self, key: Key) {
|
|
self.add(Cow::Owned(key))
|
|
}
|
|
|
|
pub fn add_ref(&mut self, key: &'a Key) {
|
|
self.add(Cow::Borrowed(key))
|
|
}
|
|
|
|
fn add(&mut self, key: Cow<'a, Key>) {
|
|
self.keys.push(key);
|
|
}
|
|
|
|
pub fn keys(&self) -> &[Cow<'a, Key>] {
|
|
&self.keys
|
|
}
|
|
|
|
pub fn load_self_private_for_decrypting(
|
|
&mut self,
|
|
context: &Context,
|
|
self_addr: impl AsRef<str>,
|
|
sql: &Sql,
|
|
) -> bool {
|
|
sql.query_get_value(
|
|
context,
|
|
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
|
|
&[self_addr.as_ref()],
|
|
)
|
|
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Private))
|
|
.map(|key| self.add_owned(key))
|
|
.is_some()
|
|
}
|
|
}
|