blob: d9f084963eda460e702af02a30e20d8abf10812a [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ú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 * **********
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 Garciae4f79e12018-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
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020063#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010064#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciae4f79e12018-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 Stachowiaka0865222019-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 Stachowiaka0865222019-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
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200117static void my_debug( void *ctx, int level,
118 const char *file, int line,
119 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +0000120{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200121 ((void) level);
122
123 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
124 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000125}
126
Rich Evans85b05ec2015-02-12 11:37:29 +0000127int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000128{
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +0100129 int ret = 1, len, cnt = 0, pid;
130 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200131 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000132 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200133 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_entropy_context entropy;
136 mbedtls_ctr_drbg_context ctr_drbg;
137 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200138 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_crt srvcert;
140 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000141
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200142 mbedtls_net_init( &listen_fd );
143 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200144 mbedtls_ssl_init( &ssl );
145 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_entropy_init( &entropy );
147 mbedtls_pk_init( &pkey );
148 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200149 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200150
Paul Bakker896ac222011-05-20 12:33:05 +0000151 signal( SIGCHLD, SIG_IGN );
152
153 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000154 * 0. Initial seeding of the RNG
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000157 fflush( stdout );
158
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200159 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200160 (const unsigned char *) pers,
161 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 {
Janos Follath582a4612016-04-28 23:37:16 +0100163 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000164 goto exit;
165 }
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000168
169 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000170 * 1. Load the certificates and private RSA key
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000173 fflush( stdout );
174
Paul Bakker896ac222011-05-20 12:33:05 +0000175 /*
176 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
178 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
181 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000182 if( ret != 0 )
183 {
Janos Follath582a4612016-04-28 23:37:16 +0100184 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000185 goto exit;
186 }
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
189 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000190 if( ret != 0 )
191 {
Janos Follath582a4612016-04-28 23:37:16 +0100192 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000193 goto exit;
194 }
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
197 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000198 if( ret != 0 )
199 {
Janos Follath582a4612016-04-28 23:37:16 +0100200 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000201 goto exit;
202 }
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000205
206 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200207 * 1b. Prepare SSL configuration
208 */
209 mbedtls_printf( " . Configuring SSL..." );
210 fflush( stdout );
211
212 if( ( ret = mbedtls_ssl_config_defaults( &conf,
213 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200214 MBEDTLS_SSL_TRANSPORT_STREAM,
215 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200216 {
Janos Follath582a4612016-04-28 23:37:16 +0100217 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200218 goto exit;
219 }
220
221 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
222 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
223
224 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
225 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
226 {
Janos Follath582a4612016-04-28 23:37:16 +0100227 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200228 goto exit;
229 }
230
231 mbedtls_printf( " ok\n" );
232
233 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000234 * 2. Setup the listening TCP socket
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000237 fflush( stdout );
238
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200239 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000240 {
Janos Follath582a4612016-04-28 23:37:16 +0100241 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000242 goto exit;
243 }
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000246
247 while( 1 )
248 {
249 /*
250 * 3. Wait until a client connects
251 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200252 mbedtls_net_init( &client_fd );
253 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000254
Janos Follath582a4612016-04-28 23:37:16 +0100255 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000256 fflush( stdout );
257
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200258 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200259 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000260 {
Janos Follath582a4612016-04-28 23:37:16 +0100261 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000262 goto exit;
263 }
264
Paul Bakker896ac222011-05-20 12:33:05 +0000265 /*
266 * 3.5. Forking server thread
267 */
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000270 fflush( stdout );
271
Janos Follath582a4612016-04-28 23:37:16 +0100272 pid = fork();
273
Paul Bakker896ac222011-05-20 12:33:05 +0000274 if( pid < 0 )
275 {
Janos Follath582a4612016-04-28 23:37:16 +0100276 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000277 goto exit;
278 }
279
Paul Bakker896ac222011-05-20 12:33:05 +0000280 if( pid != 0 )
281 {
Janos Follath582a4612016-04-28 23:37:16 +0100282 mbedtls_printf( " ok\n" );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200285 (const unsigned char *) "parent",
286 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000287 {
Janos Follath582a4612016-04-28 23:37:16 +0100288 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000289 goto exit;
290 }
291
Paul Bakker896ac222011-05-20 12:33:05 +0000292 continue;
293 }
294
Janos Follathfe049db2016-03-31 11:37:33 +0100295 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000296
Janos Follath582a4612016-04-28 23:37:16 +0100297 pid = getpid();
298
Paul Bakker896ac222011-05-20 12:33:05 +0000299 /*
300 * 4. Setup stuff
301 */
Janos Follath582a4612016-04-28 23:37:16 +0100302 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000303 fflush( stdout );
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200306 (const unsigned char *) "child",
307 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000308 {
Janos Follath582a4612016-04-28 23:37:16 +0100309 mbedtls_printf(
310 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
311 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000312 goto exit;
313 }
Paul Bakker0c226102014-04-17 16:02:36 +0200314
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200315 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
316 {
Janos Follath582a4612016-04-28 23:37:16 +0100317 mbedtls_printf(
318 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
319 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200320 goto exit;
321 }
322
323 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
324
Janos Follath582a4612016-04-28 23:37:16 +0100325 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200326
Paul Bakker896ac222011-05-20 12:33:05 +0000327 /*
328 * 5. Handshake
329 */
Janos Follath582a4612016-04-28 23:37:16 +0100330 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000331 fflush( stdout );
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000334 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100335 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000336 {
Janos Follath582a4612016-04-28 23:37:16 +0100337 mbedtls_printf(
338 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
339 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000340 goto exit;
341 }
342 }
343
Janos Follath582a4612016-04-28 23:37:16 +0100344 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000345
346 /*
347 * 6. Read the HTTP Request
348 */
Janos Follath582a4612016-04-28 23:37:16 +0100349 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000350 fflush( stdout );
351
352 do
353 {
354 len = sizeof( buf ) - 1;
355 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000357
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100358 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000359 continue;
360
361 if( ret <= 0 )
362 {
363 switch( ret )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100366 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000367 break;
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100370 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000371 break;
372
373 default:
Janos Follath582a4612016-04-28 23:37:16 +0100374 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000375 break;
376 }
377
378 break;
379 }
380
381 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100382 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000383
384 if( ret > 0 )
385 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000386 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000387 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000388
389 /*
390 * 7. Write the 200 Response
391 */
Janos Follath582a4612016-04-28 23:37:16 +0100392 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000393 fflush( stdout );
394
395 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000397
Paul Bakker030decd2014-04-17 16:03:23 +0200398 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000403 {
Janos Follath582a4612016-04-28 23:37:16 +0100404 mbedtls_printf(
405 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000406 goto exit;
407 }
408
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100409 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000410 {
Janos Follath582a4612016-04-28 23:37:16 +0100411 mbedtls_printf(
412 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
413 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000414 goto exit;
415 }
416 }
417 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100418 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000419
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200420 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000424 goto exit;
425 }
426
Andres Amaya Garciae4f79e12018-04-29 20:57:21 +0100427 exit_code = MBEDTLS_EXIT_SUCCESS;
428
Paul Bakker896ac222011-05-20 12:33:05 +0000429exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200430 mbedtls_net_free( &client_fd );
431 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_crt_free( &srvcert );
434 mbedtls_pk_free( &pkey );
435 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200436 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_ctr_drbg_free( &ctr_drbg );
438 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000439
Paul Bakkercce9d772011-11-18 14:26:47 +0000440#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000442 fflush( stdout ); getchar();
443#endif
444
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200445 mbedtls_exit( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
448 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
449 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100450 ! _WIN32 */