coqui_peer/method.rs
1//! The sans-IO handshake every authentication method implements.
2//!
3//! A [`Handshake`] does no I/O. The caller feeds it what arrived with
4//! [`receive`](Handshake::receive), asks what to send with
5//! [`transmit`](Handshake::transmit), and when [`is_complete`](Handshake::is_complete)
6//! calls [`finish`](Handshake::finish) for the verified peer and whatever the method
7//! produced (an encrypted channel, session keys, or nothing). Because the caller owns the
8//! transport, one implementation runs over a byte stream, WebSocket messages, QUIC
9//! datagrams or shared memory; [`drive_stream`](crate::drive_stream) and
10//! [`drive_messages`](crate::drive_messages) (and the `asynch` versions with the `tokio`
11//! feature) are the thin loops for the common cases.
12//!
13//! # Requirements on a method
14//!
15//! - **Key proof before pairing.** Record a peer in the [`TrustStore`](crate::TrustStore)
16//! as waiting only after it proved, inside this handshake, that it holds the key it
17//! presented. See the store's pairing contract.
18//! - **`finish` before completion is an error**, never a half-verified peer.
19//! - **No input is dropped.** Bytes received beyond the end of the handshake stay in the
20//! [`Output`](Handshake::Output) (a TLS connection keeps its buffered application data).
21//! - **Nothing global.** A method keeps its state in the handshake value.
22
23use core::net::SocketAddr;
24
25use crate::id::PeerId;
26use crate::key::KeyMaterial;
27
28/// How a method divides what it sends.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub enum Framing {
31 /// A byte stream: the boundaries between [`receive`](Handshake::receive) calls and
32 /// between [`transmit`](Handshake::transmit) results mean nothing, so the bytes may be
33 /// split or joined by the transport. Reliable, ordered transports only.
34 Stream,
35 /// Discrete messages: each [`transmit`](Handshake::transmit) result is one message and
36 /// each [`receive`](Handshake::receive) call gets exactly one. A byte-stream transport
37 /// must add framing; [`drive_stream`](crate::drive_stream) prefixes each message with its
38 /// length as four big-endian bytes.
39 Message,
40}
41
42/// A verified peer and what the method produced besides.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Established<S> {
45 /// The peer's id, verified by the handshake.
46 pub peer: PeerId,
47 /// The peer's public key material, when the method has one, to hand to a
48 /// [`TrustStore`](crate::TrustStore).
49 pub key: Option<KeyMaterial>,
50 /// What the method yields: an encrypted channel (for TLS, the finished connection),
51 /// session keys, or `()` for a method that only authenticates. The type says which.
52 pub session: S,
53}
54
55/// One authentication exchange, driven by the caller.
56pub trait Handshake {
57 /// What a completed handshake yields besides the peer.
58 type Output;
59 /// Why the handshake failed. Never holds key material.
60 type Error: core::error::Error + Send + Sync + 'static;
61
62 /// How this method divides what it sends.
63 fn framing(&self) -> Framing;
64
65 /// Gives the handshake what arrived from the peer: a chunk of the stream, or one message
66 /// (see [`Framing`]).
67 ///
68 /// # Errors
69 ///
70 /// When the input is invalid or the peer is refused. The handshake is over after an
71 /// error.
72 fn receive(&mut self, input: &[u8]) -> Result<(), Self::Error>;
73
74 /// The next thing to send to the peer, or `None` when there is nothing to send until more
75 /// input arrives. Call until `None`.
76 ///
77 /// # Errors
78 ///
79 /// When the handshake cannot continue.
80 fn transmit(&mut self) -> Result<Option<Vec<u8>>, Self::Error>;
81
82 /// Whether the exchange is over and [`finish`](Handshake::finish) can be called; it
83 /// succeeds when the method verified a peer. Output still waiting in
84 /// [`transmit`](Handshake::transmit) must be sent first.
85 fn is_complete(&self) -> bool;
86
87 /// The verified peer and the method's output.
88 ///
89 /// # Errors
90 ///
91 /// When the handshake is not complete.
92 fn finish(self) -> Result<Established<Self::Output>, Self::Error>;
93}
94
95/// An authentication method: a configured factory of handshakes.
96///
97/// Methods are named `coqui-peer-<method>`; [`name`](Method::name) is the `<method>` part and
98/// the name under which the method's keys are kept in the trust store.
99pub trait Method {
100 /// The handshake this method runs.
101 type Handshake: Handshake;
102
103 /// The method name: lowercase letters, digits and `-`, as in [`KeyMaterial::method`].
104 fn name(&self) -> &'static str;
105
106 /// Starts a handshake as the side that dials.
107 ///
108 /// # Errors
109 ///
110 /// When this method is not configured to dial.
111 fn initiate(&self) -> Result<Self::Handshake, <Self::Handshake as Handshake>::Error>;
112
113 /// Starts a handshake as the side that accepts. `source` is where the peer connected from;
114 /// it is recorded with a waiting entry.
115 ///
116 /// # Errors
117 ///
118 /// When this method is not configured to accept.
119 fn respond(
120 &self,
121 source: Option<SocketAddr>,
122 ) -> Result<Self::Handshake, <Self::Handshake as Handshake>::Error>;
123}