|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! This module shows an example of an event provider that accesses certificate chains |
| 5 | +//! from QUIC connections on both client and server. |
| 6 | +
|
| 7 | +use super::*; |
| 8 | +use crate::provider::event::events::{self, ConnectionInfo, ConnectionMeta, Subscriber}; |
| 9 | + |
| 10 | +struct Chain; |
| 11 | + |
| 12 | +#[derive(Default)] |
| 13 | +struct ChainContext { |
| 14 | + chain: Option<Vec<Vec<u8>>>, |
| 15 | + sender: Option<tokio::sync::mpsc::Sender<Vec<Vec<u8>>>>, |
| 16 | +} |
| 17 | + |
| 18 | +impl Subscriber for Chain { |
| 19 | + type ConnectionContext = ChainContext; |
| 20 | + |
| 21 | + #[inline] |
| 22 | + fn create_connection_context( |
| 23 | + &mut self, |
| 24 | + _: &ConnectionMeta, |
| 25 | + _info: &ConnectionInfo, |
| 26 | + ) -> Self::ConnectionContext { |
| 27 | + ChainContext::default() |
| 28 | + } |
| 29 | + |
| 30 | + fn on_tls_exporter_ready( |
| 31 | + &mut self, |
| 32 | + context: &mut Self::ConnectionContext, |
| 33 | + _meta: &ConnectionMeta, |
| 34 | + event: &events::TlsExporterReady, |
| 35 | + ) { |
| 36 | + if let Some(sender) = context.sender.take() { |
| 37 | + sender |
| 38 | + .blocking_send(event.session.peer_cert_chain_der().unwrap()) |
| 39 | + .unwrap(); |
| 40 | + } else { |
| 41 | + context.chain = Some(event.session.peer_cert_chain_der().unwrap()); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn start_server( |
| 47 | + mut server: Server, |
| 48 | + server_chain: tokio::sync::mpsc::Sender<Vec<Vec<u8>>>, |
| 49 | +) -> crate::provider::io::testing::Result<SocketAddr> { |
| 50 | + let server_addr = server.local_addr()?; |
| 51 | + |
| 52 | + // accept connections and echo back |
| 53 | + spawn(async move { |
| 54 | + while let Some(mut connection) = server.accept().await { |
| 55 | + let chain = connection |
| 56 | + .query_event_context_mut(|ctx: &mut ChainContext| { |
| 57 | + if let Some(chain) = ctx.chain.take() { |
| 58 | + Some(chain) |
| 59 | + } else { |
| 60 | + ctx.sender = Some(server_chain.clone()); |
| 61 | + None |
| 62 | + } |
| 63 | + }) |
| 64 | + .unwrap(); |
| 65 | + if let Some(chain) = chain { |
| 66 | + server_chain.send(chain).await.unwrap(); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + Ok(server_addr) |
| 72 | +} |
| 73 | + |
| 74 | +fn tls_test<C>(f: fn(crate::Connection, Vec<Vec<u8>>) -> C) |
| 75 | +where |
| 76 | + C: 'static + core::future::Future<Output = ()> + Send, |
| 77 | +{ |
| 78 | + let model = Model::default(); |
| 79 | + model.set_delay(Duration::from_millis(50)); |
| 80 | + |
| 81 | + test(model, |handle| { |
| 82 | + let server = Server::builder() |
| 83 | + .with_io(handle.builder().build()?)? |
| 84 | + .with_tls(build_server_mtls_provider(certificates::MTLS_CA_CERT)?)? |
| 85 | + .with_event((Chain, tracing_events()))? |
| 86 | + .start()?; |
| 87 | + let (send, server_chain) = tokio::sync::mpsc::channel(1); |
| 88 | + let server_chain = Arc::new(tokio::sync::Mutex::new(server_chain)); |
| 89 | + |
| 90 | + let addr = start_server(server, send)?; |
| 91 | + |
| 92 | + let client = Client::builder() |
| 93 | + .with_io(handle.builder().build().unwrap())? |
| 94 | + .with_tls(build_client_mtls_provider(certificates::MTLS_CA_CERT)?)? |
| 95 | + .with_event((Chain, tracing_events()))? |
| 96 | + .start()?; |
| 97 | + |
| 98 | + // show it working for several connections |
| 99 | + for _ in 0..10 { |
| 100 | + let client = client.clone(); |
| 101 | + let server_chain = server_chain.clone(); |
| 102 | + primary::spawn(async move { |
| 103 | + let connect = Connect::new(addr).with_server_name("localhost"); |
| 104 | + let conn = client.connect(connect).await.unwrap(); |
| 105 | + delay(Duration::from_millis(100)).await; |
| 106 | + let server_chain = server_chain.lock().await.recv().await.unwrap(); |
| 107 | + f(conn, server_chain).await; |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + Ok(addr) |
| 112 | + }) |
| 113 | + .unwrap(); |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +fn happy_case() { |
| 118 | + tls_test(|mut conn, server_chain| async move { |
| 119 | + let client_chain = conn |
| 120 | + .query_event_context_mut(|ctx: &mut ChainContext| ctx.chain.take().unwrap()) |
| 121 | + .unwrap(); |
| 122 | + // these are DER-encoded and we lack nice conversion functions, so just assert some simple |
| 123 | + // properties. |
| 124 | + assert!(server_chain.len() > 1); |
| 125 | + assert!(client_chain.len() > 1); |
| 126 | + assert_ne!(server_chain, client_chain); |
| 127 | + }); |
| 128 | +} |
0 commit comments