Skip to main content

sec1/
error.rs

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