ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
auth.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 auth.h
6 * @brief RFC 4252 user authentication.
7 *
8 * After NEWKEYS the client requests the "ssh-userauth" service. The server accepts it and then
9 * drives SSH_MSG_USERAUTH_REQUEST exchanges until a method succeeds or the connection is dropped.
10 * The "none" method is always answered with a failure that advertises what may continue
11 * (RFC 4252 sec 5.2), which is how a client discovers the supported methods.
12 */
13
14#ifndef PROTOCORE_AUTH_AUTH_H
15#define PROTOCORE_AUTH_AUTH_H
16
18#include "spatium/spatium.h"
19
20EMBED_BEGIN_DECLS
21
22/** @brief Parsed SSH_MSG_USERAUTH_REQUEST. */
23typedef struct
24{
25 char user[SSH_AUTH_USER_MAX]; ///< User name, null-terminated.
26 char service[32]; ///< Requested service ("ssh-connection").
27 char method[24]; ///< Method name ("none", "password", "publickey", "keyboard-interactive").
28 char password[SSH_AUTH_PASS_MAX]; ///< Password (method == "password"); the old one on a change.
29 char new_password[SSH_AUTH_PASS_MAX]; ///< New password when is_pw_change (RFC 4252 sec 8).
30 embed_bool is_password; ///< True if a password method-request was parsed.
31 embed_bool is_pw_change; ///< True if the request set the change-password flag.
32 embed_bool is_kbdint; ///< True if a keyboard-interactive method-request was parsed (RFC 4256).
33
34 // publickey method (RFC 4252 §7)
35 embed_bool is_pubkey; ///< True if a publickey method-request was parsed.
36 embed_bool has_signature; ///< True if the request carried a signature.
37 char pk_algo[SSH_AUTH_ALGO_MAX]; ///< Public-key algorithm name.
38 const uint8_t *pk_blob; ///< Public-key blob (points into the payload).
39 uint32_t pk_blob_len; ///< Length of pk_blob.
40 const uint8_t *signature; ///< Raw signature bytes (points into the payload).
41 uint32_t signature_len; ///< Length of signature.
42 const uint8_t *signed_prefix; ///< Bytes of the request that the signature covers.
43 size_t signed_prefix_len; ///< Length of signed_prefix (payload up to the signature).
45
46/**
47 * @brief Application callback that validates a username/password pair.
48 * @return true to accept the credentials.
49 */
50typedef embed_bool (*SshPasswordCb)(const char *user, const char *password);
51
52/** @brief Install the password-verification callback (nullptr → all fail). */
53
54/**
55 * @brief Application callback that STARTS a password change (RFC 4252 sec 8) for slot @p slot.
56 *
57 * The application owns the store and its encryption, and a store can be slow (flash). This callback
58 * must not block: it copies what it needs, kicks off its own work, and returns. When the change has
59 * finished (or failed to verify @p old_password) the application reports the outcome with
60 * protocore_ssh_auth_pw_change_report(); the reply to the client is deferred until then, so the SSH worker
61 * is never held on the store. @p old_password / @p new_password are wiped once this returns.
62 */
63typedef void (*SshPasswordChangeCb)(uint8_t slot, const char *user, const char *old_password, const char *new_password);
64
65/** @brief A slot's password-change state: idle, handed to the application, or finished either way. */
73
74/** @brief Install the password-change start callback (nullptr → change requests are refused busy). */
75
76/**
77 * @brief Report the outcome of the change the start callback began for slot @p slot.
78 *
79 * @p ok true when the old password verified and the new one was stored. Moves the slot to OK or
80 * FAIL, which the next poll drains into the deferred reply. A no-op when no change is in flight
81 * (a stale report after the connection left).
82 */
83
84/**
85 * @brief Take slot @p i's finished change outcome, clearing it back to NONE.
86 *
87 * @return OK or FAIL once the application reported, NONE while idle or still in flight.
88 */
89/**
90 * @brief Write the "publickey" USERAUTH_REQUEST body that RFC 4252 sec 7 signs and sends.
91 *
92 * sec 7 gives one field order and uses it twice: the signature covers
93 *
94 * string session identifier
95 * byte SSH_MSG_USERAUTH_REQUEST
96 * string user name
97 * string service name
98 * string "publickey"
99 * boolean TRUE
100 * string public key algorithm name
101 * string public key to be used for authentication
102 *
103 * and the request that carries the signature is the same bytes from SSH_MSG_USERAUTH_REQUEST on,
104 * with the signature appended. Writing it in one place is what keeps the two identical - a
105 * verifier hashes what arrived, so any divergence here is a signature that never validates.
106 *
107 * @param w Span written into.
108 * @param sid Session identifier, or null to start at SSH_MSG_USERAUTH_REQUEST (the request form).
109 * @param sid_len Length of @p sid; ignored when @p sid is null.
110 * @param user User name.
111 * @param service Service name, normally "ssh-connection".
112 * @param pk_algo Public key algorithm name.
113 * @param pk_blob Public key blob.
114 * @param pk_len Length of @p pk_blob.
115 */
116
117/**
118 * @brief True once slot @p i has gone too long without authenticating (RFC 4252 sec 4).
119 *
120 * "The server SHOULD have a timeout for authentication and disconnect if the authentication has not
121 * been accepted within the timeout period. The RECOMMENDED timeout period is 10 minutes." The clock
122 * starts on the first call for a slot and stops once authentication completes; SSH_AUTH_TIMEOUT_MS
123 * of 0 disables it. Poll it, and disconnect when it answers true.
124 */
125
127
128/**
129 * @brief Drop a parked password change without replying to it.
130 *
131 * RFC 4252 sec 5.1 sends SSH_MSG_USERAUTH_SUCCESS once, so a change whose answer arrives after some
132 * other method already succeeded is discarded rather than answered.
133 */
134
135/**
136 * @brief Drop slot @p i's half-finished authentication state and wipe its username.
137 *
138 * A keyboard-interactive exchange is armed by the USERAUTH_REQUEST and consumed by the matching
139 * INFO_RESPONSE. A connection that leaves between the two ends here.
140 */
141
142/**
143 * @brief Application callback that decides whether a public key is authorized
144 * for @p user. @p blob is the "ssh-rsa" public-key blob.
145 * @return true if the key may authenticate this user.
146 */
147typedef embed_bool (*SshPubkeyCb)(const char *user, const uint8_t *blob, size_t blob_len);
148
149/** @brief Install the publickey-authorization callback (nullptr → all fail). */
150
151/**
152 * @brief Parse an SSH_MSG_USERAUTH_REQUEST into @p req.
153 * @return 0 on success, -1 if malformed.
154 */
155
156/** @brief Build SSH_MSG_USERAUTH_FAILURE advertising "password". */
157
158/** @brief Build SSH_MSG_USERAUTH_SUCCESS. */
159
160/**
161 * @brief Handle a USERAUTH_REQUEST end-to-end for slot @p i.
162 *
163 * Parses the request, checks "password" credentials via the installed callback,
164 * and writes either USERAUTH_SUCCESS or USERAUTH_FAILURE to @p out. On success
165 * the session is marked authenticated and advanced to the connection phase.
166 *
167 * @return 0 if a response was produced (check the message type), -1 on parse
168 * error.
169 */
170
171#if PROTOCORE_ENABLE_SSH_KEYBOARD_INTERACTIVE
172/**
173 * @brief Handle an SSH_MSG_USERAUTH_INFO_RESPONSE (RFC 4256 §3.4) for slot @p i.
174 *
175 * The response to the single "Password:" prompt this server sends is verified through the installed
176 * password callback (keyboard-interactive is the challenge-response face of password auth here). Writes
177 * USERAUTH_SUCCESS or USERAUTH_FAILURE to @p out. Only valid while a keyboard-interactive exchange is
178 * pending for the slot (a prior USERAUTH_REQUEST selected it); otherwise fails.
179 *
180 * @return 0 if a response was produced (check the message type), -1 on parse error / no exchange pending.
181 */
182#endif
183
184/** @brief Dispatch messages 50 to 79; 80 and above need authentication (RFC 4252 sec 6). */
185
186/** @brief Send the reply a finished password change (RFC 4252 sec 8) deferred on slot @p i. */
187
188/** @brief RFC 4252 sec 5: the body of one userauth message. */
189typedef struct
190{
191 const uint8_t *payload; ///< the message body
192 size_t len; ///< how many bytes it has
194
195/** @brief Where a reply is written, either as a buffer or as a span. */
196typedef struct
197{
198 uint8_t *out; ///< where a reply is written
199 size_t out_len; ///< what was written
200 size_t cap; ///< how much room it has
201 mmgr_span *w; ///< the span a publickey request is built in
203
204/** @brief RFC 4252 sec 5 / sec 7: the names one USERAUTH_REQUEST carries, and the key it offers. */
205typedef struct
206{
207 const uint8_t *sid; ///< the session identifier it signs over, or NULL for the request form
208 size_t sid_len; ///< its length; ignored when sid is NULL
209 const char *user; ///< the user name
210 const char *service; ///< the service name, normally "ssh-connection"
211 const char *pk_algo; ///< the public key algorithm name
212 const uint8_t *pk_blob; ///< the public key blob
213 size_t pk_len; ///< its length
215
216/** @brief RFC 4252 sec 7 / sec 8: what an attempt of each method is checked against. */
217typedef struct
218{
219 SshPasswordCb password_cb; ///< what a password attempt is checked against
220 SshPasswordChangeCb password_change_cb; ///< what a change request is handed to
221 SshPubkeyCb pubkey_cb; ///< what a public key is checked against
222} SshAuthCbs;
223
224/**
225 * @brief The SSH authentication protocol (RFC 4252): what a slot must satisfy before a service runs.
226 *
227 * A caller sets the members a call takes, invokes it through ::SshAuth, and reads the outcome off
228 * the same handle.
229 *
230 * @var SshAuthNs::slot the SSH slot a call acts on
231 * @var SshAuthNs::msg_type the message a dispatch routes (sec 6: 50-79 here, 80+ need auth)
232 * @var SshAuthNs::msg sec 5 the message body a dispatch is given
233 * @var SshAuthNs::out_args where a reply is written
234 * @var SshAuthNs::req where a parse lands the request (sec 5)
235 * @var SshAuthNs::partial the failure is a partial success (sec 5.1)
236 * @var SshAuthNs::userauth sec 5 / sec 7 the fields one request names
237 * @var SshAuthNs::cbs what an attempt of each method is checked against
238 * @var SshAuthNs::ok a call's true/false outcome
239 * @var SshAuthNs::i32 a call's signed outcome
240 * @var SshAuthNs::set_password_cb install the password check
241 * @var SshAuthNs::set_password_change_cb install the change handler
242 * @var SshAuthNs::set_pubkey_cb install the public key check
243 * @var SshAuthNs::pw_change_report report a finished change back to the slot
244 * @var SshAuthNs::pw_change_clear drop a pending change on the slot
245 * @var SshAuthNs::passwd_change_reply send the reply a finished change deferred
246 * @var SshAuthNs::write_publickey_request build a publickey request into out_args.w (sec 7)
247 * @var SshAuthNs::timed_out the slot has gone too long unauthenticated (sec 4)
248 * @var SshAuthNs::reset clear the slot's authentication state
249 * @var SshAuthNs::parse_request parse a USERAUTH_REQUEST into req (sec 5)
250 * @var SshAuthNs::build_failure write USERAUTH_FAILURE (sec 5.1)
251 * @var SshAuthNs::build_success write USERAUTH_SUCCESS (sec 5.1)
252 * @var SshAuthNs::handle_request answer a USERAUTH_REQUEST
253 * @var SshAuthNs::handle_info_response answer a USERAUTH_INFO_RESPONSE (RFC 4256 sec 3.4)
254 * @var SshAuthNs::dispatch route messages 50 to 79
255 */
256typedef struct
257{
258 uint8_t slot; ///< the SSH slot a call acts on
259 uint8_t msg_type; ///< the message a dispatch routes
260 SshAuthReq *req; ///< where a parse lands the request (sec 5)
261 embed_bool partial; ///< the failure is a partial success (sec 5.1)
262 SshAuthMsgArgs msg; ///< sec 5 the message body a dispatch is given
263 SshAuthOutArgs out_args; ///< where a reply is written
264 SshUserauthArgs userauth; ///< sec 5 / sec 7 the fields one request names
265 SshAuthCbs cbs; ///< what an attempt is checked against
266 embed_bool ok;
267 int i32;
268#if PROTOCORE_ENABLE_SSH_KEYBOARD_INTERACTIVE
269#endif
271
272/** @brief The operands and the outcome. */
273extern SshAuthVars SshAuthV;
274
275/** @brief The entries. */
276typedef struct
277{
278 void (*const set_password_cb)(const uint8_t *work);
279 void (*const set_password_change_cb)(const uint8_t *work);
280 void (*const set_pubkey_cb)(const uint8_t *work);
281 void (*const pw_change_report)(const uint8_t *work);
282 void (*const pw_change_clear)(const uint8_t *work);
283 void (*const passwd_change_reply)(const uint8_t *work);
284 void (*const write_publickey_request)(const uint8_t *work);
285 void (*const timed_out)(const uint8_t *work);
286 void (*const reset)(const uint8_t *work);
287 void (*const parse_request)(const uint8_t *work);
288 void (*const build_failure)(const uint8_t *work);
289 void (*const build_success)(const uint8_t *work);
290 void (*const handle_request)(const uint8_t *work);
291 void (*const handle_info_response)(uint8_t *work);
292 void (*const dispatch)(uint8_t *work);
293} SshAuthNs;
294
295// What the table binds, defined once in the .c and taking one parameter each: everything
296// else an entry needs is an operand in SshAuthV or a region of the borrow at a fixed offset.
297void protocore_ssh_auth_set_password_cb(const uint8_t *work);
299void protocore_ssh_auth_set_pubkey_cb(const uint8_t *work);
300void protocore_ssh_auth_pw_change_report(const uint8_t *work);
301void protocore_ssh_auth_pw_change_clear(const uint8_t *work);
304void protocore_ssh_auth_timed_out(const uint8_t *work);
305void protocore_ssh_auth_reset(const uint8_t *work);
306void protocore_ssh_auth_parse_request(const uint8_t *work);
307void protocore_ssh_auth_build_failure(const uint8_t *work);
308void protocore_ssh_auth_build_success(const uint8_t *work);
309void protocore_ssh_auth_handle_request(const uint8_t *work);
310#if PROTOCORE_ENABLE_SSH_KEYBOARD_INTERACTIVE
311void protocore_ssh_auth_handle_info_response(uint8_t *work);
312#endif
313void protocore_ssh_auth_dispatch(uint8_t *work);
314
315// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
316// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
317// `SshAuth.set_password_cb(work)` resolves to a named function and becomes a DIRECT call. An extern table
318// leaves the call indirect and the symbol live at every level, -O2 -flto included.
319static const SshAuthNs SshAuth __attribute__((unused)) = {
320 .set_password_cb = protocore_ssh_auth_set_password_cb,
321 .set_password_change_cb = protocore_ssh_auth_set_password_change_cb,
322 .set_pubkey_cb = protocore_ssh_auth_set_pubkey_cb,
323 .pw_change_report = protocore_ssh_auth_pw_change_report,
324 .pw_change_clear = protocore_ssh_auth_pw_change_clear,
325 .passwd_change_reply = protocore_ssh_auth_passwd_change_reply,
326 .write_publickey_request = protocore_ssh_auth_write_publickey_request,
327 .timed_out = protocore_ssh_auth_timed_out,
329 .parse_request = protocore_ssh_auth_parse_request,
330 .build_failure = protocore_ssh_auth_build_failure,
331 .build_success = protocore_ssh_auth_build_success,
332 .handle_request = protocore_ssh_auth_handle_request,
333#if PROTOCORE_ENABLE_SSH_KEYBOARD_INTERACTIVE
334 .handle_info_response = protocore_ssh_auth_handle_info_response,
335#endif
336 .dispatch = protocore_ssh_auth_dispatch,
337};
338
339/**
340 * @brief The PROTOCORE_SSH_AUTH_BORROW bytes this module's state lives in.
341 *
342 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
343 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
344 * walks, so the state lasts the life of the program.
345 *
346 * @return the span.
347 */
349
350EMBED_END_DECLS
351
352#endif // PROTOCORE_AUTH_AUTH_H
#define SSH_AUTH_PASS_MAX
Max stored password length.
#define SSH_AUTH_ALGO_MAX
Max stored public-key algorithm name ("rsa-sha2-512", "ecdsa-sha2-nistp256", RFC 4253 sec 6....
#define SSH_AUTH_USER_MAX
Max stored user name (RFC 4252 imposes no limit; we cap for BSS).
Root infrastructure: fixed widths, serializers, opcodes and sizes, for every layer above.
embed_bool(* SshPubkeyCb)(const char *user, const uint8_t *blob, size_t blob_len)
Drop a parked password change without replying to it.
Definition auth.h:147
SshPwChange
A slot's password-change state: idle, handed to the application, or finished either way.
Definition auth.h:67
@ PROTOCORE_SSH_PW_CHANGE_NONE
Definition auth.h:68
@ PROTOCORE_SSH_PW_CHANGE_FAIL
Definition auth.h:71
@ PROTOCORE_SSH_PW_CHANGE_BUSY
Definition auth.h:69
@ PROTOCORE_SSH_PW_CHANGE_OK
Definition auth.h:70
void protocore_ssh_auth_build_success(const uint8_t *work)
void protocore_ssh_auth_reset(const uint8_t *work)
void protocore_ssh_auth_pw_change_clear(const uint8_t *work)
SshPwChange protocore_ssh_auth_pw_change_take(uint8_t i)
Install the password-change start callback (nullptr → change requests are refused busy).
void protocore_ssh_auth_passwd_change_reply(const uint8_t *work)
void protocore_ssh_auth_set_password_cb(const uint8_t *work)
void protocore_ssh_auth_write_publickey_request(const uint8_t *work)
void protocore_ssh_auth_set_password_change_cb(const uint8_t *work)
void protocore_ssh_auth_timed_out(const uint8_t *work)
void protocore_ssh_auth_parse_request(const uint8_t *work)
uint8_t * protocore_ssh_auth_span(void)
The PROTOCORE_SSH_AUTH_BORROW bytes this module's state lives in.
embed_bool(* SshPasswordCb)(const char *user, const char *password)
Application callback that validates a username/password pair.
Definition auth.h:50
void protocore_ssh_auth_pw_change_report(const uint8_t *work)
SshAuthVars SshAuthV
The operands and the outcome.
void(* SshPasswordChangeCb)(uint8_t slot, const char *user, const char *old_password, const char *new_password)
Install the password-verification callback (nullptr → all fail).
Definition auth.h:63
void protocore_ssh_auth_build_failure(const uint8_t *work)
void protocore_ssh_auth_set_pubkey_cb(const uint8_t *work)
void protocore_ssh_auth_dispatch(uint8_t *work)
void protocore_ssh_auth_handle_request(const uint8_t *work)
RFC 4252 sec 7 / sec 8: what an attempt of each method is checked against.
Definition auth.h:218
SshPubkeyCb pubkey_cb
what a public key is checked against
Definition auth.h:221
SshPasswordCb password_cb
what a password attempt is checked against
Definition auth.h:219
SshPasswordChangeCb password_change_cb
what a change request is handed to
Definition auth.h:220
Install the publickey-authorization callback (nullptr → all fail).
Definition auth.h:190
size_t len
how many bytes it has
Definition auth.h:192
const uint8_t * payload
the message body
Definition auth.h:191
The entries.
Definition auth.h:277
void(*const reset)(const uint8_t *work)
Definition auth.h:286
void(*const build_success)(const uint8_t *work)
Definition auth.h:289
void(*const pw_change_clear)(const uint8_t *work)
Definition auth.h:282
void(*const passwd_change_reply)(const uint8_t *work)
Definition auth.h:283
void(*const dispatch)(uint8_t *work)
Definition auth.h:292
void(*const write_publickey_request)(const uint8_t *work)
Definition auth.h:284
void(*const set_password_cb)(const uint8_t *work)
Definition auth.h:278
void(*const handle_info_response)(uint8_t *work)
Definition auth.h:291
void(*const set_pubkey_cb)(const uint8_t *work)
Definition auth.h:280
void(*const set_password_change_cb)(const uint8_t *work)
Definition auth.h:279
void(*const build_failure)(const uint8_t *work)
Definition auth.h:288
void(*const pw_change_report)(const uint8_t *work)
Definition auth.h:281
void(*const handle_request)(const uint8_t *work)
Definition auth.h:290
void(*const timed_out)(const uint8_t *work)
Definition auth.h:285
void(*const parse_request)(const uint8_t *work)
Definition auth.h:287
Where a reply is written, either as a buffer or as a span.
Definition auth.h:197
size_t out_len
what was written
Definition auth.h:199
size_t cap
how much room it has
Definition auth.h:200
uint8_t * out
where a reply is written
Definition auth.h:198
mmgr_span * w
the span a publickey request is built in
Definition auth.h:201
Parsed SSH_MSG_USERAUTH_REQUEST.
Definition auth.h:24
char method[24]
Method name ("none", "password", "publickey", "keyboard-interactive").
Definition auth.h:27
const uint8_t * signature
Raw signature bytes (points into the payload).
Definition auth.h:40
embed_bool is_pubkey
True if a publickey method-request was parsed.
Definition auth.h:35
char password[SSH_AUTH_PASS_MAX]
Password (method == "password"); the old one on a change.
Definition auth.h:28
char user[SSH_AUTH_USER_MAX]
User name, null-terminated.
Definition auth.h:25
uint32_t pk_blob_len
Length of pk_blob.
Definition auth.h:39
char service[32]
Requested service ("ssh-connection").
Definition auth.h:26
size_t signed_prefix_len
Length of signed_prefix (payload up to the signature).
Definition auth.h:43
embed_bool is_kbdint
True if a keyboard-interactive method-request was parsed (RFC 4256).
Definition auth.h:32
const uint8_t * pk_blob
Public-key blob (points into the payload).
Definition auth.h:38
const uint8_t * signed_prefix
Bytes of the request that the signature covers.
Definition auth.h:42
uint32_t signature_len
Length of signature.
Definition auth.h:41
embed_bool is_password
True if a password method-request was parsed.
Definition auth.h:30
char new_password[SSH_AUTH_PASS_MAX]
New password when is_pw_change (RFC 4252 sec 8).
Definition auth.h:29
embed_bool is_pw_change
True if the request set the change-password flag.
Definition auth.h:31
char pk_algo[SSH_AUTH_ALGO_MAX]
Public-key algorithm name.
Definition auth.h:37
embed_bool has_signature
True if the request carried a signature.
Definition auth.h:36
SshAuthMsgArgs msg
sec 5 the message body a dispatch is given
Definition auth.h:262
int i32
Definition auth.h:267
embed_bool ok
Definition auth.h:266
uint8_t slot
the SSH slot a call acts on
Definition auth.h:258
SshAuthReq * req
where a parse lands the request (sec 5)
Definition auth.h:260
uint8_t msg_type
the message a dispatch routes
Definition auth.h:259
embed_bool partial
the failure is a partial success (sec 5.1)
Definition auth.h:261
SshAuthOutArgs out_args
where a reply is written
Definition auth.h:263
SshAuthCbs cbs
what an attempt is checked against
Definition auth.h:265
SshUserauthArgs userauth
sec 5 / sec 7 the fields one request names
Definition auth.h:264
RFC 4252 sec 5 / sec 7: the names one USERAUTH_REQUEST carries, and the key it offers.
Definition auth.h:206
const char * user
the user name
Definition auth.h:209
const char * service
the service name, normally "ssh-connection"
Definition auth.h:210
size_t pk_len
its length
Definition auth.h:213
size_t sid_len
its length; ignored when sid is NULL
Definition auth.h:208
const uint8_t * pk_blob
the public key blob
Definition auth.h:212
const char * pk_algo
the public key algorithm name
Definition auth.h:211
const uint8_t * sid
the session identifier it signs over, or NULL for the request form
Definition auth.h:207