#[cfg(feature = "ring")]
use ring::digest::{self, Digest};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EndPointHash {
Sha256,
Sha384,
Sha512,
}
use EndPointHash::{Sha256, Sha384, Sha512};
#[cfg(feature = "ring")]
pub fn tls_server_end_point(certificate: &[u8]) -> Option<Digest> {
let algorithm = match tls_server_end_point_hash(certificate)? {
Sha256 => &digest::SHA256,
Sha384 => &digest::SHA384,
Sha512 => &digest::SHA512,
};
Some(digest::digest(algorithm, certificate))
}
pub fn tls_server_end_point_hash(certificate: &[u8]) -> Option<EndPointHash> {
signature_hash(certificate)
}
const RSA: &[u8] = b"\x2a\x86\x48\x86\xf7\x0d\x01\x01";
const ECDSA: &[u8] = b"\x2a\x86\x48\xce\x3d\x04\x03";
const SHA2: &[u8] = b"\x60\x86\x48\x01\x65\x03\x04\x02";
const SHA1: &[u8] = b"\x2b\x0e\x03\x02\x1a";
struct Der<'a>(&'a [u8]);
impl<'a> Der<'a> {
fn byte(&mut self) -> Option<u8> {
let (&first, rest) = self.0.split_first()?;
self.0 = rest;
Some(first)
}
fn value(&mut self, tag: u8) -> Option<&'a [u8]> {
if self.byte()? != tag {
return None;
}
let first = self.byte()?;
let len = if first < 128 {
first as usize
} else {
let count = (first & 127) as usize;
if count == 0 || count > size_of::<usize>() || self.0.first() == Some(&0) {
return None;
}
let mut len = 0usize;
for _ in 0..count {
len = len.checked_mul(256)?.checked_add(self.byte()? as usize)?;
}
if len < 128 {
return None;
}
len
};
let (value, rest) = self.0.split_at_checked(len)?;
self.0 = rest;
Some(value)
}
fn end(self) -> Option<()> {
self.0.is_empty().then_some(())
}
fn null_or_absent(self) -> Option<()> {
matches!(self.0, [] | [5, 0]).then_some(())
}
fn integer(&mut self) -> Option<usize> {
let value = self.value(2)?;
let first = *value.first()?;
if first & 128 != 0 || (value.len() > 1 && first == 0 && value[1] & 128 == 0) {
return None;
}
value
.iter()
.try_fold(0usize, |n, b| n.checked_mul(256)?.checked_add(*b as usize))
}
}
fn signature_hash(certificate: &[u8]) -> Option<EndPointHash> {
let mut der = Der(certificate);
let mut cert = Der(der.value(0x30)?);
der.end()?;
if cert.value(0x30)?.is_empty() {
return None;
}
let mut alg = Der(cert.value(0x30)?);
let oid = alg.value(6)?;
let signature = cert.value(3)?;
if signature.len() < 2 || signature[0] != 0 {
return None;
}
cert.end()?;
if let Some(suffix) = oid.strip_prefix(RSA) {
if suffix == [10] {
let params = alg.value(0x30)?;
alg.end()?;
return pss_hash(params);
}
alg.null_or_absent()?;
match suffix {
[4 | 5 | 11] => Some(Sha256), [12] => Some(Sha384),
[13] => Some(Sha512),
_ => None,
}
} else if let Some(suffix) = oid.strip_prefix(ECDSA) {
alg.end()?; match suffix {
[2] => Some(Sha256),
[3] => Some(Sha384),
[4] => Some(Sha512),
_ => None,
}
} else {
None
}
}
fn hash_identifier(bytes: &[u8]) -> Option<EndPointHash> {
let mut der = Der(bytes);
let mut alg = Der(der.value(0x30)?);
der.end()?;
let oid = alg.value(6)?;
alg.null_or_absent()?;
if oid == SHA1 {
return Some(Sha256);
}
match oid.strip_prefix(SHA2)? {
[1] => Some(Sha256),
[2] => Some(Sha384),
[3] => Some(Sha512),
_ => None,
}
}
fn pss_hash(parameters: &[u8]) -> Option<EndPointHash> {
let mut hash = Sha256;
let mut params = Der(parameters);
let mut previous = 0;
while let Some(&tag) = params.0.first() {
if !(0xa0..=0xa3).contains(&tag) || tag <= previous {
return None;
}
previous = tag;
let field = params.value(tag)?;
match tag {
0xa0 => hash = hash_identifier(field)?,
0xa1 => {
let mut der = Der(field);
let mut mgf = Der(der.value(0x30)?);
der.end()?;
if mgf.value(6)?.strip_prefix(RSA)? != [8] {
return None;
}
hash_identifier(mgf.0)?;
}
0xa2 | 0xa3 => {
let mut der = Der(field);
let integer = der.integer()?;
der.end()?;
if tag == 0xa3 && integer != 1 {
return None;
}
}
_ => return None,
}
}
Some(hash)
}