|
| 1 | +use boring::ex_data::Index; |
| 2 | +use boring::ssl::{self, ClientHello, Ssl, SslContextBuilder}; |
| 3 | +use once_cell::sync::Lazy; |
| 4 | +use std::future::Future; |
| 5 | +use std::pin::Pin; |
| 6 | +use std::task::{ready, Context, Poll}; |
| 7 | + |
| 8 | +type BoxSelectCertFuture = Pin< |
| 9 | + Box< |
| 10 | + dyn Future<Output = Result<BoxSelectCertFinish, AsyncSelectCertError>> |
| 11 | + + Send |
| 12 | + + Sync |
| 13 | + + 'static, |
| 14 | + >, |
| 15 | +>; |
| 16 | + |
| 17 | +type BoxSelectCertFinish = |
| 18 | + Box<dyn FnOnce(ClientHello<'_>) -> Result<(), AsyncSelectCertError> + 'static>; |
| 19 | + |
| 20 | +pub(crate) static TASK_CONTEXT_INDEX: Lazy<Index<Ssl, usize>> = |
| 21 | + Lazy::new(|| Ssl::new_ex_index().unwrap()); |
| 22 | +pub(crate) static SELECT_CERT_FUTURE_INDEX: Lazy<Index<Ssl, BoxSelectCertFuture>> = |
| 23 | + Lazy::new(|| Ssl::new_ex_index().unwrap()); |
| 24 | + |
| 25 | +/// Extensions to [`SslContextBuilder`]. |
| 26 | +/// |
| 27 | +/// This trait provides additional methods to use async callbacks with boring. |
| 28 | +pub trait SslContextBuilderExt: private::Sealed { |
| 29 | + /// Sets a callback that is called before most [`ClientHello`] processing |
| 30 | + /// and before the decision whether to resume a session is made. The |
| 31 | + /// callback may inspect the [`ClientHello`] and configure the connection. |
| 32 | + /// |
| 33 | + /// This method uses a function that returns a future whose output is |
| 34 | + /// itself a closure that will be passed [`ClientHello`] to configure |
| 35 | + /// the connection based on the computations done in the future. |
| 36 | + /// |
| 37 | + /// See [`SslContextBuilder::set_select_certificate_callback`] for the sync |
| 38 | + /// setter of this callback. |
| 39 | + fn set_async_select_certificate_callback<Init, Fut, Finish>(&mut self, callback: Init) |
| 40 | + where |
| 41 | + Init: Fn(&mut ClientHello<'_>) -> Result<Fut, AsyncSelectCertError> + Send + Sync + 'static, |
| 42 | + Fut: Future<Output = Result<Finish, AsyncSelectCertError>> + Send + Sync + 'static, |
| 43 | + Finish: FnOnce(ClientHello<'_>) -> Result<(), AsyncSelectCertError> + 'static; |
| 44 | + |
| 45 | + /// Sets a callback that is called before most [`ClientHello`] processing |
| 46 | + /// and before the decision whether to resume a session is made. The |
| 47 | + /// callback may inspect the [`ClientHello`] and configure the connection. |
| 48 | + /// |
| 49 | + /// This method uses a polling function. |
| 50 | + /// |
| 51 | + /// See [`SslContextBuilder::set_select_certificate_callback`] for the sync |
| 52 | + /// setter of this callback. |
| 53 | + fn set_polling_select_certificate_callback<F>( |
| 54 | + &mut self, |
| 55 | + callback: impl Fn(ClientHello<'_>, &mut Context<'_>) -> Poll<Result<(), AsyncSelectCertError>> |
| 56 | + + Send |
| 57 | + + Sync |
| 58 | + + 'static, |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +impl SslContextBuilderExt for SslContextBuilder { |
| 63 | + fn set_async_select_certificate_callback<Init, Fut, Finish>(&mut self, callback: Init) |
| 64 | + where |
| 65 | + Init: Fn(&mut ClientHello<'_>) -> Result<Fut, AsyncSelectCertError> + Send + Sync + 'static, |
| 66 | + Fut: Future<Output = Result<Finish, AsyncSelectCertError>> + Send + Sync + 'static, |
| 67 | + Finish: FnOnce(ClientHello<'_>) -> Result<(), AsyncSelectCertError> + 'static, |
| 68 | + { |
| 69 | + self.set_select_certificate_callback(async_select_certificate_callback(callback)) |
| 70 | + } |
| 71 | + |
| 72 | + fn set_polling_select_certificate_callback<F>( |
| 73 | + &mut self, |
| 74 | + callback: impl Fn(ClientHello<'_>, &mut Context<'_>) -> Poll<Result<(), AsyncSelectCertError>> |
| 75 | + + Send |
| 76 | + + Sync |
| 77 | + + 'static, |
| 78 | + ) { |
| 79 | + self.set_select_certificate_callback(polling_select_certificate_callback(callback)); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// A fatal error to be returned from select certificate callbacks. |
| 84 | +pub struct AsyncSelectCertError; |
| 85 | + |
| 86 | +fn async_select_certificate_callback<Init, Fut, Finish>( |
| 87 | + callback: Init, |
| 88 | +) -> impl Fn(ClientHello<'_>) -> Result<(), ssl::SelectCertError> + Send + Sync + 'static |
| 89 | +where |
| 90 | + Init: Fn(&mut ClientHello<'_>) -> Result<Fut, AsyncSelectCertError> + Send + Sync + 'static, |
| 91 | + Fut: Future<Output = Result<Finish, AsyncSelectCertError>> + Send + Sync + 'static, |
| 92 | + Finish: FnOnce(ClientHello<'_>) -> Result<(), AsyncSelectCertError> + 'static, |
| 93 | +{ |
| 94 | + polling_select_certificate_callback(move |mut client_hello, cx| { |
| 95 | + let fut_result = match client_hello |
| 96 | + .ssl_mut() |
| 97 | + .ex_data_mut(*SELECT_CERT_FUTURE_INDEX) |
| 98 | + { |
| 99 | + Some(fut) => ready!(fut.as_mut().poll(cx)), |
| 100 | + None => { |
| 101 | + let fut = callback(&mut client_hello)?; |
| 102 | + let mut box_fut = |
| 103 | + Box::pin(async move { Ok(Box::new(fut.await?) as BoxSelectCertFinish) }) |
| 104 | + as BoxSelectCertFuture; |
| 105 | + |
| 106 | + match box_fut.as_mut().poll(cx) { |
| 107 | + Poll::Ready(fut_result) => fut_result, |
| 108 | + Poll::Pending => { |
| 109 | + client_hello |
| 110 | + .ssl_mut() |
| 111 | + .set_ex_data(*SELECT_CERT_FUTURE_INDEX, box_fut); |
| 112 | + |
| 113 | + return Poll::Pending; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + // NOTE(nox): For memory usage concerns, maybe we should implement |
| 120 | + // a way to remove the stored future from the `Ssl` value here? |
| 121 | + |
| 122 | + Poll::Ready(fut_result?(client_hello)) |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +fn polling_select_certificate_callback( |
| 127 | + callback: impl Fn(ClientHello<'_>, &mut Context<'_>) -> Poll<Result<(), AsyncSelectCertError>> |
| 128 | + + Send |
| 129 | + + Sync |
| 130 | + + 'static, |
| 131 | +) -> impl Fn(ClientHello<'_>) -> Result<(), ssl::SelectCertError> + Send + Sync + 'static { |
| 132 | + move |client_hello| { |
| 133 | + let cx = unsafe { |
| 134 | + &mut *(*client_hello |
| 135 | + .ssl() |
| 136 | + .ex_data(*TASK_CONTEXT_INDEX) |
| 137 | + .expect("task context should be set") as *mut Context<'_>) |
| 138 | + }; |
| 139 | + |
| 140 | + match callback(client_hello, cx) { |
| 141 | + Poll::Ready(Ok(())) => Ok(()), |
| 142 | + Poll::Ready(Err(AsyncSelectCertError)) => Err(ssl::SelectCertError::ERROR), |
| 143 | + Poll::Pending => Err(ssl::SelectCertError::RETRY), |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +mod private { |
| 149 | + pub trait Sealed {} |
| 150 | +} |
| 151 | + |
| 152 | +impl private::Sealed for SslContextBuilder {} |
0 commit comments