blob: 58c8749daa67b49061f3079348ee4ff3a84925b0 [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é-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
29 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
30 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
31 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
32 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
33 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000034int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000035{
Paul Bakkercce9d772011-11-18 14:26:47 +000036 ((void) argc);
37 ((void) argv);
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
40 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
41 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
42 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
43 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020044 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000045}
Paul Bakkercce9d772011-11-18 14:26:47 +000046#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000047int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000050 "to work correctly.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020051 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000052}
53#else
Paul Bakker896ac222011-05-20 12:33:05 +000054
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010055#include "mbedtls/entropy.h"
56#include "mbedtls/ctr_drbg.h"
57#include "mbedtls/certs.h"
58#include "mbedtls/x509.h"
59#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010060#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010061#include "mbedtls/timing.h"
62
63#include <string.h>
64#include <signal.h>
65
66#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
67#include <unistd.h>
68#endif
69
70#define HTTP_RESPONSE \
71 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
72 "<h2>mbed TLS Test Server</h2>\r\n" \
73 "<p>Successful connection using: %s</p>\r\n"
74
Paul Bakker896ac222011-05-20 12:33:05 +000075#define DEBUG_LEVEL 0
76
Simon Butcher63cb97e2018-12-06 17:43:31 +000077
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020078static void my_debug( void *ctx, int level,
79 const char *file, int line,
80 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000081{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020082 ((void) level);
83
84 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
85 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000086}
87
Rich Evans85b05ec2015-02-12 11:37:29 +000088int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +000089{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010090 int ret = 1, len, cnt = 0, pid;
91 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020092 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000093 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_entropy_context entropy;
97 mbedtls_ctr_drbg_context ctr_drbg;
98 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020099 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_crt srvcert;
101 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000102
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200103 mbedtls_net_init( &listen_fd );
104 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200105 mbedtls_ssl_init( &ssl );
106 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_entropy_init( &entropy );
108 mbedtls_pk_init( &pkey );
109 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200110 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200111
Paul Bakker896ac222011-05-20 12:33:05 +0000112 signal( SIGCHLD, SIG_IGN );
113
114 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 * 0. Initial seeding of the RNG
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000118 fflush( stdout );
119
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200120 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200121 (const unsigned char *) pers,
122 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123 {
Janos Follath582a4612016-04-28 23:37:16 +0100124 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 goto exit;
126 }
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129
130 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000131 * 1. Load the certificates and private RSA key
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000134 fflush( stdout );
135
Paul Bakker896ac222011-05-20 12:33:05 +0000136 /*
137 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
139 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
142 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000143 if( ret != 0 )
144 {
Janos Follath582a4612016-04-28 23:37:16 +0100145 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000146 goto exit;
147 }
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
150 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000151 if( ret != 0 )
152 {
Janos Follath582a4612016-04-28 23:37:16 +0100153 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000154 goto exit;
155 }
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
158 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000159 if( ret != 0 )
160 {
Janos Follath582a4612016-04-28 23:37:16 +0100161 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000162 goto exit;
163 }
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000166
167 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200168 * 1b. Prepare SSL configuration
169 */
170 mbedtls_printf( " . Configuring SSL..." );
171 fflush( stdout );
172
173 if( ( ret = mbedtls_ssl_config_defaults( &conf,
174 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200175 MBEDTLS_SSL_TRANSPORT_STREAM,
176 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200177 {
Janos Follath582a4612016-04-28 23:37:16 +0100178 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200179 goto exit;
180 }
181
182 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
183 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
184
185 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
186 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
187 {
Janos Follath582a4612016-04-28 23:37:16 +0100188 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200189 goto exit;
190 }
191
192 mbedtls_printf( " ok\n" );
193
194 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000195 * 2. Setup the listening TCP socket
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000198 fflush( stdout );
199
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200200 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000201 {
Janos Follath582a4612016-04-28 23:37:16 +0100202 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000203 goto exit;
204 }
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000207
208 while( 1 )
209 {
210 /*
211 * 3. Wait until a client connects
212 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200213 mbedtls_net_init( &client_fd );
214 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000215
Janos Follath582a4612016-04-28 23:37:16 +0100216 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000217 fflush( stdout );
218
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200219 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200220 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000221 {
Janos Follath582a4612016-04-28 23:37:16 +0100222 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000223 goto exit;
224 }
225
Paul Bakker896ac222011-05-20 12:33:05 +0000226 /*
227 * 3.5. Forking server thread
228 */
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000231 fflush( stdout );
232
Janos Follath582a4612016-04-28 23:37:16 +0100233 pid = fork();
234
Paul Bakker896ac222011-05-20 12:33:05 +0000235 if( pid < 0 )
236 {
Janos Follath582a4612016-04-28 23:37:16 +0100237 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000238 goto exit;
239 }
240
Paul Bakker896ac222011-05-20 12:33:05 +0000241 if( pid != 0 )
242 {
Janos Follath582a4612016-04-28 23:37:16 +0100243 mbedtls_printf( " ok\n" );
Robert Larsendf8e5112019-08-23 10:55:47 +0200244 mbedtls_net_close( &client_fd );
Janos Follath582a4612016-04-28 23:37:16 +0100245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200247 (const unsigned char *) "parent",
248 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000249 {
Janos Follath582a4612016-04-28 23:37:16 +0100250 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000251 goto exit;
252 }
253
Paul Bakker896ac222011-05-20 12:33:05 +0000254 continue;
255 }
256
Robert Larsendf8e5112019-08-23 10:55:47 +0200257 mbedtls_net_close( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000258
Janos Follath582a4612016-04-28 23:37:16 +0100259 pid = getpid();
260
Paul Bakker896ac222011-05-20 12:33:05 +0000261 /*
262 * 4. Setup stuff
263 */
Janos Follath582a4612016-04-28 23:37:16 +0100264 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000265 fflush( stdout );
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200268 (const unsigned char *) "child",
269 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000270 {
Janos Follath582a4612016-04-28 23:37:16 +0100271 mbedtls_printf(
272 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
273 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000274 goto exit;
275 }
Paul Bakker0c226102014-04-17 16:02:36 +0200276
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200277 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
278 {
Janos Follath582a4612016-04-28 23:37:16 +0100279 mbedtls_printf(
280 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
281 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200282 goto exit;
283 }
284
285 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
286
Janos Follath582a4612016-04-28 23:37:16 +0100287 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200288
Paul Bakker896ac222011-05-20 12:33:05 +0000289 /*
290 * 5. Handshake
291 */
Janos Follath582a4612016-04-28 23:37:16 +0100292 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000293 fflush( stdout );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000296 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100297 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000298 {
Janos Follath582a4612016-04-28 23:37:16 +0100299 mbedtls_printf(
300 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
301 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000302 goto exit;
303 }
304 }
305
Janos Follath582a4612016-04-28 23:37:16 +0100306 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000307
308 /*
309 * 6. Read the HTTP Request
310 */
Janos Follath582a4612016-04-28 23:37:16 +0100311 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000312 fflush( stdout );
313
314 do
315 {
316 len = sizeof( buf ) - 1;
317 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000319
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100320 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000321 continue;
322
323 if( ret <= 0 )
324 {
325 switch( ret )
326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100328 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000329 break;
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100332 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000333 break;
334
335 default:
Janos Follath582a4612016-04-28 23:37:16 +0100336 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000337 break;
338 }
339
340 break;
341 }
342
343 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100344 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000345
346 if( ret > 0 )
347 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000348 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000349 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000350
351 /*
352 * 7. Write the 200 Response
353 */
Janos Follath582a4612016-04-28 23:37:16 +0100354 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000355 fflush( stdout );
356
357 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000359
Paul Bakker030decd2014-04-17 16:03:23 +0200360 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000365 {
Janos Follath582a4612016-04-28 23:37:16 +0100366 mbedtls_printf(
367 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000368 goto exit;
369 }
370
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100371 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000372 {
Janos Follath582a4612016-04-28 23:37:16 +0100373 mbedtls_printf(
374 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
375 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000376 goto exit;
377 }
378 }
379 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100380 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000381
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200382 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000383 }
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000386 goto exit;
387 }
388
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100389 exit_code = MBEDTLS_EXIT_SUCCESS;
390
Paul Bakker896ac222011-05-20 12:33:05 +0000391exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200392 mbedtls_net_free( &client_fd );
393 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_x509_crt_free( &srvcert );
396 mbedtls_pk_free( &pkey );
397 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200398 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_ctr_drbg_free( &ctr_drbg );
400 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000401
Paul Bakkercce9d772011-11-18 14:26:47 +0000402#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000404 fflush( stdout ); getchar();
405#endif
406
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200407 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
410 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
411 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100412 ! _WIN32 */