blob: 3304b6bb1458b57998e2ea3b4b917f8cd80abd46 [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker896ac222011-05-20 12:33:05 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker896ac222011-05-20 12:33:05 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
35#define mbedtls_time_t time_t
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010036#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010037#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
42 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
43 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
44 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
45 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
52 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
53 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
54 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
55 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000056 return( 0 );
57}
Paul Bakkercce9d772011-11-18 14:26:47 +000058#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000059int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000062 "to work correctly.\n");
63 return( 0 );
64}
65#else
Paul Bakker896ac222011-05-20 12:33:05 +000066
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010067#include "mbedtls/entropy.h"
68#include "mbedtls/ctr_drbg.h"
69#include "mbedtls/certs.h"
70#include "mbedtls/x509.h"
71#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010072#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010073#include "mbedtls/timing.h"
74
75#include <string.h>
76#include <signal.h>
77
78#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
79#include <unistd.h>
80#endif
81
82#define HTTP_RESPONSE \
83 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
84 "<h2>mbed TLS Test Server</h2>\r\n" \
85 "<p>Successful connection using: %s</p>\r\n"
86
Paul Bakker896ac222011-05-20 12:33:05 +000087#define DEBUG_LEVEL 0
88
Simon Butcher63cb97e2018-12-06 17:43:31 +000089#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
90void mbedtls_param_failed( char* failure_condition, char* file, int line )
91{
92 mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition );
93 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
94}
95#endif
96
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020097static void my_debug( void *ctx, int level,
98 const char *file, int line,
99 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +0000100{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200101 ((void) level);
102
103 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
104 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000105}
106
Rich Evans85b05ec2015-02-12 11:37:29 +0000107int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000108{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100109 int ret = 1, len, cnt = 0, pid;
110 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200111 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000112 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200113 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
117 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200118 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_x509_crt srvcert;
120 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000121
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200122 mbedtls_net_init( &listen_fd );
123 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200124 mbedtls_ssl_init( &ssl );
125 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_entropy_init( &entropy );
127 mbedtls_pk_init( &pkey );
128 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200129 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200130
Paul Bakker896ac222011-05-20 12:33:05 +0000131 signal( SIGCHLD, SIG_IGN );
132
133 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000134 * 0. Initial seeding of the RNG
135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000137 fflush( stdout );
138
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200139 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200140 (const unsigned char *) pers,
141 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000142 {
Janos Follath582a4612016-04-28 23:37:16 +0100143 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000144 goto exit;
145 }
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000148
149 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000150 * 1. Load the certificates and private RSA key
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000153 fflush( stdout );
154
Paul Bakker896ac222011-05-20 12:33:05 +0000155 /*
156 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
158 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
161 mbedtls_test_srv_crt_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_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
169 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000170 if( ret != 0 )
171 {
Janos Follath582a4612016-04-28 23:37:16 +0100172 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000173 goto exit;
174 }
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
177 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000178 if( ret != 0 )
179 {
Janos Follath582a4612016-04-28 23:37:16 +0100180 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000181 goto exit;
182 }
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000185
186 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200187 * 1b. Prepare SSL configuration
188 */
189 mbedtls_printf( " . Configuring SSL..." );
190 fflush( stdout );
191
192 if( ( ret = mbedtls_ssl_config_defaults( &conf,
193 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200194 MBEDTLS_SSL_TRANSPORT_STREAM,
195 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200196 {
Janos Follath582a4612016-04-28 23:37:16 +0100197 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200198 goto exit;
199 }
200
201 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
202 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
203
204 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
205 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
206 {
Janos Follath582a4612016-04-28 23:37:16 +0100207 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200208 goto exit;
209 }
210
211 mbedtls_printf( " ok\n" );
212
213 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000214 * 2. Setup the listening TCP socket
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000217 fflush( stdout );
218
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200219 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000220 {
Janos Follath582a4612016-04-28 23:37:16 +0100221 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000222 goto exit;
223 }
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000226
227 while( 1 )
228 {
229 /*
230 * 3. Wait until a client connects
231 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200232 mbedtls_net_init( &client_fd );
233 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000234
Janos Follath582a4612016-04-28 23:37:16 +0100235 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000236 fflush( stdout );
237
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200238 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200239 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000240 {
Janos Follath582a4612016-04-28 23:37:16 +0100241 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000242 goto exit;
243 }
244
Paul Bakker896ac222011-05-20 12:33:05 +0000245 /*
246 * 3.5. Forking server thread
247 */
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000250 fflush( stdout );
251
Janos Follath582a4612016-04-28 23:37:16 +0100252 pid = fork();
253
Paul Bakker896ac222011-05-20 12:33:05 +0000254 if( pid < 0 )
255 {
Janos Follath582a4612016-04-28 23:37:16 +0100256 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000257 goto exit;
258 }
259
Paul Bakker896ac222011-05-20 12:33:05 +0000260 if( pid != 0 )
261 {
Janos Follath582a4612016-04-28 23:37:16 +0100262 mbedtls_printf( " ok\n" );
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 *) "parent",
266 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000267 {
Janos Follath582a4612016-04-28 23:37:16 +0100268 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000269 goto exit;
270 }
271
Paul Bakker896ac222011-05-20 12:33:05 +0000272 continue;
273 }
274
Janos Follathfe049db2016-03-31 11:37:33 +0100275 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000276
Janos Follath582a4612016-04-28 23:37:16 +0100277 pid = getpid();
278
Paul Bakker896ac222011-05-20 12:33:05 +0000279 /*
280 * 4. Setup stuff
281 */
Janos Follath582a4612016-04-28 23:37:16 +0100282 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000283 fflush( stdout );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200286 (const unsigned char *) "child",
287 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000288 {
Janos Follath582a4612016-04-28 23:37:16 +0100289 mbedtls_printf(
290 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
291 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000292 goto exit;
293 }
Paul Bakker0c226102014-04-17 16:02:36 +0200294
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200295 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
296 {
Janos Follath582a4612016-04-28 23:37:16 +0100297 mbedtls_printf(
298 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
299 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200300 goto exit;
301 }
302
303 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
304
Janos Follath582a4612016-04-28 23:37:16 +0100305 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200306
Paul Bakker896ac222011-05-20 12:33:05 +0000307 /*
308 * 5. Handshake
309 */
Janos Follath582a4612016-04-28 23:37:16 +0100310 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000311 fflush( stdout );
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000314 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100315 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000316 {
Janos Follath582a4612016-04-28 23:37:16 +0100317 mbedtls_printf(
318 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
319 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000320 goto exit;
321 }
322 }
323
Janos Follath582a4612016-04-28 23:37:16 +0100324 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000325
326 /*
327 * 6. Read the HTTP Request
328 */
Janos Follath582a4612016-04-28 23:37:16 +0100329 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000330 fflush( stdout );
331
332 do
333 {
334 len = sizeof( buf ) - 1;
335 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000337
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100338 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000339 continue;
340
341 if( ret <= 0 )
342 {
343 switch( ret )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100346 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000347 break;
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100350 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000351 break;
352
353 default:
Janos Follath582a4612016-04-28 23:37:16 +0100354 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000355 break;
356 }
357
358 break;
359 }
360
361 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100362 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000363
364 if( ret > 0 )
365 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000366 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000367 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000368
369 /*
370 * 7. Write the 200 Response
371 */
Janos Follath582a4612016-04-28 23:37:16 +0100372 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000373 fflush( stdout );
374
375 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000377
Paul Bakker030decd2014-04-17 16:03:23 +0200378 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000383 {
Janos Follath582a4612016-04-28 23:37:16 +0100384 mbedtls_printf(
385 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000386 goto exit;
387 }
388
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100389 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000390 {
Janos Follath582a4612016-04-28 23:37:16 +0100391 mbedtls_printf(
392 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
393 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000394 goto exit;
395 }
396 }
397 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100398 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000399
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200400 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000401 }
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000404 goto exit;
405 }
406
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100407 exit_code = MBEDTLS_EXIT_SUCCESS;
408
Paul Bakker896ac222011-05-20 12:33:05 +0000409exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200410 mbedtls_net_free( &client_fd );
411 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_x509_crt_free( &srvcert );
414 mbedtls_pk_free( &pkey );
415 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200416 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_ctr_drbg_free( &ctr_drbg );
418 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000419
Paul Bakkercce9d772011-11-18 14:26:47 +0000420#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000422 fflush( stdout ); getchar();
423#endif
424
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100425 return( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
428 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
429 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100430 ! _WIN32 */