1 //! Error types 2 3 use core::fmt; 4 use der::asn1::ObjectIdentifier; 5 6 /// Result type with `spki` crate's [`Error`] type. 7 pub type Result<T> = core::result::Result<T, Error>; 8 9 #[cfg(feature = "pem")] 10 use der::pem; 11 12 /// Error type 13 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 14 #[non_exhaustive] 15 pub enum Error { 16 /// Algorithm parameters are missing. 17 AlgorithmParametersMissing, 18 19 /// ASN.1 DER-related errors. 20 Asn1(der::Error), 21 22 /// Malformed cryptographic key contained in a SPKI document. 23 /// 24 /// This is intended for relaying errors related to the raw data contained 25 /// in [`SubjectPublicKeyInfo::subject_public_key`][`crate::SubjectPublicKeyInfo::subject_public_key`]. 26 KeyMalformed, 27 28 /// Unknown algorithm OID. 29 OidUnknown { 30 /// Unrecognized OID value found in e.g. a SPKI `AlgorithmIdentifier`. 31 oid: ObjectIdentifier, 32 }, 33 } 34 35 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 37 match self { 38 Error::AlgorithmParametersMissing => { 39 f.write_str("AlgorithmIdentifier parameters missing") 40 } 41 Error::Asn1(err) => write!(f, "ASN.1 error: {}", err), 42 Error::KeyMalformed => f.write_str("SPKI cryptographic key data malformed"), 43 Error::OidUnknown { oid } => { 44 write!(f, "unknown/unsupported algorithm OID: {}", oid) 45 } 46 } 47 } 48 } 49 50 impl From<der::Error> for Error { from(err: der::Error) -> Error51 fn from(err: der::Error) -> Error { 52 if let der::ErrorKind::OidUnknown { oid } = err.kind() { 53 Error::OidUnknown { oid } 54 } else { 55 Error::Asn1(err) 56 } 57 } 58 } 59 60 #[cfg(feature = "pem")] 61 impl From<pem::Error> for Error { from(err: pem::Error) -> Error62 fn from(err: pem::Error) -> Error { 63 der::Error::from(err).into() 64 } 65 } 66 67 #[cfg(feature = "std")] 68 impl std::error::Error for Error {} 69