1use core::fmt;
4
5#[cfg(feature = "pem")]
6use der::pem;
7
8pub type Result<T> = core::result::Result<T, Error>;
10
11#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum Error {
15 Asn1(der::Error),
17
18 Crypto,
25
26 #[cfg(feature = "pkcs8")]
28 Pkcs8(pkcs8::Error),
29
30 Version,
32}
33
34impl fmt::Display for Error {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 Error::Asn1(err) => write!(f, "PKCS#1 ASN.1 error: {}", err),
38 Error::Crypto => f.write_str("PKCS#1 cryptographic error"),
39 #[cfg(feature = "pkcs8")]
40 Error::Pkcs8(err) => write!(f, "{}", err),
41 Error::Version => f.write_str("PKCS#1 version error"),
42 }
43 }
44}
45
46impl From<der::Error> for Error {
47 fn from(err: der::Error) -> Error {
48 Error::Asn1(err)
49 }
50}
51
52#[cfg(feature = "pem")]
53impl From<pem::Error> for Error {
54 fn from(err: pem::Error) -> Error {
55 der::Error::from(err).into()
56 }
57}
58
59#[cfg(feature = "pkcs8")]
60impl From<Error> for pkcs8::Error {
61 fn from(err: Error) -> pkcs8::Error {
62 match err {
63 Error::Asn1(e) => pkcs8::Error::Asn1(e),
64 Error::Crypto | Error::Version => pkcs8::Error::KeyMalformed,
65 Error::Pkcs8(e) => e,
66 }
67 }
68}
69
70#[cfg(feature = "pkcs8")]
71impl From<pkcs8::Error> for Error {
72 fn from(err: pkcs8::Error) -> Error {
73 Error::Pkcs8(err)
74 }
75}
76
77#[cfg(feature = "pkcs8")]
78impl From<Error> for pkcs8::spki::Error {
79 fn from(err: Error) -> pkcs8::spki::Error {
80 match err {
81 Error::Asn1(e) => pkcs8::spki::Error::Asn1(e),
82 _ => pkcs8::spki::Error::KeyMalformed,
83 }
84 }
85}
86
87#[cfg(feature = "pkcs8")]
88impl From<pkcs8::spki::Error> for Error {
89 fn from(err: pkcs8::spki::Error) -> Error {
90 Error::Pkcs8(pkcs8::Error::PublicKey(err))
91 }
92}
93
94#[cfg(feature = "std")]
95impl std::error::Error for Error {}