2011-05-20 12:33:05 +00:00
|
|
|
/*
|
2011-05-20 12:44:16 +00:00
|
|
|
* SSL server demonstration program using fork() for handling multiple clients
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-05-20 12:33:05 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 14:26:37 +00:00
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2018-04-29 19:57:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_time_t time_t
|
2018-12-10 13:31:45 +00:00
|
|
|
#define mbedtls_exit exit
|
2018-04-30 21:42:33 +00:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
2018-04-29 19:57:21 +00:00
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2015-01-19 14:26:37 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
|
|
|
|
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
|
|
|
|
!defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
|
|
|
|
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
|
|
|
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
|
|
|
|
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
|
2011-11-18 14:26:47 +00:00
|
|
|
int main( int argc, char *argv[] )
|
2011-10-12 09:19:43 +00:00
|
|
|
{
|
2011-11-18 14:26:47 +00:00
|
|
|
((void) argc);
|
|
|
|
((void) argv);
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
|
|
|
|
"and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
|
|
|
|
"MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
|
|
|
|
"MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
|
|
|
|
"MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( 0 );
|
2011-10-12 09:19:43 +00:00
|
|
|
}
|
2011-11-18 14:26:47 +00:00
|
|
|
#elif defined(_WIN32)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-10-12 09:19:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
|
2011-10-12 09:19:43 +00:00
|
|
|
"to work correctly.\n");
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( 0 );
|
2011-10-12 09:19:43 +00:00
|
|
|
}
|
|
|
|
#else
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-03-27 10:24:27 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/certs.h"
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/ssl.h"
|
2016-09-14 13:32:09 +00:00
|
|
|
#include "mbedtls/net_sockets.h"
|
2015-03-27 10:24:27 +00:00
|
|
|
#include "mbedtls/timing.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HTTP_RESPONSE \
|
|
|
|
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
|
|
|
|
"<h2>mbed TLS Test Server</h2>\r\n" \
|
|
|
|
"<p>Successful connection using: %s</p>\r\n"
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
#define DEBUG_LEVEL 0
|
|
|
|
|
2018-12-06 17:43:31 +00:00
|
|
|
|
2015-06-23 15:35:03 +00:00
|
|
|
static void my_debug( void *ctx, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *str )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2015-06-23 15:35:03 +00:00
|
|
|
((void) level);
|
|
|
|
|
|
|
|
mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
|
|
|
|
fflush( (FILE *) ctx );
|
2011-05-20 12:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2018-04-29 19:57:21 +00:00
|
|
|
int ret = 1, len, cnt = 0, pid;
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2015-06-30 13:40:39 +00:00
|
|
|
mbedtls_net_context listen_fd, client_fd;
|
2011-05-20 12:33:05 +00:00
|
|
|
unsigned char buf[1024];
|
2013-06-24 11:01:08 +00:00
|
|
|
const char *pers = "ssl_fork_server";
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_ssl_context ssl;
|
2015-05-04 12:56:36 +00:00
|
|
|
mbedtls_ssl_config conf;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt srvcert;
|
|
|
|
mbedtls_pk_context pkey;
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-06-30 13:40:39 +00:00
|
|
|
mbedtls_net_init( &listen_fd );
|
|
|
|
mbedtls_net_init( &client_fd );
|
2015-05-04 12:56:36 +00:00
|
|
|
mbedtls_ssl_init( &ssl );
|
|
|
|
mbedtls_ssl_config_init( &conf );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &entropy );
|
|
|
|
mbedtls_pk_init( &pkey );
|
|
|
|
mbedtls_x509_crt_init( &srvcert );
|
2015-04-28 20:52:30 +00:00
|
|
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
2014-04-17 14:02:36 +00:00
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
signal( SIGCHLD, SIG_IGN );
|
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
/*
|
|
|
|
* 0. Initial seeding of the RNG
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\n . Initial seeding of the random generator..." );
|
2011-12-04 17:09:26 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-28 20:52:30 +00:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
2013-06-24 11:01:08 +00:00
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen( pers ) ) ) != 0 )
|
2011-12-04 17:09:26 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-12-04 17:09:26 +00:00
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
/*
|
|
|
|
* 1. Load the certificates and private RSA key
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Loading the server cert. and key..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This demonstration program uses embedded test certificates.
|
2015-04-08 10:49:31 +00:00
|
|
|
* Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
|
|
|
|
* server and CA certificates, as well as mbedtls_pk_parse_keyfile().
|
2011-05-20 12:33:05 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
|
|
|
|
mbedtls_test_srv_crt_len );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
|
|
|
|
mbedtls_test_cas_pem_len );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
|
|
|
|
mbedtls_test_srv_key_len, NULL, 0 );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-05-11 09:32:43 +00:00
|
|
|
/*
|
|
|
|
* 1b. Prepare SSL configuration
|
|
|
|
*/
|
|
|
|
mbedtls_printf( " . Configuring SSL..." );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_config_defaults( &conf,
|
|
|
|
MBEDTLS_SSL_IS_SERVER,
|
2015-06-17 11:53:47 +00:00
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
|
2015-05-11 09:32:43 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
|
2015-05-11 09:32:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
|
|
|
|
mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
|
|
|
|
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
|
2015-05-11 09:32:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_printf( " ok\n" );
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
/*
|
|
|
|
* 2. Setup the listening TCP socket
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-06-23 10:30:57 +00:00
|
|
|
if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 3. Wait until a client connects
|
|
|
|
*/
|
2015-06-30 13:40:39 +00:00
|
|
|
mbedtls_net_init( &client_fd );
|
|
|
|
mbedtls_ssl_init( &ssl );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " . Waiting for a remote connection ...\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-06-30 13:40:39 +00:00
|
|
|
if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
|
2015-05-14 19:52:40 +00:00
|
|
|
NULL, 0, NULL ) ) != 0 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 3.5. Forking server thread
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Forking to handle connection ..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2016-04-28 22:37:16 +00:00
|
|
|
pid = fork();
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
if( pid < 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(" failed! fork returned %d\n\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pid != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2019-08-23 08:55:47 +00:00
|
|
|
mbedtls_net_close( &client_fd );
|
2016-04-28 22:37:16 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
|
2013-06-24 11:01:08 +00:00
|
|
|
(const unsigned char *) "parent",
|
|
|
|
6 ) ) != 0 )
|
2011-12-04 17:09:26 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-23 08:55:47 +00:00
|
|
|
mbedtls_net_close( &listen_fd );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2016-04-28 22:37:16 +00:00
|
|
|
pid = getpid();
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
/*
|
|
|
|
* 4. Setup stuff
|
|
|
|
*/
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
|
2013-06-24 11:01:08 +00:00
|
|
|
(const unsigned char *) "child",
|
|
|
|
5 ) ) != 0 )
|
2011-12-04 17:09:26 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-04-17 14:02:36 +00:00
|
|
|
|
2015-05-11 09:25:46 +00:00
|
|
|
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
|
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
|
|
|
|
pid, ret );
|
2015-05-11 09:25:46 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
|
|
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: SSL setup ok\n", pid );
|
2015-05-11 09:32:43 +00:00
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
/*
|
|
|
|
* 5. Handshake
|
|
|
|
*/
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2015-05-06 15:19:31 +00:00
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 6. Read the HTTP Request
|
|
|
|
*/
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: Start reading from client.\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = sizeof( buf ) - 1;
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_ssl_read( &ssl, buf, len );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-05-06 15:19:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 12:33:05 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if( ret <= 0 )
|
|
|
|
{
|
|
|
|
switch( ret )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_ERR_NET_CONN_RESET:
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
|
2015-02-14 15:53:24 +00:00
|
|
|
|
|
|
|
if( ret > 0 )
|
|
|
|
break;
|
2011-05-20 12:33:05 +00:00
|
|
|
}
|
2015-02-14 15:53:24 +00:00
|
|
|
while( 1 );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 7. Write the 200 Response
|
|
|
|
*/
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: Start writing to client.\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
len = sprintf( (char *) buf, HTTP_RESPONSE,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_get_ciphersuite( &ssl ) );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2014-04-17 14:03:23 +00:00
|
|
|
while( cnt++ < 100 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: Write failed! peer closed the connection\n\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-05-06 15:19:31 +00:00
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len = ret;
|
2016-04-28 22:37:16 +00:00
|
|
|
mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-05-14 16:22:47 +00:00
|
|
|
mbedtls_net_usleep( 1000000 );
|
2011-05-20 12:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_close_notify( &ssl );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:57:21 +00:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
exit:
|
2015-06-30 13:55:03 +00:00
|
|
|
mbedtls_net_free( &client_fd );
|
|
|
|
mbedtls_net_free( &listen_fd );
|
2014-04-17 14:02:36 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( &srvcert );
|
|
|
|
mbedtls_pk_free( &pkey );
|
|
|
|
mbedtls_ssl_free( &ssl );
|
2015-05-04 12:56:36 +00:00
|
|
|
mbedtls_ssl_config_free( &conf );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
|
|
mbedtls_entropy_free( &entropy );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " Press Enter to exit this program.\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( exit_code );
|
2011-05-20 12:33:05 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
|
|
|
|
MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
|
|
|
|
MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
|
2015-03-27 10:24:27 +00:00
|
|
|
! _WIN32 */
|