1#![allow(clippy::upper_case_acronyms)]
2#![allow(non_camel_case_types)]
3use crate::crypto::{KeyExchangeAlgorithm, hash};
4use crate::enums::ProtocolVersion;
5use crate::msgs::codec::{Codec, Reader};
6
7enum_builder! {
8 #[repr(u8)]
12 pub enum HashAlgorithm {
13 NONE => 0x00,
14 MD5 => 0x01,
15 SHA1 => 0x02,
16 SHA224 => 0x03,
17 SHA256 => 0x04,
18 SHA384 => 0x05,
19 SHA512 => 0x06,
20 }
21}
22
23impl HashAlgorithm {
24 pub(crate) fn hash_for_empty_input(&self) -> Option<hash::Output> {
29 match self {
30 Self::SHA256 => Some(hash::Output::new(
31 b"\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\
32 \x9a\xfb\xf4\xc8\x99\x6f\xb9\x24\
33 \x27\xae\x41\xe4\x64\x9b\x93\x4c\
34 \xa4\x95\x99\x1b\x78\x52\xb8\x55",
35 )),
36 Self::SHA384 => Some(hash::Output::new(
37 b"\x38\xb0\x60\xa7\x51\xac\x96\x38\
38 \x4c\xd9\x32\x7e\xb1\xb1\xe3\x6a\
39 \x21\xfd\xb7\x11\x14\xbe\x07\x43\
40 \x4c\x0c\xc7\xbf\x63\xf6\xe1\xda\
41 \x27\x4e\xde\xbf\xe7\x6f\x65\xfb\
42 \xd5\x1a\xd2\xf1\x48\x98\xb9\x5b",
43 )),
44 _ => None,
45 }
46 }
47}
48
49enum_builder! {
50 #[repr(u8)]
54 pub(crate) enum ClientCertificateType {
55 RSASign => 0x01,
56 DSSSign => 0x02,
57 RSAFixedDH => 0x03,
58 DSSFixedDH => 0x04,
59 RSAEphemeralDH => 0x05,
60 DSSEphemeralDH => 0x06,
61 FortezzaDMS => 0x14,
62 ECDSASign => 0x40,
63 RSAFixedECDH => 0x41,
64 ECDSAFixedECDH => 0x42,
65 }
66}
67
68enum_builder! {
69 #[repr(u8)]
73 pub enum Compression {
74 Null => 0x00,
75 Deflate => 0x01,
76 LSZ => 0x40,
77 }
78}
79
80enum_builder! {
81 #[repr(u8)]
85 pub enum AlertLevel {
86 Warning => 0x01,
87 Fatal => 0x02,
88 }
89}
90
91enum_builder! {
92 #[repr(u8)]
96 pub(crate) enum HeartbeatMessageType {
97 Request => 0x01,
98 Response => 0x02,
99 }
100}
101
102enum_builder! {
103 #[repr(u16)]
107 pub enum ExtensionType {
108 ServerName => 0x0000,
109 MaxFragmentLength => 0x0001,
110 ClientCertificateUrl => 0x0002,
111 TrustedCAKeys => 0x0003,
112 TruncatedHMAC => 0x0004,
113 StatusRequest => 0x0005,
114 UserMapping => 0x0006,
115 ClientAuthz => 0x0007,
116 ServerAuthz => 0x0008,
117 CertificateType => 0x0009,
118 EllipticCurves => 0x000a,
119 ECPointFormats => 0x000b,
120 SRP => 0x000c,
121 SignatureAlgorithms => 0x000d,
122 UseSRTP => 0x000e,
123 Heartbeat => 0x000f,
124 ALProtocolNegotiation => 0x0010,
125 SCT => 0x0012,
126 ClientCertificateType => 0x0013,
127 ServerCertificateType => 0x0014,
128 Padding => 0x0015,
129 ExtendedMasterSecret => 0x0017,
130 CompressCertificate => 0x001b,
131 SessionTicket => 0x0023,
132 PreSharedKey => 0x0029,
133 EarlyData => 0x002a,
134 SupportedVersions => 0x002b,
135 Cookie => 0x002c,
136 PSKKeyExchangeModes => 0x002d,
137 TicketEarlyDataInfo => 0x002e,
138 CertificateAuthorities => 0x002f,
139 OIDFilters => 0x0030,
140 PostHandshakeAuth => 0x0031,
141 SignatureAlgorithmsCert => 0x0032,
142 KeyShare => 0x0033,
143 TransportParameters => 0x0039,
144 NextProtocolNegotiation => 0x3374,
145 ChannelId => 0x754f,
146 RenegotiationInfo => 0xff01,
147 EncryptedClientHello => 0xfe0d, EncryptedClientHelloOuterExtensions => 0xfd00, }
150}
151
152impl ExtensionType {
153 pub(crate) fn ech_compress(&self) -> bool {
164 matches!(
166 self,
167 Self::StatusRequest
168 | Self::EllipticCurves
169 | Self::SignatureAlgorithms
170 | Self::SignatureAlgorithmsCert
171 | Self::ALProtocolNegotiation
172 | Self::SupportedVersions
173 | Self::Cookie
174 | Self::KeyShare
175 | Self::PSKKeyExchangeModes
176 )
177 }
178}
179
180enum_builder! {
181 #[repr(u8)]
185 pub(crate) enum ServerNameType {
186 HostName => 0x00,
187 }
188}
189
190enum_builder! {
191 #[repr(u16)]
200 pub enum NamedGroup {
201 secp256r1 => 0x0017,
202 secp384r1 => 0x0018,
203 secp521r1 => 0x0019,
204 X25519 => 0x001d,
205 X448 => 0x001e,
206 brainpoolP256r1tls13 => 0x001f,
208 brainpoolP384r1tls13 => 0x0020,
210 brainpoolP512r1tls13 => 0x0021,
212 curveSM2 => 0x0029,
214 FFDHE2048 => 0x0100,
215 FFDHE3072 => 0x0101,
216 FFDHE4096 => 0x0102,
217 FFDHE6144 => 0x0103,
218 FFDHE8192 => 0x0104,
219 MLKEM512 => 0x0200,
221 MLKEM768 => 0x0201,
223 MLKEM1024 => 0x0202,
225 secp256r1MLKEM768 => 0x11eb,
227 X25519MLKEM768 => 0x11ec,
229 secp384r1MLKEM1024 => 0x11ed,
231 }
232}
233
234impl NamedGroup {
235 pub fn key_exchange_algorithm(self) -> KeyExchangeAlgorithm {
237 match u16::from(self) {
238 x if (0x100..0x200).contains(&x) => KeyExchangeAlgorithm::DHE,
239 _ => KeyExchangeAlgorithm::ECDHE,
240 }
241 }
242
243 pub fn usable_for_version(&self, version: ProtocolVersion) -> bool {
244 match version {
245 ProtocolVersion::TLSv1_3 => true,
246 _ => !matches!(
247 self,
248 Self::MLKEM512
249 | Self::MLKEM768
250 | Self::MLKEM1024
251 | Self::X25519MLKEM768
252 | Self::secp256r1MLKEM768
253 | Self::secp384r1MLKEM1024
254 | Self::brainpoolP256r1tls13
255 | Self::brainpoolP384r1tls13
256 | Self::brainpoolP512r1tls13
257 | Self::curveSM2
258 ),
259 }
260 }
261}
262
263enum_builder! {
264 #[repr(u8)]
268 pub enum ECPointFormat {
269 Uncompressed => 0x00,
270 ANSIX962CompressedPrime => 0x01,
271 ANSIX962CompressedChar2 => 0x02,
272 }
273}
274
275enum_builder! {
276 #[repr(u8)]
280 pub(crate) enum HeartbeatMode {
281 PeerAllowedToSend => 0x01,
282 PeerNotAllowedToSend => 0x02,
283 }
284}
285
286enum_builder! {
287 #[repr(u8)]
291 pub(crate) enum ECCurveType {
292 ExplicitPrime => 0x01,
293 ExplicitChar2 => 0x02,
294 NamedCurve => 0x03,
295 }
296}
297
298enum_builder! {
299 #[repr(u8)]
303 pub enum PskKeyExchangeMode {
304 PSK_KE => 0x00,
305 PSK_DHE_KE => 0x01,
306 }
307}
308
309enum_builder! {
310 #[repr(u8)]
314 pub enum KeyUpdateRequest {
315 UpdateNotRequested => 0x00,
316 UpdateRequested => 0x01,
317 }
318}
319
320enum_builder! {
321 #[repr(u8)]
325 pub enum CertificateStatusType {
326 OCSP => 0x01,
327 }
328}
329
330enum_builder! {
331 #[repr(u16)]
336 pub enum HpkeKem {
337 DHKEM_P256_HKDF_SHA256 => 0x0010,
338 DHKEM_P384_HKDF_SHA384 => 0x0011,
339 DHKEM_P521_HKDF_SHA512 => 0x0012,
340 DHKEM_X25519_HKDF_SHA256 => 0x0020,
341 DHKEM_X448_HKDF_SHA512 => 0x0021,
342 }
343}
344
345enum_builder! {
346 #[repr(u16)]
351 #[derive(Default)]
352 pub enum HpkeKdf {
353 #[default]
355 HKDF_SHA256 => 0x0001,
356 HKDF_SHA384 => 0x0002,
357 HKDF_SHA512 => 0x0003,
358 }
359}
360
361enum_builder! {
362 #[repr(u16)]
367 #[derive(Default)]
368 pub enum HpkeAead {
369 #[default]
371 AES_128_GCM => 0x0001,
372 AES_256_GCM => 0x0002,
373 CHACHA20_POLY_1305 => 0x0003,
374 EXPORT_ONLY => 0xFFFF,
375 }
376}
377
378impl HpkeAead {
379 pub(crate) fn tag_len(&self) -> Option<usize> {
381 match self {
382 Self::AES_128_GCM | Self::AES_256_GCM | Self::CHACHA20_POLY_1305 => Some(16),
386 _ => None,
387 }
388 }
389}
390
391enum_builder! {
392 #[repr(u16)]
399 pub enum EchVersion {
400 V18 => 0xfe0d,
401 }
402}
403
404#[cfg(test)]
405pub(crate) mod tests {
406 use std::prelude::v1::*;
410
411 use super::*;
412
413 #[test]
414 fn test_enums() {
415 test_enum8::<HashAlgorithm>(HashAlgorithm::NONE, HashAlgorithm::SHA512);
416 test_enum8::<ClientCertificateType>(
417 ClientCertificateType::RSASign,
418 ClientCertificateType::ECDSAFixedECDH,
419 );
420 test_enum8::<Compression>(Compression::Null, Compression::LSZ);
421 test_enum8::<AlertLevel>(AlertLevel::Warning, AlertLevel::Fatal);
422 test_enum8::<HeartbeatMessageType>(
423 HeartbeatMessageType::Request,
424 HeartbeatMessageType::Response,
425 );
426 test_enum16::<ExtensionType>(ExtensionType::ServerName, ExtensionType::RenegotiationInfo);
427 test_enum8::<ServerNameType>(ServerNameType::HostName, ServerNameType::HostName);
428 test_enum16::<NamedGroup>(NamedGroup::secp256r1, NamedGroup::FFDHE8192);
429 test_enum8::<ECPointFormat>(
430 ECPointFormat::Uncompressed,
431 ECPointFormat::ANSIX962CompressedChar2,
432 );
433 test_enum8::<HeartbeatMode>(
434 HeartbeatMode::PeerAllowedToSend,
435 HeartbeatMode::PeerNotAllowedToSend,
436 );
437 test_enum8::<ECCurveType>(ECCurveType::ExplicitPrime, ECCurveType::NamedCurve);
438 test_enum8::<PskKeyExchangeMode>(
439 PskKeyExchangeMode::PSK_KE,
440 PskKeyExchangeMode::PSK_DHE_KE,
441 );
442 test_enum8::<KeyUpdateRequest>(
443 KeyUpdateRequest::UpdateNotRequested,
444 KeyUpdateRequest::UpdateRequested,
445 );
446 test_enum8::<CertificateStatusType>(
447 CertificateStatusType::OCSP,
448 CertificateStatusType::OCSP,
449 );
450 }
451
452 pub(crate) fn test_enum8<T: for<'a> Codec<'a>>(first: T, last: T) {
453 let first_v = get8(&first);
454 let last_v = get8(&last);
455
456 for val in first_v..last_v + 1 {
457 let mut buf = Vec::new();
458 val.encode(&mut buf);
459 assert_eq!(buf.len(), 1);
460
461 let t = T::read_bytes(&buf).unwrap();
462 assert_eq!(val, get8(&t));
463 std::println!("{val:?}");
464 }
465 }
466
467 pub(crate) fn test_enum8_display<T: for<'a> Codec<'a> + core::fmt::Display + Copy + From<u8>>(
468 first: T,
469 last: T,
470 ) where
471 u8: From<T>,
472 {
473 test_enum8(first, last);
474
475 for val in u8::from(first)..u8::from(last) + 1 {
476 let t = T::from(val);
477 std::println!("0x{val:02x} => {t}");
478 }
479 }
480
481 pub(crate) fn test_enum16<T: for<'a> Codec<'a>>(first: T, last: T) {
482 let first_v = get16(&first);
483 let last_v = get16(&last);
484
485 for val in first_v..last_v + 1 {
486 let mut buf = Vec::new();
487 val.encode(&mut buf);
488 assert_eq!(buf.len(), 2);
489
490 let t = T::read_bytes(&buf).unwrap();
491 assert_eq!(val, get16(&t));
492 std::println!("{val:?}");
493 }
494 }
495
496 fn get8<T: for<'a> Codec<'a>>(enum_value: &T) -> u8 {
497 let enc = enum_value.get_encoding();
498 assert_eq!(enc.len(), 1);
499 enc[0]
500 }
501
502 fn get16<T: for<'a> Codec<'a>>(enum_value: &T) -> u16 {
503 let enc = enum_value.get_encoding();
504 assert_eq!(enc.len(), 2);
505 (enc[0] as u16 >> 8) | (enc[1] as u16)
506 }
507}