Erase crypto::Session::PacketKey

This commit is contained in:
Benjamin Saunders
2021-10-10 15:09:38 -07:00
parent 02e02b1df3
commit 441560647d
7 changed files with 32 additions and 38 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ use tracing::{debug, trace};
use super::Connection;
use crate::{
crypto::{PacketKey, Session},
crypto::Session,
frame::{Datagram, FrameStruct},
packet::SpaceId,
TransportError,
+5 -8
View File
@@ -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<PrevCrypto<S::PacketKey>>,
prev_crypto: Option<PrevCrypto>,
/// 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<KeyPair<S::PacketKey>>,
next_crypto: Option<KeyPair<Box<dyn PacketKey>>>,
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<K>
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<K>,
crypto: KeyPair<Box<dyn PacketKey>>,
/// 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<S: crypto::Session> {
header: S::HeaderKey,
packet: S::PacketKey,
packet: Box<dyn PacketKey>,
}
#[derive(Default)]
+3 -3
View File
@@ -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);
};
+3 -5
View File
@@ -33,8 +33,6 @@ pub trait Session: Send + Sized {
type ClientConfig: ClientConfig<Self>;
/// 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<Self>;
@@ -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<dyn PacketKey>)>;
/// If the 0-RTT-encrypted data has been accepted by the peer
fn early_data_accepted(&self) -> Option<bool>;
@@ -85,7 +83,7 @@ pub trait Session: Send + Sized {
fn write_handshake(&mut self, buf: &mut Vec<u8>) -> Option<Keys<Self>>;
/// Compute keys for the next key update
fn next_1rtt_keys(&mut self) -> Option<KeyPair<Self::PacketKey>>;
fn next_1rtt_keys(&mut self) -> Option<KeyPair<Box<dyn PacketKey>>>;
/// 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<S::HeaderKey>,
/// Packet protection keys
pub packet: KeyPair<S::PacketKey>,
pub packet: KeyPair<Box<dyn PacketKey>>,
}
/// Client-side configuration for the crypto protocol
+9 -10
View File
@@ -37,7 +37,6 @@ impl TlsSession {
impl crypto::Session for TlsSession {
type ClientConfig = Arc<rustls::ClientConfig>;
type PacketKey = PacketKey;
type HeaderKey = HeaderProtectionKey;
type ServerConfig = Arc<rustls::ServerConfig>;
@@ -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<dyn Any> { Box::new(CertificateChain::from(v.to_vec())) })
}
fn early_crypto(&self) -> Option<(Self::HeaderKey, Self::PacketKey)> {
fn early_crypto(&self) -> Option<(Self::HeaderKey, Box<dyn crypto::PacketKey>)> {
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<bool> {
@@ -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<KeyPair<Self::PacketKey>> {
fn next_1rtt_keys(&mut self) -> Option<KeyPair<Box<dyn crypto::PacketKey>>> {
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),
})
}
+3 -6
View File
@@ -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::<S::PacketKey, S::HeaderKey>(&mut buf, &crypto.header.local, None);
encode.finish::<S::HeaderKey>(&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,
+8 -5
View File
@@ -438,9 +438,12 @@ pub(crate) struct PartialEncode {
}
impl PartialEncode {
pub(crate) fn finish<K, H>(self, buf: &mut [u8], header_crypto: &H, crypto: Option<(u64, &K)>)
where
K: crypto::PacketKey,
pub(crate) fn finish<H>(
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 {