blob: f4822b7e6883ce7f98166db59d94ce34d09684b1 [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker896ac222011-05-20 12:33:05 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010012#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
13 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
14 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
15 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
16 !defined(MBEDTLS_TIMING_C) || !defined(MBEDTLS_FS_IO) || \
17 !defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010018int main(int argc, char *argv[])
Paul Bakkerb892b132011-10-12 09:19:43 +000019{
Paul Bakkercce9d772011-11-18 14:26:47 +000020 ((void) argc);
21 ((void) argv);
22
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010023 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine449bd832023-01-11 14:50:10 +010024 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
25 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
26 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
27 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
28 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000029}
Paul Bakkercce9d772011-11-18 14:26:47 +000030#elif defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010031int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Gilles Peskine449bd832023-01-11 14:50:10 +010034 "to work correctly.\n");
35 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000036}
37#else
Paul Bakker896ac222011-05-20 12:33:05 +000038
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010039#include "mbedtls/entropy.h"
40#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010041#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010042#include "mbedtls/x509.h"
43#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010044#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010045#include "mbedtls/timing.h"
46
47#include <string.h>
48#include <signal.h>
49
50#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
51#include <unistd.h>
52#endif
53
54#define HTTP_RESPONSE \
55 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Gilles Peskinee820c0a2023-08-03 17:45:20 +020056 "<h2>Mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010057 "<p>Successful connection using: %s</p>\r\n"
58
Paul Bakker896ac222011-05-20 12:33:05 +000059#define DEBUG_LEVEL 0
60
Simon Butcher63cb97e2018-12-06 17:43:31 +000061
Gilles Peskine449bd832023-01-11 14:50:10 +010062static void my_debug(void *ctx, int level,
63 const char *file, int line,
64 const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000065{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020066 ((void) level);
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
69 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000073{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010074 int ret = 1, len, cnt = 0, pid;
75 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020076 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000077 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020078 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_entropy_context entropy;
81 mbedtls_ctr_drbg_context ctr_drbg;
82 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020083 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509_crt srvcert;
85 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +000086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_net_init(&listen_fd);
88 mbedtls_net_init(&client_fd);
89 mbedtls_ssl_init(&ssl);
90 mbedtls_ssl_config_init(&conf);
91 mbedtls_entropy_init(&entropy);
92 mbedtls_pk_init(&pkey);
93 mbedtls_x509_crt_init(&srvcert);
94 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +020095
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020096#if defined(MBEDTLS_USE_PSA_CRYPTO)
97 psa_status_t status = psa_crypto_init();
98 if (status != PSA_SUCCESS) {
99 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
100 (int) status);
101 goto exit;
102 }
103#endif /* MBEDTLS_USE_PSA_CRYPTO */
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000106
107 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000108 * 0. Initial seeding of the RNG
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_printf("\n . Initial seeding of the random generator...");
111 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
114 (const unsigned char *) pers,
115 strlen(pers))) != 0) {
116 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000117 goto exit;
118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121
122 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000123 * 1. Load the certificates and private RSA key
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_printf(" . Loading the server cert. and key...");
126 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000127
Paul Bakker896ac222011-05-20 12:33:05 +0000128 /*
129 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
131 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
134 mbedtls_test_srv_crt_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_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
141 mbedtls_test_cas_pem_len);
142 if (ret != 0) {
143 mbedtls_printf(" failed! mbedtls_x509_crt_parse 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 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
148 mbedtls_test_srv_key_len, NULL, 0,
149 mbedtls_ctr_drbg_random, &ctr_drbg);
150 if (ret != 0) {
151 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000152 goto exit;
153 }
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000156
157 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200158 * 1b. Prepare SSL configuration
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_printf(" . Configuring SSL...");
161 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if ((ret = mbedtls_ssl_config_defaults(&conf,
164 MBEDTLS_SSL_IS_SERVER,
165 MBEDTLS_SSL_TRANSPORT_STREAM,
166 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
167 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200168 goto exit;
169 }
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
172 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
175 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
176 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200177 goto exit;
178 }
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200181
182 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000183 * 2. Setup the listening TCP socket
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
186 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
189 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000190 goto exit;
191 }
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000196 /*
197 * 3. Wait until a client connects
198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_net_init(&client_fd);
200 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_printf(" . Waiting for a remote connection ...\n");
203 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
206 NULL, 0, NULL)) != 0) {
207 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000208 goto exit;
209 }
210
Paul Bakker896ac222011-05-20 12:33:05 +0000211 /*
212 * 3.5. Forking server thread
213 */
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_printf(" . Forking to handle connection ...");
216 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000217
Janos Follath582a4612016-04-28 23:37:16 +0100218 pid = fork();
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (pid < 0) {
221 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000222 goto exit;
223 }
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (pid != 0) {
226 mbedtls_printf(" ok\n");
227 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
230 (const unsigned char *) "parent",
231 6)) != 0) {
232 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000233 goto exit;
234 }
235
Paul Bakker896ac222011-05-20 12:33:05 +0000236 continue;
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000240
Janos Follath582a4612016-04-28 23:37:16 +0100241 pid = getpid();
242
Paul Bakker896ac222011-05-20 12:33:05 +0000243 /*
244 * 4. Setup stuff
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
247 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
250 (const unsigned char *) "child",
251 5)) != 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_ctr_drbg_reseed returned %d\n\n",
254 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000255 goto exit;
256 }
Paul Bakker0c226102014-04-17 16:02:36 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100259 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
261 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200262 goto exit;
263 }
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200268
Paul Bakker896ac222011-05-20 12:33:05 +0000269 /*
270 * 5. Handshake
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
273 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
276 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100277 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
279 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000280 goto exit;
281 }
282 }
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000285
286 /*
287 * 6. Read the HTTP Request
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 mbedtls_printf("pid %d: Start reading from client.\n", pid);
290 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 do {
293 len = sizeof(buf) - 1;
294 memset(buf, 0, sizeof(buf));
295 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000298 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 }
Paul Bakker896ac222011-05-20 12:33:05 +0000300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ret <= 0) {
302 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000305 break;
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000309 break;
310
311 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000313 break;
314 }
315
316 break;
317 }
318
319 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000323 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
325 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000326
327 /*
328 * 7. Write the 200 Response
329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_printf("pid %d: Start writing to client.\n", pid);
331 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 len = sprintf((char *) buf, HTTP_RESPONSE,
334 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 while (cnt++ < 100) {
337 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
338 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100339 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000341 goto exit;
342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100345 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
347 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000348 goto exit;
349 }
350 }
351 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000358 goto exit;
359 }
360
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100361 exit_code = MBEDTLS_EXIT_SUCCESS;
362
Paul Bakker896ac222011-05-20 12:33:05 +0000363exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_net_free(&client_fd);
365 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_x509_crt_free(&srvcert);
367 mbedtls_pk_free(&pkey);
368 mbedtls_ssl_free(&ssl);
369 mbedtls_ssl_config_free(&conf);
370 mbedtls_ctr_drbg_free(&ctr_drbg);
371 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200372#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200373 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200374#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker896ac222011-05-20 12:33:05 +0000375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000377}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100378#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
380 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100381 ! _WIN32 */