libfcp-core 1.0.0-rc.1

Portable signed wire protocol and configuration core for Federated CFR Connect Protocol
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
// Copyright Nixort <https://github.com/Nixort> 2026.
//
// License: GNU General Public License v3.0 only.
// You can find the license file in the project root.
//
// Federated CFR Connect Protocol (FCP).

//! Per-peer FCP connection state machine and engine actions.

use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::vec;
use alloc::vec::Vec;

use crate::envelope::{Body, Envelope};
use crate::error::Error;
use crate::identity::{EndpointIdentity, EndpointSigner};
use crate::types::{
    AttemptId, CloseCode, EnvelopeId, FederationId, WebRtcBinding, MAX_SEEN_ENVELOPES, PROTOCOL_ID,
};

/// Connection lifecycle state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
    /// No offer or answer exists.
    Idle,
    /// This endpoint has sent an offer.
    OfferSent,
    /// A valid offer from the configured peer awaits a local answer.
    OfferReceived,
    /// This endpoint has sent an answer and awaits transport connection.
    AnswerSent,
    /// This endpoint has received an answer and awaits transport connection.
    AnswerReceived,
    /// The platform adapter reported the WebRTC control channel connected.
    Established,
    /// A local or remote close finished this attempt.
    Closed,
}

#[derive(Debug, Clone, Copy)]
struct NegotiationIds {
    offer: EnvelopeId,
    answer: Option<EnvelopeId>,
}

/// An action emitted by the core for an untrusted transport or platform adapter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
    /// Send a signed envelope through any configured signaling or data path.
    Send(Box<Envelope>),
    /// Give an opaque offer description to the selected WebRTC engine.
    ApplyOffer {
        /// Signed digest binding exact description and DTLS fingerprint bytes.
        binding: WebRtcBinding,
        /// Exact bounded opaque description to give the platform engine.
        description: Vec<u8>,
    },
    /// Give an opaque answer description to the selected WebRTC engine.
    ApplyAnswer {
        /// Signed digest binding exact description and DTLS fingerprint bytes.
        binding: WebRtcBinding,
        /// Exact bounded opaque description to give the platform engine.
        description: Vec<u8>,
    },
    /// Add one opaque ICE candidate to the selected WebRTC engine.
    AddCandidate {
        /// Sender-local diagnostic sequence number.
        sequence: u32,
        /// Exact bounded opaque candidate to give the platform engine.
        candidate: Vec<u8>,
    },
    /// Ask the adapter to configure/open the fixed reliable ordered FCP control channel.
    OpenControlChannel,
    /// Deliver an exact raw CFR payload with its verified FCP transport origin.
    DeliverCfr {
        /// Stable identifier of the verified signed FCP envelope carrying this payload.
        envelope_id: EnvelopeId,
        /// Complete FCP endpoint identity that signed the carrying envelope.
        remote_endpoint: Box<EndpointIdentity>,
        /// Unmodified CFR wire payload.
        payload: Vec<u8>,
    },
    /// Close the selected platform connection.
    CloseTransport {
        /// Application close code from a signed remote envelope.
        reason: CloseCode,
    },
}

/// Fixed FCP data-channel configuration that platform adapters must enforce.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ControlChannelConfig {
    /// Stable data channel label.
    pub label: &'static str,
    /// Stable subprotocol string.
    pub protocol: &'static str,
    /// FCP control is ordered.
    pub ordered: bool,
    /// FCP control is fully reliable.
    pub reliable: bool,
    /// FCP control uses binary messages.
    pub binary: bool,
}

/// Required FCP data-channel configuration.
pub const CONTROL_CHANNEL: ControlChannelConfig = ControlChannelConfig {
    label: "org.nixort.cfr.fcp.control/1",
    protocol: PROTOCOL_ID,
    ordered: true,
    reliable: true,
    binary: true,
};

/// Per-peer state machine for one FCP connection attempt.
#[derive(Debug)]
pub struct Connection {
    federation: FederationId,
    attempt: AttemptId,
    local: EndpointIdentity,
    remote: EndpointIdentity,
    phase: Phase,
    negotiation: Option<NegotiationIds>,
    seen: VecDeque<EnvelopeId>,
}

impl Connection {
    /// Creates an idle connection pinned to one federation, attempt and complete remote identity.
    pub fn new(
        federation: FederationId,
        attempt: AttemptId,
        local: EndpointIdentity,
        remote: EndpointIdentity,
    ) -> Result<Self, Error> {
        if local == remote {
            return Err(Error::SameEndpoint);
        }
        Ok(Self {
            federation,
            attempt,
            local,
            remote,
            phase: Phase::Idle,
            negotiation: None,
            seen: VecDeque::new(),
        })
    }

    /// Returns the federation namespace pinned for this attempt.
    pub const fn federation(&self) -> FederationId {
        self.federation
    }

    /// Returns the application-provided attempt identifier pinned for this connection.
    pub const fn attempt(&self) -> AttemptId {
        self.attempt
    }

    /// Returns the complete local FCP endpoint identity pinned for this connection.
    pub const fn local_endpoint(&self) -> EndpointIdentity {
        self.local
    }

    /// Returns the complete remote FCP endpoint identity pinned for this connection.
    pub const fn remote_endpoint(&self) -> EndpointIdentity {
        self.remote
    }

    /// Returns the current lifecycle state.
    pub const fn phase(&self) -> Phase {
        self.phase
    }

    /// Starts a local offer from `Idle`.
    pub fn begin_offer<S: EndpointSigner>(
        &mut self,
        signer: &S,
        binding: WebRtcBinding,
        description: Vec<u8>,
    ) -> Result<Vec<Action>, Error> {
        self.require_local_signer(signer)?;
        if self.phase != Phase::Idle {
            return Err(Error::InvalidState);
        }
        let offer = Envelope::sign(
            signer,
            self.federation,
            self.attempt,
            self.remote,
            Body::Offer {
                binding,
                description,
            },
        )?;
        self.negotiation = Some(NegotiationIds {
            offer: offer.id()?,
            answer: None,
        });
        self.phase = Phase::OfferSent;
        Ok(vec![
            Action::OpenControlChannel,
            Action::Send(Box::new(offer)),
        ])
    }

    /// Answers one accepted remote offer.
    pub fn answer<S: EndpointSigner>(
        &mut self,
        signer: &S,
        binding: WebRtcBinding,
        description: Vec<u8>,
    ) -> Result<Action, Error> {
        self.require_local_signer(signer)?;
        if self.phase != Phase::OfferReceived {
            return Err(Error::InvalidState);
        }
        let offer_id = self.negotiation.ok_or(Error::InvalidState)?.offer;
        let answer = Envelope::sign(
            signer,
            self.federation,
            self.attempt,
            self.remote,
            Body::Answer {
                offer_id,
                binding,
                description,
            },
        )?;
        self.negotiation = Some(NegotiationIds {
            offer: offer_id,
            answer: Some(answer.id()?),
        });
        self.phase = Phase::AnswerSent;
        Ok(Action::Send(Box::new(answer)))
    }

    /// Creates a candidate bound to the active negotiation.
    pub fn candidate<S: EndpointSigner>(
        &self,
        signer: &S,
        sequence: u32,
        candidate: Vec<u8>,
    ) -> Result<Action, Error> {
        self.require_local_signer(signer)?;
        if !matches!(
            self.phase,
            Phase::OfferSent
                | Phase::OfferReceived
                | Phase::AnswerSent
                | Phase::AnswerReceived
                | Phase::Established
        ) {
            return Err(Error::InvalidState);
        }
        let ids = self.negotiation.ok_or(Error::InvalidState)?;
        let parent_id = ids.answer.unwrap_or(ids.offer);
        let envelope = Envelope::sign(
            signer,
            self.federation,
            self.attempt,
            self.remote,
            Body::Candidate {
                parent_id,
                sequence,
                candidate,
            },
        )?;
        Ok(Action::Send(Box::new(envelope)))
    }

    /// Emits one bounded raw CFR control envelope after WebRTC connection is established.
    pub fn cfr_control<S: EndpointSigner>(
        &self,
        signer: &S,
        payload: Vec<u8>,
    ) -> Result<Action, Error> {
        self.require_local_signer(signer)?;
        if self.phase != Phase::Established {
            return Err(Error::InvalidState);
        }
        Ok(Action::Send(Box::new(Envelope::sign(
            signer,
            self.federation,
            self.attempt,
            self.remote,
            Body::CfrControl { payload },
        )?)))
    }

    /// Emits a local close and closes the core attempt.
    pub fn close<S: EndpointSigner>(
        &mut self,
        signer: &S,
        reason: CloseCode,
    ) -> Result<Action, Error> {
        self.require_local_signer(signer)?;
        if matches!(self.phase, Phase::Idle | Phase::Closed) {
            return Err(Error::InvalidState);
        }
        self.phase = Phase::Closed;
        Ok(Action::Send(Box::new(Envelope::sign(
            signer,
            self.federation,
            self.attempt,
            self.remote,
            Body::Close { reason },
        )?)))
    }

    /// Processes a received envelope only after exact binding and signature validation.
    pub fn receive(&mut self, envelope: Envelope) -> Result<Vec<Action>, Error> {
        self.validate_inbound(&envelope)?;
        let id = envelope.id()?;
        let remote_endpoint = envelope.sender;
        if self.seen.iter().any(|known| known == &id) {
            return Ok(Vec::new());
        }
        let actions = match envelope.body {
            Body::Offer {
                binding,
                description,
            } => {
                if self.phase == Phase::OfferSent {
                    return Err(Error::Glare);
                }
                if self.phase != Phase::Idle {
                    return Err(Error::InvalidState);
                }
                self.negotiation = Some(NegotiationIds {
                    offer: id,
                    answer: None,
                });
                self.phase = Phase::OfferReceived;
                vec![Action::ApplyOffer {
                    binding,
                    description,
                }]
            }
            Body::Answer {
                offer_id,
                binding,
                description,
            } => {
                if self.phase != Phase::OfferSent
                    || self.negotiation.ok_or(Error::InvalidState)?.offer != offer_id
                {
                    return Err(Error::InvalidState);
                }
                let offer = self.negotiation.ok_or(Error::InvalidState)?.offer;
                self.negotiation = Some(NegotiationIds {
                    offer,
                    answer: Some(id),
                });
                self.phase = Phase::AnswerReceived;
                vec![Action::ApplyAnswer {
                    binding,
                    description,
                }]
            }
            Body::Candidate {
                parent_id,
                sequence,
                candidate,
            } => {
                self.validate_candidate_parent(parent_id)?;
                vec![Action::AddCandidate {
                    sequence,
                    candidate,
                }]
            }
            Body::Close { reason } => {
                if matches!(self.phase, Phase::Idle | Phase::Closed) {
                    return Err(Error::InvalidState);
                }
                self.phase = Phase::Closed;
                vec![Action::CloseTransport { reason }]
            }
            Body::CfrControl { payload } => {
                if self.phase != Phase::Established {
                    return Err(Error::InvalidState);
                }
                vec![Action::DeliverCfr {
                    envelope_id: id,
                    remote_endpoint: Box::new(remote_endpoint),
                    payload,
                }]
            }
        };
        self.note_seen(id);
        Ok(actions)
    }

    /// Records that the platform adapter reached a connected WebRTC control channel.
    pub fn transport_connected(&mut self) -> Result<Vec<Action>, Error> {
        if !matches!(self.phase, Phase::AnswerSent | Phase::AnswerReceived) {
            return Err(Error::InvalidState);
        }
        self.phase = Phase::Established;
        Ok(Vec::new())
    }

    /// Records a local platform failure and stops this attempt without manufacturing a remote close.
    pub fn transport_failed(&mut self) -> Result<(), Error> {
        if matches!(self.phase, Phase::Idle | Phase::Closed) {
            return Err(Error::InvalidState);
        }
        self.phase = Phase::Closed;
        Ok(())
    }

    fn require_local_signer<S: EndpointSigner>(&self, signer: &S) -> Result<(), Error> {
        if signer.endpoint() != self.local {
            return Err(Error::WrongLocalSigner);
        }
        Ok(())
    }

    fn validate_inbound(&self, envelope: &Envelope) -> Result<(), Error> {
        if envelope.federation != self.federation {
            return Err(Error::WrongFederation);
        }
        if envelope.attempt != self.attempt {
            return Err(Error::WrongAttempt);
        }
        if envelope.sender != self.remote {
            return Err(Error::WrongSender);
        }
        if envelope.recipient != self.local {
            return Err(Error::WrongRecipient);
        }
        envelope.verify()
    }

    fn validate_candidate_parent(&self, parent: EnvelopeId) -> Result<(), Error> {
        let ids = self.negotiation.ok_or(Error::InvalidState)?;
        if !matches!(
            self.phase,
            Phase::OfferSent
                | Phase::OfferReceived
                | Phase::AnswerSent
                | Phase::AnswerReceived
                | Phase::Established
        ) {
            return Err(Error::InvalidState);
        }
        if parent != ids.offer && ids.answer != Some(parent) {
            return Err(Error::WrongCandidateParent);
        }
        Ok(())
    }

    fn note_seen(&mut self, id: EnvelopeId) {
        if self.seen.len() == MAX_SEEN_ENVELOPES {
            let _ = self.seen.pop_front();
        }
        self.seen.push_back(id);
    }
}