/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustls-0.23.40/src/versions.rs
Line | Count | Source |
1 | | use core::fmt; |
2 | | |
3 | | use 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)] |
12 | | pub struct SupportedProtocolVersion { |
13 | | /// The TLS enumeration naming this version. |
14 | | pub version: ProtocolVersion, |
15 | | } |
16 | | |
17 | | impl fmt::Debug for SupportedProtocolVersion { |
18 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
19 | 0 | self.version.fmt(f) |
20 | 0 | } |
21 | | } |
22 | | |
23 | | /// TLS1.2 |
24 | | #[cfg(feature = "tls12")] |
25 | | pub static TLS12: SupportedProtocolVersion = SupportedProtocolVersion { |
26 | | version: ProtocolVersion::TLSv1_2, |
27 | | }; |
28 | | |
29 | | /// TLS1.3 |
30 | | pub static TLS13: SupportedProtocolVersion = SupportedProtocolVersion { |
31 | | version: ProtocolVersion::TLSv1_3, |
32 | | }; |
33 | | |
34 | | /// A list of all the protocol versions supported by rustls. |
35 | | pub 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. |
46 | | pub static DEFAULT_VERSIONS: &[&SupportedProtocolVersion] = ALL_VERSIONS; |
47 | | |
48 | | #[derive(Clone, Copy)] |
49 | | pub(crate) struct EnabledVersions { |
50 | | #[cfg(feature = "tls12")] |
51 | | tls12: Option<&'static SupportedProtocolVersion>, |
52 | | tls13: Option<&'static SupportedProtocolVersion>, |
53 | | } |
54 | | |
55 | | impl fmt::Debug for EnabledVersions { |
56 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
57 | 0 | let mut list = &mut f.debug_list(); |
58 | | #[cfg(feature = "tls12")] |
59 | 0 | if let Some(v) = self.tls12 { |
60 | 0 | list = list.entry(v); |
61 | 0 | } |
62 | 0 | if let Some(v) = self.tls13 { |
63 | 0 | list = list.entry(v); |
64 | 0 | } |
65 | 0 | list.finish() |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl EnabledVersions { |
70 | 0 | pub(crate) fn new(versions: &[&'static SupportedProtocolVersion]) -> Self { |
71 | 0 | let mut ev = Self { |
72 | 0 | #[cfg(feature = "tls12")] |
73 | 0 | tls12: None, |
74 | 0 | tls13: None, |
75 | 0 | }; |
76 | | |
77 | 0 | for v in versions { |
78 | 0 | match v.version { |
79 | | #[cfg(feature = "tls12")] |
80 | 0 | ProtocolVersion::TLSv1_2 => ev.tls12 = Some(v), |
81 | 0 | ProtocolVersion::TLSv1_3 => ev.tls13 = Some(v), |
82 | 0 | _ => {} |
83 | | } |
84 | | } |
85 | | |
86 | 0 | ev |
87 | 0 | } |
88 | | |
89 | 0 | pub(crate) fn contains(&self, version: ProtocolVersion) -> bool { |
90 | 0 | match version { |
91 | | #[cfg(feature = "tls12")] |
92 | 0 | ProtocolVersion::TLSv1_2 => self.tls12.is_some(), |
93 | 0 | ProtocolVersion::TLSv1_3 => self.tls13.is_some(), |
94 | 0 | _ => false, |
95 | | } |
96 | 0 | } |
97 | | } |