Skip to main content

bornera_rustls/
diagnostic.rs

1//! Stable provider codes for bounded rustls failure projections.
2
3use std::io;
4
5use bornera::{TransportDiagnostic, TransportError, TransportFailureKind, TransportFailurePhase};
6
7/// Stable rustls-specific code retained in a Bornera transport diagnostic.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9#[non_exhaustive]
10#[repr(u32)]
11pub enum RustlsDiagnostic {
12    /// TLS framing, cryptography, alerts, or negotiation failed.
13    Protocol = 1,
14    /// Peer certificate validation failed.
15    Certificate = 2,
16    /// The verifier rejected the logical server-name type.
17    ServerName = 3,
18    /// Raw transport EOF arrived without authenticated TLS closure.
19    Truncated = 4,
20    /// Rustls logical input or output exceeded its configured bound.
21    Capacity = 5,
22    /// The caller requested an operation outside the session lifecycle.
23    CallerState = 6,
24}
25
26impl RustlsDiagnostic {
27    /// Returns the stable provider code stored by Bornera.
28    pub const fn code(self) -> u32 {
29        self as u32
30    }
31
32    /// Interprets a stored provider code produced by this crate.
33    pub const fn from_code(code: u32) -> Option<Self> {
34        match code {
35            1 => Some(Self::Protocol),
36            2 => Some(Self::Certificate),
37            3 => Some(Self::ServerName),
38            4 => Some(Self::Truncated),
39            5 => Some(Self::Capacity),
40            6 => Some(Self::CallerState),
41            _ => None,
42        }
43    }
44}
45
46pub(crate) fn protocol_error(
47    phase: TransportFailurePhase,
48    error: &rustls::Error,
49) -> TransportError {
50    let (failure, code) = match error {
51        rustls::Error::InvalidCertificate(
52            rustls::CertificateError::NotValidForName
53            | rustls::CertificateError::NotValidForNameContext { .. },
54        )
55        | rustls::Error::UnsupportedNameType => (
56            TransportFailureKind::ServerName,
57            RustlsDiagnostic::ServerName,
58        ),
59        rustls::Error::InvalidCertificate(_)
60        | rustls::Error::InvalidCertRevocationList(_)
61        | rustls::Error::NoCertificatesPresented => (
62            TransportFailureKind::Certificate,
63            RustlsDiagnostic::Certificate,
64        ),
65        _ => (TransportFailureKind::Protocol, RustlsDiagnostic::Protocol),
66    };
67    TransportError::new(TransportDiagnostic::new(
68        phase,
69        failure,
70        io::ErrorKind::InvalidData,
71        Some(code.code()),
72    ))
73}
74
75pub(crate) fn truncated_error(phase: TransportFailurePhase) -> TransportError {
76    TransportError::new(TransportDiagnostic::new(
77        phase,
78        TransportFailureKind::Truncated,
79        io::ErrorKind::UnexpectedEof,
80        Some(RustlsDiagnostic::Truncated.code()),
81    ))
82}
83
84pub(crate) fn caller_state_error(
85    phase: TransportFailurePhase,
86    kind: io::ErrorKind,
87) -> TransportError {
88    TransportError::new(TransportDiagnostic::new(
89        phase,
90        TransportFailureKind::Contract,
91        kind,
92        Some(RustlsDiagnostic::CallerState.code()),
93    ))
94}