rings-node 0.20.0

Rings is a structured peer-to-peer network implementation using WebRTC, Chord algorithm, and full WebAssembly (WASM) support.
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
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::sync::Arc;

use bytes::Bytes;
use rings_core::dht::Did;
use rings_core::ecc::elgamal::impls::secp256k1::AeadCiphertext;
use rings_core::ecc::PublicKey;
use serde::Deserialize;
use serde::Serialize;

use super::cell::encode_message;
use super::cell::OnionCellBucket;
use super::codec::OnionCircuitInput;
use super::codec::OnionWireMessage;
use super::protocol::OnionCircuitCapabilities;
use super::OnionBackwardFrame;
use super::OnionCircuitId;
use super::OnionCircuitPayload;
use super::OnionClientReturn;
use super::OnionForwardFrame;
use super::OnionForwardLayer;
use super::OnionForwardNonce;
use super::OnionForwardSequence;
use super::MAX_ONION_RELAY_CIRCUITS;
use super::ONION_FORWARD_MAX_VALIDITY_MS;
use super::ONION_RELAY_RETURN_TTL_MS;
use crate::error::Error;
use crate::error::Result;
use crate::extension::ext::Transition;
use crate::onion::OnionRouteError;

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub(super) struct RelayReturnKey {
    pub(super) circuit_id: OnionCircuitId,
    pub(super) next_hop: Did,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct RelayReturnEdge {
    pub(super) key: RelayReturnKey,
    pub(super) previous_hop: Did,
    pub(super) previous_circuit_id: OnionCircuitId,
    pub(super) previous_session_public_key: PublicKey<33>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct RelayReturnEntry {
    previous_hop: Did,
    previous_circuit_id: OnionCircuitId,
    previous_session_public_key: PublicKey<33>,
    expires_at_ms: u128,
}

/// Stateful return-hop table for encrypted relay circuits.
///
/// Invariant: every `(next_edge_id, next_hop) -> (previous_edge_id, previous_hop)` entry
/// represents exactly one live reverse edge learned from a prior forward relay action.
/// Preservation: forward relay insertion purges expired entries before capacity checks and never
/// rewrites a live key to a different previous hop; backward frames purge expired entries before
/// lookup and refresh only the matched edge.
/// Return-state removal is TTL-based because backward close semantics are encrypted to the
/// client and are not authenticated to relays.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct OnionCircuitState {
    relay_returns: Arc<BTreeMap<RelayReturnKey, RelayReturnEntry>>,
}

impl OnionCircuitState {
    #[cfg(test)]
    pub(super) fn relay_return_count(&self) -> usize {
        self.relay_returns.len()
    }

    #[cfg(test)]
    pub(super) fn shares_return_table_with(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.relay_returns, &other.relay_returns)
    }
}

/// Effects emitted by the route-aware circuit reducer.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OnionCircuitEffect {
    /// Run forward-layer crypto at the shell boundary and re-inject the decoded layer.
    DecryptCell {
        /// Authenticated immediate sender.
        from: Did,
        /// Public padding class; direction and exact length remain encrypted.
        bucket: OnionCellBucket,
        /// Hop-encrypted fixed-size cell payload.
        sealed: AeadCiphertext,
    },
    /// Decrypt one forward onion layer after its outer cell has authenticated the direction.
    DecryptForward {
        /// Authenticated immediate sender.
        from: Did,
        /// Cell receipt time captured once at the shell boundary.
        received_at_ms: u128,
        /// Public padding class to preserve on the next edge.
        bucket: OnionCellBucket,
        /// Edge-local circuit id bound into the layer AEAD.
        circuit_id: OnionCircuitId,
        /// Forward onion layer encrypted to this node.
        payload: AeadCiphertext,
    },
    /// Encrypt and send one fixed-size cell at the shell boundary.
    SealAndSend {
        /// Next hop.
        to: Did,
        /// Next hop session key authenticated inside the current layer.
        recipient: PublicKey<33>,
        /// Padding class preserved across relay edges.
        bucket: OnionCellBucket,
        /// Encoded direction and frame protected by the cell AEAD.
        encoded_message: Bytes,
    },
    /// A forward frame reached the exit.
    Exit {
        /// Authenticated immediate sender.
        from: Did,
        /// Random circuit correlation id.
        circuit_id: OnionCircuitId,
        /// Immediate return peer.
        return_peer: Did,
        /// Immediate return peer session key for the first backward cell.
        return_session_public_key: PublicKey<33>,
        /// Client return key.
        client: OnionClientReturn,
        /// Replay token consumed by one-shot exit operations; stream frames use `forward_sequence`.
        forward_nonce: OnionForwardNonce,
        /// Monotonic client-to-exit sequence within this circuit.
        forward_sequence: OnionForwardSequence,
        /// Application payload.
        payload: OnionCircuitPayload,
    },
    /// Decrypt a backward frame for this local client at the shell boundary.
    DecryptClient {
        /// Authenticated immediate sender.
        from: Did,
        /// Random circuit correlation id.
        circuit_id: OnionCircuitId,
        /// AEAD payload encrypted to the client session public key.
        payload: AeadCiphertext,
    },
}

/// Pure state relation for onion circuits.
///
/// ```text
/// CellObserved(encrypted)      -> [DecryptCell]
/// CellReady(forward relay)     -> state' with return edge, [SealAndSend next]
/// CellReady(forward exit)      -> state, [Exit]
/// CellReady(backward match)    -> state' with refreshed edge, [SealAndSend previous]
/// CellReady(backward no match) -> state, [DecryptClient]
/// CellReady(cover)             -> state, []
/// ```
///
/// Law: replaying `apply(state, input)` with the same values returns the same `(state', effects)`.
/// Clocks, crypto, IO, and locks are represented by effects and live in the shell.
#[derive(Clone, Debug)]
pub(super) struct OnionCircuitReducer {
    capabilities: OnionCircuitCapabilities,
}

impl OnionCircuitReducer {
    pub(super) const fn new(capabilities: OnionCircuitCapabilities) -> Self {
        Self { capabilities }
    }

    pub(super) fn apply(
        &self,
        state: &OnionCircuitState,
        input: OnionCircuitInput,
    ) -> Transition<OnionCircuitState, OnionCircuitEffect> {
        let mut state = state.clone();
        let effect = match input {
            OnionCircuitInput::CellObserved {
                from,
                bucket,
                sealed,
            } => Ok(Some(OnionCircuitEffect::DecryptCell {
                from,
                bucket,
                sealed,
            })),
            OnionCircuitInput::CellReady {
                from,
                received_at_ms,
                bucket,
                message,
            } => self.advance_cell(from, received_at_ms, bucket, message, &mut state),
            OnionCircuitInput::ForwardReady {
                from,
                received_at_ms,
                bucket,
                circuit_id,
                layer,
            } => self
                .advance_forward(from, received_at_ms, bucket, circuit_id, layer, &mut state)
                .map(Some),
        };

        match effect {
            Ok(Some(effect)) => Transition::with(state, vec![effect]),
            Ok(None) => Transition::pure(state),
            Err(error) => {
                tracing::debug!("drop onion circuit message: {error}");
                Transition::pure(state)
            }
        }
    }

    fn advance_cell(
        &self,
        from: Did,
        received_at_ms: u128,
        bucket: OnionCellBucket,
        message: OnionWireMessage,
        state: &mut OnionCircuitState,
    ) -> Result<Option<OnionCircuitEffect>> {
        match message {
            OnionWireMessage::Forward(frame) => {
                if !self.capabilities.accepts_forward_layers() {
                    return Err(Error::NoPermission);
                }
                Ok(Some(OnionCircuitEffect::DecryptForward {
                    from,
                    received_at_ms,
                    bucket,
                    circuit_id: frame.circuit_id,
                    payload: frame.layer,
                }))
            }
            OnionWireMessage::Backward(frame) => self
                .advance_backward(from, received_at_ms, bucket, frame, state)
                .map(Some),
            OnionWireMessage::Cover => Ok(None),
        }
    }

    fn advance_forward(
        &self,
        from: Did,
        received_at_ms: u128,
        bucket: OnionCellBucket,
        circuit_id: OnionCircuitId,
        layer: OnionForwardLayer,
        state: &mut OnionCircuitState,
    ) -> Result<OnionCircuitEffect> {
        if !self.capabilities.accepts_forward_layers() {
            return Err(Error::NoPermission);
        }
        match layer {
            OnionForwardLayer::Relay {
                next_hop,
                next_circuit_id,
                next_session_public_key,
                return_session_public_key,
                inner,
            } => {
                self.validate_relay_forward()?;
                remember_return_hop(
                    state,
                    MAX_ONION_RELAY_CIRCUITS,
                    ONION_RELAY_RETURN_TTL_MS,
                    RelayReturnEdge {
                        key: RelayReturnKey {
                            circuit_id: next_circuit_id,
                            next_hop,
                        },
                        previous_hop: from,
                        previous_circuit_id: circuit_id,
                        previous_session_public_key: return_session_public_key,
                    },
                    received_at_ms,
                )?;
                encode_message(&OnionWireMessage::Forward(OnionForwardFrame {
                    circuit_id: next_circuit_id,
                    layer: inner,
                }))
                .map(|encoded_message| OnionCircuitEffect::SealAndSend {
                    to: next_hop,
                    recipient: next_session_public_key,
                    bucket,
                    encoded_message,
                })
            }
            OnionForwardLayer::Exit {
                client,
                return_session_public_key,
                expires_at_ms,
                forward_nonce,
                forward_sequence,
                payload,
            } => {
                if !self.capabilities.permits_exit_layer() {
                    return Err(Error::NoPermission);
                }
                // Invariant: every accepted layer expires while its replay witness is still live.
                // The upper bound also prevents a malicious client from extending authenticated
                // validity beyond the finite replay-cache retention contract.
                if expires_at_ms <= received_at_ms
                    || expires_at_ms > received_at_ms.saturating_add(ONION_FORWARD_MAX_VALIDITY_MS)
                {
                    return Err(Error::OnionRouteError(
                        OnionRouteError::ForwardPayloadExpired,
                    ));
                }
                Ok(OnionCircuitEffect::Exit {
                    from,
                    circuit_id,
                    return_peer: from,
                    return_session_public_key,
                    client,
                    forward_nonce,
                    forward_sequence,
                    payload,
                })
            }
        }
    }

    fn advance_backward(
        &self,
        from: Did,
        received_at_ms: u128,
        bucket: OnionCellBucket,
        frame: OnionBackwardFrame,
        state: &mut OnionCircuitState,
    ) -> Result<OnionCircuitEffect> {
        purge_expired_return_hops(state, received_at_ms);
        let key = RelayReturnKey {
            circuit_id: frame.circuit_id,
            next_hop: from,
        };
        if let Some(entry) = state.relay_returns.get(&key).copied() {
            let previous_hop = entry.previous_hop;
            let previous_circuit_id = entry.previous_circuit_id;
            let previous_session_public_key = entry.previous_session_public_key;
            if let Some(entry) = Arc::make_mut(&mut state.relay_returns).get_mut(&key) {
                entry.expires_at_ms = received_at_ms.saturating_add(ONION_RELAY_RETURN_TTL_MS);
            }
            let encoded_message =
                encode_message(&OnionWireMessage::Backward(OnionBackwardFrame {
                    circuit_id: previous_circuit_id,
                    payload: frame.payload,
                }))?;
            return Ok(OnionCircuitEffect::SealAndSend {
                to: previous_hop,
                recipient: previous_session_public_key,
                bucket,
                encoded_message,
            });
        }

        Ok(OnionCircuitEffect::DecryptClient {
            from,
            circuit_id: frame.circuit_id,
            payload: frame.payload,
        })
    }

    fn validate_relay_forward(&self) -> Result<()> {
        if !self.capabilities.permits_relay_layer() {
            return Err(Error::NoPermission);
        }
        // The route constructor bounds honest routes. Untrusted recursive layers are bounded by
        // the identity-independent crypto window and relay-return capacity instead of an exact
        // countdown that would disclose this relay's absolute position.
        Ok(())
    }
}

pub(super) fn remember_return_hop(
    state: &mut OnionCircuitState,
    max_relay_circuits: usize,
    ttl_ms: u128,
    edge: RelayReturnEdge,
    now_ms: u128,
) -> Result<()> {
    let RelayReturnEdge {
        key,
        previous_hop,
        previous_circuit_id,
        previous_session_public_key,
    } = edge;
    purge_expired_return_hops(state, now_ms);
    let table = Arc::make_mut(&mut state.relay_returns);
    let table_is_full = table.len() >= max_relay_circuits;
    let peer_table_is_full = table
        .values()
        .filter(|entry| entry.previous_hop == previous_hop)
        .count()
        >= max_relay_circuits_per_peer(max_relay_circuits);
    match table.entry(key) {
        Entry::Occupied(mut entry) => {
            if entry.get().previous_hop != previous_hop
                || entry.get().previous_circuit_id != previous_circuit_id
                || entry.get().previous_session_public_key != previous_session_public_key
            {
                return Err(Error::OnionRouteError(OnionRouteError::ReturnEdgeConflict));
            }
            entry.get_mut().expires_at_ms = now_ms.saturating_add(ttl_ms);
        }
        Entry::Vacant(entry) => {
            if table_is_full {
                return Err(Error::OnionRouteError(OnionRouteError::RelayTableFull));
            }
            if peer_table_is_full {
                return Err(Error::OnionRouteError(OnionRouteError::RelayPeerTableFull));
            }
            entry.insert(RelayReturnEntry {
                previous_hop,
                previous_circuit_id,
                previous_session_public_key,
                expires_at_ms: now_ms.saturating_add(ttl_ms),
            });
        }
    }
    Ok(())
}

/// Reserve at most one sixteenth of the global relay-return capacity for any
/// authenticated previous hop. Therefore one peer cannot exclude honest peers
/// while the global table has free entries.
const fn max_relay_circuits_per_peer(max_relay_circuits: usize) -> usize {
    max_relay_circuits.div_ceil(16)
}

fn purge_expired_return_hops(state: &mut OnionCircuitState, now_ms: u128) {
    if state
        .relay_returns
        .values()
        .any(|entry| entry.expires_at_ms <= now_ms)
    {
        Arc::make_mut(&mut state.relay_returns).retain(|_, entry| entry.expires_at_ms > now_ms);
    }
}