tlspuffin/tls/rustls/
versions.rs

1use crate::tls::rustls::msgs::enums::ProtocolVersion;
2
3/// A TLS protocol version supported by rustls.
4///
5/// All possible instances of this class are provided by the library in
6/// the [`ALL_VERSIONS`] array, as well as individually as [`TLS12`]
7/// and [`TLS13`].
8#[derive(Debug, PartialEq)]
9#[non_exhaustive]
10pub struct SupportedProtocolVersion {
11    /// The TLS enumeration naming this version.
12    pub version: ProtocolVersion,
13}
14
15/// TLS1.2
16pub static TLS12: SupportedProtocolVersion = SupportedProtocolVersion {
17    version: ProtocolVersion::TLSv1_2,
18};
19
20/// TLS1.3
21pub static TLS13: SupportedProtocolVersion = SupportedProtocolVersion {
22    version: ProtocolVersion::TLSv1_3,
23};
24
25/// A list of all the protocol versions supported by rustls.
26pub static ALL_VERSIONS: &[&SupportedProtocolVersion] = &[&TLS13, &TLS12];
27
28/// The version configuration that an application should use by default.
29///
30/// This will be [`ALL_VERSIONS`] for now, but gives space in the future
31/// to remove a version from here and require users to opt-in to older
32/// versions.
33pub static DEFAULT_VERSIONS: &[&SupportedProtocolVersion] = ALL_VERSIONS;
34
35#[derive(Debug, Clone)]
36pub struct EnabledVersions {
37    tls12: Option<&'static SupportedProtocolVersion>,
38    tls13: Option<&'static SupportedProtocolVersion>,
39}
40
41impl EnabledVersions {
42    pub fn new(versions: &[&'static SupportedProtocolVersion]) -> Self {
43        let mut ev = Self {
44            tls12: None,
45            tls13: None,
46        };
47
48        for v in versions {
49            match v.version {
50                ProtocolVersion::TLSv1_2 => ev.tls12 = Some(v),
51                ProtocolVersion::TLSv1_3 => ev.tls13 = Some(v),
52                _ => {}
53            }
54        }
55
56        ev
57    }
58
59    pub fn contains(&self, version: ProtocolVersion) -> bool {
60        match version {
61            ProtocolVersion::TLSv1_2 => self.tls12.is_some(),
62            ProtocolVersion::TLSv1_3 => self.tls13.is_some(),
63            _ => false,
64        }
65    }
66}