Skip to main content

rocket/tls/
error.rs

1use rustls::pki_types::pem;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
4
5#[derive(Debug)]
6pub enum KeyError {
7    BadKeyCount(usize),
8    Pem(pem::Error),
9    Unsupported(rustls::Error),
10    BadItem(pem::SectionKind),
11}
12
13#[derive(Debug)]
14pub enum Error {
15    Io(std::io::Error),
16    Bind(Box<dyn std::error::Error + Send + 'static>),
17    Tls(rustls::Error),
18    Mtls(rustls::server::VerifierBuilderError),
19    Pem(pem::Error),
20    CertChain(pem::Error),
21    PrivKey(KeyError),
22    CertAuth(rustls::Error),
23    Config(figment::Error),
24}
25
26impl std::fmt::Display for Error {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        use Error::*;
29
30        match self {
31            Io(e) => write!(f, "i/o error during tls binding: {e}"),
32            Tls(e) => write!(f, "tls configuration error: {e}"),
33            Mtls(e) => write!(f, "mtls verifier error: {e}"),
34            Pem(e) => write!(f, "failed to process pem data: {e}"),
35            CertChain(e) => write!(f, "failed to process certificate chain: {e}"),
36            PrivKey(e) => write!(f, "failed to process private key: {e}"),
37            CertAuth(e) => write!(f, "failed to process certificate authority: {e}"),
38            Bind(e) => write!(f, "failed to bind to network interface: {e}"),
39            Config(e) => write!(f, "failed to read tls configuration: {e}"),
40        }
41    }
42}
43
44impl std::fmt::Display for KeyError {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        use KeyError::*;
47
48        match self {
49            Pem(e) => write!(f, "error reading key file: {e}"),
50            BadKeyCount(0) => write!(f, "no valid keys found. is the file malformed?"),
51            BadKeyCount(n) => write!(f, "expected exactly 1 key, found {n}"),
52            Unsupported(e) => write!(f, "key is valid but is unsupported: {e}"),
53            BadItem(i) => write!(f, "found unexpected item in key file: {i:#?}"),
54        }
55    }
56}
57
58impl std::error::Error for KeyError {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            KeyError::Pem(e) => Some(e),
62            KeyError::Unsupported(e) => Some(e),
63            _ => None,
64        }
65    }
66}
67
68impl std::error::Error for Error {
69    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70        match self {
71            Error::Io(e) => Some(e),
72            Error::Tls(e) => Some(e),
73            Error::Mtls(e) => Some(e),
74            Error::Pem(e) => Some(e),
75            Error::CertChain(e) => Some(e),
76            Error::PrivKey(e) => Some(e),
77            Error::CertAuth(e) => Some(e),
78            Error::Bind(e) => Some(&**e),
79            Error::Config(e) => Some(e),
80        }
81    }
82}
83
84impl From<std::io::Error> for Error {
85    fn from(e: std::io::Error) -> Self {
86       Error::Io(e)
87    }
88}
89
90impl From<rustls::Error> for Error {
91    fn from(e: rustls::Error) -> Self {
92        Error::Tls(e)
93    }
94}
95
96impl From<rustls::server::VerifierBuilderError> for Error {
97    fn from(value: rustls::server::VerifierBuilderError) -> Self {
98        Error::Mtls(value)
99    }
100}
101
102impl From<pem::Error> for Error {
103    fn from(e: pem::Error) -> Self {
104        Error::Pem(e)
105    }
106}
107
108impl From<KeyError> for Error {
109    fn from(value: KeyError) -> Self {
110        Error::PrivKey(value)
111    }
112}
113
114impl From<std::convert::Infallible> for Error {
115    fn from(v: std::convert::Infallible) -> Self {
116        match v {}
117    }
118}
119
120impl From<figment::Error> for Error {
121    fn from(value: figment::Error) -> Self {
122        Error::Config(value)
123    }
124}