From 02e02b1df3988f520b1ff4e4c82d3744c0c97dd7 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sun, 10 Oct 2021 14:58:47 -0700 Subject: [PATCH] Erase crypto::Session::HandshakeTokenKey --- quinn-proto/src/config.rs | 24 ++++++++++++------------ quinn-proto/src/crypto.rs | 4 ---- quinn-proto/src/crypto/ring.rs | 4 ---- quinn-proto/src/crypto/rustls.rs | 3 +-- quinn-proto/src/token.rs | 12 ++++++------ 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/quinn-proto/src/config.rs b/quinn-proto/src/config.rs index 361c53ab3..4c00078ae 100644 --- a/quinn-proto/src/config.rs +++ b/quinn-proto/src/config.rs @@ -8,7 +8,7 @@ use crate::crypto::types::{Certificate, CertificateChain, PrivateKey}; use crate::{ cid_generator::{ConnectionIdGenerator, RandomConnectionIdGenerator}, congestion, - crypto::{self, HandshakeTokenKey as _, HmacKey}, + crypto::{self, HandshakeTokenKey, HmacKey}, VarInt, VarIntBoundsExceeded, DEFAULT_SUPPORTED_VERSIONS, }; @@ -430,7 +430,7 @@ where pub crypto: S::ServerConfig, /// Used to generate one-time AEAD keys to protect handshake tokens - pub(crate) token_key: Arc, + pub(crate) token_key: Arc, /// Whether to require clients to prove ownership of an address before committing resources. /// @@ -453,13 +453,13 @@ impl ServerConfig where S: crypto::Session, { - /// Create a default config with a particular `master_key` - pub fn new(crypto: S::ServerConfig, prk: S::HandshakeTokenKey) -> Self { + /// Create a default config with a particular handshake token key + pub fn new(crypto: S::ServerConfig, token_key: Arc) -> Self { Self { transport: Arc::new(TransportConfig::default()), crypto, - token_key: Arc::new(prk), + token_key, use_stateless_retry: false, retry_token_lifetime: Duration::from_secs(15), @@ -470,8 +470,11 @@ where } /// Private key used to authenticate data included in handshake tokens. - pub fn token_key(&mut self, master_key: &[u8]) -> Result<&mut Self, ConfigError> { - self.token_key = Arc::new(S::HandshakeTokenKey::from_secret(master_key)); + pub fn token_key( + &mut self, + value: Arc, + ) -> Result<&mut Self, ConfigError> { + self.token_key = value; Ok(self) } @@ -536,12 +539,9 @@ impl ServerConfig { let rng = &mut rand::thread_rng(); let mut master_key = [0u8; 64]; rng.fill_bytes(&mut master_key); - let master_key = - ::HandshakeTokenKey::from_secret( - &master_key, - ); + let master_key = ring::hkdf::Salt::new(ring::hkdf::HKDF_SHA256, &[]).extract(&master_key); - Self::new(crypto, master_key) + Self::new(crypto, Arc::new(master_key)) } } diff --git a/quinn-proto/src/crypto.rs b/quinn-proto/src/crypto.rs index dbc531c9c..3863f03ba 100644 --- a/quinn-proto/src/crypto.rs +++ b/quinn-proto/src/crypto.rs @@ -31,8 +31,6 @@ pub(crate) mod types; pub trait Session: Send + Sized { /// Type used to hold configuration for client sessions type ClientConfig: ClientConfig; - /// Key used to generate one-time-use handshake token keys - type HandshakeTokenKey: HandshakeTokenKey; /// Type of keys used to protect packet headers type HeaderKey: HeaderKey; /// Type used to represent packet protection keys @@ -200,8 +198,6 @@ pub struct ExportKeyingMaterialError; pub trait HandshakeTokenKey: Send + Sync { /// Derive AEAD using hkdf fn aead_from_hkdf(&self, random_bytes: &[u8]) -> Box; - /// Method to build pseudo random key from existing bytes - fn from_secret(secret: &[u8]) -> Self; } /// A key for sealing data with AEAD-based algorithms diff --git a/quinn-proto/src/crypto/ring.rs b/quinn-proto/src/crypto/ring.rs index 198076cb7..8d7f417a2 100644 --- a/quinn-proto/src/crypto/ring.rs +++ b/quinn-proto/src/crypto/ring.rs @@ -27,10 +27,6 @@ impl crypto::HandshakeTokenKey for hkdf::Prk { let key = aead::UnboundKey::new(&aead::AES_256_GCM, &key_buffer).unwrap(); Box::new(aead::LessSafeKey::new(key)) } - - fn from_secret(bytes: &[u8]) -> Self { - hkdf::Salt::new(hkdf::HKDF_SHA256, &[]).extract(bytes) - } } impl crypto::AeadKey for aead::LessSafeKey { diff --git a/quinn-proto/src/crypto/rustls.rs b/quinn-proto/src/crypto/rustls.rs index f2c248453..59c46f1cd 100644 --- a/quinn-proto/src/crypto/rustls.rs +++ b/quinn-proto/src/crypto/rustls.rs @@ -1,7 +1,7 @@ use std::{any::Any, convert::TryInto, io, str, sync::Arc}; use bytes::BytesMut; -use ring::{aead, hkdf}; +use ring::aead; pub use rustls::Error; use rustls::{ self, @@ -37,7 +37,6 @@ impl TlsSession { impl crypto::Session for TlsSession { type ClientConfig = Arc; - type HandshakeTokenKey = hkdf::Prk; type PacketKey = PacketKey; type HeaderKey = HeaderProtectionKey; type ServerConfig = Arc; diff --git a/quinn-proto/src/token.rs b/quinn-proto/src/token.rs index 84326f3cf..289cb5afe 100644 --- a/quinn-proto/src/token.rs +++ b/quinn-proto/src/token.rs @@ -25,7 +25,7 @@ pub struct RetryToken<'a> { impl<'a> RetryToken<'a> { pub fn encode( &self, - key: &impl HandshakeTokenKey, + key: &dyn HandshakeTokenKey, address: &SocketAddr, retry_src_cid: &ConnectionId, ) -> Vec { @@ -52,7 +52,7 @@ impl<'a> RetryToken<'a> { } pub fn from_bytes( - key: &impl HandshakeTokenKey, + key: &dyn HandshakeTokenKey, address: &SocketAddr, retry_src_cid: &ConnectionId, raw_token_bytes: &'a [u8], @@ -158,7 +158,7 @@ mod test { fn token_sanity() { use super::*; use crate::cid_generator::{ConnectionIdGenerator, RandomConnectionIdGenerator}; - use crate::{crypto, MAX_CID_SIZE}; + use crate::MAX_CID_SIZE; use rand::RngCore; use std::{ @@ -177,7 +177,7 @@ mod test { let mut master_key = vec![0u8; 64]; rng.fill_bytes(&mut master_key); - let prk: ring::hkdf::Prk = crypto::HandshakeTokenKey::from_secret(&master_key); + let prk = ring::hkdf::Salt::new(ring::hkdf::HKDF_SHA256, &[]).extract(&master_key); let addr = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 4433); let retry_src_cid = RandomConnectionIdGenerator::new(MAX_CID_SIZE).generate_cid(); @@ -199,7 +199,7 @@ mod test { fn invalid_token_returns_err() { use super::*; use crate::cid_generator::{ConnectionIdGenerator, RandomConnectionIdGenerator}; - use crate::{crypto, MAX_CID_SIZE}; + use crate::MAX_CID_SIZE; use rand::RngCore; use std::net::Ipv6Addr; @@ -211,7 +211,7 @@ mod test { let mut random_bytes = [0; 32]; rng.fill_bytes(&mut random_bytes); - let prk: ring::hkdf::Prk = crypto::HandshakeTokenKey::from_secret(&master_key); + let prk = ring::hkdf::Salt::new(ring::hkdf::HKDF_SHA256, &[]).extract(&master_key); let addr = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 4433); let retry_src_cid = RandomConnectionIdGenerator::new(MAX_CID_SIZE).generate_cid();