2011-05-20 14:33:05 +02:00
|
|
|
/*
|
2011-05-20 14:44:16 +02:00
|
|
|
* SSL server demonstration program using fork() for handling multiple clients
|
2011-05-20 14:33:05 +02:00
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-05-20 14:33:05 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02: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 14:33:05 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-20 14:33:05 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02: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 14:33:05 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2011-05-20 14:33:05 +02:00
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 15:26:37 +01:00
|
|
|
#else
|
2015-02-11 15:06:19 +01:00
|
|
|
#include <stdio.h>
|
2018-04-29 21:57:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_time_t time_t
|
2018-12-10 14:31:45 +01:00
|
|
|
#define mbedtls_exit exit
|
2018-04-30 23:42:33 +02:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
2018-04-29 21:57:21 +02:00
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2015-01-19 15:26:37 +01:00
|
|
|
|
2015-04-08 12:49:31 +02: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) || \
|
2019-03-18 10:51:18 +01:00
|
|
|
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C) || \
|
2019-06-17 15:21:07 +02:00
|
|
|
defined(MBEDTLS_SSL_PROTO_NO_TLS)
|
2011-11-18 15:26:47 +01:00
|
|
|
int main( int argc, char *argv[] )
|
2011-10-12 11:19:43 +02:00
|
|
|
{
|
2011-11-18 15:26:47 +01:00
|
|
|
((void) argc);
|
|
|
|
((void) argv);
|
|
|
|
|
2015-04-08 12:49:31 +02: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 "
|
2019-06-17 15:21:07 +02:00
|
|
|
"MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined, and/or "
|
|
|
|
"MBEDTLS_SSL_PROTO_NO_TLS defined.\n");
|
2011-10-12 11:19:43 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2011-11-18 15:26:47 +01:00
|
|
|
#elif defined(_WIN32)
|
2015-02-12 12:37:29 +01:00
|
|
|
int main( void )
|
2011-10-12 11:19:43 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
|
2011-10-12 11:19:43 +02:00
|
|
|
"to work correctly.\n");
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-03-27 11:24:27 +01: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 15:32:09 +02:00
|
|
|
#include "mbedtls/net_sockets.h"
|
2015-03-27 11:24:27 +01: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 14:33:05 +02:00
|
|
|
#define DEBUG_LEVEL 0
|
|
|
|
|
2018-12-06 18:43:31 +01:00
|
|
|
|
2019-07-02 18:00:34 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2015-06-23 17:35:03 +02:00
|
|
|
static void my_debug( void *ctx, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *str )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2015-06-23 17:35:03 +02:00
|
|
|
((void) level);
|
|
|
|
|
|
|
|
mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
|
|
|
|
fflush( (FILE *) ctx );
|
2011-05-20 14:33:05 +02:00
|
|
|
}
|
2019-07-02 18:00:34 +02:00
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2019-07-23 14:47:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CONF_RNG)
|
|
|
|
int rng_wrap( void *ctx, unsigned char *dst, size_t len );
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
|
|
|
|
int rng_wrap( void *ctx, unsigned char *dst, size_t len )
|
|
|
|
{
|
|
|
|
/* We expect the NULL parameter here. */
|
|
|
|
if( ctx != NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CONF_RNG */
|
|
|
|
|
2015-02-12 12:37:29 +01:00
|
|
|
int main( void )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2018-04-29 21:57:21 +02:00
|
|
|
int ret = 1, len, cnt = 0, pid;
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2015-06-30 15:40:39 +02:00
|
|
|
mbedtls_net_context listen_fd, client_fd;
|
2011-05-20 14:33:05 +02:00
|
|
|
unsigned char buf[1024];
|
2013-06-24 13:01:08 +02:00
|
|
|
const char *pers = "ssl_fork_server";
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_ssl_context ssl;
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_config conf;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt srvcert;
|
|
|
|
mbedtls_pk_context pkey;
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-06-30 15:40:39 +02:00
|
|
|
mbedtls_net_init( &listen_fd );
|
|
|
|
mbedtls_net_init( &client_fd );
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_init( &ssl );
|
|
|
|
mbedtls_ssl_config_init( &conf );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_init( &entropy );
|
|
|
|
mbedtls_pk_init( &pkey );
|
|
|
|
mbedtls_x509_crt_init( &srvcert );
|
2015-04-28 22:52:30 +02:00
|
|
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
2014-04-17 16:02:36 +02:00
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
signal( SIGCHLD, SIG_IGN );
|
|
|
|
|
2011-12-04 18:09:26 +01:00
|
|
|
/*
|
|
|
|
* 0. Initial seeding of the RNG
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n . Initial seeding of the random generator..." );
|
2011-12-04 18:09:26 +01:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-28 22:52:30 +02:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
2013-06-24 13:01:08 +02:00
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen( pers ) ) ) != 0 )
|
2011-12-04 18:09:26 +01:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
|
2011-12-04 18:09:26 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-12-04 18:09:26 +01:00
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
/*
|
|
|
|
* 1. Load the certificates and private RSA key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " . Loading the server cert. and key..." );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This demonstration program uses embedded test certificates.
|
2015-04-08 12:49:31 +02: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 14:33:05 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
|
|
|
|
mbedtls_test_srv_crt_len );
|
2011-05-20 14:33:05 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
|
|
|
|
mbedtls_test_cas_pem_len );
|
2011-05-20 14:33:05 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
|
|
|
|
mbedtls_test_srv_key_len, NULL, 0 );
|
2011-05-20 14:33:05 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-05-11 11:32:43 +02: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 13:53:47 +02:00
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
|
2015-05-11 11:32:43 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
|
2015-05-11 11:32:43 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-07-23 14:47:53 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_CONF_RNG)
|
2015-05-11 11:32:43 +02:00
|
|
|
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
|
2019-07-23 14:47:53 +02:00
|
|
|
#else
|
|
|
|
rng_ctx_global = &ctr_drbg;
|
|
|
|
#endif
|
|
|
|
|
2019-07-02 18:00:34 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2015-05-11 11:32:43 +02:00
|
|
|
mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
|
2019-07-02 18:00:34 +02:00
|
|
|
#endif
|
2015-05-11 11:32:43 +02:00
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
|
|
|
|
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
|
2015-05-11 11:32:43 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_printf( " ok\n" );
|
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
/*
|
|
|
|
* 2. Setup the listening TCP socket
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-06-23 12:30:57 +02:00
|
|
|
if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ok\n" );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 3. Wait until a client connects
|
|
|
|
*/
|
2015-06-30 15:40:39 +02:00
|
|
|
mbedtls_net_init( &client_fd );
|
|
|
|
mbedtls_ssl_init( &ssl );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " . Waiting for a remote connection ...\n" );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-06-30 15:40:39 +02:00
|
|
|
if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
|
2015-05-14 21:52:40 +02:00
|
|
|
NULL, 0, NULL ) ) != 0 )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 3.5. Forking server thread
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " . Forking to handle connection ..." );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
2016-04-29 00:37:16 +02:00
|
|
|
pid = fork();
|
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
if( pid < 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(" failed! fork returned %d\n\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pid != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " ok\n" );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
|
2013-06-24 13:01:08 +02:00
|
|
|
(const unsigned char *) "parent",
|
|
|
|
6 ) ) != 0 )
|
2011-12-04 18:09:26 +01:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
|
2011-12-04 18:09:26 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-31 12:37:33 +02:00
|
|
|
mbedtls_net_init( &listen_fd );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2016-04-29 00:37:16 +02:00
|
|
|
pid = getpid();
|
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
/*
|
|
|
|
* 4. Setup stuff
|
|
|
|
*/
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
|
2013-06-24 13:01:08 +02:00
|
|
|
(const unsigned char *) "child",
|
|
|
|
5 ) ) != 0 )
|
2011-12-04 18:09:26 +01:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-12-04 18:09:26 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-04-17 16:02:36 +02:00
|
|
|
|
2015-05-11 11:25:46 +02:00
|
|
|
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
|
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
|
|
|
|
pid, ret );
|
2015-05-11 11:25:46 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-07-19 18:07:30 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_CONF_RECV) && \
|
|
|
|
!defined(MBEDTLS_SSL_CONF_SEND) && \
|
|
|
|
!defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
|
|
|
|
mbedtls_ssl_set_bio( &ssl, &client_fd,
|
|
|
|
mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
|
|
#else
|
|
|
|
mbedtls_ssl_set_bio_ctx( &ssl, &client_fd );
|
|
|
|
#endif
|
2015-05-11 11:25:46 +02:00
|
|
|
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: SSL setup ok\n", pid );
|
2015-05-11 11:32:43 +02:00
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
/*
|
|
|
|
* 5. Handshake
|
|
|
|
*/
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2015-05-06 17:19:31 +02:00
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 6. Read the HTTP Request
|
|
|
|
*/
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: Start reading from client.\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = sizeof( buf ) - 1;
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_ssl_read( &ssl, buf, len );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-05-06 17:19:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 14:33:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if( ret <= 0 )
|
|
|
|
{
|
|
|
|
switch( ret )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_ERR_NET_CONN_RESET:
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
|
2015-02-14 16:53:24 +01:00
|
|
|
|
|
|
|
if( ret > 0 )
|
|
|
|
break;
|
2011-05-20 14:33:05 +02:00
|
|
|
}
|
2015-02-14 16:53:24 +01:00
|
|
|
while( 1 );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 7. Write the 200 Response
|
|
|
|
*/
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: Start writing to client.\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
len = sprintf( (char *) buf, HTTP_RESPONSE,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_get_ciphersuite( &ssl ) );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2014-04-17 16:03:23 +02:00
|
|
|
while( cnt++ < 100 )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: Write failed! peer closed the connection\n\n", pid );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:19:31 +02:00
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
2011-05-20 14:33:05 +02:00
|
|
|
{
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf(
|
|
|
|
"pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
|
|
|
|
pid, ret );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len = ret;
|
2016-04-29 00:37:16 +02:00
|
|
|
mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2015-05-14 18:22:47 +02:00
|
|
|
mbedtls_net_usleep( 1000000 );
|
2011-05-20 14:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_close_notify( &ssl );
|
2011-05-20 14:33:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-04-29 21:57:21 +02:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
|
|
|
2011-05-20 14:33:05 +02:00
|
|
|
exit:
|
2015-06-30 15:55:03 +02:00
|
|
|
mbedtls_net_free( &client_fd );
|
|
|
|
mbedtls_net_free( &listen_fd );
|
2014-04-17 16:02:36 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &srvcert );
|
|
|
|
mbedtls_pk_free( &pkey );
|
|
|
|
mbedtls_ssl_free( &ssl );
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_config_free( &conf );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
|
|
mbedtls_entropy_free( &entropy );
|
2011-05-20 14:33:05 +02:00
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
#if defined(_WIN32)
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " Press Enter to exit this program.\n" );
|
2011-05-20 14:33:05 +02:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
2018-04-29 21:57:21 +02:00
|
|
|
return( exit_code );
|
2011-05-20 14:33:05 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02: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 11:24:27 +01:00
|
|
|
! _WIN32 */
|