1use std::fmt;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4#[non_exhaustive]
5pub enum Error {
7 CouldNotParseCertificate,
9 CouldNotParseCertificationRequest,
11 CouldNotParseKeyPair,
13 #[cfg(feature = "x509-parser")]
15 InvalidCertificationRequestSignature,
16 #[cfg(feature = "x509-parser")]
17 InvalidNameType,
19 InvalidAsn1String(InvalidAsn1String),
21 InvalidIpAddressOctetLength(usize),
23 KeyGenerationUnavailable,
26 #[cfg(feature = "x509-parser")]
27 UnsupportedExtension,
29 UnsupportedSignatureAlgorithm,
31 RingUnspecified,
33 RingKeyRejected(String),
35 Time,
37 #[cfg(feature = "pem")]
38 PemError(String),
40 RemoteKeyError,
42 UnsupportedInCsr,
44 InvalidCrlNextUpdate,
46 IssuerNotCrlSigner,
48 #[cfg(not(feature = "crypto"))]
49 MissingSerialNumber,
51 #[cfg(feature = "x509-parser")]
53 X509(String),
54}
55
56impl fmt::Display for Error {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 use self::Error::*;
59 match self {
60 CouldNotParseCertificate => write!(f, "Could not parse certificate")?,
61 CouldNotParseCertificationRequest => write!(
62 f,
63 "Could not parse certificate signing \
64 request"
65 )?,
66 CouldNotParseKeyPair => write!(f, "Could not parse key pair")?,
67 #[cfg(feature = "x509-parser")]
68 InvalidCertificationRequestSignature => write!(f, "Invalid CSR signature")?,
69 #[cfg(feature = "x509-parser")]
70 InvalidNameType => write!(f, "Invalid subject alternative name type")?,
71 InvalidAsn1String(e) => write!(f, "{e}")?,
72 InvalidIpAddressOctetLength(actual) => {
73 write!(f, "Invalid IP address octet length of {actual} bytes")?
74 },
75 KeyGenerationUnavailable => write!(
76 f,
77 "There is no support for generating \
78 keys for the given algorithm"
79 )?,
80 UnsupportedSignatureAlgorithm => write!(
81 f,
82 "The requested signature algorithm \
83 is not supported"
84 )?,
85 #[cfg(feature = "x509-parser")]
86 UnsupportedExtension => write!(f, "Unsupported extension requested in CSR")?,
87 RingUnspecified => write!(f, "Unspecified ring error")?,
88 RingKeyRejected(e) => write!(f, "Key rejected by ring: {e}")?,
89
90 Time => write!(f, "Time error")?,
91 RemoteKeyError => write!(f, "Remote key error")?,
92 #[cfg(feature = "pem")]
93 PemError(e) => write!(f, "PEM error: {e}")?,
94 UnsupportedInCsr => write!(f, "Certificate parameter unsupported in CSR")?,
95 InvalidCrlNextUpdate => write!(f, "Invalid CRL next update parameter")?,
96 IssuerNotCrlSigner => write!(
97 f,
98 "CRL issuer must specify no key usage, or key usage including cRLSign"
99 )?,
100 #[cfg(not(feature = "crypto"))]
101 MissingSerialNumber => write!(f, "A serial number must be specified")?,
102 #[cfg(feature = "x509-parser")]
103 X509(e) => write!(f, "X.509 parsing error: {e}")?,
104 };
105 Ok(())
106 }
107}
108
109impl std::error::Error for Error {}
110
111#[derive(Clone, Debug, PartialEq, Eq)]
113#[non_exhaustive]
114pub enum InvalidAsn1String {
115 PrintableString(String),
117 UniversalString(String),
119 Ia5String(String),
121 TeletexString(String),
123 BmpString(String),
125}
126
127impl fmt::Display for InvalidAsn1String {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 use InvalidAsn1String::*;
130 match self {
131 PrintableString(s) => write!(f, "Invalid PrintableString: '{s}'")?,
132 Ia5String(s) => write!(f, "Invalid IA5String: '{s}'")?,
133 BmpString(s) => write!(f, "Invalid BMPString: '{s}'")?,
134 UniversalString(s) => write!(f, "Invalid UniversalString: '{s}'")?,
135 TeletexString(s) => write!(f, "Invalid TeletexString: '{s}'")?,
136 };
137 Ok(())
138 }
139}
140
141#[cfg(any(feature = "crypto", feature = "pem"))]
146pub(crate) trait ExternalError<T>: Sized {
147 fn _err(self) -> Result<T, Error>;
148}