wtx 0.52.1

A collection of different transport implementations and related tools focused primarily on web technologies.
Documentation
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
use crate::{
  calendar::{DateTime, Instant, Utc},
  collections::{ArrayVectorCopy, ShortBoxSliceU8, ShortBoxSliceU16, SingleTypeStorage, Vector},
  misc::{Lease, LeaseMut},
  rng::CryptoRng,
  tls::{
    Alpn, CipherSuite, MaxFragmentLength, NamedGroup, PlaintextCtx, PublicKeys, ServerNameList,
    TlsCtxSkInput, TlsCtxSkLoader, TrustedCtx, UnverifiedCtx,
    protocol::{
      signature_algorithms::SignatureAlgorithms,
      signature_algorithms_cert::SignatureAlgorithmsCert, supported_groups::SupportedGroups,
    },
  },
  x509::{Certificate, CvPolicy, CvTrustAnchor},
};
use core::{fmt::Debug, mem};

/// TLS Configuration
///
/// This is a non-trivial structure that should be constructed only once in your application.
pub struct TlsConfig<TCX> {
  pub(crate) inner: TlsConfigInner<ShortBoxSliceU16<u8>, TCX>,
}

impl TlsConfig<PlaintextCtx> {
  /// Placeholder used in locals where data is expected to be unencrypted.
  #[inline]
  pub fn plaintext() -> Self {
    Self { inner: TlsConfigInner::new(PlaintextCtx::new(), DateTime::default()) }
  }
}

impl TlsConfig<TrustedCtx> {
  /// Set of filtered certificates from CCADB generally suitable for web scenarios.
  ///
  /// Fetches the current timestamp to verify certificates.
  #[cfg(feature = "ccadb")]
  #[inline]
  pub fn from_ccadb() -> crate::Result<Self> {
    let mut trust_anchors = Vector::new();
    for elem in crate::x509::CCADB {
      trust_anchors.push(CvTrustAnchor::_from_raw(*elem)?)?;
    }
    let mut this = Self::new(TrustedCtx::new())?;
    this.inner.trust_anchors = trust_anchors.try_into()?;
    Ok(this)
  }

  /// New instance from the given full X.509 trust anchors in PEM format. Mostly used by clients.
  ///
  /// Fetches the current timestamp to verify certificates
  #[inline]
  pub fn from_trust_anchors_pem<'bytes>(
    trust_anchors: impl IntoIterator<Item = &'bytes [u8]>,
  ) -> crate::Result<Self> {
    let mut this = Self::new(TrustedCtx::new())?;
    this.set_trust_anchors_pem(trust_anchors)?;
    Ok(this)
  }
}

impl TlsConfig<UnverifiedCtx> {
  /// Placeholder used in locals where data is expected to be unverified.
  #[inline]
  pub fn unverified() -> Self {
    Self { inner: TlsConfigInner::new(UnverifiedCtx::new(), DateTime::default()) }
  }
}

impl<TCX> TlsConfig<TCX>
where
  TCX: TlsCtxSkLoader,
{
  /// New instance from a public key chain and a secret key in DER format. Mostly used by servers.
  ///
  /// Fetches the current timestamp to verify certificates
  #[inline]
  pub fn from_keys_der<'pk, 'sk, RNG, SK>(
    public_key: impl IntoIterator<Item = &'pk [u8]>,
    rng: &mut RNG,
    secret_key: SK,
  ) -> crate::Result<Self>
  where
    RNG: CryptoRng,
    SK: TlsCtxSkInput<TlsCtxSk = TCX>,
    TCX: TlsCtxSkLoader<SkInputDer<'sk> = SK>,
  {
    let mut this = Self::new(TCX::from_ders([secret_key], rng)?)?;
    this.set_public_keys_der([public_key])?;
    Ok(this)
  }

  /// New instance from a public key chain and a secret key in PEM format. Mostly used by servers.
  ///
  /// Fetches the current timestamp to verify certificates
  #[inline]
  pub fn from_keys_pem<'sk, RNG, SK>(
    public_key: &[u8],
    rng: &mut RNG,
    secret_key: SK,
  ) -> crate::Result<Self>
  where
    RNG: CryptoRng,
    SK: TlsCtxSkInput<TlsCtxSk = TCX>,
    TCX: TlsCtxSkLoader<SkInputPem<'sk> = SK>,
  {
    let mut this = Self::new(TCX::from_pems([secret_key], rng)?)?;
    this.set_public_keys_pem([public_key])?;
    Ok(this)
  }
}

impl<TCX> TlsConfig<TCX> {
  /// New instance that doesn't incorporate any initial certificate, which will likely make
  /// connections fail. However, it is still possible to add certificates using mutable methods.
  ///
  /// Fetches the current timestamp to verify certificates.
  #[inline]
  pub fn new(ctx: TCX) -> crate::Result<Self> {
    Ok(Self::from_validation_time(ctx, Instant::now_date_time()?))
  }

  /// Adjusts the validation time of [`CvPolicy`] that regulates certificate expiration.
  ///
  /// Taking aside [`Self::plaintext`], all other constructors implicitly fetch the current time.
  #[inline]
  pub fn from_validation_time(ctx: TCX, validation_time: DateTime<Utc>) -> Self {
    Self { inner: TlsConfigInner::new(ctx, validation_time) }
  }
}

impl<TCX> TlsConfig<TCX> {
  /// See [`Alpn`].
  #[inline]
  pub const fn alpn(&self) -> &Option<Alpn> {
    &self.inner.alpn
  }

  /// Mutable version of [`Self::alpn`].
  #[inline]
  pub const fn alpn_mut(&mut self) -> &mut Option<Alpn> {
    &mut self.inner.alpn
  }

  /// See [`CipherSuite`].
  #[inline]
  pub const fn cipher_suites(
    &self,
  ) -> &ArrayVectorCopy<CipherSuite, { CipherSuite::PRIORITY.len() }> {
    &self.inner.cipher_suites
  }

  /// Mutable version of [`Self::cipher_suites`].
  #[inline]
  pub const fn cipher_suites_mut(
    &mut self,
  ) -> &mut ArrayVectorCopy<CipherSuite, { CipherSuite::PRIORITY.len() }> {
    &mut self.inner.cipher_suites
  }

  /// See [`crate::tls::TlsCtx`].
  #[inline]
  pub const fn ctx(&self) -> &TCX {
    &self.inner.ctx
  }

  /// See [`CvPolicy`].
  #[inline]
  pub const fn cv_policy(&self) -> &CvPolicy<ShortBoxSliceU16<u8>> {
    &self.inner.cv_policy
  }

  /// Mutable version of [`Self::cv_policy`].
  #[inline]
  pub const fn cv_policy_mut(&mut self) -> &mut CvPolicy<ShortBoxSliceU16<u8>> {
    &mut self.inner.cv_policy
  }

  /// Maximum record size the local TLS instance is willing to accept from peers.
  ///
  /// * `Clients`: Servers must accept sent non-null parameters, otherwise the handshake will fail.
  /// * `Servers`: Forbids received values that are greater then non-nul parameters.
  ///
  /// If [`None`], defaults to `2^14`.
  #[inline]
  pub const fn max_fragment_length(&self) -> Option<MaxFragmentLength> {
    self.inner.max_fragment_length
  }

  /// Mutable version of [`Self::max_fragment_length`].
  ///
  /// If [`None`], defaults to `2^14`.
  #[inline]
  pub const fn max_fragment_length_mut(&mut self) -> &mut Option<MaxFragmentLength> {
    &mut self.inner.max_fragment_length
  }

  /// Maximum record size the local TLS instance can send to peers.
  ///
  /// If [`None`], defaults to `2^14`.
  #[inline]
  pub const fn max_fragment_length_send(&self) -> Option<MaxFragmentLength> {
    self.inner.max_fragment_length_send
  }

  /// Mutable version of [`Self::max_fragment_length_send`].
  ///
  /// If [`None`], defaults to `2^14`.
  #[inline]
  pub const fn max_fragment_length_send_mut(&mut self) -> &mut Option<MaxFragmentLength> {
    &mut self.inner.max_fragment_length_send
  }

  /// See [`PublicKeys`].
  #[inline]
  pub const fn public_keys(&self) -> &PublicKeys {
    &self.inner.public_keys
  }

  /// See [`ServerNameList`].
  #[inline]
  pub const fn server_name(&self) -> &Option<ServerNameList> {
    &self.inner.server_name
  }

  /// Mutable version of [`Self::server_name`].
  #[inline]
  pub const fn server_name_mut(&mut self) -> &mut Option<ServerNameList> {
    &mut self.inner.server_name
  }

  /// See [`ServerNameList`].
  #[inline]
  pub fn set_tls_mode<_TM>(self, value: _TM) -> TlsConfig<_TM> {
    TlsConfig {
      inner: TlsConfigInner {
        alpn: self.inner.alpn,
        cipher_suites: self.inner.cipher_suites,
        ctx: value,
        cv_policy: self.inner.cv_policy,
        max_fragment_length: self.inner.max_fragment_length,
        max_fragment_length_send: self.inner.max_fragment_length_send,
        public_keys: self.inner.public_keys,
        server_name: self.inner.server_name,
        signature_algorithms: self.inner.signature_algorithms,
        signature_algorithms_cert: self.inner.signature_algorithms_cert,
        supported_groups: self.inner.supported_groups,
        trust_anchors: self.inner.trust_anchors,
        unique_signature_algorithms: self.inner.unique_signature_algorithms,
      },
    }
  }

  /// Converts X.509 certificates in DER format into public keys.
  #[inline]
  pub fn set_public_keys_der<'pkc, PKC, PKS>(&mut self, public_keys: PKS) -> crate::Result<()>
  where
    PKC: IntoIterator<Item = &'pkc [u8]>,
    PKS: IntoIterator<Item = PKC>,
  {
    self.inner.public_keys.clear();
    for certs in public_keys {
      self.inner.public_keys.push_public_key_der(certs)?;
    }
    Ok(())
  }

  /// Converts X.509 certificates in PEM format into public keys.
  #[inline]
  pub fn set_public_keys_pem<'pems>(
    &mut self,
    pems: impl IntoIterator<Item = &'pems [u8]>,
  ) -> crate::Result<()> {
    let mut buffer = Vector::new();
    self.inner.public_keys.clear();
    for pem in pems {
      self.inner.public_keys.push_public_key_pem(&mut buffer, pem)?;
      buffer.clear();
    }
    Ok(())
  }

  /// Converts X.509 certificates in PEM format into trust anchors.
  #[inline]
  pub fn set_trust_anchors_pem<'bytes>(
    &mut self,
    trust_anchors: impl IntoIterator<Item = &'bytes [u8]>,
  ) -> crate::Result<()> {
    let mut buffer = Vector::new();
    let mut local_trust_anchors: Vector<_> = mem::take(&mut self.inner.trust_anchors).into();
    local_trust_anchors.clear();
    for trust_anchor in trust_anchors {
      buffer.clear();
      let cert = Certificate::<&[u8]>::from_pem(&mut buffer, trust_anchor)?.0;
      local_trust_anchors.push(CvTrustAnchor::from_certificate_ref(&cert)?)?;
    }
    self.inner.trust_anchors = local_trust_anchors.try_into()?;
    Ok(())
  }

  /// Every instance of [`TlsConfig`] is already pre-filled with a list of signature algorithms.
  ///
  /// The values are filtered in servers according to the provided set of certificates.
  ///
  /// See [`SignatureAlgorithms`].
  #[inline]
  pub const fn signature_algorithms(&self) -> &SignatureAlgorithms {
    &self.inner.signature_algorithms
  }

  /// Mutable version of [`Self::signature_algorithms`].
  #[inline]
  pub const fn signature_algorithms_mut(&mut self) -> &mut SignatureAlgorithms {
    &mut self.inner.signature_algorithms
  }

  /// See [`SupportedGroups`].
  #[inline]
  pub const fn supported_groups(&self) -> &SupportedGroups {
    &self.inner.supported_groups
  }

  /// Mutable version of [`Self::supported_groups`].
  #[inline]
  pub const fn supported_groups_mut(&mut self) -> &mut SupportedGroups {
    &mut self.inner.supported_groups
  }

  /// See [`CvTrustAnchor`].
  #[inline]
  pub fn trust_anchors(&self) -> &[CvTrustAnchor<ShortBoxSliceU16<u8>>] {
    &self.inner.trust_anchors
  }

  /// If signature schemes should be associated to a single certificate.
  ///
  /// NO-OP for clients.
  #[inline]
  pub const fn unique_signature_algorithms(&self) -> bool {
    self.inner.unique_signature_algorithms
  }

  /// Mutable version of [`Self::unique_signature_algorithms`].
  #[inline]
  pub const fn unique_signature_algorithms_mut(&mut self) -> &mut bool {
    &mut self.inner.unique_signature_algorithms
  }
}

impl<TCX> Lease<TlsConfig<TCX>> for TlsConfig<TCX> {
  #[inline]
  fn lease(&self) -> &TlsConfig<TCX> {
    self
  }
}

impl<TCX> LeaseMut<TlsConfig<TCX>> for TlsConfig<TCX> {
  #[inline]
  fn lease_mut(&mut self) -> &mut TlsConfig<TCX> {
    self
  }
}

impl<TCX> SingleTypeStorage for TlsConfig<TCX> {
  type Item = TCX;
}

impl<TCX> Debug for TlsConfig<TCX>
where
  TCX: Debug,
{
  #[inline]
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    self.inner.fmt(f)
  }
}

#[derive(Debug)]
pub(crate) struct TlsConfigInner<B, TCX> {
  pub(crate) alpn: Option<Alpn>,
  pub(crate) cipher_suites: ArrayVectorCopy<CipherSuite, { CipherSuite::PRIORITY.len() }>,
  pub(crate) ctx: TCX,
  pub(crate) cv_policy: CvPolicy<B>,
  pub(crate) max_fragment_length: Option<MaxFragmentLength>,
  pub(crate) max_fragment_length_send: Option<MaxFragmentLength>,
  pub(crate) public_keys: PublicKeys,
  pub(crate) server_name: Option<ServerNameList>,
  pub(crate) signature_algorithms: SignatureAlgorithms,
  pub(crate) signature_algorithms_cert: Option<SignatureAlgorithmsCert>,
  pub(crate) supported_groups: SupportedGroups,
  pub(crate) trust_anchors: ShortBoxSliceU8<CvTrustAnchor<B>>,
  pub(crate) unique_signature_algorithms: bool,
}

impl<B, TCX> TlsConfigInner<B, TCX>
where
  B: Default,
{
  #[inline]
  fn new(ctx: TCX, validation_time: DateTime<Utc>) -> Self {
    Self {
      alpn: None,
      cipher_suites: ArrayVectorCopy::from_array(CipherSuite::PRIORITY),
      cv_policy: CvPolicy::new(validation_time),
      ctx,
      max_fragment_length: None,
      max_fragment_length_send: None,
      public_keys: PublicKeys::default(),
      server_name: None,
      signature_algorithms: SignatureAlgorithms::default(),
      signature_algorithms_cert: Some(SignatureAlgorithmsCert::default()),
      supported_groups: SupportedGroups::new(ArrayVectorCopy::from_array(NamedGroup::PRIORITY)),
      trust_anchors: ShortBoxSliceU8::default(),
      unique_signature_algorithms: false,
    }
  }
}