Skip to content

Commit 967d9f1

Browse files
authored
pkcs8: add KeyError enum (#2305)
Adds an enum to be carried along with `pkcs8::Error::KeyMalformed` that can give more specific details about what is wrong with the key, e.g. is it being rejected because it's too short or too long. This has been requested by people who say the existing variant doesn't give enough detail to diagnose the problem.
1 parent 1a52afe commit 967d9f1

4 files changed

Lines changed: 40 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ x509-tsp = { path = "./x509-tsp" }
6161
x509-cert = { path = "./x509-cert" }
6262
x509-ocsp = { path = "./x509-ocsp" }
6363

64+
rsa = { git = "https://github.com/RustCrypto/RSA", branch = "pkcs8/add-keyerror" }
65+
6466
[workspace.lints.clippy]
6567
borrow_as_ptr = "warn"
6668
cast_lossless = "warn"

pkcs8/src/error.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum Error {
2424
/// This is intended for relaying errors related to the raw data contained
2525
/// within [`PrivateKeyInfo::private_key`][`crate::PrivateKeyInfo::private_key`]
2626
/// or [`SubjectPublicKeyInfo::subject_public_key`][`crate::SubjectPublicKeyInfo::subject_public_key`].
27-
KeyMalformed,
27+
KeyMalformed(KeyError),
2828

2929
/// [`AlgorithmIdentifier::parameters`][`crate::AlgorithmIdentifierRef::parameters`]
3030
/// is malformed or otherwise encoded in an unexpected manner.
@@ -40,8 +40,8 @@ impl fmt::Display for Error {
4040
Error::Asn1(err) => write!(f, "PKCS#8 ASN.1 error: {err}"),
4141
#[cfg(feature = "pkcs5")]
4242
Error::EncryptedPrivateKey(err) => write!(f, "{err}"),
43-
Error::KeyMalformed => f.write_str("PKCS#8 cryptographic key data malformed"),
44-
Error::ParametersMalformed => f.write_str("PKCS#8 algorithm parameters malformed"),
43+
Error::KeyMalformed(err) => write!(f, "PKCS#8 key malformed: {err}"),
44+
Error::ParametersMalformed => write!(f, "PKCS#8 algorithm parameters malformed"),
4545
Error::PublicKey(err) => write!(f, "public key error: {err}"),
4646
}
4747
}
@@ -53,12 +53,19 @@ impl core::error::Error for Error {
5353
Error::Asn1(err) => Some(err),
5454
#[cfg(feature = "pkcs5")]
5555
Error::EncryptedPrivateKey(err) => Some(err),
56+
Error::KeyMalformed(err) => Some(err),
5657
Error::PublicKey(err) => Some(err),
5758
_ => None,
5859
}
5960
}
6061
}
6162

63+
impl From<KeyError> for Error {
64+
fn from(err: KeyError) -> Error {
65+
Error::KeyMalformed(err)
66+
}
67+
}
68+
6269
impl From<der::Error> for Error {
6370
fn from(err: der::Error) -> Error {
6471
Error::Asn1(err)
@@ -100,3 +107,29 @@ impl From<Error> for spki::Error {
100107
}
101108
}
102109
}
110+
111+
/// Key-related errors.
112+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
113+
#[non_exhaustive]
114+
pub enum KeyError {
115+
/// Key is not valid for this algorithm.
116+
Invalid,
117+
118+
/// Key is too short.
119+
TooShort,
120+
121+
/// Key is too long.
122+
TooLong,
123+
}
124+
125+
impl fmt::Display for KeyError {
126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127+
match self {
128+
KeyError::Invalid => f.write_str("key invalid"),
129+
KeyError::TooShort => f.write_str("key too short"),
130+
KeyError::TooLong => f.write_str("key too long"),
131+
}
132+
}
133+
}
134+
135+
impl core::error::Error for KeyError {}

pkcs8/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mod version;
7777
pub(crate) mod encrypted_private_key_info;
7878

7979
pub use crate::{
80-
error::{Error, Result},
80+
error::{Error, KeyError, Result},
8181
private_key_info::{PrivateKeyInfo, PrivateKeyInfoRef},
8282
traits::DecodePrivateKey,
8383
version::Version,

0 commit comments

Comments
 (0)