test_https_session_info.c 6.3 KB

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