nexora-proto 0.0.1

Sans-IO HTTP, TLS, and WebSocket protocol codecs
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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! In-memory TLS 1.3 sessions. Transport reads and writes remain caller-owned.

use std::{
    io::{ErrorKind, Read, Write},
    sync::Arc,
};

use rustls::{
    ClientConnection, Connection, DigitallySignedStruct, RootCertStore, ServerConnection,
    client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
    pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime, pem::PemObject},
    server::{WebPkiClientVerifier, danger::ClientCertVerifier},
    sign::{CertifiedKey, SingleCertAndKey},
};

use crate::{BytesMut, ProtoError, Result};

/// Application protocols supported by the TLS adapter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlpnProtocol {
    /// HTTP/1.1, advertised as `http/1.1`.
    Http11,
    /// HTTP/2, advertised as `h2`.
    Http2,
}

/// The local endpoint's handshake role.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TlsRole {
    /// Initiates the TLS handshake and verifies the server identity.
    Client,
    /// Accepts the handshake using its configured identity.
    Server,
}

/// Reusable TLS 1.3 client policy, using the ring cryptographic provider.
#[derive(Debug, Clone)]
pub struct TlsClientConfig {
    roots: Arc<RootCertStore>,
    alpn: Vec<Vec<u8>>,
    insecure: bool,
    client_auth: Option<Arc<CertifiedKey>>,
}

impl Default for TlsClientConfig {
    /// Uses public WebPKI trust anchors and prefers HTTP/2 over HTTP/1.1.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    fn default() -> Self {
        Self {
            roots: Arc::new(RootCertStore {
                roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
            }),
            alpn: encode_alpn(&[AlpnProtocol::Http2, AlpnProtocol::Http11]),
            insecure: false,
            client_auth: None,
        }
    }
}

impl TlsClientConfig {
    /// Reports whether this policy permits HTTP/1.1 fallback.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn allows_http11(&self) -> bool {
        self.alpn.iter().any(|p| p.as_slice() == b"http/1.1")
    }

    /// Replaces the advertised ALPN protocols, retaining trust and client identity.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn with_alpn(mut self, alpn: &[AlpnProtocol]) -> Self {
        self.alpn = encode_alpn(alpn);
        self
    }

    /// Creates a test policy that skips certificate identity and trust checks.
    ///
    /// Handshake signatures are still verified. Do not use this policy with
    /// untrusted peers; it allows impersonation of any server name.
    ///
    /// # Errors
    /// This constructor currently always succeeds.
    ///
    /// # Panics
    /// Does not panic.
    pub fn insecure_client(alpn: &[AlpnProtocol]) -> Result<Self> {
        Ok(Self {
            roots: Arc::new(RootCertStore::empty()),
            alpn: encode_alpn(alpn),
            insecure: true,
            client_auth: None,
        })
    }

    /// Replaces public roots with the supplied DER-encoded trust anchors.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] if any trust anchor is invalid.
    ///
    /// # Panics
    /// Does not panic.
    pub fn with_root_certificates(cert_der: &[Vec<u8>], alpn: &[AlpnProtocol]) -> Result<Self> {
        let mut roots = RootCertStore::empty();
        for cert in cert_der {
            roots
                .add(CertificateDer::from(cert.clone()))
                .map_err(tls_error)?;
        }
        Ok(Self {
            roots: Arc::new(roots),
            alpn: encode_alpn(alpn),
            insecure: false,
            client_auth: None,
        })
    }

    /// Replaces public roots with every certificate in a PEM trust bundle.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for malformed PEM, invalid certificates, or
    /// a bundle containing no certificates.
    ///
    /// # Panics
    /// Does not panic.
    pub fn with_pem_root_certificates(cert_pem: &[u8], alpn: &[AlpnProtocol]) -> Result<Self> {
        let certificates = CertificateDer::pem_slice_iter(cert_pem)
            .map(|item| item.map(|cert| cert.to_vec()).map_err(tls_error))
            .collect::<Result<Vec<_>>>()?;
        if certificates.is_empty() {
            return Err(ProtoError::Tls(
                "PEM bundle contains no certificates".to_owned(),
            ));
        }
        Self::with_root_certificates(&certificates, alpn)
    }

    /// Adds a DER client identity for servers requiring mutual authentication.
    ///
    /// The first certificate must match the supplied private key. Existing trust
    /// and ALPN policy are retained; peers still validate the identity's trust,
    /// validity period, and client-authentication usage during the handshake.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for an invalid identity or mismatched key.
    ///
    /// # Panics
    /// Does not panic.
    pub fn with_client_auth(
        mut self,
        cert_chain_der: Vec<Vec<u8>>,
        private_key_der: Vec<u8>,
    ) -> Result<Self> {
        let identity = CertifiedKey::from_der(
            cert_chain_der
                .into_iter()
                .map(CertificateDer::from)
                .collect(),
            PrivateKeyDer::try_from(private_key_der).map_err(tls_error)?,
            &rustls::crypto::ring::default_provider(),
        )
        .map_err(tls_error)?;
        self.client_auth = Some(Arc::new(identity));
        Ok(self)
    }
}

/// Reusable TLS 1.3 server identity and ALPN preference order.
#[derive(Debug, Clone)]
pub struct TlsServerConfig {
    inner: Arc<rustls::ServerConfig>,
}

impl TlsServerConfig {
    /// Loads a DER certificate chain and a PKCS#1, PKCS#8, or SEC1 private key.
    ///
    /// The first certificate must identify the server and match the key.
    /// Client certificates are not requested.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for an invalid identity or cryptographic policy.
    ///
    /// # Panics
    /// Does not panic.
    pub fn from_der(
        cert_chain_der: Vec<Vec<u8>>,
        private_key_der: Vec<u8>,
        alpn: &[AlpnProtocol],
    ) -> Result<Self> {
        Self::from_der_with_verifier(
            cert_chain_der,
            private_key_der,
            alpn,
            WebPkiClientVerifier::no_client_auth(),
        )
    }

    /// Loads a server identity and requires a client certificate trusted by the
    /// supplied DER authorities. Unauthenticated clients are rejected.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for an invalid server identity, invalid or
    /// empty client authorities, or unsupported cryptographic policy.
    ///
    /// # Panics
    /// Does not panic.
    pub fn from_der_with_client_auth(
        cert_chain_der: Vec<Vec<u8>>,
        private_key_der: Vec<u8>,
        client_ca_der: &[Vec<u8>],
        alpn: &[AlpnProtocol],
    ) -> Result<Self> {
        let mut roots = RootCertStore::empty();
        for cert in client_ca_der {
            roots
                .add(CertificateDer::from(cert.clone()))
                .map_err(tls_error)?;
        }
        let verifier = WebPkiClientVerifier::builder_with_provider(
            Arc::new(roots),
            Arc::new(rustls::crypto::ring::default_provider()),
        )
        .build()
        .map_err(tls_error)?;
        Self::from_der_with_verifier(cert_chain_der, private_key_der, alpn, verifier)
    }

    fn from_der_with_verifier(
        cert_chain_der: Vec<Vec<u8>>,
        private_key_der: Vec<u8>,
        alpn: &[AlpnProtocol],
        verifier: Arc<dyn ClientCertVerifier>,
    ) -> Result<Self> {
        let chain = cert_chain_der
            .into_iter()
            .map(CertificateDer::from)
            .collect();
        let key = PrivateKeyDer::try_from(private_key_der).map_err(tls_error)?;
        let mut inner = rustls::ServerConfig::builder_with_provider(Arc::new(
            rustls::crypto::ring::default_provider(),
        ))
        .with_protocol_versions(&[&rustls::version::TLS13])
        .map_err(tls_error)?
        .with_client_cert_verifier(verifier)
        .with_single_cert(chain, key)
        .map_err(tls_error)?;
        inner.alpn_protocols = encode_alpn(alpn);
        Ok(Self {
            inner: Arc::new(inner),
        })
    }

    /// Generates a fresh localhost identity and returns its DER trust anchor.
    ///
    /// The certificate also covers the loopback IP addresses `127.0.0.1` and
    /// `::1`. Key generation uses the system random source.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] if identity generation or loading fails.
    ///
    /// # Panics
    /// Does not panic.
    pub fn self_signed_for_localhost(alpn: &[AlpnProtocol]) -> Result<(Self, Vec<u8>)> {
        let identity = rcgen::generate_simple_self_signed(vec![
            "localhost".to_owned(),
            "127.0.0.1".to_owned(),
            "::1".to_owned(),
        ])
        .map_err(tls_error)?;
        let cert_der = identity.cert.der().to_vec();
        let config = Self::from_der(
            vec![cert_der.clone()],
            identity.key_pair.serialize_der(),
            alpn,
        )?;
        Ok((config, cert_der))
    }
}

/// TLS record processing with no socket ownership or blocking I/O.
///
/// `feed_ciphertext` owns a copy of all supplied bytes. Coalesced records that
/// cannot yet be processed remain queued until plaintext is drained or handshake
/// output is taken. Drain both directions while completing a handshake.
pub struct TlsSession {
    connection: Connection,
    pending_ciphertext: BytesMut,
    plaintext_available: usize,
    peer_closed: bool,
    local_closed: bool,
    failure: Option<String>,
}

impl TlsSession {
    /// Creates a client session and queues its initial handshake flight.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for an invalid name or cryptographic policy.
    ///
    /// # Panics
    /// Does not panic.
    pub fn new_client(config: &TlsClientConfig, server_name: &str) -> Result<Self> {
        let name = ServerName::try_from(server_name.to_owned()).map_err(tls_error)?;
        let builder = rustls::ClientConfig::builder_with_provider(Arc::new(
            rustls::crypto::ring::default_provider(),
        ))
        .with_protocol_versions(&[&rustls::version::TLS13])
        .map_err(tls_error)?;
        let builder = if config.insecure {
            builder
                .dangerous()
                .with_custom_certificate_verifier(Arc::new(TestVerifier))
        } else {
            builder.with_root_certificates(Arc::clone(&config.roots))
        };
        let mut inner = match &config.client_auth {
            Some(identity) => builder
                .with_client_cert_resolver(Arc::new(SingleCertAndKey::from(Arc::clone(identity)))),
            None => builder.with_no_client_auth(),
        };
        inner.alpn_protocols = config.alpn.clone();
        let connection = ClientConnection::new(Arc::new(inner), name).map_err(tls_error)?;
        Ok(Self::from_connection(Connection::Client(connection)))
    }

    /// Creates a server session awaiting the client's first handshake flight.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] if rustls cannot create the session.
    ///
    /// # Panics
    /// Does not panic.
    pub fn new_server(config: &TlsServerConfig) -> Result<Self> {
        let connection = ServerConnection::new(Arc::clone(&config.inner)).map_err(tls_error)?;
        Ok(Self::from_connection(Connection::Server(connection)))
    }

    fn from_connection(connection: Connection) -> Self {
        Self {
            connection,
            pending_ciphertext: BytesMut::new(),
            plaintext_available: 0,
            peer_closed: false,
            local_closed: false,
            failure: None,
        }
    }

    /// Returns the local endpoint's role.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn role(&self) -> TlsRole {
        match &self.connection {
            Connection::Client(_) => TlsRole::Client,
            Connection::Server(_) => TlsRole::Server,
        }
    }

    /// Returns whether handshake processing is still in progress.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn is_handshaking(&self) -> bool {
        self.connection.is_handshaking()
    }

    /// Returns whether the transport should supply more ciphertext.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn wants_read(&self) -> bool {
        self.failure.is_none() && self.connection.wants_read()
    }

    /// Returns whether encrypted output needs to be drained.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn wants_write(&self) -> bool {
        self.connection.wants_write()
    }

    /// Returns the negotiated application protocol, if available.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn alpn_protocol(&self) -> Option<AlpnProtocol> {
        match self.connection.alpn_protocol() {
            Some(b"h2") => Some(AlpnProtocol::Http2),
            Some(b"http/1.1") => Some(AlpnProtocol::Http11),
            _ => None,
        }
    }

    /// Accepts all supplied ciphertext and returns currently readable plaintext bytes.
    ///
    /// This return value is not the number of input bytes consumed. Empty input
    /// does not indicate transport EOF. Record fragments are retained internally.
    ///
    /// Data received after the peer's close notification is discarded, including
    /// bytes coalesced with that notification. Previously received plaintext
    /// remains readable and the local sending direction remains open.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for invalid records, handshake failures, or a
    /// previous fatal error.
    ///
    /// # Panics
    /// Does not panic.
    pub fn feed_ciphertext(&mut self, ciphertext: &[u8]) -> Result<usize> {
        self.check_failure()?;
        if self.peer_closed {
            return Ok(self.plaintext_available);
        }
        self.pending_ciphertext.extend_from_slice(ciphertext);
        self.process_pending()?;
        Ok(self.plaintext_available)
    }

    /// Appends all currently obtainable plaintext to `out` using a fixed scratch buffer.
    ///
    /// Also processes queued coalesced records as their plaintext is drained.
    /// Returns zero when no plaintext is available, including after clean closure.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for record or plaintext read failures. Output
    /// appended before an error remains in `out`.
    ///
    /// # Panics
    /// Does not panic.
    pub fn read_plaintext(&mut self, out: &mut BytesMut) -> Result<usize> {
        self.check_failure()?;
        let before = out.len();
        let mut scratch = [0_u8; 16_384];
        loop {
            self.process_pending()?;
            match self.connection.reader().read(&mut scratch) {
                Ok(0) => break,
                Ok(count) => {
                    out.extend_from_slice(&scratch[..count]);
                    self.plaintext_available = self.plaintext_available.saturating_sub(count);
                }
                Err(error) if error.kind() == ErrorKind::WouldBlock => break,
                Err(error) => return self.fail(error.to_string()),
            }
        }
        Ok(out.len() - before)
    }

    /// Queues plaintext for encryption and returns the accepted byte count.
    ///
    /// Writes may be partial due to rustls buffer limits, including during the
    /// handshake. Drain ciphertext and retry the unaccepted suffix.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] for a previous fatal error, a closed sending
    /// direction, or a plaintext write failure. Receiving the peer's close
    /// notification leaves the local sending direction open.
    ///
    /// # Panics
    /// Does not panic.
    pub fn write_plaintext(&mut self, plaintext: &[u8]) -> Result<usize> {
        self.check_failure()?;
        if self.local_closed {
            return Err(ProtoError::Tls(
                "plaintext write after TLS closure".to_owned(),
            ));
        }
        self.connection.writer().write(plaintext).map_err(tls_error)
    }

    /// Appends queued handshake, application, and alert ciphertext to `out`.
    ///
    /// A fatal protocol error may leave an alert available for transmission.
    ///
    /// # Errors
    /// Returns [`ProtoError::Tls`] if output extraction or queued record
    /// processing fails. Bytes appended before an error remain in `out`.
    ///
    /// # Panics
    /// Does not panic.
    pub fn take_ciphertext(&mut self, out: &mut BytesMut) -> Result<usize> {
        let before = out.len();
        loop {
            while self.connection.wants_write() {
                let written = self
                    .connection
                    .write_tls(&mut BufferWriter(out))
                    .map_err(tls_error)?;
                if written == 0 {
                    return Err(ProtoError::Tls("TLS output made no progress".to_owned()));
                }
            }
            if self.failure.is_some() || self.pending_ciphertext.is_empty() {
                break;
            }
            self.process_pending()?;
            if !self.connection.wants_write() {
                break;
            }
        }
        Ok(out.len() - before)
    }

    /// Closes the sending direction and queues one TLS close notification.
    ///
    /// Repeated calls are harmless. During the handshake, closure is deferred
    /// until previously accepted plaintext can be encrypted before the alert.
    /// Further plaintext writes fail immediately. Drain ciphertext to transmit
    /// the handshake and alert.
    ///
    /// # Errors
    /// This operation is infallible.
    ///
    /// # Panics
    /// Does not panic.
    pub fn send_close_notify(&mut self) {
        self.local_closed = true;
        self.flush_close_notify();
    }

    fn check_failure(&self) -> Result<()> {
        match &self.failure {
            Some(reason) => Err(ProtoError::Tls(reason.clone())),
            None => Ok(()),
        }
    }

    fn fail<T>(&mut self, reason: String) -> Result<T> {
        self.failure = Some(reason.clone());
        Err(ProtoError::Tls(reason))
    }

    fn flush_close_notify(&mut self) {
        if self.local_closed && !self.connection.is_handshaking() {
            self.connection.send_close_notify();
        }
    }

    fn process_pending(&mut self) -> Result<()> {
        self.check_failure()?;
        while !self.pending_ciphertext.is_empty() && self.connection.wants_read() {
            let mut input = self.pending_ciphertext.as_slice();
            let count = match self.connection.read_tls(&mut input) {
                Ok(count) => count,
                Err(error) => return self.fail(error.to_string()),
            };
            if count == 0 {
                return self.fail("TLS input made no progress".to_owned());
            }
            self.pending_ciphertext.advance(count);
            let state = match self.connection.process_new_packets() {
                Ok(state) => state,
                Err(error) => return self.fail(error.to_string()),
            };
            self.plaintext_available = state.plaintext_bytes_to_read();
            self.peer_closed = state.peer_has_closed();
            self.flush_close_notify();
        }
        if self.peer_closed {
            self.pending_ciphertext.clear();
        }
        Ok(())
    }
}

struct BufferWriter<'a>(&'a mut BytesMut);

impl Write for BufferWriter<'_> {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        self.0.extend_from_slice(bytes);
        Ok(bytes.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

fn encode_alpn(protocols: &[AlpnProtocol]) -> Vec<Vec<u8>> {
    protocols
        .iter()
        .map(|protocol| match protocol {
            AlpnProtocol::Http11 => b"http/1.1".to_vec(),
            AlpnProtocol::Http2 => b"h2".to_vec(),
        })
        .collect()
}

fn tls_error(error: impl std::fmt::Display) -> ProtoError {
    ProtoError::Tls(error.to_string())
}

#[derive(Debug)]
struct TestVerifier;

impl ServerCertVerifier for TestVerifier {
    fn verify_server_cert(
        &self,
        _end_entity: &CertificateDer<'_>,
        _intermediates: &[CertificateDer<'_>],
        _server_name: &ServerName<'_>,
        _ocsp_response: &[u8],
        _now: UnixTime,
    ) -> std::result::Result<ServerCertVerified, rustls::Error> {
        Ok(ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &DigitallySignedStruct,
    ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {
        Err(rustls::Error::General("TLS 1.2 is disabled".to_owned()))
    }

    fn verify_tls13_signature(
        &self,
        message: &[u8],
        cert: &CertificateDer<'_>,
        dss: &DigitallySignedStruct,
    ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {
        rustls::crypto::verify_tls13_signature(
            message,
            cert,
            dss,
            &rustls::crypto::ring::default_provider().signature_verification_algorithms,
        )
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        rustls::crypto::ring::default_provider()
            .signature_verification_algorithms
            .supported_schemes()
    }
}