blob: 4777ee0d93c60f5e18f1b0e2c347560e93805c72 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker896ac222011-05-20 12:33:05 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker896ac222011-05-20 12:33:05 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
25 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
26 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
27 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
28 !defined(MBEDTLS_TIMING_C) || !defined(MBEDTLS_FS_IO) || \
29 !defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010030int main(int argc, char *argv[])
Paul Bakkerb892b132011-10-12 09:19:43 +000031{
Paul Bakkercce9d772011-11-18 14:26:47 +000032 ((void) argc);
33 ((void) argv);
34
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010035 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine449bd832023-01-11 14:50:10 +010036 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
37 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
38 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
39 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
40 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000041}
Paul Bakkercce9d772011-11-18 14:26:47 +000042#elif defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010043int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Gilles Peskine449bd832023-01-11 14:50:10 +010046 "to work correctly.\n");
47 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000048}
49#else
Paul Bakker896ac222011-05-20 12:33:05 +000050
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010051#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010053#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010054#include "mbedtls/x509.h"
55#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010057#include "mbedtls/timing.h"
58
59#include <string.h>
60#include <signal.h>
61
62#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
63#include <unistd.h>
64#endif
65
66#define HTTP_RESPONSE \
67 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
68 "<h2>mbed TLS Test Server</h2>\r\n" \
69 "<p>Successful connection using: %s</p>\r\n"
70
Paul Bakker896ac222011-05-20 12:33:05 +000071#define DEBUG_LEVEL 0
72
Simon Butcher63cb97e2018-12-06 17:43:31 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074static void my_debug(void *ctx, int level,
75 const char *file, int line,
76 const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000077{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020078 ((void) level);
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
81 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000085{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010086 int ret = 1, len, cnt = 0, pid;
87 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020088 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000089 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020090 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_entropy_context entropy;
93 mbedtls_ctr_drbg_context ctr_drbg;
94 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020095 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509_crt srvcert;
97 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +000098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_net_init(&listen_fd);
100 mbedtls_net_init(&client_fd);
101 mbedtls_ssl_init(&ssl);
102 mbedtls_ssl_config_init(&conf);
103 mbedtls_entropy_init(&entropy);
104 mbedtls_pk_init(&pkey);
105 mbedtls_x509_crt_init(&srvcert);
106 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +0200107
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200108#if defined(MBEDTLS_USE_PSA_CRYPTO)
109 psa_status_t status = psa_crypto_init();
110 if (status != PSA_SUCCESS) {
111 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
112 (int) status);
113 goto exit;
114 }
115#endif /* MBEDTLS_USE_PSA_CRYPTO */
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000118
119 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 * 0. Initial seeding of the RNG
121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf("\n . Initial seeding of the random generator...");
123 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
126 (const unsigned char *) pers,
127 strlen(pers))) != 0) {
128 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129 goto exit;
130 }
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000133
134 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000135 * 1. Load the certificates and private RSA key
136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_printf(" . Loading the server cert. and key...");
138 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000139
Paul Bakker896ac222011-05-20 12:33:05 +0000140 /*
141 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
143 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
146 mbedtls_test_srv_crt_len);
147 if (ret != 0) {
148 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000149 goto exit;
150 }
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
153 mbedtls_test_cas_pem_len);
154 if (ret != 0) {
155 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000156 goto exit;
157 }
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
160 mbedtls_test_srv_key_len, NULL, 0,
161 mbedtls_ctr_drbg_random, &ctr_drbg);
162 if (ret != 0) {
163 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000164 goto exit;
165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000168
169 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200170 * 1b. Prepare SSL configuration
171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_printf(" . Configuring SSL...");
173 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((ret = mbedtls_ssl_config_defaults(&conf,
176 MBEDTLS_SSL_IS_SERVER,
177 MBEDTLS_SSL_TRANSPORT_STREAM,
178 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
179 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200180 goto exit;
181 }
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
184 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
187 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
188 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200189 goto exit;
190 }
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200193
194 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000195 * 2. Setup the listening TCP socket
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
198 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
201 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000202 goto exit;
203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000208 /*
209 * 3. Wait until a client connects
210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_net_init(&client_fd);
212 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_printf(" . Waiting for a remote connection ...\n");
215 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
218 NULL, 0, NULL)) != 0) {
219 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000220 goto exit;
221 }
222
Paul Bakker896ac222011-05-20 12:33:05 +0000223 /*
224 * 3.5. Forking server thread
225 */
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_printf(" . Forking to handle connection ...");
228 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000229
Janos Follath582a4612016-04-28 23:37:16 +0100230 pid = fork();
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (pid < 0) {
233 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000234 goto exit;
235 }
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (pid != 0) {
238 mbedtls_printf(" ok\n");
239 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
242 (const unsigned char *) "parent",
243 6)) != 0) {
244 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000245 goto exit;
246 }
247
Paul Bakker896ac222011-05-20 12:33:05 +0000248 continue;
249 }
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000252
Janos Follath582a4612016-04-28 23:37:16 +0100253 pid = getpid();
254
Paul Bakker896ac222011-05-20 12:33:05 +0000255 /*
256 * 4. Setup stuff
257 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
259 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
262 (const unsigned char *) "child",
263 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100264 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
266 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000267 goto exit;
268 }
Paul Bakker0c226102014-04-17 16:02:36 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100271 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
273 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200274 goto exit;
275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200280
Paul Bakker896ac222011-05-20 12:33:05 +0000281 /*
282 * 5. Handshake
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
285 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
288 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100289 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
291 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000292 goto exit;
293 }
294 }
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000297
298 /*
299 * 6. Read the HTTP Request
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_printf("pid %d: Start reading from client.\n", pid);
302 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 do {
305 len = sizeof(buf) - 1;
306 memset(buf, 0, sizeof(buf));
307 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000310 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 }
Paul Bakker896ac222011-05-20 12:33:05 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (ret <= 0) {
314 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000317 break;
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000321 break;
322
323 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000325 break;
326 }
327
328 break;
329 }
330
331 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000335 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
337 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000338
339 /*
340 * 7. Write the 200 Response
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 mbedtls_printf("pid %d: Start writing to client.\n", pid);
343 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 len = sprintf((char *) buf, HTTP_RESPONSE,
346 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 while (cnt++ < 100) {
349 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
350 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100351 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000353 goto exit;
354 }
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100357 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
359 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000360 goto exit;
361 }
362 }
363 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000367 }
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000370 goto exit;
371 }
372
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100373 exit_code = MBEDTLS_EXIT_SUCCESS;
374
Paul Bakker896ac222011-05-20 12:33:05 +0000375exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_net_free(&client_fd);
377 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 mbedtls_x509_crt_free(&srvcert);
379 mbedtls_pk_free(&pkey);
380 mbedtls_ssl_free(&ssl);
381 mbedtls_ssl_config_free(&conf);
382 mbedtls_ctr_drbg_free(&ctr_drbg);
383 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200385 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200386#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker896ac222011-05-20 12:33:05 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000389}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100390#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
392 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100393 ! _WIN32 */