blob: 49de984d8f898367f0ec0f5649f2a7c9a122594a [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)
Paul Bakkercce9d772011-11-18 14:26:47 +000030int 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 "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 "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");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020040 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000041}
Paul Bakkercce9d772011-11-18 14:26:47 +000042#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000043int 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 "
Paul Bakkerb892b132011-10-12 09:19:43 +000046 "to work correctly.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020047 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
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020074static 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
80 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
81 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000082}
83
Rich Evans85b05ec2015-02-12 11:37:29 +000084int 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
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020099 mbedtls_net_init( &listen_fd );
100 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200101 mbedtls_ssl_init( &ssl );
102 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_entropy_init( &entropy );
104 mbedtls_pk_init( &pkey );
105 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200106 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200107
Paul Bakker896ac222011-05-20 12:33:05 +0000108 signal( SIGCHLD, SIG_IGN );
109
110 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111 * 0. Initial seeding of the RNG
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114 fflush( stdout );
115
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200116 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200117 (const unsigned char *) pers,
118 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119 {
Janos Follath582a4612016-04-28 23:37:16 +0100120 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121 goto exit;
122 }
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125
126 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000127 * 1. Load the certificates and private RSA key
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000130 fflush( stdout );
131
Paul Bakker896ac222011-05-20 12:33:05 +0000132 /*
133 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
135 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
138 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000139 if( ret != 0 )
140 {
Janos Follath582a4612016-04-28 23:37:16 +0100141 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000142 goto exit;
143 }
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
146 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000147 if( ret != 0 )
148 {
Janos Follath582a4612016-04-28 23:37:16 +0100149 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000150 goto exit;
151 }
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200154 mbedtls_test_srv_key_len, NULL, 0,
155 mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000156 if( ret != 0 )
157 {
Janos Follath582a4612016-04-28 23:37:16 +0100158 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000163
164 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200165 * 1b. Prepare SSL configuration
166 */
167 mbedtls_printf( " . Configuring SSL..." );
168 fflush( stdout );
169
170 if( ( ret = mbedtls_ssl_config_defaults( &conf,
171 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200172 MBEDTLS_SSL_TRANSPORT_STREAM,
173 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200174 {
Janos Follath582a4612016-04-28 23:37:16 +0100175 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200176 goto exit;
177 }
178
179 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
180 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
181
Gilles Peskineca939952021-08-31 23:18:07 +0200182 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200183 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
184 {
Janos Follath582a4612016-04-28 23:37:16 +0100185 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200186 goto exit;
187 }
188
189 mbedtls_printf( " ok\n" );
190
191 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000192 * 2. Setup the listening TCP socket
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000195 fflush( stdout );
196
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200197 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000198 {
Janos Follath582a4612016-04-28 23:37:16 +0100199 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000200 goto exit;
201 }
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000204
205 while( 1 )
206 {
207 /*
208 * 3. Wait until a client connects
209 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200210 mbedtls_net_init( &client_fd );
211 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000212
Janos Follath582a4612016-04-28 23:37:16 +0100213 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000214 fflush( stdout );
215
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200216 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200217 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000218 {
Janos Follath582a4612016-04-28 23:37:16 +0100219 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000220 goto exit;
221 }
222
Paul Bakker896ac222011-05-20 12:33:05 +0000223 /*
224 * 3.5. Forking server thread
225 */
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000228 fflush( stdout );
229
Janos Follath582a4612016-04-28 23:37:16 +0100230 pid = fork();
231
Paul Bakker896ac222011-05-20 12:33:05 +0000232 if( pid < 0 )
233 {
Janos Follath582a4612016-04-28 23:37:16 +0100234 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000235 goto exit;
236 }
237
Paul Bakker896ac222011-05-20 12:33:05 +0000238 if( pid != 0 )
239 {
Janos Follath582a4612016-04-28 23:37:16 +0100240 mbedtls_printf( " ok\n" );
Robert Larsendf8e5112019-08-23 10:55:47 +0200241 mbedtls_net_close( &client_fd );
Janos Follath582a4612016-04-28 23:37:16 +0100242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200244 (const unsigned char *) "parent",
245 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000246 {
Janos Follath582a4612016-04-28 23:37:16 +0100247 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
Robert Larsendf8e5112019-08-23 10:55:47 +0200254 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 */
Janos Follath582a4612016-04-28 23:37:16 +0100261 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000262 fflush( stdout );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200265 (const unsigned char *) "child",
266 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000267 {
Janos Follath582a4612016-04-28 23:37:16 +0100268 mbedtls_printf(
269 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
270 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000271 goto exit;
272 }
Paul Bakker0c226102014-04-17 16:02:36 +0200273
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200274 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
275 {
Janos Follath582a4612016-04-28 23:37:16 +0100276 mbedtls_printf(
277 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
278 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200279 goto exit;
280 }
281
282 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
283
Janos Follath582a4612016-04-28 23:37:16 +0100284 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200285
Paul Bakker896ac222011-05-20 12:33:05 +0000286 /*
287 * 5. Handshake
288 */
Janos Follath582a4612016-04-28 23:37:16 +0100289 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000290 fflush( stdout );
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000293 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100294 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000295 {
Janos Follath582a4612016-04-28 23:37:16 +0100296 mbedtls_printf(
297 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
298 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000299 goto exit;
300 }
301 }
302
Janos Follath582a4612016-04-28 23:37:16 +0100303 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000304
305 /*
306 * 6. Read the HTTP Request
307 */
Janos Follath582a4612016-04-28 23:37:16 +0100308 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000309 fflush( stdout );
310
311 do
312 {
313 len = sizeof( buf ) - 1;
314 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000316
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100317 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000318 continue;
319
320 if( ret <= 0 )
321 {
322 switch( ret )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100325 mbedtls_printf( "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:
Janos Follath582a4612016-04-28 23:37:16 +0100329 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000330 break;
331
332 default:
Janos Follath582a4612016-04-28 23:37:16 +0100333 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000334 break;
335 }
336
337 break;
338 }
339
340 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100341 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000342
343 if( ret > 0 )
344 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000345 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000346 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000347
348 /*
349 * 7. Write the 200 Response
350 */
Janos Follath582a4612016-04-28 23:37:16 +0100351 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000352 fflush( stdout );
353
354 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000356
Paul Bakker030decd2014-04-17 16:03:23 +0200357 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000362 {
Janos Follath582a4612016-04-28 23:37:16 +0100363 mbedtls_printf(
364 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000365 goto exit;
366 }
367
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100368 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000369 {
Janos Follath582a4612016-04-28 23:37:16 +0100370 mbedtls_printf(
371 "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;
Janos Follath582a4612016-04-28 23:37:16 +0100377 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000378
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200379 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000380 }
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000383 goto exit;
384 }
385
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100386 exit_code = MBEDTLS_EXIT_SUCCESS;
387
Paul Bakker896ac222011-05-20 12:33:05 +0000388exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200389 mbedtls_net_free( &client_fd );
390 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_x509_crt_free( &srvcert );
393 mbedtls_pk_free( &pkey );
394 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200395 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ctr_drbg_free( &ctr_drbg );
397 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000398
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200399 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000400}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100401#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
403 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100404 ! _WIN32 */