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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! TLS options configuration
//!
//! - Various parts of TLS can also be configured or even disabled on the `ClientBuilder`.
pub(super) mod conn;
pub mod compress;
pub mod keylog;
pub mod session;
pub mod trust;
use std::borrow::Cow;
/// Re-exports of TLS-related types from `btls` for public use.
pub use btls::ssl::{ExtensionType, KeyShare};
use bytes::{BufMut, Bytes, BytesMut};
use compress::CertificateCompressor;
/// Http extension carrying extra TLS layer information.
/// Made available to clients on responses when `tls_info` is set.
#[derive(Debug, Clone)]
pub struct TlsInfo {
pub(crate) peer_certificate: Option<Bytes>,
pub(crate) peer_certificate_chain: Option<Vec<Bytes>>,
}
impl TlsInfo {
/// Get the DER encoded leaf certificate of the peer.
pub fn peer_certificate(&self) -> Option<&[u8]> {
self.peer_certificate.as_deref()
}
/// Get the DER encoded certificate chain of the peer.
///
/// This includes the leaf certificate on the client side.
pub fn peer_certificate_chain(&self) -> Option<impl Iterator<Item = &[u8]>> {
self.peer_certificate_chain
.as_ref()
.map(|v| v.iter().map(|b| b.as_ref()))
}
}
/// A TLS protocol version.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct TlsVersion(btls::ssl::SslVersion);
impl TlsVersion {
/// Version 1.0 of the TLS protocol.
pub const TLS_1_0: TlsVersion = TlsVersion(btls::ssl::SslVersion::TLS1);
/// Version 1.1 of the TLS protocol.
pub const TLS_1_1: TlsVersion = TlsVersion(btls::ssl::SslVersion::TLS1_1);
/// Version 1.2 of the TLS protocol.
pub const TLS_1_2: TlsVersion = TlsVersion(btls::ssl::SslVersion::TLS1_2);
/// Version 1.3 of the TLS protocol.
pub const TLS_1_3: TlsVersion = TlsVersion(btls::ssl::SslVersion::TLS1_3);
}
/// A TLS ALPN protocol.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct AlpnProtocol(&'static [u8]);
impl AlpnProtocol {
/// Prefer HTTP/1.1
pub const HTTP1: AlpnProtocol = AlpnProtocol(b"http/1.1");
/// Prefer HTTP/2
pub const HTTP2: AlpnProtocol = AlpnProtocol(b"h2");
/// Prefer HTTP/3
pub const HTTP3: AlpnProtocol = AlpnProtocol(b"h3");
#[inline]
fn encode(self) -> Bytes {
Self::encode_sequence(std::iter::once(&self))
}
fn encode_sequence<'a, I>(items: I) -> Bytes
where
I: IntoIterator<Item = &'a AlpnProtocol>,
{
let mut buf = BytesMut::new();
for item in items {
buf.put_u8(item.0.len() as u8);
buf.extend_from_slice(item.0);
}
buf.freeze()
}
}
impl PartialEq<[u8]> for AlpnProtocol {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
self.0 == other
}
}
/// A TLS ALPS protocol.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct AlpsProtocol(&'static [u8]);
impl AlpsProtocol {
/// Prefer HTTP/1.1
pub const HTTP1: AlpsProtocol = AlpsProtocol(b"http/1.1");
/// Prefer HTTP/2
pub const HTTP2: AlpsProtocol = AlpsProtocol(b"h2");
/// Prefer HTTP/3
pub const HTTP3: AlpsProtocol = AlpsProtocol(b"h3");
}
impl PartialEq<[u8]> for AlpsProtocol {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
self.0 == other
}
}
/// 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.
#[non_exhaustive]
#[derive(Debug, Clone)]
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,
/// Whether to set specific key shares for TLS 1.3 handshakes.
///
/// **Default:** `None`
pub key_shares: Option<Cow<'static, [KeyShare]>>,
/// 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>>,
/// List of supported signature algorithms.
///
/// **Default:** `None`
pub sigalgs_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>>,
/// 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>,
/// Supported certificate compression algorithms ([RFC 8879](https://datatracker.ietf.org/doc/html/rfc8879)).
///
/// **Default:** `None`
pub certificate_compressors: Option<Cow<'static, [&'static dyn CertificateCompressor]>>,
/// Supported TLS extensions, used for extension ordering/permutation.
///
/// **Default:** `None`
pub extension_permutation: Option<Cow<'static, [ExtensionType]>>,
/// Overrides AES hardware acceleration.
///
/// **Default:** `None`
pub aes_hw_override: Option<bool>,
/// Overrides the random AES hardware acceleration.
///
/// **Default:** `false`
pub random_aes_hw_override: bool,
}
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 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 client key shares to be used in the TLS 1.3 handshake.
#[inline]
pub fn key_shares<T>(mut self, key_shares: T) -> Self
where
T: Into<Cow<'static, [KeyShare]>>,
{
self.config.key_shares = Some(key_shares.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]
pub fn certificate_compressors<T>(mut self, algs: T) -> Self
where
T: Into<Cow<'static, [&'static dyn CertificateCompressor]>>,
{
self.config.certificate_compressors = Some(algs.into());
self
}
/// Sets the extension permutation.
#[inline]
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: None,
psk_dhe_ke: true,
renegotiation: true,
delegated_credentials: None,
curves_list: None,
cipher_list: None,
sigalgs_list: None,
certificate_compressors: None,
extension_permutation: None,
aes_hw_override: None,
preserve_tls13_cipher_list: None,
random_aes_hw_override: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alpn_protocol_encode() {
let alpn = AlpnProtocol::encode_sequence(&[AlpnProtocol::HTTP1, AlpnProtocol::HTTP2]);
assert_eq!(alpn, Bytes::from_static(b"\x08http/1.1\x02h2"));
let alpn = AlpnProtocol::encode_sequence(&[AlpnProtocol::HTTP3]);
assert_eq!(alpn, Bytes::from_static(b"\x02h3"));
let alpn = AlpnProtocol::encode_sequence(&[AlpnProtocol::HTTP1, AlpnProtocol::HTTP3]);
assert_eq!(alpn, Bytes::from_static(b"\x08http/1.1\x02h3"));
let alpn = AlpnProtocol::encode_sequence(&[AlpnProtocol::HTTP2, AlpnProtocol::HTTP3]);
assert_eq!(alpn, Bytes::from_static(b"\x02h2\x02h3"));
let alpn = AlpnProtocol::encode_sequence(&[
AlpnProtocol::HTTP1,
AlpnProtocol::HTTP2,
AlpnProtocol::HTTP3,
]);
assert_eq!(alpn, Bytes::from_static(b"\x08http/1.1\x02h2\x02h3"));
}
#[test]
fn alpn_protocol_encode_single() {
let alpn = AlpnProtocol::HTTP1.encode();
assert_eq!(alpn, b"\x08http/1.1".as_ref());
let alpn = AlpnProtocol::HTTP2.encode();
assert_eq!(alpn, b"\x02h2".as_ref());
let alpn = AlpnProtocol::HTTP3.encode();
assert_eq!(alpn, b"\x02h3".as_ref());
}
}