blob: 5a4ac3eedb25b20080158aece3ba7f3dbacb2c95 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker896ac222011-05-20 12:33:05 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
29 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
30 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
31 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
32 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
33 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000034int main(int argc, char *argv[])
Paul Bakkerb892b132011-10-12 09:19:43 +000035{
Paul Bakkercce9d772011-11-18 14:26:47 +000036 ((void) argc);
37 ((void) argv);
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
David Horstmannceeaeb92023-01-05 15:44:23 +000040 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
41 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
42 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
43 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
44 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000045}
Paul Bakkercce9d772011-11-18 14:26:47 +000046#elif defined(_WIN32)
David Horstmannceeaeb92023-01-05 15:44:23 +000047int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
David Horstmannceeaeb92023-01-05 15:44:23 +000050 "to work correctly.\n");
51 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000052}
53#else
Paul Bakker896ac222011-05-20 12:33:05 +000054
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010055#include "mbedtls/entropy.h"
56#include "mbedtls/ctr_drbg.h"
57#include "mbedtls/certs.h"
58#include "mbedtls/x509.h"
59#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010060#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010061#include "mbedtls/timing.h"
62
63#include <string.h>
64#include <signal.h>
65
66#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
67#include <unistd.h>
68#endif
69
70#define HTTP_RESPONSE \
71 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
72 "<h2>mbed TLS Test Server</h2>\r\n" \
73 "<p>Successful connection using: %s</p>\r\n"
74
Paul Bakker896ac222011-05-20 12:33:05 +000075#define DEBUG_LEVEL 0
76
Simon Butcher63cb97e2018-12-06 17:43:31 +000077
David Horstmannceeaeb92023-01-05 15:44:23 +000078static void my_debug(void *ctx, int level,
79 const char *file, int line,
80 const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000081{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020082 ((void) level);
83
David Horstmannceeaeb92023-01-05 15:44:23 +000084 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
85 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000086}
87
David Horstmannceeaeb92023-01-05 15:44:23 +000088int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000089{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010090 int ret = 1, len, cnt = 0, pid;
91 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020092 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000093 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_entropy_context entropy;
97 mbedtls_ctr_drbg_context ctr_drbg;
98 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020099 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_crt srvcert;
101 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000102
David Horstmannceeaeb92023-01-05 15:44:23 +0000103 mbedtls_net_init(&listen_fd);
104 mbedtls_net_init(&client_fd);
105 mbedtls_ssl_init(&ssl);
106 mbedtls_ssl_config_init(&conf);
107 mbedtls_entropy_init(&entropy);
108 mbedtls_pk_init(&pkey);
109 mbedtls_x509_crt_init(&srvcert);
110 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +0200111
David Horstmannceeaeb92023-01-05 15:44:23 +0000112 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000113
114 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 * 0. Initial seeding of the RNG
116 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000117 mbedtls_printf("\n . Initial seeding of the random generator...");
118 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119
David Horstmannceeaeb92023-01-05 15:44:23 +0000120 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
121 (const unsigned char *) pers,
122 strlen(pers))) != 0) {
123 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 goto exit;
125 }
126
David Horstmannceeaeb92023-01-05 15:44:23 +0000127 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000128
129 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000130 * 1. Load the certificates and private RSA key
131 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000132 mbedtls_printf(" . Loading the server cert. and key...");
133 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000134
Paul Bakker896ac222011-05-20 12:33:05 +0000135 /*
136 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
138 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000139 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000140 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
141 mbedtls_test_srv_crt_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
David Horstmannceeaeb92023-01-05 15:44:23 +0000147 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
148 mbedtls_test_cas_pem_len);
149 if (ret != 0) {
150 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000151 goto exit;
152 }
153
David Horstmannceeaeb92023-01-05 15:44:23 +0000154 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
155 mbedtls_test_srv_key_len, NULL, 0);
156 if (ret != 0) {
157 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000158 goto exit;
159 }
160
David Horstmannceeaeb92023-01-05 15:44:23 +0000161 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000162
163 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200164 * 1b. Prepare SSL configuration
165 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000166 mbedtls_printf(" . Configuring SSL...");
167 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200168
David Horstmannceeaeb92023-01-05 15:44:23 +0000169 if ((ret = mbedtls_ssl_config_defaults(&conf,
170 MBEDTLS_SSL_IS_SERVER,
171 MBEDTLS_SSL_TRANSPORT_STREAM,
172 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
173 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200174 goto exit;
175 }
176
David Horstmannceeaeb92023-01-05 15:44:23 +0000177 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
178 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200179
David Horstmannceeaeb92023-01-05 15:44:23 +0000180 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
181 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
182 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200183 goto exit;
184 }
185
David Horstmannceeaeb92023-01-05 15:44:23 +0000186 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200187
188 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000189 * 2. Setup the listening TCP socket
190 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000191 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
192 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000193
David Horstmannceeaeb92023-01-05 15:44:23 +0000194 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
195 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000196 goto exit;
197 }
198
David Horstmannceeaeb92023-01-05 15:44:23 +0000199 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000200
David Horstmannceeaeb92023-01-05 15:44:23 +0000201 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000202 /*
203 * 3. Wait until a client connects
204 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000205 mbedtls_net_init(&client_fd);
206 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000207
David Horstmannceeaeb92023-01-05 15:44:23 +0000208 mbedtls_printf(" . Waiting for a remote connection ...\n");
209 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000210
David Horstmannceeaeb92023-01-05 15:44:23 +0000211 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
212 NULL, 0, NULL)) != 0) {
213 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000214 goto exit;
215 }
216
Paul Bakker896ac222011-05-20 12:33:05 +0000217 /*
218 * 3.5. Forking server thread
219 */
220
David Horstmannceeaeb92023-01-05 15:44:23 +0000221 mbedtls_printf(" . Forking to handle connection ...");
222 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000223
Janos Follath582a4612016-04-28 23:37:16 +0100224 pid = fork();
225
David Horstmannceeaeb92023-01-05 15:44:23 +0000226 if (pid < 0) {
227 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000228 goto exit;
229 }
230
David Horstmannceeaeb92023-01-05 15:44:23 +0000231 if (pid != 0) {
232 mbedtls_printf(" ok\n");
233 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100234
David Horstmannceeaeb92023-01-05 15:44:23 +0000235 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
236 (const unsigned char *) "parent",
237 6)) != 0) {
238 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000239 goto exit;
240 }
241
Paul Bakker896ac222011-05-20 12:33:05 +0000242 continue;
243 }
244
David Horstmannceeaeb92023-01-05 15:44:23 +0000245 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000246
Janos Follath582a4612016-04-28 23:37:16 +0100247 pid = getpid();
248
Paul Bakker896ac222011-05-20 12:33:05 +0000249 /*
250 * 4. Setup stuff
251 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000252 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
253 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000254
David Horstmannceeaeb92023-01-05 15:44:23 +0000255 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
256 (const unsigned char *) "child",
257 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100258 mbedtls_printf(
David Horstmannceeaeb92023-01-05 15:44:23 +0000259 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
260 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000261 goto exit;
262 }
Paul Bakker0c226102014-04-17 16:02:36 +0200263
David Horstmannceeaeb92023-01-05 15:44:23 +0000264 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100265 mbedtls_printf(
David Horstmannceeaeb92023-01-05 15:44:23 +0000266 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
267 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200268 goto exit;
269 }
270
David Horstmannceeaeb92023-01-05 15:44:23 +0000271 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200272
David Horstmannceeaeb92023-01-05 15:44:23 +0000273 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200274
Paul Bakker896ac222011-05-20 12:33:05 +0000275 /*
276 * 5. Handshake
277 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000278 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
279 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000280
David Horstmannceeaeb92023-01-05 15:44:23 +0000281 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
282 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100283 mbedtls_printf(
David Horstmannceeaeb92023-01-05 15:44:23 +0000284 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
285 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000286 goto exit;
287 }
288 }
289
David Horstmannceeaeb92023-01-05 15:44:23 +0000290 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000291
292 /*
293 * 6. Read the HTTP Request
294 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000295 mbedtls_printf("pid %d: Start reading from client.\n", pid);
296 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000297
David Horstmannceeaeb92023-01-05 15:44:23 +0000298 do {
299 len = sizeof(buf) - 1;
300 memset(buf, 0, sizeof(buf));
301 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000302
David Horstmannceeaeb92023-01-05 15:44:23 +0000303 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000304 continue;
David Horstmannceeaeb92023-01-05 15:44:23 +0000305 }
Paul Bakker896ac222011-05-20 12:33:05 +0000306
David Horstmannceeaeb92023-01-05 15:44:23 +0000307 if (ret <= 0) {
308 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
David Horstmannceeaeb92023-01-05 15:44:23 +0000310 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000311 break;
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 case MBEDTLS_ERR_NET_CONN_RESET:
David Horstmannceeaeb92023-01-05 15:44:23 +0000314 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000315 break;
316
317 default:
David Horstmannceeaeb92023-01-05 15:44:23 +0000318 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000319 break;
320 }
321
322 break;
323 }
324
325 len = ret;
David Horstmannceeaeb92023-01-05 15:44:23 +0000326 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000327
David Horstmannceeaeb92023-01-05 15:44:23 +0000328 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000329 break;
David Horstmannceeaeb92023-01-05 15:44:23 +0000330 }
331 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000332
333 /*
334 * 7. Write the 200 Response
335 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000336 mbedtls_printf("pid %d: Start writing to client.\n", pid);
337 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000338
David Horstmannceeaeb92023-01-05 15:44:23 +0000339 len = sprintf((char *) buf, HTTP_RESPONSE,
340 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000341
David Horstmannceeaeb92023-01-05 15:44:23 +0000342 while (cnt++ < 100) {
343 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
344 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100345 mbedtls_printf(
David Horstmannceeaeb92023-01-05 15:44:23 +0000346 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000347 goto exit;
348 }
349
David Horstmannceeaeb92023-01-05 15:44:23 +0000350 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100351 mbedtls_printf(
David Horstmannceeaeb92023-01-05 15:44:23 +0000352 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
353 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000354 goto exit;
355 }
356 }
357 len = ret;
David Horstmannceeaeb92023-01-05 15:44:23 +0000358 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000359
David Horstmannceeaeb92023-01-05 15:44:23 +0000360 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000361 }
362
David Horstmannceeaeb92023-01-05 15:44:23 +0000363 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000364 goto exit;
365 }
366
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100367 exit_code = MBEDTLS_EXIT_SUCCESS;
368
Paul Bakker896ac222011-05-20 12:33:05 +0000369exit:
David Horstmannceeaeb92023-01-05 15:44:23 +0000370 mbedtls_net_free(&client_fd);
371 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200372
David Horstmannceeaeb92023-01-05 15:44:23 +0000373 mbedtls_x509_crt_free(&srvcert);
374 mbedtls_pk_free(&pkey);
375 mbedtls_ssl_free(&ssl);
376 mbedtls_ssl_config_free(&conf);
377 mbedtls_ctr_drbg_free(&ctr_drbg);
378 mbedtls_entropy_free(&entropy);
Paul Bakker896ac222011-05-20 12:33:05 +0000379
Paul Bakkercce9d772011-11-18 14:26:47 +0000380#if defined(_WIN32)
David Horstmannceeaeb92023-01-05 15:44:23 +0000381 mbedtls_printf(" Press Enter to exit this program.\n");
382 fflush(stdout); getchar();
Paul Bakker896ac222011-05-20 12:33:05 +0000383#endif
384
David Horstmannceeaeb92023-01-05 15:44:23 +0000385 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
388 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
389 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100390 ! _WIN32 */