blob: 694fc3b7ae6125aba69d4026da9e3168a57d7f1b [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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010026#include <stdlib.h>
27#define mbedtls_fprintf fprintf
28#define mbedtls_printf printf
29#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010030#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010031#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010032#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
33#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)
Paul Bakkercce9d772011-11-18 14:26:47 +000041int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000042{
Paul Bakkercce9d772011-11-18 14:26:47 +000043 ((void) argc);
44 ((void) argv);
45
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010046 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
48 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
49 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
50 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020051 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000052}
Paul Bakkercce9d772011-11-18 14:26:47 +000053#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000054int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000057 "to work correctly.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020058 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000059}
60#else
Paul Bakker896ac222011-05-20 12:33:05 +000061
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010062#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010064#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010065#include "mbedtls/x509.h"
66#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010067#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010068#include "mbedtls/timing.h"
69
70#include <string.h>
71#include <signal.h>
72
73#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
74#include <unistd.h>
75#endif
76
77#define HTTP_RESPONSE \
78 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
79 "<h2>mbed TLS Test Server</h2>\r\n" \
80 "<p>Successful connection using: %s</p>\r\n"
81
Paul Bakker896ac222011-05-20 12:33:05 +000082#define DEBUG_LEVEL 0
83
Simon Butcher63cb97e2018-12-06 17:43:31 +000084
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020085static void my_debug( void *ctx, int level,
86 const char *file, int line,
87 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000088{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020089 ((void) level);
90
91 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
92 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000093}
94
Rich Evans85b05ec2015-02-12 11:37:29 +000095int 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
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200110 mbedtls_net_init( &listen_fd );
111 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200112 mbedtls_ssl_init( &ssl );
113 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_entropy_init( &entropy );
115 mbedtls_pk_init( &pkey );
116 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200117 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200118
Paul Bakker896ac222011-05-20 12:33:05 +0000119 signal( SIGCHLD, SIG_IGN );
120
121 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 * 0. Initial seeding of the RNG
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 fflush( stdout );
126
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200127 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200128 (const unsigned char *) pers,
129 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000130 {
Janos Follath582a4612016-04-28 23:37:16 +0100131 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136
137 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000138 * 1. Load the certificates and private RSA key
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000141 fflush( stdout );
142
Paul Bakker896ac222011-05-20 12:33:05 +0000143 /*
144 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
146 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
149 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000150 if( ret != 0 )
151 {
Janos Follath582a4612016-04-28 23:37:16 +0100152 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000153 goto exit;
154 }
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
157 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000158 if( ret != 0 )
159 {
Janos Follath582a4612016-04-28 23:37:16 +0100160 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000161 goto exit;
162 }
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200165 mbedtls_test_srv_key_len, NULL, 0,
166 mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000167 if( ret != 0 )
168 {
Janos Follath582a4612016-04-28 23:37:16 +0100169 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000170 goto exit;
171 }
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000174
175 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200176 * 1b. Prepare SSL configuration
177 */
178 mbedtls_printf( " . Configuring SSL..." );
179 fflush( stdout );
180
181 if( ( ret = mbedtls_ssl_config_defaults( &conf,
182 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200183 MBEDTLS_SSL_TRANSPORT_STREAM,
184 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200185 {
Janos Follath582a4612016-04-28 23:37:16 +0100186 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200187 goto exit;
188 }
189
190 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
191 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
192
Gilles Peskineca939952021-08-31 23:18:07 +0200193 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200194 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
195 {
Janos Follath582a4612016-04-28 23:37:16 +0100196 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200197 goto exit;
198 }
199
200 mbedtls_printf( " ok\n" );
201
202 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000203 * 2. Setup the listening TCP socket
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000206 fflush( stdout );
207
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200208 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000209 {
Janos Follath582a4612016-04-28 23:37:16 +0100210 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000211 goto exit;
212 }
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000215
216 while( 1 )
217 {
218 /*
219 * 3. Wait until a client connects
220 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200221 mbedtls_net_init( &client_fd );
222 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000223
Janos Follath582a4612016-04-28 23:37:16 +0100224 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000225 fflush( stdout );
226
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200227 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200228 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000229 {
Janos Follath582a4612016-04-28 23:37:16 +0100230 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000231 goto exit;
232 }
233
Paul Bakker896ac222011-05-20 12:33:05 +0000234 /*
235 * 3.5. Forking server thread
236 */
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000239 fflush( stdout );
240
Janos Follath582a4612016-04-28 23:37:16 +0100241 pid = fork();
242
Paul Bakker896ac222011-05-20 12:33:05 +0000243 if( pid < 0 )
244 {
Janos Follath582a4612016-04-28 23:37:16 +0100245 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000246 goto exit;
247 }
248
Paul Bakker896ac222011-05-20 12:33:05 +0000249 if( pid != 0 )
250 {
Janos Follath582a4612016-04-28 23:37:16 +0100251 mbedtls_printf( " ok\n" );
Robert Larsendf8e5112019-08-23 10:55:47 +0200252 mbedtls_net_close( &client_fd );
Janos Follath582a4612016-04-28 23:37:16 +0100253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200255 (const unsigned char *) "parent",
256 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000257 {
Janos Follath582a4612016-04-28 23:37:16 +0100258 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000259 goto exit;
260 }
261
Paul Bakker896ac222011-05-20 12:33:05 +0000262 continue;
263 }
264
Robert Larsendf8e5112019-08-23 10:55:47 +0200265 mbedtls_net_close( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000266
Janos Follath582a4612016-04-28 23:37:16 +0100267 pid = getpid();
268
Paul Bakker896ac222011-05-20 12:33:05 +0000269 /*
270 * 4. Setup stuff
271 */
Janos Follath582a4612016-04-28 23:37:16 +0100272 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000273 fflush( stdout );
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200276 (const unsigned char *) "child",
277 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000278 {
Janos Follath582a4612016-04-28 23:37:16 +0100279 mbedtls_printf(
280 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
281 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000282 goto exit;
283 }
Paul Bakker0c226102014-04-17 16:02:36 +0200284
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200285 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
286 {
Janos Follath582a4612016-04-28 23:37:16 +0100287 mbedtls_printf(
288 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
289 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200290 goto exit;
291 }
292
293 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
294
Janos Follath582a4612016-04-28 23:37:16 +0100295 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200296
Paul Bakker896ac222011-05-20 12:33:05 +0000297 /*
298 * 5. Handshake
299 */
Janos Follath582a4612016-04-28 23:37:16 +0100300 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000301 fflush( stdout );
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000304 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100305 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000306 {
Janos Follath582a4612016-04-28 23:37:16 +0100307 mbedtls_printf(
308 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
309 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000310 goto exit;
311 }
312 }
313
Janos Follath582a4612016-04-28 23:37:16 +0100314 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000315
316 /*
317 * 6. Read the HTTP Request
318 */
Janos Follath582a4612016-04-28 23:37:16 +0100319 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000320 fflush( stdout );
321
322 do
323 {
324 len = sizeof( buf ) - 1;
325 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000327
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100328 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000329 continue;
330
331 if( ret <= 0 )
332 {
333 switch( ret )
334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100336 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000337 break;
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100340 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000341 break;
342
343 default:
Janos Follath582a4612016-04-28 23:37:16 +0100344 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000345 break;
346 }
347
348 break;
349 }
350
351 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100352 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000353
354 if( ret > 0 )
355 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000356 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000357 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000358
359 /*
360 * 7. Write the 200 Response
361 */
Janos Follath582a4612016-04-28 23:37:16 +0100362 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000363 fflush( stdout );
364
365 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000367
Paul Bakker030decd2014-04-17 16:03:23 +0200368 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000373 {
Janos Follath582a4612016-04-28 23:37:16 +0100374 mbedtls_printf(
375 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000376 goto exit;
377 }
378
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100379 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000380 {
Janos Follath582a4612016-04-28 23:37:16 +0100381 mbedtls_printf(
382 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
383 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000384 goto exit;
385 }
386 }
387 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100388 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000389
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200390 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000391 }
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000394 goto exit;
395 }
396
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100397 exit_code = MBEDTLS_EXIT_SUCCESS;
398
Paul Bakker896ac222011-05-20 12:33:05 +0000399exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200400 mbedtls_net_free( &client_fd );
401 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crt_free( &srvcert );
404 mbedtls_pk_free( &pkey );
405 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200406 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_ctr_drbg_free( &ctr_drbg );
408 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000409
Paul Bakkercce9d772011-11-18 14:26:47 +0000410#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000412 fflush( stdout ); getchar();
413#endif
414
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200415 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000416}
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100417#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
419 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100420 ! _WIN32 */