sec1/
error.rs

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