Skip to main content

miku_ktls/
cipher.rs

1//! CipherSuite that kTLS utilize.
2
3use rustls::SupportedCipherSuite;
4
5use crate::{error::CipherSuiteError, version::KtlsVersion};
6
7pub(crate) mod cipher_suite;
8
9/// A TLS cipher suite. Used mostly internally.
10#[derive(Clone, Copy)]
11pub struct KtlsCipherSuite {
12    /// The TLS version
13    pub version: KtlsVersion,
14
15    /// The cipher type
16    pub typ: KtlsCipherType,
17}
18
19impl TryFrom<SupportedCipherSuite> for KtlsCipherSuite {
20    type Error = CipherSuiteError;
21
22    fn try_from(#[allow(unused)] suite: SupportedCipherSuite) -> Result<Self, Self::Error> {
23        let version = match suite {
24            #[cfg(feature = "tls12")]
25            SupportedCipherSuite::Tls12(..) => KtlsVersion::TLS12,
26            #[cfg(not(feature = "tls12"))]
27            SupportedCipherSuite::Tls12(..) => {
28                return Err(CipherSuiteError::Tls12NotBuiltIn);
29            }
30            SupportedCipherSuite::Tls13(..) => KtlsVersion::TLS13,
31        };
32
33        let typ = match suite {
34            suite if suite == cipher_suite::TLS13_AES_128_GCM_SHA256 => KtlsCipherType::AesGcm128,
35            suite if suite == cipher_suite::TLS13_AES_256_GCM_SHA384 => KtlsCipherType::AesGcm256,
36            suite if suite == cipher_suite::TLS13_CHACHA20_POLY1305_SHA256 => {
37                KtlsCipherType::Chacha20Poly1305
38            }
39            suite if suite == cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 => {
40                KtlsCipherType::AesGcm128
41            }
42            suite if suite == cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 => {
43                KtlsCipherType::AesGcm256
44            }
45            suite if suite == cipher_suite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 => {
46                KtlsCipherType::Chacha20Poly1305
47            }
48            _ => return Err(CipherSuiteError::UnsupportedCipherSuite(suite)),
49        };
50
51        Ok(Self { version, typ })
52    }
53}
54
55impl KtlsCipherSuite {
56    #[inline]
57    /// Converts this cipher suite into the equivalent `rustls`
58    /// [`SupportedCipherSuite`].
59    pub fn as_supported_cipher_suite(&self) -> SupportedCipherSuite {
60        match self.version {
61            KtlsVersion::TLS12 => match self.typ {
62                KtlsCipherType::AesGcm128 => cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
63                KtlsCipherType::AesGcm256 => cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
64                KtlsCipherType::Chacha20Poly1305 => {
65                    cipher_suite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
66                }
67            },
68            KtlsVersion::TLS13 => match self.typ {
69                KtlsCipherType::AesGcm128 => cipher_suite::TLS13_AES_128_GCM_SHA256,
70                KtlsCipherType::AesGcm256 => cipher_suite::TLS13_AES_256_GCM_SHA384,
71                KtlsCipherType::Chacha20Poly1305 => cipher_suite::TLS13_CHACHA20_POLY1305_SHA256,
72            },
73        }
74    }
75}
76
77#[non_exhaustive]
78#[derive(Debug, Clone, Copy)]
79/// Cipher types supported by this crate
80pub enum KtlsCipherType {
81    AesGcm128,
82    AesGcm256,
83    Chacha20Poly1305,
84}