test_https_session_info.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007, 2016 Christian Grothoff
  4. Copyright (C) 2016-2021 Evgeny Grin (Karlson2k)
  5. libmicrohttpd is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published
  7. by the Free Software Foundation; either version 2, or (at your
  8. option) any later version.
  9. libmicrohttpd is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with libmicrohttpd; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. /**
  19. * @file mhds_session_info_test.c
  20. * @brief Testcase for libmicrohttpd HTTPS connection querying operations
  21. * @author Sagie Amir
  22. * @author Karlson2k (Evgeny Grin)
  23. */
  24. #include "platform.h"
  25. #include "microhttpd.h"
  26. #include <curl/curl.h>
  27. #ifdef MHD_HTTPS_REQUIRE_GRYPT
  28. #include <gcrypt.h>
  29. #endif /* MHD_HTTPS_REQUIRE_GRYPT */
  30. #include "tls_test_common.h"
  31. extern const char srv_key_pem[];
  32. extern const char srv_self_signed_cert_pem[];
  33. struct MHD_Daemon *d;
  34. /*
  35. * HTTP access handler call back
  36. * used to query negotiated security parameters
  37. */
  38. static enum MHD_Result
  39. query_session_ahc (void *cls, struct MHD_Connection *connection,
  40. const char *url, const char *method,
  41. const char *version, const char *upload_data,
  42. size_t *upload_data_size, void **req_cls)
  43. {
  44. struct MHD_Response *response;
  45. enum MHD_Result ret;
  46. int gret;
  47. (void) cls; (void) url; (void) method; (void) version; /* Unused. Silent compiler warning. */
  48. (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */
  49. if (NULL == *req_cls)
  50. {
  51. *req_cls = (void *) &query_session_ahc;
  52. return MHD_YES;
  53. }
  54. if (GNUTLS_TLS1_1 !=
  55. (gret = MHD_get_connection_info
  56. (connection,
  57. MHD_CONNECTION_INFO_PROTOCOL)->protocol))
  58. {
  59. if (GNUTLS_TLS1_2 == gret)
  60. {
  61. /* as usual, TLS implementations sometimes don't
  62. quite do what was asked, just mildly complain... */
  63. fprintf (stderr,
  64. "Warning: requested TLS 1.1, got TLS 1.2\n");
  65. }
  66. else
  67. {
  68. /* really different version... */
  69. fprintf (stderr,
  70. "Error: requested protocol mismatch (wanted %d, got %d)\n",
  71. GNUTLS_TLS1_1,
  72. gret);
  73. return MHD_NO;
  74. }
  75. }
  76. response = MHD_create_response_from_buffer_static (strlen (EMPTY_PAGE),
  77. EMPTY_PAGE);
  78. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  79. MHD_destroy_response (response);
  80. return ret;
  81. }
  82. /**
  83. * negotiate a secure connection with server & query negotiated security parameters
  84. */
  85. #if LIBCURL_VERSION_NUM >= 0x072200
  86. static int
  87. test_query_session ()
  88. {
  89. CURL *c;
  90. struct CBC cbc;
  91. CURLcode errornum;
  92. char url[256];
  93. int port;
  94. const char *aes256_sha = "AES256-SHA";
  95. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  96. port = 0;
  97. else
  98. port = 3060;
  99. if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
  100. return 16;
  101. cbc.size = 255;
  102. cbc.pos = 0;
  103. /* setup test */
  104. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
  105. | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
  106. | MHD_USE_ERROR_LOG, port,
  107. NULL, NULL, &query_session_ahc, NULL,
  108. MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:+ARCFOUR-128",
  109. MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
  110. MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
  111. MHD_OPTION_END);
  112. if (d == NULL)
  113. {
  114. free (cbc.buf);
  115. return 2;
  116. }
  117. if (0 == port)
  118. {
  119. const union MHD_DaemonInfo *dinfo;
  120. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  121. if ((NULL == dinfo) || (0 == dinfo->port) )
  122. {
  123. MHD_stop_daemon (d);
  124. free (cbc.buf);
  125. return 32;
  126. }
  127. port = (int) dinfo->port;
  128. }
  129. if (curl_tls_is_nss ())
  130. {
  131. aes256_sha = "rsa_aes_256_sha";
  132. }
  133. gen_test_file_url (url,
  134. sizeof (url),
  135. port);
  136. c = curl_easy_init ();
  137. #if DEBUG_HTTPS_TEST
  138. curl_easy_setopt (c, CURLOPT_VERBOSE, 1L);
  139. #endif
  140. curl_easy_setopt (c, CURLOPT_URL, url);
  141. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  142. curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L);
  143. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L);
  144. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  145. curl_easy_setopt (c, CURLOPT_FILE, &cbc);
  146. /* TLS options */
  147. curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
  148. curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
  149. /* currently skip any peer authentication */
  150. curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L);
  151. curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L);
  152. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
  153. /* NOTE: use of CONNECTTIMEOUT without also
  154. * setting NOSIGNAL results in really weird
  155. * crashes on my system! */
  156. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
  157. if (CURLE_OK != (errornum = curl_easy_perform (c)))
  158. {
  159. fprintf (stderr, "curl_easy_perform failed: `%s'\n",
  160. curl_easy_strerror (errornum));
  161. MHD_stop_daemon (d);
  162. curl_easy_cleanup (c);
  163. free (cbc.buf);
  164. return -1;
  165. }
  166. curl_easy_cleanup (c);
  167. MHD_stop_daemon (d);
  168. free (cbc.buf);
  169. return 0;
  170. }
  171. #endif
  172. int
  173. main (int argc, char *const *argv)
  174. {
  175. #if LIBCURL_VERSION_NUM >= 0x072200
  176. unsigned int errorCount = 0;
  177. const char *ssl_version;
  178. (void) argc; /* Unused. Silent compiler warning. */
  179. #ifdef MHD_HTTPS_REQUIRE_GRYPT
  180. gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  181. #ifdef GCRYCTL_INITIALIZATION_FINISHED
  182. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  183. #endif
  184. #endif /* MHD_HTTPS_REQUIRE_GRYPT */
  185. if (! testsuite_curl_global_init ())
  186. return 99;
  187. ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version;
  188. if (NULL == ssl_version)
  189. {
  190. fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n");
  191. curl_global_cleanup ();
  192. return 77;
  193. }
  194. if (! curl_tls_is_gnutls ())
  195. {
  196. fprintf (stderr, "This test can be run only with libcurl-gnutls.\n");
  197. curl_global_cleanup ();
  198. return 77;
  199. }
  200. errorCount += test_query_session ();
  201. print_test_result (errorCount, argv[0]);
  202. curl_global_cleanup ();
  203. return errorCount != 0 ? 1 : 0;
  204. #else /* LIBCURL_VERSION_NUM < 0x072200 */
  205. (void) argc; (void) argv; /* Unused. Silent compiler warning. */
  206. (void) query_session_ahc; /* Mute compiler warning */
  207. return 77;
  208. #endif /* LIBCURL_VERSION_NUM < 0x072200 */
  209. }