http_digest.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. size_t totlen=0;
  145. while(*header && ISSPACE(*header))
  146. header++;
  147. /* extract a value=content pair */
  148. if(!get_pair(header, value, content, &header)) {
  149. if(Curl_raw_equal(value, "nonce")) {
  150. d->nonce = strdup(content);
  151. if(!d->nonce)
  152. return CURLDIGEST_NOMEM;
  153. }
  154. else if(Curl_raw_equal(value, "stale")) {
  155. if(Curl_raw_equal(content, "true")) {
  156. d->stale = TRUE;
  157. d->nc = 1; /* we make a new nonce now */
  158. }
  159. }
  160. else if(Curl_raw_equal(value, "realm")) {
  161. d->realm = strdup(content);
  162. if(!d->realm)
  163. return CURLDIGEST_NOMEM;
  164. }
  165. else if(Curl_raw_equal(value, "opaque")) {
  166. d->opaque = strdup(content);
  167. if(!d->opaque)
  168. return CURLDIGEST_NOMEM;
  169. }
  170. else if(Curl_raw_equal(value, "qop")) {
  171. char *tok_buf;
  172. /* tokenize the list and choose auth if possible, use a temporary
  173. clone of the buffer since strtok_r() ruins it */
  174. tmp = strdup(content);
  175. if(!tmp)
  176. return CURLDIGEST_NOMEM;
  177. token = strtok_r(tmp, ",", &tok_buf);
  178. while(token != NULL) {
  179. if(Curl_raw_equal(token, "auth")) {
  180. foundAuth = TRUE;
  181. }
  182. else if(Curl_raw_equal(token, "auth-int")) {
  183. foundAuthInt = TRUE;
  184. }
  185. token = strtok_r(NULL, ",", &tok_buf);
  186. }
  187. free(tmp);
  188. /*select only auth o auth-int. Otherwise, ignore*/
  189. if(foundAuth) {
  190. d->qop = strdup("auth");
  191. if(!d->qop)
  192. return CURLDIGEST_NOMEM;
  193. }
  194. else if(foundAuthInt) {
  195. d->qop = strdup("auth-int");
  196. if(!d->qop)
  197. return CURLDIGEST_NOMEM;
  198. }
  199. }
  200. else if(Curl_raw_equal(value, "algorithm")) {
  201. d->algorithm = strdup(content);
  202. if(!d->algorithm)
  203. return CURLDIGEST_NOMEM;
  204. if(Curl_raw_equal(content, "MD5-sess"))
  205. d->algo = CURLDIGESTALGO_MD5SESS;
  206. else if(Curl_raw_equal(content, "MD5"))
  207. d->algo = CURLDIGESTALGO_MD5;
  208. else
  209. return CURLDIGEST_BADALGO;
  210. }
  211. else {
  212. /* unknown specifier, ignore it! */
  213. }
  214. totlen = strlen(value)+strlen(content)+1;
  215. if(header[strlen(value)+1] == '\"')
  216. /* the contents were within quotes, then add 2 for them to the
  217. length */
  218. totlen += 2;
  219. }
  220. else
  221. break; /* we're done here */
  222. /* pass all additional spaces here */
  223. while(*header && ISSPACE(*header))
  224. header++;
  225. if(',' == *header)
  226. /* allow the list to be comma-separated */
  227. header++;
  228. }
  229. /* We had a nonce since before, and we got another one now without
  230. 'stale=true'. This means we provided bad credentials in the previous
  231. request */
  232. if(before && !d->stale)
  233. return CURLDIGEST_BAD;
  234. /* We got this header without a nonce, that's a bad Digest line! */
  235. if(!d->nonce)
  236. return CURLDIGEST_BAD;
  237. }
  238. else
  239. /* else not a digest, get out */
  240. return CURLDIGEST_NONE;
  241. return CURLDIGEST_FINE;
  242. }
  243. /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
  244. static void md5_to_ascii(unsigned char *source, /* 16 bytes */
  245. unsigned char *dest) /* 33 bytes */
  246. {
  247. int i;
  248. for(i=0; i<16; i++)
  249. snprintf((char *)&dest[i*2], 3, "%02x", source[i]);
  250. }
  251. CURLcode Curl_output_digest(struct connectdata *conn,
  252. bool proxy,
  253. const unsigned char *request,
  254. const unsigned char *uripath)
  255. {
  256. /* We have a Digest setup for this, use it! Now, to get all the details for
  257. this sorted out, I must urge you dear friend to read up on the RFC2617
  258. section 3.2.2, */
  259. unsigned char md5buf[16]; /* 16 bytes/128 bits */
  260. unsigned char request_digest[33];
  261. unsigned char *md5this;
  262. unsigned char *ha1;
  263. unsigned char ha2[33];/* 32 digits and 1 zero byte */
  264. char cnoncebuf[7];
  265. char *cnonce;
  266. char *tmp = NULL;
  267. struct timeval now;
  268. char **allocuserpwd;
  269. const char *userp;
  270. const char *passwdp;
  271. struct auth *authp;
  272. struct SessionHandle *data = conn->data;
  273. struct digestdata *d;
  274. #ifdef CURL_DOES_CONVERSIONS
  275. CURLcode rc;
  276. /* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
  277. It converts digest text to ASCII so the MD5 will be correct for
  278. what ultimately goes over the network.
  279. */
  280. #define CURL_OUTPUT_DIGEST_CONV(a, b) \
  281. rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
  282. if(rc != CURLE_OK) { \
  283. free(b); \
  284. return rc; \
  285. }
  286. #else
  287. #define CURL_OUTPUT_DIGEST_CONV(a, b)
  288. #endif /* CURL_DOES_CONVERSIONS */
  289. if(proxy) {
  290. d = &data->state.proxydigest;
  291. allocuserpwd = &conn->allocptr.proxyuserpwd;
  292. userp = conn->proxyuser;
  293. passwdp = conn->proxypasswd;
  294. authp = &data->state.authproxy;
  295. }
  296. else {
  297. d = &data->state.digest;
  298. allocuserpwd = &conn->allocptr.userpwd;
  299. userp = conn->user;
  300. passwdp = conn->passwd;
  301. authp = &data->state.authhost;
  302. }
  303. if(*allocuserpwd) {
  304. Curl_safefree(*allocuserpwd);
  305. *allocuserpwd = NULL;
  306. }
  307. /* not set means empty */
  308. if(!userp)
  309. userp="";
  310. if(!passwdp)
  311. passwdp="";
  312. if(!d->nonce) {
  313. authp->done = FALSE;
  314. return CURLE_OK;
  315. }
  316. authp->done = TRUE;
  317. if(!d->nc)
  318. d->nc = 1;
  319. if(!d->cnonce) {
  320. /* Generate a cnonce */
  321. now = Curl_tvnow();
  322. snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", (long)now.tv_sec);
  323. if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
  324. d->cnonce = cnonce;
  325. else
  326. return CURLE_OUT_OF_MEMORY;
  327. }
  328. /*
  329. if the algorithm is "MD5" or unspecified (which then defaults to MD5):
  330. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  331. if the algorithm is "MD5-sess" then:
  332. A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
  333. ":" unq(nonce-value) ":" unq(cnonce-value)
  334. */
  335. md5this = (unsigned char *)
  336. aprintf("%s:%s:%s", userp, d->realm, passwdp);
  337. if(!md5this)
  338. return CURLE_OUT_OF_MEMORY;
  339. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  340. Curl_md5it(md5buf, md5this);
  341. free(md5this); /* free this again */
  342. ha1 = malloc(33); /* 32 digits and 1 zero byte */
  343. if(!ha1)
  344. return CURLE_OUT_OF_MEMORY;
  345. md5_to_ascii(md5buf, ha1);
  346. if(d->algo == CURLDIGESTALGO_MD5SESS) {
  347. /* nonce and cnonce are OUTSIDE the hash */
  348. tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
  349. if(!tmp)
  350. return CURLE_OUT_OF_MEMORY;
  351. CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
  352. Curl_md5it(md5buf, (unsigned char *)tmp);
  353. free(tmp); /* free this again */
  354. md5_to_ascii(md5buf, ha1);
  355. }
  356. /*
  357. If the "qop" directive's value is "auth" or is unspecified, then A2 is:
  358. A2 = Method ":" digest-uri-value
  359. If the "qop" value is "auth-int", then A2 is:
  360. A2 = Method ":" digest-uri-value ":" H(entity-body)
  361. (The "Method" value is the HTTP request method as specified in section
  362. 5.1.1 of RFC 2616)
  363. */
  364. /* So IE browsers < v7 cut off the URI part at the query part when they
  365. evaluate the MD5 and some (IIS?) servers work with them so we may need to
  366. do the Digest IE-style. Note that the different ways cause different MD5
  367. sums to get sent.
  368. Apache servers can be set to do the Digest IE-style automatically using
  369. the BrowserMatch feature:
  370. http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
  371. Further details on Digest implementation differences:
  372. http://www.fngtps.com/2006/09/http-authentication
  373. */
  374. if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
  375. md5this = (unsigned char *)aprintf("%s:%.*s", request,
  376. (int)(tmp - (char *)uripath), uripath);
  377. }
  378. else
  379. md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
  380. if(!md5this) {
  381. free(ha1);
  382. return CURLE_OUT_OF_MEMORY;
  383. }
  384. if(d->qop && Curl_raw_equal(d->qop, "auth-int")) {
  385. /* We don't support auth-int at the moment. I can't see a easy way to get
  386. entity-body here */
  387. /* TODO: Append H(entity-body)*/
  388. }
  389. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  390. Curl_md5it(md5buf, md5this);
  391. free(md5this); /* free this again */
  392. md5_to_ascii(md5buf, ha2);
  393. if(d->qop) {
  394. md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
  395. ha1,
  396. d->nonce,
  397. d->nc,
  398. d->cnonce,
  399. d->qop,
  400. ha2);
  401. }
  402. else {
  403. md5this = (unsigned char *)aprintf("%s:%s:%s",
  404. ha1,
  405. d->nonce,
  406. ha2);
  407. }
  408. free(ha1);
  409. if(!md5this)
  410. return CURLE_OUT_OF_MEMORY;
  411. CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
  412. Curl_md5it(md5buf, md5this);
  413. free(md5this); /* free this again */
  414. md5_to_ascii(md5buf, request_digest);
  415. /* for test case 64 (snooped from a Mozilla 1.3a request)
  416. Authorization: Digest username="testuser", realm="testrealm", \
  417. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  418. */
  419. if(d->qop) {
  420. *allocuserpwd =
  421. aprintf( "%sAuthorization: Digest "
  422. "username=\"%s\", "
  423. "realm=\"%s\", "
  424. "nonce=\"%s\", "
  425. "uri=\"%s\", "
  426. "cnonce=\"%s\", "
  427. "nc=%08x, "
  428. "qop=\"%s\", "
  429. "response=\"%s\"",
  430. proxy?"Proxy-":"",
  431. userp,
  432. d->realm,
  433. d->nonce,
  434. uripath, /* this is the PATH part of the URL */
  435. d->cnonce,
  436. d->nc,
  437. d->qop,
  438. request_digest);
  439. if(Curl_raw_equal(d->qop, "auth"))
  440. d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
  441. which tells to the server how many times you are using the
  442. same nonce in the qop=auth mode. */
  443. }
  444. else {
  445. *allocuserpwd =
  446. aprintf( "%sAuthorization: Digest "
  447. "username=\"%s\", "
  448. "realm=\"%s\", "
  449. "nonce=\"%s\", "
  450. "uri=\"%s\", "
  451. "response=\"%s\"",
  452. proxy?"Proxy-":"",
  453. userp,
  454. d->realm,
  455. d->nonce,
  456. uripath, /* this is the PATH part of the URL */
  457. request_digest);
  458. }
  459. if(!*allocuserpwd)
  460. return CURLE_OUT_OF_MEMORY;
  461. /* Add optional fields */
  462. if(d->opaque) {
  463. /* append opaque */
  464. tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
  465. if(!tmp)
  466. return CURLE_OUT_OF_MEMORY;
  467. free(*allocuserpwd);
  468. *allocuserpwd = tmp;
  469. }
  470. if(d->algorithm) {
  471. /* append algorithm */
  472. tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
  473. if(!tmp)
  474. return CURLE_OUT_OF_MEMORY;
  475. free(*allocuserpwd);
  476. *allocuserpwd = tmp;
  477. }
  478. /* append CRLF to the userpwd header */
  479. tmp = realloc(*allocuserpwd, strlen(*allocuserpwd) + 3 + 1);
  480. if(!tmp)
  481. return CURLE_OUT_OF_MEMORY;
  482. strcat(tmp, "\r\n");
  483. *allocuserpwd = tmp;
  484. return CURLE_OK;
  485. }
  486. void Curl_digest_cleanup_one(struct digestdata *d)
  487. {
  488. if(d->nonce)
  489. free(d->nonce);
  490. d->nonce = NULL;
  491. if(d->cnonce)
  492. free(d->cnonce);
  493. d->cnonce = NULL;
  494. if(d->realm)
  495. free(d->realm);
  496. d->realm = NULL;
  497. if(d->opaque)
  498. free(d->opaque);
  499. d->opaque = NULL;
  500. if(d->qop)
  501. free(d->qop);
  502. d->qop = NULL;
  503. if(d->algorithm)
  504. free(d->algorithm);
  505. d->algorithm = NULL;
  506. d->nc = 0;
  507. d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
  508. d->stale = FALSE; /* default means normal, not stale */
  509. }
  510. void Curl_digest_cleanup(struct SessionHandle *data)
  511. {
  512. Curl_digest_cleanup_one(&data->state.digest);
  513. Curl_digest_cleanup_one(&data->state.proxydigest);
  514. }
  515. #endif