blob: 74190103e2dc0468a86584f0515191a4fe5a776b [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010030#include <stdlib.h>
31#define mbedtls_fprintf fprintf
32#define mbedtls_printf printf
33#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010039#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
40 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
41 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
42 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
43 !defined(MBEDTLS_TIMING_C) || !defined(MBEDTLS_FS_IO) || \
44 !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010050 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
52 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
53 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
54 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020055 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000056}
Paul Bakkercce9d772011-11-18 14:26:47 +000057#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000058int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000061 "to work correctly.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020062 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000063}
64#else
Paul Bakker896ac222011-05-20 12:33:05 +000065
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010066#include "mbedtls/entropy.h"
67#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010068#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010069#include "mbedtls/x509.h"
70#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010071#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010072#include "mbedtls/timing.h"
73
74#include <string.h>
75#include <signal.h>
76
77#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
78#include <unistd.h>
79#endif
80
81#define HTTP_RESPONSE \
82 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
83 "<h2>mbed TLS Test Server</h2>\r\n" \
84 "<p>Successful connection using: %s</p>\r\n"
85
Paul Bakker896ac222011-05-20 12:33:05 +000086#define DEBUG_LEVEL 0
87
Simon Butcher63cb97e2018-12-06 17:43:31 +000088
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020089static void my_debug( void *ctx, int level,
90 const char *file, int line,
91 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000092{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020093 ((void) level);
94
95 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
96 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000097}
98
Rich Evans85b05ec2015-02-12 11:37:29 +000099int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000100{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100101 int ret = 1, len, cnt = 0, pid;
102 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200103 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000104 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200105 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_entropy_context entropy;
108 mbedtls_ctr_drbg_context ctr_drbg;
109 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200110 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_x509_crt srvcert;
112 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000113
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200114 mbedtls_net_init( &listen_fd );
115 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200116 mbedtls_ssl_init( &ssl );
117 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_entropy_init( &entropy );
119 mbedtls_pk_init( &pkey );
120 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200121 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200122
Paul Bakker896ac222011-05-20 12:33:05 +0000123 signal( SIGCHLD, SIG_IGN );
124
125 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000126 * 0. Initial seeding of the RNG
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129 fflush( stdout );
130
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200131 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200132 (const unsigned char *) pers,
133 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000134 {
Janos Follath582a4612016-04-28 23:37:16 +0100135 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136 goto exit;
137 }
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000140
141 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000142 * 1. Load the certificates and private RSA key
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000145 fflush( stdout );
146
Paul Bakker896ac222011-05-20 12:33:05 +0000147 /*
148 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
150 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
153 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000154 if( ret != 0 )
155 {
Janos Follath582a4612016-04-28 23:37:16 +0100156 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000157 goto exit;
158 }
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
161 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000162 if( ret != 0 )
163 {
Janos Follath582a4612016-04-28 23:37:16 +0100164 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000165 goto exit;
166 }
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200169 mbedtls_test_srv_key_len, NULL, 0,
170 mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000171 if( ret != 0 )
172 {
Janos Follath582a4612016-04-28 23:37:16 +0100173 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000174 goto exit;
175 }
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000178
179 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200180 * 1b. Prepare SSL configuration
181 */
182 mbedtls_printf( " . Configuring SSL..." );
183 fflush( stdout );
184
185 if( ( ret = mbedtls_ssl_config_defaults( &conf,
186 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200187 MBEDTLS_SSL_TRANSPORT_STREAM,
188 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200189 {
Janos Follath582a4612016-04-28 23:37:16 +0100190 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200191 goto exit;
192 }
193
194 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
195 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
196
Mateusz Starzyk55768f22021-05-27 14:57:03 +0200197 mbedtls_ssl_conf_ca_chain( &conf, srvcert.MBEDTLS_PRIVATE(next), NULL );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200198 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
199 {
Janos Follath582a4612016-04-28 23:37:16 +0100200 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200201 goto exit;
202 }
203
204 mbedtls_printf( " ok\n" );
205
206 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000207 * 2. Setup the listening TCP socket
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000210 fflush( stdout );
211
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200212 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000213 {
Janos Follath582a4612016-04-28 23:37:16 +0100214 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000215 goto exit;
216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000219
220 while( 1 )
221 {
222 /*
223 * 3. Wait until a client connects
224 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200225 mbedtls_net_init( &client_fd );
226 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000227
Janos Follath582a4612016-04-28 23:37:16 +0100228 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000229 fflush( stdout );
230
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200231 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200232 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000233 {
Janos Follath582a4612016-04-28 23:37:16 +0100234 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000235 goto exit;
236 }
237
Paul Bakker896ac222011-05-20 12:33:05 +0000238 /*
239 * 3.5. Forking server thread
240 */
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000243 fflush( stdout );
244
Janos Follath582a4612016-04-28 23:37:16 +0100245 pid = fork();
246
Paul Bakker896ac222011-05-20 12:33:05 +0000247 if( pid < 0 )
248 {
Janos Follath582a4612016-04-28 23:37:16 +0100249 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000250 goto exit;
251 }
252
Paul Bakker896ac222011-05-20 12:33:05 +0000253 if( pid != 0 )
254 {
Janos Follath582a4612016-04-28 23:37:16 +0100255 mbedtls_printf( " ok\n" );
Robert Larsendf8e5112019-08-23 10:55:47 +0200256 mbedtls_net_close( &client_fd );
Janos Follath582a4612016-04-28 23:37:16 +0100257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200259 (const unsigned char *) "parent",
260 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000261 {
Janos Follath582a4612016-04-28 23:37:16 +0100262 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000263 goto exit;
264 }
265
Paul Bakker896ac222011-05-20 12:33:05 +0000266 continue;
267 }
268
Robert Larsendf8e5112019-08-23 10:55:47 +0200269 mbedtls_net_close( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000270
Janos Follath582a4612016-04-28 23:37:16 +0100271 pid = getpid();
272
Paul Bakker896ac222011-05-20 12:33:05 +0000273 /*
274 * 4. Setup stuff
275 */
Janos Follath582a4612016-04-28 23:37:16 +0100276 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000277 fflush( stdout );
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200280 (const unsigned char *) "child",
281 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000282 {
Janos Follath582a4612016-04-28 23:37:16 +0100283 mbedtls_printf(
284 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
285 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000286 goto exit;
287 }
Paul Bakker0c226102014-04-17 16:02:36 +0200288
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200289 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
290 {
Janos Follath582a4612016-04-28 23:37:16 +0100291 mbedtls_printf(
292 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
293 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200294 goto exit;
295 }
296
297 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
298
Janos Follath582a4612016-04-28 23:37:16 +0100299 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200300
Paul Bakker896ac222011-05-20 12:33:05 +0000301 /*
302 * 5. Handshake
303 */
Janos Follath582a4612016-04-28 23:37:16 +0100304 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000305 fflush( stdout );
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000308 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100309 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000310 {
Janos Follath582a4612016-04-28 23:37:16 +0100311 mbedtls_printf(
312 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
313 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000314 goto exit;
315 }
316 }
317
Janos Follath582a4612016-04-28 23:37:16 +0100318 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000319
320 /*
321 * 6. Read the HTTP Request
322 */
Janos Follath582a4612016-04-28 23:37:16 +0100323 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000324 fflush( stdout );
325
326 do
327 {
328 len = sizeof( buf ) - 1;
329 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000331
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100332 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000333 continue;
334
335 if( ret <= 0 )
336 {
337 switch( ret )
338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100340 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000341 break;
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100344 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000345 break;
346
347 default:
Janos Follath582a4612016-04-28 23:37:16 +0100348 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000349 break;
350 }
351
352 break;
353 }
354
355 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100356 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000357
358 if( ret > 0 )
359 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000360 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000361 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000362
363 /*
364 * 7. Write the 200 Response
365 */
Janos Follath582a4612016-04-28 23:37:16 +0100366 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000367 fflush( stdout );
368
369 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000371
Paul Bakker030decd2014-04-17 16:03:23 +0200372 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000377 {
Janos Follath582a4612016-04-28 23:37:16 +0100378 mbedtls_printf(
379 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000380 goto exit;
381 }
382
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100383 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000384 {
Janos Follath582a4612016-04-28 23:37:16 +0100385 mbedtls_printf(
386 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
387 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000388 goto exit;
389 }
390 }
391 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100392 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000393
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200394 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000395 }
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000398 goto exit;
399 }
400
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100401 exit_code = MBEDTLS_EXIT_SUCCESS;
402
Paul Bakker896ac222011-05-20 12:33:05 +0000403exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200404 mbedtls_net_free( &client_fd );
405 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_x509_crt_free( &srvcert );
408 mbedtls_pk_free( &pkey );
409 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200410 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_ctr_drbg_free( &ctr_drbg );
412 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000413
Paul Bakkercce9d772011-11-18 14:26:47 +0000414#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000416 fflush( stdout ); getchar();
417#endif
418
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200419 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000420}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100421#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
423 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100424 ! _WIN32 */