http_digest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_HTTP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include "urldata.h"
  32. #include "sendf.h"
  33. #include "strequal.h"
  34. #include "md5.h"
  35. #include "http_digest.h"
  36. #define _MPRINTF_REPLACE /* use our functions only */
  37. #include <curl/mprintf.h>
  38. /* The last #include file should be: */
  39. #ifdef CURLDEBUG
  40. #include "memdebug.h"
  41. #endif
  42. /* Test example header:
  43. WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
  44. */
  45. CURLdigest Curl_input_digest(struct connectdata *conn,
  46. char *header) /* rest of the www-authenticate:
  47. header */
  48. {
  49. bool more = TRUE;
  50. struct SessionHandle *data=conn->data;
  51. /* skip initial whitespaces */
  52. while(*header && isspace((int)*header))
  53. header++;
  54. if(checkprefix("Digest", header)) {
  55. header += strlen("Digest");
  56. /* clear off any former leftovers and init to defaults */
  57. Curl_digest_cleanup(data);
  58. while(more) {
  59. char value[32];
  60. char content[128];
  61. size_t totlen=0;
  62. while(*header && isspace((int)*header))
  63. header++;
  64. /* how big can these strings be? */
  65. if(2 == sscanf(header, "%31[^=]=\"%127[^\"]\"",
  66. value, content)) {
  67. if(strequal(value, "nonce")) {
  68. data->state.digest.nonce = strdup(content);
  69. }
  70. else if(strequal(value, "cnonce")) {
  71. data->state.digest.cnonce = strdup(content);
  72. }
  73. else if(strequal(value, "realm")) {
  74. data->state.digest.realm = strdup(content);
  75. }
  76. else if(strequal(value, "algorithm")) {
  77. if(strequal(content, "MD5-sess"))
  78. data->state.digest.algo = CURLDIGESTALGO_MD5SESS;
  79. /* else, remain using the default md5 */
  80. }
  81. else {
  82. /* unknown specifier, ignore it! */
  83. }
  84. totlen = strlen(value)+strlen(content)+3;
  85. }
  86. else
  87. break; /* we're done here */
  88. header += totlen;
  89. if(',' == *header)
  90. /* allow the list to be comma-separated */
  91. header++;
  92. }
  93. if(!data->state.digest.nonce)
  94. return CURLDIGEST_BAD;
  95. }
  96. else
  97. /* else not a digest, get out */
  98. return CURLDIGEST_NONE;
  99. return CURLDIGEST_FINE;
  100. }
  101. /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
  102. static void md5_to_ascii(unsigned char *source, /* 16 bytes */
  103. unsigned char *dest) /* 33 bytes */
  104. {
  105. int i;
  106. for(i=0; i<16; i++)
  107. sprintf((char *)&dest[i*2], "%02x", source[i]);
  108. }
  109. CURLcode Curl_output_digest(struct connectdata *conn,
  110. unsigned char *request,
  111. unsigned char *uripath)
  112. {
  113. /* We have a Digest setup for this, use it! Now, to get all the details for
  114. this sorted out, I must urge you dear friend to read up on the RFC2617
  115. section 3.2.2, */
  116. unsigned char md5buf[16]; /* 16 bytes/128 bits */
  117. unsigned char ha1[33]; /* 32 digits and 1 zero byte */
  118. unsigned char ha2[33];
  119. unsigned char request_digest[33];
  120. unsigned char *md5this;
  121. struct SessionHandle *data = conn->data;
  122. /*
  123. if the algorithm is "MD5" or unspecified (which then defaults to MD5):
  124. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  125. if the algorithm is "MD5-sess" then:
  126. A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
  127. ":" unq(nonce-value) ":" unq(cnonce-value)
  128. */
  129. if(data->state.digest.algo == CURLDIGESTALGO_MD5SESS) {
  130. md5this = (unsigned char *)
  131. aprintf("%s:%s:%s:%s:%s",
  132. conn->user,
  133. data->state.digest.realm,
  134. conn->passwd,
  135. data->state.digest.nonce,
  136. data->state.digest.cnonce);
  137. }
  138. else {
  139. md5this = (unsigned char *)
  140. aprintf("%s:%s:%s",
  141. conn->user,
  142. data->state.digest.realm,
  143. conn->passwd);
  144. }
  145. Curl_md5it(md5buf, md5this);
  146. free(md5this); /* free this again */
  147. md5_to_ascii(md5buf, ha1);
  148. /*
  149. A2 = Method ":" digest-uri-value
  150. (The "Method" value is the HTTP request method as specified in section
  151. 5.1.1 of RFC 2616)
  152. */
  153. md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
  154. Curl_md5it(md5buf, md5this);
  155. free(md5this); /* free this again */
  156. md5_to_ascii(md5buf, ha2);
  157. md5this = (unsigned char *)aprintf("%s:%s:%s", ha1, data->state.digest.nonce,
  158. ha2);
  159. Curl_md5it(md5buf, md5this);
  160. free(md5this); /* free this again */
  161. md5_to_ascii(md5buf, request_digest);
  162. /* for test case 64 (snooped from a Mozilla 1.3a request)
  163. Authorization: Digest username="testuser", realm="testrealm", \
  164. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  165. */
  166. conn->allocptr.userpwd =
  167. aprintf( "Authorization: Digest "
  168. "username=\"%s\", "
  169. "realm=\"%s\", "
  170. "nonce=\"%s\", "
  171. "uri=\"%s\", "
  172. "response=\"%s\"\r\n",
  173. conn->user,
  174. data->state.digest.realm,
  175. data->state.digest.nonce,
  176. uripath, /* this is the PATH part of the URL */
  177. request_digest );
  178. return CURLE_OK;
  179. }
  180. void Curl_digest_cleanup(struct SessionHandle *data)
  181. {
  182. if(data->state.digest.nonce)
  183. free(data->state.digest.nonce);
  184. data->state.digest.nonce = NULL;
  185. if(data->state.digest.cnonce)
  186. free(data->state.digest.cnonce);
  187. data->state.digest.cnonce = NULL;
  188. if(data->state.digest.realm)
  189. free(data->state.digest.realm);
  190. data->state.digest.realm = NULL;
  191. data->state.digest.algo = CURLDIGESTALGO_MD5; /* default algorithm */
  192. }
  193. #endif