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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Enum of HMAC / EC / RSA / Ed Keys.

use openssl::pkey::{Id, PKey};

use crate::{
    ecdsa::{EcdsaPrivateKey, EcdsaPublicKey},
    eddsa::{Ed25519PrivateKey, Ed25519PublicKey},
    jwk::Jwk,
    rsa::{RsaAlgorithm, RsaPrivateKey, RsaPublicKey},
    Error, PrivateKeyToJwk, PublicKeyToJwk, Result, SigningKey, VerificationKey,
};

/// An RSA, EC or Ed25519 private key.
///
/// Use this if you just want to load SOME private key from an external pem
/// file.
#[non_exhaustive]
#[derive(Debug)]
pub enum SomePrivateKey {
    Ed25519(Ed25519PrivateKey),
    Ecdsa(EcdsaPrivateKey),
    Rsa(RsaPrivateKey),
}

/// An RSA, EC or Ed25519 public.
///
/// Use this if you just want to load SOME public key from an external pem file
/// or JWK.
#[non_exhaustive]
#[derive(Debug)]
pub enum SomePublicKey {
    Ed25519(Ed25519PublicKey),
    Ecdsa(EcdsaPublicKey),
    Rsa(RsaPublicKey),
}

impl From<Ed25519PrivateKey> for SomePrivateKey {
    #[inline]
    fn from(k: Ed25519PrivateKey) -> SomePrivateKey {
        SomePrivateKey::Ed25519(k)
    }
}

impl From<EcdsaPrivateKey> for SomePrivateKey {
    #[inline]
    fn from(k: EcdsaPrivateKey) -> SomePrivateKey {
        SomePrivateKey::Ecdsa(k)
    }
}

impl From<RsaPrivateKey> for SomePrivateKey {
    #[inline]
    fn from(k: RsaPrivateKey) -> SomePrivateKey {
        SomePrivateKey::Rsa(k)
    }
}

impl From<Ed25519PublicKey> for SomePublicKey {
    #[inline]
    fn from(k: Ed25519PublicKey) -> SomePublicKey {
        SomePublicKey::Ed25519(k)
    }
}

impl From<EcdsaPublicKey> for SomePublicKey {
    #[inline]
    fn from(k: EcdsaPublicKey) -> SomePublicKey {
        SomePublicKey::Ecdsa(k)
    }
}

impl From<RsaPublicKey> for SomePublicKey {
    #[inline]
    fn from(k: RsaPublicKey) -> SomePublicKey {
        SomePublicKey::Rsa(k)
    }
}

impl SomePrivateKey {
    /// Read an RSA/EC/Ed25519 private key from PEM.
    ///
    /// For an EC/Ed25519 private key, algorithm is deduced from the curve, e.g.
    /// P-256 -> ES256.
    ///
    /// For an RSA private key, `if_rsa_algorithm` is used.
    pub fn from_pem(pem: &[u8], if_rsa_algorithm: RsaAlgorithm) -> Result<Self> {
        let pk = PKey::private_key_from_pem(pem)?;

        match pk.id() {
            Id::RSA => {
                let k = RsaPrivateKey::from_pkey(pk, if_rsa_algorithm)?;
                Ok(Self::Rsa(k))
            }
            Id::EC => {
                let k = EcdsaPrivateKey::from_pkey(pk)?;
                Ok(Self::Ecdsa(k))
            }
            Id::ED25519 => {
                let k = Ed25519PrivateKey::from_pkey(pk)?;
                Ok(Self::Ed25519(k))
            }
            _ => Err(Error::UnsupportedOrInvalidKey),
        }
    }

    pub fn private_key_to_pem_pkcs8(&self) -> Result<String> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.private_key_to_pem_pkcs8(),
            SomePrivateKey::Ecdsa(ec) => ec.private_key_to_pem_pkcs8(),
            SomePrivateKey::Rsa(rsa) => rsa.private_key_to_pem_pkcs8(),
        }
    }

    pub fn public_key_to_pem(&self) -> Result<String> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.public_key_to_pem(),
            SomePrivateKey::Ecdsa(ec) => ec.public_key_to_pem(),
            SomePrivateKey::Rsa(rsa) => rsa.public_key_to_pem(),
        }
    }
}

impl PublicKeyToJwk for SomePrivateKey {
    fn public_key_to_jwk(&self) -> Result<Jwk> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.public_key_to_jwk(),
            SomePrivateKey::Ecdsa(ec) => ec.public_key_to_jwk(),
            SomePrivateKey::Rsa(rsa) => rsa.public_key_to_jwk(),
        }
    }
}

impl PrivateKeyToJwk for SomePrivateKey {
    fn private_key_to_jwk(&self) -> Result<Jwk> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.private_key_to_jwk(),
            SomePrivateKey::Ecdsa(ec) => ec.private_key_to_jwk(),
            SomePrivateKey::Rsa(rsa) => rsa.private_key_to_jwk(),
        }
    }
}

impl SomePublicKey {
    /// Read an RSA/EC/Ed25519 public key from PEM.
    ///
    /// For an EC/Ed25519 public key, algorithm is deduced from the curve, e.g.
    /// P-256 -> ES256.
    ///
    /// For an RSA public key, signatures generated by any RSA algorithms can be
    /// verified.
    pub fn from_pem(pem: &[u8]) -> Result<Self> {
        let pk = PKey::public_key_from_pem(pem)?;
        match pk.id() {
            Id::RSA => {
                let k = RsaPublicKey::from_pkey(pk, None)?;
                Ok(Self::Rsa(k))
            }
            Id::EC => {
                let k = EcdsaPublicKey::from_pkey(pk)?;
                Ok(Self::Ecdsa(k))
            }
            Id::ED25519 => {
                let k = Ed25519PublicKey::from_pkey(pk)?;
                Ok(Self::Ed25519(k))
            }
            _ => Err(Error::UnsupportedOrInvalidKey),
        }
    }

    pub fn to_pem(&self) -> Result<String> {
        match self {
            SomePublicKey::Ed25519(ed) => ed.to_pem(),
            SomePublicKey::Ecdsa(ec) => ec.to_pem(),
            SomePublicKey::Rsa(rsa) => rsa.to_pem(),
        }
    }
}

impl SigningKey for SomePrivateKey {
    fn alg(&self) -> &'static str {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.alg(),
            SomePrivateKey::Ecdsa(ec) => ec.alg(),
            SomePrivateKey::Rsa(rsa) => rsa.alg(),
        }
    }

    fn sign(&self, v: &[u8]) -> crate::Result<smallvec::SmallVec<[u8; 64]>> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.sign(v),
            SomePrivateKey::Ecdsa(ec) => ec.sign(v),
            SomePrivateKey::Rsa(rsa) => rsa.sign(v),
        }
    }
}

impl VerificationKey for SomePrivateKey {
    fn verify(&self, v: &[u8], sig: &[u8], alg: &str) -> crate::Result<()> {
        match self {
            SomePrivateKey::Ed25519(ed) => ed.verify(v, sig, alg),
            SomePrivateKey::Ecdsa(ec) => ec.verify(v, sig, alg),
            SomePrivateKey::Rsa(rsa) => rsa.verify(v, sig, alg),
        }
    }
}

impl VerificationKey for SomePublicKey {
    fn verify(&self, v: &[u8], sig: &[u8], alg: &str) -> crate::Result<()> {
        match self {
            SomePublicKey::Ed25519(ed) => ed.verify(v, sig, alg),
            SomePublicKey::Ecdsa(ec) => ec.verify(v, sig, alg),
            SomePublicKey::Rsa(rsa) => rsa.verify(v, sig, alg),
        }
    }
}

impl PublicKeyToJwk for SomePublicKey {
    fn public_key_to_jwk(&self) -> Result<Jwk> {
        match self {
            SomePublicKey::Ed25519(ed) => ed.public_key_to_jwk(),
            SomePublicKey::Ecdsa(ec) => ec.public_key_to_jwk(),
            SomePublicKey::Rsa(rsa) => rsa.public_key_to_jwk(),
        }
    }
}