use crate::packet::{self, key, Key};
use crate::crypto::SessionKey;
use crate::crypto::mpi;
use crate::types::HashAlgorithm;
use crate::Result;
pub trait Signer {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;
fn acceptable_hashes(&self) -> &[HashAlgorithm] {
&crate::crypto::hash::DEFAULT_HASHES_SORTED
}
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature>;
}
impl Signer for Box<dyn Signer> {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
self.as_ref().public()
}
fn acceptable_hashes(&self) -> &[HashAlgorithm] {
self.as_ref().acceptable_hashes()
}
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature> {
self.as_mut().sign(hash_algo, digest)
}
}
impl Signer for Box<dyn Signer + Send + Sync> {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
self.as_ref().public()
}
fn acceptable_hashes(&self) -> &[HashAlgorithm] {
self.as_ref().acceptable_hashes()
}
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature> {
self.as_mut().sign(hash_algo, digest)
}
}
pub trait Decryptor {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;
fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
plaintext_len: Option<usize>)
-> Result<SessionKey>;
}
impl Decryptor for Box<dyn Decryptor> {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
self.as_ref().public()
}
fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
plaintext_len: Option<usize>)
-> Result<SessionKey> {
self.as_mut().decrypt(ciphertext, plaintext_len)
}
}
impl Decryptor for Box<dyn Decryptor + Send + Sync> {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
self.as_ref().public()
}
fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
plaintext_len: Option<usize>)
-> Result<SessionKey> {
self.as_mut().decrypt(ciphertext, plaintext_len)
}
}
#[derive(Clone)]
pub struct KeyPair {
public: Key<key::PublicParts, key::UnspecifiedRole>,
secret: packet::key::Unencrypted,
}
assert_send_and_sync!(KeyPair);
impl KeyPair {
pub fn new(public: Key<key::PublicParts, key::UnspecifiedRole>,
secret: packet::key::Unencrypted)
-> Result<Self>
{
Ok(Self {
public,
secret,
})
}
pub fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
&self.public
}
pub fn secret(&self) -> &packet::key::Unencrypted {
&self.secret
}
}
impl From<KeyPair> for Key<key::SecretParts, key::UnspecifiedRole> {
fn from(p: KeyPair) -> Self {
let (key, secret) = (p.public, p.secret);
key.add_secret(secret.into()).0
}
}