blob: 638c37b83c4440042e7fa8e49cdec9518b53159e [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úti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker896ac222011-05-20 12:33:05 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker896ac222011-05-20 12:33:05 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker896ac222011-05-20 12:33:05 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +010057#include <stdlib.h>
58#define mbedtls_fprintf fprintf
59#define mbedtls_printf printf
60#define mbedtls_time_t time_t
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
67 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
68 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
69 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
70 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
71 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000072int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000073{
Paul Bakkercce9d772011-11-18 14:26:47 +000074 ((void) argc);
75 ((void) argv);
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
78 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
79 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
80 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
81 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020082 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000083}
Paul Bakkercce9d772011-11-18 14:26:47 +000084#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000085int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000088 "to work correctly.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020089 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000090}
91#else
Paul Bakker896ac222011-05-20 12:33:05 +000092
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010093#include "mbedtls/entropy.h"
94#include "mbedtls/ctr_drbg.h"
95#include "mbedtls/certs.h"
96#include "mbedtls/x509.h"
97#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010098#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010099#include "mbedtls/timing.h"
100
101#include <string.h>
102#include <signal.h>
103
104#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
105#include <unistd.h>
106#endif
107
108#define HTTP_RESPONSE \
109 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
110 "<h2>mbed TLS Test Server</h2>\r\n" \
111 "<p>Successful connection using: %s</p>\r\n"
112
Paul Bakker896ac222011-05-20 12:33:05 +0000113#define DEBUG_LEVEL 0
114
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200115static void my_debug( void *ctx, int level,
116 const char *file, int line,
117 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +0000118{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200119 ((void) level);
120
121 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
122 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000123}
124
Rich Evans85b05ec2015-02-12 11:37:29 +0000125int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000126{
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +0100127 int ret = 1, len, cnt = 0, pid;
128 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200129 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000130 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200131 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
135 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200136 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_x509_crt srvcert;
138 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000139
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200140 mbedtls_net_init( &listen_fd );
141 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200142 mbedtls_ssl_init( &ssl );
143 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_entropy_init( &entropy );
145 mbedtls_pk_init( &pkey );
146 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200147 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200148
Paul Bakker896ac222011-05-20 12:33:05 +0000149 signal( SIGCHLD, SIG_IGN );
150
151 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000152 * 0. Initial seeding of the RNG
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000155 fflush( stdout );
156
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200157 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200158 (const unsigned char *) pers,
159 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000160 {
Janos Follath582a4612016-04-28 23:37:16 +0100161 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 goto exit;
163 }
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166
167 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000168 * 1. Load the certificates and private RSA key
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000171 fflush( stdout );
172
Paul Bakker896ac222011-05-20 12:33:05 +0000173 /*
174 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
176 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
179 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000180 if( ret != 0 )
181 {
Janos Follath582a4612016-04-28 23:37:16 +0100182 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000183 goto exit;
184 }
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
187 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000188 if( ret != 0 )
189 {
Janos Follath582a4612016-04-28 23:37:16 +0100190 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000191 goto exit;
192 }
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
195 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000196 if( ret != 0 )
197 {
Janos Follath582a4612016-04-28 23:37:16 +0100198 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000199 goto exit;
200 }
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000203
204 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200205 * 1b. Prepare SSL configuration
206 */
207 mbedtls_printf( " . Configuring SSL..." );
208 fflush( stdout );
209
210 if( ( ret = mbedtls_ssl_config_defaults( &conf,
211 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200212 MBEDTLS_SSL_TRANSPORT_STREAM,
213 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200214 {
Janos Follath582a4612016-04-28 23:37:16 +0100215 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200216 goto exit;
217 }
218
219 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
220 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
221
222 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
223 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
224 {
Janos Follath582a4612016-04-28 23:37:16 +0100225 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200226 goto exit;
227 }
228
229 mbedtls_printf( " ok\n" );
230
231 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000232 * 2. Setup the listening TCP socket
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000235 fflush( stdout );
236
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200237 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000238 {
Janos Follath582a4612016-04-28 23:37:16 +0100239 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000240 goto exit;
241 }
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000244
245 while( 1 )
246 {
247 /*
248 * 3. Wait until a client connects
249 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200250 mbedtls_net_init( &client_fd );
251 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000252
Janos Follath582a4612016-04-28 23:37:16 +0100253 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000254 fflush( stdout );
255
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200256 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200257 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000258 {
Janos Follath582a4612016-04-28 23:37:16 +0100259 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000260 goto exit;
261 }
262
Paul Bakker896ac222011-05-20 12:33:05 +0000263 /*
264 * 3.5. Forking server thread
265 */
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000268 fflush( stdout );
269
Janos Follath582a4612016-04-28 23:37:16 +0100270 pid = fork();
271
Paul Bakker896ac222011-05-20 12:33:05 +0000272 if( pid < 0 )
273 {
Janos Follath582a4612016-04-28 23:37:16 +0100274 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000275 goto exit;
276 }
277
Paul Bakker896ac222011-05-20 12:33:05 +0000278 if( pid != 0 )
279 {
Janos Follath582a4612016-04-28 23:37:16 +0100280 mbedtls_printf( " ok\n" );
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200283 (const unsigned char *) "parent",
284 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000285 {
Janos Follath582a4612016-04-28 23:37:16 +0100286 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000287 goto exit;
288 }
289
Paul Bakker896ac222011-05-20 12:33:05 +0000290 continue;
291 }
292
Janos Follathfe049db2016-03-31 11:37:33 +0100293 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000294
Janos Follath582a4612016-04-28 23:37:16 +0100295 pid = getpid();
296
Paul Bakker896ac222011-05-20 12:33:05 +0000297 /*
298 * 4. Setup stuff
299 */
Janos Follath582a4612016-04-28 23:37:16 +0100300 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000301 fflush( stdout );
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200304 (const unsigned char *) "child",
305 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000306 {
Janos Follath582a4612016-04-28 23:37:16 +0100307 mbedtls_printf(
308 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
309 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000310 goto exit;
311 }
Paul Bakker0c226102014-04-17 16:02:36 +0200312
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200313 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
314 {
Janos Follath582a4612016-04-28 23:37:16 +0100315 mbedtls_printf(
316 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
317 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200318 goto exit;
319 }
320
321 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
322
Janos Follath582a4612016-04-28 23:37:16 +0100323 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200324
Paul Bakker896ac222011-05-20 12:33:05 +0000325 /*
326 * 5. Handshake
327 */
Janos Follath582a4612016-04-28 23:37:16 +0100328 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000329 fflush( stdout );
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000332 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100333 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000334 {
Janos Follath582a4612016-04-28 23:37:16 +0100335 mbedtls_printf(
336 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
337 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000338 goto exit;
339 }
340 }
341
Janos Follath582a4612016-04-28 23:37:16 +0100342 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000343
344 /*
345 * 6. Read the HTTP Request
346 */
Janos Follath582a4612016-04-28 23:37:16 +0100347 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000348 fflush( stdout );
349
350 do
351 {
352 len = sizeof( buf ) - 1;
353 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000355
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100356 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000357 continue;
358
359 if( ret <= 0 )
360 {
361 switch( ret )
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100364 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000365 break;
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100368 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000369 break;
370
371 default:
Janos Follath582a4612016-04-28 23:37:16 +0100372 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000373 break;
374 }
375
376 break;
377 }
378
379 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100380 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000381
382 if( ret > 0 )
383 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000384 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000385 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000386
387 /*
388 * 7. Write the 200 Response
389 */
Janos Follath582a4612016-04-28 23:37:16 +0100390 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000391 fflush( stdout );
392
393 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000395
Paul Bakker030decd2014-04-17 16:03:23 +0200396 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000401 {
Janos Follath582a4612016-04-28 23:37:16 +0100402 mbedtls_printf(
403 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000404 goto exit;
405 }
406
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100407 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000408 {
Janos Follath582a4612016-04-28 23:37:16 +0100409 mbedtls_printf(
410 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
411 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000412 goto exit;
413 }
414 }
415 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100416 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000417
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200418 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000419 }
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000422 goto exit;
423 }
424
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +0100425 exit_code = MBEDTLS_EXIT_SUCCESS;
426
Paul Bakker896ac222011-05-20 12:33:05 +0000427exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200428 mbedtls_net_free( &client_fd );
429 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_crt_free( &srvcert );
432 mbedtls_pk_free( &pkey );
433 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200434 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_ctr_drbg_free( &ctr_drbg );
436 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000437
Paul Bakkercce9d772011-11-18 14:26:47 +0000438#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000440 fflush( stdout ); getchar();
441#endif
442
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200443 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
446 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
447 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100448 ! _WIN32 */