test_digestauth_with_arguments.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2010, 2012 Christian Grothoff
  4. Copyright (C) 2016-2022 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 daemontest_digestauth_with_arguments.c
  20. * @brief Testcase for libmicrohttpd Digest Auth with arguments
  21. * @author Amr Ali
  22. * @author Karlson2k (Evgeny Grin)
  23. */
  24. #include "mhd_options.h"
  25. #include "platform.h"
  26. #include <curl/curl.h>
  27. #include <microhttpd.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <errno.h>
  32. #if defined(MHD_HTTPS_REQUIRE_GCRYPT) && \
  33. (defined(MHD_SHA256_TLSLIB) || defined(MHD_MD5_TLSLIB))
  34. #define NEED_GCRYP_INIT 1
  35. #include <gcrypt.h>
  36. #endif /* MHD_HTTPS_REQUIRE_GCRYPT && (MHD_SHA256_TLSLIB || MHD_MD5_TLSLIB) */
  37. #ifndef WINDOWS
  38. #include <sys/socket.h>
  39. #include <unistd.h>
  40. #else
  41. #include <wincrypt.h>
  42. #endif
  43. #define PAGE \
  44. "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
  45. #define DENIED \
  46. "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
  47. #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
  48. struct CBC
  49. {
  50. char *buf;
  51. size_t pos;
  52. size_t size;
  53. };
  54. static size_t
  55. copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
  56. {
  57. struct CBC *cbc = ctx;
  58. if (cbc->pos + size * nmemb > cbc->size)
  59. return 0; /* overflow */
  60. memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
  61. cbc->pos += size * nmemb;
  62. return size * nmemb;
  63. }
  64. static enum MHD_Result
  65. ahc_echo (void *cls,
  66. struct MHD_Connection *connection,
  67. const char *url,
  68. const char *method,
  69. const char *version,
  70. const char *upload_data, size_t *upload_data_size,
  71. void **req_cls)
  72. {
  73. struct MHD_Response *response;
  74. char *username;
  75. const char *password = "testpass";
  76. const char *realm = "[email protected]";
  77. enum MHD_Result ret;
  78. int ret_i;
  79. static int already_called_marker;
  80. (void) cls; (void) url; /* Unused. Silent compiler warning. */
  81. (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */
  82. (void) upload_data_size; (void) req_cls; /* Unused. Silent compiler warning. */
  83. if (&already_called_marker != *req_cls)
  84. { /* Called for the first time, request not fully read yet */
  85. *req_cls = &already_called_marker;
  86. /* Wait for complete request */
  87. return MHD_YES;
  88. }
  89. username = MHD_digest_auth_get_username (connection);
  90. if ( (username == NULL) ||
  91. (0 != strcmp (username, "testuser")) )
  92. {
  93. response = MHD_create_response_from_buffer_static (strlen (DENIED),
  94. DENIED);
  95. ret = MHD_queue_auth_fail_response2 (connection, realm,
  96. MY_OPAQUE,
  97. response,
  98. MHD_NO,
  99. MHD_DIGEST_ALG_MD5);
  100. MHD_destroy_response (response);
  101. return ret;
  102. }
  103. ret_i = MHD_digest_auth_check2 (connection,
  104. realm,
  105. username,
  106. password,
  107. 300,
  108. MHD_DIGEST_ALG_MD5);
  109. MHD_free (username);
  110. if (ret_i != MHD_YES)
  111. {
  112. response = MHD_create_response_from_buffer_static (strlen (DENIED),
  113. DENIED);
  114. if (NULL == response)
  115. fprintf (stderr, "MHD_create_response_from_buffer() failed.\n");
  116. ret = MHD_queue_auth_fail_response2 (connection,
  117. realm,
  118. MY_OPAQUE,
  119. response,
  120. (MHD_INVALID_NONCE == ret_i) ?
  121. MHD_YES : MHD_NO,
  122. MHD_DIGEST_ALG_MD5);
  123. if (MHD_YES != ret)
  124. fprintf (stderr, "MHD_queue_auth_fail_response2() failed.\n");
  125. MHD_destroy_response (response);
  126. return ret;
  127. }
  128. response = MHD_create_response_from_buffer_static (strlen (PAGE),
  129. PAGE);
  130. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  131. MHD_destroy_response (response);
  132. return ret;
  133. }
  134. static unsigned int
  135. testDigestAuth (void)
  136. {
  137. CURL *c;
  138. CURLcode errornum;
  139. struct MHD_Daemon *d;
  140. struct CBC cbc;
  141. char buf[2048];
  142. char rnd[8];
  143. uint16_t port;
  144. char url[128];
  145. #ifndef WINDOWS
  146. int fd;
  147. size_t len;
  148. size_t off = 0;
  149. #endif /* ! WINDOWS */
  150. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  151. port = 0;
  152. else
  153. port = 1160;
  154. cbc.buf = buf;
  155. cbc.size = 2048;
  156. cbc.pos = 0;
  157. #ifndef WINDOWS
  158. fd = open ("/dev/urandom", O_RDONLY);
  159. if (-1 == fd)
  160. {
  161. fprintf (stderr, "Failed to open `%s': %s\n",
  162. "/dev/urandom",
  163. strerror (errno));
  164. return 1;
  165. }
  166. while (off < 8)
  167. {
  168. len = (size_t) read (fd, rnd + off, 8 - off);
  169. if (len == (size_t) -1)
  170. {
  171. fprintf (stderr,
  172. "Failed to read `%s': %s\n",
  173. "/dev/urandom",
  174. strerror (errno));
  175. (void) close (fd);
  176. return 1;
  177. }
  178. off += len;
  179. }
  180. (void) close (fd);
  181. #else
  182. {
  183. HCRYPTPROV cc;
  184. BOOL b;
  185. b = CryptAcquireContext (&cc, NULL, NULL, PROV_RSA_FULL,
  186. CRYPT_VERIFYCONTEXT);
  187. if (b == 0)
  188. {
  189. fprintf (stderr, "Failed to acquire crypto provider context: %lu\n",
  190. GetLastError ());
  191. return 1;
  192. }
  193. b = CryptGenRandom (cc, 8, (BYTE *) rnd);
  194. if (b == 0)
  195. {
  196. fprintf (stderr, "Failed to generate 8 random bytes: %lu\n",
  197. GetLastError ());
  198. }
  199. CryptReleaseContext (cc, 0);
  200. if (b == 0)
  201. return 1;
  202. }
  203. #endif
  204. d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
  205. port, NULL, NULL, &ahc_echo, NULL,
  206. MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd,
  207. MHD_OPTION_NONCE_NC_SIZE, 300,
  208. MHD_OPTION_DIGEST_AUTH_DEFAULT_MAX_NC, (uint32_t) 999,
  209. MHD_OPTION_END);
  210. if (d == NULL)
  211. return 1;
  212. if (0 == port)
  213. {
  214. const union MHD_DaemonInfo *dinfo;
  215. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  216. if ((NULL == dinfo) || (0 == dinfo->port) )
  217. {
  218. MHD_stop_daemon (d); return 32;
  219. }
  220. port = dinfo->port;
  221. }
  222. snprintf (url,
  223. sizeof (url),
  224. "http://127.0.0.1:%u/bar%%20foo?"
  225. "key=value&more=even%%20more&empty&=no_key&&same=one&&same=two",
  226. (unsigned int) port);
  227. c = curl_easy_init ();
  228. curl_easy_setopt (c, CURLOPT_URL, url);
  229. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  230. curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
  231. curl_easy_setopt (c, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  232. curl_easy_setopt (c, CURLOPT_USERPWD, "testuser:testpass");
  233. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
  234. curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
  235. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
  236. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  237. /* NOTE: use of CONNECTTIMEOUT without also
  238. setting NOSIGNAL results in really weird
  239. crashes on my system!*/
  240. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
  241. if (CURLE_OK != (errornum = curl_easy_perform (c)))
  242. {
  243. fprintf (stderr,
  244. "curl_easy_perform failed: `%s'\n",
  245. curl_easy_strerror (errornum));
  246. curl_easy_cleanup (c);
  247. MHD_stop_daemon (d);
  248. return 2;
  249. }
  250. curl_easy_cleanup (c);
  251. MHD_stop_daemon (d);
  252. if (cbc.pos != strlen (PAGE))
  253. return 4;
  254. if (0 != strncmp (PAGE, cbc.buf, strlen (PAGE)))
  255. return 8;
  256. return 0;
  257. }
  258. int
  259. main (int argc, char *const *argv)
  260. {
  261. unsigned int errorCount = 0;
  262. (void) argc; (void) argv; /* Unused. Silent compiler warning. */
  263. #if (LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 62)
  264. if (1)
  265. {
  266. fprintf (stderr, "libcurl version 7.62.x has bug in processing"
  267. "URI with GET arguments for Digest Auth.\n");
  268. fprintf (stderr, "This test cannot be performed.\n");
  269. exit (77);
  270. }
  271. #endif /* libcurl version 7.62.x */
  272. #ifdef MHD_HTTPS_REQUIRE_GCRYPT
  273. #ifdef HAVE_GCRYPT_H
  274. gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  275. #ifdef GCRYCTL_INITIALIZATION_FINISHED
  276. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  277. #endif
  278. #endif
  279. #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
  280. if (0 != curl_global_init (CURL_GLOBAL_WIN32))
  281. return 2;
  282. errorCount += testDigestAuth ();
  283. if (errorCount != 0)
  284. fprintf (stderr, "Error (code: %u)\n", errorCount);
  285. curl_global_cleanup ();
  286. return (0 == errorCount) ? 0 : 1; /* 0 == pass */
  287. }