rcgen/
error.rs

1use std::fmt;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4#[non_exhaustive]
5/// The error type of the rcgen crate
6pub enum Error {
7	/// The given certificate couldn't be parsed
8	CouldNotParseCertificate,
9	/// The given certificate signing request couldn't be parsed
10	CouldNotParseCertificationRequest,
11	/// The given key pair couldn't be parsed
12	CouldNotParseKeyPair,
13	/// The CSR signature is invalid
14	#[cfg(feature = "x509-parser")]
15	InvalidCertificationRequestSignature,
16	#[cfg(feature = "x509-parser")]
17	/// Invalid subject alternative name type
18	InvalidNameType,
19	/// Invalid ASN.1 string
20	InvalidAsn1String(InvalidAsn1String),
21	/// An IP address was provided as a byte array, but the byte array was an invalid length.
22	InvalidIpAddressOctetLength(usize),
23	/// There is no support for generating
24	/// keys for the given algorithm
25	KeyGenerationUnavailable,
26	#[cfg(feature = "x509-parser")]
27	/// Unsupported extension requested in CSR
28	UnsupportedExtension,
29	/// The requested signature algorithm is not supported
30	UnsupportedSignatureAlgorithm,
31	/// Unspecified `ring` error
32	RingUnspecified,
33	/// The `ring` library rejected the key upon loading
34	RingKeyRejected(String),
35	/// Time conversion related errors
36	Time,
37	#[cfg(feature = "pem")]
38	/// Error from the pem crate
39	PemError(String),
40	/// Error generated by a remote key operation
41	RemoteKeyError,
42	/// Unsupported field when generating a CSR
43	UnsupportedInCsr,
44	/// Invalid certificate revocation list (CRL) next update.
45	InvalidCrlNextUpdate,
46	/// CRL issuer specifies Key Usages that don't include cRLSign.
47	IssuerNotCrlSigner,
48	#[cfg(not(feature = "crypto"))]
49	/// Missing serial number
50	MissingSerialNumber,
51	/// X509 parsing error
52	#[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/// Invalid ASN.1 string type
112#[derive(Clone, Debug, PartialEq, Eq)]
113#[non_exhaustive]
114pub enum InvalidAsn1String {
115	/// Invalid PrintableString type
116	PrintableString(String),
117	/// Invalid UniversalString type
118	UniversalString(String),
119	/// Invalid Ia5String type
120	Ia5String(String),
121	/// Invalid TeletexString type
122	TeletexString(String),
123	/// Invalid BmpString type
124	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/// A trait describing an error that can be converted into an `rcgen::Error`.
142///
143/// We use this trait to avoid leaking external error types into the public API
144/// through a `From<x> for Error` implementation.
145#[cfg(any(feature = "crypto", feature = "pem"))]
146pub(crate) trait ExternalError<T>: Sized {
147	fn _err(self) -> Result<T, Error>;
148}