Skip to content

Commit b0fef6b

Browse files
committed
error: combine Error::Ring and Error::RingUnspecified
Use one generic `Error::Ring(String)` error for all upstream *ring* errors.
1 parent 34375ac commit b0fef6b

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

rcgen/src/csr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ impl CertificateSigningRequest {
5151
let csr = x509_parser::certification_request::X509CertificationRequest::from_der(csr)
5252
.map_err(|_| Error::CouldNotParseCertificationRequest)?
5353
.1;
54-
csr.verify_signature().map_err(|_| Error::RingUnspecified)?;
54+
csr.verify_signature()
55+
.map_err(|_| Error::Ring("Unspecified error".into()))?;
5556
let alg_oid = csr
5657
.signature_algorithm
5758
.algorithm

rcgen/src/error.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ pub enum Error {
2323
UnsupportedExtension,
2424
/// The requested signature algorithm is not supported
2525
UnsupportedSignatureAlgorithm,
26-
/// Unspecified `ring` error
27-
RingUnspecified,
28-
/// The `ring` library rejected the key upon loading
29-
RingKeyRejected(String),
26+
/// An error from the `ring` library was encountered
27+
Ring(String),
3028
/// The provided certificate's signature algorithm
3129
/// is incompatible with the given key pair
3230
CertificateKeyPairMismatch,
@@ -73,8 +71,7 @@ impl fmt::Display for Error {
7371
)?,
7472
#[cfg(feature = "x509-parser")]
7573
UnsupportedExtension => write!(f, "Unsupported extension requested in CSR")?,
76-
RingUnspecified => write!(f, "Unspecified ring error")?,
77-
RingKeyRejected(e) => write!(f, "Key rejected by ring: {}", e)?,
74+
Ring(e) => write!(f, "Error from *ring*: {}", e)?,
7875
CertificateKeyPairMismatch => write!(
7976
f,
8077
"The provided certificate's signature \

rcgen/src/key_pair.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,30 @@ impl KeyPair {
113113

114114
let kind = if alg == &PKCS_ED25519 {
115115
KeyPairKind::Ed(
116-
Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8).map_err(key_rejected_err)?,
116+
Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8)
117+
.map_err(|e| Error::Ring(e.to_string()))?,
117118
)
118119
} else if alg == &PKCS_ECDSA_P256_SHA256 {
119120
KeyPairKind::Ec(
120121
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P256_SHA256_ASN1_SIGNING, pkcs8, rng)
121-
.map_err(key_rejected_err)?,
122+
.map_err(|e| Error::Ring(e.to_string()))?,
122123
)
123124
} else if alg == &PKCS_ECDSA_P384_SHA384 {
124125
KeyPairKind::Ec(
125126
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, pkcs8, rng)
126-
.map_err(key_rejected_err)?,
127+
.map_err(|e| Error::Ring(e.to_string()))?,
127128
)
128129
} else if alg == &PKCS_RSA_SHA256 {
129-
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(key_rejected_err)?;
130+
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(|e| Error::Ring(e.to_string()))?;
130131
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA256)
131132
} else if alg == &PKCS_RSA_SHA384 {
132-
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(key_rejected_err)?;
133+
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(|e| Error::Ring(e.to_string()))?;
133134
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA384)
134135
} else if alg == &PKCS_RSA_SHA512 {
135-
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(key_rejected_err)?;
136+
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(|e| Error::Ring(e.to_string()))?;
136137
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA512)
137138
} else if alg == &PKCS_RSA_PSS_SHA256 {
138-
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(key_rejected_err)?;
139+
let rsakp = RsaKeyPair::from_pkcs8(pkcs8).map_err(|e| Error::Ring(e.to_string()))?;
139140
KeyPairKind::Rsa(rsakp, &signature::RSA_PSS_SHA256)
140141
} else {
141142
panic!("Unknown SignatureAlgorithm specified!");
@@ -180,7 +181,7 @@ impl KeyPair {
180181
match alg.sign_alg {
181182
SignAlgo::EcDsa(sign_alg) => {
182183
let key_pair_doc = EcdsaKeyPair::generate_pkcs8(sign_alg, rng)
183-
.map_err(|_| Error::RingUnspecified)?;
184+
.map_err(|_| Error::Ring("Unspecified error".into()))?;
184185
let key_pair_serialized = key_pair_doc.as_ref().to_vec();
185186

186187
let key_pair =
@@ -192,8 +193,8 @@ impl KeyPair {
192193
})
193194
},
194195
SignAlgo::EdDsa(_sign_alg) => {
195-
let key_pair_doc =
196-
Ed25519KeyPair::generate_pkcs8(rng).map_err(|_| Error::RingUnspecified)?;
196+
let key_pair_doc = Ed25519KeyPair::generate_pkcs8(rng)
197+
.map_err(|_| Error::Ring("Unspecified error".into()))?;
197198
let key_pair_serialized = key_pair_doc.as_ref().to_vec();
198199

199200
let key_pair = Ed25519KeyPair::from_pkcs8(&&key_pair_doc.as_ref()).unwrap();
@@ -236,7 +237,7 @@ impl KeyPair {
236237
let system_random = SystemRandom::new();
237238
let signature = kp
238239
.sign(&system_random, msg)
239-
.map_err(|_| Error::RingUnspecified)?;
240+
.map_err(|_| Error::Ring("Unspecified error".into()))?;
240241
let sig = &signature.as_ref();
241242
writer.write_bitvec_bytes(&sig, &sig.len() * 8);
242243
},
@@ -249,7 +250,7 @@ impl KeyPair {
249250
let system_random = SystemRandom::new();
250251
let mut signature = vec![0; kp.public().modulus_len()];
251252
kp.sign(*padding_alg, &system_random, msg, &mut signature)
252-
.map_err(|_| Error::RingUnspecified)?;
253+
.map_err(|_| Error::Ring("Unspecified error".into()))?;
253254
let sig = &signature.as_ref();
254255
writer.write_bitvec_bytes(&sig, &sig.len() * 8);
255256
},
@@ -376,10 +377,6 @@ pub trait RemoteKeyPair {
376377
fn algorithm(&self) -> &'static SignatureAlgorithm;
377378
}
378379

379-
pub(crate) fn key_rejected_err(err: ring::error::KeyRejected) -> Error {
380-
Error::RingKeyRejected(err.to_string())
381-
}
382-
383380
pub(crate) trait PublicKeyData {
384381
fn alg(&self) -> &SignatureAlgorithm;
385382

rcgen/tests/webpki.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn from_remote() {
327327
self.0
328328
.sign(&system_random, msg)
329329
.map(|s| s.as_ref().to_owned())
330-
.map_err(|_| Error::RingUnspecified)
330+
.map_err(|e| Error::Ring(e.to_string()))
331331
}
332332

333333
fn algorithm(&self) -> &'static rcgen::SignatureAlgorithm {

0 commit comments

Comments
 (0)