blob: e9c220c298628d44c7bd4b75582a40c46d09f21f [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker896ac222011-05-20 12:33:05 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker896ac222011-05-20 12:33:05 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
35#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010036#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010037#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +010038#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
42 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
43 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
44 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
45 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010046 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C) || \
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +020047 defined(MBEDTLS_SSL_PROTO_NO_TLS)
Paul Bakkercce9d772011-11-18 14:26:47 +000048int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000049{
Paul Bakkercce9d772011-11-18 14:26:47 +000050 ((void) argc);
51 ((void) argv);
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
54 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
55 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
56 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +020057 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined, and/or "
58 "MBEDTLS_SSL_PROTO_NO_TLS defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000059 return( 0 );
60}
Paul Bakkercce9d772011-11-18 14:26:47 +000061#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000062int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000065 "to work correctly.\n");
66 return( 0 );
67}
68#else
Paul Bakker896ac222011-05-20 12:33:05 +000069
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010070#include "mbedtls/entropy.h"
71#include "mbedtls/ctr_drbg.h"
72#include "mbedtls/certs.h"
73#include "mbedtls/x509.h"
74#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010075#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010076#include "mbedtls/timing.h"
77
78#include <string.h>
79#include <signal.h>
80
81#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
82#include <unistd.h>
83#endif
84
85#define HTTP_RESPONSE \
86 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
87 "<h2>mbed TLS Test Server</h2>\r\n" \
88 "<p>Successful connection using: %s</p>\r\n"
89
Paul Bakker896ac222011-05-20 12:33:05 +000090#define DEBUG_LEVEL 0
91
Simon Butcher63cb97e2018-12-06 17:43:31 +000092
Hanno Becker14a4a442019-07-02 17:00:34 +010093#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020094static void my_debug( void *ctx, int level,
95 const char *file, int line,
96 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000097{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020098 ((void) level);
99
100 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
101 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +0000102}
Hanno Becker14a4a442019-07-02 17:00:34 +0100103#endif /* MBEDTLS_DEBUG_C */
Paul Bakker896ac222011-05-20 12:33:05 +0000104
Hanno Becker572d4482019-07-23 13:47:53 +0100105#if defined(MBEDTLS_SSL_CONF_RNG)
106int rng_wrap( void *ctx, unsigned char *dst, size_t len );
107
108mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
109int rng_wrap( void *ctx, unsigned char *dst, size_t len )
110{
111 /* We expect the NULL parameter here. */
112 if( ctx != NULL )
113 return( -1 );
114
115 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
116}
117#endif /* MBEDTLS_SSL_CONF_RNG */
118
Jarno Lamsa642596e2019-10-04 12:52:42 +0300119#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
120int mbedtls_hardware_poll( void *data, unsigned char *output,
121 size_t len, size_t *olen )
122{
123 size_t i;
124 (void) data;
125 for( i = 0; i < len; ++i )
126 output[i] = rand();
127 *olen = len;
128 return( 0 );
129}
130#endif
131
Rich Evans85b05ec2015-02-12 11:37:29 +0000132int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +0000133{
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100134 int ret = 1, len, cnt = 0, pid;
135 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200136 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000137 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200138 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_entropy_context entropy;
141 mbedtls_ctr_drbg_context ctr_drbg;
142 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200143 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_x509_crt srvcert;
145 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000146
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200147 mbedtls_net_init( &listen_fd );
148 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200149 mbedtls_ssl_init( &ssl );
150 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_entropy_init( &entropy );
152 mbedtls_pk_init( &pkey );
153 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200154 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200155
Paul Bakker896ac222011-05-20 12:33:05 +0000156 signal( SIGCHLD, SIG_IGN );
157
158 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000159 * 0. Initial seeding of the RNG
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 fflush( stdout );
163
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200164 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200165 (const unsigned char *) pers,
166 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000167 {
Janos Follath582a4612016-04-28 23:37:16 +0100168 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000169 goto exit;
170 }
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173
174 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000175 * 1. Load the certificates and private RSA key
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000178 fflush( stdout );
179
Paul Bakker896ac222011-05-20 12:33:05 +0000180 /*
181 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
183 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
186 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000187 if( ret != 0 )
188 {
Janos Follath582a4612016-04-28 23:37:16 +0100189 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000190 goto exit;
191 }
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
194 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000195 if( ret != 0 )
196 {
Janos Follath582a4612016-04-28 23:37:16 +0100197 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000198 goto exit;
199 }
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
202 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000203 if( ret != 0 )
204 {
Janos Follath582a4612016-04-28 23:37:16 +0100205 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000206 goto exit;
207 }
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000210
211 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200212 * 1b. Prepare SSL configuration
213 */
214 mbedtls_printf( " . Configuring SSL..." );
215 fflush( stdout );
216
217 if( ( ret = mbedtls_ssl_config_defaults( &conf,
218 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200219 MBEDTLS_SSL_TRANSPORT_STREAM,
220 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200221 {
Janos Follath582a4612016-04-28 23:37:16 +0100222 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200223 goto exit;
224 }
225
Hanno Becker572d4482019-07-23 13:47:53 +0100226#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200227 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
Hanno Becker572d4482019-07-23 13:47:53 +0100228#else
229 rng_ctx_global = &ctr_drbg;
230#endif
231
Hanno Becker14a4a442019-07-02 17:00:34 +0100232#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200233 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +0100234#endif
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200235
236 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
237 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
238 {
Janos Follath582a4612016-04-28 23:37:16 +0100239 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200240 goto exit;
241 }
242
243 mbedtls_printf( " ok\n" );
244
245 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000246 * 2. Setup the listening TCP socket
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000249 fflush( stdout );
250
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200251 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000252 {
Janos Follath582a4612016-04-28 23:37:16 +0100253 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000254 goto exit;
255 }
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000258
259 while( 1 )
260 {
261 /*
262 * 3. Wait until a client connects
263 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200264 mbedtls_net_init( &client_fd );
265 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000266
Janos Follath582a4612016-04-28 23:37:16 +0100267 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000268 fflush( stdout );
269
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200270 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200271 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000272 {
Janos Follath582a4612016-04-28 23:37:16 +0100273 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000274 goto exit;
275 }
276
Paul Bakker896ac222011-05-20 12:33:05 +0000277 /*
278 * 3.5. Forking server thread
279 */
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000282 fflush( stdout );
283
Janos Follath582a4612016-04-28 23:37:16 +0100284 pid = fork();
285
Paul Bakker896ac222011-05-20 12:33:05 +0000286 if( pid < 0 )
287 {
Janos Follath582a4612016-04-28 23:37:16 +0100288 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000289 goto exit;
290 }
291
Paul Bakker896ac222011-05-20 12:33:05 +0000292 if( pid != 0 )
293 {
Janos Follath582a4612016-04-28 23:37:16 +0100294 mbedtls_printf( " ok\n" );
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200297 (const unsigned char *) "parent",
298 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000299 {
Janos Follath582a4612016-04-28 23:37:16 +0100300 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000301 goto exit;
302 }
303
Paul Bakker896ac222011-05-20 12:33:05 +0000304 continue;
305 }
306
Janos Follathfe049db2016-03-31 11:37:33 +0100307 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000308
Janos Follath582a4612016-04-28 23:37:16 +0100309 pid = getpid();
310
Paul Bakker896ac222011-05-20 12:33:05 +0000311 /*
312 * 4. Setup stuff
313 */
Janos Follath582a4612016-04-28 23:37:16 +0100314 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000315 fflush( stdout );
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200318 (const unsigned char *) "child",
319 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000320 {
Janos Follath582a4612016-04-28 23:37:16 +0100321 mbedtls_printf(
322 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
323 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000324 goto exit;
325 }
Paul Bakker0c226102014-04-17 16:02:36 +0200326
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200327 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
328 {
Janos Follath582a4612016-04-28 23:37:16 +0100329 mbedtls_printf(
330 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
331 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200332 goto exit;
333 }
334
Hanno Becker41e5a682019-07-19 17:07:30 +0100335#if !defined(MBEDTLS_SSL_CONF_RECV) && \
336 !defined(MBEDTLS_SSL_CONF_SEND) && \
337 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
338 mbedtls_ssl_set_bio( &ssl, &client_fd,
339 mbedtls_net_send, mbedtls_net_recv, NULL );
340#else
341 mbedtls_ssl_set_bio_ctx( &ssl, &client_fd );
342#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200343
Janos Follath582a4612016-04-28 23:37:16 +0100344 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200345
Paul Bakker896ac222011-05-20 12:33:05 +0000346 /*
347 * 5. Handshake
348 */
Janos Follath582a4612016-04-28 23:37:16 +0100349 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000350 fflush( stdout );
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000353 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100354 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000355 {
Janos Follath582a4612016-04-28 23:37:16 +0100356 mbedtls_printf(
357 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
358 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000359 goto exit;
360 }
361 }
362
Janos Follath582a4612016-04-28 23:37:16 +0100363 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000364
365 /*
366 * 6. Read the HTTP Request
367 */
Janos Follath582a4612016-04-28 23:37:16 +0100368 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000369 fflush( stdout );
370
371 do
372 {
373 len = sizeof( buf ) - 1;
374 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000376
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100377 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000378 continue;
379
380 if( ret <= 0 )
381 {
382 switch( ret )
383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100385 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000386 break;
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100389 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000390 break;
391
392 default:
Janos Follath582a4612016-04-28 23:37:16 +0100393 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000394 break;
395 }
396
397 break;
398 }
399
400 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100401 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000402
403 if( ret > 0 )
404 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000405 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000406 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000407
408 /*
409 * 7. Write the 200 Response
410 */
Janos Follath582a4612016-04-28 23:37:16 +0100411 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000412 fflush( stdout );
413
414 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000416
Paul Bakker030decd2014-04-17 16:03:23 +0200417 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000422 {
Janos Follath582a4612016-04-28 23:37:16 +0100423 mbedtls_printf(
424 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000425 goto exit;
426 }
427
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100428 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000429 {
Janos Follath582a4612016-04-28 23:37:16 +0100430 mbedtls_printf(
431 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
432 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000433 goto exit;
434 }
435 }
436 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100437 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000438
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200439 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000440 }
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000443 goto exit;
444 }
445
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100446 exit_code = MBEDTLS_EXIT_SUCCESS;
447
Paul Bakker896ac222011-05-20 12:33:05 +0000448exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200449 mbedtls_net_free( &client_fd );
450 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_x509_crt_free( &srvcert );
453 mbedtls_pk_free( &pkey );
454 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200455 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_ctr_drbg_free( &ctr_drbg );
457 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000458
Paul Bakkercce9d772011-11-18 14:26:47 +0000459#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000461 fflush( stdout ); getchar();
462#endif
463
Andres Amaya Garcia4be53b52018-04-29 20:57:21 +0100464 return( exit_code );
Paul Bakker896ac222011-05-20 12:33:05 +0000465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
467 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
468 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100469 ! _WIN32 */