Skip to content

Commit

Permalink
Parse SOL_TLS control message, closes #2064 (#2065)
Browse files Browse the repository at this point in the history
* Parse SOL_TLS control message, closes #2064

* Only Linux gets SOL_TLS I guess (this is wrong, BSDs have it too)

* Use libc constants

* Also parse SOL_TLS on Android

* Decode TLS record types

* Only have TlsGetRecordType enum variant on supported platforms

* Remove android from target platforms

...since the corresponding libc constant isn't gated for Android.

* Add changelog entry
  • Loading branch information
fasterthanlime authored Nov 10, 2023
1 parent 49283c9 commit b9ff39e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ targets = [
]

[dependencies]
libc = { version = "0.2.147", features = ["extra_traits"] }
libc = { version = "0.2.148", features = ["extra_traits"] }
bitflags = "2.3.1"
cfg-if = "1.0"
pin-utils = { version = "0.1.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions changelog/2065.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `TlsGetRecordType` control message type and corresponding enum for linux
36 changes: 36 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ pub enum ControlMessageOwned {
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Ipv6RecvErr(libc::sock_extended_err, Option<sockaddr_in6>),

/// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE`
#[cfg(any(target_os = "linux"))]
TlsGetRecordType(TlsGetRecordType),

/// Catch-all variant for unimplemented cmsg types.
#[doc(hidden)]
Unknown(UnknownCmsg),
Expand All @@ -880,6 +884,33 @@ pub struct Timestamps {
pub hw_raw: TimeSpec,
}

/// These constants correspond to TLS 1.2 message types, as defined in
/// RFC 5246, Appendix A.1
#[cfg(any(target_os = "linux"))]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
#[non_exhaustive]
pub enum TlsGetRecordType {
ChangeCipherSpec ,
Alert,
Handshake,
ApplicationData,
Unknown(u8),
}

#[cfg(any(target_os = "linux"))]
impl From<u8> for TlsGetRecordType {
fn from(x: u8) -> Self {
match x {
20 => TlsGetRecordType::ChangeCipherSpec,
21 => TlsGetRecordType::Alert,
22 => TlsGetRecordType::Handshake,
23 => TlsGetRecordType::ApplicationData,
_ => TlsGetRecordType::Unknown(x),
}
}
}

impl ControlMessageOwned {
/// Decodes a `ControlMessageOwned` from raw bytes.
///
Expand Down Expand Up @@ -1018,6 +1049,11 @@ impl ControlMessageOwned {
let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6);
ControlMessageOwned::Ipv6OrigDstAddr(dl)
},
#[cfg(any(target_os = "linux"))]
(libc::SOL_TLS, libc::TLS_GET_RECORD_TYPE) => {
let content_type = ptr::read_unaligned(p as *const u8);
ControlMessageOwned::TlsGetRecordType(content_type.into())
},
(_, _) => {
let sl = std::slice::from_raw_parts(p, len);
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(sl));
Expand Down

0 comments on commit b9ff39e

Please sign in to comment.