use crate::tls::rustls::msgs::enums::{
CipherSuite, ProtocolVersion, SignatureAlgorithm, SignatureScheme,
};
use crate::tls::rustls::msgs::handshake::DecomposedSignatureScheme;
use crate::tls::rustls::tls12::{
Tls12CipherSuite, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
use crate::tls::rustls::tls13::{
Tls13CipherSuite, TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384,
TLS13_CHACHA20_POLY1305_SHA256,
};
use crate::tls::rustls::versions::{SupportedProtocolVersion, TLS12, TLS13};
#[allow(non_camel_case_types)]
#[derive(Debug, PartialEq)]
pub enum BulkAlgorithm {
Aes128Gcm,
Aes256Gcm,
Chacha20Poly1305,
}
pub struct CipherSuiteCommon {
pub suite: CipherSuite,
pub bulk: BulkAlgorithm,
pub aead_algorithm: &'static ring::aead::Algorithm,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SupportedCipherSuite {
Tls12(&'static Tls12CipherSuite),
Tls13(&'static Tls13CipherSuite),
}
impl SupportedCipherSuite {
pub fn hash_algorithm(&self) -> &'static ring::digest::Algorithm {
match self {
SupportedCipherSuite::Tls12(inner) => inner.hash_algorithm(),
SupportedCipherSuite::Tls13(inner) => inner.hash_algorithm(),
}
}
pub fn suite(&self) -> CipherSuite {
self.common().suite
}
pub fn common(&self) -> &CipherSuiteCommon {
match self {
SupportedCipherSuite::Tls12(inner) => &inner.common,
SupportedCipherSuite::Tls13(inner) => &inner.common,
}
}
pub fn tls13(&self) -> Option<&'static Tls13CipherSuite> {
match self {
SupportedCipherSuite::Tls12(_) => None,
SupportedCipherSuite::Tls13(inner) => Some(inner),
}
}
pub fn tls12(&self) -> Option<&'static Tls12CipherSuite> {
match self {
SupportedCipherSuite::Tls12(inner) => Some(inner),
SupportedCipherSuite::Tls13(_inner) => None,
}
}
pub fn version(&self) -> &'static SupportedProtocolVersion {
match self {
SupportedCipherSuite::Tls12(_) => &TLS12,
SupportedCipherSuite::Tls13(_) => &TLS13,
}
}
pub fn usable_for_signature_algorithm(&self, _sig_alg: SignatureAlgorithm) -> bool {
match self {
SupportedCipherSuite::Tls13(_) => true, SupportedCipherSuite::Tls12(inner) => {
inner.sign.iter().any(|scheme| scheme.sign() == _sig_alg)
}
}
}
}
pub static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = &[
TLS13_AES_256_GCM_SHA384,
TLS13_AES_128_GCM_SHA256,
TLS13_CHACHA20_POLY1305_SHA256,
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
];
pub static DEFAULT_CIPHER_SUITES: &[SupportedCipherSuite] = ALL_CIPHER_SUITES;
pub fn choose_ciphersuite_preferring_client(
client_suites: &[CipherSuite],
server_suites: &[SupportedCipherSuite],
) -> Option<SupportedCipherSuite> {
for client_suite in client_suites {
if let Some(selected) = server_suites.iter().find(|x| *client_suite == x.suite()) {
return Some(*selected);
}
}
None
}
pub fn choose_ciphersuite_preferring_server(
client_suites: &[CipherSuite],
server_suites: &[SupportedCipherSuite],
) -> Option<SupportedCipherSuite> {
if let Some(selected) = server_suites
.iter()
.find(|x| client_suites.contains(&x.suite()))
{
return Some(*selected);
}
None
}
pub fn reduce_given_sigalg(
all: &[SupportedCipherSuite],
sigalg: SignatureAlgorithm,
) -> Vec<SupportedCipherSuite> {
all.iter()
.filter(|&&suite| suite.usable_for_signature_algorithm(sigalg))
.copied()
.collect()
}
pub fn reduce_given_version(
all: &[SupportedCipherSuite],
version: ProtocolVersion,
) -> Vec<SupportedCipherSuite> {
all.iter()
.filter(|&&suite| suite.version().version == version)
.copied()
.collect()
}
pub fn compatible_sigscheme_for_suites(
sigscheme: SignatureScheme,
common_suites: &[SupportedCipherSuite],
) -> bool {
let sigalg = sigscheme.sign();
common_suites
.iter()
.any(|&suite| suite.usable_for_signature_algorithm(sigalg))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tls::rustls::msgs::enums::CipherSuite;
#[test_log::test]
fn test_client_pref() {
let client = vec![
CipherSuite::TLS13_AES_128_GCM_SHA256,
CipherSuite::TLS13_AES_256_GCM_SHA384,
];
let server = vec![TLS13_AES_256_GCM_SHA384, TLS13_AES_128_GCM_SHA256];
let chosen = choose_ciphersuite_preferring_client(&client, &server);
assert!(chosen.is_some());
assert_eq!(chosen.unwrap(), TLS13_AES_128_GCM_SHA256);
}
#[test_log::test]
fn test_server_pref() {
let client = vec![
CipherSuite::TLS13_AES_128_GCM_SHA256,
CipherSuite::TLS13_AES_256_GCM_SHA384,
];
let server = vec![TLS13_AES_256_GCM_SHA384, TLS13_AES_128_GCM_SHA256];
let chosen = choose_ciphersuite_preferring_server(&client, &server);
assert!(chosen.is_some());
assert_eq!(chosen.unwrap(), TLS13_AES_256_GCM_SHA384);
}
#[test_log::test]
fn test_pref_fails() {
assert!(choose_ciphersuite_preferring_client(
&[CipherSuite::TLS_NULL_WITH_NULL_NULL],
ALL_CIPHER_SUITES
)
.is_none());
assert!(choose_ciphersuite_preferring_server(
&[CipherSuite::TLS_NULL_WITH_NULL_NULL],
ALL_CIPHER_SUITES
)
.is_none());
}
#[test_log::test]
fn test_scs_is_debug() {
}
#[test_log::test]
fn test_can_resume_to() {
assert!(TLS13_AES_128_GCM_SHA256
.tls13()
.unwrap()
.can_resume_from(crate::tls::rustls::tls13::TLS13_CHACHA20_POLY1305_SHA256_INTERNAL)
.is_some());
assert!(TLS13_AES_256_GCM_SHA384
.tls13()
.unwrap()
.can_resume_from(crate::tls::rustls::tls13::TLS13_CHACHA20_POLY1305_SHA256_INTERNAL)
.is_none());
}
}