blob: b9598585bfa1bd7081aa1e8c6fa5947d2edd3da4 [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,
141 mbedtls_test_srv_key_len, NULL, 0,
142 mbedtls_ctr_drbg_random, &ctr_drbg);
143 if (ret != 0) {
144 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000145 goto exit;
146 }
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000149
150 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200151 * 1b. Prepare SSL configuration
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_printf(" . Configuring SSL...");
154 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if ((ret = mbedtls_ssl_config_defaults(&conf,
157 MBEDTLS_SSL_IS_SERVER,
158 MBEDTLS_SSL_TRANSPORT_STREAM,
159 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
160 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200161 goto exit;
162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
165 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
168 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
169 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200170 goto exit;
171 }
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200174
175 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000176 * 2. Setup the listening TCP socket
177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
179 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
182 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000183 goto exit;
184 }
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000189 /*
190 * 3. Wait until a client connects
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_net_init(&client_fd);
193 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_printf(" . Waiting for a remote connection ...\n");
196 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
199 NULL, 0, NULL)) != 0) {
200 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000201 goto exit;
202 }
203
Paul Bakker896ac222011-05-20 12:33:05 +0000204 /*
205 * 3.5. Forking server thread
206 */
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 mbedtls_printf(" . Forking to handle connection ...");
209 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000210
Janos Follath582a4612016-04-28 23:37:16 +0100211 pid = fork();
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (pid < 0) {
214 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000215 goto exit;
216 }
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (pid != 0) {
219 mbedtls_printf(" ok\n");
220 mbedtls_net_close(&client_fd);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200221 fflush(stdout);
Janos Follath582a4612016-04-28 23:37:16 +0100222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
224 (const unsigned char *) "parent",
225 6)) != 0) {
226 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000227 goto exit;
228 }
229
Paul Bakker896ac222011-05-20 12:33:05 +0000230 continue;
231 }
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000234
Janos Follath582a4612016-04-28 23:37:16 +0100235 pid = getpid();
236
Paul Bakker896ac222011-05-20 12:33:05 +0000237 /*
238 * 4. Setup stuff
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
241 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
244 (const unsigned char *) "child",
245 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100246 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
248 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000249 goto exit;
250 }
Paul Bakker0c226102014-04-17 16:02:36 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100253 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
255 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200256 goto exit;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200262
Paul Bakker896ac222011-05-20 12:33:05 +0000263 /*
264 * 5. Handshake
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
267 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
270 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100271 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
273 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000274 goto exit;
275 }
276 }
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200279 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000280
281 /*
282 * 6. Read the HTTP Request
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_printf("pid %d: Start reading from client.\n", pid);
285 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 do {
288 len = sizeof(buf) - 1;
289 memset(buf, 0, sizeof(buf));
290 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000293 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 }
Paul Bakker896ac222011-05-20 12:33:05 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ret <= 0) {
297 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000300 break;
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000304 break;
305
306 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000308 break;
309 }
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200310 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000311
312 break;
313 }
314
315 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200317 fflush(stdout);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000320 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
322 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000323
324 /*
325 * 7. Write the 200 Response
326 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 mbedtls_printf("pid %d: Start writing to client.\n", pid);
328 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 len = sprintf((char *) buf, HTTP_RESPONSE,
331 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000332
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200333 while (cnt++ < 10) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
335 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100336 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000338 goto exit;
339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100342 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
344 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000345 goto exit;
346 }
347 }
348 len = ret;
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200349 mbedtls_printf("pid %d: %d bytes written (cnt=%d)\n\n%s\n",
350 pid, len, cnt, (char *) buf);
351 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000354 }
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_ssl_close_notify(&ssl);
Gilles Peskinec83e56c2024-09-04 17:47:14 +0200357 mbedtls_printf("pid %d: shutting down\n", pid);
358 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000359 goto exit;
360 }
361
362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 mbedtls_net_free(&client_fd);
364 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_x509_crt_free(&srvcert);
366 mbedtls_pk_free(&pkey);
367 mbedtls_ssl_free(&ssl);
368 mbedtls_ssl_config_free(&conf);
369 mbedtls_ctr_drbg_free(&ctr_drbg);
370 mbedtls_entropy_free(&entropy);
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200371 mbedtls_psa_crypto_free();
Paul Bakker896ac222011-05-20 12:33:05 +0000372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000374}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100375#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
377 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100378 ! _WIN32 */