blob: 7ee880d38050a5ab29c8ec39695b59970e323382 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000109
110 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111 * 0. Initial seeding of the RNG
112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_printf("\n . Initial seeding of the random generator...");
114 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
117 (const unsigned char *) pers,
118 strlen(pers))) != 0) {
119 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124
125 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000126 * 1. Load the certificates and private RSA key
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_printf(" . Loading the server cert. and key...");
129 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000130
Paul Bakker896ac222011-05-20 12:33:05 +0000131 /*
132 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
134 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
137 mbedtls_test_srv_crt_len);
138 if (ret != 0) {
139 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000140 goto exit;
141 }
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
144 mbedtls_test_cas_pem_len);
145 if (ret != 0) {
146 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000147 goto exit;
148 }
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
151 mbedtls_test_srv_key_len, NULL, 0,
152 mbedtls_ctr_drbg_random, &ctr_drbg);
153 if (ret != 0) {
154 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000155 goto exit;
156 }
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000159
160 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200161 * 1b. Prepare SSL configuration
162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_printf(" . Configuring SSL...");
164 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if ((ret = mbedtls_ssl_config_defaults(&conf,
167 MBEDTLS_SSL_IS_SERVER,
168 MBEDTLS_SSL_TRANSPORT_STREAM,
169 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
170 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200171 goto exit;
172 }
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
175 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
178 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
179 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert 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_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200184
185 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000186 * 2. Setup the listening TCP socket
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
189 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
192 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000193 goto exit;
194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000199 /*
200 * 3. Wait until a client connects
201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_net_init(&client_fd);
203 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_printf(" . Waiting for a remote connection ...\n");
206 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
209 NULL, 0, NULL)) != 0) {
210 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000211 goto exit;
212 }
213
Paul Bakker896ac222011-05-20 12:33:05 +0000214 /*
215 * 3.5. Forking server thread
216 */
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_printf(" . Forking to handle connection ...");
219 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000220
Janos Follath582a4612016-04-28 23:37:16 +0100221 pid = fork();
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (pid < 0) {
224 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000225 goto exit;
226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (pid != 0) {
229 mbedtls_printf(" ok\n");
230 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
233 (const unsigned char *) "parent",
234 6)) != 0) {
235 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000236 goto exit;
237 }
238
Paul Bakker896ac222011-05-20 12:33:05 +0000239 continue;
240 }
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000243
Janos Follath582a4612016-04-28 23:37:16 +0100244 pid = getpid();
245
Paul Bakker896ac222011-05-20 12:33:05 +0000246 /*
247 * 4. Setup stuff
248 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
250 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
253 (const unsigned char *) "child",
254 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100255 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
257 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000258 goto exit;
259 }
Paul Bakker0c226102014-04-17 16:02:36 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100262 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
264 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200265 goto exit;
266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200271
Paul Bakker896ac222011-05-20 12:33:05 +0000272 /*
273 * 5. Handshake
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
276 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
279 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100280 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
282 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000283 goto exit;
284 }
285 }
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000288
289 /*
290 * 6. Read the HTTP Request
291 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_printf("pid %d: Start reading from client.\n", pid);
293 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 do {
296 len = sizeof(buf) - 1;
297 memset(buf, 0, sizeof(buf));
298 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000301 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
Paul Bakker896ac222011-05-20 12:33:05 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (ret <= 0) {
305 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000308 break;
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000312 break;
313
314 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000316 break;
317 }
318
319 break;
320 }
321
322 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000326 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 }
328 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000329
330 /*
331 * 7. Write the 200 Response
332 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_printf("pid %d: Start writing to client.\n", pid);
334 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 len = sprintf((char *) buf, HTTP_RESPONSE,
337 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 while (cnt++ < 100) {
340 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
341 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100342 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000344 goto exit;
345 }
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100348 mbedtls_printf(
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
350 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000351 goto exit;
352 }
353 }
354 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000358 }
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000361 goto exit;
362 }
363
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100364 exit_code = MBEDTLS_EXIT_SUCCESS;
365
Paul Bakker896ac222011-05-20 12:33:05 +0000366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_net_free(&client_fd);
368 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 mbedtls_x509_crt_free(&srvcert);
371 mbedtls_pk_free(&pkey);
372 mbedtls_ssl_free(&ssl);
373 mbedtls_ssl_config_free(&conf);
374 mbedtls_ctr_drbg_free(&ctr_drbg);
375 mbedtls_entropy_free(&entropy);
Paul Bakker896ac222011-05-20 12:33:05 +0000376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000378}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100379#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
381 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100382 ! _WIN32 */