Skip to main content

synta_certificate/crypto/
errors.rs

1//! Error types shared across the crypto submodules.
2
3// ── PrivateKeyError ───────────────────────────────────────────────────────────
4
5/// Type-erased error returned by [`PrivateKey`] operations.
6///
7/// Wraps a [`Box<dyn Error + Send + Sync>`] in a named struct so that the
8/// `impl Error for PrivateKeyError` bound needed by
9/// [`CertificateSigner::Error`](super::CertificateSigner::Error)
10/// can be satisfied without ambiguity.
11///
12/// [`PrivateKey`]: super::PrivateKey
13#[derive(Debug)]
14pub struct PrivateKeyError(pub Box<dyn std::error::Error + Send + Sync + 'static>);
15
16impl std::fmt::Display for PrivateKeyError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        self.0.fmt(f)
19    }
20}
21
22impl std::error::Error for PrivateKeyError {
23    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
24        self.0.source()
25    }
26}
27
28impl PrivateKeyError {
29    /// Wrap any `Error + Send + Sync` value.
30    pub fn new<E: std::error::Error + Send + Sync + 'static>(e: E) -> Self {
31        Self(Box::new(e))
32    }
33}
34
35// ── NoCryptoError ─────────────────────────────────────────────────────────────
36
37/// Error returned by no-op sentinel crypto types when no backend is compiled in.
38#[derive(Debug)]
39pub struct NoCryptoError;
40
41impl std::fmt::Display for NoCryptoError {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.write_str(
44            "no crypto backend configured; cannot decrypt PKCS#12 encrypted bags \
45                     (enable the 'openssl' feature or provide a Pkcs12Decryptor)",
46        )
47    }
48}
49
50impl std::error::Error for NoCryptoError {}
51
52// ── NoEncryptorError ──────────────────────────────────────────────────────────
53
54/// Error returned by [`NoEncryptor`] and [`NoPkcs12Encryptor`].
55///
56/// [`NoEncryptor`]: super::NoEncryptor
57/// [`NoPkcs12Encryptor`]: super::NoPkcs12Encryptor
58#[derive(Debug)]
59pub struct NoEncryptorError;
60
61impl std::fmt::Display for NoEncryptorError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.write_str(
64            "no crypto backend configured; cannot encrypt content \
65             (enable the 'openssl' feature or provide an Encryptor implementation)",
66        )
67    }
68}
69
70impl std::error::Error for NoEncryptorError {}
71
72// ── NoSignerError ─────────────────────────────────────────────────────────────
73
74/// Error returned by [`NoSigner`].
75///
76/// [`NoSigner`]: super::NoSigner
77#[derive(Debug)]
78pub struct NoSignerError;
79
80impl std::fmt::Display for NoSignerError {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        f.write_str(
83            "no certificate signer configured; cannot sign certificates \
84             (enable the 'openssl' feature or provide a CertificateSigner implementation)",
85        )
86    }
87}
88
89impl std::error::Error for NoSignerError {}
90
91// ── NoSignatureVerifierError ──────────────────────────────────────────────────
92
93/// Error returned by [`NoSignatureVerifier`].
94///
95/// [`NoSignatureVerifier`]: super::NoSignatureVerifier
96#[derive(Debug)]
97pub struct NoSignatureVerifierError;
98
99impl std::fmt::Display for NoSignatureVerifierError {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        f.write_str(
102            "no signature verifier configured; cannot verify certificate signatures \
103             (enable the 'openssl' feature or provide a SignatureVerifier implementation)",
104        )
105    }
106}
107
108impl std::error::Error for NoSignatureVerifierError {}
109
110// ── NoKeyIdHasherError ────────────────────────────────────────────────────────
111
112/// Error returned by [`NoKeyIdHasher`].
113///
114/// [`NoKeyIdHasher`]: super::NoKeyIdHasher
115#[derive(Debug)]
116pub struct NoKeyIdHasherError;
117
118impl std::fmt::Display for NoKeyIdHasherError {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        f.write_str(
121            "no crypto backend configured: enable the 'openssl' feature \
122             for SubjectKeyIdentifier / AuthorityKeyIdentifier hashing",
123        )
124    }
125}
126
127impl std::error::Error for NoKeyIdHasherError {}
128
129// ── NoEnvelopedDataDecryptorError ─────────────────────────────────────────────
130
131/// Error returned by [`NoEnvelopedDataDecryptor`].
132///
133/// [`NoEnvelopedDataDecryptor`]: super::NoEnvelopedDataDecryptor
134#[derive(Debug)]
135pub struct NoEnvelopedDataDecryptorError;
136
137impl std::fmt::Display for NoEnvelopedDataDecryptorError {
138    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139        f.write_str(
140            "no crypto backend configured; cannot decrypt EnvelopedData \
141             (enable the 'openssl' feature or provide an EnvelopedDataDecryptor implementation)",
142        )
143    }
144}
145
146impl std::error::Error for NoEnvelopedDataDecryptorError {}