dope-tls 0.12.0

TCP TLS streams for dope runtime, shin-backed
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
use std::io::{self, BufRead, Error, ErrorKind, IoSlice, Write};
use std::sync::Arc;

use dope_net::wire::send::{Plain, Prepared, Storage, Vectored};
use dope_net::wire::{
    Reclaim, RuntimeLimits, Wire,
    buffered::{Buffer, Buffered, Recv, Scratch},
};
use dope_net::{Bytes, Leased};
use rustls::{
    ClientConfig, ClientConnection, Connection, ServerConfig, ServerConnection,
    pki_types::ServerName,
};
use shin::record::MAX_PLAINTEXT_BODY;

use crate::send::{SendProtocol, Sender};
use crate::staging::TLS13_RECORD_OVERHEAD;
use crate::tls::{SendState, Tls};

#[derive(Clone, Default)]
pub enum RustTlsEndpoint {
    #[default]
    None,
    Server(Arc<ServerConfig>),
    Client {
        config: Arc<ClientConfig>,
        server_name: ServerName<'static>,
    },
}

struct ConnectionState {
    conn: Option<Connection>,
    readable_plain: usize,
    close: bool,
    close_notify_sent: bool,
}

struct CiphertextWriter<'a> {
    egress: &'a mut Buffer<Scratch>,
}

impl CiphertextWriter<'_> {
    fn remaining(&self) -> usize {
        self.egress.spare_capacity()
    }
}

impl Write for CiphertextWriter<'_> {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        if bytes.is_empty() {
            return Ok(0);
        }
        let n = self.remaining().min(bytes.len());
        if n == 0 {
            Err(Error::from(ErrorKind::WouldBlock))
        } else {
            self.egress
                .try_extend_from_slice(&bytes[..n])
                .map_err(|_| Error::from(ErrorKind::WouldBlock))?;
            Ok(n)
        }
    }

    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
        let mut written = 0;
        let mut has_bytes = false;
        for bytes in bufs {
            if bytes.is_empty() {
                continue;
            }
            has_bytes = true;
            let n = self.remaining().min(bytes.len());
            if n == 0 {
                break;
            }
            self.egress
                .try_extend_from_slice(&bytes[..n])
                .map_err(|_| Error::from(ErrorKind::WouldBlock))?;
            written += n;
            if n != bytes.len() {
                break;
            }
        }
        if written == 0 && has_bytes {
            Err(Error::from(ErrorKind::WouldBlock))
        } else {
            Ok(written)
        }
    }

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

impl ConnectionState {
    fn empty() -> Self {
        Self {
            conn: None,
            readable_plain: 0,
            close: false,
            close_notify_sent: false,
        }
    }
}

pub struct RustTls {
    state: ConnectionState,
    send_inflight: bool,
}

impl RustTls {
    fn is_established(&self) -> bool {
        self.state
            .conn
            .as_ref()
            .is_some_and(|c| !c.is_handshaking())
    }

    fn ingress_decrypt(&mut self, wire_in: &[u8], out: &mut Buffer<Recv>) {
        if self.state.conn.is_none() {
            self.state.close = true;
            return;
        }

        let mut cursor = wire_in;

        while !cursor.is_empty() {
            let conn = match self.state.conn.as_mut() {
                Some(c) => c,
                None => break,
            };
            match conn.read_tls(&mut cursor) {
                Ok(0) => break,
                Ok(_) => {}
                Err(e) if e.kind() == ErrorKind::Other => {
                    let before = out.len();
                    self.drain_reader(out);
                    if self.state.close {
                        return;
                    }
                    if out.len() == before {
                        self.state.close = true;
                        return;
                    }
                    continue;
                }
                Err(_) => {
                    self.state.close = true;
                    return;
                }
            }
            let readable = match self.state.conn.as_mut() {
                Some(conn) => match conn.process_new_packets() {
                    Ok(io) => {
                        if io.peer_has_closed() {
                            self.state.close = true;
                        }
                        io.plaintext_bytes_to_read()
                    }
                    Err(_) => {
                        self.state.close = true;
                        return;
                    }
                },
                None => break,
            };
            self.state.readable_plain = readable;
            self.drain_reader(out);
        }
    }

    fn drain_to_egress(&mut self, egress: &mut Buffer<Scratch>) {
        if self.send_inflight {
            return;
        }
        let mut writer = CiphertextWriter { egress };
        let Some(conn) = self.state.conn.as_mut() else {
            return;
        };
        while conn.wants_write() && writer.remaining() != 0 {
            match conn.write_tls(&mut writer) {
                Ok(0) => break,
                Ok(_) => {}
                Err(error) if error.kind() == ErrorKind::WouldBlock => break,
                Err(_) => {
                    self.state.close = true;
                    break;
                }
            }
        }
    }

    fn egress_has_record_room(egress: &Buffer<Scratch>, plaintext_len: usize) -> bool {
        egress.spare_capacity() >= plaintext_len + TLS13_RECORD_OVERHEAD
    }

    fn encrypt(&mut self, egress: &mut Buffer<Scratch>, plain: &[u8]) -> usize {
        if self.state.conn.is_none() {
            return 0;
        }
        self.drain_to_egress(egress);
        if !self.is_established() {
            return 0;
        }
        if plain.is_empty() {
            return 0;
        }
        let mut consumed = 0;
        while consumed < plain.len() {
            let end = (consumed + MAX_PLAINTEXT_BODY).min(plain.len());
            if !Self::egress_has_record_room(egress, end - consumed) {
                break;
            }
            let n = match self.state.conn.as_mut() {
                Some(conn) => match conn.writer().write(&plain[consumed..end]) {
                    Ok(n) => n,
                    Err(_) => {
                        self.state.close = true;
                        break;
                    }
                },
                None => break,
            };
            if n == 0 {
                break;
            }
            consumed += n;
            self.drain_to_egress(egress);
        }
        consumed
    }

    fn drain_reader(&mut self, out: &mut Buffer<Recv>) {
        let mut remaining = self.state.readable_plain;
        if remaining == 0 {
            return;
        }
        let Some(conn) = self.state.conn.as_mut() else {
            return;
        };
        if remaining > out.spare_capacity() {
            self.state.close = true;
            return;
        }
        let mut close = false;
        while remaining > 0 {
            let read = match conn.reader().into_first_chunk() {
                Ok([]) => {
                    close = true;
                    break;
                }
                Ok(chunk) => {
                    let read = remaining.min(chunk.len());
                    if out.try_extend_from_slice(&chunk[..read]).is_err() {
                        close = true;
                        break;
                    }
                    read
                }
                Err(e) if e.kind() == ErrorKind::WouldBlock => break,
                Err(_) => {
                    close = true;
                    break;
                }
            };
            conn.reader().consume(read);
            remaining -= read;
        }
        self.state.readable_plain = remaining;
        self.state.close |= close;
    }

    fn propagate_close(&mut self, egress: &mut Buffer<Scratch>) -> bool {
        if self.state.close {
            if !self.state.close_notify_sent
                && let Some(conn) = self.state.conn.as_mut()
                && !conn.is_handshaking()
            {
                conn.send_close_notify();
                self.state.close_notify_sent = true;
            }
            self.drain_to_egress(egress);
        }
        self.state.close
    }

    pub fn alpn_protocol(&self) -> Option<&[u8]> {
        self.state
            .conn
            .as_ref()
            .and_then(|conn| conn.alpn_protocol())
    }
}

impl SendProtocol for RustTls {
    fn encrypt(&mut self, egress: &mut Buffer<Scratch>, plain: &[u8]) -> usize {
        self.encrypt(egress, plain)
    }

    fn propagate_close(&mut self, egress: &mut Buffer<Scratch>) -> bool {
        self.propagate_close(egress)
    }

    fn drain_to_egress(&mut self, egress: &mut Buffer<Scratch>) {
        self.drain_to_egress(egress);
    }

    fn send_inflight(&mut self) -> &mut bool {
        &mut self.send_inflight
    }
}

impl Wire for RustTls {
    type InitConfig = RustTlsEndpoint;
    type RuntimeContext = Buffered;
    type Recv<'a> = Bytes<Leased>;
    type SendStorage = SendState;

    const RECLAIM: Reclaim = Reclaim::OnSubmit;

    fn holds_plain(&self, send: &Self::SendStorage) -> bool {
        !send.0.as_slice().is_empty()
            || self
                .state
                .conn
                .as_ref()
                .is_some_and(|conn| conn.wants_write())
    }

    fn runtime_context(limits: RuntimeLimits) -> io::Result<Buffered> {
        Tls::runtime_buffers(limits, 1)
    }

    fn open(cfg: &RustTlsEndpoint, runtime: &Buffered) -> Option<(Self, SendState)> {
        let send = SendState(runtime.try_acquire_scratch()?);
        let mut s = ConnectionState::empty();
        let conn = match cfg {
            RustTlsEndpoint::Server(c) => ServerConnection::new(c.clone())
                .ok()
                .map(Connection::Server),
            RustTlsEndpoint::Client {
                config,
                server_name,
            } => ClientConnection::new(config.clone(), server_name.clone())
                .ok()
                .map(Connection::Client),
            RustTlsEndpoint::None => None,
        };
        s.close = conn.is_none();
        s.conn = conn;
        Some((
            Self {
                state: s,
                send_inflight: false,
            },
            send,
        ))
    }

    fn process_recv<'a>(&mut self, runtime: &Buffered, bytes: &'a [u8]) -> Option<Self::Recv<'a>> {
        let Some(mut data) = runtime.try_acquire_recv() else {
            self.state.close = true;
            return None;
        };
        self.ingress_decrypt(bytes, &mut data);
        if data.is_empty() {
            return None;
        }
        Some(data.freeze())
    }

    fn recv_eof(&mut self) {
        self.state.close = true;
    }

    fn prepare_send<'a>(
        &'a mut self,
        send: Storage<'a, Self::SendStorage>,
        plain: Plain<'a>,
    ) -> Prepared<'a> {
        let mut sender = Sender::new(self);
        sender.prepare(send, plain)
    }

    fn prepare_send_vectored<'a>(
        &'a mut self,
        send: Storage<'a, Self::SendStorage>,
        vectored: Vectored<'a>,
    ) -> Prepared<'a> {
        let mut sender = Sender::new(self);
        sender.prepare_vectored(send, vectored)
    }

    fn submit_failed(&mut self) {
        self.send_inflight = false;
    }

    fn after_send<'a>(
        &'a mut self,
        send: Storage<'a, Self::SendStorage>,
        written: usize,
    ) -> Prepared<'a> {
        let mut sender = Sender::new(self);
        sender.after_send(send, written)
    }

    fn flush_pending<'a>(&'a mut self, send: Storage<'a, Self::SendStorage>) -> Prepared<'a> {
        let mut sender = Sender::new(self);
        sender.flush(send)
    }

    fn graceful_close<'a>(&'a mut self, mut send: Storage<'a, Self::SendStorage>) -> Prepared<'a> {
        self.state.close = true;
        let close = self.propagate_close(&mut send.0);
        let mut sender = Sender::new(self);
        sender.finish(send, 0, close)
    }
}