Skip to content

Commit

Permalink
reworked transport parameters, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilumary committed Aug 9, 2024
1 parent 79af137 commit 92242c1
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 234 deletions.
44 changes: 18 additions & 26 deletions project/src/quic/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use packet::{
};
use rand::RngCore;
use rustls::{
pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer},
pki_types::{CertificateDer, PrivateKeyDer},
quic::{Connection as RustlsConnection, KeyChange, Keys, PacketKeySet, Version},
Side,
};
Expand Down Expand Up @@ -349,29 +349,20 @@ impl Connection {
let initial_local_scid = ConnectionId::generate_with_length(8);
let orig_dcid = head.dcid.clone();

let mut transport_config = transport_parameters::TransportConfig::default();
transport_config
.original_destination_connection_id(orig_dcid.id())
.initial_source_connection_id(initial_local_scid.id())
.stateless_reset_token(
token::StatelessResetToken::new(&hmac_reset_key, &initial_local_scid)
.token
.to_vec(),
);

//Allocate byte buffer and encode transport config to create rustls connection
let mut buf = [0u8; 1024];
let mut param_buffer = OctetsMut::with_slice(&mut buf);
transport_config.encode(&mut param_buffer).unwrap();
let (data, _) = param_buffer.split_at(param_buffer.off()).unwrap();
let mut tpc = transport_parameters::TransportConfig::default();
tpc.original_destination_connection_id =
transport_parameters::OriginalDestinationConnectionId::try_from(orig_dcid.clone())?;
tpc.initial_source_connection_id =
transport_parameters::InitialSourceConnectionId::try_from(initial_local_scid.clone())?;
tpc.stateless_reset_token = transport_parameters::StatelessResetTokenTP::try_from(
token::StatelessResetToken::new(&hmac_reset_key, &initial_local_scid),
)?;

let data = tpc.encode(Side::Server)?;

let conn = RustlsConnection::Server(
rustls::quic::ServerConnection::new(
server_config,
rustls::quic::Version::V1,
data.to_vec(),
)
.unwrap(),
rustls::quic::ServerConnection::new(server_config, rustls::quic::Version::V1, data)
.unwrap(),
);

let initial_space: PacketNumberSpace = PacketNumberSpace {
Expand Down Expand Up @@ -602,10 +593,11 @@ impl Inner {
self.process_payload(header, packet_raw)?;

if let Some(tpc) = self.tls_session.quic_transport_parameters() {
self.remote_tpc.update(tpc);
self.remote_tpc.update(tpc).unwrap();
}

if self.remote_tpc.get_original_scid() != self.initial_remote_scid {
if *self.remote_tpc.initial_source_connection_id.get().unwrap() != self.initial_remote_scid
{
return Err(terror::Error::quic_protocol_violation(
"scids from packet header and transport parameters differ",
));
Expand Down Expand Up @@ -923,7 +915,7 @@ impl Inner {
.sort_by(|a, b| b.cmp(a));

//TODO figure out delay
let ack_delay = 64 * (2 ^ self.remote_tpc.ack_delay_exponent.as_varint());
let ack_delay = 64 * (2 ^ self.remote_tpc.ack_delay_exponent.get().unwrap().get());

//directly generate ack frame from packet number vector
let ack_frame = AckFrame::from_packet_number_vec(
Expand Down Expand Up @@ -993,7 +985,7 @@ impl PacketNumberSpace {
}

#[derive(Eq, Hash, PartialEq, Clone)]
struct ConnectionId {
pub struct ConnectionId {
id: Vec<u8>,
}

Expand Down
1 change: 1 addition & 0 deletions project/src/quic/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ pub fn varint_length(num: u64) -> usize {
0..=63 => 1,
64..=16383 => 2,
16384..=1073741823 => 3,
1073741824..=4611686018427387903 => 4,
_ => unreachable!("number exceeded abnormally large size"),
}
}
Expand Down
12 changes: 10 additions & 2 deletions project/src/quic/terror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ impl Error {
taurus_error!(crypto_error, 0x07);
taurus_error!(quic_protocol_violation, 0x0a);
taurus_error!(taurus_misc_error, 0xff);

pub fn quic_transport_error<T>(reason: T, code: QuicTransportError) -> Self
where
T: Into<String>,
{
Self {
code: code as u64,
msg: reason.into(),
}
}
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -66,7 +76,6 @@ pub enum QuicTransportError {
KeyUpdateError = 0x0e,
AeadLimitReached = 0x0f,
NoViablePath = 0x10,
CryptoError(CryptoError),
}

impl fmt::Display for QuicTransportError {
Expand All @@ -93,7 +102,6 @@ impl fmt::Display for QuicTransportError {
QuicTransportError::KeyUpdateError => write!(f, "0x0e key update error"),
QuicTransportError::AeadLimitReached => write!(f, "0x0f aead limit reached"),
QuicTransportError::NoViablePath => write!(f, "0x10 no viable path"),
QuicTransportError::CryptoError(c) => write!(f, "{} crypto error", c),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions project/src/quic/token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ConnectionId;

#[derive(PartialEq, Default)]
pub struct StatelessResetToken {
pub token: [u8; 0x10],
}
Expand Down
Loading

0 comments on commit 92242c1

Please sign in to comment.