libmicrohttpd

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

test_tls_authentication.c (4274B)


      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007 Christian Grothoff
      4  Copyright (C) 2016-2022 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_tls_authentication.c
     24  * @brief  Testcase for libmicrohttpd HTTPS GET operations with CA-signed TLS server certificate
     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 #include <limits.h>
     33 #include <sys/stat.h>
     34 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
     35 #include <gcrypt.h>
     36 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
     37 #include "tls_test_common.h"
     38 #include "tls_test_keys.h"
     39 
     40 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     41    test into a marked, classifiable test error (TESTING.md, P5). */
     42 #include "mhd_panic_tripwire.h"
     43 
     44 
     45 /* perform a HTTP GET request via SSL/TLS */
     46 static unsigned int
     47 test_secure_get (void *cls, const char *cipher_suite, int proto_version)
     48 {
     49   enum test_get_result ret;
     50   struct MHD_Daemon *d;
     51   uint16_t port;
     52   (void) cls;    /* Unused. Silent compiler warning. */
     53 
     54   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
     55     port = 0;
     56   else
     57     port = 3075;
     58 
     59   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
     60                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
     61                         | MHD_USE_ERROR_LOG, port,
     62                         NULL, NULL, &http_ahc, NULL,
     63                         MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
     64                         MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
     65                         MHD_OPTION_END);
     66 
     67   if (d == NULL)
     68   {
     69     fprintf (stderr, MHD_E_SERVER_INIT);
     70     return 1;
     71   }
     72   if (0 == port)
     73   {
     74     const union MHD_DaemonInfo *dinfo;
     75     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
     76     if ((NULL == dinfo) || (0 == dinfo->port) )
     77     {
     78       MHD_stop_daemon (d);
     79       return 1;
     80     }
     81     port = dinfo->port;
     82   }
     83 
     84   ret = test_daemon_get (NULL, cipher_suite, proto_version, port, 1);
     85 
     86   MHD_stop_daemon (d);
     87   if (TEST_GET_HARD_ERROR == ret)
     88     return 99;
     89   if (TEST_GET_CURL_GEN_ERROR == ret)
     90   {
     91     fprintf (stderr, "libcurl error.\nTest aborted.\n");
     92     return 99;
     93   }
     94   if ((TEST_GET_CURL_CA_ERROR == ret) ||
     95       (TEST_GET_CURL_NOT_IMPLT == ret))
     96   {
     97     fprintf (stderr, "libcurl TLS backend does not support custom CA.\n"
     98              "Test skipped.\n");
     99     return 77;
    100   }
    101   return TEST_GET_OK == ret ? 0 : 1;
    102 }
    103 
    104 
    105 int
    106 main (int argc, char *const *argv)
    107 {
    108   unsigned int errorCount;
    109   (void) argc;
    110   (void) argv;       /* Unused. Silent compiler warning. */
    111 
    112 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
    113   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    114 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    115   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    116 #endif
    117 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
    118   if (! testsuite_curl_global_init ())
    119     return 99;
    120   if (NULL == curl_version_info (CURLVERSION_NOW)->ssl_version)
    121   {
    122     fprintf (stderr, "Curl does not support SSL.  Cannot run the test.\n");
    123     curl_global_cleanup ();
    124     return 77;
    125   }
    126 #if ! CURL_AT_LEAST_VERSION (7,60,0)
    127   if (curl_tls_is_schannel ())
    128   {
    129     fprintf (stderr, "libcurl before version 7.60.0 does not support "
    130              "custom CA with Schannel backend.\nTest skipped.\n");
    131     curl_global_cleanup ();
    132     return 77;
    133   }
    134 #endif /* ! CURL_AT_LEAST_VERSION(7,60,0) */
    135 
    136   errorCount =
    137     test_secure_get (NULL, NULL, CURL_SSLVERSION_DEFAULT);
    138 
    139   print_test_result (errorCount, argv[0]);
    140 
    141   curl_global_cleanup ();
    142   if (77 == errorCount)
    143     return 77;
    144   if (99 == errorCount)
    145     return 77;
    146   return errorCount != 0 ? 1 : 0;
    147 }