2
0

test_https_session_info.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 const char srv_key_pem[];
  30. extern const char srv_self_signed_cert_pem[];
  31. struct MHD_Daemon *d;
  32. /*
  33. * HTTP access handler call back
  34. * used to query negotiated security parameters
  35. */
  36. static enum MHD_Result
  37. query_session_ahc (void *cls, struct MHD_Connection *connection,
  38. const char *url, const char *method,
  39. const char *version, const char *upload_data,
  40. size_t *upload_data_size, void **ptr)
  41. {
  42. struct MHD_Response *response;
  43. enum MHD_Result ret;
  44. int gret;
  45. (void) cls; (void) url; (void) method; (void) version; /* Unused. Silent compiler warning. */
  46. (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */
  47. if (NULL == *ptr)
  48. {
  49. *ptr = (void*) &query_session_ahc;
  50. return MHD_YES;
  51. }
  52. if (GNUTLS_TLS1_1 !=
  53. (gret = MHD_get_connection_info
  54. (connection,
  55. MHD_CONNECTION_INFO_PROTOCOL)->protocol))
  56. {
  57. if (GNUTLS_TLS1_2 == gret)
  58. {
  59. /* as usual, TLS implementations sometimes don't
  60. quite do what was asked, just mildly complain... */
  61. fprintf (stderr,
  62. "Warning: requested TLS 1.1, got TLS 1.2\n");
  63. }
  64. else
  65. {
  66. /* really different version... */
  67. fprintf (stderr,
  68. "Error: requested protocol mismatch (wanted %d, got %d)\n",
  69. GNUTLS_TLS1_1,
  70. gret);
  71. return MHD_NO;
  72. }
  73. }
  74. response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE),
  75. (void *) EMPTY_PAGE,
  76. MHD_RESPMEM_PERSISTENT);
  77. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  78. MHD_destroy_response (response);
  79. return ret;
  80. }
  81. /**
  82. * negotiate a secure connection with server & query negotiated security parameters
  83. */
  84. #if LIBCURL_VERSION_NUM >= 0x072200
  85. static int
  86. test_query_session ()
  87. {
  88. CURL *c;
  89. struct CBC cbc;
  90. CURLcode errornum;
  91. char url[256];
  92. int port;
  93. const char *aes256_sha = "AES256-SHA";
  94. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  95. port = 0;
  96. else
  97. port = 3060;
  98. if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
  99. return 16;
  100. cbc.size = 255;
  101. cbc.pos = 0;
  102. /* setup test */
  103. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
  104. | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
  105. | MHD_USE_ERROR_LOG, port,
  106. NULL, NULL, &query_session_ahc, NULL,
  107. MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:+ARCFOUR-128",
  108. MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
  109. MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
  110. MHD_OPTION_END);
  111. if (d == NULL)
  112. {
  113. free (cbc.buf);
  114. return 2;
  115. }
  116. if (0 == port)
  117. {
  118. const union MHD_DaemonInfo *dinfo;
  119. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  120. if ((NULL == dinfo) || (0 == dinfo->port) )
  121. {
  122. MHD_stop_daemon (d);
  123. free (cbc.buf);
  124. return 32;
  125. }
  126. port = (int) dinfo->port;
  127. }
  128. if (curl_uses_nss_ssl () == 0)
  129. {
  130. aes256_sha = "rsa_aes_256_sha";
  131. }
  132. gen_test_file_url (url,
  133. sizeof (url),
  134. port);
  135. c = curl_easy_init ();
  136. #if DEBUG_HTTPS_TEST
  137. curl_easy_setopt (c, CURLOPT_VERBOSE, 1L);
  138. #endif
  139. curl_easy_setopt (c, CURLOPT_URL, url);
  140. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  141. curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L);
  142. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L);
  143. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  144. curl_easy_setopt (c, CURLOPT_FILE, &cbc);
  145. /* TLS options */
  146. curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
  147. curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
  148. /* currently skip any peer authentication */
  149. curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L);
  150. curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L);
  151. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
  152. /* NOTE: use of CONNECTTIMEOUT without also
  153. * setting NOSIGNAL results in really weird
  154. * crashes on my system! */
  155. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
  156. if (CURLE_OK != (errornum = curl_easy_perform (c)))
  157. {
  158. fprintf (stderr, "curl_easy_perform failed: `%s'\n",
  159. curl_easy_strerror (errornum));
  160. MHD_stop_daemon (d);
  161. curl_easy_cleanup (c);
  162. free (cbc.buf);
  163. return -1;
  164. }
  165. curl_easy_cleanup (c);
  166. MHD_stop_daemon (d);
  167. free (cbc.buf);
  168. return 0;
  169. }
  170. #endif
  171. int
  172. main (int argc, char *const *argv)
  173. {
  174. #if LIBCURL_VERSION_NUM >= 0x072200
  175. unsigned int errorCount = 0;
  176. const char *ssl_version;
  177. (void) argc; /* Unused. Silent compiler warning. */
  178. #ifdef MHD_HTTPS_REQUIRE_GRYPT
  179. gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  180. #ifdef GCRYCTL_INITIALIZATION_FINISHED
  181. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  182. #endif
  183. #endif /* MHD_HTTPS_REQUIRE_GRYPT */
  184. if (! testsuite_curl_global_init ())
  185. return 99;
  186. ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version;
  187. if (NULL == ssl_version)
  188. {
  189. fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n");
  190. curl_global_cleanup ();
  191. return 77;
  192. }
  193. if (0 != strncmp (ssl_version, "GnuTLS", 6))
  194. {
  195. fprintf (stderr, "This test can be run only with libcurl-gnutls.\n");
  196. curl_global_cleanup ();
  197. return 77;
  198. }
  199. errorCount += test_query_session ();
  200. print_test_result (errorCount, argv[0]);
  201. curl_global_cleanup ();
  202. return errorCount != 0 ? 1 : 0;
  203. #else /* LIBCURL_VERSION_NUM < 0x072200 */
  204. return 77;
  205. #endif /* LIBCURL_VERSION_NUM < 0x072200 */
  206. }