blob: adba12aabedaa88eb20d51911856580eb4d4b8df [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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034int 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 "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 "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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047int 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 "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 "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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078static 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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
85 fflush((FILE *) ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000086}
87
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088int 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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 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
Przemek Stekielb00688f2023-04-17 11:10:05 +0200112#if defined(MBEDTLS_USE_PSA_CRYPTO)
113 psa_status_t status = psa_crypto_init();
114 if (status != PSA_SUCCESS) {
115 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
116 (int) status);
117 goto exit;
118 }
119#endif /* MBEDTLS_USE_PSA_CRYPTO */
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000122
123 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 * 0. Initial seeding of the RNG
125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_printf("\n . Initial seeding of the random generator...");
127 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
130 (const unsigned char *) pers,
131 strlen(pers))) != 0) {
132 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000133 goto exit;
134 }
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000137
138 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000139 * 1. Load the certificates and private RSA key
140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_printf(" . Loading the server cert. and key...");
142 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000143
Paul Bakker896ac222011-05-20 12:33:05 +0000144 /*
145 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
147 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
150 mbedtls_test_srv_crt_len);
151 if (ret != 0) {
152 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000153 goto exit;
154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
157 mbedtls_test_cas_pem_len);
158 if (ret != 0) {
159 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000160 goto exit;
161 }
162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
164 mbedtls_test_srv_key_len, NULL, 0);
165 if (ret != 0) {
166 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000167 goto exit;
168 }
169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000171
172 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200173 * 1b. Prepare SSL configuration
174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_printf(" . Configuring SSL...");
176 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if ((ret = mbedtls_ssl_config_defaults(&conf,
179 MBEDTLS_SSL_IS_SERVER,
180 MBEDTLS_SSL_TRANSPORT_STREAM,
181 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
182 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200183 goto exit;
184 }
185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
187 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
190 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
191 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200192 goto exit;
193 }
194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200196
197 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000198 * 2. Setup the listening TCP socket
199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
201 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
204 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000205 goto exit;
206 }
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000211 /*
212 * 3. Wait until a client connects
213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_net_init(&client_fd);
215 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 mbedtls_printf(" . Waiting for a remote connection ...\n");
218 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
221 NULL, 0, NULL)) != 0) {
222 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000223 goto exit;
224 }
225
Paul Bakker896ac222011-05-20 12:33:05 +0000226 /*
227 * 3.5. Forking server thread
228 */
229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_printf(" . Forking to handle connection ...");
231 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000232
Janos Follath582a4612016-04-28 23:37:16 +0100233 pid = fork();
234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (pid < 0) {
236 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000237 goto exit;
238 }
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (pid != 0) {
241 mbedtls_printf(" ok\n");
242 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
245 (const unsigned char *) "parent",
246 6)) != 0) {
247 mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000248 goto exit;
249 }
250
Paul Bakker896ac222011-05-20 12:33:05 +0000251 continue;
252 }
253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000255
Janos Follath582a4612016-04-28 23:37:16 +0100256 pid = getpid();
257
Paul Bakker896ac222011-05-20 12:33:05 +0000258 /*
259 * 4. Setup stuff
260 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
262 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg,
265 (const unsigned char *) "child",
266 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100267 mbedtls_printf(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
269 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000270 goto exit;
271 }
Paul Bakker0c226102014-04-17 16:02:36 +0200272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100274 mbedtls_printf(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
276 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200277 goto exit;
278 }
279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200283
Paul Bakker896ac222011-05-20 12:33:05 +0000284 /*
285 * 5. Handshake
286 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
288 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
291 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100292 mbedtls_printf(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
294 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000295 goto exit;
296 }
297 }
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000300
301 /*
302 * 6. Read the HTTP Request
303 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 mbedtls_printf("pid %d: Start reading from client.\n", pid);
305 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 do {
308 len = sizeof(buf) - 1;
309 memset(buf, 0, sizeof(buf));
310 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker896ac222011-05-20 12:33:05 +0000313 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 }
Paul Bakker896ac222011-05-20 12:33:05 +0000315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 if (ret <= 0) {
317 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 mbedtls_printf("pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000320 break;
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 mbedtls_printf("pid %d: connection was reset by peer\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000324 break;
325
326 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000328 break;
329 }
330
331 break;
332 }
333
334 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if (ret > 0) {
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000338 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 }
340 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000341
342 /*
343 * 7. Write the 200 Response
344 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 mbedtls_printf("pid %d: Start writing to client.\n", pid);
346 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 len = sprintf((char *) buf, HTTP_RESPONSE,
349 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 while (cnt++ < 100) {
352 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
353 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100354 mbedtls_printf(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 "pid %d: Write failed! peer closed the connection\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000356 goto exit;
357 }
358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100360 mbedtls_printf(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
362 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000363 goto exit;
364 }
365 }
366 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000370 }
371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000373 goto exit;
374 }
375
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100376 exit_code = MBEDTLS_EXIT_SUCCESS;
377
Paul Bakker896ac222011-05-20 12:33:05 +0000378exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 mbedtls_net_free(&client_fd);
380 mbedtls_net_free(&listen_fd);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 mbedtls_x509_crt_free(&srvcert);
382 mbedtls_pk_free(&pkey);
383 mbedtls_ssl_free(&ssl);
384 mbedtls_ssl_config_free(&conf);
385 mbedtls_ctr_drbg_free(&ctr_drbg);
386 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200387#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200388 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200389#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker896ac222011-05-20 12:33:05 +0000390
Paul Bakkercce9d772011-11-18 14:26:47 +0000391#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 mbedtls_printf(" Press Enter to exit this program.\n");
393 fflush(stdout); getchar();
Paul Bakker896ac222011-05-20 12:33:05 +0000394#endif
395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
399 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
400 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100401 ! _WIN32 */