| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
| |
| 28 |
| |
| 29 |
| |
| 30 |
| |
| 31 |
| |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
use s2n_codec::decoder::DecoderError; |
| 42 |
use s2n_tls_metrics_schema::static_lists::{CertKeyType, CertSignatureAlgorithm}; |
| 43 |
|
| 44 |
|
| 45 |
#[derive(Debug, PartialEq, Eq)] |
| 46 |
pub struct ParsedCertContent<'a> { |
| 47 |
|
| 48 |
pub serial: &'a [u8], |
| 49 |
|
| 50 |
pub issuer: &'a str, |
| 51 |
|
| 52 |
pub common_name: &'a str, |
| 53 |
pub key_type: CertKeyType, |
| 54 |
pub signature: CertSignatureAlgorithm, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct CertKeyWrapper(CertKeyType); |
| 61 |
|
| 62 |
mod der_codec { |
| 63 |
use crate::parsing::cert::CertKeyWrapper; |
| 64 |
use const_oid::ObjectIdentifier; |
| 65 |
use core::mem::size_of; |
| 66 |
use s2n_codec::{DecoderBuffer, DecoderBufferResult, DecoderError, DecoderValue}; |
| 67 |
|
| 68 |
|
| 69 |
const TAG_SEQUENCE: u8 = 0x30; |
| 70 |
const TAG_SET: u8 = 0x31; |
| 71 |
const TAG_INTEGER: u8 = 0x02; |
| 72 |
const TAG_OID: u8 = 0x06; |
| 73 |
const TAG_BIT_STRING: u8 = 0x03; |
| 74 |
const TAG_CONTEXT_0: u8 = 0xa0; |
| 75 |
) |
| 76 |
|
| 77 |
const TAG_UTF8_STRING: u8 = 0x0c; |
| 78 |
|
| 79 |
const TAG_PRINTABLE_STRING: u8 = 0x13; |
| 80 |
|
| 81 |
const TAG_IA5_STRING: u8 = 0x16; |
| 82 |
|
| 83 |
|
| 84 |
const OID_RSA_PKCS_SHA1: &[u8] = |
| 85 |
ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.5").as_bytes(); |
| 86 |
const OID_RSA_PKCS_SHA256: &[u8] = |
| 87 |
ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.11").as_bytes(); |
| 88 |
const OID_RSA_PKCS_SHA384: &[u8] = |
| 89 |
ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.12").as_bytes(); |
| 90 |
const OID_RSA_PKCS_SHA512: &[u8] = |
| 91 |
ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.13").as_bytes(); |
| 92 |
const OID_RSA_PSS: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.10").as_bytes(); |
| 93 |
const OID_ECDSA_SHA256: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2").as_bytes(); |
| 94 |
const OID_ECDSA_SHA384: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.3").as_bytes(); |
| 95 |
const OID_ECDSA_SHA512: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.4").as_bytes(); |
| 96 |
|
| 97 |
|
| 98 |
const OID_RSA_KEY: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1").as_bytes(); |
| 99 |
const OID_RSA_PSS_KEY: &[u8] = OID_RSA_PSS; |
| 100 |
const OID_EC_PUBLIC_KEY: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.10045.2.1").as_bytes(); |
| 101 |
|
| 102 |
|
| 103 |
const OID_SECP256R1: &[u8] = ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7").as_bytes(); |
| 104 |
const OID_SECP384R1: &[u8] = ObjectIdentifier::new_unwrap("1.3.132.0.34").as_bytes(); |
| 105 |
const OID_SECP521R1: &[u8] = ObjectIdentifier::new_unwrap("1.3.132.0.35").as_bytes(); |
| 106 |
|
| 107 |
|
| 108 |
const OID_CN: &[u8] = ObjectIdentifier::new_unwrap("2.5.4.3").as_bytes(); |
| 109 |
|
| 110 |
pub fn signature_from_oid(oid: &[u8]) -> super::CertSignatureAlgorithm { |
| 111 |
use super::CertSignatureAlgorithm; |
| 112 |
match oid { |
| 113 |
OID_RSA_PKCS_SHA1 => CertSignatureAlgorithm::RsaPkcsSha1, |
| 114 |
OID_RSA_PKCS_SHA256 => CertSignatureAlgorithm::RsaPkcsSha256, |
| 115 |
OID_RSA_PKCS_SHA384 => CertSignatureAlgorithm::RsaPkcsSha384, |
| 116 |
OID_RSA_PKCS_SHA512 => CertSignatureAlgorithm::RsaPkcsSha512, |
| 117 |
OID_RSA_PSS => CertSignatureAlgorithm::RsaPss, |
| 118 |
OID_ECDSA_SHA256 => CertSignatureAlgorithm::EcdsaSha256, |
| 119 |
OID_ECDSA_SHA384 => CertSignatureAlgorithm::EcdsaSha384, |
| 120 |
OID_ECDSA_SHA512 => CertSignatureAlgorithm::EcdsaSha512, |
| 121 |
_ => CertSignatureAlgorithm::Unknown, |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
127) |
| 129 |
es |
| 130 |
|
| 131 |
|
| 132 |
struct DerLength(usize); |
| 133 |
|
| 134 |
enum DerLengthEncoding { |
| 135 |
Short, |
| 136 |
Long, |
| 137 |
Indefinite, |
| 138 |
} |
| 139 |
|
| 140 |
impl DerLengthEncoding { |
| 141 |
const SENTINEL_VALUE: u8 = 0b10000000; |
| 142 |
const LONG_FORM_MASK: u8 = 0b01111111; |
| 143 |
fn from_first_byte(first_length_byte: u8) -> Self { |
| 144 |
if first_length_byte < Self::SENTINEL_VALUE { |
| 145 |
Self::Short |
| 146 |
} else if first_length_byte > Self::SENTINEL_VALUE { |
| 147 |
Self::Long |
| 148 |
} else { |
| 149 |
Self::Indefinite |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl<'a> DecoderValue<'a> for DerLength { |
| 155 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 156 |
let first = buffer.peek_byte(0)?; |
| 157 |
let encoding = DerLengthEncoding::from_first_byte(first); |
| 158 |
match encoding { |
| 159 |
DerLengthEncoding::Short => { |
| 160 |
let (length, buffer) = buffer.decode::<u8>()?; |
| 161 |
Ok((DerLength(length as usize), buffer)) |
| 162 |
} |
| 163 |
DerLengthEncoding::Long => { |
| 164 |
|
| 165 |
let (length_encoding_size, buffer) = { |
| 166 |
let (size, buffer) = buffer.decode::<u8>()?; |
| 167 |
let size = (size & DerLengthEncoding::LONG_FORM_MASK) as usize; |
| 168 |
if size > size_of::<usize>() { |
| 169 |
return Err(s2n_codec::decoder::DecoderError::LengthCapacityExceeded); |
| 170 |
} |
| 171 |
(size, buffer) |
| 172 |
}; |
| 173 |
let (length, buffer) = { |
| 174 |
let (len_bytes, buffer) = buffer.decode_slice(length_encoding_size)?; |
| 175 |
let raw = len_bytes.into_less_safe_slice(); |
| 176 |
let mut buf = [0u8; size_of::<usize>()]; |
| 177 |
let start = size_of::<usize>() |
| 178 |
.checked_sub(raw.len()) |
| 179 |
.ok_or(DecoderError::LengthCapacityExceeded)?; |
| 180 |
buf.get_mut(start..) |
| 181 |
.ok_or(DecoderError::LengthCapacityExceeded)? |
| 182 |
.copy_from_slice(raw); |
| 183 |
(usize::from_be_bytes(buf), buffer) |
| 184 |
}; |
| 185 |
. |
| 186 |
|
| 187 |
if length < DerLengthEncoding::SENTINEL_VALUE as usize { |
| 188 |
return Err(DecoderError::InvariantViolation( |
| 189 |
"non-minimal length encoding", |
| 190 |
)); |
| 191 |
} |
| 192 |
Ok((DerLength(length), buffer)) |
| 193 |
} |
| 194 |
DerLengthEncoding::Indefinite => { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
Err(DecoderError::InvariantViolation( |
| 200 |
"indefinite length encoding not allowed", |
| 201 |
)) |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
struct Tlv<'a> { |
| 215 |
tag: u8, |
| 216 |
content: &'a [u8], |
| 217 |
} |
| 218 |
|
| 219 |
impl<'a> DecoderValue<'a> for Tlv<'a> { |
| 220 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 221 |
let (tag, buffer) = buffer.decode::<u8>()?; |
| 222 |
const MULTI_BYTE_TAG_MASK: u8 = 0x1F; |
| 223 |
|
| 224 |
|
| 225 |
if tag & MULTI_BYTE_TAG_MASK == MULTI_BYTE_TAG_MASK { |
| 226 |
return Err(DecoderError::InvariantViolation( |
| 227 |
"multi-byte tags not supported", |
| 228 |
)); |
| 229 |
} |
| 230 |
let (DerLength(len), buffer) = buffer.decode::<DerLength>()?; |
| 231 |
let (content, buffer) = buffer.decode_slice(len)?; |
| 232 |
Ok(( |
| 233 |
Tlv { |
| 234 |
tag, |
| 235 |
content: content.into_less_safe_slice(), |
| 236 |
}, |
| 237 |
buffer, |
| 238 |
)) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
macro_rules! der_element { |
| 245 |
($name:ident, $tag:expr, $doc:expr) => { |
| 246 |
#[doc = $doc] |
| 247 |
pub struct $name<'a> { |
| 248 |
|
| 249 |
|
| 250 |
#[allow(dead_code)] |
| 251 |
pub content: &'a [u8], |
| 252 |
} |
| 253 |
|
| 254 |
impl<'a> DecoderValue<'a> for $name<'a> { |
| 255 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 256 |
let (tlv, buffer) = buffer.decode::<Tlv<'a>>()?; |
| 257 |
if tlv.tag != $tag { |
| 258 |
return Err(DecoderError::InvariantViolation(concat!( |
| 259 |
"expected ", |
| 260 |
stringify!($name) |
| 261 |
))); |
| 262 |
} |
| 263 |
Ok(( |
| 264 |
$name { |
| 265 |
content: tlv.content, |
| 266 |
}, |
| 267 |
buffer, |
| 268 |
)) |
| 269 |
} |
| 270 |
} |
| 271 |
}; |
| 272 |
} |
| 273 |
|
| 274 |
der_element!(DerSequence, TAG_SEQUENCE, "A DER SEQUENCE element."); |
| 275 |
der_element!(DerSet, TAG_SET, "A DER SET element."); |
| 276 |
der_element!(DerInteger, TAG_INTEGER, "A DER INTEGER element."); |
| 277 |
der_element!(DerOid, TAG_OID, "A DER OID element."); |
| 278 |
der_element!(DerContext0, TAG_CONTEXT_0, "A DER [0] EXPLICIT element."); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
ow |
| 283 |
|
| 284 |
pub struct DerBitString<'a> { |
| 285 |
pub unused_bits: u8, |
| 286 |
pub content: &'a [u8], |
| 287 |
} |
| 288 |
|
| 289 |
impl<'a> DecoderValue<'a> for DerBitString<'a> { |
| 290 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 291 |
let (tlv, buffer) = buffer.decode::<Tlv<'a>>()?; |
| 292 |
if tlv.tag != TAG_BIT_STRING { |
| 293 |
return Err(DecoderError::InvariantViolation("expected DerBitString")); |
| 294 |
} |
| 295 |
let (&unused_bits, content) = |
| 296 |
tlv.content |
| 297 |
.split_first() |
| 298 |
.ok_or(DecoderError::InvariantViolation( |
| 299 |
"BIT STRING content too short", |
| 300 |
))?; |
| 301 |
Ok(( |
| 302 |
DerBitString { |
| 303 |
unused_bits, |
| 304 |
content, |
| 305 |
}, |
| 306 |
buffer, |
| 307 |
)) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
pub struct DerUtf8ishString<'a> { |
| 315 |
pub content: &'a str, |
| 316 |
} |
| 317 |
|
| 318 |
impl<'a> DecoderValue<'a> for DerUtf8ishString<'a> { |
| 319 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 320 |
let (tlv, buffer) = buffer.decode::<Tlv<'a>>()?; |
| 321 |
match tlv.tag { |
| 322 |
|
| 323 |
TAG_UTF8_STRING | TAG_PRINTABLE_STRING | TAG_IA5_STRING => {} |
| 324 |
_ => { |
| 325 |
return Err(DecoderError::InvariantViolation( |
| 326 |
"expected UTF-8 string tag", |
| 327 |
)); |
| 328 |
} |
| 329 |
} |
| 330 |
let content = core::str::from_utf8(tlv.content) |
| 331 |
.map_err(|_| DecoderError::InvariantViolation("invalid utf8"))?; |
| 332 |
Ok((DerUtf8ishString { content }, buffer)) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
fn decode_oid<'a>(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, &'a [u8]> { |
| 338 |
let (oid, buffer) = buffer.decode::<DerOid<'a>>()?; |
| 339 |
Ok((oid.content, buffer)) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
struct RsaPublicKey<'a> { |
| 345 |
modulus: &'a [u8], |
| 346 |
} |
| 347 |
|
| 348 |
impl<'a> DecoderValue<'a> for RsaPublicKey<'a> { |
| 349 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 350 |
|
| 351 |
let (bits, buffer) = buffer.decode::<DerBitString<'a>>()?; |
| 352 |
if bits.unused_bits != 0 { |
| 353 |
return Err(DecoderError::InvariantViolation( |
| 354 |
"BIT STRING has non-zero unused bits", |
| 355 |
)); |
| 356 |
} |
| 357 |
|
| 358 |
let (seq, _) = DecoderBuffer::new(bits.content).decode::<DerSequence<'_>>()?; |
| 359 |
|
| 360 |
let (modulus_int, _) = DecoderBuffer::new(seq.content).decode::<DerInteger<'_>>()?; |
| 361 |
let modulus = modulus_int.content; |
| 362 |
|
| 363 |
let modulus = if modulus.first() == Some(&0x00) { |
| 364 |
modulus.get(1..).unwrap_or(modulus) |
| 365 |
} else { |
| 366 |
modulus |
| 367 |
}; |
| 368 |
Ok((RsaPublicKey { modulus }, buffer)) |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
impl<'a> DecoderValue<'a> for CertKeyWrapper { |
| 374 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 375 |
use super::CertKeyType; |
| 376 |
|
| 377 |
|
| 378 |
let (key_alg, rest) = buffer.decode::<DerSequence<'a>>()?; |
| 379 |
let (key_oid, key_alg_rest) = decode_oid(DecoderBuffer::new(key_alg.content))?; |
| 380 |
|
| 381 |
match key_oid { |
| 382 |
OID_EC_PUBLIC_KEY => { |
| 383 |
let (curve_oid, _) = decode_oid(key_alg_rest)?; |
| 384 |
let key_type = match curve_oid { |
| 385 |
OID_SECP256R1 => CertKeyType::Secp256r1, |
| 386 |
OID_SECP384R1 => CertKeyType::Secp384r1, |
| 387 |
OID_SECP521R1 => CertKeyType::Secp521r1, |
| 388 |
_ => CertKeyType::Unknown, |
| 389 |
}; |
| 390 |
Ok((CertKeyWrapper(key_type), rest)) |
| 391 |
} |
| 392 |
OID_RSA_KEY | OID_RSA_PSS_KEY => { |
| 393 |
let (rsa_key, buffer) = rest.decode::<RsaPublicKey<'_>>()?; |
| 394 |
let is_pss = key_oid == OID_RSA_PSS_KEY; |
| 395 |
|
| 396 |
let key_type = match (is_pss, rsa_key.modulus.len() * 8) { |
| 397 |
(false, 1024) => CertKeyType::Rsa1024, |
| 398 |
(false, 2048) => CertKeyType::Rsa2048, |
| 399 |
(false, 3072) => CertKeyType::Rsa3072, |
| 400 |
(false, 4096) => CertKeyType::Rsa4096, |
| 401 |
(true, 2048) => CertKeyType::RsaPss2048, |
| 402 |
(true, 3072) => CertKeyType::RsaPss3072, |
| 403 |
(true, 4096) => CertKeyType::RsaPss4096, |
| 404 |
_ => CertKeyType::Unknown, |
| 405 |
}; |
| 406 |
Ok((CertKeyWrapper(key_type), buffer)) |
| 407 |
} |
| 408 |
_ => Ok((CertKeyWrapper(CertKeyType::Unknown), rest)), |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
fn decode_common_name(content: &[u8]) -> Result<&str, DecoderError> { |
| 419 |
let mut buffer = DecoderBuffer::new(content); |
| 420 |
while !buffer.is_empty() { |
| 421 |
let (set, rest) = buffer.decode::<DerSet<'_>>()?; |
| 422 |
let mut set_buf = DecoderBuffer::new(set.content); |
| 423 |
while !set_buf.is_empty() { |
| 424 |
let (attr, set_rest) = set_buf.decode::<DerSequence<'_>>()?; |
| 425 |
let (oid, val_buf) = DecoderBuffer::new(attr.content).decode::<DerOid<'_>>()?; |
| 426 |
if oid.content == OID_CN { |
| 427 |
let (cn, _) = val_buf.decode::<DerUtf8ishString<'_>>()?; |
| 428 |
return Ok(cn.content); |
| 429 |
} |
| 430 |
set_buf = set_rest; |
| 431 |
} |
| 432 |
buffer = rest; |
| 433 |
} |
| 434 |
Ok("") |
| 435 |
} |
| 436 |
|
| 437 |
impl<'a> DecoderValue<'a> for super::ParsedCertContent<'a> { |
| 438 |
fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self> { |
| 439 |
|
| 440 |
let (cert_seq, _buffer) = buffer.decode::<DerSequence<'a>>()?; |
| 441 |
|
| 442 |
|
| 443 |
let (tbs, _) = DecoderBuffer::new(cert_seq.content).decode::<DerSequence<'a>>()?; |
| 444 |
let mut buffer = DecoderBuffer::new(tbs.content); |
| 445 |
|
| 446 |
|
| 447 |
if buffer.peek_byte(0)? == TAG_CONTEXT_0 { |
| 448 |
let (_, b) = buffer.decode::<DerContext0<'a>>()?; |
| 449 |
buffer = b; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
let (serial, buffer) = buffer.decode::<DerInteger<'a>>()?; |
| 454 |
|
| 455 |
|
| 456 |
let (sig_alg, buffer) = buffer.decode::<DerSequence<'a>>()?; |
| 457 |
let (sig_oid, _) = decode_oid(DecoderBuffer::new(sig_alg.content))?; |
| 458 |
|
| 459 |
|
| 460 |
let (issuer, buffer) = buffer.decode::<DerSequence<'a>>()?; |
| 461 |
|
| 462 |
|
| 463 |
let (_, buffer) = buffer.decode::<DerSequence<'a>>()?; |
| 464 |
|
| 465 |
|
| 466 |
let (subject, buffer) = buffer.decode::<DerSequence<'a>>()?; |
| 467 |
|
| 468 |
|
| 469 |
let (spki, _) = buffer.decode::<DerSequence<'a>>()?; |
| 470 |
let (key_type, _) = DecoderBuffer::new(spki.content).decode::<CertKeyWrapper>()?; |
| 471 |
|
| 472 |
Ok(( |
| 473 |
super::ParsedCertContent { |
| 474 |
serial: serial.content, |
| 475 |
issuer: decode_common_name(issuer.content)?, |
| 476 |
common_name: decode_common_name(subject.content)?, |
| 477 |
key_type: key_type.0, |
| 478 |
signature: signature_from_oid(sig_oid), |
| 479 |
}, |
| 480 |
DecoderBuffer::new(&[]), |
| 481 |
)) |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
pub fn parse(der: &[u8]) -> Result<ParsedCertContent<'_>, DecoderError> { |
| 488 |
let buf = s2n_codec::DecoderBuffer::new(der); |
| 489 |
let (parsed, _) = buf.decode::<ParsedCertContent>()?; |
| 490 |
Ok(parsed) |
| 491 |
} |
| 492 |
|
| 493 |
#[cfg(test)] |
| 494 |
mod tests { |
| 495 |
use super::*; |
| 496 |
use s2n_tls::testing::{CertKeyPair, TestPair}; |
| 497 |
|
| 498 |
|
| 499 |
fn handshake_leaf_der(prefix: &str) -> Vec<u8> { |
| 500 |
let cert = CertKeyPair::from_path(prefix, "cert", "key", "cert"); |
| 501 |
let policy = s2n_tls::security::DEFAULT_TLS13; |
| 502 |
let mut builder = s2n_tls::config::Builder::new(); |
| 503 |
builder.set_security_policy(&policy).unwrap(); |
| 504 |
builder.load_pem(cert.cert(), cert.key()).unwrap(); |
| 505 |
builder.trust_pem(cert.cert()).unwrap(); |
| 506 |
builder |
| 507 |
.set_verify_host_callback(s2n_tls::testing::InsecureAcceptAllCertificatesHandler {}) |
| 508 |
.unwrap(); |
| 509 |
builder.with_system_certs(false).unwrap(); |
| 510 |
let config = builder.build().unwrap(); |
| 511 |
let mut pair = TestPair::from_config(&config); |
| 512 |
pair.handshake().unwrap(); |
| 513 |
let chain = pair.server.selected_cert().unwrap(); |
| 514 |
chain |
| 515 |
.iter() |
| 516 |
.next() |
| 517 |
.unwrap() |
| 518 |
.unwrap() |
| 519 |
.der() |
| 520 |
.unwrap() |
| 521 |
.to_vec() |
| 522 |
} |
| 523 |
|
| 524 |
const S2N_LOCALHOST: &str = "localhost"; |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
#[test] |
| 529 |
fn s2n_test_certs() { |
| 530 |
let cases: &[(&str, &[u8], CertKeyType, CertSignatureAlgorithm)] = &[ |
| 531 |
( |
| 532 |
"rsa_2048_sha256_client_", |
| 533 |
&[0x00, 0xa9, 0xea, 0x92, 0x92, 0x5c, 0x65, 0x56, 0x34], |
| 534 |
CertKeyType::Rsa2048, |
| 535 |
CertSignatureAlgorithm::RsaPkcsSha256, |
| 536 |
), |
| 537 |
( |
| 538 |
"rsa_2048_sha384_client_", |
| 539 |
&[0x00, 0xf5, 0x20, 0xe0, 0xfd, 0x51, 0xdd, 0xcb, 0x40], |
| 540 |
CertKeyType::Rsa2048, |
| 541 |
CertSignatureAlgorithm::RsaPkcsSha384, |
| 542 |
), |
| 543 |
( |
| 544 |
"rsa_4096_sha512_client_", |
| 545 |
&[0x00, 0xda, 0x54, 0x50, 0xbd, 0xeb, 0x60, 0xcb, 0x7d], |
| 546 |
CertKeyType::Rsa4096, |
| 547 |
CertSignatureAlgorithm::RsaPkcsSha512, |
| 548 |
), |
| 549 |
( |
| 550 |
"ecdsa_p256_pkcs1_", |
| 551 |
&[ |
| 552 |
0x3d, 0x86, 0x04, 0x9c, 0xad, 0xb8, 0xa8, 0x3c, 0xf3, 0xe7, 0xd2, 0x08, 0x0d, |
| 553 |
0xc3, 0x4b, 0x73, 0x83, 0xf6, 0x1f, 0x9b, |
| 554 |
], |
| 555 |
CertKeyType::Secp256r1, |
| 556 |
CertSignatureAlgorithm::EcdsaSha256, |
| 557 |
), |
| 558 |
( |
| 559 |
"ecdsa_p384_pkcs1_", |
| 560 |
&[ |
| 561 |
0x33, 0x15, 0x1a, 0x7b, 0xe6, 0xb3, 0x75, 0xad, 0x4c, 0x49, 0x9d, 0xde, 0xb1, |
| 562 |
0xc2, 0x5f, 0x25, 0x36, 0x70, 0x45, 0xa9, |
| 563 |
], |
| 564 |
CertKeyType::Secp384r1, |
| 565 |
CertSignatureAlgorithm::EcdsaSha256, |
| 566 |
), |
| 567 |
( |
| 568 |
"localhost_rsa_pss_2048_sha256_", |
| 569 |
&[ |
| 570 |
0x31, 0x94, 0xe2, 0x4a, 0xc2, 0x96, 0xdc, 0xe9, 0x94, 0x3d, 0xfd, 0x67, 0xc4, |
| 571 |
0xa8, 0x94, 0x52, 0x05, 0xc2, 0x77, 0x44, |
| 572 |
], |
| 573 |
CertKeyType::RsaPss2048, |
| 574 |
CertSignatureAlgorithm::RsaPss, |
| 575 |
), |
| 576 |
]; |
| 577 |
|
| 578 |
for (prefix, serial, key_type, signature) in cases { |
| 579 |
let der = handshake_leaf_der(prefix); |
| 580 |
let expected = ParsedCertContent { |
| 581 |
serial, |
| 582 |
issuer: S2N_LOCALHOST, |
| 583 |
common_name: S2N_LOCALHOST, |
| 584 |
key_type: *key_type, |
| 585 |
signature: *signature, |
| 586 |
}; |
| 587 |
assert_eq!(parse(&der).unwrap(), expected, "failed for {prefix}"); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
#[test] |
| 594 |
fn sqs_cert_chain() { |
| 595 |
const SQS_LEAF: &[u8] = include_bytes!(concat!( |
| 596 |
env!("CARGO_MANIFEST_DIR"), |
| 597 |
"/resources/test_certs/sqs_leaf.der" |
| 598 |
)); |
| 599 |
const SQS_INTERMEDIATE: &[u8] = include_bytes!(concat!( |
| 600 |
env!("CARGO_MANIFEST_DIR"), |
| 601 |
"/resources/test_certs/sqs_intermediate.der" |
| 602 |
)); |
| 603 |
const SQS_ROOT: &[u8] = include_bytes!(concat!( |
| 604 |
env!("CARGO_MANIFEST_DIR"), |
| 605 |
"/resources/test_certs/sqs_root.der" |
| 606 |
)); |
| 607 |
|
| 608 |
assert_eq!( |
| 609 |
parse(SQS_LEAF).unwrap(), |
| 610 |
ParsedCertContent { |
| 611 |
serial: &[ |
| 612 |
0x06, 0xb1, 0xde, 0xc6, 0x59, 0x3a, 0x5f, 0x5d, 0x52, 0xcc, 0xce, 0x05, 0x13, |
| 613 |
0x23, 0x8d, 0x1c, |
| 614 |
], |
| 615 |
issuer: "Amazon RSA 2048 M04", |
| 616 |
common_name: "sqs.us-east-2.amazonaws.com", |
| 617 |
key_type: CertKeyType::Rsa2048, |
| 618 |
signature: CertSignatureAlgorithm::RsaPkcsSha256, |
| 619 |
} |
| 620 |
); |
| 621 |
|
| 622 |
assert_eq!( |
| 623 |
parse(SQS_INTERMEDIATE).unwrap(), |
| 624 |
ParsedCertContent { |
| 625 |
serial: &[ |
| 626 |
0x07, 0x73, 0x12, 0x4f, 0x2a, 0x95, 0x2e, 0x3e, 0xd1, 0x8a, 0x58, 0xbd, 0xb8, |
| 627 |
0x5d, 0x1b, 0xc0, 0xce, 0x5f, 0x27, |
| 628 |
], |
| 629 |
issuer: "Amazon Root CA 1", |
| 630 |
common_name: "Amazon RSA 2048 M04", |
| 631 |
key_type: CertKeyType::Rsa2048, |
| 632 |
signature: CertSignatureAlgorithm::RsaPkcsSha256, |
| 633 |
} |
| 634 |
); |
| 635 |
|
| 636 |
assert_eq!( |
| 637 |
parse(SQS_ROOT).unwrap(), |
| 638 |
ParsedCertContent { |
| 639 |
serial: &[ |
| 640 |
0x06, 0x7f, 0x94, 0x4a, 0x2a, 0x27, 0xcd, 0xf3, 0xfa, 0xc2, 0xae, 0x2b, 0x01, |
| 641 |
0xf9, 0x08, 0xee, 0xb9, 0xc4, 0xc6, |
| 642 |
], |
| 643 |
issuer: "Starfield Services Root Certificate Authority - G2", |
| 644 |
common_name: "Amazon Root CA 1", |
| 645 |
key_type: CertKeyType::Rsa2048, |
| 646 |
signature: CertSignatureAlgorithm::RsaPkcsSha256, |
| 647 |
} |
| 648 |
); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
#[test] |
| 657 |
fn ed25519_unknown_key_type() { |
| 658 |
assert_eq!( |
| 659 |
parse(include_bytes!(concat!( |
| 660 |
env!("CARGO_MANIFEST_DIR"), |
| 661 |
"/resources/test_certs/ed25519_cert.der" |
| 662 |
))) |
| 663 |
.unwrap(), |
| 664 |
ParsedCertContent { |
| 665 |
serial: &[ |
| 666 |
0x5b, 0x9d, 0xf7, 0x74, 0x5d, 0x46, 0x4e, 0xaf, 0x5f, 0x71, 0x9a, 0xb9, 0xa1, |
| 667 |
0xb9, 0x55, 0xf9, 0xfe, 0x8b, 0x71, 0x59, |
| 668 |
], |
| 669 |
issuer: "localhost", |
| 670 |
common_name: "localhost", |
| 671 |
key_type: CertKeyType::Unknown, |
| 672 |
signature: CertSignatureAlgorithm::Unknown, |
| 673 |
} |
| 674 |
); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn error_cases() { |
| 679 |
|
| 680 |
assert!(parse(&[0xff, 0x00]).is_err()); |
| 681 |
|
| 682 |
|
| 683 |
assert!(parse(&[]).is_err()); |
| 684 |
|
| 685 |
|
| 686 |
let der = handshake_leaf_der("rsa_2048_sha256_client_"); |
| 687 |
assert!(parse(&der[..der.len() / 2]).is_err()); |
| 688 |
} |
| 689 |
} |