Skip to content

Commit

Permalink
error: avoid leaking pem::PemError into api
Browse files Browse the repository at this point in the history
Having `From<pem::PemError>` defined on the public `Error`
type means that the `pem` type leaks into rcgen's public API,
complicating semver incompatible updates.

This commit updates the sites that previously used this trait to
directly map to the generic rcgen `Error::PemError` err. There's only
two places that need that so a helper fn doesn't seem warranted.
Additionally, the error variant is changed to hold a `String` with the
`pem::PemError` error string instead of the type itself. This allows the
`From` impl on `Error` to be removed, fixing the type leak.
  • Loading branch information
cpu committed Oct 27, 2023
1 parent b2a21fb commit 64fc928
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
9 changes: 1 addition & 8 deletions rcgen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum Error {
Time,
#[cfg(feature = "pem")]
/// Error from the pem crate
PemError(pem::PemError),
PemError(String),
/// Error generated by a remote key operation
RemoteKeyError,
/// Unsupported field when generating a CSR
Expand Down Expand Up @@ -97,10 +97,3 @@ impl fmt::Display for Error {
}

impl std::error::Error for Error {}

#[cfg(feature = "pem")]
impl From<pem::PemError> for Error {
fn from(e: pem::PemError) -> Self {
Error::PemError(e)
}
}
4 changes: 2 additions & 2 deletions rcgen/src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl KeyPair {
/// Parses the key pair from the ASCII PEM format
#[cfg(feature = "pem")]
pub fn from_pem(pem_str: &str) -> Result<Self, Error> {
let private_key = pem::parse(pem_str)?;
let private_key = pem::parse(pem_str).map_err(|e| Error::PemError(e.to_string()))?;
let private_key_der: &[_] = private_key.contents();
Ok(private_key_der.try_into()?)
}
Expand All @@ -90,7 +90,7 @@ impl KeyPair {
pem_str: &str,
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error> {
let private_key = pem::parse(pem_str)?;
let private_key = pem::parse(pem_str).map_err(|e| Error::PemError(e.to_string()))?;
let private_key_der: &[_] = private_key.contents();
Ok(Self::from_der_and_sign_algo(private_key_der, alg)?)
}
Expand Down

0 comments on commit 64fc928

Please sign in to comment.