From 4fe43f85d160bbf05a3626012580c2a1b75e5562 Mon Sep 17 00:00:00 2001 From: Rushil Mehra Date: Mon, 22 Jul 2024 18:00:23 -0700 Subject: [PATCH] Impl From for SslVersion --- boring/src/ssl/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs index fade400c..751daae7 100644 --- a/boring/src/ssl/mod.rs +++ b/boring/src/ssl/mod.rs @@ -533,6 +533,10 @@ impl SelectCertError { } /// Extension types, to be used with `ClientHello::get_extension`. +/// +/// NOTE: The current implementation of `From` is unsound, as it's possible to create an ExtensionType +/// that is not defined by the impl. `From` will be deprecated in favor of `TryFrom` in the next +/// major bump of the library. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct ExtensionType(u16); @@ -598,6 +602,21 @@ impl SslVersion { pub const TLS1_3: SslVersion = SslVersion(ffi::TLS1_3_VERSION as _); } +impl TryFrom for SslVersion { + type Error = &'static str; + + fn try_from(value: u16) -> Result { + match value as i32 { + ffi::SSL3_VERSION + | ffi::TLS1_VERSION + | ffi::TLS1_1_VERSION + | ffi::TLS1_2_VERSION + | ffi::TLS1_3_VERSION => Ok(Self(value)), + _ => Err("Unknown SslVersion"), + } + } +} + impl fmt::Debug for SslVersion { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(match *self { @@ -625,6 +644,10 @@ impl fmt::Display for SslVersion { } /// A signature verification algorithm. +/// +/// NOTE: The current implementation of `From` is unsound, as it's possible to create an +/// SslSignatureAlgorithm that is not defined by the impl. `From` will be deprecated in favor of +/// `TryFrom` in the next major bump of the library. #[repr(transparent)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct SslSignatureAlgorithm(u16);