http_digest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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 Curl_easy *data,
  37. bool proxy,
  38. const char *header) /* rest of the *-authenticate:
  39. header */
  40. {
  41. /* Point to the correct struct with this */
  42. struct digestdata *digest;
  43. if(proxy) {
  44. digest = &data->state.proxydigest;
  45. }
  46. else {
  47. digest = &data->state.digest;
  48. }
  49. if(!checkprefix("Digest", header) || !ISSPACE(header[6]))
  50. return CURLE_BAD_CONTENT_ENCODING;
  51. header += strlen("Digest");
  52. while(*header && ISSPACE(*header))
  53. header++;
  54. return Curl_auth_decode_digest_http_message(header, digest);
  55. }
  56. CURLcode Curl_output_digest(struct Curl_easy *data,
  57. bool proxy,
  58. const unsigned char *request,
  59. const unsigned char *uripath)
  60. {
  61. CURLcode result;
  62. unsigned char *path = NULL;
  63. char *tmp = NULL;
  64. char *response;
  65. size_t len;
  66. bool have_chlg;
  67. /* Point to the address of the pointer that holds the string to send to the
  68. server, which is for a plain host or for a HTTP proxy */
  69. char **allocuserpwd;
  70. /* Point to the name and password for this */
  71. const char *userp;
  72. const char *passwdp;
  73. /* Point to the correct struct with this */
  74. struct digestdata *digest;
  75. struct auth *authp;
  76. if(proxy) {
  77. #ifdef CURL_DISABLE_PROXY
  78. return CURLE_NOT_BUILT_IN;
  79. #else
  80. digest = &data->state.proxydigest;
  81. allocuserpwd = &data->state.aptr.proxyuserpwd;
  82. userp = data->state.aptr.proxyuser;
  83. passwdp = data->state.aptr.proxypasswd;
  84. authp = &data->state.authproxy;
  85. #endif
  86. }
  87. else {
  88. digest = &data->state.digest;
  89. allocuserpwd = &data->state.aptr.userpwd;
  90. userp = data->state.aptr.user;
  91. passwdp = data->state.aptr.passwd;
  92. authp = &data->state.authhost;
  93. }
  94. Curl_safefree(*allocuserpwd);
  95. /* not set means empty */
  96. if(!userp)
  97. userp = "";
  98. if(!passwdp)
  99. passwdp = "";
  100. #if defined(USE_WINDOWS_SSPI)
  101. have_chlg = digest->input_token ? TRUE : FALSE;
  102. #else
  103. have_chlg = digest->nonce ? TRUE : FALSE;
  104. #endif
  105. if(!have_chlg) {
  106. authp->done = FALSE;
  107. return CURLE_OK;
  108. }
  109. /* So IE browsers < v7 cut off the URI part at the query part when they
  110. evaluate the MD5 and some (IIS?) servers work with them so we may need to
  111. do the Digest IE-style. Note that the different ways cause different MD5
  112. sums to get sent.
  113. Apache servers can be set to do the Digest IE-style automatically using
  114. the BrowserMatch feature:
  115. https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
  116. Further details on Digest implementation differences:
  117. http://www.fngtps.com/2006/09/http-authentication
  118. */
  119. if(authp->iestyle) {
  120. tmp = strchr((char *)uripath, '?');
  121. if(tmp) {
  122. size_t urilen = tmp - (char *)uripath;
  123. /* typecast is fine here since the value is always less than 32 bits */
  124. path = (unsigned char *) aprintf("%.*s", (int)urilen, uripath);
  125. }
  126. }
  127. if(!tmp)
  128. path = (unsigned char *) strdup((char *) uripath);
  129. if(!path)
  130. return CURLE_OUT_OF_MEMORY;
  131. result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
  132. path, digest, &response, &len);
  133. free(path);
  134. if(result)
  135. return result;
  136. *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
  137. proxy ? "Proxy-" : "",
  138. response);
  139. free(response);
  140. if(!*allocuserpwd)
  141. return CURLE_OUT_OF_MEMORY;
  142. authp->done = TRUE;
  143. return CURLE_OK;
  144. }
  145. void Curl_http_auth_cleanup_digest(struct Curl_easy *data)
  146. {
  147. Curl_auth_digest_cleanup(&data->state.digest);
  148. Curl_auth_digest_cleanup(&data->state.proxydigest);
  149. }
  150. #endif