signatory/key/
ring.rs

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
44
45
46
47
48
49
50
51
52
53
//! Signature key ring.

use crate::{Algorithm, Error, KeyHandle, Result};

#[cfg(feature = "ecdsa")]
use crate::ecdsa;

#[cfg(feature = "ed25519")]
use crate::ed25519;

/// Signature key ring which can contain signing keys for all supported algorithms.
#[derive(Debug, Default)]
pub struct KeyRing {
    /// ECDSA key ring.
    #[cfg(feature = "ecdsa")]
    pub ecdsa: ecdsa::KeyRing,

    /// Ed25519 key ring.
    #[cfg(feature = "ed25519")]
    pub ed25519: ed25519::KeyRing,
}

impl KeyRing {
    /// Create a new keyring.
    pub fn new() -> Self {
        Self::default()
    }
}

/// Support for loading PKCS#8 private keys.
pub trait LoadPkcs8 {
    /// Load a PKCS#8 key into the key ring.
    fn load_pkcs8(&mut self, private_key: pkcs8::PrivateKeyInfo<'_>) -> Result<KeyHandle>;
}

impl LoadPkcs8 for KeyRing {
    fn load_pkcs8(&mut self, private_key: pkcs8::PrivateKeyInfo<'_>) -> Result<KeyHandle> {
        #[allow(unused_variables)]
        let algorithm = Algorithm::try_from(private_key.algorithm)?;

        #[cfg(feature = "ecdsa")]
        if algorithm.is_ecdsa() {
            return self.ecdsa.load_pkcs8(private_key);
        }

        #[cfg(feature = "ed25519")]
        if algorithm == Algorithm::Ed25519 {
            return self.ed25519.load_pkcs8(private_key);
        }

        Err(Error::AlgorithmInvalid)
    }
}