@@ -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+
6269impl 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 { }
0 commit comments