summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-crypto/src/agentio.rs
blob: 7c57a0ef45094e3c6f5c12ead9138b57e482e245 (plain)
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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{
    cmp::min,
    fmt, mem,
    ops::Deref,
    os::raw::{c_uint, c_void},
    pin::Pin,
    ptr::{null, null_mut},
};

use neqo_common::{hex, hex_with_len, qtrace};

use crate::{
    constants::{ContentType, Epoch},
    err::{nspr, Error, PR_SetError, Res},
    null_safe_slice, prio, ssl,
};

// Alias common types.
type PrFd = *mut prio::PRFileDesc;
type PrStatus = prio::PRStatus::Type;
const PR_SUCCESS: PrStatus = prio::PRStatus::PR_SUCCESS;
const PR_FAILURE: PrStatus = prio::PRStatus::PR_FAILURE;

/// Convert a pinned, boxed object into a void pointer.
pub fn as_c_void<T: Unpin>(pin: &mut Pin<Box<T>>) -> *mut c_void {
    (Pin::into_inner(pin.as_mut()) as *mut T).cast()
}

/// A slice of the output.
#[derive(Default)]
pub struct Record {
    pub epoch: Epoch,
    pub ct: ContentType,
    pub data: Vec<u8>,
}

impl Record {
    #[must_use]
    pub fn new(epoch: Epoch, ct: ContentType, data: &[u8]) -> Self {
        Self {
            epoch,
            ct,
            data: data.to_vec(),
        }
    }

    // Shoves this record into the socket, returns true if blocked.
    pub(crate) fn write(self, fd: *mut ssl::PRFileDesc) -> Res<()> {
        qtrace!("write {:?}", self);
        unsafe {
            ssl::SSL_RecordLayerData(
                fd,
                self.epoch,
                ssl::SSLContentType::Type::from(self.ct),
                self.data.as_ptr(),
                c_uint::try_from(self.data.len())?,
            )
        }
    }
}

impl fmt::Debug for Record {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Record {:?}:{:?} {}",
            self.epoch,
            self.ct,
            hex_with_len(&self.data[..])
        )
    }
}

#[derive(Debug, Default)]
pub struct RecordList {
    records: Vec<Record>,
}

impl RecordList {
    fn append(&mut self, epoch: Epoch, ct: ContentType, data: &[u8]) {
        self.records.push(Record::new(epoch, ct, data));
    }

    #[allow(clippy::unused_self)]
    unsafe extern "C" fn ingest(
        _fd: *mut ssl::PRFileDesc,
        epoch: ssl::PRUint16,
        ct: ssl::SSLContentType::Type,
        data: *const ssl::PRUint8,
        len: c_uint,
        arg: *mut c_void,
    ) -> ssl::SECStatus {
        let records = arg.cast::<Self>().as_mut().unwrap();

        let slice = null_safe_slice(data, len);
        records.append(epoch, ContentType::try_from(ct).unwrap(), slice);
        ssl::SECSuccess
    }

    /// Create a new record list.
    pub(crate) fn setup(fd: *mut ssl::PRFileDesc) -> Res<Pin<Box<Self>>> {
        let mut records = Box::pin(Self::default());
        unsafe {
            ssl::SSL_RecordLayerWriteCallback(fd, Some(Self::ingest), as_c_void(&mut records))
        }?;
        Ok(records)
    }
}

impl Deref for RecordList {
    type Target = Vec<Record>;
    #[must_use]
    fn deref(&self) -> &Vec<Record> {
        &self.records
    }
}

pub struct RecordListIter(std::vec::IntoIter<Record>);

impl Iterator for RecordListIter {
    type Item = Record;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

impl IntoIterator for RecordList {
    type Item = Record;
    type IntoIter = RecordListIter;
    #[must_use]
    fn into_iter(self) -> Self::IntoIter {
        RecordListIter(self.records.into_iter())
    }
}

pub struct AgentIoInputContext<'a> {
    input: &'a mut AgentIoInput,
}

impl<'a> Drop for AgentIoInputContext<'a> {
    fn drop(&mut self) {
        self.input.reset();
    }
}

#[derive(Debug)]
struct AgentIoInput {
    // input is data that is read by TLS.
    input: *const u8,
    // input_available is how much data is left for reading.
    available: usize,
}

impl AgentIoInput {
    fn wrap<'a: 'c, 'b: 'c, 'c>(&'a mut self, input: &'b [u8]) -> AgentIoInputContext<'c> {
        assert!(self.input.is_null());
        self.input = input.as_ptr();
        self.available = input.len();
        qtrace!("AgentIoInput wrap {:p}", self.input);
        AgentIoInputContext { input: self }
    }

    // Take the data provided as input and provide it to the TLS stack.
    fn read_input(&mut self, buf: *mut u8, count: usize) -> Res<usize> {
        let amount = min(self.available, count);
        if amount == 0 {
            unsafe {
                PR_SetError(nspr::PR_WOULD_BLOCK_ERROR, 0);
            }
            return Err(Error::NoDataAvailable);
        }

        #[allow(clippy::disallowed_methods)] // We just checked if this was empty.
        let src = unsafe { std::slice::from_raw_parts(self.input, amount) };
        qtrace!([self], "read {}", hex(src));
        let dst = unsafe { std::slice::from_raw_parts_mut(buf, amount) };
        dst.copy_from_slice(src);
        self.input = self.input.wrapping_add(amount);
        self.available -= amount;
        Ok(amount)
    }

    fn reset(&mut self) {
        qtrace!([self], "reset");
        self.input = null();
        self.available = 0;
    }
}

impl ::std::fmt::Display for AgentIoInput {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "AgentIoInput {:p}", self.input)
    }
}

#[derive(Debug)]
pub struct AgentIo {
    // input collects the input we might provide to TLS.
    input: AgentIoInput,

    // output contains data that is written by TLS.
    output: Vec<u8>,
}

impl AgentIo {
    pub fn new() -> Self {
        Self {
            input: AgentIoInput {
                input: null(),
                available: 0,
            },
            output: Vec::new(),
        }
    }

    unsafe fn borrow(fd: &mut PrFd) -> &mut Self {
        #[allow(clippy::cast_ptr_alignment)]
        (**fd).secret.cast::<Self>().as_mut().unwrap()
    }

    pub fn wrap<'a: 'c, 'b: 'c, 'c>(&'a mut self, input: &'b [u8]) -> AgentIoInputContext<'c> {
        assert_eq!(self.output.len(), 0);
        self.input.wrap(input)
    }

    // Stage output from TLS into the output buffer.
    fn save_output(&mut self, buf: *const u8, count: usize) {
        let slice = unsafe { null_safe_slice(buf, count) };
        qtrace!([self], "save output {}", hex(slice));
        self.output.extend_from_slice(slice);
    }

    pub fn take_output(&mut self) -> Vec<u8> {
        qtrace!([self], "take output");
        mem::take(&mut self.output)
    }
}

impl ::std::fmt::Display for AgentIo {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "AgentIo")
    }
}

unsafe extern "C" fn agent_close(fd: PrFd) -> PrStatus {
    (*fd).secret = null_mut();
    if let Some(dtor) = (*fd).dtor {
        dtor(fd);
    }
    PR_SUCCESS
}

unsafe extern "C" fn agent_read(mut fd: PrFd, buf: *mut c_void, amount: prio::PRInt32) -> PrStatus {
    let io = AgentIo::borrow(&mut fd);
    if let Ok(a) = usize::try_from(amount) {
        match io.input.read_input(buf.cast(), a) {
            Ok(_) => PR_SUCCESS,
            Err(_) => PR_FAILURE,
        }
    } else {
        PR_FAILURE
    }
}

unsafe extern "C" fn agent_recv(
    mut fd: PrFd,
    buf: *mut c_void,
    amount: prio::PRInt32,
    flags: prio::PRIntn,
    _timeout: prio::PRIntervalTime,
) -> prio::PRInt32 {
    let io = AgentIo::borrow(&mut fd);
    if flags != 0 {
        return PR_FAILURE;
    }
    if let Ok(a) = usize::try_from(amount) {
        match io.input.read_input(buf.cast(), a) {
            Ok(v) => prio::PRInt32::try_from(v).unwrap_or(PR_FAILURE),
            Err(_) => PR_FAILURE,
        }
    } else {
        PR_FAILURE
    }
}

unsafe extern "C" fn agent_write(
    mut fd: PrFd,
    buf: *const c_void,
    amount: prio::PRInt32,
) -> PrStatus {
    let io = AgentIo::borrow(&mut fd);
    if let Ok(a) = usize::try_from(amount) {
        io.save_output(buf.cast(), a);
        amount
    } else {
        PR_FAILURE
    }
}

unsafe extern "C" fn agent_send(
    mut fd: PrFd,
    buf: *const c_void,
    amount: prio::PRInt32,
    flags: prio::PRIntn,
    _timeout: prio::PRIntervalTime,
) -> prio::PRInt32 {
    let io = AgentIo::borrow(&mut fd);

    if flags != 0 {
        return PR_FAILURE;
    }
    if let Ok(a) = usize::try_from(amount) {
        io.save_output(buf.cast(), a);
        amount
    } else {
        PR_FAILURE
    }
}

unsafe extern "C" fn agent_available(mut fd: PrFd) -> prio::PRInt32 {
    let io = AgentIo::borrow(&mut fd);
    io.input.available.try_into().unwrap_or(PR_FAILURE)
}

unsafe extern "C" fn agent_available64(mut fd: PrFd) -> prio::PRInt64 {
    let io = AgentIo::borrow(&mut fd);
    io.input
        .available
        .try_into()
        .unwrap_or_else(|_| PR_FAILURE.into())
}

#[allow(clippy::cast_possible_truncation)]
unsafe extern "C" fn agent_getname(_fd: PrFd, addr: *mut prio::PRNetAddr) -> PrStatus {
    let a = addr.as_mut().unwrap();
    // Cast is safe because prio::PR_AF_INET is 2
    a.inet.family = prio::PR_AF_INET as prio::PRUint16;
    a.inet.port = 0;
    a.inet.ip = 0;
    PR_SUCCESS
}

unsafe extern "C" fn agent_getsockopt(_fd: PrFd, opt: *mut prio::PRSocketOptionData) -> PrStatus {
    let o = opt.as_mut().unwrap();
    if o.option == prio::PRSockOption::PR_SockOpt_Nonblocking {
        o.value.non_blocking = 1;
        return PR_SUCCESS;
    }
    PR_FAILURE
}

pub const METHODS: &prio::PRIOMethods = &prio::PRIOMethods {
    file_type: prio::PRDescType::PR_DESC_LAYERED,
    close: Some(agent_close),
    read: Some(agent_read),
    write: Some(agent_write),
    available: Some(agent_available),
    available64: Some(agent_available64),
    fsync: None,
    seek: None,
    seek64: None,
    fileInfo: None,
    fileInfo64: None,
    writev: None,
    connect: None,
    accept: None,
    bind: None,
    listen: None,
    shutdown: None,
    recv: Some(agent_recv),
    send: Some(agent_send),
    recvfrom: None,
    sendto: None,
    poll: None,
    acceptread: None,
    transmitfile: None,
    getsockname: Some(agent_getname),
    getpeername: Some(agent_getname),
    reserved_fn_6: None,
    reserved_fn_5: None,
    getsocketoption: Some(agent_getsockopt),
    setsocketoption: None,
    sendfile: None,
    connectcontinue: None,
    reserved_fn_3: None,
    reserved_fn_2: None,
    reserved_fn_1: None,
    reserved_fn_0: None,
};