1 //! Error types 2 3 use core::fmt; 4 5 #[cfg(feature = "pem")] 6 use der::pem; 7 8 /// Result type with `sec1` crate's [`Error`] type. 9 pub type Result<T> = core::result::Result<T, Error>; 10 11 /// Error type 12 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 13 #[non_exhaustive] 14 pub enum Error { 15 /// ASN.1 DER-related errors. 16 #[cfg(feature = "der")] 17 #[cfg_attr(docsrs, doc(cfg(feature = "der")))] 18 Asn1(der::Error), 19 20 /// Cryptographic errors. 21 /// 22 /// These can be used by EC implementations to signal that a key is 23 /// invalid for cryptographic reasons. This means the document parsed 24 /// correctly, but one of the values contained within was invalid, e.g. 25 /// a number expected to be a prime was not a prime. 26 Crypto, 27 28 /// PKCS#8 errors. 29 #[cfg(feature = "pkcs8")] 30 Pkcs8(pkcs8::Error), 31 32 /// Errors relating to the `Elliptic-Curve-Point-to-Octet-String` or 33 /// `Octet-String-to-Elliptic-Curve-Point` encodings. 34 PointEncoding, 35 36 /// Version errors 37 Version, 38 } 39 40 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 42 match self { 43 #[cfg(feature = "der")] 44 Error::Asn1(err) => write!(f, "SEC1 ASN.1 error: {}", err), 45 Error::Crypto => f.write_str("SEC1 cryptographic error"), 46 #[cfg(feature = "pkcs8")] 47 Error::Pkcs8(err) => write!(f, "{}", err), 48 Error::PointEncoding => f.write_str("elliptic curve point encoding error"), 49 Error::Version => f.write_str("SEC1 version error"), 50 } 51 } 52 } 53 54 #[cfg(feature = "der")] 55 #[cfg_attr(docsrs, doc(cfg(feature = "der")))] 56 impl From<der::Error> for Error { from(err: der::Error) -> Error57 fn from(err: der::Error) -> Error { 58 Error::Asn1(err) 59 } 60 } 61 62 #[cfg(feature = "pem")] 63 impl From<pem::Error> for Error { from(err: pem::Error) -> Error64 fn from(err: pem::Error) -> Error { 65 der::Error::from(err).into() 66 } 67 } 68 69 #[cfg(feature = "pkcs8")] 70 impl From<pkcs8::Error> for Error { from(err: pkcs8::Error) -> Error71 fn from(err: pkcs8::Error) -> Error { 72 Error::Pkcs8(err) 73 } 74 } 75 76 #[cfg(feature = "pkcs8")] 77 impl From<pkcs8::spki::Error> for Error { from(err: pkcs8::spki::Error) -> Error78 fn from(err: pkcs8::spki::Error) -> Error { 79 Error::Pkcs8(pkcs8::Error::PublicKey(err)) 80 } 81 } 82 83 #[cfg(feature = "std")] 84 impl std::error::Error for Error {} 85