Skip to main content

wasi_crypto_preview/asymmetric_common/
publickey.rs

1use super::*;
2use crate::types as guest_types;
3use crate::{AlgorithmType, CryptoCtx, HandleManagers};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum PublicKeyEncoding {
7    Raw,
8    Der,
9    Pem,
10    Sec,
11    CompressedSec,
12}
13
14impl From<guest_types::PublickeyEncoding> for PublicKeyEncoding {
15    fn from(encoding: guest_types::PublickeyEncoding) -> Self {
16        match encoding {
17            guest_types::PublickeyEncoding::Raw => PublicKeyEncoding::Raw,
18            guest_types::PublickeyEncoding::Der => PublicKeyEncoding::Der,
19            guest_types::PublickeyEncoding::Pem => PublicKeyEncoding::Pem,
20            guest_types::PublickeyEncoding::Sec => PublicKeyEncoding::Sec,
21            guest_types::PublickeyEncoding::CompressedSec => PublicKeyEncoding::CompressedSec,
22        }
23    }
24}
25
26#[derive(Clone)]
27pub enum PublicKey {
28    Signature(SignaturePublicKey),
29    KeyExchange(KxPublicKey),
30}
31
32impl PublicKey {
33    pub(crate) fn into_signature_public_key(self) -> Result<SignaturePublicKey, CryptoError> {
34        match self {
35            PublicKey::Signature(pk) => Ok(pk),
36            _ => bail!(CryptoError::InvalidHandle),
37        }
38    }
39
40    pub(crate) fn into_kx_public_key(self) -> Result<KxPublicKey, CryptoError> {
41        match self {
42            PublicKey::KeyExchange(pk) => Ok(pk),
43            _ => bail!(CryptoError::InvalidHandle),
44        }
45    }
46
47    fn import(
48        alg_type: AlgorithmType,
49        alg_str: &str,
50        encoded: &[u8],
51        encoding: PublicKeyEncoding,
52    ) -> Result<PublicKey, CryptoError> {
53        match alg_type {
54            AlgorithmType::Signatures => Ok(PublicKey::Signature(SignaturePublicKey::import(
55                SignatureAlgorithm::try_from(alg_str)?,
56                encoded,
57                encoding,
58            )?)),
59            _ => bail!(CryptoError::InvalidOperation),
60        }
61    }
62
63    fn export(&self, encoding: PublicKeyEncoding) -> Result<Vec<u8>, CryptoError> {
64        match self {
65            PublicKey::Signature(pk) => pk.export(encoding),
66            PublicKey::KeyExchange(pk) => pk.export(encoding),
67        }
68    }
69
70    fn verify(handles: &HandleManagers, pk_handle: Handle) -> Result<(), CryptoError> {
71        match handles.publickey.get(pk_handle)? {
72            PublicKey::Signature(pk) => SignaturePublicKey::verify(pk),
73            PublicKey::KeyExchange(pk) => pk.verify(),
74        }
75    }
76}
77
78impl CryptoCtx {
79    pub fn publickey_import(
80        &self,
81        alg_type: AlgorithmType,
82        alg_str: &str,
83        encoded: &[u8],
84        encoding: PublicKeyEncoding,
85    ) -> Result<Handle, CryptoError> {
86        let pk = PublicKey::import(alg_type, alg_str, encoded, encoding)?;
87        let handle = self.handles.publickey.register(pk)?;
88        Ok(handle)
89    }
90
91    pub fn publickey_export(
92        &self,
93        pk_handle: Handle,
94        encoding: PublicKeyEncoding,
95    ) -> Result<Handle, CryptoError> {
96        let pk = self.handles.publickey.get(pk_handle)?;
97        let encoded = pk.export(encoding)?;
98        let array_output_handle = ArrayOutput::register(&self.handles, encoded)?;
99        Ok(array_output_handle)
100    }
101
102    pub fn publickey_verify(&self, pk: Handle) -> Result<(), CryptoError> {
103        PublicKey::verify(&self.handles, pk)
104    }
105
106    pub fn publickey_close(&self, pk: Handle) -> Result<(), CryptoError> {
107        self.handles.publickey.close(pk)
108    }
109}