|
Packit |
875988 |
/*
|
|
Packit |
875988 |
This file is part of libmicrohttpd
|
|
Packit |
875988 |
Copyright (C) 2007 Christian Grothoff
|
|
Packit |
875988 |
|
|
Packit |
875988 |
libmicrohttpd is free software; you can redistribute it and/or modify
|
|
Packit |
875988 |
it under the terms of the GNU General Public License as published
|
|
Packit |
875988 |
by the Free Software Foundation; either version 2, or (at your
|
|
Packit |
875988 |
option) any later version.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
libmicrohttpd is distributed in the hope that it will be useful, but
|
|
Packit |
875988 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
875988 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
875988 |
General Public License for more details.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
You should have received a copy of the GNU General Public License
|
|
Packit |
875988 |
along with libmicrohttpd; see the file COPYING. If not, write to the
|
|
Packit |
875988 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit |
875988 |
Boston, MA 02110-1301, USA.
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* @file test_https_time_out.c
|
|
Packit |
875988 |
* @brief: daemon TLS alert response test-case
|
|
Packit |
875988 |
*
|
|
Packit |
875988 |
* @author Sagie Amir
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#include "platform.h"
|
|
Packit |
875988 |
#include "microhttpd.h"
|
|
Packit |
875988 |
#include "tls_test_common.h"
|
|
Packit |
875988 |
#ifdef MHD_HTTPS_REQUIRE_GRYPT
|
|
Packit |
875988 |
#include <gcrypt.h>
|
|
Packit |
875988 |
#endif /* MHD_HTTPS_REQUIRE_GRYPT */
|
|
Packit |
875988 |
#include "mhd_sockets.h" /* only macros used */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifdef _WIN32
|
|
Packit |
875988 |
#ifndef WIN32_LEAN_AND_MEAN
|
|
Packit |
875988 |
#define WIN32_LEAN_AND_MEAN 1
|
|
Packit |
875988 |
#endif /* !WIN32_LEAN_AND_MEAN */
|
|
Packit |
875988 |
#include <windows.h>
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
extern const char srv_key_pem[];
|
|
Packit |
875988 |
extern const char srv_self_signed_cert_pem[];
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static const int TIME_OUT = 3;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int
|
|
Packit |
875988 |
test_tls_session_time_out (gnutls_session_t session, int port)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
int ret;
|
|
Packit |
875988 |
MHD_socket sd;
|
|
Packit |
875988 |
struct sockaddr_in sa;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
sd = socket (AF_INET, SOCK_STREAM, 0);
|
|
Packit |
875988 |
if (sd == MHD_INVALID_SOCKET)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
memset (&sa, '\0', sizeof (struct sockaddr_in));
|
|
Packit |
875988 |
sa.sin_family = AF_INET;
|
|
Packit |
875988 |
sa.sin_port = htons (port);
|
|
Packit |
875988 |
sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) sd);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
ret = connect (sd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in));
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (ret < 0)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
|
|
Packit |
875988 |
MHD_socket_close_chk_ (sd);
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
ret = gnutls_handshake (session);
|
|
Packit |
875988 |
if (ret < 0)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, "Handshake failed\n");
|
|
Packit |
875988 |
MHD_socket_close_chk_ (sd);
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
(void)sleep (TIME_OUT + 1);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/* check that server has closed the connection */
|
|
Packit |
875988 |
/* TODO better RST trigger */
|
|
Packit |
875988 |
if (send (sd, "", 1, 0) == 0)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, "Connection failed to time-out\n");
|
|
Packit |
875988 |
MHD_socket_close_chk_ (sd);
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
MHD_socket_close_chk_ (sd);
|
|
Packit |
875988 |
return 0;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
int
|
|
Packit |
875988 |
main (int argc, char *const *argv)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
int errorCount = 0;;
|
|
Packit |
875988 |
struct MHD_Daemon *d;
|
|
Packit |
875988 |
gnutls_session_t session;
|
|
Packit |
875988 |
gnutls_datum_t key;
|
|
Packit |
875988 |
gnutls_datum_t cert;
|
|
Packit |
875988 |
gnutls_certificate_credentials_t xcred;
|
|
Packit |
875988 |
int port;
|
|
Packit |
875988 |
(void)argc; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
|
|
Packit |
875988 |
port = 0;
|
|
Packit |
875988 |
else
|
|
Packit |
875988 |
port = 3070;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifdef MHD_HTTPS_REQUIRE_GRYPT
|
|
Packit |
875988 |
gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
|
|
Packit |
875988 |
#ifdef GCRYCTL_INITIALIZATION_FINISHED
|
|
Packit |
875988 |
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
#endif /* MHD_HTTPS_REQUIRE_GRYPT */
|
|
Packit |
875988 |
gnutls_global_init ();
|
|
Packit |
875988 |
gnutls_global_set_log_level (11);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS |
|
|
Packit |
875988 |
MHD_USE_ERROR_LOG, port,
|
|
Packit |
875988 |
NULL, NULL, &http_dummy_ahc, NULL,
|
|
Packit |
875988 |
MHD_OPTION_CONNECTION_TIMEOUT, TIME_OUT,
|
|
Packit |
875988 |
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
|
|
Packit |
875988 |
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
|
|
Packit |
875988 |
MHD_OPTION_END);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (NULL == d)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, MHD_E_SERVER_INIT);
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
if (0 == port)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
const union MHD_DaemonInfo *dinfo;
|
|
Packit |
875988 |
dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
|
|
Packit |
875988 |
if (NULL == dinfo || 0 == dinfo->port)
|
|
Packit |
875988 |
{ MHD_stop_daemon (d); return -1; }
|
|
Packit |
875988 |
port = (int)dinfo->port;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (0 != setup_session (&session, &key, &cert, &xcred))
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr, "failed to setup session\n");
|
|
Packit |
875988 |
return 1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
errorCount += test_tls_session_time_out (session, port);
|
|
Packit |
875988 |
teardown_session (session, &key, &cert, xcred);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
print_test_result (errorCount, argv[0]);
|
|
Packit |
875988 |
|
|
Packit |
875988 |
MHD_stop_daemon (d);
|
|
Packit |
875988 |
gnutls_global_deinit ();
|
|
Packit |
875988 |
|
|
Packit |
875988 |
return errorCount != 0 ? 1 : 0;
|
|
Packit |
875988 |
}
|