Erase crypto::Session::HandshakeData

This commit is contained in:
Benjamin Saunders
2021-10-10 13:18:30 -07:00
parent 78dc23614f
commit a384ee2edb
5 changed files with 19 additions and 13 deletions
+2 -5
View File
@@ -8,7 +8,7 @@
//! Note that usage of any protocol (version) other than TLS 1.3 does not conform to any
//! published versions of the specification, and will not be supported in QUIC v1.
use std::str;
use std::{any::Any, str};
use bytes::BytesMut;
@@ -29,9 +29,6 @@ pub(crate) mod types;
/// A cryptographic session (commonly TLS)
pub trait Session: Send + Sized {
/// Parameters determined when the handshake begins, e.g. server name and/or application
/// protocol
type HandshakeData;
/// Cryptographic identity of the peer
type Identity: Sized;
/// Type used to hold configuration for client sessions
@@ -53,7 +50,7 @@ pub trait Session: Send + Sized {
/// Get data negotiated during the handshake, if available
///
/// Returns `None` until the connection emits `HandshakeDataReady`.
fn handshake_data(&self) -> Option<Self::HandshakeData>;
fn handshake_data(&self) -> Option<Box<dyn Any>>;
/// Get the peer's identity, if available
fn peer_identity(&self) -> Option<Self::Identity>;
+4 -5
View File
@@ -1,4 +1,4 @@
use std::{convert::TryInto, io, str, sync::Arc};
use std::{any::Any, convert::TryInto, io, str, sync::Arc};
use bytes::BytesMut;
use ring::{aead, hkdf, hmac};
@@ -36,7 +36,6 @@ impl TlsSession {
}
impl crypto::Session for TlsSession {
type HandshakeData = HandshakeData;
type Identity = CertificateChain;
type ClientConfig = Arc<rustls::ClientConfig>;
type HmacKey = hmac::Key;
@@ -59,17 +58,17 @@ impl crypto::Session for TlsSession {
}
}
fn handshake_data(&self) -> Option<HandshakeData> {
fn handshake_data(&self) -> Option<Box<dyn Any>> {
if !self.got_handshake_data {
return None;
}
Some(HandshakeData {
Some(Box::new(HandshakeData {
protocol: self.inner.alpn_protocol().map(|x| x.into()),
server_name: match self.inner {
Connection::Client(_) => None,
Connection::Server(ref session) => session.sni_hostname().map(|x| x.into()),
},
})
}))
}
fn peer_identity(&self) -> Option<CertificateChain> {
+2
View File
@@ -561,6 +561,8 @@ fn alpn_success() {
.client_conn_mut(client_ch)
.crypto_session()
.handshake_data()
.unwrap()
.downcast::<crate::crypto::rustls::HandshakeData>()
.unwrap();
assert_eq!(hd.protocol.unwrap(), &b"bar"[..]);
}
+1
View File
@@ -156,6 +156,7 @@ async fn handle_connection(root: Arc<Path>, conn: quinn::Connecting) -> Result<(
protocol = %connection
.handshake_data()
.unwrap()
.downcast::<quinn::crypto::rustls::HandshakeData>().unwrap()
.protocol
.map_or_else(|| "<none>".into(), |x| String::from_utf8_lossy(&x).into_owned())
);
+10 -3
View File
@@ -1,4 +1,5 @@
use std::{
any::Any,
fmt,
future::Future,
mem,
@@ -111,7 +112,12 @@ where
}
/// Parameters negotiated during the handshake
pub async fn handshake_data(&mut self) -> Result<S::HandshakeData, ConnectionError> {
///
/// The dynamic type returned is determined by the configured
/// [`Session`](proto::crypto::Session). For the default `rustls` session, the return value can
/// be [`downcast`](Box::downcast) to a
/// [`crypto::rustls::HandshakeData`](crate::crypto::rustls::HandshakeData).
pub async fn handshake_data(&mut self) -> Result<Box<dyn Any>, ConnectionError> {
// Taking &mut self allows us to use a single oneshot channel rather than dealing with
// potentially many tasks waiting on the same event. It's a bit of a hack, but keeps things
// simple.
@@ -459,10 +465,11 @@ where
/// Parameters negotiated during the handshake
///
/// Guaranteed to return `Some` on fully established connections or after
/// [`Connecting::handshake_data()`] succeeds.
/// [`Connecting::handshake_data()`] succeeds. See that method's documentations for details on
/// the returned value.
///
/// [`Connection::handshake_data()`]: crate::generic::Connecting::handshake_data
pub fn handshake_data(&self) -> Option<S::HandshakeData> {
pub fn handshake_data(&self) -> Option<Box<dyn Any>> {
self.0
.lock("handshake_data")
.inner