use core::fmt;
use pki_types::FipsStatus;
use crate::common_state::Protocol;
use crate::crypto::cipher::{AeadKey, Iv};
use crate::crypto::kx::KeyExchangeAlgorithm;
use crate::crypto::{CipherSuite, SignatureScheme, hash};
use crate::enums::ProtocolVersion;
use crate::tls12::Tls12CipherSuite;
use crate::tls13::Tls13CipherSuite;
#[expect(clippy::exhaustive_structs)]
pub struct CipherSuiteCommon {
pub suite: CipherSuite,
pub hash_provider: &'static dyn hash::Hash,
pub confidentiality_limit: u64,
}
impl CipherSuiteCommon {
pub fn fips(&self) -> FipsStatus {
self.hash_provider.fips()
}
}
#[non_exhaustive]
#[derive(Clone, Copy, PartialEq)]
pub enum SupportedCipherSuite {
Tls12(&'static Tls12CipherSuite),
Tls13(&'static Tls13CipherSuite),
}
impl SupportedCipherSuite {
pub fn suite(&self) -> CipherSuite {
self.common().suite
}
pub(crate) fn hash_provider(&self) -> &'static dyn hash::Hash {
self.common().hash_provider
}
pub(crate) fn common(&self) -> &CipherSuiteCommon {
match self {
Self::Tls12(inner) => &inner.common,
Self::Tls13(inner) => &inner.common,
}
}
pub(crate) fn usable_for_protocol(&self, proto: Protocol) -> bool {
match self {
Self::Tls12(tls12) => tls12.usable_for_protocol(proto),
Self::Tls13(tls13) => tls13.usable_for_protocol(proto),
}
}
}
impl fmt::Debug for SupportedCipherSuite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.suite().fmt(f)
}
}
pub(crate) trait Suite: fmt::Debug {
fn client_handler(&self) -> &'static dyn crate::client::ClientHandler<Self>;
fn server_handler(&self) -> &'static dyn crate::server::ServerHandler<Self>;
fn usable_for_protocol(&self, proto: Protocol) -> bool;
fn usable_for_signature_scheme(&self, _scheme: SignatureScheme) -> bool;
fn usable_for_kx_algorithm(&self, _kxa: KeyExchangeAlgorithm) -> bool {
true
}
fn suite(&self) -> CipherSuite {
self.common().suite
}
fn common(&self) -> &CipherSuiteCommon;
const VERSION: ProtocolVersion;
}
#[expect(clippy::exhaustive_structs)]
pub struct ExtractedSecrets {
pub tx: (u64, ConnectionTrafficSecrets),
pub rx: (u64, ConnectionTrafficSecrets),
}
pub(crate) struct PartiallyExtractedSecrets {
pub(crate) tx: ConnectionTrafficSecrets,
pub(crate) rx: ConnectionTrafficSecrets,
}
#[non_exhaustive]
pub enum ConnectionTrafficSecrets {
Aes128Gcm {
key: AeadKey,
iv: Iv,
},
Aes256Gcm {
key: AeadKey,
iv: Iv,
},
Chacha20Poly1305 {
key: AeadKey,
iv: Iv,
},
Sm4Gcm {
key: AeadKey,
iv: Iv,
},
Sm4Ccm {
key: AeadKey,
iv: Iv,
},
}
#[cfg(test)]
mod tests {
use std::println;
use super::SupportedCipherSuite;
use crate::crypto::TEST_PROVIDER;
#[test]
fn test_scs_is_debug() {
println!(
"{:?}",
SupportedCipherSuite::Tls13(
TEST_PROVIDER
.tls13_cipher_suites
.first()
.unwrap()
)
);
}
}