blob: f1eb21f3d9fa88d296044baa4ac55f79aa427746 [file] [log] [blame]
Paul Bakker896ac222011-05-20 12:33:05 +00001/*
Paul Bakkercb79ae0b2011-05-20 12:44:16 +00002 * SSL server demonstration program using fork() for handling multiple clients
Paul Bakker896ac222011-05-20 12:33:05 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker896ac222011-05-20 12:33:05 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker896ac222011-05-20 12:33:05 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Gilles Peskinec83e56c2024-09-04 17:47:14 +020014#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
15 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_SRV_C) || \
16 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
17int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000018{
Gilles Peskinec83e56c2024-09-04 17:47:14 +020019 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
20 "MBEDTLS_NET_C and/or MBEDTLS_SSL_SRV_C and/or "
21 "MBEDTLS_PEM_PARSE_C and/or MBEDTLS_X509_CRT_PARSE_C "
22 "not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010023 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000024}
Paul Bakkercce9d772011-11-18 14:26:47 +000025#elif defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010026int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Gilles Peskine449bd832023-01-11 14:50:10 +010029 "to work correctly.\n");
30 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000031}
32#else
Paul Bakker896ac222011-05-20 12:33:05 +000033
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010034#include "mbedtls/entropy.h"
35#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010036#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010037#include "mbedtls/x509.h"
38#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010039#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010040#include "mbedtls/timing.h"
41
42#include <string.h>
43#include <signal.h>
44
45#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
46#include <unistd.h>
47#endif
48
49#define HTTP_RESPONSE \
50 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Gilles Peskinee820c0a2023-08-03 17:45:20 +020051 "<h2>Mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010052 "<p>Successful connection using: %s</p>\r\n"
53
Paul Bakker896ac222011-05-20 12:33:05 +000054#define DEBUG_LEVEL 0
55
Simon Butcher63cb97e2018-12-06 17:43:31 +000056
Gilles Peskine449bd832023-01-11 14:50:10 +010057static void my_debug(void *ctx, int level,
58 const char *file, int line,
59 const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000060{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020061 ((void) level);
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
64 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000065}
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000068{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010069 int ret = 1, len, cnt = 0, pid;
70 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020071 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000072 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_entropy_context entropy;
76 mbedtls_ctr_drbg_context ctr_drbg;
77 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020078 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_x509_crt srvcert;
80 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_net_init(&listen_fd);
83 mbedtls_net_init(&client_fd);
84 mbedtls_ssl_init(&ssl);
85 mbedtls_ssl_config_init(&conf);
86 mbedtls_entropy_init(&entropy);
87 mbedtls_pk_init(&pkey);
88 mbedtls_x509_crt_init(&srvcert);
89 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +020090
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020091 psa_status_t status = psa_crypto_init();
92 if (status != PSA_SUCCESS) {
93 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
94 (int) status);
95 goto exit;
96 }
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +000099
100 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000101 * 0. Initial seeding of the RNG
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_printf("\n . Initial seeding of the random generator...");
104 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
107 (const unsigned char *) pers,
108 strlen(pers))) != 0) {
109 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000110 goto exit;
111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114
115 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000116 * 1. Load the certificates and private RSA key
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_printf(" . Loading the server cert. and key...");
119 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000120
Paul Bakker896ac222011-05-20 12:33:05 +0000121 /*
122 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
124 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
127 mbedtls_test_srv_crt_len);
128 if (ret != 0) {
129 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000130 goto exit;
131 }
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
134 mbedtls_test_cas_pem_len);
135 if (ret != 0) {
136 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000137 goto exit;
138 }
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000141 mbedtls_test_srv_key_len, NULL, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (ret != 0) {
143 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000144 goto exit;
145 }
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000148
149 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200150 * 1b. Prepare SSL configuration
151 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_printf(" . Configuring SSL...");
153 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if ((ret = mbedtls_ssl_config_defaults(&conf,
156 MBEDTLS_SSL_IS_SERVER,
157 MBEDTLS_SSL_TRANSPORT_STREAM,
158 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
159 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200160 goto exit;
161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
164 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
167 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
168 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200169 goto exit;
170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200173
174 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000175 * 2. Setup the listening TCP socket
176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
178 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
181 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000182 goto exit;
183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000188 /*
189 * 3. Wait until a client connects
190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_net_init(&client_fd);
192 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 mbedtls_printf(" . Waiting for a remote connection ...\n");
195 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
198 NULL, 0, NULL)) != 0) {
199 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000200 goto exit;
201 }
202
Paul Bakker896ac222011-05-20 12:33:05 +0000203 /*
204 * 3.5. Forking server thread
205 */
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_printf(" . Forking to handle connection ...");
208 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000209
Janos Follath582a4612016-04-28 23:37:16 +0100210 pid = fork();
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (pid < 0) {
213 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000214 goto exit;
215 }
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (pid != 0) {
218 mbedtls_printf(" ok\n");
219 mbedtls_net_close(&client_fd);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200220 fflush(stdout);
Janos Follath582a4612016-04-28 23:37:16 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
223 (const unsigned char *) "parent",
224 6)) != 0) {
225 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000226 goto exit;
227 }
228
Paul Bakker896ac222011-05-20 12:33:05 +0000229 continue;
230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000233
Janos Follath582a4612016-04-28 23:37:16 +0100234 pid = getpid();
235
Paul Bakker896ac222011-05-20 12:33:05 +0000236 /*
237 * 4. Setup stuff
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
240 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
243 (const unsigned char *) "child",
244 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100245 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
247 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000248 goto exit;
249 }
Paul Bakker0c226102014-04-17 16:02:36 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100252 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
254 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200255 goto exit;
256 }
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200261
Paul Bakker896ac222011-05-20 12:33:05 +0000262 /*
263 * 5. Handshake
264 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
266 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
269 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100270 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
272 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000273 goto exit;
274 }
275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200278 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000279
280 /*
281 * 6. Read the HTTP Request
282 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_printf("pid %d: Start reading from client.\n", pid);
284 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 do {
287 len = sizeof(buf) - 1;
288 memset(buf, 0, sizeof(buf));
289 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000292 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 }
Paul Bakker896ac222011-05-20 12:33:05 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (ret <= 0) {
296 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000299 break;
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000303 break;
304
305 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000307 break;
308 }
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200309 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000310
311 break;
312 }
313
314 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200316 fflush(stdout);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000319 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 }
321 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000322
323 /*
324 * 7. Write the 200 Response
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_printf("pid %d: Start writing to client.\n", pid);
327 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 len = sprintf((char *) buf, HTTP_RESPONSE,
330 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000331
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200332 while (cnt++ < 10) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
334 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100335 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000337 goto exit;
338 }
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100341 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
343 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000344 goto exit;
345 }
346 }
347 len = ret;
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200348 mbedtls_printf("pid %d: %d bytes written (cnt=%d)\n\n%s\n",
349 pid, len, cnt, (char *) buf);
350 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_ssl_close_notify(&ssl);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200356 mbedtls_printf("pid %d: shutting down\n", pid);
357 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000358 goto exit;
359 }
360
361exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_net_free(&client_fd);
363 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_x509_crt_free(&srvcert);
365 mbedtls_pk_free(&pkey);
366 mbedtls_ssl_free(&ssl);
367 mbedtls_ssl_config_free(&conf);
368 mbedtls_ctr_drbg_free(&ctr_drbg);
369 mbedtls_entropy_free(&entropy);
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200370 mbedtls_psa_crypto_free();
Paul Bakker896ac222011-05-20 12:33:05 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000373}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100374#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
376 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100377 ! _WIN32 */