rmux-server 0.7.1

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
use std::io;

use base64::Engine;
use rmux_web_crypto::{Message, Opener, Sealer};
use serde::Deserialize;
use zeroize::Zeroizing;

use super::websocket::{WebSocketMessage, WebSocketReader, WebSocketWriter};

pub(super) const E2EE_CAPABILITY: &str = "e2ee-token-auth";

/// A parsed web-share client hello.
///
/// `raw` is the EXACT hello text received on the wire. It is bound into the
/// session key schedule as part of the handshake transcript, so it must be the
/// untouched bytes, not a re-serialization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ClientHello {
    pub(super) token_id: String,
    pub(super) client_nonce: String,
    pub(super) client_public: [u8; 32],
    /// The client's ML-KEM-768 encapsulation key, length-validated to exactly
    /// 1184 bytes at parse time (a [u8; N>32] does not derive Eq/Debug, so it is
    /// held as a Vec). Its ML-KEM validity is checked at encapsulation time.
    pub(super) client_ml_kem_ek: Vec<u8>,
    pub(super) raw: String,
}

pub(super) struct EncryptedWebSocketReader {
    reader: WebSocketReader,
    opener: FrameOpener,
}

pub(super) struct EncryptedWebSocketWriter {
    writer: WebSocketWriter,
    sealer: FrameSealer,
    seal_scratch: Vec<u8>,
}

/// Thin newtype around the [`rmux_web_crypto::Opener`] (server-to-client opener
/// from the caller's point of view: the server opens client-to-server frames).
pub(super) struct FrameOpener {
    opener: Opener,
}

/// Thin newtype around the [`rmux_web_crypto::Sealer`].
pub(super) struct FrameSealer {
    sealer: Sealer,
}

/// Encodes bytes as base64url without padding (the web-share wire encoding).
pub(super) fn base64url(bytes: &[u8]) -> String {
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
}

pub(super) fn random_handshake_nonce() -> io::Result<String> {
    let mut nonce = [0u8; 16];
    getrandom::fill(&mut nonce).map_err(|error| {
        io::Error::other(format!("failed to create web-share e2ee nonce: {error}"))
    })?;
    Ok(base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(nonce))
}

/// Derives the server side of a web-share session from the token-derived PSK, the
/// X25519 DH shared secret, the ML-KEM shared secret, and the exact handshake
/// transcript bytes.
///
/// Returns the opener for client-to-server frames and the sealer for
/// server-to-client frames, ready to wrap into the encrypted reader/writer.
pub(super) fn derive_server_crypto(
    psk: &[u8],
    dh: &[u8; 32],
    ml_kem_secret: &[u8; 32],
    client_hello_bytes: &[u8],
    server_challenge_bytes: &[u8],
) -> io::Result<(FrameOpener, FrameSealer)> {
    let (sealer, opener) = rmux_web_crypto::derive_server_session(
        psk,
        dh,
        ml_kem_secret,
        client_hello_bytes,
        server_challenge_bytes,
    )
    .map_err(|error| io::Error::other(format!("failed to derive web-share session: {error}")))?;
    Ok((FrameOpener { opener }, FrameSealer { sealer }))
}

pub(super) fn parse_client_hello(text: &str, protocol_version: u16) -> Result<ClientHello, ()> {
    let hello = serde_json::from_str::<ClientHelloWire>(text).map_err(|_| ())?;
    if hello.kind != "hello" || hello.protocol_version != protocol_version {
        return Err(());
    }
    if !hello
        .capabilities
        .iter()
        .any(|capability| capability == E2EE_CAPABILITY)
    {
        return Err(());
    }
    if !super::secrets::valid_token_id_shape(&hello.token_id)
        || decode_nonce(&hello.client_nonce).is_err()
    {
        return Err(());
    }
    let client_public = decode_client_public(&hello.client_public)?;
    let client_ml_kem_ek = decode_ml_kem_ek(&hello.client_ml_kem_ek)?;
    Ok(ClientHello {
        token_id: hello.token_id,
        client_nonce: hello.client_nonce,
        client_public,
        client_ml_kem_ek,
        raw: text.to_owned(),
    })
}

/// Decodes and length-validates the client's ML-KEM-768 encapsulation key
/// (base64url, exactly 1184 bytes). A wrong length is rejected here; ML-KEM
/// validity is enforced at encapsulation time.
fn decode_ml_kem_ek(encoded: &str) -> Result<Vec<u8>, ()> {
    let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(encoded)
        .map_err(|_| ())?;
    if decoded.len() != rmux_web_crypto::ml_kem::ENCAPSULATION_KEY_LEN {
        return Err(());
    }
    Ok(decoded)
}

/// The result of a successful ML-KEM encapsulation: the public ciphertext for
/// the challenge and the (zeroizing) shared secret for the key schedule.
type MlKemEncapsulation = (
    [u8; rmux_web_crypto::ml_kem::CIPHERTEXT_LEN],
    Zeroizing<[u8; 32]>,
);

/// Encapsulates to the client's ML-KEM encapsulation key, returning the
/// ciphertext (for the challenge) and the shared secret (for the schedule).
///
/// `Ok(None)` means the key failed ML-KEM validation — the caller MUST collapse
/// that to the uniform pre-ready rejection (fail closed), not bypass the delay.
/// `Err` is a server RNG failure.
pub(super) fn encapsulate_ml_kem(
    client_ml_kem_ek: &[u8],
) -> io::Result<Option<MlKemEncapsulation>> {
    let Ok(ek) =
        <&[u8; rmux_web_crypto::ml_kem::ENCAPSULATION_KEY_LEN]>::try_from(client_ml_kem_ek)
    else {
        return Ok(None);
    };
    let mut randomness = Zeroizing::new([0u8; rmux_web_crypto::ml_kem::ENCAPS_RANDOMNESS_LEN]);
    getrandom::fill(randomness.as_mut()).map_err(|error| {
        io::Error::other(format!(
            "failed to create ml-kem encaps randomness: {error}"
        ))
    })?;
    // The ciphertext is public; the shared secret is wiped when it drops.
    Ok(rmux_web_crypto::ml_kem::encapsulate(ek, *randomness)
        .map(|(ciphertext, shared_secret)| (ciphertext, Zeroizing::new(shared_secret))))
}

impl EncryptedWebSocketReader {
    pub(super) fn new(reader: WebSocketReader, opener: FrameOpener) -> Self {
        Self { reader, opener }
    }

    pub(super) async fn read_message(&mut self) -> io::Result<WebSocketMessage> {
        let message = self.reader.read_message().await?;
        match message {
            WebSocketMessage::Binary(bytes) => self.opener.open_message(&bytes),
            WebSocketMessage::Ping(payload) => Ok(WebSocketMessage::Ping(payload)),
            WebSocketMessage::Pong => Ok(WebSocketMessage::Pong),
            WebSocketMessage::Close => Ok(WebSocketMessage::Close),
            WebSocketMessage::Text(_) => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "plaintext websocket text after e2ee handshake",
            )),
        }
    }
}

impl EncryptedWebSocketWriter {
    pub(super) fn new(writer: WebSocketWriter, sealer: FrameSealer) -> Self {
        Self {
            writer,
            sealer,
            seal_scratch: Vec::new(),
        }
    }

    pub(super) async fn write_text(&mut self, text: &str) -> io::Result<()> {
        self.seal_scratch.clear();
        self.sealer.seal_text_into(text, &mut self.seal_scratch)?;
        self.writer.write_binary(&self.seal_scratch).await
    }

    pub(super) async fn write_binary(&mut self, payload: &[u8]) -> io::Result<()> {
        self.seal_scratch.clear();
        self.sealer
            .seal_binary_into(payload, &mut self.seal_scratch)?;
        self.writer.write_binary(&self.seal_scratch).await
    }

    pub(super) async fn write_close(&mut self) -> io::Result<()> {
        self.writer.write_close().await
    }

    pub(super) async fn write_close_code(&mut self, code: u16, reason: &str) -> io::Result<()> {
        self.writer.write_close_code(code, reason).await
    }

    pub(super) async fn write_pong(&mut self, payload: &[u8]) -> io::Result<()> {
        self.writer.write_pong(payload).await
    }
}

impl FrameOpener {
    pub(super) fn open_message(&mut self, frame: &[u8]) -> io::Result<WebSocketMessage> {
        match self
            .opener
            .open(frame)
            .map_err(|_| invalid_data("e2ee open failed"))?
        {
            Message::Text(text) => Ok(WebSocketMessage::Text(text)),
            Message::Binary(payload) => Ok(WebSocketMessage::Binary(payload)),
        }
    }
}

impl FrameSealer {
    pub(super) fn seal_text_into(&mut self, text: &str, dst: &mut Vec<u8>) -> io::Result<()> {
        self.sealer
            .seal_text_into(text, dst)
            .map_err(|_| io::Error::other("e2ee seal failed"))
    }

    pub(super) fn seal_binary_into(&mut self, payload: &[u8], dst: &mut Vec<u8>) -> io::Result<()> {
        self.sealer
            .seal_binary_into(payload, dst)
            .map_err(|_| io::Error::other("e2ee seal failed"))
    }
}

fn decode_nonce(nonce: &str) -> io::Result<Vec<u8>> {
    let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(nonce)
        .map_err(|_| invalid_data("invalid e2ee nonce"))?;
    if decoded.len() != 16 {
        return Err(invalid_data("invalid e2ee nonce length"));
    }
    Ok(decoded)
}

fn decode_client_public(client_public: &str) -> Result<[u8; 32], ()> {
    let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(client_public)
        .map_err(|_| ())?;
    <[u8; 32]>::try_from(decoded.as_slice()).map_err(|_| ())
}

fn invalid_data(message: &'static str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, message)
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ClientHelloWire {
    #[serde(rename = "type")]
    kind: String,
    protocol_version: u16,
    capabilities: Vec<String>,
    token_id: String,
    client_nonce: String,
    client_public: String,
    client_ml_kem_ek: String,
}

#[cfg(test)]
mod tests {
    use super::{derive_server_crypto, FrameSealer, WebSocketMessage};
    use crate::web::secrets::SecretHash;
    use base64::Engine;
    use rmux_web_crypto::{derive_client_session, generate_ephemeral};
    use static_assertions::assert_not_impl_any;

    assert_not_impl_any!(FrameSealer: Clone);

    fn b64(bytes: &[u8]) -> String {
        base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
    }

    #[test]
    fn token_id_is_stable_and_not_the_secret_hash() {
        let secret = SecretHash::from_secret("token");

        assert_eq!(
            secret.token_id(),
            SecretHash::from_secret("token").token_id()
        );
        assert_ne!(
            secret.token_id(),
            base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(secret.as_bytes())
        );
    }

    #[test]
    fn e2ee_round_trips_text_and_binary_in_order() {
        let secret = SecretHash::from_secret("token");
        let psk = secret.as_bytes();

        // Full X25519 agreement, exactly as the live handshake performs it.
        let client_eph = generate_ephemeral();
        let server_eph = generate_ephemeral();
        let client_public = client_eph.public_bytes();
        let server_public = server_eph.public_bytes();

        let client_hello = br#"{"type":"hello","client_public":"..."}"#;
        let server_challenge = br#"{"type":"challenge","server_public":"..."}"#;

        let client_dh = client_eph.into_shared_secret(&server_public);
        let server_dh = server_eph.into_shared_secret(&client_public);
        assert_eq!(*client_dh, *server_dh, "X25519 agreement must converge");

        // A matching ML-KEM shared secret on both sides (the agreement itself is
        // tested in rmux-web-crypto); this exercises the hybrid record layer.
        let ml_kem_ss = [0x55u8; 32];
        let (mut server_open, mut server_seal) =
            derive_server_crypto(&psk, &server_dh, &ml_kem_ss, client_hello, server_challenge)
                .expect("derive server");
        let (mut client_seal, mut client_open) =
            derive_client_session(&psk, &client_dh, &ml_kem_ss, client_hello, server_challenge)
                .expect("derive client");

        let frame = client_seal.seal_text(r#"{"type":"auth"}"#).expect("seal");
        assert_eq!(
            server_open.open_message(&frame).expect("open"),
            WebSocketMessage::Text(r#"{"type":"auth"}"#.to_owned())
        );

        let mut frame = Vec::new();
        server_seal
            .seal_binary_into(&[0x10, b'o', b'k'], &mut frame)
            .expect("seal into");
        assert_eq!(
            client_open.open(&frame).expect("open"),
            rmux_web_crypto::Message::Binary(vec![0x10, b'o', b'k'])
        );

        frame.clear();
        server_seal
            .seal_binary_into(&[0x11, b'o', b'k'], &mut frame)
            .expect("seal into");
        assert_eq!(
            client_open.open(&frame).expect("open"),
            rmux_web_crypto::Message::Binary(vec![0x11, b'o', b'k'])
        );
    }

    #[test]
    fn client_hello_rejects_missing_e2ee_capability() {
        let client_public = b64(&generate_ephemeral().public_bytes());
        let ek = b64(&rmux_web_crypto::ml_kem::KeyPair::generate([1u8; 64]).encapsulation_key());
        let text = format!(
            r#"{{"type":"hello","protocol_version":1,"capabilities":["token-auth"],"token_id":"aaaaaaaaaaaaaaaaaaaaaa","client_nonce":"AQIDBAUGBwgJCgsMDQ4PEA","client_public":"{client_public}","client_ml_kem_ek":"{ek}"}}"#
        );

        assert!(super::parse_client_hello(&text, 1).is_err());
    }

    #[test]
    fn client_hello_preserves_exact_raw_wire_text() {
        let client_public = b64(&generate_ephemeral().public_bytes());
        let ek = b64(&rmux_web_crypto::ml_kem::KeyPair::generate([1u8; 64]).encapsulation_key());
        let text = format!(
            r#"{{"type":"hello","protocol_version":1,"capabilities":["terminal-palette-v1","e2ee-token-auth"],"token_id":"VANRFV6FYQX1QTOi-BMVrQ","client_nonce":"AQIDBAUGBwgJCgsMDQ4PEA","client_public":"{client_public}","client_ml_kem_ek":"{ek}"}}"#
        );

        let hello = super::parse_client_hello(&text, 1).expect("valid hello");
        assert_eq!(hello.raw, text);
    }

    #[test]
    fn e2ee_wrong_token_fails_to_open() {
        // Identical handshake transcript + DH, but the client authenticated with
        // a different token (PSK). The server's AEAD must reject the first frame
        // — implicit authentication by the token, end to end at the server layer.
        let server_secret = SecretHash::from_secret("server-token");
        let wrong_secret = SecretHash::from_secret("attacker-token");
        let client_eph = generate_ephemeral();
        let server_eph = generate_ephemeral();
        let client_public = client_eph.public_bytes();
        let server_public = server_eph.public_bytes();
        let client_hello = br#"{"type":"hello"}"#;
        let server_challenge = br#"{"type":"challenge"}"#;
        let client_dh = client_eph.into_shared_secret(&server_public);
        let server_dh = server_eph.into_shared_secret(&client_public);

        let ml_kem_ss = [0x55u8; 32];
        let (mut server_open, _server_seal) = derive_server_crypto(
            &server_secret.as_bytes(),
            &server_dh,
            &ml_kem_ss,
            client_hello,
            server_challenge,
        )
        .expect("derive server");
        let (mut client_seal, _client_open) = derive_client_session(
            &wrong_secret.as_bytes(),
            &client_dh,
            &ml_kem_ss,
            client_hello,
            server_challenge,
        )
        .expect("derive client");

        let frame = client_seal.seal_text(r#"{"type":"auth"}"#).expect("seal");
        assert!(
            server_open.open_message(&frame).is_err(),
            "a frame sealed under the wrong token must fail AEAD authentication"
        );
    }
}