blob: 37f14607c90a0c14362d1547e893e848a0354ca2 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_fprintf fprintf
28# define mbedtls_printf printf
29# define mbedtls_time_t time_t
30# define mbedtls_exit exit
31# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
32# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010033#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000034
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010035#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
36 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
37 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
38 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
39 !defined(MBEDTLS_TIMING_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_PEM_PARSE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041int main(int argc, char *argv[])
Paul Bakkerb892b132011-10-12 09:19:43 +000042{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043 ((void)argc);
44 ((void)argv);
Paul Bakkercce9d772011-11-18 14:26:47 +000045
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046 mbedtls_printf(
47 "MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
48 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
49 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
50 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
51 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
52 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000053}
Paul Bakkercce9d772011-11-18 14:26:47 +000054#elif defined(_WIN32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055int main(void)
Paul Bakkerb892b132011-10-12 09:19:43 +000056{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057 mbedtls_printf(
58 "_WIN32 defined. This application requires fork() and signals "
59 "to work correctly.\n");
60 mbedtls_exit(0);
Paul Bakkerb892b132011-10-12 09:19:43 +000061}
62#else
Paul Bakker896ac222011-05-20 12:33:05 +000063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# include "mbedtls/entropy.h"
65# include "mbedtls/ctr_drbg.h"
66# include "test/certs.h"
67# include "mbedtls/x509.h"
68# include "mbedtls/ssl.h"
69# include "mbedtls/net_sockets.h"
70# include "mbedtls/timing.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072# include <string.h>
73# include <signal.h>
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010074
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075# if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
76# include <unistd.h>
77# endif
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010078
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079# define HTTP_RESPONSE \
80 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
81 "<h2>mbed TLS Test Server</h2>\r\n" \
82 "<p>Successful connection using: %s</p>\r\n"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010083
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084# define DEBUG_LEVEL 0
Paul Bakker896ac222011-05-20 12:33:05 +000085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086static void
87my_debug(void *ctx, int level, const char *file, int line, const char *str)
Paul Bakker896ac222011-05-20 12:33:05 +000088{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 ((void)level);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091 mbedtls_fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
92 fflush((FILE *)ctx);
Paul Bakker896ac222011-05-20 12:33:05 +000093}
94
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095int main(void)
Paul Bakker896ac222011-05-20 12:33:05 +000096{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010097 int ret = 1, len, cnt = 0, pid;
98 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020099 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000100 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_entropy_context entropy;
104 mbedtls_ctr_drbg_context ctr_drbg;
105 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200106 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_x509_crt srvcert;
108 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000109
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110 mbedtls_net_init(&listen_fd);
111 mbedtls_net_init(&client_fd);
112 mbedtls_ssl_init(&ssl);
113 mbedtls_ssl_config_init(&conf);
114 mbedtls_entropy_init(&entropy);
115 mbedtls_pk_init(&pkey);
116 mbedtls_x509_crt_init(&srvcert);
117 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker0c226102014-04-17 16:02:36 +0200118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119 signal(SIGCHLD, SIG_IGN);
Paul Bakker896ac222011-05-20 12:33:05 +0000120
121 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 * 0. Initial seeding of the RNG
123 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 mbedtls_printf("\n . Initial seeding of the random generator...");
125 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
128 (const unsigned char *)pers,
129 strlen(pers))) != 0) {
130 mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000131 goto exit;
132 }
133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135
136 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000137 * 1. Load the certificates and private RSA key
138 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139 mbedtls_printf(" . Loading the server cert. and key...");
140 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000141
Paul Bakker896ac222011-05-20 12:33:05 +0000142 /*
143 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
145 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000146 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200147 ret = mbedtls_x509_crt_parse(&srvcert,
148 (const unsigned char *)mbedtls_test_srv_crt,
149 mbedtls_test_srv_crt_len);
150 if (ret != 0) {
151 mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000152 goto exit;
153 }
154
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200155 ret = mbedtls_x509_crt_parse(&srvcert,
156 (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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163 ret = mbedtls_pk_parse_key(&pkey,
164 (const unsigned char *)mbedtls_test_srv_key,
165 mbedtls_test_srv_key_len, NULL, 0,
166 mbedtls_ctr_drbg_random, &ctr_drbg);
167 if (ret != 0) {
168 mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000169 goto exit;
170 }
171
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000173
174 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200175 * 1b. Prepare SSL configuration
176 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 mbedtls_printf(" . Configuring SSL...");
178 fflush(stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER,
181 MBEDTLS_SSL_TRANSPORT_STREAM,
182 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
183 mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n",
184 ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200185 goto exit;
186 }
187
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
189 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200190
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191 mbedtls_ssl_conf_ca_chain(&conf, srvcert.MBEDTLS_PRIVATE(next), NULL);
192 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
193 mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n",
194 ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200195 goto exit;
196 }
197
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200199
200 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000201 * 2. Setup the listening TCP socket
202 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
204 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433",
207 MBEDTLS_NET_PROTO_TCP)) != 0) {
208 mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000209 goto exit;
210 }
211
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 mbedtls_printf(" ok\n");
Paul Bakker896ac222011-05-20 12:33:05 +0000213
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214 while (1) {
Paul Bakker896ac222011-05-20 12:33:05 +0000215 /*
216 * 3. Wait until a client connects
217 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218 mbedtls_net_init(&client_fd);
219 mbedtls_ssl_init(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000220
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 mbedtls_printf(" . Waiting for a remote connection ...\n");
222 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000223
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) !=
225 0) {
226 mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000227 goto exit;
228 }
229
Paul Bakker896ac222011-05-20 12:33:05 +0000230 /*
231 * 3.5. Forking server thread
232 */
233
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 mbedtls_printf(" . Forking to handle connection ...");
235 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000236
Janos Follath582a4612016-04-28 23:37:16 +0100237 pid = fork();
238
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239 if (pid < 0) {
240 mbedtls_printf(" failed! fork returned %d\n\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000241 goto exit;
242 }
243
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244 if (pid != 0) {
245 mbedtls_printf(" ok\n");
246 mbedtls_net_close(&client_fd);
Janos Follath582a4612016-04-28 23:37:16 +0100247
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200248 if ((ret = mbedtls_ctr_drbg_reseed(
249 &ctr_drbg, (const unsigned char *)"parent", 6)) != 0) {
250 mbedtls_printf(
251 " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000252 goto exit;
253 }
254
Paul Bakker896ac222011-05-20 12:33:05 +0000255 continue;
256 }
257
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 mbedtls_net_close(&listen_fd);
Paul Bakker896ac222011-05-20 12:33:05 +0000259
Janos Follath582a4612016-04-28 23:37:16 +0100260 pid = getpid();
261
Paul Bakker896ac222011-05-20 12:33:05 +0000262 /*
263 * 4. Setup stuff
264 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265 mbedtls_printf("pid %d: Setting up the SSL data.\n", pid);
266 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000267
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200268 if ((ret = mbedtls_ctr_drbg_reseed(
269 &ctr_drbg, (const unsigned char *)"child", 5)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100270 mbedtls_printf(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200271 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
272 pid, ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000273 goto exit;
274 }
Paul Bakker0c226102014-04-17 16:02:36 +0200275
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
Janos Follath582a4612016-04-28 23:37:16 +0100277 mbedtls_printf(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
279 pid, ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200280 goto exit;
281 }
282
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200283 mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send,
284 mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 mbedtls_printf("pid %d: SSL setup ok\n", pid);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200287
Paul Bakker896ac222011-05-20 12:33:05 +0000288 /*
289 * 5. Handshake
290 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200291 mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid);
292 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000293
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200294 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
295 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
296 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100297 mbedtls_printf(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
299 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000300 goto exit;
301 }
302 }
303
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200304 mbedtls_printf("pid %d: SSL handshake ok\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000305
306 /*
307 * 6. Read the HTTP Request
308 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200309 mbedtls_printf("pid %d: Start reading from client.\n", pid);
310 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000311
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200312 do {
313 len = sizeof(buf) - 1;
314 memset(buf, 0, sizeof(buf));
315 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker896ac222011-05-20 12:33:05 +0000316
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200317 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
318 ret == MBEDTLS_ERR_SSL_WANT_WRITE)
Paul Bakker896ac222011-05-20 12:33:05 +0000319 continue;
320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321 if (ret <= 0) {
322 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200324 mbedtls_printf(
325 "pid %d: connection was closed gracefully\n", pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000326 break;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 case MBEDTLS_ERR_NET_CONN_RESET:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329 mbedtls_printf("pid %d: connection was reset by peer\n",
330 pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000331 break;
332
333 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n",
335 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000336 break;
337 }
338
339 break;
340 }
341
342 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343 mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len,
344 (char *)buf);
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000345
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200346 if (ret > 0)
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000347 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 } while (1);
Paul Bakker896ac222011-05-20 12:33:05 +0000349
350 /*
351 * 7. Write the 200 Response
352 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200353 mbedtls_printf("pid %d: Start writing to client.\n", pid);
354 fflush(stdout);
Paul Bakker896ac222011-05-20 12:33:05 +0000355
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200356 len = sprintf((char *)buf, HTTP_RESPONSE,
357 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker896ac222011-05-20 12:33:05 +0000358
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200359 while (cnt++ < 100) {
360 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
361 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
Janos Follath582a4612016-04-28 23:37:16 +0100362 mbedtls_printf(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363 "pid %d: Write failed! peer closed the connection\n\n",
364 pid);
Paul Bakker896ac222011-05-20 12:33:05 +0000365 goto exit;
366 }
367
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200368 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
369 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Janos Follath582a4612016-04-28 23:37:16 +0100370 mbedtls_printf(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
372 pid, ret);
Paul Bakker896ac222011-05-20 12:33:05 +0000373 goto exit;
374 }
375 }
376 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200377 mbedtls_printf("pid %d: %d bytes written\n\n%s\n", pid, len,
378 (char *)buf);
Paul Bakker896ac222011-05-20 12:33:05 +0000379
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380 mbedtls_net_usleep(1000000);
Paul Bakker896ac222011-05-20 12:33:05 +0000381 }
382
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383 mbedtls_ssl_close_notify(&ssl);
Paul Bakker896ac222011-05-20 12:33:05 +0000384 goto exit;
385 }
386
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100387 exit_code = MBEDTLS_EXIT_SUCCESS;
388
Paul Bakker896ac222011-05-20 12:33:05 +0000389exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390 mbedtls_net_free(&client_fd);
391 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200392
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200393 mbedtls_x509_crt_free(&srvcert);
394 mbedtls_pk_free(&pkey);
395 mbedtls_ssl_free(&ssl);
396 mbedtls_ssl_config_free(&conf);
397 mbedtls_ctr_drbg_free(&ctr_drbg);
398 mbedtls_entropy_free(&entropy);
Paul Bakker896ac222011-05-20 12:33:05 +0000399
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400# if defined(_WIN32)
401 mbedtls_printf(" Press Enter to exit this program.\n");
402 fflush(stdout);
403 getchar();
404# endif
Paul Bakker896ac222011-05-20 12:33:05 +0000405
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200406 mbedtls_exit(exit_code);
Paul Bakker896ac222011-05-20 12:33:05 +0000407}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && \
409 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && \
410 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C && \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100411 ! _WIN32 */