blob: 1bd18c1f192cd2dc3c9e05e55bbb7911ad950344 [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
Gilles Peskinec83e56c2024-09-04 17:47:14 +020012#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
13 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_SRV_C) || \
14 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
15int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000016{
Gilles Peskinec83e56c2024-09-04 17:47:14 +020017 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
18 "MBEDTLS_NET_C and/or MBEDTLS_SSL_SRV_C and/or "
19 "MBEDTLS_PEM_PARSE_C and/or MBEDTLS_X509_CRT_PARSE_C "
20 "not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000022}
Paul Bakkercce9d772011-11-18 14:26:47 +000023#elif defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010024int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Gilles Peskine449bd832023-01-11 14:50:10 +010027 "to work correctly.\n");
28 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000029}
30#else
Paul Bakker896ac222011-05-20 12:33:05 +000031
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010032#include "mbedtls/entropy.h"
33#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010034#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010035#include "mbedtls/x509.h"
36#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010037#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010038#include "mbedtls/timing.h"
39
40#include <string.h>
41#include <signal.h>
42
43#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
44#include <unistd.h>
45#endif
46
47#define HTTP_RESPONSE \
48 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Gilles Peskinee820c0a2023-08-03 17:45:20 +020049 "<h2>Mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010050 "<p>Successful connection using: %s</p>\r\n"
51
Paul Bakker896ac222011-05-20 12:33:05 +000052#define DEBUG_LEVEL 0
53
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Gilles Peskine449bd832023-01-11 14:50:10 +010055static void my_debug(void *ctx, int level,
56 const char *file, int line,
57 const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000058{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020059 ((void) level);
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
62 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000063}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000066{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010067 int ret = 1, len, cnt = 0, pid;
68 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020069 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000070 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_entropy_context entropy;
74 mbedtls_ctr_drbg_context ctr_drbg;
75 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020076 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_x509_crt srvcert;
78 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_net_init(&listen_fd);
81 mbedtls_net_init(&client_fd);
82 mbedtls_ssl_init(&ssl);
83 mbedtls_ssl_config_init(&conf);
84 mbedtls_entropy_init(&entropy);
85 mbedtls_pk_init(&pkey);
86 mbedtls_x509_crt_init(&srvcert);
87 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +020088
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020089 psa_status_t status = psa_crypto_init();
90 if (status != PSA_SUCCESS) {
91 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
92 (int) status);
93 goto exit;
94 }
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +000097
98 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 * 0. Initial seeding of the RNG
100 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_printf("\n . Initial seeding of the random generator...");
102 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
105 (const unsigned char *) pers,
106 strlen(pers))) != 0) {
107 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000108 goto exit;
109 }
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000112
113 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000114 * 1. Load the certificates and private RSA key
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_printf(" . Loading the server cert. and key...");
117 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000118
Paul Bakker896ac222011-05-20 12:33:05 +0000119 /*
120 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
122 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
125 mbedtls_test_srv_crt_len);
126 if (ret != 0) {
127 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
132 mbedtls_test_cas_pem_len);
133 if (ret != 0) {
134 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000135 goto exit;
136 }
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
139 mbedtls_test_srv_key_len, NULL, 0,
140 mbedtls_ctr_drbg_random, &ctr_drbg);
141 if (ret != 0) {
142 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000143 goto exit;
144 }
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000147
148 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200149 * 1b. Prepare SSL configuration
150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_printf(" . Configuring SSL...");
152 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_ssl_config_defaults(&conf,
155 MBEDTLS_SSL_IS_SERVER,
156 MBEDTLS_SSL_TRANSPORT_STREAM,
157 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
158 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200159 goto exit;
160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
163 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
166 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
167 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert 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_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200172
173 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000174 * 2. Setup the listening TCP socket
175 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
177 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
180 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000181 goto exit;
182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000187 /*
188 * 3. Wait until a client connects
189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_net_init(&client_fd);
191 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_printf(" . Waiting for a remote connection ...\n");
194 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
197 NULL, 0, NULL)) != 0) {
198 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000199 goto exit;
200 }
201
Paul Bakker896ac222011-05-20 12:33:05 +0000202 /*
203 * 3.5. Forking server thread
204 */
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_printf(" . Forking to handle connection ...");
207 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000208
Janos Follath582a4612016-04-28 23:37:16 +0100209 pid = fork();
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (pid < 0) {
212 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000213 goto exit;
214 }
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (pid != 0) {
217 mbedtls_printf(" ok\n");
218 mbedtls_net_close(&client_fd);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200219 fflush(stdout);
Janos Follath582a4612016-04-28 23:37:16 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
222 (const unsigned char *) "parent",
223 6)) != 0) {
224 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000225 goto exit;
226 }
227
Paul Bakker896ac222011-05-20 12:33:05 +0000228 continue;
229 }
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000232
Janos Follath582a4612016-04-28 23:37:16 +0100233 pid = getpid();
234
Paul Bakker896ac222011-05-20 12:33:05 +0000235 /*
236 * 4. Setup stuff
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
239 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
242 (const unsigned char *) "child",
243 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100244 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
246 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000247 goto exit;
248 }
Paul Bakker0c226102014-04-17 16:02:36 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100251 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
253 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200254 goto exit;
255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200260
Paul Bakker896ac222011-05-20 12:33:05 +0000261 /*
262 * 5. Handshake
263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
265 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
268 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100269 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
271 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000272 goto exit;
273 }
274 }
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200277 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000278
279 /*
280 * 6. Read the HTTP Request
281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 mbedtls_printf("pid %d: Start reading from client.\n", pid);
283 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 do {
286 len = sizeof(buf) - 1;
287 memset(buf, 0, sizeof(buf));
288 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000291 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 }
Paul Bakker896ac222011-05-20 12:33:05 +0000293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (ret <= 0) {
295 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000298 break;
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000302 break;
303
304 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000306 break;
307 }
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200308 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000309
310 break;
311 }
312
313 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200315 fflush(stdout);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000318 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 }
320 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000321
322 /*
323 * 7. Write the 200 Response
324 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_printf("pid %d: Start writing to client.\n", pid);
326 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 len = sprintf((char *) buf, HTTP_RESPONSE,
329 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000330
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200331 while (cnt++ < 10) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
333 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100334 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000336 goto exit;
337 }
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100340 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
342 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000343 goto exit;
344 }
345 }
346 len = ret;
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200347 mbedtls_printf("pid %d: %d bytes written (cnt=%d)\n\n%s\n",
348 pid, len, cnt, (char *) buf);
349 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000352 }
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_ssl_close_notify(&ssl);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200355 mbedtls_printf("pid %d: shutting down\n", pid);
356 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000357 goto exit;
358 }
359
360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_net_free(&client_fd);
362 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 mbedtls_x509_crt_free(&srvcert);
364 mbedtls_pk_free(&pkey);
365 mbedtls_ssl_free(&ssl);
366 mbedtls_ssl_config_free(&conf);
367 mbedtls_ctr_drbg_free(&ctr_drbg);
368 mbedtls_entropy_free(&entropy);
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200369 mbedtls_psa_crypto_free();
Paul Bakker896ac222011-05-20 12:33:05 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000372}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100373#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
375 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100376 ! _WIN32 */