blob: 3e411ea6b16ae30a7a8160d29ba511c28996c3eb [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker896ac222011-05-20 12:33:05 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker896ac222011-05-20 12:33:05 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010059#include <stdlib.h>
60#define mbedtls_fprintf fprintf
61#define mbedtls_printf printf
62#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010063#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010064#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010065#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
66#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
69 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
70 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
71 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
72 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
73 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000074int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000075{
Paul Bakkercce9d772011-11-18 14:26:47 +000076 ((void) argc);
77 ((void) argv);
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
80 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
81 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
82 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
83 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020084 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000085}
Paul Bakkercce9d772011-11-18 14:26:47 +000086#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000087int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000090 "to work correctly.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020091 mbedtls_exit( 0 );
Paul Bakkerb892b132011-10-12 09:19:43 +000092}
93#else
Paul Bakker896ac222011-05-20 12:33:05 +000094
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010095#include "mbedtls/entropy.h"
96#include "mbedtls/ctr_drbg.h"
97#include "mbedtls/certs.h"
98#include "mbedtls/x509.h"
99#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +0100100#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100101#include "mbedtls/timing.h"
102
103#include <string.h>
104#include <signal.h>
105
106#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
107#include <unistd.h>
108#endif
109
110#define HTTP_RESPONSE \
111 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
112 "<h2>mbed TLS Test Server</h2>\r\n" \
113 "<p>Successful connection using: %s</p>\r\n"
114
Paul Bakker896ac222011-05-20 12:33:05 +0000115#define DEBUG_LEVEL 0
116
Simon Butcher63cb97e2018-12-06 17:43:31 +0000117
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200118static void my_debug( void *ctx, int level,
119 const char *file, int line,
120 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +0000121{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200122 ((void) level);
123
124 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
125 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000126}
127
Rich Evans85b05ec2015-02-12 11:37:29 +0000128int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000129{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100130 int ret = 1, len, cnt = 0, pid;
131 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200132 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000133 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200134 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_entropy_context entropy;
137 mbedtls_ctr_drbg_context ctr_drbg;
138 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200139 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_crt srvcert;
141 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000142
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200143 mbedtls_net_init( &listen_fd );
144 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200145 mbedtls_ssl_init( &ssl );
146 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_entropy_init( &entropy );
148 mbedtls_pk_init( &pkey );
149 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200150 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200151
Paul Bakker896ac222011-05-20 12:33:05 +0000152 signal( SIGCHLD, SIG_IGN );
153
154 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000155 * 0. Initial seeding of the RNG
156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000158 fflush( stdout );
159
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200160 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200161 (const unsigned char *) pers,
162 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000163 {
Janos Follath582a4612016-04-28 23:37:16 +0100164 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000165 goto exit;
166 }
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000169
170 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000171 * 1. Load the certificates and private RSA key
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000174 fflush( stdout );
175
Paul Bakker896ac222011-05-20 12:33:05 +0000176 /*
177 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
179 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
182 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000183 if( ret != 0 )
184 {
Janos Follath582a4612016-04-28 23:37:16 +0100185 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000186 goto exit;
187 }
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
190 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000191 if( ret != 0 )
192 {
Janos Follath582a4612016-04-28 23:37:16 +0100193 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000194 goto exit;
195 }
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
198 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000199 if( ret != 0 )
200 {
Janos Follath582a4612016-04-28 23:37:16 +0100201 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000202 goto exit;
203 }
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000206
207 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200208 * 1b. Prepare SSL configuration
209 */
210 mbedtls_printf( " . Configuring SSL..." );
211 fflush( stdout );
212
213 if( ( ret = mbedtls_ssl_config_defaults( &conf,
214 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200215 MBEDTLS_SSL_TRANSPORT_STREAM,
216 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200217 {
Janos Follath582a4612016-04-28 23:37:16 +0100218 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200219 goto exit;
220 }
221
222 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
223 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
224
225 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
226 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
227 {
Janos Follath582a4612016-04-28 23:37:16 +0100228 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200229 goto exit;
230 }
231
232 mbedtls_printf( " ok\n" );
233
234 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000235 * 2. Setup the listening TCP socket
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000238 fflush( stdout );
239
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200240 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000241 {
Janos Follath582a4612016-04-28 23:37:16 +0100242 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000243 goto exit;
244 }
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000247
248 while( 1 )
249 {
250 /*
251 * 3. Wait until a client connects
252 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200253 mbedtls_net_init( &client_fd );
254 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000255
Janos Follath582a4612016-04-28 23:37:16 +0100256 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000257 fflush( stdout );
258
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200259 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200260 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000261 {
Janos Follath582a4612016-04-28 23:37:16 +0100262 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000263 goto exit;
264 }
265
Paul Bakker896ac222011-05-20 12:33:05 +0000266 /*
267 * 3.5. Forking server thread
268 */
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000271 fflush( stdout );
272
Janos Follath582a4612016-04-28 23:37:16 +0100273 pid = fork();
274
Paul Bakker896ac222011-05-20 12:33:05 +0000275 if( pid < 0 )
276 {
Janos Follath582a4612016-04-28 23:37:16 +0100277 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000278 goto exit;
279 }
280
Paul Bakker896ac222011-05-20 12:33:05 +0000281 if( pid != 0 )
282 {
Janos Follath582a4612016-04-28 23:37:16 +0100283 mbedtls_printf( " ok\n" );
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 *) "parent",
287 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000288 {
Janos Follath582a4612016-04-28 23:37:16 +0100289 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000290 goto exit;
291 }
292
Paul Bakker896ac222011-05-20 12:33:05 +0000293 continue;
294 }
295
Janos Follathfe049db2016-03-31 11:37:33 +0100296 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000297
Janos Follath582a4612016-04-28 23:37:16 +0100298 pid = getpid();
299
Paul Bakker896ac222011-05-20 12:33:05 +0000300 /*
301 * 4. Setup stuff
302 */
Janos Follath582a4612016-04-28 23:37:16 +0100303 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000304 fflush( stdout );
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200307 (const unsigned char *) "child",
308 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000309 {
Janos Follath582a4612016-04-28 23:37:16 +0100310 mbedtls_printf(
311 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
312 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000313 goto exit;
314 }
Paul Bakker0c226102014-04-17 16:02:36 +0200315
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200316 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
317 {
Janos Follath582a4612016-04-28 23:37:16 +0100318 mbedtls_printf(
319 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
320 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200321 goto exit;
322 }
323
324 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
325
Janos Follath582a4612016-04-28 23:37:16 +0100326 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200327
Paul Bakker896ac222011-05-20 12:33:05 +0000328 /*
329 * 5. Handshake
330 */
Janos Follath582a4612016-04-28 23:37:16 +0100331 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000332 fflush( stdout );
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000335 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100336 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000337 {
Janos Follath582a4612016-04-28 23:37:16 +0100338 mbedtls_printf(
339 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
340 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000341 goto exit;
342 }
343 }
344
Janos Follath582a4612016-04-28 23:37:16 +0100345 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000346
347 /*
348 * 6. Read the HTTP Request
349 */
Janos Follath582a4612016-04-28 23:37:16 +0100350 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000351 fflush( stdout );
352
353 do
354 {
355 len = sizeof( buf ) - 1;
356 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000358
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100359 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000360 continue;
361
362 if( ret <= 0 )
363 {
364 switch( ret )
365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100367 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000368 break;
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100371 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000372 break;
373
374 default:
Janos Follath582a4612016-04-28 23:37:16 +0100375 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000376 break;
377 }
378
379 break;
380 }
381
382 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100383 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000384
385 if( ret > 0 )
386 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000387 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000388 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000389
390 /*
391 * 7. Write the 200 Response
392 */
Janos Follath582a4612016-04-28 23:37:16 +0100393 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000394 fflush( stdout );
395
396 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000398
Paul Bakker030decd2014-04-17 16:03:23 +0200399 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000404 {
Janos Follath582a4612016-04-28 23:37:16 +0100405 mbedtls_printf(
406 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000407 goto exit;
408 }
409
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100410 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000411 {
Janos Follath582a4612016-04-28 23:37:16 +0100412 mbedtls_printf(
413 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
414 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000415 goto exit;
416 }
417 }
418 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100419 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000420
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200421 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000422 }
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000425 goto exit;
426 }
427
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100428 exit_code = MBEDTLS_EXIT_SUCCESS;
429
Paul Bakker896ac222011-05-20 12:33:05 +0000430exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200431 mbedtls_net_free( &client_fd );
432 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_x509_crt_free( &srvcert );
435 mbedtls_pk_free( &pkey );
436 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200437 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ctr_drbg_free( &ctr_drbg );
439 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000440
Paul Bakkercce9d772011-11-18 14:26:47 +0000441#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000443 fflush( stdout ); getchar();
444#endif
445
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200446 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
449 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
450 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100451 ! _WIN32 */