| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::{fmt::Display, str::FromStr}; |
| 9 |
|
| 10 |
use s2n_codec::zerocopy::U16; |
| 11 |
use serde::{Deserialize, Serialize}; |
| 12 |
use serde_with::{DeserializeAs, SerializeAs, serde_as}; |
| 13 |
use zerocopy::{FromBytes, Immutable, Unaligned}; |
| 14 |
|
| 15 |
pub const GROUP_COUNT: usize = GROUPS_AVAILABLE_IN_S2N.len(); |
| 16 |
pub const CIPHER_COUNT: usize = CIPHERS_AVAILABLE_IN_S2N.len(); |
| 17 |
pub const SIGNATURE_COUNT: usize = SIGNATURE_SCHEMES_AVAILABLE_IN_S2N.len(); |
| 18 |
pub const PROTOCOL_COUNT: usize = VERSIONS_AVAILABLE_IN_S2N.len(); |
| 19 |
pub const CERT_KEY_COUNT: usize = CERT_KEYS.len(); |
| 20 |
pub const CERT_SIG_COUNT: usize = CERT_SIGS.len(); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
pub(crate) struct ZerocopyU16; |
| 26 |
|
| 27 |
impl SerializeAs<U16> for ZerocopyU16 { |
| 28 |
fn serialize_as<S: serde::Serializer>(value: &U16, serializer: S) -> Result<S::Ok, S::Error> { |
| 29 |
serializer.serialize_u16(value.get()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl<'de> DeserializeAs<'de, U16> for ZerocopyU16 { |
| 34 |
fn deserialize_as<D: serde::Deserializer<'de>>(deserializer: D) -> Result<U16, D::Error> { |
| 35 |
<u16 as serde::Deserialize>::deserialize(deserializer).map(U16::new) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub trait FiniteCounter<const N: usize>: Copy + PartialEq { |
| 45 |
|
| 46 |
|
| 47 |
const ELEMENTS: [Self; N]; |
| 48 |
|
| 49 |
|
| 50 |
fn slot_from_key(&self) -> Option<usize> { |
| 51 |
Self::ELEMENTS.iter().position(|e| e == self) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
fn key_from_slot(slot: usize) -> Option<Self> { |
| 56 |
Self::ELEMENTS.get(slot).copied() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl FiniteCounter<CIPHER_COUNT> for Cipher { |
| 61 |
const ELEMENTS: [Cipher; CIPHER_COUNT] = { |
| 62 |
let mut out = [Cipher([0, 0]); CIPHER_COUNT]; |
| 63 |
let mut i = 0; |
| 64 |
while i < CIPHER_COUNT { |
| 65 |
out[i] = CIPHERS_AVAILABLE_IN_S2N[i].cipher; |
| 66 |
i += 1; |
| 67 |
} |
| 68 |
out |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
impl FromStr for Cipher { |
| 73 |
type Err = (); |
| 74 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 75 |
CIPHERS_AVAILABLE_IN_S2N |
| 76 |
.iter() |
| 77 |
.find(|info| info.iana_description == s) |
| 78 |
.map(|info| info.cipher) |
| 79 |
.ok_or(()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl FiniteCounter<PROTOCOL_COUNT> for Version { |
| 84 |
const ELEMENTS: [Version; PROTOCOL_COUNT] = { |
| 85 |
let mut out = [Version(U16::new(0)); PROTOCOL_COUNT]; |
| 86 |
let mut i = 0; |
| 87 |
while i < PROTOCOL_COUNT { |
| 88 |
out[i] = Version(U16::new(VERSIONS_AVAILABLE_IN_S2N[i].iana_value)); |
| 89 |
i += 1; |
| 90 |
} |
| 91 |
out |
| 92 |
}; |
| 93 |
} |
| 94 |
|
| 95 |
impl FromStr for Version { |
| 96 |
type Err = (); |
| 97 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 98 |
VERSIONS_AVAILABLE_IN_S2N |
| 99 |
.iter() |
| 100 |
.find(|info| info.description == s) |
| 101 |
.map(|info| Version(U16::new(info.iana_value))) |
| 102 |
.ok_or(()) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl FiniteCounter<GROUP_COUNT> for Group { |
| 107 |
const ELEMENTS: [Group; GROUP_COUNT] = { |
| 108 |
let mut out = [Group(U16::new(0)); GROUP_COUNT]; |
| 109 |
let mut i = 0; |
| 110 |
while i < GROUP_COUNT { |
| 111 |
out[i] = GROUPS_AVAILABLE_IN_S2N[i].group; |
| 112 |
i += 1; |
| 113 |
} |
| 114 |
out |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
impl FromStr for Group { |
| 119 |
type Err = (); |
| 120 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 121 |
GROUPS_AVAILABLE_IN_S2N |
| 122 |
.iter() |
| 123 |
.find(|info| info.iana_description == s) |
| 124 |
.map(|info| info.group) |
| 125 |
.ok_or(()) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl FiniteCounter<SIGNATURE_COUNT> for Signature { |
| 130 |
const ELEMENTS: [Signature; SIGNATURE_COUNT] = { |
| 131 |
let mut out = [Signature(U16::new(0)); SIGNATURE_COUNT]; |
| 132 |
let mut i = 0; |
| 133 |
while i < SIGNATURE_COUNT { |
| 134 |
out[i] = SIGNATURE_SCHEMES_AVAILABLE_IN_S2N[i].signature; |
| 135 |
i += 1; |
| 136 |
} |
| 137 |
out |
| 138 |
}; |
| 139 |
} |
| 140 |
|
| 141 |
impl FromStr for Signature { |
| 142 |
type Err = (); |
| 143 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 144 |
SIGNATURE_SCHEMES_AVAILABLE_IN_S2N |
| 145 |
.iter() |
| 146 |
.find(|info| info.description == s) |
| 147 |
.map(|info| info.signature) |
| 148 |
.ok_or(()) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
impl FiniteCounter<DEFINED_ALERTS_COUNT> for Alert { |
| 153 |
const ELEMENTS: [Self; DEFINED_ALERTS_COUNT] = Alert::DEFINED_ALERTS; |
| 154 |
} |
| 155 |
|
| 156 |
#[serde_as] |
| 157 |
#[derive( |
| 158 |
Debug, |
| 159 |
Clone, |
| 160 |
Copy, |
| 161 |
PartialEq, |
| 162 |
Eq, |
| 163 |
Hash, |
| 164 |
FromBytes, |
| 165 |
Immutable, |
| 166 |
Unaligned, |
| 167 |
serde::Serialize, |
| 168 |
serde::Deserialize, |
| 169 |
)] |
| 170 |
#[repr(C)] |
| 171 |
pub struct Version(#[serde_as(as = "ZerocopyU16")] pub s2n_codec::zerocopy::U16); |
| 172 |
|
| 173 |
impl Version { |
| 174 |
pub const SSL_V3: Version = Version(U16::new(0x0300)); |
| 175 |
pub const TLS_1_0: Version = Version(U16::new(0x0301)); |
| 176 |
pub const TLS_1_1: Version = Version(U16::new(0x0302)); |
| 177 |
pub const TLS_1_2: Version = Version(U16::new(0x0303)); |
| 178 |
pub const TLS_1_3: Version = Version(U16::new(0x0304)); |
| 179 |
|
| 180 |
pub fn known_description(&self) -> Option<&'static str> { |
| 181 |
match *self { |
| 182 |
Self::SSL_V3 => Some("SSLv3"), |
| 183 |
Self::TLS_1_0 => Some("TLSv1_0"), |
| 184 |
Self::TLS_1_1 => Some("TLSv1_1"), |
| 185 |
Self::TLS_1_2 => Some("TLSv1_2"), |
| 186 |
Self::TLS_1_3 => Some("TLSv1_3"), |
| 187 |
_ => None, |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl Display for Version { |
| 193 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 194 |
match self.known_description() { |
| 195 |
Some(name) => f.write_str(name), |
| 196 |
None => write!(f, "unknown_version_0x{:04x}", self.0.get()), |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl<'a> s2n_codec::DecoderValue<'a> for Version { |
| 202 |
fn decode(bytes: s2n_codec::DecoderBuffer<'a>) -> s2n_codec::DecoderBufferResult<'a, Self> { |
| 203 |
let (value, bytes) = bytes.decode()?; |
| 204 |
Ok((Self(value), bytes)) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[derive( |
| 209 |
Debug, |
| 210 |
Clone, |
| 211 |
Copy, |
| 212 |
PartialEq, |
| 213 |
Eq, |
| 214 |
Hash, |
| 215 |
FromBytes, |
| 216 |
Immutable, |
| 217 |
Unaligned, |
| 218 |
serde::Serialize, |
| 219 |
serde::Deserialize, |
| 220 |
)] |
| 221 |
#[repr(C)] |
| 222 |
pub struct Cipher(pub [u8; 2]); |
| 223 |
|
| 224 |
impl Cipher { |
| 225 |
#[allow(dead_code)] |
| 226 |
pub const TLS_EMPTY_RENEGOTIATION_INFO_SCSV: Self = Cipher([0, 255]); |
| 227 |
|
| 228 |
pub const TLS_AES_128_GCM_SHA256: Self = Cipher([0x13, 0x01]); |
| 229 |
pub const TLS_AES_256_GCM_SHA384: Self = Cipher([0x13, 0x02]); |
| 230 |
pub const TLS_CHACHA20_POLY1305_SHA256: Self = Cipher([0x13, 0x03]); |
| 231 |
pub const TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: Self = Cipher([0xC0, 0x2B]); |
| 232 |
pub const TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: Self = Cipher([0xC0, 0x2C]); |
| 233 |
pub const TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: Self = Cipher([0xC0, 0x2F]); |
| 234 |
pub const TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: Self = Cipher([0xC0, 0x30]); |
| 235 |
|
| 236 |
pub fn known_description(&self) -> Option<&'static str> { |
| 237 |
CIPHERS_AVAILABLE_IN_S2N |
| 238 |
.iter() |
| 239 |
.find(|info| info.cipher == *self) |
| 240 |
.map(|info| info.iana_description) |
| 241 |
} |
| 242 |
|
| 243 |
pub fn from_openssl_name(name: &str) -> Option<Self> { |
| 244 |
CIPHERS_AVAILABLE_IN_S2N |
| 245 |
.iter() |
| 246 |
.find(|info| info.openssl_name == name) |
| 247 |
.map(|info| info.cipher) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
impl Display for Cipher { |
| 252 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 253 |
match self.known_description() { |
| 254 |
Some(name) => f.write_str(name), |
| 255 |
None => { |
| 256 |
let [hi, lo] = self.0; |
| 257 |
let id = ((hi as u16) << 8) | (lo as u16); |
| 258 |
write!(f, "unknown_cipher_0x{id:04x}") |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
#[serde_as] |
| 265 |
#[derive( |
| 266 |
Debug, |
| 267 |
Clone, |
| 268 |
Copy, |
| 269 |
PartialEq, |
| 270 |
Eq, |
| 271 |
Hash, |
| 272 |
FromBytes, |
| 273 |
Immutable, |
| 274 |
Unaligned, |
| 275 |
serde::Serialize, |
| 276 |
serde::Deserialize, |
| 277 |
)] |
| 278 |
#[repr(C)] |
| 279 |
pub struct Signature(#[serde_as(as = "ZerocopyU16")] pub s2n_codec::zerocopy::U16); |
| 280 |
|
| 281 |
#[allow(non_upper_case_globals)] |
| 282 |
impl Signature { |
| 283 |
pub const rsa_pss_rsae_sha256: Self = Signature(U16::new(0x0804)); |
| 284 |
pub const rsa_pss_rsae_sha384: Self = Signature(U16::new(0x0805)); |
| 285 |
pub const rsa_pss_rsae_sha512: Self = Signature(U16::new(0x0806)); |
| 286 |
|
| 287 |
pub const rsa_pss_pss_sha256: Self = Signature(U16::new(0x0809)); |
| 288 |
pub const rsa_pss_pss_sha384: Self = Signature(U16::new(0x080A)); |
| 289 |
pub const rsa_pss_pss_sha512: Self = Signature(U16::new(0x080B)); |
| 290 |
|
| 291 |
pub const ecdsa_secp256r1_sha256: Self = Signature(U16::new(0x0403)); |
| 292 |
pub const ecdsa_secp384r1_sha384: Self = Signature(U16::new(0x0503)); |
| 293 |
pub const ecdsa_secp521r1_sha512: Self = Signature(U16::new(0x0603)); |
| 294 |
|
| 295 |
#[allow(dead_code)] |
| 296 |
pub const mldsa44: Self = Signature(U16::new(0x0904)); |
| 297 |
#[allow(dead_code)] |
| 298 |
pub const mldsa65: Self = Signature(U16::new(0x0905)); |
| 299 |
pub const mldsa87: Self = Signature(U16::new(0x0906)); |
| 300 |
|
| 301 |
pub fn known_description(&self) -> Option<&'static str> { |
| 302 |
SIGNATURE_SCHEMES_AVAILABLE_IN_S2N |
| 303 |
.iter() |
| 304 |
.find(|info| info.signature == *self) |
| 305 |
.map(|info| info.description) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
impl Display for Signature { |
| 310 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 311 |
match self.known_description() { |
| 312 |
Some(name) => f.write_str(name), |
| 313 |
None => write!(f, "unknown_signature_0x{:04x}", self.0.get()), |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#[serde_as] |
| 319 |
#[derive( |
| 320 |
Debug, |
| 321 |
Clone, |
| 322 |
Copy, |
| 323 |
PartialEq, |
| 324 |
Eq, |
| 325 |
Hash, |
| 326 |
FromBytes, |
| 327 |
Immutable, |
| 328 |
Unaligned, |
| 329 |
serde::Serialize, |
| 330 |
serde::Deserialize, |
| 331 |
)] |
| 332 |
#[repr(C)] |
| 333 |
pub struct Group(#[serde_as(as = "ZerocopyU16")] pub s2n_codec::zerocopy::U16); |
| 334 |
|
| 335 |
#[allow(non_upper_case_globals)] |
| 336 |
impl Group { |
| 337 |
pub const x25519: Self = Group(U16::new(29)); |
| 338 |
pub const secp256r1: Self = Group(U16::new(23)); |
| 339 |
pub const secp384r1: Self = Group(U16::new(24)); |
| 340 |
pub const secp521r1: Self = Group(U16::new(25)); |
| 341 |
|
| 342 |
pub const SecP256r1MLKEM768: Self = Group(U16::new(4587)); |
| 343 |
pub const X25519MLKEM768: Self = Group(U16::new(4588)); |
| 344 |
pub const SecP384r1MLKEM1024: Self = Group(U16::new(4589)); |
| 345 |
|
| 346 |
pub const MLKEM1024: Self = Group(U16::new(514)); |
| 347 |
|
| 348 |
pub fn known_description(&self) -> Option<&'static str> { |
| 349 |
GROUPS_AVAILABLE_IN_S2N |
| 350 |
.iter() |
| 351 |
.find(|info| info.group == *self) |
| 352 |
.map(|info| info.iana_description) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
impl Display for Group { |
| 357 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 358 |
match self.known_description() { |
| 359 |
Some(name) => f.write_str(name), |
| 360 |
None => write!(f, "unknown_group_0x{:04x}", self.0.get()), |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 366 |
pub struct VersionInformation { |
| 367 |
pub description: &'static str, |
| 368 |
pub iana_value: u16, |
| 369 |
} |
| 370 |
|
| 371 |
impl VersionInformation { |
| 372 |
pub const fn new(description: &'static str, iana_value: u16) -> Self { |
| 373 |
Self { |
| 374 |
description, |
| 375 |
iana_value, |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 381 |
pub struct CipherInformation { |
| 382 |
pub cipher: Cipher, |
| 383 |
pub iana_description: &'static str, |
| 384 |
pub openssl_name: &'static str, |
| 385 |
} |
| 386 |
|
| 387 |
impl CipherInformation { |
| 388 |
pub const fn new( |
| 389 |
iana_description: &'static str, |
| 390 |
iana_value: [u8; 2], |
| 391 |
openssl_name: &'static str, |
| 392 |
) -> Self { |
| 393 |
Self { |
| 394 |
openssl_name, |
| 395 |
iana_description, |
| 396 |
cipher: Cipher(iana_value), |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 402 |
pub struct GroupInformation { |
| 403 |
pub iana_description: &'static str, |
| 404 |
pub group: Group, |
| 405 |
} |
| 406 |
|
| 407 |
impl GroupInformation { |
| 408 |
pub const fn new(iana_description: &'static str, iana_value: u16) -> Self { |
| 409 |
Self { |
| 410 |
iana_description, |
| 411 |
group: Group(U16::new(iana_value)), |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 417 |
pub struct SignatureSchemeInformation { |
| 418 |
pub description: &'static str, |
| 419 |
pub signature: Signature, |
| 420 |
} |
| 421 |
|
| 422 |
impl SignatureSchemeInformation { |
| 423 |
pub const fn new(iana_description: &'static str, iana_value: u16) -> Self { |
| 424 |
Self { |
| 425 |
description: iana_description, |
| 426 |
signature: Signature(U16::new(iana_value)), |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, serde::Serialize, serde::Deserialize)] |
| 448 |
pub struct Alert(pub u8); |
| 449 |
include!(concat!(env!("OUT_DIR"), "/alerts_generated.rs")); |
| 450 |
|
| 451 |
impl Display for Alert { |
| 452 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 453 |
match self.get_description() { |
| 454 |
Some(name) => f.write_str(name), |
| 455 |
None => write!(f, "unknown_alert_{}", self.0), |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
impl FromStr for Alert { |
| 461 |
type Err = (); |
| 462 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 463 |
Self::DEFINED_ALERTS |
| 464 |
.iter() |
| 465 |
.find(|a| a.get_description() == Some(s)) |
| 466 |
.copied() |
| 467 |
.ok_or(()) |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
pub const DEFINED_ALERTS_COUNT: usize = Alert::DEFINED_ALERTS.len(); |
| 472 |
|
| 473 |
pub const VERSIONS_AVAILABLE_IN_S2N: [VersionInformation; 5] = [ |
| 474 |
VersionInformation::new("SSLv3", 0x0300), |
| 475 |
VersionInformation::new("TLSv1_0", 0x0301), |
| 476 |
VersionInformation::new("TLSv1_1", 0x0302), |
| 477 |
VersionInformation::new("TLSv1_2", 0x0303), |
| 478 |
VersionInformation::new("TLSv1_3", 0x0304), |
| 479 |
]; |
| 480 |
|
| 481 |
#[rustfmt::skip] |
| 482 |
pub const CIPHERS_AVAILABLE_IN_S2N: [CipherInformation; 37] = [ |
| 483 |
CipherInformation::new("TLS_AES_128_GCM_SHA256", [19, 1], "TLS_AES_128_GCM_SHA256" ), |
| 484 |
CipherInformation::new("TLS_AES_256_GCM_SHA384", [19, 2], "TLS_AES_256_GCM_SHA384" ), |
| 485 |
CipherInformation::new("TLS_CHACHA20_POLY1305_SHA256", [19, 3], "TLS_CHACHA20_POLY1305_SHA256" ), |
| 486 |
CipherInformation::new("TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA", [0, 22], "DHE-RSA-DES-CBC3-SHA" ), |
| 487 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_128_CBC_SHA", [0, 51], "DHE-RSA-AES128-SHA" ), |
| 488 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_128_CBC_SHA256", [0, 103], "DHE-RSA-AES128-SHA256" ), |
| 489 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_128_GCM_SHA256", [0, 158], "DHE-RSA-AES128-GCM-SHA256" ), |
| 490 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_256_CBC_SHA", [0, 57], "DHE-RSA-AES256-SHA" ), |
| 491 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_256_CBC_SHA256", [0, 107], "DHE-RSA-AES256-SHA256" ), |
| 492 |
CipherInformation::new("TLS_DHE_RSA_WITH_AES_256_GCM_SHA384", [0, 159], "DHE-RSA-AES256-GCM-SHA384" ), |
| 493 |
CipherInformation::new("TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256", [204, 170], "DHE-RSA-CHACHA20-POLY1305" ), |
| 494 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", [192, 9], "ECDHE-ECDSA-AES128-SHA" ), |
| 495 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", [192, 35], "ECDHE-ECDSA-AES128-SHA256" ), |
| 496 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", [192, 43], "ECDHE-ECDSA-AES128-GCM-SHA256" ), |
| 497 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", [192, 10], "ECDHE-ECDSA-AES256-SHA" ), |
| 498 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", [192, 36], "ECDHE-ECDSA-AES256-SHA384" ), |
| 499 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", [192, 44], "ECDHE-ECDSA-AES256-GCM-SHA384" ), |
| 500 |
CipherInformation::new("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", [204, 169], "ECDHE-ECDSA-CHACHA20-POLY1305" ), |
| 501 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", [192, 18], "ECDHE-RSA-DES-CBC3-SHA" ), |
| 502 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", [192, 19], "ECDHE-RSA-AES128-SHA" ), |
| 503 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", [192, 39], "ECDHE-RSA-AES128-SHA256" ), |
| 504 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", [192, 47], "ECDHE-RSA-AES128-GCM-SHA256" ), |
| 505 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", [192, 20], "ECDHE-RSA-AES256-SHA" ), |
| 506 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", [192, 40], "ECDHE-RSA-AES256-SHA384" ), |
| 507 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", [192, 48], "ECDHE-RSA-AES256-GCM-SHA384" ), |
| 508 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", [204, 168], "ECDHE-RSA-CHACHA20-POLY1305" ), |
| 509 |
CipherInformation::new("TLS_ECDHE_RSA_WITH_RC4_128_SHA", [192, 17], "ECDHE-RSA-RC4-SHA" ), |
| 510 |
CipherInformation::new("TLS_NULL_WITH_NULL_NULL", [0, 0], "TLS_NULL_WITH_NULL_NULL" ), |
| 511 |
CipherInformation::new("TLS_RSA_WITH_3DES_EDE_CBC_SHA", [0, 10], "DES-CBC3-SHA" ), |
| 512 |
CipherInformation::new("TLS_RSA_WITH_AES_128_CBC_SHA", [0, 47], "AES128-SHA" ), |
| 513 |
CipherInformation::new("TLS_RSA_WITH_AES_128_CBC_SHA256", [0, 60], "AES128-SHA256" ), |
| 514 |
CipherInformation::new("TLS_RSA_WITH_AES_128_GCM_SHA256", [0, 156], "AES128-GCM-SHA256" ), |
| 515 |
CipherInformation::new("TLS_RSA_WITH_AES_256_CBC_SHA", [0, 53], "AES256-SHA" ), |
| 516 |
CipherInformation::new("TLS_RSA_WITH_AES_256_CBC_SHA256", [0, 61], "AES256-SHA256" ), |
| 517 |
CipherInformation::new("TLS_RSA_WITH_AES_256_GCM_SHA384", [0, 157], "AES256-GCM-SHA384" ), |
| 518 |
CipherInformation::new("TLS_RSA_WITH_RC4_128_MD5", [0, 4], "RC4-MD5" ), |
| 519 |
CipherInformation::new("TLS_RSA_WITH_RC4_128_SHA", [0, 5], "RC4-SHA"), |
| 520 |
]; |
| 521 |
|
| 522 |
pub const GROUPS_AVAILABLE_IN_S2N: [GroupInformation; 8] = [ |
| 523 |
GroupInformation::new("MLKEM1024", 514), |
| 524 |
GroupInformation::new("SecP256r1MLKEM768", 4587), |
| 525 |
GroupInformation::new("SecP384r1MLKEM1024", 4589), |
| 526 |
GroupInformation::new("X25519MLKEM768", 4588), |
| 527 |
GroupInformation::new("secp256r1", 23), |
| 528 |
GroupInformation::new("secp384r1", 24), |
| 529 |
GroupInformation::new("secp521r1", 25), |
| 530 |
GroupInformation::new("x25519", 29), |
| 531 |
]; |
| 532 |
|
| 533 |
pub const SIGNATURE_SCHEMES_AVAILABLE_IN_S2N: [SignatureSchemeInformation; 20] = [ |
| 534 |
SignatureSchemeInformation::new("ecdsa_sha1", 515), |
| 535 |
SignatureSchemeInformation::new("ecdsa_sha256", 1027), |
| 536 |
SignatureSchemeInformation::new("ecdsa_sha384", 1283), |
| 537 |
SignatureSchemeInformation::new("ecdsa_sha512", 1539), |
| 538 |
SignatureSchemeInformation::new("legacy_ecdsa_sha224", 771), |
| 539 |
SignatureSchemeInformation::new("legacy_rsa_md5_sha1", 65535), |
| 540 |
SignatureSchemeInformation::new("legacy_rsa_sha224", 769), |
| 541 |
SignatureSchemeInformation::new("mldsa44", 2308), |
| 542 |
SignatureSchemeInformation::new("mldsa65", 2309), |
| 543 |
SignatureSchemeInformation::new("mldsa87", 2310), |
| 544 |
SignatureSchemeInformation::new("rsa_pkcs1_sha1", 513), |
| 545 |
SignatureSchemeInformation::new("rsa_pkcs1_sha256", 1025), |
| 546 |
SignatureSchemeInformation::new("rsa_pkcs1_sha384", 1281), |
| 547 |
SignatureSchemeInformation::new("rsa_pkcs1_sha512", 1537), |
| 548 |
SignatureSchemeInformation::new("rsa_pss_pss_sha256", 2057), |
| 549 |
SignatureSchemeInformation::new("rsa_pss_pss_sha384", 2058), |
| 550 |
SignatureSchemeInformation::new("rsa_pss_pss_sha512", 2059), |
| 551 |
SignatureSchemeInformation::new("rsa_pss_rsae_sha256", 2052), |
| 552 |
SignatureSchemeInformation::new("rsa_pss_rsae_sha384", 2053), |
| 553 |
SignatureSchemeInformation::new("rsa_pss_rsae_sha512", 2054), |
| 554 |
]; |
| 555 |
|
| 556 |
|
| 557 |
#[derive(Debug, Clone, PartialEq, Eq, Copy, Serialize, Deserialize)] |
| 558 |
pub enum CertKeyType { |
| 559 |
Rsa1024, |
| 560 |
Rsa2048, |
| 561 |
Rsa3072, |
| 562 |
Rsa4096, |
| 563 |
RsaPss2048, |
| 564 |
RsaPss3072, |
| 565 |
RsaPss4096, |
| 566 |
Secp256r1, |
| 567 |
Secp384r1, |
| 568 |
Secp521r1, |
| 569 |
Unknown, |
| 570 |
} |
| 571 |
|
| 572 |
impl Display for CertKeyType { |
| 573 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 574 |
write!(f, "{self:?}") |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
pub const CERT_KEYS: [CertKeyType; 11] = [ |
| 579 |
CertKeyType::Rsa1024, |
| 580 |
CertKeyType::Rsa2048, |
| 581 |
CertKeyType::Rsa3072, |
| 582 |
CertKeyType::Rsa4096, |
| 583 |
CertKeyType::RsaPss2048, |
| 584 |
CertKeyType::RsaPss3072, |
| 585 |
CertKeyType::RsaPss4096, |
| 586 |
CertKeyType::Secp256r1, |
| 587 |
CertKeyType::Secp384r1, |
| 588 |
CertKeyType::Secp521r1, |
| 589 |
CertKeyType::Unknown, |
| 590 |
]; |
| 591 |
|
| 592 |
impl FiniteCounter<CERT_KEY_COUNT> for CertKeyType { |
| 593 |
const ELEMENTS: [CertKeyType; CERT_KEY_COUNT] = CERT_KEYS; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
#[derive(Debug, Clone, PartialEq, Eq, Copy, Serialize, Deserialize)] |
| 600 |
pub enum CertSignatureAlgorithm { |
| 601 |
RsaPkcsSha1, |
| 602 |
RsaPkcsSha256, |
| 603 |
RsaPkcsSha384, |
| 604 |
RsaPkcsSha512, |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
RsaPss, |
| 609 |
EcdsaSha256, |
| 610 |
EcdsaSha384, |
| 611 |
EcdsaSha512, |
| 612 |
Unknown, |
| 613 |
} |
| 614 |
|
| 615 |
impl Display for CertSignatureAlgorithm { |
| 616 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 617 |
write!(f, "{self:?}") |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
pub const CERT_SIGS: [CertSignatureAlgorithm; 9] = [ |
| 622 |
CertSignatureAlgorithm::RsaPkcsSha1, |
| 623 |
CertSignatureAlgorithm::RsaPkcsSha256, |
| 624 |
CertSignatureAlgorithm::RsaPkcsSha384, |
| 625 |
CertSignatureAlgorithm::RsaPkcsSha512, |
| 626 |
CertSignatureAlgorithm::RsaPss, |
| 627 |
CertSignatureAlgorithm::EcdsaSha256, |
| 628 |
CertSignatureAlgorithm::EcdsaSha384, |
| 629 |
CertSignatureAlgorithm::EcdsaSha512, |
| 630 |
CertSignatureAlgorithm::Unknown, |
| 631 |
]; |
| 632 |
|
| 633 |
impl FiniteCounter<CERT_SIG_COUNT> for CertSignatureAlgorithm { |
| 634 |
const ELEMENTS: [CertSignatureAlgorithm; CERT_SIG_COUNT] = CERT_SIGS; |
| 635 |
} |
| 636 |
|
| 637 |
#[cfg(test)] |
| 638 |
mod tests { |
| 639 |
use super::*; |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn display_fallback_for_unknown_elements() { |
| 643 |
assert_eq!(Cipher([0xFF, 0xFF]).to_string(), "unknown_cipher_0xffff"); |
| 644 |
assert_eq!( |
| 645 |
Version(U16::new(0x9999)).to_string(), |
| 646 |
"unknown_version_0x9999" |
| 647 |
); |
| 648 |
assert_eq!(Group(U16::new(0x9999)).to_string(), "unknown_group_0x9999"); |
| 649 |
assert_eq!( |
| 650 |
Signature(U16::new(0x9999)).to_string(), |
| 651 |
"unknown_signature_0x9999" |
| 652 |
); |
| 653 |
} |
| 654 |
|
| 655 |
#[test] |
| 656 |
fn cipher_constants_match() { |
| 657 |
let cases: &[(Cipher, &str)] = &[ |
| 658 |
(Cipher::TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256"), |
| 659 |
(Cipher::TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384"), |
| 660 |
( |
| 661 |
Cipher::TLS_CHACHA20_POLY1305_SHA256, |
| 662 |
"TLS_CHACHA20_POLY1305_SHA256", |
| 663 |
), |
| 664 |
( |
| 665 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 666 |
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", |
| 667 |
), |
| 668 |
( |
| 669 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 670 |
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", |
| 671 |
), |
| 672 |
( |
| 673 |
Cipher::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 674 |
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", |
| 675 |
), |
| 676 |
( |
| 677 |
Cipher::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 678 |
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", |
| 679 |
), |
| 680 |
]; |
| 681 |
for (constant, expected_name) in cases { |
| 682 |
assert_eq!(constant.known_description().unwrap(), *expected_name); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
#[test] |
| 687 |
fn group_constants_match() { |
| 688 |
let cases: &[(Group, &str)] = &[ |
| 689 |
(Group::x25519, "x25519"), |
| 690 |
(Group::secp256r1, "secp256r1"), |
| 691 |
(Group::secp384r1, "secp384r1"), |
| 692 |
(Group::secp521r1, "secp521r1"), |
| 693 |
(Group::SecP256r1MLKEM768, "SecP256r1MLKEM768"), |
| 694 |
(Group::X25519MLKEM768, "X25519MLKEM768"), |
| 695 |
(Group::SecP384r1MLKEM1024, "SecP384r1MLKEM1024"), |
| 696 |
(Group::MLKEM1024, "MLKEM1024"), |
| 697 |
]; |
| 698 |
for (constant, expected_name) in cases { |
| 699 |
assert_eq!(constant.known_description().unwrap(), *expected_name); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn signature_constants_match() { |
| 705 |
let cases: &[(Signature, &str)] = &[ |
| 706 |
(Signature::rsa_pss_rsae_sha256, "rsa_pss_rsae_sha256"), |
| 707 |
(Signature::rsa_pss_rsae_sha384, "rsa_pss_rsae_sha384"), |
| 708 |
(Signature::rsa_pss_rsae_sha512, "rsa_pss_rsae_sha512"), |
| 709 |
(Signature::rsa_pss_pss_sha256, "rsa_pss_pss_sha256"), |
| 710 |
(Signature::rsa_pss_pss_sha384, "rsa_pss_pss_sha384"), |
| 711 |
(Signature::rsa_pss_pss_sha512, "rsa_pss_pss_sha512"), |
| 712 |
(Signature::ecdsa_secp256r1_sha256, "ecdsa_sha256"), |
| 713 |
(Signature::ecdsa_secp384r1_sha384, "ecdsa_sha384"), |
| 714 |
(Signature::ecdsa_secp521r1_sha512, "ecdsa_sha512"), |
| 715 |
(Signature::mldsa44, "mldsa44"), |
| 716 |
(Signature::mldsa65, "mldsa65"), |
| 717 |
(Signature::mldsa87, "mldsa87"), |
| 718 |
]; |
| 719 |
for (constant, expected_name) in cases { |
| 720 |
assert_eq!(constant.known_description().unwrap(), *expected_name); |
| 721 |
} |
| 722 |
} |
| 723 |
} |