rustls/
versions.rs

1use core::fmt;
2
3use crate::enums::ProtocolVersion;
4
5/// A TLS protocol version supported by rustls.
6///
7/// All possible instances of this class are provided by the library in
8/// the [`ALL_VERSIONS`] array, as well as individually as [`TLS12`]
9/// and [`TLS13`].
10#[non_exhaustive]
11#[derive(Eq, PartialEq)]
12pub struct SupportedProtocolVersion {
13    /// The TLS enumeration naming this version.
14    pub version: ProtocolVersion,
15}
16
17impl fmt::Debug for SupportedProtocolVersion {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        self.version.fmt(f)
20    }
21}
22
23/// TLS1.2
24#[cfg(feature = "tls12")]
25pub static TLS12: SupportedProtocolVersion = SupportedProtocolVersion {
26    version: ProtocolVersion::TLSv1_2,
27};
28
29/// TLS1.3
30pub static TLS13: SupportedProtocolVersion = SupportedProtocolVersion {
31    version: ProtocolVersion::TLSv1_3,
32};
33
34/// A list of all the protocol versions supported by rustls.
35pub static ALL_VERSIONS: &[&SupportedProtocolVersion] = &[
36    &TLS13,
37    #[cfg(feature = "tls12")]
38    &TLS12,
39];
40
41/// The version configuration that an application should use by default.
42///
43/// This will be [`ALL_VERSIONS`] for now, but gives space in the future
44/// to remove a version from here and require users to opt-in to older
45/// versions.
46pub static DEFAULT_VERSIONS: &[&SupportedProtocolVersion] = ALL_VERSIONS;
47
48#[derive(Clone, Copy)]
49pub(crate) struct EnabledVersions {
50    #[cfg(feature = "tls12")]
51    tls12: Option<&'static SupportedProtocolVersion>,
52    tls13: Option<&'static SupportedProtocolVersion>,
53}
54
55impl fmt::Debug for EnabledVersions {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        let mut list = &mut f.debug_list();
58        #[cfg(feature = "tls12")]
59        if let Some(v) = self.tls12 {
60            list = list.entry(v);
61        }
62        if let Some(v) = self.tls13 {
63            list = list.entry(v);
64        }
65        list.finish()
66    }
67}
68
69impl EnabledVersions {
70    pub(crate) fn new(versions: &[&'static SupportedProtocolVersion]) -> Self {
71        let mut ev = Self {
72            #[cfg(feature = "tls12")]
73            tls12: None,
74            tls13: None,
75        };
76
77        for v in versions {
78            match v.version {
79                #[cfg(feature = "tls12")]
80                ProtocolVersion::TLSv1_2 => ev.tls12 = Some(v),
81                ProtocolVersion::TLSv1_3 => ev.tls13 = Some(v),
82                _ => {}
83            }
84        }
85
86        ev
87    }
88
89    pub(crate) fn contains(&self, version: ProtocolVersion) -> bool {
90        match version {
91            #[cfg(feature = "tls12")]
92            ProtocolVersion::TLSv1_2 => self.tls12.is_some(),
93            ProtocolVersion::TLSv1_3 => self.tls13.is_some(),
94            _ => false,
95        }
96    }
97}