signatory/key/
ring.rs

1//! Signature key ring.
2
3use crate::{Algorithm, Error, KeyHandle, Result};
4
5#[cfg(feature = "ecdsa")]
6use crate::ecdsa;
7
8#[cfg(feature = "ed25519")]
9use crate::ed25519;
10
11/// Signature key ring which can contain signing keys for all supported algorithms.
12#[derive(Debug, Default)]
13pub struct KeyRing {
14    /// ECDSA key ring.
15    #[cfg(feature = "ecdsa")]
16    pub ecdsa: ecdsa::KeyRing,
17
18    /// Ed25519 key ring.
19    #[cfg(feature = "ed25519")]
20    pub ed25519: ed25519::KeyRing,
21}
22
23impl KeyRing {
24    /// Create a new keyring.
25    pub fn new() -> Self {
26        Self::default()
27    }
28}
29
30/// Support for loading PKCS#8 private keys.
31pub trait LoadPkcs8 {
32    /// Load a PKCS#8 key into the key ring.
33    fn load_pkcs8(&mut self, private_key: pkcs8::PrivateKeyInfo<'_>) -> Result<KeyHandle>;
34}
35
36impl LoadPkcs8 for KeyRing {
37    fn load_pkcs8(&mut self, private_key: pkcs8::PrivateKeyInfo<'_>) -> Result<KeyHandle> {
38        #[allow(unused_variables)]
39        let algorithm = Algorithm::try_from(private_key.algorithm)?;
40
41        #[cfg(feature = "ecdsa")]
42        if algorithm.is_ecdsa() {
43            return self.ecdsa.load_pkcs8(private_key);
44        }
45
46        #[cfg(feature = "ed25519")]
47        if algorithm == Algorithm::Ed25519 {
48            return self.ed25519.load_pkcs8(private_key);
49        }
50
51        Err(Error::AlgorithmInvalid)
52    }
53}