http_digest.c 16 KB

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