Skip to content

Commit 3921f35

Browse files
vyniouskornelski
authored andcommitted
Expose DTLS version constants in SslVersion
1 parent ac34693 commit 3921f35

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

boring/src/ssl/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl From<u16> for ExtensionType {
614614
}
615615
}
616616

617-
/// An SSL/TLS protocol version.
617+
/// An SSL/TLS/DTLS protocol version.
618618
#[derive(Copy, Clone, PartialEq, Eq)]
619619
pub struct SslVersion(u16);
620620

@@ -633,6 +633,15 @@ impl SslVersion {
633633

634634
/// TLSv1.3
635635
pub const TLS1_3: SslVersion = SslVersion(ffi::TLS1_3_VERSION as _);
636+
637+
/// DTLSv1.0
638+
pub const DTLS1: SslVersion = SslVersion(ffi::DTLS1_VERSION as _);
639+
640+
/// DTLSv1.2
641+
pub const DTLS1_2: SslVersion = SslVersion(ffi::DTLS1_2_VERSION as _);
642+
643+
/// DTLSv1.3
644+
pub const DTLS1_3: SslVersion = SslVersion(ffi::DTLS1_3_VERSION as _);
636645
}
637646

638647
impl TryFrom<u16> for SslVersion {
@@ -644,7 +653,10 @@ impl TryFrom<u16> for SslVersion {
644653
| ffi::TLS1_VERSION
645654
| ffi::TLS1_1_VERSION
646655
| ffi::TLS1_2_VERSION
647-
| ffi::TLS1_3_VERSION => Ok(Self(value)),
656+
| ffi::TLS1_3_VERSION
657+
| ffi::DTLS1_VERSION
658+
| ffi::DTLS1_2_VERSION
659+
| ffi::DTLS1_3_VERSION => Ok(Self(value)),
648660
_ => Err("Unknown SslVersion"),
649661
}
650662
}
@@ -658,6 +670,9 @@ impl fmt::Debug for SslVersion {
658670
Self::TLS1_1 => "TLS1_1",
659671
Self::TLS1_2 => "TLS1_2",
660672
Self::TLS1_3 => "TLS1_3",
673+
Self::DTLS1 => "DTLS1",
674+
Self::DTLS1_2 => "DTLS1_2",
675+
Self::DTLS1_3 => "DTLS1_3",
661676
_ => return write!(f, "{:#06x}", self.0),
662677
})
663678
}
@@ -671,6 +686,9 @@ impl fmt::Display for SslVersion {
671686
Self::TLS1_1 => "TLSv1.1",
672687
Self::TLS1_2 => "TLSv1.2",
673688
Self::TLS1_3 => "TLSv1.3",
689+
Self::DTLS1 => "DTLSv1.0",
690+
Self::DTLS1_2 => "DTLSv1.2",
691+
Self::DTLS1_3 => "DTLSv1.3",
674692
_ => return write!(f, "unknown ({:#06x})", self.0),
675693
})
676694
}

boring/src/ssl/test/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,53 @@ fn test_connect_with_srtp_ssl() {
233233
assert_eq!(buf[..], buf2[..]);
234234
}
235235

236+
/// Tests that DTLS 1.3 can be enabled and negotiated successfully.
237+
#[test]
238+
fn test_dtls_1_3_version() {
239+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
240+
let addr = listener.local_addr().unwrap();
241+
242+
let guard = thread::spawn(move || {
243+
let stream = listener.accept().unwrap().0;
244+
let mut ctx = SslContext::builder(SslMethod::dtls()).unwrap();
245+
ctx.set_certificate_file(Path::new("test/cert.pem"), SslFiletype::PEM)
246+
.unwrap();
247+
ctx.set_private_key_file(Path::new("test/key.pem"), SslFiletype::PEM)
248+
.unwrap();
249+
// Enable DTLS 1.3
250+
ctx.set_max_proto_version(Some(SslVersion::DTLS1_3))
251+
.unwrap();
252+
let mut ssl = Ssl::new(&ctx.build()).unwrap();
253+
ssl.set_mtu(1500).unwrap();
254+
let stream = ssl.accept(stream).unwrap();
255+
256+
// Verify DTLS 1.3 was negotiated
257+
let version = stream.ssl().version2().unwrap();
258+
assert_eq!(version, SslVersion::DTLS1_3);
259+
260+
stream
261+
});
262+
263+
let stream = TcpStream::connect(addr).unwrap();
264+
let mut ctx = SslContext::builder(SslMethod::dtls()).unwrap();
265+
// Enable DTLS 1.3 on client
266+
ctx.set_max_proto_version(Some(SslVersion::DTLS1_3))
267+
.unwrap();
268+
let mut ssl = Ssl::new(&ctx.build()).unwrap();
269+
ssl.set_mtu(1500).unwrap();
270+
let stream = ssl.connect(stream).unwrap();
271+
272+
// Verify DTLS 1.3 was negotiated on client side
273+
let version = stream.ssl().version2().unwrap();
274+
assert_eq!(version, SslVersion::DTLS1_3);
275+
276+
// Also check version string
277+
let version_str = stream.ssl().version_str();
278+
assert_eq!(version_str, "DTLSv1.3");
279+
280+
guard.join().unwrap();
281+
}
282+
236283
/// Tests that when the `SslStream` is created as a server stream, the protocols
237284
/// are correctly advertised to the client.
238285
#[test]

0 commit comments

Comments
 (0)