blob: 9713c3439fb9f1a1887039ca8088c511f323d5ad [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útia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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 Garcia4be53b52018-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
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-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 Stachowiak3b0c4302019-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 Stachowiak3b0c4302019-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
Simon Butcher63cb97e2018-12-06 17:43:31 +0000115
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200116static void my_debug( void *ctx, int level,
117 const char *file, int line,
118 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +0000119{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200120 ((void) level);
121
122 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
123 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000124}
125
Rich Evans85b05ec2015-02-12 11:37:29 +0000126int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000127{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100128 int ret = 1, len, cnt = 0, pid;
129 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200130 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000131 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200132 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_entropy_context entropy;
135 mbedtls_ctr_drbg_context ctr_drbg;
136 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200137 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_x509_crt srvcert;
139 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000140
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200141 mbedtls_net_init( &listen_fd );
142 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200143 mbedtls_ssl_init( &ssl );
144 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_entropy_init( &entropy );
146 mbedtls_pk_init( &pkey );
147 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200148 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200149
Paul Bakker896ac222011-05-20 12:33:05 +0000150 signal( SIGCHLD, SIG_IGN );
151
152 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000153 * 0. Initial seeding of the RNG
154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000156 fflush( stdout );
157
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200158 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200159 (const unsigned char *) pers,
160 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000161 {
Janos Follath582a4612016-04-28 23:37:16 +0100162 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000163 goto exit;
164 }
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000167
168 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000169 * 1. Load the certificates and private RSA key
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000172 fflush( stdout );
173
Paul Bakker896ac222011-05-20 12:33:05 +0000174 /*
175 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
177 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
180 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000181 if( ret != 0 )
182 {
Janos Follath582a4612016-04-28 23:37:16 +0100183 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000184 goto exit;
185 }
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
188 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000189 if( ret != 0 )
190 {
Janos Follath582a4612016-04-28 23:37:16 +0100191 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000192 goto exit;
193 }
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
196 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000197 if( ret != 0 )
198 {
Janos Follath582a4612016-04-28 23:37:16 +0100199 mbedtls_printf( " failed! mbedtls_pk_parse_key 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 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200206 * 1b. Prepare SSL configuration
207 */
208 mbedtls_printf( " . Configuring SSL..." );
209 fflush( stdout );
210
211 if( ( ret = mbedtls_ssl_config_defaults( &conf,
212 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200213 MBEDTLS_SSL_TRANSPORT_STREAM,
214 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200215 {
Janos Follath582a4612016-04-28 23:37:16 +0100216 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200217 goto exit;
218 }
219
220 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
221 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
222
223 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
224 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
225 {
Janos Follath582a4612016-04-28 23:37:16 +0100226 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200227 goto exit;
228 }
229
230 mbedtls_printf( " ok\n" );
231
232 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000233 * 2. Setup the listening TCP socket
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000236 fflush( stdout );
237
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200238 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000239 {
Janos Follath582a4612016-04-28 23:37:16 +0100240 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000241 goto exit;
242 }
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000245
246 while( 1 )
247 {
248 /*
249 * 3. Wait until a client connects
250 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200251 mbedtls_net_init( &client_fd );
252 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000253
Janos Follath582a4612016-04-28 23:37:16 +0100254 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000255 fflush( stdout );
256
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200257 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200258 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000259 {
Janos Follath582a4612016-04-28 23:37:16 +0100260 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000261 goto exit;
262 }
263
Paul Bakker896ac222011-05-20 12:33:05 +0000264 /*
265 * 3.5. Forking server thread
266 */
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000269 fflush( stdout );
270
Janos Follath582a4612016-04-28 23:37:16 +0100271 pid = fork();
272
Paul Bakker896ac222011-05-20 12:33:05 +0000273 if( pid < 0 )
274 {
Janos Follath582a4612016-04-28 23:37:16 +0100275 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000276 goto exit;
277 }
278
Paul Bakker896ac222011-05-20 12:33:05 +0000279 if( pid != 0 )
280 {
Janos Follath582a4612016-04-28 23:37:16 +0100281 mbedtls_printf( " ok\n" );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200284 (const unsigned char *) "parent",
285 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000286 {
Janos Follath582a4612016-04-28 23:37:16 +0100287 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000288 goto exit;
289 }
290
Paul Bakker896ac222011-05-20 12:33:05 +0000291 continue;
292 }
293
Janos Follathfe049db2016-03-31 11:37:33 +0100294 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000295
Janos Follath582a4612016-04-28 23:37:16 +0100296 pid = getpid();
297
Paul Bakker896ac222011-05-20 12:33:05 +0000298 /*
299 * 4. Setup stuff
300 */
Janos Follath582a4612016-04-28 23:37:16 +0100301 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000302 fflush( stdout );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200305 (const unsigned char *) "child",
306 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000307 {
Janos Follath582a4612016-04-28 23:37:16 +0100308 mbedtls_printf(
309 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
310 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000311 goto exit;
312 }
Paul Bakker0c226102014-04-17 16:02:36 +0200313
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200314 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
315 {
Janos Follath582a4612016-04-28 23:37:16 +0100316 mbedtls_printf(
317 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
318 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200319 goto exit;
320 }
321
322 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
323
Janos Follath582a4612016-04-28 23:37:16 +0100324 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200325
Paul Bakker896ac222011-05-20 12:33:05 +0000326 /*
327 * 5. Handshake
328 */
Janos Follath582a4612016-04-28 23:37:16 +0100329 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000330 fflush( stdout );
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000333 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100334 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000335 {
Janos Follath582a4612016-04-28 23:37:16 +0100336 mbedtls_printf(
337 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
338 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000339 goto exit;
340 }
341 }
342
Janos Follath582a4612016-04-28 23:37:16 +0100343 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000344
345 /*
346 * 6. Read the HTTP Request
347 */
Janos Follath582a4612016-04-28 23:37:16 +0100348 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000349 fflush( stdout );
350
351 do
352 {
353 len = sizeof( buf ) - 1;
354 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000356
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100357 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000358 continue;
359
360 if( ret <= 0 )
361 {
362 switch( ret )
363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100365 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000366 break;
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100369 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000370 break;
371
372 default:
Janos Follath582a4612016-04-28 23:37:16 +0100373 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000374 break;
375 }
376
377 break;
378 }
379
380 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100381 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000382
383 if( ret > 0 )
384 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000385 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000386 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000387
388 /*
389 * 7. Write the 200 Response
390 */
Janos Follath582a4612016-04-28 23:37:16 +0100391 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000392 fflush( stdout );
393
394 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000396
Paul Bakker030decd2014-04-17 16:03:23 +0200397 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000402 {
Janos Follath582a4612016-04-28 23:37:16 +0100403 mbedtls_printf(
404 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000405 goto exit;
406 }
407
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100408 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000409 {
Janos Follath582a4612016-04-28 23:37:16 +0100410 mbedtls_printf(
411 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
412 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000413 goto exit;
414 }
415 }
416 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100417 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000418
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200419 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000420 }
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000423 goto exit;
424 }
425
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100426 exit_code = MBEDTLS_EXIT_SUCCESS;
427
Paul Bakker896ac222011-05-20 12:33:05 +0000428exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200429 mbedtls_net_free( &client_fd );
430 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_x509_crt_free( &srvcert );
433 mbedtls_pk_free( &pkey );
434 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200435 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ctr_drbg_free( &ctr_drbg );
437 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000438
Paul Bakkercce9d772011-11-18 14:26:47 +0000439#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000441 fflush( stdout ); getchar();
442#endif
443
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200444 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
447 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
448 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100449 ! _WIN32 */