http_digest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
  24. #include "urldata.h"
  25. #include "strcase.h"
  26. #include "vauth/vauth.h"
  27. #include "http_digest.h"
  28. /* The last 3 #include files should be in this order */
  29. #include "curl_printf.h"
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /* Test example headers:
  33. WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
  34. Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
  35. */
  36. CURLcode Curl_input_digest(struct connectdata *conn,
  37. bool proxy,
  38. const char *header) /* rest of the *-authenticate:
  39. header */
  40. {
  41. struct Curl_easy *data = conn->data;
  42. /* Point to the correct struct with this */
  43. struct digestdata *digest;
  44. if(proxy) {
  45. digest = &data->state.proxydigest;
  46. }
  47. else {
  48. digest = &data->state.digest;
  49. }
  50. if(!checkprefix("Digest", header))
  51. return CURLE_BAD_CONTENT_ENCODING;
  52. header += strlen("Digest");
  53. while(*header && ISSPACE(*header))
  54. header++;
  55. return Curl_auth_decode_digest_http_message(header, digest);
  56. }
  57. CURLcode Curl_output_digest(struct connectdata *conn,
  58. bool proxy,
  59. const unsigned char *request,
  60. const unsigned char *uripath)
  61. {
  62. CURLcode result;
  63. struct Curl_easy *data = conn->data;
  64. unsigned char *path = NULL;
  65. char *tmp = NULL;
  66. char *response;
  67. size_t len;
  68. bool have_chlg;
  69. /* Point to the address of the pointer that holds the string to send to the
  70. server, which is for a plain host or for a HTTP proxy */
  71. char **allocuserpwd;
  72. /* Point to the name and password for this */
  73. const char *userp;
  74. const char *passwdp;
  75. /* Point to the correct struct with this */
  76. struct digestdata *digest;
  77. struct auth *authp;
  78. if(proxy) {
  79. #ifdef CURL_DISABLE_PROXY
  80. return CURLE_NOT_BUILT_IN;
  81. #else
  82. digest = &data->state.proxydigest;
  83. allocuserpwd = &data->state.aptr.proxyuserpwd;
  84. userp = conn->http_proxy.user;
  85. passwdp = conn->http_proxy.passwd;
  86. authp = &data->state.authproxy;
  87. #endif
  88. }
  89. else {
  90. digest = &data->state.digest;
  91. allocuserpwd = &data->state.aptr.userpwd;
  92. userp = conn->user;
  93. passwdp = conn->passwd;
  94. authp = &data->state.authhost;
  95. }
  96. Curl_safefree(*allocuserpwd);
  97. /* not set means empty */
  98. if(!userp)
  99. userp = "";
  100. if(!passwdp)
  101. passwdp = "";
  102. #if defined(USE_WINDOWS_SSPI)
  103. have_chlg = digest->input_token ? TRUE : FALSE;
  104. #else
  105. have_chlg = digest->nonce ? TRUE : FALSE;
  106. #endif
  107. if(!have_chlg) {
  108. authp->done = FALSE;
  109. return CURLE_OK;
  110. }
  111. /* So IE browsers < v7 cut off the URI part at the query part when they
  112. evaluate the MD5 and some (IIS?) servers work with them so we may need to
  113. do the Digest IE-style. Note that the different ways cause different MD5
  114. sums to get sent.
  115. Apache servers can be set to do the Digest IE-style automatically using
  116. the BrowserMatch feature:
  117. https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
  118. Further details on Digest implementation differences:
  119. http://www.fngtps.com/2006/09/http-authentication
  120. */
  121. if(authp->iestyle) {
  122. tmp = strchr((char *)uripath, '?');
  123. if(tmp) {
  124. size_t urilen = tmp - (char *)uripath;
  125. path = (unsigned char *) aprintf("%.*s", urilen, uripath);
  126. }
  127. }
  128. if(!tmp)
  129. path = (unsigned char *) strdup((char *) uripath);
  130. if(!path)
  131. return CURLE_OUT_OF_MEMORY;
  132. result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
  133. path, digest, &response, &len);
  134. free(path);
  135. if(result)
  136. return result;
  137. *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
  138. proxy ? "Proxy-" : "",
  139. response);
  140. free(response);
  141. if(!*allocuserpwd)
  142. return CURLE_OUT_OF_MEMORY;
  143. authp->done = TRUE;
  144. return CURLE_OK;
  145. }
  146. void Curl_http_auth_cleanup_digest(struct Curl_easy *data)
  147. {
  148. Curl_auth_digest_cleanup(&data->state.digest);
  149. Curl_auth_digest_cleanup(&data->state.proxydigest);
  150. }
  151. #endif