| 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 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 19 | self.version.fmt(f) |
| 20 | } |
| 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 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | let mut list: &mut DebugList<'_, '_> = &mut f.debug_list(); |
| 58 | #[cfg (feature = "tls12" )] |
| 59 | if let Some(v: &'static SupportedProtocolVersion) = self.tls12 { |
| 60 | list = list.entry(v); |
| 61 | } |
| 62 | if let Some(v: &'static SupportedProtocolVersion) = self.tls13 { |
| 63 | list = list.entry(v); |
| 64 | } |
| 65 | list.finish() |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl 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 | } |
| 98 | |