ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
phase_machine.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file phase_machine.h
6 * @brief The handshake phase machine, RFC 4253 sec 4.2 through sec 10.
7 *
8 * One connection walks the transport layer's own progression, then hands over to RFC 4252:
9 *
10 * IDENT (sec 4.2) -> KEXINIT (sec 7.1) -> DH_INIT (sec 8) -> NEWKEYS (sec 7.3)
11 * -> SERVICE (sec 10) -> AUTH (RFC 4252) -> OPEN (RFC 4254)
12 *
13 * and sec 9 sends an established connection back to KEXINIT for a re-exchange. The advance
14 * functions are named for the RFC event that causes them and are the only writers of the phase;
15 * the admits functions are the questions the dispatch asks before acting on a message.
16 */
17
18#ifndef PROTOCORE_TRANSPORT_PHASE_MACHINE_H
19#define PROTOCORE_TRANSPORT_PHASE_MACHINE_H
20
21#include "protocore_config.h" // the entry point: protocore_types.h for the widths
22
23#if PROTOCORE_ENABLE_SSH || PROTOCORE_ENABLE_SSH_CLIENT
24
25EMBED_BEGIN_DECLS
26
27// This module holds nothing between calls, so it carves no borrow and states none. An entry
28// takes one all the same, and never reads it, so every namespace in the tree is invoked the
29// same way.
30
31/** @brief SSH connection lifecycle phase. */
32EMBED_ENUM_BYTE(SshPhase,
33 SSH_PHASE_IDENT, ///< Awaiting the peer identification string.
34 SSH_PHASE_KEXINIT, ///< Awaiting the peer KEXINIT.
35 SSH_PHASE_DH_INIT, ///< Awaiting SSH_MSG_KEXDH_INIT.
36 SSH_PHASE_NEWKEYS, ///< Awaiting SSH_MSG_NEWKEYS.
37 SSH_PHASE_SERVICE, ///< Awaiting SERVICE_REQUEST ("ssh-userauth").
38 SSH_PHASE_AUTH, ///< User authentication in progress (RFC 4252).
39 SSH_PHASE_OPEN ///< Authenticated; connection/channel protocol active.
40);
41
42/** @brief What get takes: i. */
43typedef struct
44{
45 uint8_t i;
46} PhaseMachineGetArgs;
47
48/** @brief What is takes: i, p. */
49typedef struct
50{
51 uint8_t i;
52 SshPhase p;
53} PhaseMachineIsArgs;
54
55/** @brief What reset takes: i. */
56typedef struct
57{
58 uint8_t i;
59} PhaseMachineResetArgs;
60
61/** @brief What ident_done takes: i. */
62typedef struct
63{
64 uint8_t i;
65} PhaseMachineIdentDoneArgs;
66
67/** @brief What kexinit_done takes: i. */
68typedef struct
69{
70 uint8_t i;
71} PhaseMachineKexinitDoneArgs;
72
73/** @brief What kex_done takes: i. */
74typedef struct
75{
76 uint8_t i;
77} PhaseMachineKexDoneArgs;
78
79/** @brief What newkeys_done takes: i. */
80typedef struct
81{
82 uint8_t i;
83} PhaseMachineNewkeysDoneArgs;
84
85/** @brief What service_done takes: i. */
86typedef struct
87{
88 uint8_t i;
89} PhaseMachineServiceDoneArgs;
90
91/** @brief What auth_done takes: i. */
92typedef struct
93{
94 uint8_t i;
95} PhaseMachineAuthDoneArgs;
96
97/** @brief What rekey_begin takes: i. */
98typedef struct
99{
100 uint8_t i;
101} PhaseMachineRekeyBeginArgs;
102
103/** @brief What admits_ident takes: i. */
104typedef struct
105{
106 uint8_t i;
107} PhaseMachineAdmitsIdentArgs;
108
109/** @brief What admits_kexinit takes: i. */
110typedef struct
111{
112 uint8_t i;
113} PhaseMachineAdmitsKexinitArgs;
114
115/** @brief What kexinit_needs_reply takes: i. */
116typedef struct
117{
118 uint8_t i;
119} PhaseMachineKexinitNeedsReplyArgs;
120
121/** @brief What admits_kexdh_init takes: i. */
122typedef struct
123{
124 uint8_t i;
125} PhaseMachineAdmitsKexdhInitArgs;
126
127/** @brief What admits_newkeys takes: i. */
128typedef struct
129{
130 uint8_t i;
131} PhaseMachineAdmitsNewkeysArgs;
132
133/** @brief What admits_service_request takes: i. */
134typedef struct
135{
136 uint8_t i;
137} PhaseMachineAdmitsServiceRequestArgs;
138
139/** @brief What admits_userauth takes: i. */
140typedef struct
141{
142 uint8_t i;
143} PhaseMachineAdmitsUserauthArgs;
144
145/** @brief What auth_complete takes: i. */
146typedef struct
147{
148 uint8_t i;
149} PhaseMachineAuthCompleteArgs;
150
151/** @brief What admits_rekey takes: i. */
152typedef struct
153{
154 uint8_t i;
155} PhaseMachineAdmitsRekeyArgs;
156
157/** @brief What is_open takes: i. */
158typedef struct
159{
160 uint8_t i;
161} PhaseMachineIsOpenArgs;
162
163/**
164 * @brief The handshake phase machine, RFC 4253 sec 4.2 through sec 10.
165 *
166 * A caller sets the members a call takes, invokes it through ::PhaseMachine with the bytes it runs
167 * out of, and reads the outcome off the same handle.
168 *
169 * PhaseMachine.get_args.i = ...;
170 * PhaseMachine.get(work);
171 * // PhaseMachine.value is what the call reports
172 *
173 * @var PhaseMachineNs::get_args what get takes: i
174 * @var PhaseMachineNs::is_args what is takes: i, p
175 * @var PhaseMachineNs::reset_args what reset takes: i
176 * @var PhaseMachineNs::ident_done_args what ident_done takes: i
177 * @var PhaseMachineNs::kexinit_done_args what kexinit_done takes: i
178 * @var PhaseMachineNs::kex_done_args what kex_done takes: i
179 * @var PhaseMachineNs::newkeys_done_args what newkeys_done takes: i
180 * @var PhaseMachineNs::service_done_args what service_done takes: i
181 * @var PhaseMachineNs::auth_done_args what auth_done takes: i
182 * @var PhaseMachineNs::rekey_begin_args what rekey_begin takes: i
183 * @var PhaseMachineNs::admits_ident_args what admits_ident takes: i
184 * @var PhaseMachineNs::admits_kexinit_args what admits_kexinit takes: i
185 * @var PhaseMachineNs::kexinit_needs_reply_args what kexinit_needs_reply takes: i
186 * @var PhaseMachineNs::admits_kexdh_init_args what admits_kexdh_init takes: i
187 * @var PhaseMachineNs::admits_newkeys_args what admits_newkeys takes: i
188 * @var PhaseMachineNs::admits_service_request_args what admits_service_request takes: i
189 * @var PhaseMachineNs::admits_userauth_args what admits_userauth takes: i
190 * @var PhaseMachineNs::auth_complete_args what auth_complete takes: i
191 * @var PhaseMachineNs::admits_rekey_args what admits_rekey takes: i
192 * @var PhaseMachineNs::is_open_args what is_open takes: i
193 * @var PhaseMachineNs::ok a call's true/false outcome
194 * @var PhaseMachineNs::value the value a call reports
195 * @var PhaseMachineNs::get the phase slot i is in
196 * @var PhaseMachineNs::is true when slot i is in phase p
197 * @var PhaseMachineNs::reset start a connection at the identification exchange (RFC 4253 sec 4.2)
198 * @var PhaseMachineNs::ident_done the peer identification string is whole: sec 4.2 is done, sec 7.1 ...
199 * @var PhaseMachineNs::kexinit_done algorithms are negotiated (sec 7.1): await the exchange of sec 8
200 * @var PhaseMachineNs::kex_done the exchange produced K and H (sec 8): await NEWKEYS
201 * @var PhaseMachineNs::newkeys_done NEWKEYS ended the exchange (sec 7.3): the keys are in use. A first ...
202 * @var PhaseMachineNs::service_done the service request named "ssh-userauth" and was accepted (sec 10)
203 * @var PhaseMachineNs::auth_done authentication completed (RFC 4252 sec 5.1 SUCCESS): the channel ...
204 * @var PhaseMachineNs::rekey_begin A key re-exchange begins on an established connection (sec 9)
205 * @var PhaseMachineNs::admits_ident RFC 4253 sec 4.2: identification bytes are only read before the ...
206 * @var PhaseMachineNs::admits_kexinit RFC 4253 sec 7.1: a KEXINIT starts an exchange, and one is refused ...
207 * @var PhaseMachineNs::kexinit_needs_reply RFC 4253 sec 7.1: does an inbound KEXINIT still need our own in ...
208 * @var PhaseMachineNs::admits_kexdh_init RFC 4253 sec 8: KEXDH_INIT belongs to an exchange that has been ...
209 * @var PhaseMachineNs::admits_newkeys RFC 4253 sec 7.3: "NEWKEYS ends a key exchange", so one outside an ...
210 * @var PhaseMachineNs::admits_service_request RFC 4253 sec 10: a service request is only valid once the exchange ...
211 * @var PhaseMachineNs::admits_userauth RFC 4252: a userauth request belongs to the authentication phase
212 * @var PhaseMachineNs::auth_complete RFC 4252 sec 5.1: a request arriving after SUCCESS is ignored ...
213 * @var PhaseMachineNs::admits_rekey RFC 4253 sec 9: a re-exchange starts only on an established ...
214 * @var PhaseMachineNs::is_open RFC 4254: the channel protocol runs once the connection is open
215 *
216 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
217 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
218 * a caller drives every namespace the same way.
219 */
220typedef struct
221{
222 PhaseMachineGetArgs get_args;
223 PhaseMachineIsArgs is_args;
224 PhaseMachineResetArgs reset_args;
225 PhaseMachineIdentDoneArgs ident_done_args;
226 PhaseMachineKexinitDoneArgs kexinit_done_args;
227 PhaseMachineKexDoneArgs kex_done_args;
228 PhaseMachineNewkeysDoneArgs newkeys_done_args;
229 PhaseMachineServiceDoneArgs service_done_args;
230 PhaseMachineAuthDoneArgs auth_done_args;
231 PhaseMachineRekeyBeginArgs rekey_begin_args;
232 PhaseMachineAdmitsIdentArgs admits_ident_args;
233 PhaseMachineAdmitsKexinitArgs admits_kexinit_args;
234 PhaseMachineKexinitNeedsReplyArgs kexinit_needs_reply_args;
235 PhaseMachineAdmitsKexdhInitArgs admits_kexdh_init_args;
236 PhaseMachineAdmitsNewkeysArgs admits_newkeys_args;
237 PhaseMachineAdmitsServiceRequestArgs admits_service_request_args;
238 PhaseMachineAdmitsUserauthArgs admits_userauth_args;
239 PhaseMachineAuthCompleteArgs auth_complete_args;
240 PhaseMachineAdmitsRekeyArgs admits_rekey_args;
241 PhaseMachineIsOpenArgs is_open_args;
242 embed_bool ok;
243 SshPhase value;
244} PhaseMachineVars;
245
246/** @brief The operands and the outcome. */
247extern PhaseMachineVars PhaseMachineV;
248
249/** @brief The entries. */
250typedef struct
251{
252 void (*const get)(const uint8_t *work);
253 void (*const is)(const uint8_t *work);
254 void (*const reset)(const uint8_t *work);
255 void (*const ident_done)(const uint8_t *work);
256 void (*const kexinit_done)(const uint8_t *work);
257 void (*const kex_done)(const uint8_t *work);
258 void (*const newkeys_done)(const uint8_t *work);
259 void (*const service_done)(const uint8_t *work);
260 void (*const auth_done)(const uint8_t *work);
261 void (*const rekey_begin)(const uint8_t *work);
262 void (*const admits_ident)(const uint8_t *work);
263 void (*const admits_kexinit)(const uint8_t *work);
264 void (*const kexinit_needs_reply)(const uint8_t *work);
265 void (*const admits_kexdh_init)(const uint8_t *work);
266 void (*const admits_newkeys)(const uint8_t *work);
267 void (*const admits_service_request)(const uint8_t *work);
268 void (*const admits_userauth)(const uint8_t *work);
269 void (*const auth_complete)(const uint8_t *work);
270 void (*const admits_rekey)(const uint8_t *work);
271 void (*const is_open)(const uint8_t *work);
272} PhaseMachineNs;
273
274// What the table binds, defined once in the .c and taking one parameter each: everything
275// else an entry needs is an operand in PhaseMachineV or a region of the borrow at a fixed offset.
276void protocore_phase_machine_get(const uint8_t *work);
277void protocore_phase_machine_is(const uint8_t *work);
278void protocore_phase_machine_reset(const uint8_t *work);
279void protocore_phase_machine_ident_done(const uint8_t *work);
280void protocore_phase_machine_kexinit_done(const uint8_t *work);
281void protocore_phase_machine_kex_done(const uint8_t *work);
282void protocore_phase_machine_newkeys_done(const uint8_t *work);
283void protocore_phase_machine_service_done(const uint8_t *work);
284void protocore_phase_machine_auth_done(const uint8_t *work);
285void protocore_phase_machine_rekey_begin(const uint8_t *work);
286void protocore_phase_machine_admits_ident(const uint8_t *work);
287void protocore_phase_machine_admits_kexinit(const uint8_t *work);
288void protocore_phase_machine_kexinit_needs_reply(const uint8_t *work);
289void protocore_phase_machine_admits_kexdh_init(const uint8_t *work);
290void protocore_phase_machine_admits_newkeys(const uint8_t *work);
291void protocore_phase_machine_admits_service_request(const uint8_t *work);
292void protocore_phase_machine_admits_userauth(const uint8_t *work);
293void protocore_phase_machine_auth_complete(const uint8_t *work);
294void protocore_phase_machine_admits_rekey(const uint8_t *work);
295void protocore_phase_machine_is_open(const uint8_t *work);
296
297// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
298// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
299// `PhaseMachine.get(work)` resolves to a named function and becomes a DIRECT call. An extern table
300// leaves the call indirect and the symbol live at every level, -O2 -flto included.
301static const PhaseMachineNs PhaseMachine __attribute__((unused)) = {
302 .get = protocore_phase_machine_get,
303 .is = protocore_phase_machine_is,
304 .reset = protocore_phase_machine_reset,
305 .ident_done = protocore_phase_machine_ident_done,
306 .kexinit_done = protocore_phase_machine_kexinit_done,
307 .kex_done = protocore_phase_machine_kex_done,
308 .newkeys_done = protocore_phase_machine_newkeys_done,
309 .service_done = protocore_phase_machine_service_done,
310 .auth_done = protocore_phase_machine_auth_done,
311 .rekey_begin = protocore_phase_machine_rekey_begin,
312 .admits_ident = protocore_phase_machine_admits_ident,
313 .admits_kexinit = protocore_phase_machine_admits_kexinit,
314 .kexinit_needs_reply = protocore_phase_machine_kexinit_needs_reply,
315 .admits_kexdh_init = protocore_phase_machine_admits_kexdh_init,
316 .admits_newkeys = protocore_phase_machine_admits_newkeys,
317 .admits_service_request = protocore_phase_machine_admits_service_request,
318 .admits_userauth = protocore_phase_machine_admits_userauth,
319 .auth_complete = protocore_phase_machine_auth_complete,
320 .admits_rekey = protocore_phase_machine_admits_rekey,
321 .is_open = protocore_phase_machine_is_open,
322};
323
324EMBED_END_DECLS
325
326#endif // PROTOCORE_ENABLE_SSH || PROTOCORE_ENABLE_SSH_CLIENT
327
328#endif // PROTOCORE_TRANSPORT_PHASE_MACHINE_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.