From 441560647de35f4f682f7dc8bde851deecedfd5e Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sun, 10 Oct 2021 15:09:38 -0700 Subject: [PATCH] Erase crypto::Session::PacketKey --- quinn-proto/src/connection/datagrams.rs | 2 +- quinn-proto/src/connection/mod.rs | 13 +++++-------- quinn-proto/src/connection/packet_builder.rs | 6 +++--- quinn-proto/src/crypto.rs | 8 +++----- quinn-proto/src/crypto/rustls.rs | 19 +++++++++---------- quinn-proto/src/endpoint.rs | 9 +++------ quinn-proto/src/packet.rs | 13 ++++++++----- 7 files changed, 32 insertions(+), 38 deletions(-) diff --git a/quinn-proto/src/connection/datagrams.rs b/quinn-proto/src/connection/datagrams.rs index ddecbb50f..903f2140d 100644 --- a/quinn-proto/src/connection/datagrams.rs +++ b/quinn-proto/src/connection/datagrams.rs @@ -6,7 +6,7 @@ use tracing::{debug, trace}; use super::Connection; use crate::{ - crypto::{PacketKey, Session}, + crypto::Session, frame::{Datagram, FrameStruct}, packet::SpaceId, TransportError, diff --git a/quinn-proto/src/connection/mod.rs b/quinn-proto/src/connection/mod.rs index 38b7accc9..b8e568bb3 100644 --- a/quinn-proto/src/connection/mod.rs +++ b/quinn-proto/src/connection/mod.rs @@ -163,12 +163,12 @@ where /// Highest usable packet number space highest_space: SpaceId, /// 1-RTT keys used prior to a key update - prev_crypto: Option>, + prev_crypto: Option, /// 1-RTT keys to be used for the next key update /// /// These are generated in advance to prevent timing attacks and/or DoS by third-party attackers /// spoofing key updates. - next_crypto: Option>, + next_crypto: Option>>, accepted_0rtt: bool, /// Whether the idle timer should be reset the next time an ack-eliciting packet is transmitted. permit_idle_reset: bool, @@ -3165,13 +3165,10 @@ mod state { } } -struct PrevCrypto -where - K: crypto::PacketKey, -{ +struct PrevCrypto { /// The keys used for the previous key phase, temporarily retained to decrypt packets sent by /// the peer prior to its own key update. - crypto: KeyPair, + crypto: KeyPair>, /// The incoming packet that ends the interval for which these keys are applicable, and the time /// of its receipt. /// @@ -3258,7 +3255,7 @@ const MAX_TRANSMIT_SEGMENTS: usize = 10; struct ZeroRttCrypto { header: S::HeaderKey, - packet: S::PacketKey, + packet: Box, } #[derive(Default)] diff --git a/quinn-proto/src/connection/packet_builder.rs b/quinn-proto/src/connection/packet_builder.rs index d9a346c9f..a21c93052 100644 --- a/quinn-proto/src/connection/packet_builder.rs +++ b/quinn-proto/src/connection/packet_builder.rs @@ -6,7 +6,7 @@ use tracing::{trace, trace_span}; use super::{spaces::SentPacket, Connection, SentFrames, State}; use crate::{ - crypto::{HeaderKey, PacketKey, Session}, + crypto::{HeaderKey, Session}, frame::{self, Close}, packet::{Header, LongType, PacketNumber, PartialEncode, SpaceId}, TransportError, TransportErrorCode, @@ -217,10 +217,10 @@ impl PacketBuilder { let space = &conn.spaces[self.space]; let (header_crypto, packet_crypto) = if let Some(ref crypto) = space.crypto { - (&crypto.header.local, &crypto.packet.local) + (&crypto.header.local, &*crypto.packet.local) } else if self.space == SpaceId::Data { let zero_rtt = conn.zero_rtt_crypto.as_ref().unwrap(); - (&zero_rtt.header, &zero_rtt.packet) + (&zero_rtt.header, &*zero_rtt.packet) } else { unreachable!("tried to send {:?} packet without keys", self.space); }; diff --git a/quinn-proto/src/crypto.rs b/quinn-proto/src/crypto.rs index 3863f03ba..789edee2b 100644 --- a/quinn-proto/src/crypto.rs +++ b/quinn-proto/src/crypto.rs @@ -33,8 +33,6 @@ pub trait Session: Send + Sized { type ClientConfig: ClientConfig; /// Type of keys used to protect packet headers type HeaderKey: HeaderKey; - /// Type used to represent packet protection keys - type PacketKey: PacketKey; /// Type used to hold configuration for server sessions type ServerConfig: ServerConfig; @@ -56,7 +54,7 @@ pub trait Session: Send + Sized { /// /// Returns `None` if the key material is not available. This might happen if you have /// not connected to this server before. - fn early_crypto(&self) -> Option<(Self::HeaderKey, Self::PacketKey)>; + fn early_crypto(&self) -> Option<(Self::HeaderKey, Box)>; /// If the 0-RTT-encrypted data has been accepted by the peer fn early_data_accepted(&self) -> Option; @@ -85,7 +83,7 @@ pub trait Session: Send + Sized { fn write_handshake(&mut self, buf: &mut Vec) -> Option>; /// Compute keys for the next key update - fn next_1rtt_keys(&mut self) -> Option>; + fn next_1rtt_keys(&mut self) -> Option>>; /// Generate the integrity tag for a retry packet fn retry_tag(orig_dst_cid: &ConnectionId, packet: &[u8]) -> [u8; 16]; @@ -123,7 +121,7 @@ where /// Header protection keys pub header: KeyPair, /// Packet protection keys - pub packet: KeyPair, + pub packet: KeyPair>, } /// Client-side configuration for the crypto protocol diff --git a/quinn-proto/src/crypto/rustls.rs b/quinn-proto/src/crypto/rustls.rs index 59c46f1cd..afcf503b1 100644 --- a/quinn-proto/src/crypto/rustls.rs +++ b/quinn-proto/src/crypto/rustls.rs @@ -37,7 +37,6 @@ impl TlsSession { impl crypto::Session for TlsSession { type ClientConfig = Arc; - type PacketKey = PacketKey; type HeaderKey = HeaderProtectionKey; type ServerConfig = Arc; @@ -49,8 +48,8 @@ impl crypto::Session for TlsSession { remote: keys.remote.header, }, packet: KeyPair { - local: keys.local.packet, - remote: keys.remote.packet, + local: Box::new(keys.local.packet), + remote: Box::new(keys.remote.packet), }, } } @@ -74,9 +73,9 @@ impl crypto::Session for TlsSession { .map(|v| -> Box { Box::new(CertificateChain::from(v.to_vec())) }) } - fn early_crypto(&self) -> Option<(Self::HeaderKey, Self::PacketKey)> { + fn early_crypto(&self) -> Option<(Self::HeaderKey, Box)> { let keys = self.inner.zero_rtt_keys()?; - Some((keys.header, keys.packet)) + Some((keys.header, Box::new(keys.packet))) } fn early_data_accepted(&self) -> Option { @@ -151,18 +150,18 @@ impl crypto::Session for TlsSession { remote: keys.remote.header, }, packet: KeyPair { - local: keys.local.packet, - remote: keys.remote.packet, + local: Box::new(keys.local.packet), + remote: Box::new(keys.remote.packet), }, }) } - fn next_1rtt_keys(&mut self) -> Option> { + fn next_1rtt_keys(&mut self) -> Option>> { let secrets = self.next_secrets.as_mut()?; let keys = secrets.next_packet_keys(); Some(KeyPair { - local: keys.local, - remote: keys.remote, + local: Box::new(keys.local), + remote: Box::new(keys.remote), }) } diff --git a/quinn-proto/src/endpoint.rs b/quinn-proto/src/endpoint.rs index 5b2e0aa01..c40ab0c19 100644 --- a/quinn-proto/src/endpoint.rs +++ b/quinn-proto/src/endpoint.rs @@ -20,10 +20,7 @@ use crate::{ coding::BufMutExt, config::{ClientConfig, EndpointConfig, ServerConfig}, connection::{Connection, ConnectionError}, - crypto::{ - self, ClientConfig as ClientCryptoConfig, Keys, PacketKey, - ServerConfig as ServerCryptoConfig, - }, + crypto::{self, ClientConfig as ClientCryptoConfig, Keys, ServerConfig as ServerCryptoConfig}, frame, packet::{Header, Packet, PacketDecodeError, PacketNumber, PartialDecode}, shared::{ @@ -605,7 +602,7 @@ where let encode = header.encode(&mut buf); buf.put_slice(&token); buf.extend_from_slice(&S::retry_tag(&dst_cid, &buf)); - encode.finish::(&mut buf, &crypto.header.local, None); + encode.finish::(&mut buf, &crypto.header.local, None); self.transmits.push_back(Transmit { destination: remote, @@ -700,7 +697,7 @@ where partial_encode.finish( &mut buf, &crypto.header.local, - Some((0, &crypto.packet.local)), + Some((0, &*crypto.packet.local)), ); self.transmits.push_back(Transmit { destination, diff --git a/quinn-proto/src/packet.rs b/quinn-proto/src/packet.rs index 45fb32caa..d25a19ef0 100644 --- a/quinn-proto/src/packet.rs +++ b/quinn-proto/src/packet.rs @@ -438,9 +438,12 @@ pub(crate) struct PartialEncode { } impl PartialEncode { - pub(crate) fn finish(self, buf: &mut [u8], header_crypto: &H, crypto: Option<(u64, &K)>) - where - K: crypto::PacketKey, + pub(crate) fn finish( + self, + buf: &mut [u8], + header_crypto: &H, + crypto: Option<(u64, &dyn crypto::PacketKey)>, + ) where H: crypto::HeaderKey, { let PartialEncode { header_len, pn, .. } = self; @@ -830,7 +833,7 @@ mod tests { #[test] fn header_encoding() { use crate::{ - crypto::{rustls::TlsSession, PacketKey, Session}, + crypto::{rustls::TlsSession, Session}, Side, }; @@ -850,7 +853,7 @@ mod tests { encode.finish( &mut buf, &client.header.local, - Some((0, &client.packet.local)), + Some((0, &*client.packet.local)), ); for byte in &buf {