| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![expect( |
| 8 |
clippy::unwrap_used, |
| 9 |
reason = "Let's assume the use of `unwrap` was checked when the use of `unsafe` was reviewed." |
| 10 |
)] |
| 11 |
|
| 12 |
use std::{ |
| 13 |
cell::RefCell, |
| 14 |
fmt::{self, Debug, Formatter}, |
| 15 |
os::raw::{c_uint, c_void}, |
| 16 |
pin::Pin, |
| 17 |
rc::Rc, |
| 18 |
}; |
| 19 |
|
| 20 |
use crate::{ |
| 21 |
agentio::as_c_void, |
| 22 |
constants::{Extension, HandshakeMessage, TLS_HS_CLIENT_HELLO, TLS_HS_ENCRYPTED_EXTENSIONS}, |
| 23 |
err::Res, |
| 24 |
null_safe_slice, |
| 25 |
ssl::{ |
| 26 |
PRBool, PRFileDesc, SECFailure, SECStatus, SECSuccess, SSLAlertDescription, |
| 27 |
SSLExtensionHandler, SSLExtensionWriter, SSLHandshakeType, |
| 28 |
}, |
| 29 |
}; |
| 30 |
|
| 31 |
experimental_api!(SSL_InstallExtensionHooks( |
| 32 |
fd: *mut PRFileDesc, |
| 33 |
extension: u16, |
| 34 |
writer: SSLExtensionWriter, |
| 35 |
writer_arg: *mut c_void, |
| 36 |
handler: SSLExtensionHandler, |
| 37 |
handler_arg: *mut c_void, |
| 38 |
)); |
| 39 |
|
| 40 |
pub enum ExtensionWriterResult { |
| 41 |
Write(usize), |
| 42 |
Skip, |
| 43 |
} |
| 44 |
|
| 45 |
pub enum ExtensionHandlerResult { |
| 46 |
Ok, |
| 47 |
Alert(crate::constants::Alert), |
| 48 |
} |
| 49 |
|
| 50 |
pub trait ExtensionHandler { |
| 51 |
fn write(&mut self, msg: HandshakeMessage, _d: &mut [u8]) -> ExtensionWriterResult { |
| 52 |
match msg { |
| 53 |
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => ExtensionWriterResult::Write(0), |
| 54 |
_ => ExtensionWriterResult::Skip, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
fn handle(&mut self, msg: HandshakeMessage, _d: &[u8]) -> ExtensionHandlerResult { |
| 59 |
match msg { |
| 60 |
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => ExtensionHandlerResult::Ok, |
| 61 |
_ => ExtensionHandlerResult::Alert(110), // unsupported_extension |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
type BoxedExtensionHandler = Box<Rc<RefCell<dyn ExtensionHandler>>>; |
| 67 |
|
| 68 |
pub struct ExtensionTracker { |
| 69 |
extension: Extension, |
| 70 |
handler: Pin<Box<BoxedExtensionHandler>>, |
| 71 |
} |
| 72 |
|
| 73 |
impl ExtensionTracker { |
| 74 |
// Technically the as_mut() call here is the only unsafe bit, |
| 75 |
// but don't call this function lightly. |
| 76 |
unsafe fn wrap_handler_call<F, T>(arg: *mut c_void, f: F) -> T |
| 77 |
where |
| 78 |
F: FnOnce(&mut dyn ExtensionHandler) -> T, |
| 79 |
{ |
| 80 |
let rc = arg.cast::<BoxedExtensionHandler>().as_mut().unwrap(); |
| 81 |
f(&mut *rc.borrow_mut()) |
| 82 |
} |
| 83 |
|
| 84 |
unsafe extern "C" fn extension_writer( |
| 85 |
_fd: *mut PRFileDesc, |
| 86 |
message: SSLHandshakeType::Type, |
| 87 |
data: *mut u8, |
| 88 |
len: *mut c_uint, |
| 89 |
max_len: c_uint, |
| 90 |
arg: *mut c_void, |
| 91 |
) -> PRBool { |
| 92 |
let d = std::slice::from_raw_parts_mut(data, max_len as usize); |
| 93 |
Self::wrap_handler_call(arg, |handler| { |
| 94 |
#[allow( |
| 95 |
clippy::allow_attributes, |
| 96 |
clippy::cast_possible_truncation, |
| 97 |
clippy::cast_sign_loss, |
| 98 |
reason = "Cast is safe here because the message type is always part of the enum." |
| 99 |
)] |
| 100 |
match handler.write(message as HandshakeMessage, d) { |
| 101 |
ExtensionWriterResult::Write(sz) => { |
| 102 |
*len = c_uint::try_from(sz).expect("integer overflow from extension writer"); |
| 103 |
1 |
| 104 |
} |
| 105 |
ExtensionWriterResult::Skip => 0, |
| 106 |
} |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
unsafe extern "C" fn extension_handler( |
| 111 |
_fd: *mut PRFileDesc, |
| 112 |
message: SSLHandshakeType::Type, |
| 113 |
data: *const u8, |
| 114 |
len: c_uint, |
| 115 |
alert: *mut SSLAlertDescription, |
| 116 |
arg: *mut c_void, |
| 117 |
) -> SECStatus { |
| 118 |
let d = null_safe_slice(data, len); |
| 119 |
Self::wrap_handler_call(arg, |handler| { |
| 120 |
// Cast is safe here because the message type is always part of the enum |
| 121 |
#[allow( |
| 122 |
clippy::allow_attributes, |
| 123 |
clippy::cast_possible_truncation, |
| 124 |
clippy::cast_sign_loss, |
| 125 |
reason = "Cast is safe here because the message type is always part of the enum." |
| 126 |
)] |
| 127 |
match handler.handle(message as HandshakeMessage, d) { |
| 128 |
ExtensionHandlerResult::Ok => SECSuccess, |
| 129 |
ExtensionHandlerResult::Alert(a) => { |
| 130 |
*alert = a; |
| 131 |
SECFailure |
| 132 |
} |
| 133 |
} |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
/// Use the provided handler to manage an extension. This is quite unsafe. |
| 138 |
/// |
| 139 |
/// # Safety |
| 140 |
/// |
| 141 |
/// The holder of this `ExtensionTracker` needs to ensure that it lives at |
| 142 |
/// least as long as the file descriptor, as NSS provides no way to remove |
| 143 |
/// an extension handler once it is configured. |
| 144 |
/// |
| 145 |
/// # Errors |
| 146 |
/// |
| 147 |
/// If the underlying NSS API fails to register a handler. |
| 148 |
pub unsafe fn new( |
| 149 |
fd: *mut PRFileDesc, |
| 150 |
extension: Extension, |
| 151 |
handler: Rc<RefCell<dyn ExtensionHandler>>, |
| 152 |
) -> Res<Self> { |
| 153 |
// The ergonomics here aren't great for users of this API, but it's |
| 154 |
// horrific here. The pinned outer box gives us a stable pointer to the inner |
| 155 |
// box. This is the pointer that is passed to NSS. |
| 156 |
// |
| 157 |
// The inner box points to the reference-counted object. This inner box is |
| 158 |
// what we end up with a reference to in callbacks. That extra wrapper around |
| 159 |
// the Rc avoid any touching of reference counts in callbacks, which would |
| 160 |
// inevitably lead to leaks as we don't control how many times the callback |
| 161 |
// is invoked. |
| 162 |
// |
| 163 |
// This way, only this "outer" code deals with the reference count. |
| 164 |
let mut tracker = Self { |
| 165 |
extension, |
| 166 |
handler: Box::pin(Box::new(handler)), |
| 167 |
}; |
| 168 |
SSL_InstallExtensionHooks( |
| 169 |
fd, |
| 170 |
extension, |
| 171 |
Some(Self::extension_writer), |
| 172 |
as_c_void(&mut tracker.handler), |
| 173 |
Some(Self::extension_handler), |
| 174 |
as_c_void(&mut tracker.handler), |
| 175 |
)?; |
| 176 |
Ok(tracker) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl Debug for ExtensionTracker { |
| 181 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 182 |
write!(f, "ExtensionTracker: {:?}", self.extension) |
| 183 |
} |
| 184 |
} |