http_digest.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
  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 "rawstr.h"
  34. #include "curl_base64.h"
  35. #include "curl_md5.h"
  36. #include "http_digest.h"
  37. #include "strtok.h"
  38. #include "url.h" /* for Curl_safefree() */
  39. #include "curl_memory.h"
  40. #include "easyif.h" /* included for Curl_convert_... prototypes */
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. /* The last #include file should be: */
  44. #include "memdebug.h"
  45. #define MAX_VALUE_LENGTH 256
  46. #define MAX_CONTENT_LENGTH 1024
  47. /*
  48. * Return 0 on success and then the buffers are filled in fine.
  49. *
  50. * Non-zero means failure to parse.
  51. */
  52. static int get_pair(const char *str, char *value, char *content,
  53. const char **endptr)
  54. {
  55. int c;
  56. bool starts_with_quote = FALSE;
  57. bool escape = FALSE;
  58. for(c=MAX_VALUE_LENGTH-1; (*str && (*str != '=') && c--); )
  59. *value++ = *str++;
  60. *value=0;
  61. if('=' != *str++)
  62. /* eek, no match */
  63. return 1;
  64. if('\"' == *str) {
  65. /* this starts with a quote so it must end with one as well! */
  66. str++;
  67. starts_with_quote = TRUE;
  68. }
  69. for(c=MAX_CONTENT_LENGTH-1; *str && c--; str++) {
  70. switch(*str) {
  71. case '\\':
  72. if(!escape) {
  73. /* possibly the start of an escaped quote */
  74. escape = TRUE;
  75. *content++ = '\\'; /* even though this is an escape character, we still
  76. store it as-is in the target buffer */
  77. continue;
  78. }
  79. break;
  80. case ',':
  81. if(!starts_with_quote) {
  82. /* this signals the end of the content if we didn't get a starting quote
  83. and then we do "sloppy" parsing */
  84. c=0; /* the end */
  85. continue;
  86. }
  87. break;
  88. case '\r':
  89. case '\n':
  90. /* end of string */
  91. c=0;
  92. continue;
  93. case '\"':
  94. if(!escape && starts_with_quote) {
  95. /* end of string */
  96. c=0;
  97. continue;
  98. }
  99. break;
  100. }
  101. escape = FALSE;
  102. *content++ = *str;
  103. }
  104. *content=0;
  105. *endptr = str;
  106. return 0; /* all is fine! */
  107. }
  108. /* Test example headers:
  109. WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
  110. Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
  111. */
  112. CURLdigest Curl_input_digest(struct connectdata *conn,
  113. bool proxy,
  114. const char *header) /* rest of the *-authenticate:
  115. header */
  116. {
  117. bool more = TRUE;
  118. char *token = NULL;
  119. char *tmp = NULL;
  120. bool foundAuth = FALSE;
  121. bool foundAuthInt = FALSE;
  122. struct SessionHandle *data=conn->data;
  123. bool before = FALSE; /* got a nonce before */
  124. struct digestdata *d;
  125. if(proxy) {
  126. d = &data->state.proxydigest;
  127. }
  128. else {
  129. d = &data->state.digest;
  130. }
  131. /* skip initial whitespaces */
  132. while(*header && ISSPACE(*header))
  133. header++;
  134. if(checkprefix("Digest", header)) {
  135. header += strlen("Digest");
  136. /* If we already have received a nonce, keep that in mind */
  137. if(d->nonce)
  138. before = TRUE;
  139. /* clear off any former leftovers and init to defaults */
  140. Curl_digest_cleanup_one(d);
  141. while(more) {
  142. char value[MAX_VALUE_LENGTH];
  143. char content[MAX_CONTENT_LENGTH];
  144. while(*header && ISSPACE(*header))
  145. header++;
  146. /* extract a value=content pair */
  147. if(!get_pair(header, value, content, &header)) {
  148. if(Curl_raw_equal(value, "nonce")) {
  149. d->nonce = strdup(content);
  150. if(!d->nonce)
  151. return CURLDIGEST_NOMEM;
  152. }
  153. else if(Curl_raw_equal(value, "stale")) {
  154. if(Curl_raw_equal(content, "true")) {
  155. d->stale = TRUE;
  156. d->nc = 1; /* we make a new nonce now */
  157. }
  158. }
  159. else if(Curl_raw_equal(value, "realm")) {
  160. d->realm = strdup(content);
  161. if(!d->realm)
  162. return CURLDIGEST_NOMEM;
  163. }
  164. else if(Curl_raw_equal(value, "opaque")) {
  165. d->opaque = strdup(content);
  166. if(!d->opaque)
  167. return CURLDIGEST_NOMEM;
  168. }
  169. else if(Curl_raw_equal(value, "qop")) {
  170. char *tok_buf;
  171. /* tokenize the list and choose auth if possible, use a temporary
  172. clone of the buffer since strtok_r() ruins it */
  173. tmp = strdup(content);
  174. if(!tmp)
  175. return CURLDIGEST_NOMEM;
  176. token = strtok_r(tmp, ",", &tok_buf);
  177. while(token != NULL) {
  178. if(Curl_raw_equal(token, "auth")) {
  179. foundAuth = TRUE;
  180. }
  181. else if(Curl_raw_equal(token, "auth-int")) {
  182. foundAuthInt = TRUE;
  183. }
  184. token = strtok_r(NULL, ",", &tok_buf);
  185. }
  186. free(tmp);
  187. /*select only auth o auth-int. Otherwise, ignore*/
  188. if(foundAuth) {
  189. d->qop = strdup("auth");
  190. if(!d->qop)
  191. return CURLDIGEST_NOMEM;
  192. }
  193. else if(foundAuthInt) {
  194. d->qop = strdup("auth-int");
  195. if(!d->qop)
  196. return CURLDIGEST_NOMEM;
  197. }
  198. }
  199. else if(Curl_raw_equal(value, "algorithm")) {
  200. d->algorithm = strdup(content);
  201. if(!d->algorithm)
  202. return CURLDIGEST_NOMEM;
  203. if(Curl_raw_equal(content, "MD5-sess"))
  204. d->algo = CURLDIGESTALGO_MD5SESS;
  205. else if(Curl_raw_equal(content, "MD5"))
  206. d->algo = CURLDIGESTALGO_MD5;
  207. else
  208. return CURLDIGEST_BADALGO;
  209. }
  210. else {
  211. /* unknown specifier, ignore it! */
  212. }
  213. }
  214. else
  215. break; /* we're done here */
  216. /* pass all additional spaces here */
  217. while(*header && ISSPACE(*header))
  218. header++;
  219. if(',' == *header)
  220. /* allow the list to be comma-separated */
  221. header++;
  222. }
  223. /* We had a nonce since before, and we got another one now without
  224. 'stale=true'. This means we provided bad credentials in the previous
  225. request */
  226. if(before && !d->stale)
  227. return CURLDIGEST_BAD;
  228. /* We got this header without a nonce, that's a bad Digest line! */
  229. if(!d->nonce)
  230. return CURLDIGEST_BAD;
  231. }
  232. else
  233. /* else not a digest, get out */
  234. return CURLDIGEST_NONE;
  235. return CURLDIGEST_FINE;
  236. }
  237. /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
  238. static void md5_to_ascii(unsigned char *source, /* 16 bytes */
  239. unsigned char *dest) /* 33 bytes */
  240. {
  241. int i;
  242. for(i=0; i<16; i++)
  243. snprintf((char *)&dest[i*2], 3, "%02x", source[i]);
  244. }
  245. CURLcode Curl_output_digest(struct connectdata *conn,
  246. bool proxy,
  247. const unsigned char *request,
  248. const unsigned char *uripath)
  249. {
  250. /* We have a Digest setup for this, use it! Now, to get all the details for
  251. this sorted out, I must urge you dear friend to read up on the RFC2617
  252. section 3.2.2, */
  253. unsigned char md5buf[16]; /* 16 bytes/128 bits */
  254. unsigned char request_digest[33];
  255. unsigned char *md5this;
  256. unsigned char *ha1;
  257. unsigned char ha2[33];/* 32 digits and 1 zero byte */
  258. char cnoncebuf[7];
  259. char *cnonce;
  260. char *tmp = NULL;
  261. struct timeval now;
  262. char **allocuserpwd;
  263. const char *userp;
  264. const char *passwdp;
  265. struct auth *authp;
  266. struct SessionHandle *data = conn->data;
  267. struct digestdata *d;
  268. #ifdef CURL_DOES_CONVERSIONS
  269. CURLcode rc;
  270. /* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
  271. It converts digest text to ASCII so the MD5 will be correct for
  272. what ultimately goes over the network.
  273. */
  274. #define CURL_OUTPUT_DIGEST_CONV(a, b) \
  275. rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
  276. if(rc != CURLE_OK) { \
  277. free(b); \
  278. return rc; \
  279. }
  280. #else
  281. #define CURL_OUTPUT_DIGEST_CONV(a, b)
  282. #endif /* CURL_DOES_CONVERSIONS */
  283. if(proxy) {
  284. d = &data->state.proxydigest;
  285. allocuserpwd = &conn->allocptr.proxyuserpwd;
  286. userp = conn->proxyuser;
  287. passwdp = conn->proxypasswd;
  288. authp = &data->state.authproxy;
  289. }
  290. else {
  291. d = &data->state.digest;
  292. allocuserpwd = &conn->allocptr.userpwd;
  293. userp = conn->user;
  294. passwdp = conn->passwd;
  295. authp = &data->state.authhost;
  296. }
  297. if(*allocuserpwd) {
  298. Curl_safefree(*allocuserpwd);
  299. *allocuserpwd = NULL;
  300. }
  301. /* not set means empty */
  302. if(!userp)
  303. userp="";
  304. if(!passwdp)
  305. passwdp="";
  306. if(!d->nonce) {
  307. authp->done = FALSE;
  308. return CURLE_OK;
  309. }
  310. authp->done = TRUE;
  311. if(!d->nc)
  312. d->nc = 1;
  313. if(!d->cnonce) {
  314. /* Generate a cnonce */
  315. now = Curl_tvnow();
  316. snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", (long)now.tv_sec);
  317. if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
  318. d->cnonce = cnonce;
  319. else
  320. return CURLE_OUT_OF_MEMORY;
  321. }
  322. /*
  323. if the algorithm is "MD5" or unspecified (which then defaults to MD5):
  324. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  325. if the algorithm is "MD5-sess" then:
  326. A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
  327. ":" unq(nonce-value) ":" unq(cnonce-value)
  328. */
  329. md5this = (unsigned char *)
  330. aprintf("%s:%s:%s", userp, d->realm, passwdp);
  331. if(!md5this)
  332. return CURLE_OUT_OF_MEMORY;
  333. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  334. Curl_md5it(md5buf, md5this);
  335. free(md5this); /* free this again */
  336. ha1 = malloc(33); /* 32 digits and 1 zero byte */
  337. if(!ha1)
  338. return CURLE_OUT_OF_MEMORY;
  339. md5_to_ascii(md5buf, ha1);
  340. if(d->algo == CURLDIGESTALGO_MD5SESS) {
  341. /* nonce and cnonce are OUTSIDE the hash */
  342. tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
  343. if(!tmp)
  344. return CURLE_OUT_OF_MEMORY;
  345. CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
  346. Curl_md5it(md5buf, (unsigned char *)tmp);
  347. free(tmp); /* free this again */
  348. md5_to_ascii(md5buf, ha1);
  349. }
  350. /*
  351. If the "qop" directive's value is "auth" or is unspecified, then A2 is:
  352. A2 = Method ":" digest-uri-value
  353. If the "qop" value is "auth-int", then A2 is:
  354. A2 = Method ":" digest-uri-value ":" H(entity-body)
  355. (The "Method" value is the HTTP request method as specified in section
  356. 5.1.1 of RFC 2616)
  357. */
  358. /* So IE browsers < v7 cut off the URI part at the query part when they
  359. evaluate the MD5 and some (IIS?) servers work with them so we may need to
  360. do the Digest IE-style. Note that the different ways cause different MD5
  361. sums to get sent.
  362. Apache servers can be set to do the Digest IE-style automatically using
  363. the BrowserMatch feature:
  364. http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
  365. Further details on Digest implementation differences:
  366. http://www.fngtps.com/2006/09/http-authentication
  367. */
  368. if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
  369. md5this = (unsigned char *)aprintf("%s:%.*s", request,
  370. (int)(tmp - (char *)uripath), uripath);
  371. }
  372. else
  373. md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
  374. if(!md5this) {
  375. free(ha1);
  376. return CURLE_OUT_OF_MEMORY;
  377. }
  378. if(d->qop && Curl_raw_equal(d->qop, "auth-int")) {
  379. /* We don't support auth-int at the moment. I can't see a easy way to get
  380. entity-body here */
  381. /* TODO: Append H(entity-body)*/
  382. }
  383. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  384. Curl_md5it(md5buf, md5this);
  385. free(md5this); /* free this again */
  386. md5_to_ascii(md5buf, ha2);
  387. if(d->qop) {
  388. md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
  389. ha1,
  390. d->nonce,
  391. d->nc,
  392. d->cnonce,
  393. d->qop,
  394. ha2);
  395. }
  396. else {
  397. md5this = (unsigned char *)aprintf("%s:%s:%s",
  398. ha1,
  399. d->nonce,
  400. ha2);
  401. }
  402. free(ha1);
  403. if(!md5this)
  404. return CURLE_OUT_OF_MEMORY;
  405. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  406. Curl_md5it(md5buf, md5this);
  407. free(md5this); /* free this again */
  408. md5_to_ascii(md5buf, request_digest);
  409. /* for test case 64 (snooped from a Mozilla 1.3a request)
  410. Authorization: Digest username="testuser", realm="testrealm", \
  411. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  412. */
  413. if(d->qop) {
  414. *allocuserpwd =
  415. aprintf( "%sAuthorization: Digest "
  416. "username=\"%s\", "
  417. "realm=\"%s\", "
  418. "nonce=\"%s\", "
  419. "uri=\"%s\", "
  420. "cnonce=\"%s\", "
  421. "nc=%08x, "
  422. "qop=\"%s\", "
  423. "response=\"%s\"",
  424. proxy?"Proxy-":"",
  425. userp,
  426. d->realm,
  427. d->nonce,
  428. uripath, /* this is the PATH part of the URL */
  429. d->cnonce,
  430. d->nc,
  431. d->qop,
  432. request_digest);
  433. if(Curl_raw_equal(d->qop, "auth"))
  434. d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
  435. which tells to the server how many times you are using the
  436. same nonce in the qop=auth mode. */
  437. }
  438. else {
  439. *allocuserpwd =
  440. aprintf( "%sAuthorization: Digest "
  441. "username=\"%s\", "
  442. "realm=\"%s\", "
  443. "nonce=\"%s\", "
  444. "uri=\"%s\", "
  445. "response=\"%s\"",
  446. proxy?"Proxy-":"",
  447. userp,
  448. d->realm,
  449. d->nonce,
  450. uripath, /* this is the PATH part of the URL */
  451. request_digest);
  452. }
  453. if(!*allocuserpwd)
  454. return CURLE_OUT_OF_MEMORY;
  455. /* Add optional fields */
  456. if(d->opaque) {
  457. /* append opaque */
  458. tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
  459. if(!tmp)
  460. return CURLE_OUT_OF_MEMORY;
  461. free(*allocuserpwd);
  462. *allocuserpwd = tmp;
  463. }
  464. if(d->algorithm) {
  465. /* append algorithm */
  466. tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
  467. if(!tmp)
  468. return CURLE_OUT_OF_MEMORY;
  469. free(*allocuserpwd);
  470. *allocuserpwd = tmp;
  471. }
  472. /* append CRLF + zero (3 bytes) to the userpwd header */
  473. tmp = realloc(*allocuserpwd, strlen(*allocuserpwd) + 3);
  474. if(!tmp)
  475. return CURLE_OUT_OF_MEMORY;
  476. strcat(tmp, "\r\n");
  477. *allocuserpwd = tmp;
  478. return CURLE_OK;
  479. }
  480. void Curl_digest_cleanup_one(struct digestdata *d)
  481. {
  482. if(d->nonce)
  483. free(d->nonce);
  484. d->nonce = NULL;
  485. if(d->cnonce)
  486. free(d->cnonce);
  487. d->cnonce = NULL;
  488. if(d->realm)
  489. free(d->realm);
  490. d->realm = NULL;
  491. if(d->opaque)
  492. free(d->opaque);
  493. d->opaque = NULL;
  494. if(d->qop)
  495. free(d->qop);
  496. d->qop = NULL;
  497. if(d->algorithm)
  498. free(d->algorithm);
  499. d->algorithm = NULL;
  500. d->nc = 0;
  501. d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
  502. d->stale = FALSE; /* default means normal, not stale */
  503. }
  504. void Curl_digest_cleanup(struct SessionHandle *data)
  505. {
  506. Curl_digest_cleanup_one(&data->state.digest);
  507. Curl_digest_cleanup_one(&data->state.proxydigest);
  508. }
  509. #endif