use alloc::vec::Vec;
use core::cmp;
use core::time::Duration;
use pki_types::{DnsName, UnixTime};
use zeroize::Zeroizing;
use crate::client::Tls13ClientSessionValue;
use crate::crypto::cipher::Payload;
use crate::crypto::{CipherSuite, Identity};
use crate::enums::{ApplicationProtocol, ProtocolVersion};
use crate::error::InvalidMessage;
use crate::msgs::base::{MaybeEmpty, SizedPayload};
use crate::msgs::codec::{Codec, Reader};
use crate::sync::Arc;
pub(crate) struct Retrieved<T> {
pub(crate) value: T,
retrieved_at: UnixTime,
}
impl<T> Retrieved<T> {
pub(crate) fn new(value: T, retrieved_at: UnixTime) -> Self {
Self {
value,
retrieved_at,
}
}
pub(crate) fn map<M>(&self, f: impl FnOnce(&T) -> Option<&M>) -> Option<Retrieved<&M>> {
Some(Retrieved {
value: f(&self.value)?,
retrieved_at: self.retrieved_at,
})
}
}
impl Retrieved<&Tls13ClientSessionValue> {
pub(crate) fn obfuscated_ticket_age(&self) -> u32 {
let age_secs = self
.retrieved_at
.as_secs()
.saturating_sub(self.value.common.epoch);
let age_millis = age_secs as u32 * 1000;
age_millis.wrapping_add(self.value.age_add)
}
}
impl<T: core::ops::Deref<Target = ClientSessionCommon>> Retrieved<T> {
pub(crate) fn has_expired(&self) -> bool {
let common = &*self.value;
common.lifetime != Duration::ZERO
&& common
.epoch
.saturating_add(common.lifetime.as_secs())
< self.retrieved_at.as_secs()
}
}
impl<T> core::ops::Deref for Retrieved<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
#[derive(Debug, Clone)]
pub struct ClientSessionCommon {
pub(crate) ticket: Arc<SizedPayload<'static, u16>>,
pub(crate) epoch: u64,
lifetime: Duration,
peer_identity: Arc<Identity<'static>>,
}
impl ClientSessionCommon {
pub(crate) fn new(
ticket: Arc<SizedPayload<'static, u16>>,
time_now: UnixTime,
lifetime: Duration,
peer_identity: Identity<'static>,
) -> Self {
Self {
ticket,
epoch: time_now.as_secs(),
lifetime: cmp::min(lifetime, MAX_TICKET_LIFETIME),
peer_identity: Arc::new(peer_identity),
}
}
pub(crate) fn peer_identity(&self) -> &Identity<'static> {
&self.peer_identity
}
pub(crate) fn ticket(&self) -> &[u8] {
(*self.ticket).bytes()
}
}
static MAX_TICKET_LIFETIME: Duration = Duration::from_secs(7 * 24 * 60 * 60);
static MAX_FRESHNESS_SKEW_MS: u32 = 60 * 1000;
#[non_exhaustive]
#[derive(Debug)]
pub enum ServerSessionValue {
Tls12(Tls12ServerSessionValue),
Tls13(Tls13ServerSessionValue),
}
impl Codec<'_> for ServerSessionValue {
fn encode(&self, bytes: &mut Vec<u8>) {
match self {
Self::Tls12(value) => {
ProtocolVersion::TLSv1_2.encode(bytes);
value.encode(bytes);
}
Self::Tls13(value) => {
ProtocolVersion::TLSv1_3.encode(bytes);
value.encode(bytes);
}
}
}
fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
match ProtocolVersion::read(r)? {
ProtocolVersion::TLSv1_2 => Ok(Self::Tls12(Tls12ServerSessionValue::read(r)?)),
ProtocolVersion::TLSv1_3 => Ok(Self::Tls13(Tls13ServerSessionValue::read(r)?)),
_ => Err(InvalidMessage::UnknownProtocolVersion),
}
}
}
#[derive(Debug)]
pub struct Tls12ServerSessionValue {
pub(crate) common: CommonServerSessionValue,
pub(crate) master_secret: Zeroizing<[u8; 48]>,
pub(crate) extended_ms: bool,
}
impl Tls12ServerSessionValue {
pub(crate) fn new(
common: CommonServerSessionValue,
master_secret: &[u8; 48],
extended_ms: bool,
) -> Self {
Self {
common,
master_secret: Zeroizing::new(*master_secret),
extended_ms,
}
}
}
impl Codec<'_> for Tls12ServerSessionValue {
fn encode(&self, bytes: &mut Vec<u8>) {
self.common.encode(bytes);
bytes.extend_from_slice(self.master_secret.as_ref());
(self.extended_ms as u8).encode(bytes);
}
fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
Ok(Self {
common: CommonServerSessionValue::read(r)?,
master_secret: Zeroizing::new(
match r
.take(48)
.and_then(|slice| slice.try_into().ok())
{
Some(array) => array,
None => return Err(InvalidMessage::MessageTooShort),
},
),
extended_ms: matches!(u8::read(r)?, 1),
})
}
}
impl From<Tls12ServerSessionValue> for ServerSessionValue {
fn from(value: Tls12ServerSessionValue) -> Self {
Self::Tls12(value)
}
}
#[derive(Debug)]
pub struct Tls13ServerSessionValue {
pub(crate) common: CommonServerSessionValue,
pub(crate) secret: Zeroizing<SizedPayload<'static, u8>>,
pub(crate) age_obfuscation_offset: u32,
freshness: Option<bool>,
}
impl Tls13ServerSessionValue {
pub(crate) fn new(
common: CommonServerSessionValue,
secret: &[u8],
age_obfuscation_offset: u32,
) -> Self {
Self {
common,
secret: Zeroizing::new(secret.to_vec().into()),
age_obfuscation_offset,
freshness: None,
}
}
pub(crate) fn set_freshness(
mut self,
obfuscated_client_age_ms: u32,
time_now: UnixTime,
) -> Self {
let client_age_ms = obfuscated_client_age_ms.wrapping_sub(self.age_obfuscation_offset);
let server_age_ms = (time_now
.as_secs()
.saturating_sub(self.common.creation_time_sec) as u32)
.saturating_mul(1000);
let age_difference = server_age_ms.abs_diff(client_age_ms);
self.freshness = Some(age_difference <= MAX_FRESHNESS_SKEW_MS);
self
}
pub(crate) fn is_fresh(&self) -> bool {
self.freshness.unwrap_or_default()
}
}
impl Codec<'_> for Tls13ServerSessionValue {
fn encode(&self, bytes: &mut Vec<u8>) {
self.common.encode(bytes);
self.secret.encode(bytes);
self.age_obfuscation_offset
.encode(bytes);
}
fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
Ok(Self {
common: CommonServerSessionValue::read(r)?,
secret: Zeroizing::new(SizedPayload::read(r)?.into_owned()),
age_obfuscation_offset: u32::read(r)?,
freshness: None,
})
}
}
impl From<Tls13ServerSessionValue> for ServerSessionValue {
fn from(value: Tls13ServerSessionValue) -> Self {
Self::Tls13(value)
}
}
#[derive(Debug)]
pub(crate) struct CommonServerSessionValue {
pub(crate) creation_time_sec: u64,
pub(crate) sni: Option<DnsName<'static>>,
pub(crate) cipher_suite: CipherSuite,
pub(crate) peer_identity: Option<Identity<'static>>,
pub(crate) alpn: Option<ApplicationProtocol<'static>>,
pub(crate) application_data: SizedPayload<'static, u16, MaybeEmpty>,
}
impl CommonServerSessionValue {
pub(crate) fn new(
sni: Option<&DnsName<'_>>,
cipher_suite: CipherSuite,
peer_identity: Option<Identity<'static>>,
alpn: Option<ApplicationProtocol<'static>>,
application_data: Vec<u8>,
creation_time: UnixTime,
) -> Self {
Self {
creation_time_sec: creation_time.as_secs(),
sni: sni.map(|s| s.to_owned()),
cipher_suite,
peer_identity,
alpn,
application_data: SizedPayload::from(Payload::new(application_data)),
}
}
pub(crate) fn can_resume(&self, suite: CipherSuite, sni: Option<&DnsName<'_>>) -> bool {
self.cipher_suite == suite && self.sni.as_ref() == sni
}
}
impl Codec<'_> for CommonServerSessionValue {
fn encode(&self, bytes: &mut Vec<u8>) {
self.creation_time_sec.encode(bytes);
if let Some(sni) = &self.sni {
1u8.encode(bytes);
let sni_bytes: &str = sni.as_ref();
SizedPayload::<u8, MaybeEmpty>::from(Payload::Borrowed(sni_bytes.as_bytes()))
.encode(bytes);
} else {
0u8.encode(bytes);
}
self.cipher_suite.encode(bytes);
if let Some(identity) = &self.peer_identity {
1u8.encode(bytes);
identity.encode(bytes);
} else {
0u8.encode(bytes);
}
if let Some(alpn) = &self.alpn {
1u8.encode(bytes);
alpn.encode(bytes);
} else {
0u8.encode(bytes);
}
self.application_data.encode(bytes);
}
fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
let creation_time_sec = u64::read(r)?;
let sni = match u8::read(r)? {
1 => {
let dns_name = SizedPayload::<u8, MaybeEmpty>::read(r)?;
let dns_name = match DnsName::try_from(dns_name.bytes()) {
Ok(dns_name) => dns_name.to_owned(),
Err(_) => return Err(InvalidMessage::InvalidServerName),
};
Some(dns_name)
}
_ => None,
};
Ok(Self {
creation_time_sec,
sni,
cipher_suite: CipherSuite::read(r)?,
peer_identity: match u8::read(r)? {
1 => Some(Identity::read(r)?.into_owned()),
_ => None,
},
alpn: match u8::read(r)? {
1 => Some(ApplicationProtocol::read(r)?.to_owned()),
_ => None,
},
application_data: SizedPayload::read(r)?.into_owned(),
})
}
}
#[cfg(test)]
mod tests {
use pki_types::CertificateDer;
use super::*;
use crate::crypto::CertificateIdentity;
#[cfg(feature = "std")] #[test]
fn serversessionvalue_is_debug() {
use std::{println, vec};
let ssv = ServerSessionValue::Tls13(Tls13ServerSessionValue::new(
CommonServerSessionValue::new(
None,
CipherSuite::TLS13_AES_128_GCM_SHA256,
None,
None,
vec![4, 5, 6],
UnixTime::now(),
),
&[1, 2, 3],
0x12345678,
));
println!("{ssv:?}");
println!("{:#04x?}", ssv.get_encoding());
}
#[test]
fn serversessionvalue_no_sni() {
let bytes = [
0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x69, 0x7a, 0x4a, 0xdf, 0x00, 0x13, 0x01, 0x00,
0x00, 0x00, 0x03, 0x04, 0x05, 0x06, 0x03, 0x01, 0x02, 0x03, 0x12, 0x34, 0x56, 0x78,
];
let mut rd = Reader::init(&bytes);
let ssv = ServerSessionValue::read(&mut rd).unwrap();
assert_eq!(ssv.get_encoding(), bytes);
}
#[test]
fn serversessionvalue_with_cert() {
std::eprintln!(
"{:#04x?}",
ServerSessionValue::Tls13(Tls13ServerSessionValue::new(
CommonServerSessionValue::new(
None,
CipherSuite::TLS13_AES_128_GCM_SHA256,
Some(Identity::X509(CertificateIdentity {
end_entity: CertificateDer::from(&[10, 11, 12][..]),
intermediates: alloc::vec![],
})),
None,
alloc::vec![4, 5, 6],
UnixTime::now(),
),
&[1, 2, 3],
0x12345678,
))
.get_encoding()
);
let bytes = [
0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x69, 0x7a, 0x4b, 0x06, 0x00, 0x13, 0x01, 0x01,
0x00, 0x00, 0x00, 0x03, 0x0a, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04,
0x05, 0x06, 0x03, 0x01, 0x02, 0x03, 0x12, 0x34, 0x56, 0x78,
];
let mut rd = Reader::init(&bytes);
let ssv = ServerSessionValue::read(&mut rd).unwrap();
assert_eq!(ssv.get_encoding(), bytes);
}
}