test_tls_options.c (14607B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2016 Christian Grothoff 4 Copyright (C) 2014-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_options.c 24 * @brief Testcase for libmicrohttpd HTTPS TLS version match/mismatch 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 * HTTP access handler call back 44 * used to query negotiated security parameters 45 */ 46 static enum MHD_Result 47 simple_ahc (void *cls, struct MHD_Connection *connection, 48 const char *url, const char *method, 49 const char *version, const char *upload_data, 50 size_t *upload_data_size, void **req_cls) 51 { 52 struct MHD_Response *response; 53 enum MHD_Result ret; 54 (void) cls; (void) url; (void) method; (void) version; /* Unused. Silent compiler warning. */ 55 (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */ 56 57 if (NULL == *req_cls) 58 { 59 *req_cls = (void *) &simple_ahc; 60 return MHD_YES; 61 } 62 63 response = 64 MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (EMPTY_PAGE), 65 EMPTY_PAGE); 66 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 67 MHD_destroy_response (response); 68 return ret; 69 } 70 71 72 enum check_result 73 { 74 CHECK_RES_OK = 0, 75 CHECK_RES_ERR = 1, 76 77 CHECK_RES_MHD_START_FAILED = 17, 78 CHECK_RES_CURL_TLS_INIT_FAIL = 18, 79 CHECK_RES_CURL_TLS_CONN_FAIL = 19, 80 81 CHECK_RES_HARD_ERROR = 99 82 }; 83 84 static enum check_result 85 check_tls_match_inner (enum know_gnutls_tls_id tls_ver_mhd, 86 enum know_gnutls_tls_id tls_ver_libcurl, 87 uint16_t *pport, 88 struct MHD_Daemon **d_ptr, 89 struct CBC *pcbc, 90 CURL **c_ptr) 91 { 92 CURLcode errornum; 93 char url[256]; 94 int libcurl_tls_set; 95 CURL *c; 96 struct MHD_Daemon *d; 97 98 /* setup test */ 99 d = 100 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 101 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS 102 | MHD_USE_ERROR_LOG, *pport, 103 NULL, NULL, 104 &simple_ahc, NULL, 105 MHD_OPTION_HTTPS_PRIORITIES, priorities_map[tls_ver_mhd], 106 MHD_OPTION_HTTPS_MEM_KEY, srv_self_signed_key_pem, 107 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, 108 MHD_OPTION_END); 109 fflush (stderr); 110 fflush (stdout); 111 *d_ptr = d; 112 113 if (d == NULL) 114 { 115 fprintf (stderr, "MHD_start_daemon() with %s failed.\n", 116 tls_names[tls_ver_mhd]); 117 return CHECK_RES_MHD_START_FAILED; 118 } 119 if (0 == *pport) 120 { 121 const union MHD_DaemonInfo *dinfo; 122 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 123 if ((NULL == dinfo) || (0 == dinfo->port) ) 124 { 125 fprintf (stderr, "MHD_get_daemon_info() failed.\n"); 126 return CHECK_RES_ERR; 127 } 128 *pport = dinfo->port; /* Use the same port for rest of the checks */ 129 } 130 131 if (0 != gen_test_uri (url, 132 sizeof (url), 133 *pport)) 134 { 135 fprintf (stderr, "failed to generate URI.\n"); 136 return CHECK_RES_CURL_TLS_INIT_FAIL; 137 } 138 c = curl_easy_init (); 139 fflush (stderr); 140 fflush (stdout); 141 *c_ptr = c; 142 if (NULL == c) 143 { 144 fprintf (stderr, "curl_easy_init() failed.\n"); 145 return CHECK_RES_HARD_ERROR; 146 } 147 #ifdef _DEBUG 148 curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); 149 #endif 150 151 if ((CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_URL, url))) || 152 (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_HTTP_VERSION, 153 CURL_HTTP_VERSION_1_1))) || 154 (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L))) || 155 (CURLE_OK != 156 (errornum = curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L))) || 157 (CURLE_OK != 158 (errornum = curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer))) || 159 (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_WRITEDATA, 160 pcbc))) || 161 /* TLS options */ 162 /* currently skip any peer authentication */ 163 (CURLE_OK != 164 (errornum = curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L))) || 165 (CURLE_OK != 166 (errornum = curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L))) || 167 (CURLE_OK != 168 (errornum = curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L))) || 169 (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L))) || 170 (CURLE_OK != (errornum = curl_easy_setopt (c, CURLOPT_PROXY, "")))) 171 { 172 fflush (stderr); 173 fflush (stdout); 174 fprintf (stderr, "Error setting libcurl option: %s.\n", 175 curl_easy_strerror (errornum)); 176 return CHECK_RES_HARD_ERROR; 177 } 178 libcurl_tls_set = 0; 179 #if CURL_AT_LEAST_VERSION (7,54,0) 180 if (CURL_SSLVERSION_MAX_DEFAULT != 181 libcurl_tls_max_vers_map[tls_ver_libcurl]) 182 { 183 errornum = curl_easy_setopt (c, CURLOPT_SSLVERSION, 184 libcurl_tls_vers_map[tls_ver_libcurl] 185 | libcurl_tls_max_vers_map[tls_ver_libcurl]); 186 if (CURLE_OK == errornum) 187 libcurl_tls_set = 1; 188 else 189 { 190 fprintf (stderr, "Error setting libcurl TLS version range: " 191 "%s.\nRetrying with minimum TLS version only.\n", 192 curl_easy_strerror (errornum)); 193 } 194 } 195 #endif /* CURL_AT_LEAST_VERSION(7,54,0) */ 196 if (! libcurl_tls_set && 197 (CURLE_OK != 198 (errornum = curl_easy_setopt (c, CURLOPT_SSLVERSION, 199 libcurl_tls_vers_map[tls_ver_libcurl])))) 200 { 201 fprintf (stderr, "Error setting libcurl minimum TLS version: %s.\n", 202 curl_easy_strerror (errornum)); 203 return CHECK_RES_CURL_TLS_INIT_FAIL; 204 } 205 206 errornum = curl_easy_perform (c); 207 fflush (stderr); 208 fflush (stdout); 209 if (CURLE_OK != errornum) 210 { 211 if ((CURLE_SSL_CONNECT_ERROR == errornum) || 212 (CURLE_SSL_CIPHER == errornum)) 213 { 214 fprintf (stderr, "libcurl request failed due to TLS error: '%s'\n", 215 curl_easy_strerror (errornum)); 216 return CHECK_RES_CURL_TLS_CONN_FAIL; 217 218 } 219 else 220 { 221 fprintf (stderr, "curl_easy_perform failed: '%s'\n", 222 curl_easy_strerror (errornum)); 223 return CHECK_RES_ERR; 224 } 225 } 226 return CHECK_RES_OK; 227 } 228 229 230 /** 231 * negotiate a secure connection with server with specific TLS versions 232 * set for MHD and for libcurl 233 */ 234 static enum check_result 235 check_tls_match (enum know_gnutls_tls_id tls_ver_mhd, 236 enum know_gnutls_tls_id tls_ver_libcurl, 237 uint16_t *pport) 238 { 239 CURL *c; 240 struct CBC cbc; 241 enum check_result ret; 242 struct MHD_Daemon *d; 243 244 if (NULL == (cbc.buf = malloc (sizeof (char) * 255))) 245 return CHECK_RES_HARD_ERROR; 246 cbc.size = 255; 247 cbc.pos = 0; 248 249 d = NULL; 250 c = NULL; 251 ret = check_tls_match_inner (tls_ver_mhd, tls_ver_libcurl, pport, 252 &d, &cbc, &c); 253 fflush (stderr); 254 fflush (stdout); 255 if (NULL != d) 256 MHD_stop_daemon (d); 257 if (NULL != c) 258 curl_easy_cleanup (c); 259 free (cbc.buf); 260 261 return ret; 262 } 263 264 265 static unsigned int 266 test_first_supported_versions (void) 267 { 268 enum know_gnutls_tls_id ver_for_check; /**< TLS version used for test */ 269 const gnutls_protocol_t *vers_list; /**< The list of GnuTLS supported TLS versions */ 270 uint16_t port; 271 272 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 273 port = 0; /* Use system automatic assignment */ 274 else 275 port = 3080; /* Use predefined port, may break parallel testing of another MHD build */ 276 277 vers_list = gnutls_protocol_list (); 278 if (NULL == vers_list) 279 { 280 fprintf (stderr, "Error getting GnuTLS supported TLS versions"); 281 return 99; 282 } 283 284 for (ver_for_check = KNOWN_TLS_MIN; KNOWN_TLS_MAX >= ver_for_check; 285 ++ver_for_check) 286 { 287 const gnutls_protocol_t *ver_ptr; /**< The pointer to the position on the @a vers_list */ 288 enum check_result res; 289 for (ver_ptr = vers_list; 0 != *ver_ptr; ++ver_ptr) 290 { 291 if (ver_for_check == (enum know_gnutls_tls_id) *ver_ptr) 292 break; 293 } 294 if (0 == *ver_ptr) 295 { 296 printf ("%s is not supported by GnuTLS, skipping.\n\n", 297 tls_names[ver_for_check]); 298 fflush (stdout); 299 continue; 300 } 301 if (CURL_SSLVERSION_LAST == libcurl_tls_vers_map[ver_for_check]) 302 { 303 printf ("%s is not supported by libcurl, skipping.\n\n", 304 tls_names[ver_for_check]); 305 fflush (stdout); 306 continue; 307 } 308 /* Found some TLS version that supported by GnuTLS and should be supported 309 by libcurl (but in practice support depends on used TLS library) */ 310 311 if (KNOWN_TLS_MIN != ver_for_check) 312 printf ("\n"); 313 printf ("Starting check with MHD set to '%s' and " 314 "libcurl set to '%s' (successful connection is expected)...\n", 315 tls_names[ver_for_check], tls_names[ver_for_check]); 316 fflush (stdout); 317 318 /* Check with MHD and libcurl set to the same TLS version */ 319 res = check_tls_match (ver_for_check, ver_for_check, &port); 320 if (CHECK_RES_HARD_ERROR == res) 321 { 322 fprintf (stderr, "Hard error. Test stopped.\n"); 323 fflush (stderr); 324 return 99; 325 } 326 else if (CHECK_RES_ERR == res) 327 { 328 printf ("Test failed.\n"); 329 fflush (stdout); 330 return 2; 331 } 332 else if (CHECK_RES_MHD_START_FAILED == res) 333 { 334 printf ("Skipping '%s' as MHD cannot be started with this setting.\n", 335 tls_names[ver_for_check]); 336 fflush (stdout); 337 continue; 338 } 339 else if (CHECK_RES_CURL_TLS_INIT_FAIL == res) 340 { 341 printf ("Skipping '%s' as libcurl rejected this setting.\n", 342 tls_names[ver_for_check]); 343 fflush (stdout); 344 continue; 345 } 346 else if (CHECK_RES_CURL_TLS_CONN_FAIL == res) 347 { 348 printf ("Skipping '%s' as it is not supported by current libcurl " 349 "and GnuTLS combination.\n", 350 tls_names[ver_for_check]); 351 fflush (stdout); 352 continue; 353 } 354 printf ("Connection succeeded for MHD set to '%s' and " 355 "libcurl set to '%s'.\n\n", 356 tls_names[ver_for_check], tls_names[ver_for_check]); 357 358 /* Check with libcurl set to the next TLS version relative to MHD setting */ 359 if (KNOWN_TLS_MAX == ver_for_check) 360 { 361 printf ("Test is incomplete as the latest known TLS version ('%s') " 362 "was found as minimum working version.\nThere is no space to " 363 "advance to the next version.\nAssuming that test is fine.\n", 364 tls_names[ver_for_check]); 365 fflush (stdout); 366 return 0; 367 } 368 if (CURL_SSLVERSION_LAST == libcurl_tls_vers_map[ver_for_check + 1]) 369 { 370 printf ("Test is incomplete as '%s' is the latest version supported " 371 "by libcurl.\nThere is no space to " 372 "advance to the next version.\nAssuming that test is fine.\n", 373 tls_names[ver_for_check]); 374 fflush (stdout); 375 return 0; 376 } 377 printf ("Starting check with MHD set to '%s' and " 378 "minimum libcurl TLS version set to '%s' " 379 "(failed connection is expected)...\n", 380 tls_names[ver_for_check], tls_names[ver_for_check + 1]); 381 fflush (stdout); 382 res = check_tls_match (ver_for_check, ver_for_check + 1, 383 &port); 384 if (CHECK_RES_HARD_ERROR == res) 385 { 386 fprintf (stderr, "Hard error. Test stopped.\n"); 387 fflush (stderr); 388 return 99; 389 } 390 else if (CHECK_RES_ERR == res) 391 { 392 printf ("Test failed.\n"); 393 fflush (stdout); 394 return 2; 395 } 396 else if (CHECK_RES_MHD_START_FAILED == res) 397 { 398 printf ("MHD cannot be started for the second time with " 399 "the same setting.\n"); 400 fflush (stdout); 401 return 4; 402 } 403 else if (CHECK_RES_CURL_TLS_INIT_FAIL == res) 404 { 405 printf ("'%s' has been rejected by libcurl.\n" 406 "Assuming that test is fine.\n", 407 tls_names[ver_for_check + 1]); 408 fflush (stdout); 409 return 0; 410 } 411 else if (CHECK_RES_CURL_TLS_CONN_FAIL == res) 412 { 413 printf ("As expected, libcurl cannot connect to MHD when libcurl " 414 "minimum TLS version is set to '%s' while MHD TLS version set " 415 "to '%s'.\n" 416 "Test succeeded.\n", 417 tls_names[ver_for_check + 1], tls_names[ver_for_check]); 418 fflush (stdout); 419 return 0; 420 } 421 } 422 423 fprintf (stderr, "The test skipped: No know TLS versions are supported by " 424 "both MHD and libcurl.\n"); 425 fflush (stderr); 426 return 77; 427 } 428 429 430 int 431 main (int argc, char *const *argv) 432 { 433 unsigned int errorCount = 0; 434 const char *ssl_version; 435 (void) argc; /* Unused. Silent compiler warning. */ 436 437 #ifdef MHD_HTTPS_REQUIRE_GCRYPT 438 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); 439 #ifdef GCRYCTL_INITIALIZATION_FINISHED 440 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 441 #endif 442 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */ 443 if (! testsuite_curl_global_init ()) 444 return 99; 445 446 ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version; 447 if (NULL == ssl_version) 448 { 449 fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n"); 450 curl_global_cleanup (); 451 return 77; 452 } 453 errorCount = test_first_supported_versions (); 454 fflush (stderr); 455 fflush (stdout); 456 curl_global_cleanup (); 457 if (77 == errorCount) 458 return 77; 459 else if (99 == errorCount) 460 return 99; 461 print_test_result (errorCount, argv[0]); 462 return errorCount != 0 ? 1 : 0; 463 }