rocket/tls/
error.rs

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