ytls_extensions/
supported_versions.rs

1//! yTLS Extension (13) Signature Algorithms
2
3use crate::TlsExtError;
4use ytls_typed::Version;
5
6/// Downstream Supported Versions Processor
7pub trait ExtVersionProcessor {
8    ///
9    fn supported_version(&mut self, _: Version) -> bool;
10}
11
12/// TLS Extension 43 Supported Verison handling
13pub struct TlsExtVersion {}
14
15impl TlsExtVersion {
16    /// Client Hello supported versions callback
17    #[inline]
18    pub fn client_supported_version_cb<P: ExtVersionProcessor>(
19        p: &mut P,
20        versions_raw: &[u8],
21    ) -> Result<(), TlsExtError> {
22        if versions_raw.len() < 1 {
23            return Err(TlsExtError::InvalidLength);
24        }
25
26        let versions_len = versions_raw[0];
27
28        if versions_len == 0 {
29            return Err(TlsExtError::NoData);
30        }
31
32        if versions_raw.len() < 2 {
33            return Err(TlsExtError::InvalidLength);
34        }
35
36        let remaining = &versions_raw[1..];
37        let expected_len = remaining.len();
38
39        if expected_len != versions_len as usize {
40            return Err(TlsExtError::InvalidLength);
41        }
42
43        let mut versions_i = remaining.chunks(2);
44
45        while let Some(version_raw) = versions_i.next() {
46            let version = u16::from_be_bytes([version_raw[0], version_raw[1]]);
47            p.supported_version(version.into());
48        }
49        Ok(())
50    }
51}
52
53// TODO: Test
54// 04 03 04 03 03 -> Tls13, Tls12