ssh_server.h
Go to the documentation of this file.
1 /**
2  * @file ssh_server.h
3  * @brief SSH server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.6
29  **/
30 
31 #ifndef _SSH_SERVER_H
32 #define _SSH_SERVER_H
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 
37 //Stack size required to run the SSH server
38 #ifndef SSH_SERVER_STACK_SIZE
39  #define SSH_SERVER_STACK_SIZE 750
40 #elif (SSH_SERVER_STACK_SIZE < 1)
41  #error SSH_SERVER_STACK_SIZE parameter is not valid
42 #endif
43 
44 //Priority at which the SSH server should run
45 #ifndef SSH_SERVER_PRIORITY
46  #define SSH_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
47 #endif
48 
49 //Idle connection timeout
50 #ifndef SSH_SERVER_TIMEOUT
51  #define SSH_SERVER_TIMEOUT 60000
52 #elif (SSH_SERVER_TIMEOUT < 1000)
53  #error SSH_SERVER_TIMEOUT parameter is not valid
54 #endif
55 
56 //SSH server tick interval
57 #ifndef SSH_SERVER_TICK_INTERVAL
58  #define SSH_SERVER_TICK_INTERVAL 1000
59 #elif (SSH_SERVER_TICK_INTERVAL < 100)
60  #error SSH_SERVER_TICK_INTERVAL parameter is not valid
61 #endif
62 
63 //Application specific context
64 #ifndef SSH_SERVER_CONTEXT_PRIVATE
65  #define SSH_SERVER_CONTEXT_PRIVATE
66 #endif
67 
68 //C++ guard
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 
74 /**
75  * @brief SSH server settings
76  **/
77 
78 typedef struct
79 {
80  OsTaskParameters task; ///<Task parameters
81  NetContext *netContext; ///<TCP/IP stack context
82  NetInterface *interface; ///<Underlying network interface
83  uint16_t port; ///<SSH port number
84  systime_t timeout; ///<Idle connection timeout
85  uint_t numConnections; ///<Maximum number of SSH connections
86  SshConnection *connections; ///<SSH connections
87  uint_t numChannels; ///<Maximum number of SSH channels
88  SshChannel *channels; ///<SSH channels
89  const PrngAlgo *prngAlgo; ///<Pseudo-random number generator to be used
90  void *prngContext; ///<Pseudo-random number generator context
91 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
92  SshPublicKeyAuthCallback publicKeyAuthCallback; ///<Public key authentication callback
93 #endif
94 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED && SSH_CERT_SUPPORT == ENABLED)
95  SshCertAuthCallback certAuthCallback; ///<Certificate authentication callback
96  SshCaPublicKeyVerifyCallback caPublicKeyVerifyCallback; ///<CA public key verification callback
97 #endif
98 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
99  SshPasswordAuthCallback passwordAuthCallback; ///<Password authentication callback
100  SshPasswordChangeCallback passwordChangeCallback; ///<Password change callback
101 #endif
102 #if (SSH_SIGN_CALLBACK_SUPPORT == ENABLED)
103  SshSignGenCallback signGenCallback; ///<Signature generation callback
104  SshSignVerifyCallback signVerifyCallback; ///<Signature verification callback
105 #endif
106 #if (SSH_ECDH_CALLBACK_SUPPORT == ENABLED)
107  SshEcdhKeyPairGenCallback ecdhKeyPairGenCallback; ///<ECDH key pair generation callback
108  SshEcdhSharedSecretCalcCallback ecdhSharedSecretCalcCallback; ///<ECDH shared secret calculation callback
109 #endif
110 #if (SSH_KEY_LOG_SUPPORT == ENABLED)
111  SshKeyLogCallback keyLogCallback; ///<Key logging callback (for debugging purpose only)
112 #endif
114 
115 
116 /**
117  * @brief SSH server context
118  **/
119 
120 typedef struct
121 {
122  bool_t running; ///<Operational state of the SSH server
123  bool_t stop; ///<Stop request
124  OsTaskParameters taskParams; ///<Task parameters
125  OsTaskId taskId; ///<Task identifier
126  NetContext *netContext; ///<TCP/IP stack context
127  NetInterface *interface; ///<Underlying network interface
128  Socket *socket; ///<Listening socket
129  uint16_t port; ///<SSH port number
130  systime_t timeout; ///<Idle connection timeout
131  SshContext sshContext; ///<SSH context
132  SSH_SERVER_CONTEXT_PRIVATE ///<Application specific context
134 
135 
136 //SSH server related functions
138 
140  const SshServerSettings *settings);
141 
143  SshGlobalReqCallback callback, void *param);
144 
146  SshGlobalReqCallback callback);
147 
149  SshChannelReqCallback callback, void *param);
150 
152  SshChannelReqCallback callback);
153 
155  SshChannelOpenCallback callback, void *param);
156 
158  SshChannelOpenCallback callback);
159 
161  SshConnectionOpenCallback callback, void *param);
162 
164  SshConnectionOpenCallback callback);
165 
167  SshConnectionCloseCallback callback, void *param);
168 
170  SshConnectionCloseCallback callback);
171 
173  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
174  size_t privateKeyLen, const char_t *password);
175 
177 
179  const char_t *dhParams, size_t dhParamsLen);
180 
182 
184  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
185  size_t privateKeyLen, const char_t *password);
186 
188 
190  const char_t *cert, size_t certLen, const char_t *privateKey,
191  size_t privateKeyLen, const char_t *password);
192 
194 
197 
198 void sshServerTask(SshServerContext *context);
199 
200 void sshServerDeinit(SshServerContext *context);
201 
202 //C++ guard
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif
NetInterface * interface
Underlying network interface.
Definition: ssh_server.h:82
error_t(* SshChannelReqCallback)(SshChannel *channel, const SshString *type, const uint8_t *data, size_t length, void *param)
Channel request callback function.
Definition: ssh.h:1339
#define NetContext
Definition: net.h:36
error_t sshServerStart(SshServerContext *context)
Start SSH server.
Definition: ssh_server.c:662
int bool_t
Definition: compiler_port.h:63
error_t(* SshCaPublicKeyVerifyCallback)(SshConnection *connection, const uint8_t *publicKey, size_t publicKeyLen)
CA public key verification callback function.
Definition: ssh.h:1253
void(* SshConnectionCloseCallback)(SshConnection *connection, void *param)
Connection close callback function.
Definition: ssh.h:1364
void(* SshKeyLogCallback)(SshConnection *connection, const char_t *key)
Key logging callback function (for debugging purpose only)
Definition: ssh.h:1372
#define PrngAlgo
Definition: crypto.h:1140
SSH server settings.
Definition: ssh_server.h:79
bool_t stop
Stop request.
Definition: ssh_server.h:123
SshEcdhSharedSecretCalcCallback ecdhSharedSecretCalcCallback
ECDH shared secret calculation callback.
Definition: ssh_server.h:108
SshPublicKeyAuthCallback publicKeyAuthCallback
Public key authentication callback.
Definition: ssh_server.h:92
error_t(* SshCertAuthCallback)(SshConnection *connection, const char_t *user, const SshCertificate *cert)
Certificate authentication callback function.
Definition: ssh.h:1269
error_t sshServerInit(SshServerContext *context, const SshServerSettings *settings)
Initialize SSH server context.
Definition: ssh_server.c:126
SshEcdhKeyPairGenCallback ecdhKeyPairGenCallback
ECDH key pair generation callback.
Definition: ssh_server.h:107
SshSignVerifyCallback signVerifyCallback
Signature verification callback.
Definition: ssh_server.h:104
void sshServerTask(SshServerContext *context)
SSH server task.
Definition: ssh_server.c:810
error_t sshServerRegisterChannelRequestCallback(SshServerContext *context, SshChannelReqCallback callback, void *param)
Register channel request callback function.
Definition: ssh_server.c:376
error_t sshServerUnregisterChannelOpenCallback(SshServerContext *context, SshChannelOpenCallback callback)
Unregister channel open callback function.
Definition: ssh_server.c:424
error_t sshServerRegisterConnectionOpenCallback(SshServerContext *context, SshConnectionOpenCallback callback, void *param)
Register connection open callback function.
Definition: ssh_server.c:440
systime_t timeout
Idle connection timeout.
Definition: ssh_server.h:84
error_t sshServerLoadCertificate(SshServerContext *context, uint_t index, const char_t *cert, size_t certLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load server's certificate.
Definition: ssh_server.c:622
error_t sshServerUnregisterChannelRequestCallback(SshServerContext *context, SshChannelReqCallback callback)
Unregister channel request callback function.
Definition: ssh_server.c:392
void sshServerGetDefaultSettings(SshServerSettings *settings)
Initialize settings with default values.
Definition: ssh_server.c:50
systime_t timeout
Idle connection timeout.
Definition: ssh_server.h:130
#define SshContext
Definition: ssh.h:931
error_t
Error codes.
Definition: error.h:43
error_t sshServerLoadRsaKey(SshServerContext *context, uint_t index, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load transient RSA key (for RSA key exchange)
Definition: ssh_server.c:511
error_t(* SshGlobalReqCallback)(SshConnection *connection, const SshString *name, const uint8_t *data, size_t length, void *param)
Global request callback function.
Definition: ssh.h:1331
bool_t running
Operational state of the SSH server.
Definition: ssh_server.h:122
error_t sshServerUnregisterConnectionCloseCallback(SshServerContext *context, SshConnectionCloseCallback callback)
Unregister connection close callback function.
Definition: ssh_server.c:488
uint_t numConnections
Maximum number of SSH connections.
Definition: ssh_server.h:85
error_t(* SshChannelOpenCallback)(SshConnection *connection, const SshString *type, uint32_t senderChannel, uint32_t initialWindowSize, uint32_t maxPacketSize, const uint8_t *data, size_t length, void *param)
Channel open callback function.
Definition: ssh.h:1347
NetContext * netContext
TCP/IP stack context.
Definition: ssh_server.h:81
#define NetInterface
Definition: net.h:40
void sshServerDeinit(SshServerContext *context)
Release SSH server context.
Definition: ssh_server.c:916
SshCaPublicKeyVerifyCallback caPublicKeyVerifyCallback
CA public key verification callback.
Definition: ssh_server.h:96
SSH server context.
Definition: ssh_server.h:121
SshAuthStatus(* SshPasswordChangeCallback)(SshConnection *connection, const char_t *user, const char_t *oldPassword, size_t oldPasswordLen, const char_t *newPassword, size_t newPasswordLen)
Password change callback function.
Definition: ssh.h:1285
error_t(* SshEcdhKeyPairGenCallback)(SshConnection *connection, const char_t *kexAlgo, EcPublicKey *publicKey)
ECDH key pair generation callback.
Definition: ssh.h:1314
OsTaskParameters task
Task parameters.
Definition: ssh_server.h:80
error_t(* SshEcdhSharedSecretCalcCallback)(SshConnection *connection, const char_t *kexAlgo, const EcPublicKey *publicKey, uint8_t *output, size_t *outputLen)
ECDH shared secret calculation callback.
Definition: ssh.h:1322
Task parameters.
void * prngContext
Pseudo-random number generator context.
Definition: ssh_server.h:90
error_t sshServerUnloadHostKey(SshServerContext *context, uint_t index)
Unload server's host key.
Definition: ssh_server.c:600
error_t sshServerUnloadCertificate(SshServerContext *context, uint_t index)
Unload server's certificate.
Definition: ssh_server.c:644
error_t(* SshConnectionOpenCallback)(SshConnection *connection, void *param)
Connection open callback function.
Definition: ssh.h:1356
error_t(* SshSignVerifyCallback)(SshConnection *connection, const SshString *publicKeyAlgo, const SshBinaryString *publicKeyBlob, const SshBinaryString *sessionId, const SshBinaryString *message, const SshBinaryString *signatureBlob)
Signature verification callback function.
Definition: ssh.h:1304
SshCertAuthCallback certAuthCallback
Certificate authentication callback.
Definition: ssh_server.h:95
error_t sshServerRegisterChannelOpenCallback(SshServerContext *context, SshChannelOpenCallback callback, void *param)
Register channel open callback function.
Definition: ssh_server.c:408
uint32_t systime_t
System time.
SshKeyLogCallback keyLogCallback
Key logging callback (for debugging purpose only)
Definition: ssh_server.h:111
uint16_t port
SSH port number.
Definition: ssh_server.h:83
char char_t
Definition: compiler_port.h:55
error_t sshServerLoadDhGexGroup(SshServerContext *context, uint_t index, const char_t *dhParams, size_t dhParamsLen)
Load Diffie-Hellman group.
Definition: ssh_server.c:545
OsTaskId taskId
Task identifier.
Definition: ssh_server.h:125
SshPasswordChangeCallback passwordChangeCallback
Password change callback.
Definition: ssh_server.h:100
error_t sshServerUnloadDhGexGroup(SshServerContext *context, uint_t index)
Unload Diffie-Hellman group.
Definition: ssh_server.c:561
error_t(* SshPublicKeyAuthCallback)(SshConnection *connection, const char_t *user, const uint8_t *publicKey, size_t publicKeyLen)
Public key authentication callback function.
Definition: ssh.h:1261
error_t sshServerRegisterConnectionCloseCallback(SshServerContext *context, SshConnectionCloseCallback callback, void *param)
Register connection close callback function.
Definition: ssh_server.c:472
#define SshConnection
Definition: ssh.h:935
error_t sshServerRegisterGlobalRequestCallback(SshServerContext *context, SshGlobalReqCallback callback, void *param)
Register global request callback function.
Definition: ssh_server.c:344
#define Socket
Definition: socket.h:36
SshConnection * connections
SSH connections.
Definition: ssh_server.h:86
SshAuthStatus(* SshPasswordAuthCallback)(SshConnection *connection, const char_t *user, const char_t *password, size_t passwordLen)
Password authentication callback function.
Definition: ssh.h:1277
error_t sshServerUnloadRsaKey(SshServerContext *context, uint_t index)
Unload transient RSA key (for RSA key exchange)
Definition: ssh_server.c:528
SshContext sshContext
SSH context.
Definition: ssh_server.h:131
SshPasswordAuthCallback passwordAuthCallback
Password authentication callback.
Definition: ssh_server.h:99
error_t sshServerUnregisterGlobalRequestCallback(SshServerContext *context, SshGlobalReqCallback callback)
Unregister global request callback function.
Definition: ssh_server.c:360
NetInterface * interface
Underlying network interface.
Definition: ssh_server.h:127
uint16_t port
SSH port number.
Definition: ssh_server.h:129
thread_t * OsTaskId
Task identifier.
error_t sshServerStop(SshServerContext *context)
Stop SSH server.
Definition: ssh_server.c:757
error_t sshServerUnregisterConnectionOpenCallback(SshServerContext *context, SshConnectionOpenCallback callback)
Unregister connection open callback function.
Definition: ssh_server.c:456
uint_t numChannels
Maximum number of SSH channels.
Definition: ssh_server.h:87
SshSignGenCallback signGenCallback
Signature generation callback.
Definition: ssh_server.h:103
unsigned int uint_t
Definition: compiler_port.h:57
error_t(* SshSignGenCallback)(SshConnection *connection, const char_t *publicKeyAlgo, const SshHostKey *hostKey, const SshBinaryString *sessionId, const SshBinaryString *message, uint8_t *p, size_t *written)
Signature generation callback function.
Definition: ssh.h:1294
Secure Shell (SSH)
NetContext * netContext
TCP/IP stack context.
Definition: ssh_server.h:126
const PrngAlgo * prngAlgo
Pseudo-random number generator to be used.
Definition: ssh_server.h:89
SshChannel * channels
SSH channels.
Definition: ssh_server.h:88
OsTaskParameters taskParams
Task parameters.
Definition: ssh_server.h:124
Socket * socket
Listening socket.
Definition: ssh_server.h:128
#define SSH_SERVER_CONTEXT_PRIVATE
Definition: ssh_server.h:65
error_t sshServerLoadHostKey(SshServerContext *context, uint_t index, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load server's host key.
Definition: ssh_server.c:583
#define SshChannel
Definition: ssh.h:939