macro_rules! wire_type {
($name:ident, $raw:ty, $grease:expr) => {
#[doc = concat!("A raw TLS ", stringify!($name), " value.")]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct $name($raw);
impl $name {
#[inline]
pub const fn new(raw: $raw) -> Self {
Self(raw)
}
#[inline]
pub const fn raw(self) -> $raw {
self.0
}
#[inline]
pub const fn is_grease(self) -> bool {
$grease(self.0)
}
}
};
}
const fn grease_u16(value: u16) -> bool {
let high = (value >> 8) as u8;
let low = value as u8;
high == low && high & 0x0f == 0x0a
}
const fn grease_u8(value: u8) -> bool {
matches!(value, 0x0b | 0x2a | 0x49 | 0x68 | 0x87 | 0xa6 | 0xc5 | 0xe4)
}
const fn never_u8(_: u8) -> bool {
false
}
const fn never_u16(_: u16) -> bool {
false
}
wire_type!(TlsContentType, u8, never_u8);
wire_type!(TlsProtocolVersion, u16, grease_u16);
wire_type!(TlsHandshakeType, u8, never_u8);
wire_type!(TlsCipherSuite, u16, grease_u16);
wire_type!(TlsCompressionMethod, u8, never_u8);
wire_type!(TlsExtensionType, u16, grease_u16);
wire_type!(TlsNamedGroup, u16, grease_u16);
wire_type!(TlsSignatureScheme, u16, grease_u16);
wire_type!(TlsPskKeyExchangeMode, u8, grease_u8);
wire_type!(TlsServerNameType, u8, never_u8);
wire_type!(TlsCertificateCompressionAlgorithm, u16, never_u16);
impl TlsContentType {
pub const CHANGE_CIPHER_SPEC: Self = Self(20);
pub const ALERT: Self = Self(21);
pub const HANDSHAKE: Self = Self(22);
pub const APPLICATION_DATA: Self = Self(23);
}
impl TlsProtocolVersion {
pub const TLS12: Self = Self(0x0303);
pub const TLS13: Self = Self(0x0304);
}
impl TlsHandshakeType {
pub const CLIENT_HELLO: Self = Self(1);
pub const SERVER_HELLO: Self = Self(2);
}
impl TlsExtensionType {
pub const SERVER_NAME: Self = Self(0);
pub const SUPPORTED_GROUPS: Self = Self(10);
pub const EC_POINT_FORMATS: Self = Self(11);
pub const SIGNATURE_ALGORITHMS: Self = Self(13);
pub const APPLICATION_LAYER_PROTOCOL_NEGOTIATION: Self = Self(16);
pub const COMPRESS_CERTIFICATE: Self = Self(27);
pub const PRE_SHARED_KEY: Self = Self(41);
pub const EARLY_DATA: Self = Self(42);
pub const COOKIE: Self = Self(44);
pub const SIGNATURE_ALGORITHMS_CERT: Self = Self(50);
pub const SUPPORTED_VERSIONS: Self = Self(43);
pub const PSK_KEY_EXCHANGE_MODES: Self = Self(45);
pub const KEY_SHARE: Self = Self(51);
pub const QUIC_TRANSPORT_PARAMETERS: Self = Self(0x0039);
}
impl TlsCipherSuite {
pub const TLS_AES_128_GCM_SHA256: Self = Self(0x1301);
pub const TLS_AES_256_GCM_SHA384: Self = Self(0x1302);
pub const TLS_CHACHA20_POLY1305_SHA256: Self = Self(0x1303);
}
impl TlsCompressionMethod {
pub const NULL: Self = Self(0);
}
impl TlsNamedGroup {
pub const SECP256R1: Self = Self(23);
pub const SECP384R1: Self = Self(24);
pub const X25519: Self = Self(29);
}
impl TlsSignatureScheme {
pub const RSA_PSS_RSAE_SHA256: Self = Self(0x0804);
pub const ECDSA_SECP256R1_SHA256: Self = Self(0x0403);
pub const ED25519: Self = Self(0x0807);
}
impl TlsPskKeyExchangeMode {
pub const PSK_KE: Self = Self(0);
pub const PSK_DHE_KE: Self = Self(1);
}
impl TlsServerNameType {
pub const HOST_NAME: Self = Self(0);
}
impl TlsCertificateCompressionAlgorithm {
pub const ZLIB: Self = Self(1);
pub const BROTLI: Self = Self(2);
pub const ZSTD: Self = Self(3);
}