Skip to main content

net_wire/tls/
types.rs

1//! TLS scalar wire values.
2
3macro_rules! wire_type {
4    ($name:ident, $raw:ty, $grease:expr) => {
5        #[doc = concat!("A raw TLS ", stringify!($name), " value.")]
6        #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
7        pub struct $name($raw);
8        impl $name {
9            /// Creates a value without restricting private or unknown assignments.
10            #[inline]
11            pub const fn new(raw: $raw) -> Self {
12                Self(raw)
13            }
14            /// Returns the encoded wire value.
15            #[inline]
16            pub const fn raw(self) -> $raw {
17                self.0
18            }
19            /// Returns whether this value is an RFC 8701 GREASE value.
20            #[inline]
21            pub const fn is_grease(self) -> bool {
22                $grease(self.0)
23            }
24        }
25    };
26}
27
28const fn grease_u16(value: u16) -> bool {
29    let high = (value >> 8) as u8;
30    let low = value as u8;
31    high == low && high & 0x0f == 0x0a
32}
33const fn grease_u8(value: u8) -> bool {
34    matches!(value, 0x0b | 0x2a | 0x49 | 0x68 | 0x87 | 0xa6 | 0xc5 | 0xe4)
35}
36
37const fn never_u8(_: u8) -> bool {
38    false
39}
40
41const fn never_u16(_: u16) -> bool {
42    false
43}
44
45wire_type!(TlsContentType, u8, never_u8);
46wire_type!(TlsProtocolVersion, u16, grease_u16);
47wire_type!(TlsHandshakeType, u8, never_u8);
48wire_type!(TlsCipherSuite, u16, grease_u16);
49wire_type!(TlsCompressionMethod, u8, never_u8);
50wire_type!(TlsExtensionType, u16, grease_u16);
51wire_type!(TlsNamedGroup, u16, grease_u16);
52wire_type!(TlsSignatureScheme, u16, grease_u16);
53wire_type!(TlsPskKeyExchangeMode, u8, grease_u8);
54wire_type!(TlsServerNameType, u8, never_u8);
55wire_type!(TlsCertificateCompressionAlgorithm, u16, never_u16);
56
57impl TlsContentType {
58    /// The change_cipher_spec record content type.
59    pub const CHANGE_CIPHER_SPEC: Self = Self(20);
60    /// The alert record content type.
61    pub const ALERT: Self = Self(21);
62    /// The handshake record content type.
63    pub const HANDSHAKE: Self = Self(22);
64    /// The application_data record content type.
65    pub const APPLICATION_DATA: Self = Self(23);
66}
67impl TlsProtocolVersion {
68    /// TLS 1.2.
69    pub const TLS12: Self = Self(0x0303);
70    /// TLS 1.3.
71    pub const TLS13: Self = Self(0x0304);
72}
73impl TlsHandshakeType {
74    /// ClientHello.
75    pub const CLIENT_HELLO: Self = Self(1);
76    /// ServerHello.
77    pub const SERVER_HELLO: Self = Self(2);
78}
79impl TlsExtensionType {
80    /// Server name indication.
81    pub const SERVER_NAME: Self = Self(0);
82    /// Supported groups.
83    pub const SUPPORTED_GROUPS: Self = Self(10);
84    /// EC point formats.
85    pub const EC_POINT_FORMATS: Self = Self(11);
86    /// Signature algorithms.
87    pub const SIGNATURE_ALGORITHMS: Self = Self(13);
88    /// ALPN.
89    pub const APPLICATION_LAYER_PROTOCOL_NEGOTIATION: Self = Self(16);
90    /// Certificate compression algorithms.
91    pub const COMPRESS_CERTIFICATE: Self = Self(27);
92    /// Pre-shared key extension.
93    pub const PRE_SHARED_KEY: Self = Self(41);
94    /// Early data.
95    pub const EARLY_DATA: Self = Self(42);
96    /// Cookie.
97    pub const COOKIE: Self = Self(44);
98    /// Certificate signature algorithms.
99    pub const SIGNATURE_ALGORITHMS_CERT: Self = Self(50);
100    /// Supported versions.
101    pub const SUPPORTED_VERSIONS: Self = Self(43);
102    /// PSK key exchange modes.
103    pub const PSK_KEY_EXCHANGE_MODES: Self = Self(45);
104    /// Key share.
105    pub const KEY_SHARE: Self = Self(51);
106    /// RFC 9001 QUIC transport parameters.
107    pub const QUIC_TRANSPORT_PARAMETERS: Self = Self(0x0039);
108}
109
110impl TlsCipherSuite {
111    /// TLS_AES_128_GCM_SHA256.
112    pub const TLS_AES_128_GCM_SHA256: Self = Self(0x1301);
113    /// TLS_AES_256_GCM_SHA384.
114    pub const TLS_AES_256_GCM_SHA384: Self = Self(0x1302);
115    /// TLS_CHACHA20_POLY1305_SHA256.
116    pub const TLS_CHACHA20_POLY1305_SHA256: Self = Self(0x1303);
117}
118
119impl TlsCompressionMethod {
120    /// The null legacy compression method.
121    pub const NULL: Self = Self(0);
122}
123
124impl TlsNamedGroup {
125    /// The secp256r1 named group.
126    pub const SECP256R1: Self = Self(23);
127    /// The secp384r1 named group.
128    pub const SECP384R1: Self = Self(24);
129    /// The X25519 named group.
130    pub const X25519: Self = Self(29);
131}
132
133impl TlsSignatureScheme {
134    /// rsa_pss_rsae_sha256.
135    pub const RSA_PSS_RSAE_SHA256: Self = Self(0x0804);
136    /// ecdsa_secp256r1_sha256.
137    pub const ECDSA_SECP256R1_SHA256: Self = Self(0x0403);
138    /// ed25519.
139    pub const ED25519: Self = Self(0x0807);
140}
141
142impl TlsPskKeyExchangeMode {
143    /// PSK-only key establishment.
144    pub const PSK_KE: Self = Self(0);
145    /// PSK with an ephemeral Diffie-Hellman key exchange.
146    pub const PSK_DHE_KE: Self = Self(1);
147}
148
149impl TlsServerNameType {
150    /// A DNS hostname.
151    pub const HOST_NAME: Self = Self(0);
152}
153
154impl TlsCertificateCompressionAlgorithm {
155    /// The zlib certificate-compression algorithm.
156    pub const ZLIB: Self = Self(1);
157    /// The Brotli certificate-compression algorithm.
158    pub const BROTLI: Self = Self(2);
159    /// The Zstandard certificate-compression algorithm.
160    pub const ZSTD: Self = Self(3);
161}