rcgen/
error.rs

1use std::fmt;
2
3#[derive(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	#[cfg(feature = "x509-parser")]
14	/// Invalid subject alternative name type
15	InvalidNameType,
16	/// Invalid ASN.1 string
17	InvalidAsn1String(InvalidAsn1String),
18	/// An IP address was provided as a byte array, but the byte array was an invalid length.
19	InvalidIpAddressOctetLength(usize),
20	/// There is no support for generating
21	/// keys for the given algorithm
22	KeyGenerationUnavailable,
23	#[cfg(feature = "x509-parser")]
24	/// Unsupported extension requested in CSR
25	UnsupportedExtension,
26	/// The requested signature algorithm is not supported
27	UnsupportedSignatureAlgorithm,
28	/// Unspecified `ring` error
29	RingUnspecified,
30	/// The `ring` library rejected the key upon loading
31	RingKeyRejected(String),
32	/// Time conversion related errors
33	Time,
34	#[cfg(feature = "pem")]
35	/// Error from the pem crate
36	PemError(String),
37	/// Error generated by a remote key operation
38	RemoteKeyError,
39	/// Unsupported field when generating a CSR
40	UnsupportedInCsr,
41	/// Invalid certificate revocation list (CRL) next update.
42	InvalidCrlNextUpdate,
43	/// CRL issuer specifies Key Usages that don't include cRLSign.
44	IssuerNotCrlSigner,
45	#[cfg(not(feature = "crypto"))]
46	/// Missing serial number
47	MissingSerialNumber,
48	/// X509 parsing error
49	#[cfg(feature = "x509-parser")]
50	X509(String),
51}
52
53impl fmt::Display for Error {
54	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55		use self::Error::*;
56		match self {
57			CouldNotParseCertificate => write!(f, "Could not parse certificate")?,
58			CouldNotParseCertificationRequest => write!(
59				f,
60				"Could not parse certificate signing \
61				request"
62			)?,
63			CouldNotParseKeyPair => write!(f, "Could not parse key pair")?,
64			#[cfg(feature = "x509-parser")]
65			InvalidNameType => write!(f, "Invalid subject alternative name type")?,
66			InvalidAsn1String(e) => write!(f, "{}", e)?,
67			InvalidIpAddressOctetLength(actual) => {
68				write!(f, "Invalid IP address octet length of {actual} bytes")?
69			},
70			KeyGenerationUnavailable => write!(
71				f,
72				"There is no support for generating \
73				keys for the given algorithm"
74			)?,
75			UnsupportedSignatureAlgorithm => write!(
76				f,
77				"The requested signature algorithm \
78				is not supported"
79			)?,
80			#[cfg(feature = "x509-parser")]
81			UnsupportedExtension => write!(f, "Unsupported extension requested in CSR")?,
82			RingUnspecified => write!(f, "Unspecified ring error")?,
83			RingKeyRejected(e) => write!(f, "Key rejected by ring: {}", e)?,
84
85			Time => write!(f, "Time error")?,
86			RemoteKeyError => write!(f, "Remote key error")?,
87			#[cfg(feature = "pem")]
88			PemError(e) => write!(f, "PEM error: {}", e)?,
89			UnsupportedInCsr => write!(f, "Certificate parameter unsupported in CSR")?,
90			InvalidCrlNextUpdate => write!(f, "Invalid CRL next update parameter")?,
91			IssuerNotCrlSigner => write!(
92				f,
93				"CRL issuer must specify no key usage, or key usage including cRLSign"
94			)?,
95			#[cfg(not(feature = "crypto"))]
96			MissingSerialNumber => write!(f, "A serial number must be specified")?,
97			#[cfg(feature = "x509-parser")]
98			X509(e) => write!(f, "X.509 parsing error: {e}")?,
99		};
100		Ok(())
101	}
102}
103
104impl std::error::Error for Error {}
105
106/// Invalid ASN.1 string type
107#[derive(Debug, PartialEq, Eq)]
108#[non_exhaustive]
109pub enum InvalidAsn1String {
110	/// Invalid PrintableString type
111	PrintableString(String),
112	/// Invalid UniversalString type
113	UniversalString(String),
114	/// Invalid Ia5String type
115	Ia5String(String),
116	/// Invalid TeletexString type
117	TeletexString(String),
118	/// Invalid BmpString type
119	BmpString(String),
120}
121
122impl fmt::Display for InvalidAsn1String {
123	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124		use InvalidAsn1String::*;
125		match self {
126			PrintableString(s) => write!(f, "Invalid PrintableString: '{}'", s)?,
127			Ia5String(s) => write!(f, "Invalid IA5String: '{}'", s)?,
128			BmpString(s) => write!(f, "Invalid BMPString: '{}'", s)?,
129			UniversalString(s) => write!(f, "Invalid UniversalString: '{}'", s)?,
130			TeletexString(s) => write!(f, "Invalid TeletexString: '{}'", s)?,
131		};
132		Ok(())
133	}
134}
135
136/// A trait describing an error that can be converted into an `rcgen::Error`.
137///
138/// We use this trait to avoid leaking external error types into the public API
139/// through a `From<x> for Error` implementation.
140#[cfg(any(feature = "crypto", feature = "pem"))]
141pub(crate) trait ExternalError<T>: Sized {
142	fn _err(self) -> Result<T, Error>;
143}