1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::{
borrow::Cow,
hash::{Hash, Hasher},
};
use super::{AlpnProtocol, AlpsProtocol, TlsVersion};
#[cfg(feature = "boring")]
use super::{CertificateCompressionAlgorithm, ExtensionType};
/// Builder for `[`TlsOptions`]`.
#[must_use]
#[derive(Debug, Clone)]
pub struct TlsOptionsBuilder {
config: TlsOptions,
}
/// TLS connection configuration options.
///
/// This struct provides fine-grained control over the behavior of TLS
/// connections, including:
/// - **Protocol negotiation** (ALPN, ALPS, TLS versions)
/// - **Session management** (tickets, PSK, key shares)
/// - **Security & privacy** (OCSP, GREASE, ECH, delegated credentials)
/// - **Performance tuning** (record size, cipher preferences, hardware overrides)
///
/// All fields are optional or have defaults. See each field for details.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TlsOptions {
/// Application-Layer Protocol Negotiation ([RFC 7301](https://datatracker.ietf.org/doc/html/rfc7301)).
///
/// Specifies which application protocols (e.g., HTTP/2, HTTP/1.1) may be negotiated
/// over a single TLS connection.
///
/// **Default:** `Some([HTTP/2, HTTP/1.1])`
pub alpn_protocols: Option<Cow<'static, [AlpnProtocol]>>,
/// Application-Layer Protocol Settings (ALPS).
///
/// Enables exchanging application-layer settings during the handshake
/// for protocols negotiated via ALPN.
///
/// **Default:** `None`
pub alps_protocols: Option<Cow<'static, [AlpsProtocol]>>,
/// Whether to use an alternative ALPS codepoint for compatibility.
///
/// Useful when larger ALPS payloads are required.
///
/// **Default:** `false`
pub alps_use_new_codepoint: bool,
/// Enables TLS Session Tickets ([RFC 5077](https://tools.ietf.org/html/rfc5077)).
///
/// Allows session resumption without requiring server-side state.
///
/// **Default:** `true`
pub session_ticket: bool,
/// Minimum TLS version allowed for the connection.
///
/// **Default:** `None` (library default applied)
pub min_tls_version: Option<TlsVersion>,
/// Maximum TLS version allowed for the connection.
///
/// **Default:** `None` (library default applied)
pub max_tls_version: Option<TlsVersion>,
/// Enables Pre-Shared Key (PSK) cipher suites ([RFC 4279](https://datatracker.ietf.org/doc/html/rfc4279)).
///
/// Authentication relies on out-of-band pre-shared keys instead of certificates.
///
/// **Default:** `false`
pub pre_shared_key: bool,
/// Controls whether to send a GREASE Encrypted ClientHello (ECH) extension
/// when no supported ECH configuration is available.
///
/// GREASE prevents protocol ossification by sending unknown extensions.
///
/// **Default:** `false`
pub enable_ech_grease: bool,
/// Controls whether ClientHello extensions should be permuted.
///
/// **Default:** `None` (implementation default)
pub permute_extensions: Option<bool>,
/// Controls whether GREASE extensions ([RFC 8701](https://datatracker.ietf.org/doc/html/rfc8701))
/// are enabled in general.
///
/// **Default:** `None` (implementation default)
pub grease_enabled: Option<bool>,
/// Enables OCSP stapling for the connection.
///
/// **Default:** `false`
pub enable_ocsp_stapling: bool,
/// Enables Signed Certificate Timestamps (SCT).
///
/// **Default:** `false`
pub enable_signed_cert_timestamps: bool,
/// Sets the maximum TLS record size.
///
/// **Default:** `None`
pub record_size_limit: Option<u16>,
/// Whether to skip session tickets when using PSK.
///
/// **Default:** `false`
pub psk_skip_session_ticket: bool,
/// Maximum number of key shares to include in ClientHello.
///
/// **Default:** `None`
pub key_shares_limit: Option<u8>,
/// Enables PSK with (EC)DHE key establishment (`psk_dhe_ke`).
///
/// **Default:** `true`
pub psk_dhe_ke: bool,
/// Enables TLS renegotiation by sending the `renegotiation_info` extension.
///
/// **Default:** `true`
pub renegotiation: bool,
/// Delegated Credentials ([RFC 9345](https://datatracker.ietf.org/doc/html/rfc9345)).
///
/// Allows TLS 1.3 endpoints to use temporary delegated credentials
/// for authentication with reduced long-term key exposure.
///
/// **Default:** `None`
pub delegated_credentials: Option<Cow<'static, str>>,
/// List of supported elliptic curves.
///
/// **Default:** `None`
pub curves_list: Option<Cow<'static, str>>,
/// Cipher suite configuration string.
///
/// Uses BoringSSL's mini-language to select, enable, and prioritize ciphers.
///
/// **Default:** `None`
pub cipher_list: Option<Cow<'static, str>>,
/// List of supported signature algorithms.
///
/// **Default:** `None`
pub sigalgs_list: Option<Cow<'static, str>>,
/// Supported certificate compression algorithms ([RFC 8879](https://datatracker.ietf.org/doc/html/rfc8879)).
///
/// **Default:** `None`
#[cfg(feature = "boring")]
pub certificate_compression_algorithms: Option<Cow<'static, [CertificateCompressionAlgorithm]>>,
/// Supported TLS extensions, used for extension ordering/permutation.
///
/// **Default:** `None`
#[cfg(feature = "boring")]
pub extension_permutation: Option<Cow<'static, [ExtensionType]>>,
/// Overrides AES hardware acceleration.
///
/// **Default:** `None`
pub aes_hw_override: Option<bool>,
/// Sets whether to preserve the TLS 1.3 cipher list as configured by [`Self::cipher_list`].
///
/// **Default:** `None`
pub preserve_tls13_cipher_list: Option<bool>,
/// Overrides the random AES hardware acceleration.
///
/// **Default:** `false`
pub random_aes_hw_override: bool,
}
impl Hash for TlsOptions {
fn hash<H: Hasher>(&self, state: &mut H) {
self.alpn_protocols.hash(state);
self.alps_protocols.hash(state);
self.alps_use_new_codepoint.hash(state);
self.session_ticket.hash(state);
self.min_tls_version.hash(state);
self.max_tls_version.hash(state);
self.pre_shared_key.hash(state);
self.enable_ech_grease.hash(state);
self.permute_extensions.hash(state);
self.grease_enabled.hash(state);
self.enable_ocsp_stapling.hash(state);
self.enable_signed_cert_timestamps.hash(state);
self.record_size_limit.hash(state);
self.psk_skip_session_ticket.hash(state);
self.key_shares_limit.hash(state);
self.psk_dhe_ke.hash(state);
self.renegotiation.hash(state);
self.delegated_credentials.hash(state);
self.curves_list.hash(state);
self.cipher_list.hash(state);
self.sigalgs_list.hash(state);
// certificate_compression_algorithms and extension_permutation do not implement Hash
// We skip them for hashing, relying on PartialEq for collision resolution.
self.aes_hw_override.hash(state);
self.preserve_tls13_cipher_list.hash(state);
self.random_aes_hw_override.hash(state);
}
}
impl TlsOptionsBuilder {
/// Sets the ALPN protocols to use.
#[inline]
pub fn alpn_protocols<I>(mut self, alpn: I) -> Self
where
I: IntoIterator<Item = AlpnProtocol>,
{
self.config.alpn_protocols = Some(Cow::Owned(alpn.into_iter().collect()));
self
}
/// Sets the ALPS protocols to use.
#[inline]
pub fn alps_protocols<I>(mut self, alps: I) -> Self
where
I: IntoIterator<Item = AlpsProtocol>,
{
self.config.alps_protocols = Some(Cow::Owned(alps.into_iter().collect()));
self
}
/// Sets whether to use a new codepoint for ALPS.
#[inline]
pub fn alps_use_new_codepoint(mut self, enabled: bool) -> Self {
self.config.alps_use_new_codepoint = enabled;
self
}
/// Sets the session ticket flag.
#[inline]
pub fn session_ticket(mut self, enabled: bool) -> Self {
self.config.session_ticket = enabled;
self
}
/// Sets the minimum TLS version to use.
#[inline]
pub fn min_tls_version<T>(mut self, version: T) -> Self
where
T: Into<Option<TlsVersion>>,
{
self.config.min_tls_version = version.into();
self
}
/// Sets the maximum TLS version to use.
#[inline]
pub fn max_tls_version<T>(mut self, version: T) -> Self
where
T: Into<Option<TlsVersion>>,
{
self.config.max_tls_version = version.into();
self
}
/// Sets the pre-shared key flag.
#[inline]
pub fn pre_shared_key(mut self, enabled: bool) -> Self {
self.config.pre_shared_key = enabled;
self
}
/// Sets the GREASE ECH extension flag.
#[inline]
pub fn enable_ech_grease(mut self, enabled: bool) -> Self {
self.config.enable_ech_grease = enabled;
self
}
/// Sets whether to permute ClientHello extensions.
#[inline]
pub fn permute_extensions<T>(mut self, permute: T) -> Self
where
T: Into<Option<bool>>,
{
self.config.permute_extensions = permute.into();
self
}
/// Sets the GREASE enabled flag.
#[inline]
pub fn grease_enabled<T>(mut self, enabled: T) -> Self
where
T: Into<Option<bool>>,
{
self.config.grease_enabled = enabled.into();
self
}
/// Sets the OCSP stapling flag.
#[inline]
pub fn enable_ocsp_stapling(mut self, enabled: bool) -> Self {
self.config.enable_ocsp_stapling = enabled;
self
}
/// Sets the signed certificate timestamps flag.
#[inline]
pub fn enable_signed_cert_timestamps(mut self, enabled: bool) -> Self {
self.config.enable_signed_cert_timestamps = enabled;
self
}
/// Sets the record size limit.
#[inline]
pub fn record_size_limit<U: Into<Option<u16>>>(mut self, limit: U) -> Self {
self.config.record_size_limit = limit.into();
self
}
/// Sets the PSK skip session ticket flag.
#[inline]
pub fn psk_skip_session_ticket(mut self, skip: bool) -> Self {
self.config.psk_skip_session_ticket = skip;
self
}
/// Sets the key shares length limit.
#[inline]
pub fn key_shares_limit<T>(mut self, limit: T) -> Self
where
T: Into<Option<u8>>,
{
self.config.key_shares_limit = limit.into();
self
}
/// Sets the PSK DHE key establishment flag.
#[inline]
pub fn psk_dhe_ke(mut self, enabled: bool) -> Self {
self.config.psk_dhe_ke = enabled;
self
}
/// Sets the renegotiation flag.
#[inline]
pub fn renegotiation(mut self, enabled: bool) -> Self {
self.config.renegotiation = enabled;
self
}
/// Sets the delegated credentials.
#[inline]
pub fn delegated_credentials<T>(mut self, creds: T) -> Self
where
T: Into<Cow<'static, str>>,
{
self.config.delegated_credentials = Some(creds.into());
self
}
/// Sets the supported curves list.
#[inline]
pub fn curves_list<T>(mut self, curves: T) -> Self
where
T: Into<Cow<'static, str>>,
{
self.config.curves_list = Some(curves.into());
self
}
/// Sets the cipher list.
#[inline]
pub fn cipher_list<T>(mut self, ciphers: T) -> Self
where
T: Into<Cow<'static, str>>,
{
self.config.cipher_list = Some(ciphers.into());
self
}
/// Sets the supported signature algorithms.
#[inline]
pub fn sigalgs_list<T>(mut self, sigalgs: T) -> Self
where
T: Into<Cow<'static, str>>,
{
self.config.sigalgs_list = Some(sigalgs.into());
self
}
/// Sets the certificate compression algorithms.
#[inline]
#[cfg(feature = "boring")]
pub fn certificate_compression_algorithms<T>(mut self, algs: T) -> Self
where
T: Into<Cow<'static, [CertificateCompressionAlgorithm]>>,
{
self.config.certificate_compression_algorithms = Some(algs.into());
self
}
/// Sets the extension permutation.
#[inline]
#[cfg(feature = "boring")]
pub fn extension_permutation<T>(mut self, permutation: T) -> Self
where
T: Into<Cow<'static, [ExtensionType]>>,
{
self.config.extension_permutation = Some(permutation.into());
self
}
/// Sets the AES hardware override flag.
#[inline]
pub fn aes_hw_override<T>(mut self, enabled: T) -> Self
where
T: Into<Option<bool>>,
{
self.config.aes_hw_override = enabled.into();
self
}
/// Sets the random AES hardware override flag.
#[inline]
pub fn random_aes_hw_override(mut self, enabled: bool) -> Self {
self.config.random_aes_hw_override = enabled;
self
}
/// Sets whether to preserve the TLS 1.3 cipher list as configured by [`Self::cipher_list`].
///
/// By default, BoringSSL does not preserve the TLS 1.3 cipher list. When this option is
/// disabled (the default), BoringSSL uses its internal default TLS 1.3 cipher suites in its
/// default order, regardless of what is set via [`Self::cipher_list`].
///
/// When enabled, this option ensures that the TLS 1.3 cipher suites explicitly set via
/// [`Self::cipher_list`] are retained in their original order, without being reordered or
/// modified by BoringSSL's internal logic. This is useful for maintaining specific cipher suite
/// priorities for TLS 1.3. Note that if [`Self::cipher_list`] does not include any TLS 1.3
/// cipher suites, BoringSSL will still fall back to its default TLS 1.3 cipher suites and
/// order.
#[inline]
pub fn preserve_tls13_cipher_list<T>(mut self, enabled: T) -> Self
where
T: Into<Option<bool>>,
{
self.config.preserve_tls13_cipher_list = enabled.into();
self
}
/// Builds the `TlsOptions` from the builder.
#[inline]
pub fn build(self) -> TlsOptions {
self.config
}
}
impl TlsOptions {
/// Creates a new `TlsOptionsBuilder` instance.
pub fn builder() -> TlsOptionsBuilder {
TlsOptionsBuilder {
config: TlsOptions::default(),
}
}
}
impl Default for TlsOptions {
fn default() -> Self {
TlsOptions {
alpn_protocols: Some(Cow::Borrowed(&[AlpnProtocol::HTTP2, AlpnProtocol::HTTP1])),
alps_protocols: None,
alps_use_new_codepoint: false,
session_ticket: true,
min_tls_version: None,
max_tls_version: None,
pre_shared_key: false,
enable_ech_grease: false,
permute_extensions: None,
grease_enabled: None,
enable_ocsp_stapling: false,
enable_signed_cert_timestamps: false,
record_size_limit: None,
psk_skip_session_ticket: false,
key_shares_limit: None,
psk_dhe_ke: true,
renegotiation: true,
delegated_credentials: None,
curves_list: None,
cipher_list: None,
sigalgs_list: None,
#[cfg(feature = "boring")]
certificate_compression_algorithms: None,
#[cfg(feature = "boring")]
extension_permutation: None,
aes_hw_override: None,
preserve_tls13_cipher_list: None,
random_aes_hw_override: false,
}
}
}