libmicrohttpd

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

tls_test_common.h (7054B)


      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007 Christian Grothoff
      4  Copyright (C) 2017-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 #ifndef TLS_TEST_COMMON_H_
     23 #define TLS_TEST_COMMON_H_
     24 
     25 #include "platform.h"
     26 #include "microhttpd.h"
     27 #include <curl/curl.h>
     28 #include <sys/stat.h>
     29 #include <limits.h>
     30 #include <gnutls/gnutls.h>
     31 
     32 #ifndef CURL_VERSION_BITS
     33 #define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z))
     34 #endif /* ! CURL_VERSION_BITS */
     35 #ifndef CURL_AT_LEAST_VERSION
     36 #define CURL_AT_LEAST_VERSION(x,y,z) \
     37   (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z))
     38 #endif /* ! CURL_AT_LEAST_VERSION */
     39 
     40 #define test_data "Hello World\n"
     41 #define ca_cert_file_name SRCDIR "/test-ca.crt"
     42 
     43 #define EMPTY_PAGE \
     44   "<html><head><title>Empty page</title></head><body>Empty page</body></html>"
     45 #define PAGE_NOT_FOUND \
     46   "<html><head><title>File not found</title></head><body>File not found</body></html>"
     47 
     48 #define MHD_E_MEM "Error: memory error\n"
     49 #define MHD_E_SERVER_INIT "Error: failed to start server\n"
     50 #define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
     51 #define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
     52 #define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
     53 #define MHD_E_FAILED_TO_CONNECT \
     54   "Error: server connection could not be established\n"
     55 
     56 #ifndef MHD_STATICSTR_LEN_
     57 /**
     58  * Determine length of static string / macro strings at compile time.
     59  */
     60 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1)
     61 #endif /* ! MHD_STATICSTR_LEN_ */
     62 
     63 
     64 /* The local copy if GnuTLS IDs to avoid long #ifdefs list with various
     65  * GnuTLS versions */
     66 /**
     67  * The list of know (at the moment of writing) GnuTLS IDs of TLS versions.
     68  * Can be safely casted to/from @a gnutls_protocol_t.
     69  */
     70 enum know_gnutls_tls_id
     71 {
     72   KNOWN_BAD = 0,       /**< No TLS */
     73   KNOWN_TLS_SSLv3 = 1, /**< GNUTLS_SSL3 */
     74   KNOWN_TLS_V1_0 =  2, /**< GNUTLS_TLS1_0 */
     75   KNOWN_TLS_V1_1 =  3, /**< GNUTLS_TLS1_1 */
     76   KNOWN_TLS_V1_2 =  4, /**< GNUTLS_TLS1_2 */
     77   KNOWN_TLS_V1_3 =  5, /**< GNUTLS_TLS1_3 */
     78   KNOWN_TLS_MIN = KNOWN_TLS_SSLv3, /**< Minimum valid value */
     79   KNOWN_TLS_MAX = KNOWN_TLS_V1_3   /**< Maximum valid value */
     80 };
     81 
     82 #define KNOW_TLS_IDS_COUNT 6 /* KNOWN_TLS_MAX + 1 */
     83 /**
     84  * Map @a know_gnutls_tls_ids values to printable names.
     85  */
     86 extern const char *tls_names[KNOW_TLS_IDS_COUNT];
     87 
     88 /**
     89  * Map @a know_gnutls_tls_ids values to GnuTLS priorities strings.
     90  */
     91 extern const char *priorities_map[KNOW_TLS_IDS_COUNT];
     92 
     93 /**
     94  * Map @a know_gnutls_tls_ids values to GnuTLS priorities append strings.
     95  */
     96 extern const char *priorities_append_map[KNOW_TLS_IDS_COUNT];
     97 
     98 /**
     99  * Map @a know_gnutls_tls_ids values to libcurl @a CURLOPT_SSLVERSION value.
    100  */
    101 extern const long libcurl_tls_vers_map[KNOW_TLS_IDS_COUNT];
    102 
    103 #if CURL_AT_LEAST_VERSION (7,54,0)
    104 /**
    105  * Map @a know_gnutls_tls_ids values to libcurl @a CURLOPT_SSLVERSION value
    106  * for maximum supported TLS version.
    107  */
    108 extern const long libcurl_tls_max_vers_map[KNOW_TLS_IDS_COUNT];
    109 #endif /* CURL_AT_LEAST_VERSION(7,54,0) */
    110 
    111 struct https_test_data
    112 {
    113   void *cls;
    114   uint16_t port;
    115   const char *cipher_suite;
    116   int proto_version;
    117 };
    118 
    119 struct CBC
    120 {
    121   char *buf;
    122   size_t pos;
    123   size_t size;
    124 };
    125 
    126 int
    127 curl_tls_is_gnutls (void);
    128 
    129 int
    130 curl_tls_is_openssl (void);
    131 
    132 int
    133 curl_tls_is_nss (void);
    134 
    135 int
    136 curl_tls_is_schannel (void);
    137 
    138 int
    139 curl_tls_is_sectransport (void);
    140 
    141 
    142 enum test_get_result
    143 {
    144   TEST_GET_OK = 0,
    145   TEST_GET_ERROR = 1,
    146 
    147   TEST_GET_MHD_ERROR = 16,
    148   TEST_GET_TRANSFER_ERROR = 17,
    149 
    150   TEST_GET_CURL_GEN_ERROR = 32,
    151   TEST_GET_CURL_CA_ERROR = 33,
    152   TEST_GET_CURL_NOT_IMPLT = 34,
    153 
    154   TEST_GET_HARD_ERROR = 999
    155 };
    156 /**
    157  * perform cURL request for file
    158  */
    159 enum test_get_result
    160 test_daemon_get (void *cls,
    161                  const char *cipher_suite, int proto_version,
    162                  uint16_t port, int ver_peer);
    163 
    164 void
    165 print_test_result (unsigned int test_outcome,
    166                    const char *test_name);
    167 
    168 size_t
    169 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx);
    170 
    171 enum MHD_Result
    172 http_ahc (void *cls, struct MHD_Connection *connection,
    173           const char *url, const char *method, const char *upload_data,
    174           const char *version, size_t *upload_data_size, void **req_cls);
    175 
    176 enum MHD_Result
    177 http_dummy_ahc (void *cls, struct MHD_Connection *connection,
    178                 const char *url, const char *method, const char *upload_data,
    179                 const char *version, size_t *upload_data_size,
    180                 void **req_cls);
    181 
    182 
    183 /**
    184  * compile test URI
    185  *
    186  * @param[out] uri - char buffer into which the url is compiled
    187  * @param uri_len number of bytes available in @a url
    188  * @param port port to use for the test
    189  * @return 1 on error
    190  */
    191 unsigned int
    192 gen_test_uri (char *uri,
    193               size_t uri_len,
    194               uint16_t port);
    195 
    196 CURLcode
    197 send_curl_req (char *url,
    198                struct CBC *cbc,
    199                const char *cipher_suite,
    200                int proto_version);
    201 
    202 unsigned int
    203 test_https_transfer (void *cls,
    204                      uint16_t port,
    205                      const char *cipher_suite,
    206                      int proto_version);
    207 
    208 unsigned int
    209 setup_session (gnutls_session_t *session,
    210                gnutls_certificate_credentials_t *xcred);
    211 
    212 unsigned int
    213 teardown_session (gnutls_session_t session,
    214                   gnutls_certificate_credentials_t xcred);
    215 
    216 unsigned int
    217 test_wrap (const char *test_name, unsigned int
    218            (*test_function)(void *cls, uint16_t port, const char *cipher_suite,
    219                             int proto_version), void *cls,
    220            uint16_t port,
    221            unsigned int daemon_flags, const char *cipher_suite,
    222            int proto_version, ...);
    223 
    224 int testsuite_curl_global_init (void);
    225 
    226 /**
    227  * Check whether program name contains specific @a marker string.
    228  * Only last component in pathname is checked for marker presence,
    229  * all leading directories names (if any) are ignored. Directories
    230  * separators are handled correctly on both non-W32 and W32
    231  * platforms.
    232  * @param prog_name program name, may include path
    233  * @param marker    marker to look for.
    234  * @return zero if any parameter is NULL or empty string or
    235  *         @a prog_name ends with slash or @a marker is not found in
    236  *         program name, non-zero if @a maker is found in program
    237  *         name.
    238  */
    239 int
    240 has_in_name (const char *prog_name, const char *marker);
    241 
    242 #endif /* TLS_TEST_COMMON_H_ */