diff --git a/openssl-sys/src/bio.rs b/openssl-sys/src/bio.rs index e2bfbc0c41f70222d04785fadf69a385077a7222..bf9006a89ac639b67e0ec2816430ddc44371faa4 100644 --- a/openssl-sys/src/bio.rs +++ b/openssl-sys/src/bio.rs @@ -7,6 +7,7 @@ pub const BIO_TYPE_NONE: c_int = 0; pub const BIO_CTRL_EOF: c_int = 2; pub const BIO_CTRL_INFO: c_int = 3; pub const BIO_CTRL_FLUSH: c_int = 11; +pub const BIO_CTRL_DGRAM_QUERY_MTU: c_int = 40; pub const BIO_C_SET_BUF_MEM_EOF_RETURN: c_int = 130; extern "C" { diff --git a/openssl/src/ssl/bio.rs b/openssl/src/ssl/bio.rs index d33db296f4a933e21b0738e7f59c3832abc86960..6bd81ffe371750bdbdfe0939544e159675b3ee6c 100644 --- a/openssl/src/ssl/bio.rs +++ b/openssl/src/ssl/bio.rs @@ -1,6 +1,6 @@ use ffi::{ self, BIO_clear_retry_flags, BIO_new, BIO_set_retry_read, BIO_set_retry_write, BIO, - BIO_CTRL_FLUSH, + BIO_CTRL_FLUSH, BIO_CTRL_DGRAM_QUERY_MTU, }; use libc::{c_char, c_int, c_long, c_void, strlen}; use std::any::Any; @@ -18,6 +18,7 @@ pub struct StreamState { pub stream: S, pub error: Option, pub panic: Option>, + pub dtls_mtu_size: c_long, } /// Safe wrapper for BIO_METHOD @@ -39,6 +40,7 @@ pub fn new(stream: S) -> Result<(*mut BIO, BioMethod), ErrorSta stream: stream, error: None, panic: None, + dtls_mtu_size: 0, }); unsafe { @@ -69,6 +71,13 @@ pub unsafe fn get_mut<'a, S: 'a>(bio: *mut BIO) -> &'a mut S { &mut state(bio).stream } +pub unsafe fn set_dtls_mtu_size(bio: *mut BIO, mtu_size: usize) { + if mtu_size as u64 > c_long::max_value() as u64 { + panic!("Given MTU size {} can't be represented in a positive `c_long` range") + } + state::(bio).dtls_mtu_size = mtu_size as c_long; +} + unsafe fn state<'a, S: 'a>(bio: *mut BIO) -> &'a mut StreamState { &mut *(BIO_get_data(bio) as *mut _) } @@ -134,9 +143,9 @@ unsafe extern "C" fn ctrl( _num: c_long, _ptr: *mut c_void, ) -> c_long { - if cmd == BIO_CTRL_FLUSH { - let state = state::(bio); + let state = state::(bio); + if cmd == BIO_CTRL_FLUSH { match catch_unwind(AssertUnwindSafe(|| state.stream.flush())) { Ok(Ok(())) => 1, Ok(Err(err)) => { @@ -148,6 +157,8 @@ unsafe extern "C" fn ctrl( 0 } } + } else if cmd == BIO_CTRL_DGRAM_QUERY_MTU { + state.dtls_mtu_size } else { 0 } diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index e09e6f3d125925834bd26ac60885597b50cfed14..314b734c866fbc5bb703f4e430f0b4c75c7f8740 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -3804,6 +3804,20 @@ impl SslStreamBuilder { pub fn ssl(&self) -> &SslRef { &self.inner.ssl } + + /// Set the DTLS MTU size. + /// + /// It will be ignored if the value is smaller than the minimum packet size + /// the DTLS protocol requires. + /// + /// # Panics + /// This function panics if the given mtu size can't be represented in a positive `c_long` range + pub fn set_dtls_mtu_size(&mut self, mtu_size: usize) { + unsafe { + let bio = self.inner.ssl.get_raw_rbio(); + bio::set_dtls_mtu_size::(bio, mtu_size); + } + } } /// The result of a shutdown request. diff --git a/openssl/src/ssl/test/mod.rs b/openssl/src/ssl/test/mod.rs index ebc1ae0349031c8fb3cb035eb3f969c7fabc9855..a7d2780c96e686cdebf537aac87b024092f7b39f 100644 --- a/openssl/src/ssl/test/mod.rs +++ b/openssl/src/ssl/test/mod.rs @@ -31,7 +31,7 @@ use ssl::{ClientHelloResponse, ExtensionContext}; use ssl::{ Error, HandshakeError, MidHandshakeSslStream, ShutdownResult, ShutdownState, Ssl, SslAcceptor, SslAcceptorBuilder, SslConnector, SslContext, SslContextBuilder, SslFiletype, SslMethod, - SslOptions, SslSessionCacheMode, SslStream, SslVerifyMode, StatusType, + SslOptions, SslSessionCacheMode, SslStream, SslStreamBuilder, SslVerifyMode, StatusType, }; #[cfg(ossl102)] use x509::store::X509StoreBuilder; @@ -322,7 +322,9 @@ fn test_connect_with_srtp_ctx() { ctx.set_private_key_file(&Path::new("test/key.pem"), SslFiletype::PEM) .unwrap(); let ssl = Ssl::new(&ctx.build()).unwrap(); - let mut stream = ssl.accept(stream).unwrap(); + let mut builder = SslStreamBuilder::new(ssl, stream); + builder.set_dtls_mtu_size(1500); + let mut stream = builder.accept().unwrap(); let mut buf = [0; 60]; stream @@ -340,7 +342,9 @@ fn test_connect_with_srtp_ctx() { ctx.set_tlsext_use_srtp("SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32") .unwrap(); let ssl = Ssl::new(&ctx.build()).unwrap(); - let mut stream = ssl.connect(stream).unwrap(); + let mut builder = SslStreamBuilder::new(ssl, stream); + builder.set_dtls_mtu_size(1500); + let mut stream = builder.connect().unwrap(); let mut buf = [1; 60]; { @@ -390,7 +394,9 @@ fn test_connect_with_srtp_ssl() { "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32", profilenames ); - let mut stream = ssl.accept(stream).unwrap(); + let mut builder = SslStreamBuilder::new(ssl, stream); + builder.set_dtls_mtu_size(1500); + let mut stream = builder.accept().unwrap(); let mut buf = [0; 60]; stream @@ -408,7 +414,9 @@ fn test_connect_with_srtp_ssl() { let mut ssl = Ssl::new(&ctx.build()).unwrap(); ssl.set_tlsext_use_srtp("SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32") .unwrap(); - let mut stream = ssl.connect(stream).unwrap(); + let mut builder = SslStreamBuilder::new(ssl, stream); + builder.set_dtls_mtu_size(1500); + let mut stream = builder.connect().unwrap(); let mut buf = [1; 60]; {