Skip to main content

coqui_peer_tls/
source.rs

1//! The addresses a handshake came from and arrived at, made visible to the verifiers.
2//!
3//! rustls shares one verifier between every connection of a `ServerConfig` and does not
4//! expose it, so the verifier cannot be given a per-connection value. Its callbacks run
5//! synchronously inside the handshake call that the accept helpers make, on the calling
6//! thread, so the helpers scope the addresses around exactly those calls.
7
8use core::cell::Cell;
9use core::net::SocketAddr;
10
11use crate::error::Refusal;
12
13/// What the calls on this thread are scoped to.
14#[derive(Clone, Copy, Default)]
15struct Scope {
16    source: Option<SocketAddr>,
17    #[cfg_attr(
18        not(feature = "acl"),
19        expect(dead_code, reason = "only the access control list reads it")
20    )]
21    local: Option<SocketAddr>,
22    /// The refusal of a check that cannot carry one in its own error (the certificate resolver).
23    refused: Option<Refusal>,
24}
25
26thread_local! {
27    static CURRENT: Cell<Scope> = const {
28        Cell::new(Scope {
29            source: None,
30            local: None,
31            refused: None,
32        })
33    };
34}
35
36struct Restore(Scope);
37
38impl Drop for Restore {
39    fn drop(&mut self) {
40        CURRENT.with(|current| current.set(self.0));
41    }
42}
43
44/// Runs `work` with the connection's addresses in scope: `source` is where the verifiers record
45/// waiting entries and what a policy's access control list decides by, and `local` the address
46/// the connection arrived at, which the list needs for rules that name a destination or a port.
47///
48/// The accept helpers call this around the handshake; a host that drives rustls itself wraps
49/// each call that can reach a verifier (for example `process_new_packets`). Outside it, entries
50/// are recorded without a source.
51pub fn with_addresses<R>(
52    source: Option<SocketAddr>,
53    local: Option<SocketAddr>,
54    work: impl FnOnce() -> R,
55) -> R {
56    scoped(source, local, work).0
57}
58
59/// Runs `work` in a scope of its own, and returns what it returned with the refusal a check
60/// inside it recorded.
61pub(crate) fn scoped<R>(
62    source: Option<SocketAddr>,
63    local: Option<SocketAddr>,
64    work: impl FnOnce() -> R,
65) -> (R, Option<Refusal>) {
66    let _restore = Restore(CURRENT.with(|current| {
67        current.replace(Scope {
68            source,
69            local,
70            refused: None,
71        })
72    }));
73    let result = work();
74    (result, CURRENT.with(Cell::get).refused)
75}
76
77/// The source of the current scope.
78pub(crate) fn current() -> Option<SocketAddr> {
79    CURRENT.with(Cell::get).source
80}
81
82/// The local address of the current scope.
83#[cfg(feature = "acl")]
84pub(crate) fn local() -> Option<SocketAddr> {
85    CURRENT.with(Cell::get).local
86}
87
88/// Records `refusal` in the current scope, for the accept helpers to report.
89#[cfg(feature = "acl")]
90pub(crate) fn refuse(refusal: Refusal) {
91    CURRENT.with(|current| {
92        let mut scope = current.get();
93        scope.refused.get_or_insert(refusal);
94        current.set(scope);
95    });
96}