test_https_session_info.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (strlen (EMPTY_PAGE),
  77. (void *) EMPTY_PAGE,
  78. MHD_RESPMEM_PERSISTENT);
  79. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  80. MHD_destroy_response (response);
  81. return ret;
  82. }
  83. /**
  84. * negotiate a secure connection with server & query negotiated security parameters
  85. */
  86. #if LIBCURL_VERSION_NUM >= 0x072200
  87. static int
  88. test_query_session ()
  89. {
  90. CURL *c;
  91. struct CBC cbc;
  92. CURLcode errornum;
  93. char url[256];
  94. int port;
  95. const char *aes256_sha = "AES256-SHA";
  96. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  97. port = 0;
  98. else
  99. port = 3060;
  100. if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
  101. return 16;
  102. cbc.size = 255;
  103. cbc.pos = 0;
  104. /* setup test */
  105. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
  106. | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
  107. | MHD_USE_ERROR_LOG, port,
  108. NULL, NULL, &query_session_ahc, NULL,
  109. MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:+ARCFOUR-128",
  110. MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
  111. MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
  112. MHD_OPTION_END);
  113. if (d == NULL)
  114. {
  115. free (cbc.buf);
  116. return 2;
  117. }
  118. if (0 == port)
  119. {
  120. const union MHD_DaemonInfo *dinfo;
  121. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  122. if ((NULL == dinfo) || (0 == dinfo->port) )
  123. {
  124. MHD_stop_daemon (d);
  125. free (cbc.buf);
  126. return 32;
  127. }
  128. port = (int) dinfo->port;
  129. }
  130. if (curl_tls_is_nss ())
  131. {
  132. aes256_sha = "rsa_aes_256_sha";
  133. }
  134. gen_test_file_url (url,
  135. sizeof (url),
  136. port);
  137. c = curl_easy_init ();
  138. #if DEBUG_HTTPS_TEST
  139. curl_easy_setopt (c, CURLOPT_VERBOSE, 1L);
  140. #endif
  141. curl_easy_setopt (c, CURLOPT_URL, url);
  142. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  143. curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L);
  144. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L);
  145. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  146. curl_easy_setopt (c, CURLOPT_FILE, &cbc);
  147. /* TLS options */
  148. curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
  149. curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
  150. /* currently skip any peer authentication */
  151. curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L);
  152. curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L);
  153. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
  154. /* NOTE: use of CONNECTTIMEOUT without also
  155. * setting NOSIGNAL results in really weird
  156. * crashes on my system! */
  157. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
  158. if (CURLE_OK != (errornum = curl_easy_perform (c)))
  159. {
  160. fprintf (stderr, "curl_easy_perform failed: `%s'\n",
  161. curl_easy_strerror (errornum));
  162. MHD_stop_daemon (d);
  163. curl_easy_cleanup (c);
  164. free (cbc.buf);
  165. return -1;
  166. }
  167. curl_easy_cleanup (c);
  168. MHD_stop_daemon (d);
  169. free (cbc.buf);
  170. return 0;
  171. }
  172. #endif
  173. int
  174. main (int argc, char *const *argv)
  175. {
  176. #if LIBCURL_VERSION_NUM >= 0x072200
  177. unsigned int errorCount = 0;
  178. const char *ssl_version;
  179. (void) argc; /* Unused. Silent compiler warning. */
  180. #ifdef MHD_HTTPS_REQUIRE_GRYPT
  181. gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  182. #ifdef GCRYCTL_INITIALIZATION_FINISHED
  183. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  184. #endif
  185. #endif /* MHD_HTTPS_REQUIRE_GRYPT */
  186. if (! testsuite_curl_global_init ())
  187. return 99;
  188. ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version;
  189. if (NULL == ssl_version)
  190. {
  191. fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n");
  192. curl_global_cleanup ();
  193. return 77;
  194. }
  195. if (! curl_tls_is_gnutls ())
  196. {
  197. fprintf (stderr, "This test can be run only with libcurl-gnutls.\n");
  198. curl_global_cleanup ();
  199. return 77;
  200. }
  201. errorCount += test_query_session ();
  202. print_test_result (errorCount, argv[0]);
  203. curl_global_cleanup ();
  204. return errorCount != 0 ? 1 : 0;
  205. #else /* LIBCURL_VERSION_NUM < 0x072200 */
  206. (void) argc; (void) argv; /* Unused. Silent compiler warning. */
  207. (void) query_session_ahc; /* Mute compiler warning */
  208. return 77;
  209. #endif /* LIBCURL_VERSION_NUM < 0x072200 */
  210. }