Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spki: error improvements #212

Merged
merged 1 commit into from Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkcs8/src/error.rs
Expand Up @@ -28,6 +28,9 @@ pub enum Error {
/// [`AlgorithmIdentifier::parameters`][`crate::AlgorithmIdentifier::parameters`]
/// is malformed or otherwise encoded in an unexpected manner.
ParametersMalformed,

/// Public key errors propagated from the [`spki::Error`] type.
PublicKey(spki::Error),
}

impl fmt::Display for Error {
Expand All @@ -37,6 +40,7 @@ impl fmt::Display for Error {
Error::Crypto => f.write_str("PKCS#8 cryptographic error"),
Error::KeyMalformed => f.write_str("PKCS#8 cryptographic key data malformed"),
Error::ParametersMalformed => f.write_str("PKCS#8 algorithm parameters malformed"),
Error::PublicKey(err) => write!(f, "public key error: {}", err),
}
}
}
Expand All @@ -55,3 +59,9 @@ impl From<der::ErrorKind> for Error {
Error::Asn1(err.into())
}
}

impl From<spki::Error> for Error {
fn from(err: spki::Error) -> Error {
Error::PublicKey(err)
}
}
4 changes: 2 additions & 2 deletions spki/src/algorithm.rs
Expand Up @@ -31,7 +31,7 @@ impl<'a> AlgorithmIdentifier<'a> {
if self.oid == expected_oid {
Ok(expected_oid)
} else {
Err(Error::UnknownOid { oid: expected_oid })
Err(Error::OidUnknown { oid: expected_oid })
}
}

Expand All @@ -45,7 +45,7 @@ impl<'a> AlgorithmIdentifier<'a> {
if actual_oid == expected_oid {
Ok(actual_oid)
} else {
Err(Error::UnknownOid { oid: expected_oid })
Err(Error::OidUnknown { oid: expected_oid })
}
}

Expand Down
10 changes: 7 additions & 3 deletions spki/src/error.rs
Expand Up @@ -17,7 +17,7 @@ pub enum Error {
Asn1(der::Error),

/// Unknown algorithm OID.
UnknownOid {
OidUnknown {
/// Unrecognized OID value found in e.g. a SPKI `AlgorithmIdentifier`.
oid: ObjectIdentifier,
},
Expand All @@ -30,7 +30,7 @@ impl fmt::Display for Error {
f.write_str("AlgorithmIdentifier parameters missing")
}
Error::Asn1(err) => write!(f, "ASN.1 error: {}", err),
Error::UnknownOid { oid } => {
Error::OidUnknown { oid } => {
write!(f, "unknown/unsupported algorithm OID: {}", oid)
}
}
Expand All @@ -39,7 +39,11 @@ impl fmt::Display for Error {

impl From<der::Error> for Error {
fn from(err: der::Error) -> Error {
Error::Asn1(err)
if let der::ErrorKind::OidUnknown { oid } = err.kind() {
Error::OidUnknown { oid }
} else {
Error::Asn1(err)
}
}
}

Expand Down