Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse SOL_TLS control message, closes #2064 #2065

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,9 @@ 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`
TlsGetRecordType(TlsGetRecordType),

/// Catch-all variant for unimplemented cmsg types.
#[doc(hidden)]
Unknown(UnknownCmsg),
Expand All @@ -885,6 +888,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 = "android", target_os = "linux"))]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
#[non_exhaustive]
pub enum TlsGetRecordType {
Copy link
Contributor

@Arnavion Arnavion Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: nvm, I confused the content type with the handshake type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, re: your original comment:

That said, and I know @asomers asked for it, but I don't think this belongs in the nix crate because it's about TLS, not libc or Linux.

I agree with this, but I'm willing to do whatever to get this PR merged at this point. I agree the API boundaries are kinda unclear right now, after all libc itself is a grab bag 🤷

ChangeCipherSpec ,
Alert,
Handshake,
ApplicationData,
Unknown(u8),
}

#[cfg(any(target_os = "android", 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 @@ -1027,6 +1057,11 @@ impl ControlMessageOwned {
let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6);
ControlMessageOwned::Ipv6OrigDstAddr(dl)
},
#[cfg(any(target_os = "android", 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