libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_https_session_info.c (11330B)


      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007, 2016 Christian Grothoff
      4  Copyright (C) 2016-2021 Evgeny Grin (Karlson2k)
      5 
      6  libmicrohttpd is free software; you can redistribute it and/or modify
      7  it under the terms of the GNU General Public License as published
      8  by the Free Software Foundation; either version 2, or (at your
      9  option) any later version.
     10 
     11  libmicrohttpd is distributed in the hope that it will be useful, but
     12  WITHOUT ANY WARRANTY; without even the implied warranty of
     13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  General Public License for more details.
     15 
     16  You should have received a copy of the GNU General Public License
     17  along with libmicrohttpd; see the file COPYING.  If not, write to the
     18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  Boston, MA 02110-1301, USA.
     20  */
     21 
     22 /**
     23  * @file test_https_session_info.c
     24  * @brief  Testcase for libmicrohttpd HTTPS connection querying operations
     25  * @author Sagie Amir
     26  * @author Karlson2k (Evgeny Grin)
     27  */
     28 
     29 #include "platform.h"
     30 #include "microhttpd.h"
     31 #include <curl/curl.h>
     32 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
     33 #include <gcrypt.h>
     34 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
     35 #include "tls_test_common.h"
     36 #include "tls_test_keys.h"
     37 
     38 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     39    test into a marked, classifiable test error (TESTING.md, P5). */
     40 #include "mhd_panic_tripwire.h"
     41 
     42 
     43 static int test_append_prio;
     44 
     45 /*
     46  * HTTP access handler call back
     47  * used to query negotiated security parameters
     48  */
     49 static enum MHD_Result
     50 query_info_ahc (void *cls, struct MHD_Connection *connection,
     51                 const char *url, const char *method,
     52                 const char *version, const char *upload_data,
     53                 size_t *upload_data_size, void **req_cls)
     54 {
     55   struct MHD_Response *response;
     56   enum MHD_Result ret;
     57   const union MHD_ConnectionInfo *conn_info;
     58   enum know_gnutls_tls_id *used_tls_ver;
     59   (void) url; (void) method; (void) version;   /* Unused. Silent compiler warning. */
     60   (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */
     61   used_tls_ver = (enum know_gnutls_tls_id *) cls;
     62 
     63   if (NULL == *req_cls)
     64   {
     65     *req_cls = (void *) &query_info_ahc;
     66     return MHD_YES;
     67   }
     68 
     69   conn_info = MHD_get_connection_info (connection,
     70                                        MHD_CONNECTION_INFO_PROTOCOL);
     71   if (NULL == conn_info)
     72   {
     73     fflush (stderr);
     74     fflush (stdout);
     75     fprintf (stderr, "MHD_get_connection_info() failed.\n");
     76     fflush (stderr);
     77     return MHD_NO;
     78   }
     79   if (0 == (unsigned int) conn_info->protocol)
     80   {
     81     fflush (stderr);
     82     fflush (stdout);
     83     fprintf (stderr, "MHD_get_connection_info()->protocol has "
     84              "wrong zero value.\n");
     85     fflush (stderr);
     86     return MHD_NO;
     87   }
     88   *used_tls_ver = (enum know_gnutls_tls_id) conn_info->protocol;
     89 
     90   response = MHD_create_response_from_buffer_static (strlen (EMPTY_PAGE),
     91                                                      EMPTY_PAGE);
     92   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     93   MHD_destroy_response (response);
     94   return ret;
     95 }
     96 
     97 
     98 /**
     99  * negotiate a secure connection with server & query negotiated security parameters
    100  */
    101 static unsigned int
    102 test_query_session (enum know_gnutls_tls_id tls_ver, uint16_t *pport)
    103 {
    104   CURL *c;
    105   struct CBC cbc;
    106   CURLcode errornum;
    107   char url[256];
    108   enum know_gnutls_tls_id found_tls_ver;
    109   struct MHD_Daemon *d;
    110 
    111   if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
    112     return 99;
    113   cbc.size = 255;
    114   cbc.pos = 0;
    115 
    116   /* setup test */
    117   found_tls_ver = KNOWN_BAD;
    118   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    119                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
    120                         | MHD_USE_ERROR_LOG, *pport,
    121                         NULL, NULL,
    122                         &query_info_ahc, &found_tls_ver,
    123                         test_append_prio ?
    124                         MHD_OPTION_HTTPS_PRIORITIES_APPEND :
    125                         MHD_OPTION_HTTPS_PRIORITIES,
    126                         test_append_prio ?
    127                         priorities_append_map[tls_ver] :
    128                         priorities_map[tls_ver],
    129                         MHD_OPTION_HTTPS_MEM_KEY, srv_self_signed_key_pem,
    130                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
    131                         MHD_OPTION_END);
    132 
    133   if (d == NULL)
    134   {
    135     free (cbc.buf);
    136     fprintf (stderr, "MHD_start_daemon() with %s failed.\n",
    137              tls_names[tls_ver]);
    138     fflush (stderr);
    139     return 77;
    140   }
    141   if (0 == *pport)
    142   {
    143     const union MHD_DaemonInfo *dinfo;
    144     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    145     if ((NULL == dinfo) || (0 == dinfo->port) )
    146     {
    147       MHD_stop_daemon (d);
    148       free (cbc.buf);
    149       fprintf (stderr, "MHD_get_daemon_info() failed.\n");
    150       fflush (stderr);
    151       return 10;
    152     }
    153     *pport = dinfo->port; /* Use the same port for rest of the checks */
    154   }
    155 
    156   gen_test_uri (url,
    157                 sizeof (url),
    158                 *pport);
    159   c = curl_easy_init ();
    160   fflush (stderr);
    161   if (NULL == c)
    162   {
    163     fprintf (stderr, "curl_easy_init() failed.\n");
    164     fflush (stderr);
    165     MHD_stop_daemon (d);
    166     free (cbc.buf);
    167     return 99;
    168   }
    169 #ifdef _DEBUG
    170   curl_easy_setopt (c, CURLOPT_VERBOSE, 1L);
    171 #endif
    172 
    173   if ((CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_URL, url))) ||
    174       (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    175                                                  CURL_HTTP_VERSION_1_1))) ||
    176       (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L))) ||
    177       (CURLE_OK !=
    178        (errornum = curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L))) ||
    179       (CURLE_OK !=
    180        (errornum = curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer))) ||
    181       (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_WRITEDATA,
    182                                                  &cbc))) ||
    183       /* TLS options */
    184       /* currently skip any peer authentication */
    185       (CURLE_OK !=
    186        (errornum = curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L))) ||
    187       (CURLE_OK !=
    188        (errornum = curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L))) ||
    189       (CURLE_OK !=
    190        (errornum = curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L))) ||
    191       (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L))) ||
    192       (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_PROXY, ""))))
    193   {
    194     curl_easy_cleanup (c);
    195     free (cbc.buf);
    196     MHD_stop_daemon (d);
    197     fflush (stderr);
    198     fflush (stdout);
    199     fprintf (stderr, "Error setting libcurl option: %s.\n",
    200              curl_easy_strerror (errornum));
    201     fflush (stderr);
    202     return 99;
    203   }
    204 
    205   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    206   {
    207     unsigned int ret;
    208     curl_easy_cleanup (c);
    209     free (cbc.buf);
    210     MHD_stop_daemon (d);
    211 
    212     fflush (stderr);
    213     fflush (stdout);
    214     if ((CURLE_SSL_CONNECT_ERROR == errornum) ||
    215         (CURLE_SSL_CIPHER == errornum))
    216     {
    217       ret = 77;
    218       fprintf (stderr, "libcurl request failed due to TLS error: '%s'\n",
    219                curl_easy_strerror (errornum));
    220 
    221     }
    222     else
    223     {
    224       ret = 1;
    225       fprintf (stderr, "curl_easy_perform failed: '%s'\n",
    226                curl_easy_strerror (errornum));
    227     }
    228     fflush (stderr);
    229 
    230     return ret;
    231   }
    232 
    233   curl_easy_cleanup (c);
    234   free (cbc.buf);
    235   MHD_stop_daemon (d);
    236 
    237   if (tls_ver != found_tls_ver)
    238   {
    239     fflush (stderr);
    240     fflush (stdout);
    241     fprintf (stderr, "MHD_get_connection_info (conn, "
    242              "MHD_CONNECTION_INFO_PROTOCOL) returned unexpected "
    243              "protocol version.\n"
    244              "\tReturned: %s (%u)\tExpected: %s (%u)\n",
    245              ((unsigned int) found_tls_ver) > KNOWN_TLS_MAX ?
    246              "[wrong value]" : tls_names[found_tls_ver],
    247              (unsigned int) found_tls_ver,
    248              tls_names[tls_ver], (unsigned int) tls_ver);
    249     fflush (stderr);
    250     return 2;
    251   }
    252   return 0;
    253 }
    254 
    255 
    256 static unsigned int
    257 test_all_supported_versions (void)
    258 {
    259   enum know_gnutls_tls_id ver_for_test; /**< TLS version used for test */
    260   const gnutls_protocol_t *vers_list;    /**< The list of GnuTLS supported TLS versions */
    261   uint16_t port;
    262   unsigned int num_success; /**< Number of tests succeeded */
    263   unsigned int num_failed;  /**< Number of tests failed */
    264 
    265   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    266     port = 0;     /* Use system automatic assignment */
    267   else
    268     port = 3060;  /* Use predefined port, may break parallel testing of another MHD build */
    269 
    270   vers_list = gnutls_protocol_list ();
    271   if (NULL == vers_list)
    272   {
    273     fprintf (stderr, "Error getting GnuTLS supported TLS versions");
    274     return 99;
    275   }
    276   num_success = 0;
    277   num_failed = 0;
    278 
    279   for (ver_for_test = KNOWN_TLS_MIN; KNOWN_TLS_MAX >= ver_for_test;
    280        ++ver_for_test)
    281   {
    282     const gnutls_protocol_t *ver_ptr;      /**< The pointer to the position on the @a vers_list */
    283     unsigned int res;
    284     for (ver_ptr = vers_list; 0 != *ver_ptr; ++ver_ptr)
    285     {
    286       if (ver_for_test == (enum know_gnutls_tls_id) *ver_ptr)
    287         break;
    288     }
    289     if (0 == *ver_ptr)
    290     {
    291       printf ("%s is not supported by GnuTLS, skipping.\n\n",
    292               tls_names[ver_for_test]);
    293       fflush (stdout);
    294       continue;
    295     }
    296     printf ("Starting check for %s...\n",
    297             tls_names[ver_for_test]);
    298     fflush (stdout);
    299     res = test_query_session (ver_for_test, &port);
    300     fflush (stderr);
    301     fflush (stdout);
    302     if (99 == res)
    303     {
    304       fprintf (stderr, "Hard error. Test stopped.\n");
    305       fflush (stderr);
    306       return 99;
    307     }
    308     else if (77 == res)
    309     {
    310       printf ("%s does not work with libcurl client and GnuTLS "
    311               "server combination, skipping.\n",
    312               tls_names[ver_for_test]);
    313       fflush (stdout);
    314     }
    315     else if (0 != res)
    316     {
    317       fprintf (stderr, "Check failed for %s.\n",
    318                tls_names[ver_for_test]);
    319       fflush (stderr);
    320       num_failed++;
    321     }
    322     else
    323     {
    324       printf ("Check succeeded for %s.\n",
    325               tls_names[ver_for_test]);
    326       fflush (stdout);
    327       num_success++;
    328     }
    329     printf ("\n");
    330     fflush (stdout);
    331   }
    332 
    333   if (0 == num_failed)
    334   {
    335     if (0 == num_success)
    336     {
    337       fprintf (stderr, "No supported TLS version was found.\n");
    338       fflush (stderr);
    339       return 77;
    340     }
    341     return 0;
    342   }
    343   return num_failed;
    344 }
    345 
    346 
    347 int
    348 main (int argc, char *const *argv)
    349 {
    350   unsigned int errorCount = 0;
    351   const char *ssl_version;
    352   (void) argc;   /* Unused. Silent compiler warning. */
    353 
    354 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
    355   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    356 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    357   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    358 #endif
    359 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
    360   test_append_prio = has_in_name (argv[0], "_append");
    361   if (! testsuite_curl_global_init ())
    362     return 99;
    363 
    364   ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version;
    365   if (NULL == ssl_version)
    366   {
    367     fprintf (stderr, "Curl does not support SSL.  Cannot run the test.\n");
    368     curl_global_cleanup ();
    369     return 77;
    370   }
    371   errorCount = test_all_supported_versions ();
    372   fflush (stderr);
    373   fflush (stdout);
    374   curl_global_cleanup ();
    375   if (77 == errorCount)
    376     return 77;
    377   else if (99 == errorCount)
    378     return 99;
    379   print_test_result (errorCount, argv[0]);
    380   return errorCount != 0 ? 1 : 0;
    381 }