1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::{
    key::sodiumoxide::{
        SodiumOxidePublicAsymmetricKeyUnsealable, SodiumOxideSecretAsymmetricKeyUnsealable,
        SodiumOxideSymmetricKeyUnsealable,
    },
    ByteSealable, ByteSource, CryptoError, Storer,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

#[async_trait]
pub trait Unsealable {
    async fn unseal<S: Storer>(self, storer: S) -> Result<ByteSealable, CryptoError>;
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "t", content = "c")]
pub enum ByteUnsealable {
    SodiumOxideSymmetricKey(SodiumOxideSymmetricKeyUnsealable),
    SodiumOxideSecretAsymmetricKey(SodiumOxideSecretAsymmetricKeyUnsealable),
    SodiumOxidePublicAsymmetricKey(SodiumOxidePublicAsymmetricKeyUnsealable),
}

#[async_trait]
impl Unsealable for ByteUnsealable {
    async fn unseal<S: Storer>(self, storer: S) -> Result<ByteSealable, CryptoError> {
        match self {
            Self::SodiumOxideSymmetricKey(sosku) => sosku.unseal(storer).await,
            Self::SodiumOxideSecretAsymmetricKey(sosaku) => sosaku.unseal(storer).await,
            Self::SodiumOxidePublicAsymmetricKey(sopaku) => sopaku.unseal(storer).await,
        }
    }
}

impl ByteUnsealable {
    pub fn get_source(&self) -> &ByteSource {
        match self {
            Self::SodiumOxideSymmetricKey(sosku) => &sosku.source,
            Self::SodiumOxideSecretAsymmetricKey(sosaku) => &sosaku.source,
            Self::SodiumOxidePublicAsymmetricKey(sopaku) => &sopaku.source,
        }
    }
}