Skip to content

Commit d6f4fa9

Browse files
authored
feat: Allow configuring the max number of TLS tickets (#3442)
## Description Allows increasing the max number of TLS tickets without having to patch iroh. This is needed if you want to do a large number of 0rtt connections to different clients. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions Q: is this enough, or should we allow passing in an `Arc<dyn ClientSessionStore>`? The latter would mean exposing one more rustls type. Note: this is mostly for playing around with it, not sure if we want to merge it as is. But then, it seems pretty straightforward. If we ever want the ability to provide an `Arc<dyn ClientSessionStore>` we can always make the `fn max_tls_tickets` just init an in-mem provider. ## Change checklist <!-- Remove any that are not relevant. --> - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme)
1 parent b791123 commit d6f4fa9

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

iroh/src/endpoint.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
magicsock::{self, Handle, NodeIdMappedAddr, OwnAddressSnafu},
4646
metrics::EndpointMetrics,
4747
net_report::Report,
48-
tls,
48+
tls::{self, DEFAULT_MAX_TLS_TICKETS},
4949
};
5050

5151
mod rtt_actor;
@@ -118,6 +118,7 @@ pub struct Builder {
118118
addr_v6: Option<SocketAddrV6>,
119119
#[cfg(any(test, feature = "test-utils"))]
120120
path_selection: PathSelection,
121+
max_tls_tickets: usize,
121122
}
122123

123124
impl Default for Builder {
@@ -142,6 +143,7 @@ impl Default for Builder {
142143
addr_v6: None,
143144
#[cfg(any(test, feature = "test-utils"))]
144145
path_selection: PathSelection::default(),
146+
max_tls_tickets: DEFAULT_MAX_TLS_TICKETS,
145147
}
146148
}
147149
}
@@ -160,7 +162,7 @@ impl Builder {
160162
.unwrap_or_else(|| SecretKey::generate(rand::rngs::OsRng));
161163
let static_config = StaticConfig {
162164
transport_config: Arc::new(self.transport_config),
163-
tls_config: tls::TlsConfig::new(secret_key.clone()),
165+
tls_config: tls::TlsConfig::new(secret_key.clone(), self.max_tls_tickets),
164166
keylog: self.keylog,
165167
};
166168
let server_config = static_config.create_server_config(self.alpn_protocols);
@@ -474,6 +476,17 @@ impl Builder {
474476
self.path_selection = path_selection;
475477
self
476478
}
479+
480+
/// Set the maximum number of TLS tickets to cache.
481+
///
482+
/// Set this to a larger value if you want to do 0rtt connections to a large
483+
/// number of clients.
484+
///
485+
/// The default is 256, taking about 150 KiB in memory.
486+
pub fn max_tls_tickets(mut self, n: usize) -> Self {
487+
self.max_tls_tickets = n;
488+
self
489+
}
477490
}
478491

479492
/// Configuration for a [`quinn::Endpoint`] that cannot be changed at runtime.

iroh/src/magicsock.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,7 +2545,7 @@ mod tests {
25452545
dns::DnsResolver,
25462546
endpoint::{DirectAddr, PathSelection, Source},
25472547
magicsock::{Handle, MagicSock, node_map},
2548-
tls,
2548+
tls::{self, DEFAULT_MAX_TLS_TICKETS},
25492549
};
25502550

25512551
const ALPN: &[u8] = b"n0/test/1";
@@ -2577,7 +2577,8 @@ mod tests {
25772577
/// Generate a server config with no ALPNS and a default transport configuration
25782578
fn make_default_server_config(secret_key: &SecretKey) -> ServerConfig {
25792579
let quic_server_config =
2580-
crate::tls::TlsConfig::new(secret_key.clone()).make_server_config(vec![], false);
2580+
crate::tls::TlsConfig::new(secret_key.clone(), DEFAULT_MAX_TLS_TICKETS)
2581+
.make_server_config(vec![], false);
25812582
let mut server_config = ServerConfig::with_crypto(Arc::new(quic_server_config));
25822583
server_config.transport_config(Arc::new(quinn::TransportConfig::default()));
25832584
server_config
@@ -3080,8 +3081,8 @@ mod tests {
30803081
/// Use [`magicsock_connect`] to establish connections.
30813082
#[instrument(name = "ep", skip_all, fields(me = secret_key.public().fmt_short()))]
30823083
async fn magicsock_ep(secret_key: SecretKey) -> Result<Handle> {
3083-
let quic_server_config =
3084-
tls::TlsConfig::new(secret_key.clone()).make_server_config(vec![ALPN.to_vec()], true);
3084+
let quic_server_config = tls::TlsConfig::new(secret_key.clone(), DEFAULT_MAX_TLS_TICKETS)
3085+
.make_server_config(vec![ALPN.to_vec()], true);
30853086
let mut server_config = ServerConfig::with_crypto(Arc::new(quic_server_config));
30863087
server_config.transport_config(Arc::new(quinn::TransportConfig::default()));
30873088

@@ -3144,7 +3145,8 @@ mod tests {
31443145
) -> Result<quinn::Connection> {
31453146
let alpns = vec![ALPN.to_vec()];
31463147
let quic_client_config =
3147-
tls::TlsConfig::new(ep_secret_key.clone()).make_client_config(alpns, true);
3148+
tls::TlsConfig::new(ep_secret_key.clone(), DEFAULT_MAX_TLS_TICKETS)
3149+
.make_client_config(alpns, true);
31483150
let mut client_config = quinn::ClientConfig::new(Arc::new(quic_client_config));
31493151
client_config.transport_config(transport_config);
31503152
let connect = ep

iroh/src/tls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod verifier;
2525
/// So 8 * 32 * (200 + 387) = 150.272 bytes, assuming pointers to certificates
2626
/// are never aliased pointers (they're Arc'ed).
2727
/// I think 150KB is an acceptable default upper limit for such a cache.
28-
const MAX_TLS_TICKETS: usize = 8 * 32;
28+
pub(crate) const DEFAULT_MAX_TLS_TICKETS: usize = 8 * 32;
2929

3030
/// Configuration for TLS.
3131
///
@@ -44,7 +44,7 @@ pub(crate) struct TlsConfig {
4444
}
4545

4646
impl TlsConfig {
47-
pub(crate) fn new(secret_key: SecretKey) -> Self {
47+
pub(crate) fn new(secret_key: SecretKey, max_tls_tickets: usize) -> Self {
4848
let cert_resolver = Arc::new(
4949
AlwaysResolvesCert::new(&secret_key).expect("Client cert key DER is valid; qed"),
5050
);
@@ -54,7 +54,7 @@ impl TlsConfig {
5454
server_verifier: Arc::new(verifier::ServerCertificateVerifier),
5555
client_verifier: Arc::new(verifier::ClientCertificateVerifier),
5656
session_store: Arc::new(rustls::client::ClientSessionMemoryCache::new(
57-
MAX_TLS_TICKETS,
57+
max_tls_tickets,
5858
)),
5959
}
6060
}

0 commit comments

Comments
 (0)