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
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2011-05-20 12:33:05 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
#if defined(POLARSSL_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>
|
2015-01-19 14:26:37 +00:00
|
|
|
#define polarssl_fprintf fprintf
|
2015-02-11 14:06:19 +00:00
|
|
|
#define polarssl_printf printf
|
2015-01-19 14:26:37 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2011-05-20 12:33:05 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 14:09:44 +00:00
|
|
|
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_CERTS_C) && \
|
|
|
|
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_SSL_TLS_C) && \
|
|
|
|
defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_NET_C) && \
|
|
|
|
defined(POLARSSL_RSA_C) && defined(POLARSSL_CTR_DRBG_C) && \
|
|
|
|
defined(POLARSSL_X509_CRT_PARSE_C) && defined(POLARSSL_TIMING_C) && \
|
2015-03-27 10:08:49 +00:00
|
|
|
defined(POLARSSL_FS_IO) && defined(POLARSSL_PEM_PARSE_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/certs.h"
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/ssl.h"
|
|
|
|
#include "mbedtls/net.h"
|
|
|
|
#include "mbedtls/timing.h"
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
#define HTTP_RESPONSE \
|
|
|
|
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
|
2015-01-22 16:26:39 +00:00
|
|
|
"<h2>mbed TLS Test Server</h2>\r\n" \
|
2011-05-20 12:33:05 +00:00
|
|
|
"<p>Successful connection using: %s</p>\r\n"
|
|
|
|
|
2011-10-12 09:19:43 +00:00
|
|
|
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
|
2011-12-04 17:09:26 +00:00
|
|
|
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
|
2011-10-12 09:19:43 +00:00
|
|
|
!defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
|
2013-04-18 20:46:23 +00:00
|
|
|
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
|
2015-02-13 14:09:44 +00:00
|
|
|
!defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_C) || \
|
2015-02-11 14:06:19 +00:00
|
|
|
!defined(POLARSSL_FS_IO)
|
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-01-19 14:26:37 +00:00
|
|
|
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
|
2011-10-12 09:19:43 +00:00
|
|
|
"and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
|
2011-12-04 17:09:26 +00:00
|
|
|
"POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
|
2013-09-17 11:25:29 +00:00
|
|
|
"POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
|
2015-03-27 10:08:49 +00:00
|
|
|
"POLARSSL_TIMING_C and/or POLARSSL_PEM_PARSE_C not defined.\n");
|
2011-10-12 09:19:43 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
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-01-19 14:26:37 +00:00
|
|
|
polarssl_printf("_WIN32 defined. This application requires fork() and signals "
|
2011-10-12 09:19:43 +00:00
|
|
|
"to work correctly.\n");
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
#define DEBUG_LEVEL 0
|
|
|
|
|
2013-06-25 14:37:45 +00:00
|
|
|
static void my_debug( void *ctx, int level, const char *str )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
|
|
|
if( level < DEBUG_LEVEL )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_fprintf( (FILE *) ctx, "%s", str );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( (FILE *) ctx );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
|
|
|
int ret, len, cnt = 0, pid;
|
|
|
|
int listen_fd;
|
2013-09-16 10:34:33 +00:00
|
|
|
int client_fd = -1;
|
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
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
entropy_context entropy;
|
|
|
|
ctr_drbg_context ctr_drbg;
|
2011-05-20 12:33:05 +00:00
|
|
|
ssl_context ssl;
|
2013-09-18 12:13:26 +00:00
|
|
|
x509_crt srvcert;
|
2013-08-19 12:10:16 +00:00
|
|
|
pk_context pkey;
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2014-04-17 14:02:36 +00:00
|
|
|
memset( &ssl, 0, sizeof(ssl_context) );
|
|
|
|
|
|
|
|
entropy_init( &entropy );
|
|
|
|
pk_init( &pkey );
|
|
|
|
x509_crt_init( &srvcert );
|
|
|
|
|
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-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n . Initial seeding of the random generator..." );
|
2011-12-04 17:09:26 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( ret = ctr_drbg_init( &ctr_drbg, 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
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_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-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Loading the server cert. and key..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This demonstration program uses embedded test certificates.
|
2013-09-18 11:46:23 +00:00
|
|
|
* Instead, you may want to use x509_crt_parse_file() to read the
|
|
|
|
* server and CA certificates, as well as pk_parse_keyfile().
|
2011-05-20 12:33:05 +00:00
|
|
|
*/
|
2013-09-18 11:46:23 +00:00
|
|
|
ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
|
2015-03-27 08:56:18 +00:00
|
|
|
test_srv_crt_len );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-03-27 09:23:53 +00:00
|
|
|
ret = x509_crt_parse( &srvcert, (const unsigned char *) test_cas_pem,
|
|
|
|
test_cas_pem_len );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-09-15 11:01:22 +00:00
|
|
|
ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
|
2015-03-27 08:56:18 +00:00
|
|
|
test_srv_key_len, NULL, 0 );
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 2. Setup the listening TCP socket
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Bind on https://localhost:4433/ ..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
if( ( ret = net_bind( &listen_fd, NULL, 4433, NET_PROTO_TCP ) ) != 0 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 3. Wait until a client connects
|
|
|
|
*/
|
|
|
|
client_fd = -1;
|
|
|
|
memset( &ssl, 0, sizeof( ssl ) );
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Waiting for a remote connection ..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 3.5. Forking server thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Forking to handle connection ..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( pid < 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf(" failed\n ! fork returned %d\n\n", pid );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
if( pid != 0 )
|
|
|
|
{
|
2011-12-04 17:09:26 +00:00
|
|
|
if( ( ret = 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
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
close( client_fd );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
close( listen_fd );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 4. Setup stuff
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Setting up the SSL data...." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
if( ( ret = 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
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-04-17 14:02:36 +00:00
|
|
|
|
2011-05-20 12:33:05 +00:00
|
|
|
if( ( ret = ssl_init( &ssl ) ) != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
ssl_set_endpoint( &ssl, SSL_IS_SERVER );
|
|
|
|
ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
|
|
|
|
|
2015-01-12 10:40:14 +00:00
|
|
|
/* SSLv3 is deprecated, set minimum to TLS 1.0 */
|
|
|
|
ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3,
|
|
|
|
SSL_MINOR_VERSION_1 );
|
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
|
2011-05-20 12:33:05 +00:00
|
|
|
ssl_set_dbg( &ssl, my_debug, stdout );
|
2015-03-25 18:38:23 +00:00
|
|
|
ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv, NULL, 0 );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
|
2014-07-08 12:05:52 +00:00
|
|
|
if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
|
2014-07-08 12:05:52 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 5. Handshake
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Performing the SSL/TLS handshake..." );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
while( ( ret = ssl_handshake( &ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 6. Read the HTTP Request
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " < Read from client:" );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = sizeof( buf ) - 1;
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
ret = ssl_read( &ssl, buf, len );
|
|
|
|
|
|
|
|
if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( ret <= 0 )
|
|
|
|
{
|
|
|
|
switch( ret )
|
|
|
|
{
|
|
|
|
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " connection was closed gracefully\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POLARSSL_ERR_NET_CONN_RESET:
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " connection was reset by peer\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ssl_read returned %d\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " %d bytes read\n\n%s", 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
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " > Write to client:" );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
len = sprintf( (char *) buf, HTTP_RESPONSE,
|
|
|
|
ssl_get_ciphersuite( &ssl ) );
|
|
|
|
|
2014-04-17 14:03:23 +00:00
|
|
|
while( cnt++ < 100 )
|
2011-05-20 12:33:05 +00:00
|
|
|
{
|
|
|
|
while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_NET_CONN_RESET )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! peer closed the connection\n\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
|
2011-05-20 12:33:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len = ret;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " %d bytes written\n\n%s\n", len, (char *) buf );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
|
|
|
m_sleep( 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl_close_notify( &ssl );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2014-04-17 14:02:36 +00:00
|
|
|
if( client_fd != -1 )
|
|
|
|
net_close( client_fd );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
x509_crt_free( &srvcert );
|
2013-08-19 12:10:16 +00:00
|
|
|
pk_free( &pkey );
|
2011-05-20 12:33:05 +00:00
|
|
|
ssl_free( &ssl );
|
2014-06-18 14:44:11 +00:00
|
|
|
ctr_drbg_free( &ctr_drbg );
|
2013-09-28 13:23:03 +00:00
|
|
|
entropy_free( &entropy );
|
2011-05-20 12:33:05 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " Press Enter to exit this program.\n" );
|
2011-05-20 12:33:05 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2011-12-04 17:09:26 +00:00
|
|
|
#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
|
2011-05-26 13:16:06 +00:00
|
|
|
POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
|
2011-12-04 17:09:26 +00:00
|
|
|
POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */
|