tokio_rboring/
async_callbacks.rs

1use boring::ssl::{
2    AsyncPrivateKeyMethod, AsyncSelectCertError, BoxGetSessionFuture, BoxSelectCertFuture,
3    ClientHello, SslContextBuilder, SslRef,
4};
5
6/// Extensions to [`SslContextBuilder`].
7///
8/// This trait provides additional methods to use async callbacks with boring.
9pub trait SslContextBuilderExt: private::Sealed {
10    /// Sets a callback that is called before most [`ClientHello`] processing
11    /// and before the decision whether to resume a session is made. The
12    /// callback may inspect the [`ClientHello`] and configure the connection.
13    ///
14    /// This method uses a function that returns a future whose output is
15    /// itself a closure that will be passed [`ClientHello`] to configure
16    /// the connection based on the computations done in the future.
17    ///
18    /// See [`SslContextBuilder::set_select_certificate_callback`] for the sync
19    /// setter of this callback.
20    fn set_async_select_certificate_callback<F>(&mut self, callback: F)
21    where
22        F: Fn(&mut ClientHello<'_>) -> Result<BoxSelectCertFuture, AsyncSelectCertError>
23            + Send
24            + Sync
25            + 'static;
26
27    /// Configures a custom private key method on the context.
28    ///
29    /// See [`AsyncPrivateKeyMethod`] for more details.
30    fn set_async_private_key_method(&mut self, method: impl AsyncPrivateKeyMethod);
31
32    /// Sets a callback that is called when a client proposed to resume a session
33    /// but it was not found in the internal cache.
34    ///
35    /// The callback is passed a reference to the session ID provided by the client.
36    /// It should return the session corresponding to that ID if available. This is
37    /// only used for servers, not clients.
38    ///
39    /// See [`SslContextBuilder::set_get_session_callback`] for the sync setter
40    /// of this callback.
41    ///
42    /// # Safety
43    ///
44    /// The returned [`SslSession`] must not be associated with a different [`SslContext`].
45    unsafe fn set_async_get_session_callback<F>(&mut self, callback: F)
46    where
47        F: Fn(&mut SslRef, &[u8]) -> Option<BoxGetSessionFuture> + Send + Sync + 'static;
48}
49
50impl SslContextBuilderExt for SslContextBuilder {
51    fn set_async_select_certificate_callback<F>(&mut self, callback: F)
52    where
53        F: Fn(&mut ClientHello<'_>) -> Result<BoxSelectCertFuture, AsyncSelectCertError>
54            + Send
55            + Sync
56            + 'static,
57    {
58        self.set_async_select_certificate_callback(callback);
59    }
60
61    fn set_async_private_key_method(&mut self, method: impl AsyncPrivateKeyMethod) {
62        self.set_async_private_key_method(method);
63    }
64
65    unsafe fn set_async_get_session_callback<F>(&mut self, callback: F)
66    where
67        F: Fn(&mut SslRef, &[u8]) -> Option<BoxGetSessionFuture> + Send + Sync + 'static,
68    {
69        self.set_async_get_session_callback(callback);
70    }
71}
72
73mod private {
74    pub trait Sealed {}
75}
76
77impl private::Sealed for SslContextBuilder {}