1#![allow(clippy::module_name_repetitions)]
2
3use rcgen::KeyPair;
4use x509_certificate::{CapturedX509Certificate, InMemorySigningKeyPair};
5
6pub struct SigningKey {
8 pub(crate) key_rcgen: KeyPair,
9 pub(crate) key_cms: InMemorySigningKeyPair,
10}
11
12impl SigningKey {
13 pub fn from_pem(pem_str: &str) -> Result<Self, SigningKeyParseError> {
18 let key_rcgen = KeyPair::from_pem(pem_str)?;
19 let key_cms = InMemorySigningKeyPair::from_pkcs8_pem(pem_str)?;
20 Ok(Self { key_rcgen, key_cms })
21 }
22
23 pub fn from_der(der: &[u8]) -> Result<Self, SigningKeyParseError> {
28 let key_rcgen = KeyPair::try_from(der)?;
29 let key_cms = InMemorySigningKeyPair::from_pkcs8_der(der)?;
30 Ok(Self { key_rcgen, key_cms })
31 }
32}
33
34impl std::fmt::Debug for SigningKey {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 f.debug_struct("SigningKey").finish()
37 }
38}
39
40#[derive(Debug, Clone)]
42pub struct SigningCertificate {
43 pub(crate) cert: CapturedX509Certificate,
44}
45
46impl SigningCertificate {
47 pub fn from_pem(pem_str: &str) -> Result<Self, SigningCertificateParseError> {
52 let cert = CapturedX509Certificate::from_pem(pem_str)?;
53 Ok(Self { cert })
54 }
55
56 pub fn from_der(der: &[u8]) -> Result<Self, SigningCertificateParseError> {
61 let cert = CapturedX509Certificate::from_der(der)?;
62 Ok(Self { cert })
63 }
64}
65
66#[derive(Debug)]
68pub enum SigningKeyParseError {
69 Csr(rcgen::Error),
70 Cms(x509_certificate::X509CertificateError),
71}
72
73impl std::fmt::Display for SigningKeyParseError {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self {
76 Self::Csr(e) => write!(f, "failed to parse key for CSR signing: {e}"),
77 Self::Cms(e) => write!(f, "failed to parse key for CMS signing: {e}"),
78 }
79 }
80}
81
82impl std::error::Error for SigningKeyParseError {}
83
84impl From<rcgen::Error> for SigningKeyParseError {
85 fn from(value: rcgen::Error) -> Self {
86 Self::Csr(value)
87 }
88}
89
90impl From<x509_certificate::X509CertificateError> for SigningKeyParseError {
91 fn from(value: x509_certificate::X509CertificateError) -> Self {
92 Self::Cms(value)
93 }
94}
95
96#[derive(Debug)]
98pub struct SigningCertificateParseError(x509_certificate::X509CertificateError);
99
100impl std::fmt::Display for SigningCertificateParseError {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 write!(f, "failed to parse certificate: {}", self.0)
103 }
104}
105
106impl std::error::Error for SigningCertificateParseError {}
107
108impl From<x509_certificate::X509CertificateError> for SigningCertificateParseError {
109 fn from(value: x509_certificate::X509CertificateError) -> Self {
110 Self(value)
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 const EXAMPLE_RSA_CERT_PEM: &str = include_str!("../test/example.rsa.pem.cert");
119 const EXAMPLE_RSA_CERT_DER: &[u8] = include_bytes!("../test/example.rsa.der.cert");
120 const EXAMPLE_EC_KEY_PEM: &str = include_str!("../test/example.ec.pem.key");
121 const EXAMPLE_EC_KEY_DER: &[u8] = include_bytes!("../test/example.ec.der.key");
122
123 impl SigningKey {
124 pub(crate) fn new_testing_key() -> Self {
125 Self::from_pem(EXAMPLE_EC_KEY_PEM).unwrap()
126 }
127 }
128
129 impl SigningCertificate {
130 pub(crate) fn new_testing_cert() -> Self {
131 Self::from_pem(EXAMPLE_RSA_CERT_PEM).unwrap()
132 }
133 }
134
135 #[test]
136 fn test_example_idempotency() {
137 let once = SigningKey::new_testing_key();
138 let twice = SigningKey::new_testing_key();
139 assert_eq!(
140 once.key_rcgen.serialized_der(),
141 twice.key_rcgen.serialized_der()
142 );
143
144 let once = SigningCertificate::new_testing_cert();
145 let twice = SigningCertificate::new_testing_cert();
146 assert_eq!(
147 once.cert.encode_der().unwrap(),
148 twice.cert.encode_der().unwrap()
149 );
150 }
151
152 #[test]
153 fn test_signing_key_from_pem() {
154 SigningKey::from_pem(EXAMPLE_EC_KEY_PEM).unwrap();
155 assert!(SigningKey::from_pem("invalid").is_err());
156 }
157
158 #[test]
159 fn test_signing_key_from_der() {
160 SigningKey::from_der(EXAMPLE_EC_KEY_DER).unwrap();
161 assert!(SigningKey::from_der(b"invalid").is_err());
162 }
163
164 #[test]
165 fn test_certificate_from_pem() {
166 SigningCertificate::from_pem(EXAMPLE_RSA_CERT_PEM).unwrap();
167 assert!(SigningCertificate::from_pem("invalid").is_err());
168 }
169
170 #[test]
171 fn test_certificate_from_der() {
172 SigningCertificate::from_der(EXAMPLE_RSA_CERT_DER).unwrap();
173 assert!(SigningCertificate::from_der(b"invalid").is_err());
174 }
175}