http_digest.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. digest = &data->state.proxydigest;
  80. allocuserpwd = &conn->allocptr.proxyuserpwd;
  81. userp = conn->http_proxy.user;
  82. passwdp = conn->http_proxy.passwd;
  83. authp = &data->state.authproxy;
  84. }
  85. else {
  86. digest = &data->state.digest;
  87. allocuserpwd = &conn->allocptr.userpwd;
  88. userp = conn->user;
  89. passwdp = conn->passwd;
  90. authp = &data->state.authhost;
  91. }
  92. Curl_safefree(*allocuserpwd);
  93. /* not set means empty */
  94. if(!userp)
  95. userp = "";
  96. if(!passwdp)
  97. passwdp = "";
  98. #if defined(USE_WINDOWS_SSPI)
  99. have_chlg = digest->input_token ? TRUE : FALSE;
  100. #else
  101. have_chlg = digest->nonce ? TRUE : FALSE;
  102. #endif
  103. if(!have_chlg) {
  104. authp->done = FALSE;
  105. return CURLE_OK;
  106. }
  107. /* So IE browsers < v7 cut off the URI part at the query part when they
  108. evaluate the MD5 and some (IIS?) servers work with them so we may need to
  109. do the Digest IE-style. Note that the different ways cause different MD5
  110. sums to get sent.
  111. Apache servers can be set to do the Digest IE-style automatically using
  112. the BrowserMatch feature:
  113. https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
  114. Further details on Digest implementation differences:
  115. http://www.fngtps.com/2006/09/http-authentication
  116. */
  117. if(authp->iestyle) {
  118. tmp = strchr((char *)uripath, '?');
  119. if(tmp) {
  120. size_t urilen = tmp - (char *)uripath;
  121. path = (unsigned char *) aprintf("%.*s", urilen, uripath);
  122. }
  123. }
  124. if(!tmp)
  125. path = (unsigned char *) strdup((char *) uripath);
  126. if(!path)
  127. return CURLE_OUT_OF_MEMORY;
  128. result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
  129. path, digest, &response, &len);
  130. free(path);
  131. if(result)
  132. return result;
  133. *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
  134. proxy ? "Proxy-" : "",
  135. response);
  136. free(response);
  137. if(!*allocuserpwd)
  138. return CURLE_OUT_OF_MEMORY;
  139. authp->done = TRUE;
  140. return CURLE_OK;
  141. }
  142. void Curl_digest_cleanup(struct Curl_easy *data)
  143. {
  144. Curl_auth_digest_cleanup(&data->state.digest);
  145. Curl_auth_digest_cleanup(&data->state.proxydigest);
  146. }
  147. #endif