rustls_jls/client/client_conn.rs
1use alloc::vec::Vec;
2use core::marker::PhantomData;
3use core::ops::{Deref, DerefMut};
4use core::{fmt, mem};
5
6use pki_types::{ServerName, UnixTime};
7
8use super::handy::NoClientSessionStorage;
9use super::hs::{self, ClientHelloInput};
10#[cfg(feature = "std")]
11use crate::WantsVerifier;
12use crate::builder::ConfigBuilder;
13use crate::client::{EchMode, EchStatus};
14use crate::common_state::{CommonState, Protocol, Side};
15use crate::conn::{ConnectionCore, UnbufferedConnectionCommon};
16use crate::crypto::{CryptoProvider, SupportedKxGroup};
17use crate::enums::{CipherSuite, ProtocolVersion, SignatureScheme};
18use crate::error::Error;
19use crate::kernel::KernelConnection;
20use crate::log::trace;
21use crate::msgs::enums::NamedGroup;
22use crate::msgs::handshake::ClientExtensionsInput;
23use crate::msgs::persist;
24use crate::suites::{ExtractedSecrets, SupportedCipherSuite};
25use crate::sync::Arc;
26#[cfg(feature = "std")]
27use crate::time_provider::DefaultTimeProvider;
28use crate::time_provider::TimeProvider;
29use crate::unbuffered::{EncryptError, TransmitTlsData};
30#[cfg(doc)]
31use crate::{DistinguishedName, crypto};
32use crate::{KeyLog, WantsVersions, compress, sign, verify, versions};
33
34/// A trait for the ability to store client session data, so that sessions
35/// can be resumed in future connections.
36///
37/// Generally all data in this interface should be treated as
38/// **highly sensitive**, containing enough key material to break all security
39/// of the corresponding session.
40///
41/// `set_`, `insert_`, `remove_` and `take_` operations are mutating; this isn't
42/// expressed in the type system to allow implementations freedom in
43/// how to achieve interior mutability. `Mutex` is a common choice.
44pub trait ClientSessionStore: fmt::Debug + Send + Sync {
45 /// Remember what `NamedGroup` the given server chose.
46 fn set_kx_hint(&self, server_name: ServerName<'static>, group: NamedGroup);
47
48 /// This should return the value most recently passed to `set_kx_hint`
49 /// for the given `server_name`.
50 ///
51 /// If `None` is returned, the caller chooses the first configured group,
52 /// and an extra round trip might happen if that choice is unsatisfactory
53 /// to the server.
54 fn kx_hint(&self, server_name: &ServerName<'_>) -> Option<NamedGroup>;
55
56 /// Remember a TLS1.2 session.
57 ///
58 /// At most one of these can be remembered at a time, per `server_name`.
59 fn set_tls12_session(
60 &self,
61 server_name: ServerName<'static>,
62 value: persist::Tls12ClientSessionValue,
63 );
64
65 /// Get the most recently saved TLS1.2 session for `server_name` provided to `set_tls12_session`.
66 fn tls12_session(
67 &self,
68 server_name: &ServerName<'_>,
69 ) -> Option<persist::Tls12ClientSessionValue>;
70
71 /// Remove and forget any saved TLS1.2 session for `server_name`.
72 fn remove_tls12_session(&self, server_name: &ServerName<'static>);
73
74 /// Remember a TLS1.3 ticket that might be retrieved later from `take_tls13_ticket`, allowing
75 /// resumption of this session.
76 ///
77 /// This can be called multiple times for a given session, allowing multiple independent tickets
78 /// to be valid at once. The number of times this is called is controlled by the server, so
79 /// implementations of this trait should apply a reasonable bound of how many items are stored
80 /// simultaneously.
81 fn insert_tls13_ticket(
82 &self,
83 server_name: ServerName<'static>,
84 value: persist::Tls13ClientSessionValue,
85 );
86
87 /// Return a TLS1.3 ticket previously provided to `add_tls13_ticket`.
88 ///
89 /// Implementations of this trait must return each value provided to `add_tls13_ticket` _at most once_.
90 fn take_tls13_ticket(
91 &self,
92 server_name: &ServerName<'static>,
93 ) -> Option<persist::Tls13ClientSessionValue>;
94}
95
96/// A trait for the ability to choose a certificate chain and
97/// private key for the purposes of client authentication.
98pub trait ResolvesClientCert: fmt::Debug + Send + Sync {
99 /// Resolve a client certificate chain/private key to use as the client's
100 /// identity.
101 ///
102 /// `root_hint_subjects` is an optional list of certificate authority
103 /// subject distinguished names that the client can use to help
104 /// decide on a client certificate the server is likely to accept. If
105 /// the list is empty, the client should send whatever certificate it
106 /// has. The hints are expected to be DER-encoded X.500 distinguished names,
107 /// per [RFC 5280 A.1]. See [`DistinguishedName`] for more information
108 /// on decoding with external crates like `x509-parser`.
109 ///
110 /// `sigschemes` is the list of the [`SignatureScheme`]s the server
111 /// supports.
112 ///
113 /// Return `None` to continue the handshake without any client
114 /// authentication. The server may reject the handshake later
115 /// if it requires authentication.
116 ///
117 /// [RFC 5280 A.1]: https://www.rfc-editor.org/rfc/rfc5280#appendix-A.1
118 fn resolve(
119 &self,
120 root_hint_subjects: &[&[u8]],
121 sigschemes: &[SignatureScheme],
122 ) -> Option<Arc<sign::CertifiedKey>>;
123
124 /// Return true if the client only supports raw public keys.
125 ///
126 /// See [RFC 7250](https://www.rfc-editor.org/rfc/rfc7250).
127 fn only_raw_public_keys(&self) -> bool {
128 false
129 }
130
131 /// Return true if any certificates at all are available.
132 fn has_certs(&self) -> bool;
133}
134
135/// Common configuration for (typically) all connections made by a program.
136///
137/// Making one of these is cheap, though one of the inputs may be expensive: gathering trust roots
138/// from the operating system to add to the [`RootCertStore`] passed to `with_root_certificates()`
139/// (the rustls-native-certs crate is often used for this) may take on the order of a few hundred
140/// milliseconds.
141///
142/// These must be created via the [`ClientConfig::builder()`] or [`ClientConfig::builder_with_provider()`]
143/// function.
144///
145/// Note that using [`ConfigBuilder<ClientConfig, WantsVersions>::with_ech()`] will produce a common
146/// configuration specific to the provided [`crate::client::EchConfig`] that may not be appropriate
147/// for all connections made by the program. In this case the configuration should only be shared
148/// by connections intended for domains that offer the provided [`crate::client::EchConfig`] in
149/// their DNS zone.
150///
151/// # Defaults
152///
153/// * [`ClientConfig::max_fragment_size`]: the default is `None` (meaning 16kB).
154/// * [`ClientConfig::resumption`]: supports resumption with up to 256 server names, using session
155/// ids or tickets, with a max of eight tickets per server.
156/// * [`ClientConfig::alpn_protocols`]: the default is empty -- no ALPN protocol is negotiated.
157/// * [`ClientConfig::key_log`]: key material is not logged.
158/// * [`ClientConfig::cert_decompressors`]: depends on the crate features, see [`compress::default_cert_decompressors()`].
159/// * [`ClientConfig::cert_compressors`]: depends on the crate features, see [`compress::default_cert_compressors()`].
160/// * [`ClientConfig::cert_compression_cache`]: caches the most recently used 4 compressions
161///
162/// [`RootCertStore`]: crate::RootCertStore
163#[derive(Clone, Debug)]
164pub struct ClientConfig {
165 /// Which ALPN protocols we include in our client hello.
166 /// If empty, no ALPN extension is sent.
167 pub alpn_protocols: Vec<Vec<u8>>,
168
169 /// How and when the client can resume a previous session.
170 ///
171 /// # Sharing `resumption` between `ClientConfig`s
172 /// In a program using many `ClientConfig`s it may improve resumption rates
173 /// (which has a significant impact on connection performance) if those
174 /// configs share a single `Resumption`.
175 ///
176 /// However, resumption is only allowed between two `ClientConfig`s if their
177 /// `client_auth_cert_resolver` (ie, potential client authentication credentials)
178 /// and `verifier` (ie, server certificate verification settings) are
179 /// the same (according to `Arc::ptr_eq`).
180 ///
181 /// To illustrate, imagine two `ClientConfig`s `A` and `B`. `A` fully validates
182 /// the server certificate, `B` does not. If `A` and `B` shared a resumption store,
183 /// it would be possible for a session originated by `B` to be inserted into the
184 /// store, and then resumed by `A`. This would give a false impression to the user
185 /// of `A` that the server certificate is fully validated.
186 pub resumption: Resumption,
187
188 /// The maximum size of plaintext input to be emitted in a single TLS record.
189 /// A value of None is equivalent to the [TLS maximum] of 16 kB.
190 ///
191 /// rustls enforces an arbitrary minimum of 32 bytes for this field.
192 /// Out of range values are reported as errors from [ClientConnection::new].
193 ///
194 /// Setting this value to a little less than the TCP MSS may improve latency
195 /// for stream-y workloads.
196 ///
197 /// [TLS maximum]: https://datatracker.ietf.org/doc/html/rfc8446#section-5.1
198 /// [ClientConnection::new]: crate::client::ClientConnection::new
199 pub max_fragment_size: Option<usize>,
200
201 /// How to decide what client auth certificate/keys to use.
202 pub client_auth_cert_resolver: Arc<dyn ResolvesClientCert>,
203
204 /// Whether to send the Server Name Indication (SNI) extension
205 /// during the client handshake.
206 ///
207 /// The default is true.
208 pub enable_sni: bool,
209
210 /// How to output key material for debugging. The default
211 /// does nothing.
212 pub key_log: Arc<dyn KeyLog>,
213
214 /// Allows traffic secrets to be extracted after the handshake,
215 /// e.g. for kTLS setup.
216 pub enable_secret_extraction: bool,
217
218 /// Whether to send data on the first flight ("early data") in
219 /// TLS 1.3 handshakes.
220 ///
221 /// The default is false.
222 pub enable_early_data: bool,
223
224 /// JLS Client Configuration
225 pub jls_config: crate::jls::JlsClientConfig,
226
227 /// If set to `true`, requires the server to support the extended
228 /// master secret extraction method defined in [RFC 7627].
229 ///
230 /// The default is `true` if the `fips` crate feature is enabled,
231 /// `false` otherwise.
232 ///
233 /// It must be set to `true` to meet FIPS requirement mentioned in section
234 /// **D.Q Transition of the TLS 1.2 KDF to Support the Extended Master
235 /// Secret** from [FIPS 140-3 IG.pdf].
236 ///
237 /// [RFC 7627]: https://datatracker.ietf.org/doc/html/rfc7627
238 /// [FIPS 140-3 IG.pdf]: https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf
239 #[cfg(feature = "tls12")]
240 pub require_ems: bool,
241
242 /// Provides the current system time
243 pub time_provider: Arc<dyn TimeProvider>,
244
245 /// Source of randomness and other crypto.
246 pub(super) provider: Arc<CryptoProvider>,
247
248 /// Supported versions, in no particular order. The default
249 /// is all supported versions.
250 pub(super) versions: versions::EnabledVersions,
251
252 /// How to verify the server certificate chain.
253 pub(super) verifier: Arc<dyn verify::ServerCertVerifier>,
254
255 /// How to decompress the server's certificate chain.
256 ///
257 /// If this is non-empty, the [RFC8779] certificate compression
258 /// extension is offered, and any compressed certificates are
259 /// transparently decompressed during the handshake.
260 ///
261 /// This only applies to TLS1.3 connections. It is ignored for
262 /// TLS1.2 connections.
263 ///
264 /// [RFC8779]: https://datatracker.ietf.org/doc/rfc8879/
265 pub cert_decompressors: Vec<&'static dyn compress::CertDecompressor>,
266
267 /// How to compress the client's certificate chain.
268 ///
269 /// If a server supports this extension, and advertises support
270 /// for one of the compression algorithms included here, the
271 /// client certificate will be compressed according to [RFC8779].
272 ///
273 /// This only applies to TLS1.3 connections. It is ignored for
274 /// TLS1.2 connections.
275 ///
276 /// [RFC8779]: https://datatracker.ietf.org/doc/rfc8879/
277 pub cert_compressors: Vec<&'static dyn compress::CertCompressor>,
278
279 /// Caching for compressed certificates.
280 ///
281 /// This is optional: [`compress::CompressionCache::Disabled`] gives
282 /// a cache that does no caching.
283 pub cert_compression_cache: Arc<compress::CompressionCache>,
284
285 /// How to offer Encrypted Client Hello (ECH). The default is to not offer ECH.
286 pub(super) ech_mode: Option<EchMode>,
287}
288
289impl ClientConfig {
290 /// Create a builder for a client configuration with
291 /// [the process-default `CryptoProvider`][CryptoProvider#using-the-per-process-default-cryptoprovider]
292 /// and safe protocol version defaults.
293 ///
294 /// For more information, see the [`ConfigBuilder`] documentation.
295 #[cfg(feature = "std")]
296 pub fn builder() -> ConfigBuilder<Self, WantsVerifier> {
297 Self::builder_with_protocol_versions(versions::DEFAULT_VERSIONS)
298 }
299
300 /// Create a builder for a client configuration with
301 /// [the process-default `CryptoProvider`][CryptoProvider#using-the-per-process-default-cryptoprovider]
302 /// and the provided protocol versions.
303 ///
304 /// Panics if
305 /// - the supported versions are not compatible with the provider (eg.
306 /// the combination of ciphersuites supported by the provider and supported
307 /// versions lead to zero cipher suites being usable),
308 /// - if a `CryptoProvider` cannot be resolved using a combination of
309 /// the crate features and process default.
310 ///
311 /// For more information, see the [`ConfigBuilder`] documentation.
312 #[cfg(feature = "std")]
313 pub fn builder_with_protocol_versions(
314 versions: &[&'static versions::SupportedProtocolVersion],
315 ) -> ConfigBuilder<Self, WantsVerifier> {
316 // Safety assumptions:
317 // 1. that the provider has been installed (explicitly or implicitly)
318 // 2. that the process-level default provider is usable with the supplied protocol versions.
319 Self::builder_with_provider(
320 CryptoProvider::get_default_or_install_from_crate_features().clone(),
321 )
322 .with_protocol_versions(versions)
323 .unwrap()
324 }
325
326 /// Create a builder for a client configuration with a specific [`CryptoProvider`].
327 ///
328 /// This will use the provider's configured ciphersuites. You must additionally choose
329 /// which protocol versions to enable, using `with_protocol_versions` or
330 /// `with_safe_default_protocol_versions` and handling the `Result` in case a protocol
331 /// version is not supported by the provider's ciphersuites.
332 ///
333 /// For more information, see the [`ConfigBuilder`] documentation.
334 #[cfg(feature = "std")]
335 pub fn builder_with_provider(
336 provider: Arc<CryptoProvider>,
337 ) -> ConfigBuilder<Self, WantsVersions> {
338 ConfigBuilder {
339 state: WantsVersions {},
340 provider,
341 time_provider: Arc::new(DefaultTimeProvider),
342 side: PhantomData,
343 }
344 }
345 /// Create a builder for a client configuration with no default implementation details.
346 ///
347 /// This API must be used by `no_std` users.
348 ///
349 /// You must provide a specific [`TimeProvider`].
350 ///
351 /// You must provide a specific [`CryptoProvider`].
352 ///
353 /// This will use the provider's configured ciphersuites. You must additionally choose
354 /// which protocol versions to enable, using `with_protocol_versions` or
355 /// `with_safe_default_protocol_versions` and handling the `Result` in case a protocol
356 /// version is not supported by the provider's ciphersuites.
357 ///
358 /// For more information, see the [`ConfigBuilder`] documentation.
359 pub fn builder_with_details(
360 provider: Arc<CryptoProvider>,
361 time_provider: Arc<dyn TimeProvider>,
362 ) -> ConfigBuilder<Self, WantsVersions> {
363 ConfigBuilder {
364 state: WantsVersions {},
365 provider,
366 time_provider,
367 side: PhantomData,
368 }
369 }
370
371 /// Return true if connections made with this `ClientConfig` will
372 /// operate in FIPS mode.
373 ///
374 /// This is different from [`CryptoProvider::fips()`]: [`CryptoProvider::fips()`]
375 /// is concerned only with cryptography, whereas this _also_ covers TLS-level
376 /// configuration that NIST recommends, as well as ECH HPKE suites if applicable.
377 pub fn fips(&self) -> bool {
378 let mut is_fips = self.provider.fips();
379
380 #[cfg(feature = "tls12")]
381 {
382 is_fips = is_fips && self.require_ems
383 }
384
385 if let Some(ech_mode) = &self.ech_mode {
386 is_fips = is_fips && ech_mode.fips();
387 }
388
389 is_fips
390 }
391
392 /// Return the crypto provider used to construct this client configuration.
393 pub fn crypto_provider(&self) -> &Arc<CryptoProvider> {
394 &self.provider
395 }
396
397 /// Access configuration options whose use is dangerous and requires
398 /// extra care.
399 pub fn dangerous(&mut self) -> danger::DangerousClientConfig<'_> {
400 danger::DangerousClientConfig { cfg: self }
401 }
402
403 pub(super) fn needs_key_share(&self) -> bool {
404 self.supports_version(ProtocolVersion::TLSv1_3)
405 }
406
407 /// We support a given TLS version if it's quoted in the configured
408 /// versions *and* at least one ciphersuite for this version is
409 /// also configured.
410 pub(crate) fn supports_version(&self, v: ProtocolVersion) -> bool {
411 self.versions.contains(v)
412 && self
413 .provider
414 .cipher_suites
415 .iter()
416 .any(|cs| cs.version().version == v)
417 }
418
419 #[cfg(feature = "std")]
420 pub(crate) fn supports_protocol(&self, proto: Protocol) -> bool {
421 self.provider
422 .cipher_suites
423 .iter()
424 .any(|cs| cs.usable_for_protocol(proto))
425 }
426
427 pub(super) fn find_cipher_suite(&self, suite: CipherSuite) -> Option<SupportedCipherSuite> {
428 self.provider
429 .cipher_suites
430 .iter()
431 .copied()
432 .find(|&scs| scs.suite() == suite)
433 }
434
435 pub(super) fn find_kx_group(
436 &self,
437 group: NamedGroup,
438 version: ProtocolVersion,
439 ) -> Option<&'static dyn SupportedKxGroup> {
440 self.provider
441 .kx_groups
442 .iter()
443 .copied()
444 .find(|skxg| skxg.usable_for_version(version) && skxg.name() == group)
445 }
446
447 pub(super) fn current_time(&self) -> Result<UnixTime, Error> {
448 self.time_provider
449 .current_time()
450 .ok_or(Error::FailedToGetCurrentTime)
451 }
452}
453
454/// Configuration for how/when a client is allowed to resume a previous session.
455#[derive(Clone, Debug)]
456pub struct Resumption {
457 /// How we store session data or tickets. The default is to use an in-memory
458 /// [super::handy::ClientSessionMemoryCache].
459 pub(super) store: Arc<dyn ClientSessionStore>,
460
461 /// What mechanism is used for resuming a TLS 1.2 session.
462 pub(super) tls12_resumption: Tls12Resumption,
463}
464
465impl Resumption {
466 /// Create a new `Resumption` that stores data for the given number of sessions in memory.
467 ///
468 /// This is the default `Resumption` choice, and enables resuming a TLS 1.2 session with
469 /// a session id or RFC 5077 ticket.
470 #[cfg(feature = "std")]
471 pub fn in_memory_sessions(num: usize) -> Self {
472 Self {
473 store: Arc::new(super::handy::ClientSessionMemoryCache::new(num)),
474 tls12_resumption: Tls12Resumption::SessionIdOrTickets,
475 }
476 }
477
478 /// Use a custom [`ClientSessionStore`] implementation to store sessions.
479 ///
480 /// By default, enables resuming a TLS 1.2 session with a session id or RFC 5077 ticket.
481 pub fn store(store: Arc<dyn ClientSessionStore>) -> Self {
482 Self {
483 store,
484 tls12_resumption: Tls12Resumption::SessionIdOrTickets,
485 }
486 }
487
488 /// Disable all use of session resumption.
489 pub fn disabled() -> Self {
490 Self {
491 store: Arc::new(NoClientSessionStorage),
492 tls12_resumption: Tls12Resumption::Disabled,
493 }
494 }
495
496 /// Configure whether TLS 1.2 sessions may be resumed, and by what mechanism.
497 ///
498 /// This is meaningless if you've disabled resumption entirely, which is the case in `no-std`
499 /// contexts.
500 pub fn tls12_resumption(mut self, tls12: Tls12Resumption) -> Self {
501 self.tls12_resumption = tls12;
502 self
503 }
504}
505
506impl Default for Resumption {
507 /// Create an in-memory session store resumption with up to 256 server names, allowing
508 /// a TLS 1.2 session to resume with a session id or RFC 5077 ticket.
509 fn default() -> Self {
510 #[cfg(feature = "std")]
511 let ret = Self::in_memory_sessions(256);
512
513 #[cfg(not(feature = "std"))]
514 let ret = Self::disabled();
515
516 ret
517 }
518}
519
520/// What mechanisms to support for resuming a TLS 1.2 session.
521#[derive(Clone, Copy, Debug, PartialEq)]
522pub enum Tls12Resumption {
523 /// Disable 1.2 resumption.
524 Disabled,
525 /// Support 1.2 resumption using session ids only.
526 SessionIdOnly,
527 /// Support 1.2 resumption using session ids or RFC 5077 tickets.
528 ///
529 /// See[^1] for why you might like to disable RFC 5077 by instead choosing the `SessionIdOnly`
530 /// option. Note that TLS 1.3 tickets do not have those issues.
531 ///
532 /// [^1]: <https://words.filippo.io/we-need-to-talk-about-session-tickets/>
533 SessionIdOrTickets,
534}
535
536/// Container for unsafe APIs
537pub(super) mod danger {
538 use super::ClientConfig;
539 use super::verify::ServerCertVerifier;
540 use crate::sync::Arc;
541
542 /// Accessor for dangerous configuration options.
543 #[derive(Debug)]
544 pub struct DangerousClientConfig<'a> {
545 /// The underlying ClientConfig
546 pub cfg: &'a mut ClientConfig,
547 }
548
549 impl DangerousClientConfig<'_> {
550 /// Overrides the default `ServerCertVerifier` with something else.
551 pub fn set_certificate_verifier(&mut self, verifier: Arc<dyn ServerCertVerifier>) {
552 self.cfg.verifier = verifier;
553 }
554 }
555}
556
557#[derive(Debug, PartialEq)]
558enum EarlyDataState {
559 Disabled,
560 Ready,
561 Accepted,
562 AcceptedFinished,
563 Rejected,
564}
565
566#[derive(Debug)]
567pub(super) struct EarlyData {
568 state: EarlyDataState,
569 left: usize,
570}
571
572impl EarlyData {
573 fn new() -> Self {
574 Self {
575 left: 0,
576 state: EarlyDataState::Disabled,
577 }
578 }
579
580 pub(super) fn is_enabled(&self) -> bool {
581 matches!(self.state, EarlyDataState::Ready | EarlyDataState::Accepted)
582 }
583
584 #[cfg(feature = "std")]
585 fn is_accepted(&self) -> bool {
586 matches!(
587 self.state,
588 EarlyDataState::Accepted | EarlyDataState::AcceptedFinished
589 )
590 }
591
592 pub(super) fn enable(&mut self, max_data: usize) {
593 assert_eq!(self.state, EarlyDataState::Disabled);
594 self.state = EarlyDataState::Ready;
595 self.left = max_data;
596 }
597
598 pub(super) fn rejected(&mut self) {
599 trace!("EarlyData rejected");
600 self.state = EarlyDataState::Rejected;
601 }
602
603 pub(super) fn accepted(&mut self) {
604 trace!("EarlyData accepted");
605 assert_eq!(self.state, EarlyDataState::Ready);
606 self.state = EarlyDataState::Accepted;
607 }
608
609 pub(super) fn finished(&mut self) {
610 trace!("EarlyData finished");
611 self.state = match self.state {
612 EarlyDataState::Accepted => EarlyDataState::AcceptedFinished,
613 _ => panic!("bad EarlyData state"),
614 }
615 }
616
617 fn check_write_opt(&mut self, sz: usize) -> Option<usize> {
618 match self.state {
619 EarlyDataState::Disabled => unreachable!(),
620 EarlyDataState::Ready | EarlyDataState::Accepted => {
621 let take = if self.left < sz {
622 mem::replace(&mut self.left, 0)
623 } else {
624 self.left -= sz;
625 sz
626 };
627
628 Some(take)
629 }
630 EarlyDataState::Rejected | EarlyDataState::AcceptedFinished => None,
631 }
632 }
633}
634
635#[cfg(feature = "std")]
636mod connection {
637 use alloc::vec::Vec;
638 use core::fmt;
639 use core::ops::{Deref, DerefMut};
640 use std::io;
641
642 use pki_types::ServerName;
643
644 use super::{ClientConnectionData, ClientExtensionsInput};
645 use crate::ClientConfig;
646 use crate::client::EchStatus;
647 use crate::common_state::Protocol;
648 use crate::conn::{ConnectionCommon, ConnectionCore};
649 use crate::error::Error;
650 use crate::suites::ExtractedSecrets;
651 use crate::sync::Arc;
652
653 /// Stub that implements io::Write and dispatches to `write_early_data`.
654 pub struct WriteEarlyData<'a> {
655 sess: &'a mut ClientConnection,
656 }
657
658 impl<'a> WriteEarlyData<'a> {
659 fn new(sess: &'a mut ClientConnection) -> Self {
660 WriteEarlyData { sess }
661 }
662
663 /// How many bytes you may send. Writes will become short
664 /// once this reaches zero.
665 pub fn bytes_left(&self) -> usize {
666 self.sess
667 .inner
668 .core
669 .data
670 .early_data
671 .bytes_left()
672 }
673 }
674
675 impl io::Write for WriteEarlyData<'_> {
676 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
677 self.sess.write_early_data(buf)
678 }
679
680 fn flush(&mut self) -> io::Result<()> {
681 Ok(())
682 }
683 }
684
685 impl super::EarlyData {
686 fn check_write(&mut self, sz: usize) -> io::Result<usize> {
687 self.check_write_opt(sz)
688 .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))
689 }
690
691 fn bytes_left(&self) -> usize {
692 self.left
693 }
694 }
695
696 /// This represents a single TLS client connection.
697 pub struct ClientConnection {
698 inner: ConnectionCommon<ClientConnectionData>,
699 }
700
701 impl fmt::Debug for ClientConnection {
702 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
703 f.debug_struct("ClientConnection")
704 .finish()
705 }
706 }
707
708 impl ClientConnection {
709 /// Make a new ClientConnection. `config` controls how
710 /// we behave in the TLS protocol, `name` is the
711 /// name of the server we want to talk to.
712 pub fn new(config: Arc<ClientConfig>, name: ServerName<'static>) -> Result<Self, Error> {
713 Self::new_with_alpn(config.clone(), name, config.alpn_protocols.clone())
714 }
715
716 /// Make a new ClientConnection with custom ALPN protocols.
717 pub fn new_with_alpn(
718 config: Arc<ClientConfig>,
719 name: ServerName<'static>,
720 alpn_protocols: Vec<Vec<u8>>,
721 ) -> Result<Self, Error> {
722 Ok(Self {
723 inner: ConnectionCommon::from(ConnectionCore::for_client(
724 config,
725 name,
726 ClientExtensionsInput::from_alpn(alpn_protocols),
727 Protocol::Tcp,
728 )?),
729 })
730 }
731 /// Returns an `io::Write` implementer you can write bytes to
732 /// to send TLS1.3 early data (a.k.a. "0-RTT data") to the server.
733 ///
734 /// This returns None in many circumstances when the capability to
735 /// send early data is not available, including but not limited to:
736 ///
737 /// - The server hasn't been talked to previously.
738 /// - The server does not support resumption.
739 /// - The server does not support early data.
740 /// - The resumption data for the server has expired.
741 ///
742 /// The server specifies a maximum amount of early data. You can
743 /// learn this limit through the returned object, and writes through
744 /// it will process only this many bytes.
745 ///
746 /// The server can choose not to accept any sent early data --
747 /// in this case the data is lost but the connection continues. You
748 /// can tell this happened using `is_early_data_accepted`.
749 pub fn early_data(&mut self) -> Option<WriteEarlyData<'_>> {
750 if self
751 .inner
752 .core
753 .data
754 .early_data
755 .is_enabled()
756 {
757 Some(WriteEarlyData::new(self))
758 } else {
759 None
760 }
761 }
762
763 /// Returns True if the server signalled it will process early data.
764 ///
765 /// If you sent early data and this returns false at the end of the
766 /// handshake then the server will not process the data. This
767 /// is not an error, but you may wish to resend the data.
768 pub fn is_early_data_accepted(&self) -> bool {
769 self.inner.core.is_early_data_accepted()
770 }
771
772 /// Extract secrets, so they can be used when configuring kTLS, for example.
773 /// Should be used with care as it exposes secret key material.
774 pub fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
775 self.inner.dangerous_extract_secrets()
776 }
777
778 /// Return the connection's Encrypted Client Hello (ECH) status.
779 pub fn ech_status(&self) -> EchStatus {
780 self.inner.core.data.ech_status
781 }
782
783 /// Returns the number of TLS1.3 tickets that have been received.
784 pub fn tls13_tickets_received(&self) -> u32 {
785 self.inner.tls13_tickets_received
786 }
787
788 /// Return true if the connection was made with a `ClientConfig` that is FIPS compatible.
789 ///
790 /// This is different from [`crate::crypto::CryptoProvider::fips()`]:
791 /// it is concerned only with cryptography, whereas this _also_ covers TLS-level
792 /// configuration that NIST recommends, as well as ECH HPKE suites if applicable.
793 pub fn fips(&self) -> bool {
794 self.inner.core.common_state.fips
795 }
796
797 fn write_early_data(&mut self, data: &[u8]) -> io::Result<usize> {
798 self.inner
799 .core
800 .data
801 .early_data
802 .check_write(data.len())
803 .map(|sz| {
804 self.inner
805 .send_early_plaintext(&data[..sz])
806 })
807 }
808 }
809
810 impl Deref for ClientConnection {
811 type Target = ConnectionCommon<ClientConnectionData>;
812
813 fn deref(&self) -> &Self::Target {
814 &self.inner
815 }
816 }
817
818 impl DerefMut for ClientConnection {
819 fn deref_mut(&mut self) -> &mut Self::Target {
820 &mut self.inner
821 }
822 }
823
824 #[doc(hidden)]
825 impl<'a> TryFrom<&'a mut crate::Connection> for &'a mut ClientConnection {
826 type Error = ();
827
828 fn try_from(value: &'a mut crate::Connection) -> Result<Self, Self::Error> {
829 use crate::Connection::*;
830 match value {
831 Client(conn) => Ok(conn),
832 Server(_) => Err(()),
833 }
834 }
835 }
836
837 impl From<ClientConnection> for crate::Connection {
838 fn from(conn: ClientConnection) -> Self {
839 Self::Client(conn)
840 }
841 }
842}
843#[cfg(feature = "std")]
844pub use connection::{ClientConnection, WriteEarlyData};
845
846impl ConnectionCore<ClientConnectionData> {
847 pub(crate) fn for_client(
848 config: Arc<ClientConfig>,
849 name: ServerName<'static>,
850 extra_exts: ClientExtensionsInput<'static>,
851 proto: Protocol,
852 ) -> Result<Self, Error> {
853 let mut common_state = CommonState::new(Side::Client);
854 common_state.set_max_fragment_size(config.max_fragment_size)?;
855 common_state.protocol = proto;
856 common_state.enable_secret_extraction = config.enable_secret_extraction;
857 common_state.fips = config.fips();
858 common_state.jls_authed = if config.jls_config.enable {
859 crate::jls::JlsState::NotAuthed
860 } else {
861 crate::jls::JlsState::Disabled
862 };
863 let mut data = ClientConnectionData::new();
864
865 let mut cx = hs::ClientContext {
866 common: &mut common_state,
867 data: &mut data,
868 // `start_handshake` won't produce plaintext
869 sendable_plaintext: None,
870 };
871
872 let input = ClientHelloInput::new(name, &extra_exts, &mut cx, config)?;
873 let state = input.start_handshake(extra_exts, &mut cx)?;
874 Ok(Self::new(state, data, common_state))
875 }
876
877 #[cfg(feature = "std")]
878 pub(crate) fn is_early_data_accepted(&self) -> bool {
879 self.data.early_data.is_accepted()
880 }
881}
882
883/// Unbuffered version of `ClientConnection`
884///
885/// See the [`crate::unbuffered`] module docs for more details
886pub struct UnbufferedClientConnection {
887 inner: UnbufferedConnectionCommon<ClientConnectionData>,
888}
889
890impl UnbufferedClientConnection {
891 /// Make a new ClientConnection. `config` controls how we behave in the TLS protocol, `name` is
892 /// the name of the server we want to talk to.
893 pub fn new(config: Arc<ClientConfig>, name: ServerName<'static>) -> Result<Self, Error> {
894 Self::new_with_extensions(
895 config.clone(),
896 name,
897 ClientExtensionsInput::from_alpn(config.alpn_protocols.clone()),
898 )
899 }
900
901 /// Make a new UnbufferedClientConnection with custom ALPN protocols.
902 pub fn new_with_alpn(
903 config: Arc<ClientConfig>,
904 name: ServerName<'static>,
905 alpn_protocols: Vec<Vec<u8>>,
906 ) -> Result<Self, Error> {
907 Self::new_with_extensions(
908 config,
909 name,
910 ClientExtensionsInput::from_alpn(alpn_protocols),
911 )
912 }
913
914 fn new_with_extensions(
915 config: Arc<ClientConfig>,
916 name: ServerName<'static>,
917 extensions: ClientExtensionsInput<'static>,
918 ) -> Result<Self, Error> {
919 Ok(Self {
920 inner: UnbufferedConnectionCommon::from(ConnectionCore::for_client(
921 config,
922 name,
923 extensions,
924 Protocol::Tcp,
925 )?),
926 })
927 }
928
929 /// Extract secrets, so they can be used when configuring kTLS, for example.
930 /// Should be used with care as it exposes secret key material.
931 #[deprecated = "dangerous_extract_secrets() does not support session tickets or \
932 key updates, use dangerous_into_kernel_connection() instead"]
933 pub fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
934 self.inner.dangerous_extract_secrets()
935 }
936
937 /// Extract secrets and a [`KernelConnection`] object.
938 ///
939 /// This allows you use rustls to manage keys and then manage encryption and
940 /// decryption yourself (e.g. for kTLS).
941 ///
942 /// Should be used with care as it exposes secret key material.
943 ///
944 /// See the [`crate::kernel`] documentations for details on prerequisites
945 /// for calling this method.
946 pub fn dangerous_into_kernel_connection(
947 self,
948 ) -> Result<(ExtractedSecrets, KernelConnection<ClientConnectionData>), Error> {
949 self.inner
950 .core
951 .dangerous_into_kernel_connection()
952 }
953
954 /// Returns the number of TLS1.3 tickets that have been received.
955 pub fn tls13_tickets_received(&self) -> u32 {
956 self.inner.tls13_tickets_received
957 }
958}
959
960impl Deref for UnbufferedClientConnection {
961 type Target = UnbufferedConnectionCommon<ClientConnectionData>;
962
963 fn deref(&self) -> &Self::Target {
964 &self.inner
965 }
966}
967
968impl DerefMut for UnbufferedClientConnection {
969 fn deref_mut(&mut self) -> &mut Self::Target {
970 &mut self.inner
971 }
972}
973
974impl TransmitTlsData<'_, ClientConnectionData> {
975 /// returns an adapter that allows encrypting early (RTT-0) data before transmitting the
976 /// already encoded TLS data
977 ///
978 /// IF allowed by the protocol
979 pub fn may_encrypt_early_data(&mut self) -> Option<MayEncryptEarlyData<'_>> {
980 if self
981 .conn
982 .core
983 .data
984 .early_data
985 .is_enabled()
986 {
987 Some(MayEncryptEarlyData { conn: self.conn })
988 } else {
989 None
990 }
991 }
992}
993
994/// Allows encrypting early (RTT-0) data
995pub struct MayEncryptEarlyData<'c> {
996 conn: &'c mut UnbufferedConnectionCommon<ClientConnectionData>,
997}
998
999impl MayEncryptEarlyData<'_> {
1000 /// Encrypts `application_data` into the `outgoing_tls` buffer
1001 ///
1002 /// returns the number of bytes that were written into `outgoing_tls`, or an error if
1003 /// the provided buffer was too small. In the error case, `outgoing_tls` is not modified
1004 pub fn encrypt(
1005 &mut self,
1006 early_data: &[u8],
1007 outgoing_tls: &mut [u8],
1008 ) -> Result<usize, EarlyDataError> {
1009 let Some(allowed) = self
1010 .conn
1011 .core
1012 .data
1013 .early_data
1014 .check_write_opt(early_data.len())
1015 else {
1016 return Err(EarlyDataError::ExceededAllowedEarlyData);
1017 };
1018
1019 self.conn
1020 .core
1021 .common_state
1022 .write_plaintext(early_data[..allowed].into(), outgoing_tls)
1023 .map_err(|e| e.into())
1024 }
1025}
1026
1027/// Errors that may arise when encrypting early (RTT-0) data
1028#[derive(Debug)]
1029pub enum EarlyDataError {
1030 /// Cannot encrypt more early data due to imposed limits
1031 ExceededAllowedEarlyData,
1032 /// Encryption error
1033 Encrypt(EncryptError),
1034}
1035
1036impl From<EncryptError> for EarlyDataError {
1037 fn from(v: EncryptError) -> Self {
1038 Self::Encrypt(v)
1039 }
1040}
1041
1042impl fmt::Display for EarlyDataError {
1043 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1044 match self {
1045 Self::ExceededAllowedEarlyData => f.write_str("cannot send any more early data"),
1046 Self::Encrypt(e) => fmt::Display::fmt(e, f),
1047 }
1048 }
1049}
1050
1051#[cfg(feature = "std")]
1052impl std::error::Error for EarlyDataError {}
1053
1054/// State associated with a client connection.
1055#[derive(Debug)]
1056pub struct ClientConnectionData {
1057 pub(super) early_data: EarlyData,
1058 pub(super) ech_status: EchStatus,
1059}
1060
1061impl ClientConnectionData {
1062 fn new() -> Self {
1063 Self {
1064 early_data: EarlyData::new(),
1065 ech_status: EchStatus::NotOffered,
1066 }
1067 }
1068}
1069
1070impl crate::conn::SideData for ClientConnectionData {}