Skip to content

Commit 0c9946e

Browse files
committed
Revise errors for CertificateSigningRequestParams::from_der
- Introduce specific error for CSR signature verification - Make error name more specific for unsupported CSR extensions
1 parent 453bcb5 commit 0c9946e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

rcgen/src/csr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ impl CertificateSigningRequestParams {
105105
let csr = x509_parser::certification_request::X509CertificationRequest::from_der(csr)
106106
.map_err(|_| Error::CouldNotParseCertificationRequest)?
107107
.1;
108-
csr.verify_signature().map_err(|_| Error::RingUnspecified)?;
108+
csr.verify_signature()
109+
.map_err(|_| Error::InvalidSignatureInCsr)?;
109110
let alg_oid = csr
110111
.signature_algorithm
111112
.algorithm
@@ -161,10 +162,10 @@ impl CertificateSigningRequestParams {
161162
params.insert_extended_key_usage(ExtendedKeyUsagePurpose::OcspSigning);
162163
}
163164
if !eku.other.is_empty() {
164-
return Err(Error::UnsupportedExtension);
165+
return Err(Error::UnsupportedExtensionInCsr);
165166
}
166167
},
167-
_ => return Err(Error::UnsupportedExtension),
168+
_ => return Err(Error::UnsupportedExtensionInCsr),
168169
}
169170
}
170171
}

rcgen/src/error.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum Error {
2222
KeyGenerationUnavailable,
2323
#[cfg(feature = "x509-parser")]
2424
/// Unsupported extension requested in CSR
25-
UnsupportedExtension,
25+
UnsupportedExtensionInCsr,
2626
/// The requested signature algorithm is not supported
2727
UnsupportedSignatureAlgorithm,
2828
/// Unspecified `ring` error
@@ -48,6 +48,9 @@ pub enum Error {
4848
/// X509 parsing error
4949
#[cfg(feature = "x509-parser")]
5050
X509(String),
51+
/// Invalid signature when decoding a CSR
52+
#[cfg(feature = "x509-parser")]
53+
InvalidSignatureInCsr,
5154
}
5255

5356
impl fmt::Display for Error {
@@ -78,7 +81,7 @@ impl fmt::Display for Error {
7881
is not supported"
7982
)?,
8083
#[cfg(feature = "x509-parser")]
81-
UnsupportedExtension => write!(f, "Unsupported extension requested in CSR")?,
84+
UnsupportedExtensionInCsr => write!(f, "Unsupported extension requested in CSR")?,
8285
RingUnspecified => write!(f, "Unspecified ring error")?,
8386
RingKeyRejected(e) => write!(f, "Key rejected by ring: {e}")?,
8487

@@ -96,6 +99,8 @@ impl fmt::Display for Error {
9699
MissingSerialNumber => write!(f, "A serial number must be specified")?,
97100
#[cfg(feature = "x509-parser")]
98101
X509(e) => write!(f, "X.509 parsing error: {e}")?,
102+
#[cfg(feature = "x509-parser")]
103+
InvalidSignatureInCsr => write!(f, "Signature of CSR does not verify")?,
99104
};
100105
Ok(())
101106
}

0 commit comments

Comments
 (0)