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.0
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 //C++ guard
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
69 /**
70  * @brief SSH server settings
71  **/
72 
73 typedef struct
74 {
75  OsTaskParameters task; ///<Task parameters
76  NetContext *netContext; ///<TCP/IP stack context
77  NetInterface *interface; ///<Underlying network interface
78  uint16_t port; ///<SSH port number
79  systime_t timeout; ///<Idle connection timeout
80  uint_t numConnections; ///<Maximum number of SSH connections
81  SshConnection *connections; ///<SSH connections
82  uint_t numChannels; ///<Maximum number of SSH channels
83  SshChannel *channels; ///<SSH channels
84  const PrngAlgo *prngAlgo; ///<Pseudo-random number generator to be used
85  void *prngContext; ///<Pseudo-random number generator context
86 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
87  SshPublicKeyAuthCallback publicKeyAuthCallback; ///<Public key authentication callback
88 #endif
89 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED && SSH_CERT_SUPPORT == ENABLED)
90  SshCertAuthCallback certAuthCallback; ///<Certificate authentication callback
91  SshCaPublicKeyVerifyCallback caPublicKeyVerifyCallback; ///<CA public key verification callback
92 #endif
93 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
94  SshPasswordAuthCallback passwordAuthCallback; ///<Password authentication callback
95  SshPasswordChangeCallback passwordChangeCallback; ///<Password change callback
96 #endif
97 #if (SSH_SIGN_CALLBACK_SUPPORT == ENABLED)
98  SshSignGenCallback signGenCallback; ///<Signature generation callback
99  SshSignVerifyCallback signVerifyCallback; ///<Signature verification callback
100 #endif
101 #if (SSH_ECDH_CALLBACK_SUPPORT == ENABLED)
102  SshEcdhKeyPairGenCallback ecdhKeyPairGenCallback; ///<ECDH key pair generation callback
103  SshEcdhSharedSecretCalcCallback ecdhSharedSecretCalcCallback; ///<ECDH shared secret calculation callback
104 #endif
105 #if (SSH_KEY_LOG_SUPPORT == ENABLED)
106  SshKeyLogCallback keyLogCallback; ///<Key logging callback (for debugging purpose only)
107 #endif
109 
110 
111 /**
112  * @brief SSH server context
113  **/
114 
115 typedef struct
116 {
117  bool_t running; ///<Operational state of the SSH server
118  bool_t stop; ///<Stop request
119  OsTaskParameters taskParams; ///<Task parameters
120  OsTaskId taskId; ///<Task identifier
121  NetContext *netContext; ///<TCP/IP stack context
122  NetInterface *interface; ///<Underlying network interface
123  Socket *socket; ///<Listening socket
124  uint16_t port; ///<SSH port number
125  systime_t timeout; ///<Idle connection timeout
126  SshContext sshContext; ///<SSH context
128 
129 
130 //SSH server related functions
132 
134  const SshServerSettings *settings);
135 
137  SshGlobalReqCallback callback, void *param);
138 
140  SshGlobalReqCallback callback);
141 
143  SshChannelReqCallback callback, void *param);
144 
146  SshChannelReqCallback callback);
147 
149  SshChannelOpenCallback callback, void *param);
150 
152  SshChannelOpenCallback callback);
153 
155  SshConnectionOpenCallback callback, void *param);
156 
158  SshConnectionOpenCallback callback);
159 
161  SshConnectionCloseCallback callback, void *param);
162 
164  SshConnectionCloseCallback callback);
165 
167  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
168  size_t privateKeyLen, const char_t *password);
169 
171 
173  const char_t *dhParams, size_t dhParamsLen);
174 
176 
178  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
179  size_t privateKeyLen, const char_t *password);
180 
182 
184  const char_t *cert, size_t certLen, const char_t *privateKey,
185  size_t privateKeyLen, const char_t *password);
186 
188 
191 
192 void sshServerTask(SshServerContext *context);
193 
194 void sshServerDeinit(SshServerContext *context);
195 
196 //C++ guard
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif
NetInterface * interface
Underlying network interface.
Definition: ssh_server.h:77
error_t(* SshChannelReqCallback)(SshChannel *channel, const SshString *type, const uint8_t *data, size_t length, void *param)
Channel request callback function.
Definition: ssh.h:1300
#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:1214
void(* SshConnectionCloseCallback)(SshConnection *connection, void *param)
Connection close callback function.
Definition: ssh.h:1325
void(* SshKeyLogCallback)(SshConnection *connection, const char_t *key)
Key logging callback function (for debugging purpose only)
Definition: ssh.h:1333
#define PrngAlgo
Definition: crypto.h:1035
SSH server settings.
Definition: ssh_server.h:74
bool_t stop
Stop request.
Definition: ssh_server.h:118
SshEcdhSharedSecretCalcCallback ecdhSharedSecretCalcCallback
ECDH shared secret calculation callback.
Definition: ssh_server.h:103
SshPublicKeyAuthCallback publicKeyAuthCallback
Public key authentication callback.
Definition: ssh_server.h:87
error_t(* SshCertAuthCallback)(SshConnection *connection, const char_t *user, const SshCertificate *cert)
Certificate authentication callback function.
Definition: ssh.h:1230
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:102
SshSignVerifyCallback signVerifyCallback
Signature verification callback.
Definition: ssh_server.h:99
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:79
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:125
#define SshContext
Definition: ssh.h:892
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:1292
bool_t running
Operational state of the SSH server.
Definition: ssh_server.h:117
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:80
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:1308
NetContext * netContext
TCP/IP stack context.
Definition: ssh_server.h:76
#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:91
SSH server context.
Definition: ssh_server.h:116
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:1246
error_t(* SshEcdhKeyPairGenCallback)(SshConnection *connection, const char_t *kexAlgo, EcPublicKey *publicKey)
ECDH key pair generation callback.
Definition: ssh.h:1275
OsTaskParameters task
Task parameters.
Definition: ssh_server.h:75
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:1283
Task parameters.
void * prngContext
Pseudo-random number generator context.
Definition: ssh_server.h:85
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:1317
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:1265
SshCertAuthCallback certAuthCallback
Certificate authentication callback.
Definition: ssh_server.h:90
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:106
uint16_t port
SSH port number.
Definition: ssh_server.h:78
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:120
SshPasswordChangeCallback passwordChangeCallback
Password change callback.
Definition: ssh_server.h:95
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:1222
error_t sshServerRegisterConnectionCloseCallback(SshServerContext *context, SshConnectionCloseCallback callback, void *param)
Register connection close callback function.
Definition: ssh_server.c:472
#define SshConnection
Definition: ssh.h:896
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:81
SshAuthStatus(* SshPasswordAuthCallback)(SshConnection *connection, const char_t *user, const char_t *password, size_t passwordLen)
Password authentication callback function.
Definition: ssh.h:1238
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:126
SshPasswordAuthCallback passwordAuthCallback
Password authentication callback.
Definition: ssh_server.h:94
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:122
uint16_t port
SSH port number.
Definition: ssh_server.h:124
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:82
SshSignGenCallback signGenCallback
Signature generation callback.
Definition: ssh_server.h:98
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:1255
Secure Shell (SSH)
NetContext * netContext
TCP/IP stack context.
Definition: ssh_server.h:121
const PrngAlgo * prngAlgo
Pseudo-random number generator to be used.
Definition: ssh_server.h:84
SshChannel * channels
SSH channels.
Definition: ssh_server.h:83
OsTaskParameters taskParams
Task parameters.
Definition: ssh_server.h:119
Socket * socket
Listening socket.
Definition: ssh_server.h:123
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:900