digest.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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.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. * SPDX-License-Identifier: curl
  22. *
  23. * RFC2831 DIGEST-MD5 authentication
  24. * RFC7616 DIGEST-SHA256, DIGEST-SHA512-256 authentication
  25. *
  26. ***************************************************************************/
  27. #include "../curl_setup.h"
  28. #ifndef CURL_DISABLE_DIGEST_AUTH
  29. #include <curl/curl.h>
  30. #include "vauth.h"
  31. #include "digest.h"
  32. #include "../urldata.h"
  33. #include "../curlx/base64.h"
  34. #include "../curl_hmac.h"
  35. #include "../curl_md5.h"
  36. #include "../curl_sha256.h"
  37. #include "../curl_sha512_256.h"
  38. #include "../vtls/vtls.h"
  39. #include "../curlx/warnless.h"
  40. #include "../curlx/strparse.h"
  41. #include "../rand.h"
  42. /* The last #include files should be: */
  43. #include "../curl_memory.h"
  44. #include "../memdebug.h"
  45. #ifndef USE_WINDOWS_SSPI
  46. #define SESSION_ALGO 1 /* for algos with this bit set */
  47. #define ALGO_MD5 0
  48. #define ALGO_MD5SESS (ALGO_MD5 | SESSION_ALGO)
  49. #define ALGO_SHA256 2
  50. #define ALGO_SHA256SESS (ALGO_SHA256 | SESSION_ALGO)
  51. #define ALGO_SHA512_256 4
  52. #define ALGO_SHA512_256SESS (ALGO_SHA512_256 | SESSION_ALGO)
  53. #define DIGEST_QOP_VALUE_AUTH (1 << 0)
  54. #define DIGEST_QOP_VALUE_AUTH_INT (1 << 1)
  55. #define DIGEST_QOP_VALUE_AUTH_CONF (1 << 2)
  56. #define DIGEST_QOP_VALUE_STRING_AUTH "auth"
  57. #define DIGEST_QOP_VALUE_STRING_AUTH_INT "auth-int"
  58. #define DIGEST_QOP_VALUE_STRING_AUTH_CONF "auth-conf"
  59. #endif
  60. bool Curl_auth_digest_get_pair(const char *str, char *value, char *content,
  61. const char **endptr)
  62. {
  63. int c;
  64. bool starts_with_quote = FALSE;
  65. bool escape = FALSE;
  66. for(c = DIGEST_MAX_VALUE_LENGTH - 1; (*str && (*str != '=') && c--);)
  67. *value++ = *str++;
  68. *value = 0;
  69. if('=' != *str++)
  70. /* eek, no match */
  71. return FALSE;
  72. if('\"' == *str) {
  73. /* This starts with a quote so it must end with one as well! */
  74. str++;
  75. starts_with_quote = TRUE;
  76. }
  77. for(c = DIGEST_MAX_CONTENT_LENGTH - 1; *str && c--; str++) {
  78. if(!escape) {
  79. switch(*str) {
  80. case '\\':
  81. if(starts_with_quote) {
  82. /* the start of an escaped quote */
  83. escape = TRUE;
  84. continue;
  85. }
  86. break;
  87. case ',':
  88. if(!starts_with_quote) {
  89. /* This signals the end of the content if we did not get a starting
  90. quote and then we do "sloppy" parsing */
  91. c = 0; /* the end */
  92. continue;
  93. }
  94. break;
  95. case '\r':
  96. case '\n':
  97. /* end of string */
  98. if(starts_with_quote)
  99. return FALSE; /* No closing quote */
  100. c = 0;
  101. continue;
  102. case '\"':
  103. if(starts_with_quote) {
  104. /* end of string */
  105. c = 0;
  106. continue;
  107. }
  108. else
  109. return FALSE;
  110. }
  111. }
  112. escape = FALSE;
  113. *content++ = *str;
  114. }
  115. if(escape)
  116. return FALSE; /* No character after backslash */
  117. *content = 0;
  118. *endptr = str;
  119. return TRUE;
  120. }
  121. #ifndef USE_WINDOWS_SSPI
  122. /* Convert MD5 chunk to RFC2617 (section 3.1.3) -suitable ASCII string */
  123. static void auth_digest_md5_to_ascii(unsigned char *source, /* 16 bytes */
  124. unsigned char *dest) /* 33 bytes */
  125. {
  126. int i;
  127. for(i = 0; i < 16; i++)
  128. curl_msnprintf((char *) &dest[i * 2], 3, "%02x", source[i]);
  129. }
  130. /* Convert sha256 or SHA-512/256 chunk to RFC7616 -suitable ASCII string */
  131. static void auth_digest_sha256_to_ascii(unsigned char *source, /* 32 bytes */
  132. unsigned char *dest) /* 65 bytes */
  133. {
  134. int i;
  135. for(i = 0; i < 32; i++)
  136. curl_msnprintf((char *) &dest[i * 2], 3, "%02x", source[i]);
  137. }
  138. /* Perform quoted-string escaping as described in RFC2616 and its errata */
  139. static char *auth_digest_string_quoted(const char *source)
  140. {
  141. char *dest;
  142. const char *s = source;
  143. size_t n = 1; /* null-terminator */
  144. /* Calculate size needed */
  145. while(*s) {
  146. ++n;
  147. if(*s == '"' || *s == '\\') {
  148. ++n;
  149. }
  150. ++s;
  151. }
  152. dest = malloc(n);
  153. if(dest) {
  154. char *d = dest;
  155. s = source;
  156. while(*s) {
  157. if(*s == '"' || *s == '\\') {
  158. *d++ = '\\';
  159. }
  160. *d++ = *s++;
  161. }
  162. *d = '\0';
  163. }
  164. return dest;
  165. }
  166. /* Retrieves the value for a corresponding key from the challenge string
  167. * returns TRUE if the key could be found, FALSE if it does not exists
  168. */
  169. static bool auth_digest_get_key_value(const char *chlg, const char *key,
  170. char *buf, size_t buflen)
  171. {
  172. /* keyword=[value],keyword2=[value]
  173. The values may or may not be quoted.
  174. */
  175. do {
  176. struct Curl_str data;
  177. struct Curl_str name;
  178. if(!curlx_str_until(&chlg, &name, 64, '=') &&
  179. !curlx_str_single(&chlg, '=')) {
  180. /* this is the key, get the value, possibly quoted */
  181. int rc = curlx_str_quotedword(&chlg, &data, 256);
  182. if(rc == STRE_BEGQUOTE)
  183. /* try unquoted until comma */
  184. rc = curlx_str_until(&chlg, &data, 256, ',');
  185. if(rc)
  186. return FALSE; /* weird */
  187. if(curlx_str_cmp(&name, key)) {
  188. /* if this is our key, return the value */
  189. if(curlx_strlen(&data) >= buflen)
  190. /* doesn't fit */
  191. return FALSE;
  192. memcpy(buf, curlx_str(&data), curlx_strlen(&data));
  193. buf[curlx_strlen(&data)] = 0;
  194. return TRUE;
  195. }
  196. if(curlx_str_single(&chlg, ','))
  197. return FALSE;
  198. }
  199. else /* odd syntax */
  200. break;
  201. } while(1);
  202. return FALSE;
  203. }
  204. static CURLcode auth_digest_get_qop_values(const char *options, int *value)
  205. {
  206. struct Curl_str out;
  207. /* Initialise the output */
  208. *value = 0;
  209. while(!curlx_str_until(&options, &out, 32, ',')) {
  210. if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH))
  211. *value |= DIGEST_QOP_VALUE_AUTH;
  212. else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_INT))
  213. *value |= DIGEST_QOP_VALUE_AUTH_INT;
  214. else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_CONF))
  215. *value |= DIGEST_QOP_VALUE_AUTH_CONF;
  216. if(curlx_str_single(&options, ','))
  217. break;
  218. }
  219. return CURLE_OK;
  220. }
  221. /*
  222. * auth_decode_digest_md5_message()
  223. *
  224. * This is used internally to decode an already encoded DIGEST-MD5 challenge
  225. * message into the separate attributes.
  226. *
  227. * Parameters:
  228. *
  229. * chlgref [in] - The challenge message.
  230. * nonce [in/out] - The buffer where the nonce will be stored.
  231. * nlen [in] - The length of the nonce buffer.
  232. * realm [in/out] - The buffer where the realm will be stored.
  233. * rlen [in] - The length of the realm buffer.
  234. * alg [in/out] - The buffer where the algorithm will be stored.
  235. * alen [in] - The length of the algorithm buffer.
  236. * qop [in/out] - The buffer where the qop-options will be stored.
  237. * qlen [in] - The length of the qop buffer.
  238. *
  239. * Returns CURLE_OK on success.
  240. */
  241. static CURLcode auth_decode_digest_md5_message(const struct bufref *chlgref,
  242. char *nonce, size_t nlen,
  243. char *realm, size_t rlen,
  244. char *alg, size_t alen,
  245. char *qop, size_t qlen)
  246. {
  247. const char *chlg = (const char *) Curl_bufref_ptr(chlgref);
  248. /* Ensure we have a valid challenge message */
  249. if(!Curl_bufref_len(chlgref))
  250. return CURLE_BAD_CONTENT_ENCODING;
  251. /* Retrieve nonce string from the challenge */
  252. if(!auth_digest_get_key_value(chlg, "nonce", nonce, nlen))
  253. return CURLE_BAD_CONTENT_ENCODING;
  254. /* Retrieve realm string from the challenge */
  255. if(!auth_digest_get_key_value(chlg, "realm", realm, rlen)) {
  256. /* Challenge does not have a realm, set empty string [RFC2831] page 6 */
  257. *realm = '\0';
  258. }
  259. /* Retrieve algorithm string from the challenge */
  260. if(!auth_digest_get_key_value(chlg, "algorithm", alg, alen))
  261. return CURLE_BAD_CONTENT_ENCODING;
  262. /* Retrieve qop-options string from the challenge */
  263. if(!auth_digest_get_key_value(chlg, "qop", qop, qlen))
  264. return CURLE_BAD_CONTENT_ENCODING;
  265. return CURLE_OK;
  266. }
  267. /*
  268. * Curl_auth_is_digest_supported()
  269. *
  270. * This is used to evaluate if DIGEST is supported.
  271. *
  272. * Parameters: None
  273. *
  274. * Returns TRUE as DIGEST as handled by libcurl.
  275. */
  276. bool Curl_auth_is_digest_supported(void)
  277. {
  278. return TRUE;
  279. }
  280. /*
  281. * Curl_auth_create_digest_md5_message()
  282. *
  283. * This is used to generate an already encoded DIGEST-MD5 response message
  284. * ready for sending to the recipient.
  285. *
  286. * Parameters:
  287. *
  288. * data [in] - The session handle.
  289. * chlg [in] - The challenge message.
  290. * userp [in] - The username.
  291. * passwdp [in] - The user's password.
  292. * service [in] - The service type such as http, smtp, pop or imap.
  293. * out [out] - The result storage.
  294. *
  295. * Returns CURLE_OK on success.
  296. */
  297. CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
  298. const struct bufref *chlg,
  299. const char *userp,
  300. const char *passwdp,
  301. const char *service,
  302. struct bufref *out)
  303. {
  304. size_t i;
  305. struct MD5_context *ctxt;
  306. char *response = NULL;
  307. unsigned char digest[MD5_DIGEST_LEN];
  308. char HA1_hex[2 * MD5_DIGEST_LEN + 1];
  309. char HA2_hex[2 * MD5_DIGEST_LEN + 1];
  310. char resp_hash_hex[2 * MD5_DIGEST_LEN + 1];
  311. char nonce[64];
  312. char realm[128];
  313. char algorithm[64];
  314. char qop_options[64];
  315. int qop_values;
  316. char cnonce[33];
  317. char nonceCount[] = "00000001";
  318. char method[] = "AUTHENTICATE";
  319. char qop[] = DIGEST_QOP_VALUE_STRING_AUTH;
  320. char *spn = NULL;
  321. /* Decode the challenge message */
  322. CURLcode result = auth_decode_digest_md5_message(chlg,
  323. nonce, sizeof(nonce),
  324. realm, sizeof(realm),
  325. algorithm,
  326. sizeof(algorithm),
  327. qop_options,
  328. sizeof(qop_options));
  329. if(result)
  330. return result;
  331. /* We only support md5 sessions */
  332. if(strcmp(algorithm, "md5-sess") != 0)
  333. return CURLE_BAD_CONTENT_ENCODING;
  334. /* Get the qop-values from the qop-options */
  335. result = auth_digest_get_qop_values(qop_options, &qop_values);
  336. if(result)
  337. return result;
  338. /* We only support auth quality-of-protection */
  339. if(!(qop_values & DIGEST_QOP_VALUE_AUTH))
  340. return CURLE_BAD_CONTENT_ENCODING;
  341. /* Generate 32 random hex chars, 32 bytes + 1 null-termination */
  342. result = Curl_rand_hex(data, (unsigned char *)cnonce, sizeof(cnonce));
  343. if(result)
  344. return result;
  345. /* So far so good, now calculate A1 and H(A1) according to RFC 2831 */
  346. ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
  347. if(!ctxt)
  348. return CURLE_OUT_OF_MEMORY;
  349. Curl_MD5_update(ctxt, (const unsigned char *) userp,
  350. curlx_uztoui(strlen(userp)));
  351. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  352. Curl_MD5_update(ctxt, (const unsigned char *) realm,
  353. curlx_uztoui(strlen(realm)));
  354. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  355. Curl_MD5_update(ctxt, (const unsigned char *) passwdp,
  356. curlx_uztoui(strlen(passwdp)));
  357. Curl_MD5_final(ctxt, digest);
  358. ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
  359. if(!ctxt)
  360. return CURLE_OUT_OF_MEMORY;
  361. Curl_MD5_update(ctxt, (const unsigned char *) digest, MD5_DIGEST_LEN);
  362. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  363. Curl_MD5_update(ctxt, (const unsigned char *) nonce,
  364. curlx_uztoui(strlen(nonce)));
  365. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  366. Curl_MD5_update(ctxt, (const unsigned char *) cnonce,
  367. curlx_uztoui(strlen(cnonce)));
  368. Curl_MD5_final(ctxt, digest);
  369. /* Convert calculated 16 octet hex into 32 bytes string */
  370. for(i = 0; i < MD5_DIGEST_LEN; i++)
  371. curl_msnprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]);
  372. /* Generate our SPN */
  373. spn = Curl_auth_build_spn(service, data->conn->host.name, NULL);
  374. if(!spn)
  375. return CURLE_OUT_OF_MEMORY;
  376. /* Calculate H(A2) */
  377. ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
  378. if(!ctxt) {
  379. free(spn);
  380. return CURLE_OUT_OF_MEMORY;
  381. }
  382. Curl_MD5_update(ctxt, (const unsigned char *) method,
  383. curlx_uztoui(strlen(method)));
  384. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  385. Curl_MD5_update(ctxt, (const unsigned char *) spn,
  386. curlx_uztoui(strlen(spn)));
  387. Curl_MD5_final(ctxt, digest);
  388. for(i = 0; i < MD5_DIGEST_LEN; i++)
  389. curl_msnprintf(&HA2_hex[2 * i], 3, "%02x", digest[i]);
  390. /* Now calculate the response hash */
  391. ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
  392. if(!ctxt) {
  393. free(spn);
  394. return CURLE_OUT_OF_MEMORY;
  395. }
  396. Curl_MD5_update(ctxt, (const unsigned char *) HA1_hex, 2 * MD5_DIGEST_LEN);
  397. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  398. Curl_MD5_update(ctxt, (const unsigned char *) nonce,
  399. curlx_uztoui(strlen(nonce)));
  400. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  401. Curl_MD5_update(ctxt, (const unsigned char *) nonceCount,
  402. curlx_uztoui(strlen(nonceCount)));
  403. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  404. Curl_MD5_update(ctxt, (const unsigned char *) cnonce,
  405. curlx_uztoui(strlen(cnonce)));
  406. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  407. Curl_MD5_update(ctxt, (const unsigned char *) qop,
  408. curlx_uztoui(strlen(qop)));
  409. Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
  410. Curl_MD5_update(ctxt, (const unsigned char *) HA2_hex, 2 * MD5_DIGEST_LEN);
  411. Curl_MD5_final(ctxt, digest);
  412. for(i = 0; i < MD5_DIGEST_LEN; i++)
  413. curl_msnprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]);
  414. /* Generate the response */
  415. response = curl_maprintf("username=\"%s\",realm=\"%s\",nonce=\"%s\","
  416. "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\","
  417. "response=%s,qop=%s",
  418. userp, realm, nonce,
  419. cnonce, nonceCount, spn, resp_hash_hex, qop);
  420. free(spn);
  421. if(!response)
  422. return CURLE_OUT_OF_MEMORY;
  423. /* Return the response. */
  424. Curl_bufref_set(out, response, strlen(response), curl_free);
  425. return result;
  426. }
  427. /*
  428. * Curl_auth_decode_digest_http_message()
  429. *
  430. * This is used to decode an HTTP DIGEST challenge message into the separate
  431. * attributes.
  432. *
  433. * Parameters:
  434. *
  435. * chlg [in] - The challenge message.
  436. * digest [in/out] - The digest data struct being used and modified.
  437. *
  438. * Returns CURLE_OK on success.
  439. */
  440. CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
  441. struct digestdata *digest)
  442. {
  443. bool before = FALSE; /* got a nonce before */
  444. /* If we already have received a nonce, keep that in mind */
  445. if(digest->nonce)
  446. before = TRUE;
  447. /* Clean up any former leftovers and initialise to defaults */
  448. Curl_auth_digest_cleanup(digest);
  449. for(;;) {
  450. char value[DIGEST_MAX_VALUE_LENGTH];
  451. char content[DIGEST_MAX_CONTENT_LENGTH];
  452. /* Pass all additional spaces here */
  453. while(*chlg && ISBLANK(*chlg))
  454. chlg++;
  455. /* Extract a value=content pair */
  456. if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
  457. if(curl_strequal(value, "nonce")) {
  458. free(digest->nonce);
  459. digest->nonce = strdup(content);
  460. if(!digest->nonce)
  461. return CURLE_OUT_OF_MEMORY;
  462. }
  463. else if(curl_strequal(value, "stale")) {
  464. if(curl_strequal(content, "true")) {
  465. digest->stale = TRUE;
  466. digest->nc = 1; /* we make a new nonce now */
  467. }
  468. }
  469. else if(curl_strequal(value, "realm")) {
  470. free(digest->realm);
  471. digest->realm = strdup(content);
  472. if(!digest->realm)
  473. return CURLE_OUT_OF_MEMORY;
  474. }
  475. else if(curl_strequal(value, "opaque")) {
  476. free(digest->opaque);
  477. digest->opaque = strdup(content);
  478. if(!digest->opaque)
  479. return CURLE_OUT_OF_MEMORY;
  480. }
  481. else if(curl_strequal(value, "qop")) {
  482. const char *token = content;
  483. struct Curl_str out;
  484. bool foundAuth = FALSE;
  485. bool foundAuthInt = FALSE;
  486. /* Pass leading spaces */
  487. while(*token && ISBLANK(*token))
  488. token++;
  489. while(!curlx_str_until(&token, &out, 32, ',')) {
  490. if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH))
  491. foundAuth = TRUE;
  492. else if(curlx_str_casecompare(&out,
  493. DIGEST_QOP_VALUE_STRING_AUTH_INT))
  494. foundAuthInt = TRUE;
  495. if(curlx_str_single(&token, ','))
  496. break;
  497. while(*token && ISBLANK(*token))
  498. token++;
  499. }
  500. /* Select only auth or auth-int. Otherwise, ignore */
  501. if(foundAuth) {
  502. free(digest->qop);
  503. digest->qop = strdup(DIGEST_QOP_VALUE_STRING_AUTH);
  504. if(!digest->qop)
  505. return CURLE_OUT_OF_MEMORY;
  506. }
  507. else if(foundAuthInt) {
  508. free(digest->qop);
  509. digest->qop = strdup(DIGEST_QOP_VALUE_STRING_AUTH_INT);
  510. if(!digest->qop)
  511. return CURLE_OUT_OF_MEMORY;
  512. }
  513. }
  514. else if(curl_strequal(value, "algorithm")) {
  515. free(digest->algorithm);
  516. digest->algorithm = strdup(content);
  517. if(!digest->algorithm)
  518. return CURLE_OUT_OF_MEMORY;
  519. if(curl_strequal(content, "MD5-sess"))
  520. digest->algo = ALGO_MD5SESS;
  521. else if(curl_strequal(content, "MD5"))
  522. digest->algo = ALGO_MD5;
  523. else if(curl_strequal(content, "SHA-256"))
  524. digest->algo = ALGO_SHA256;
  525. else if(curl_strequal(content, "SHA-256-SESS"))
  526. digest->algo = ALGO_SHA256SESS;
  527. else if(curl_strequal(content, "SHA-512-256")) {
  528. #ifdef CURL_HAVE_SHA512_256
  529. digest->algo = ALGO_SHA512_256;
  530. #else /* ! CURL_HAVE_SHA512_256 */
  531. return CURLE_NOT_BUILT_IN;
  532. #endif /* ! CURL_HAVE_SHA512_256 */
  533. }
  534. else if(curl_strequal(content, "SHA-512-256-SESS")) {
  535. #ifdef CURL_HAVE_SHA512_256
  536. digest->algo = ALGO_SHA512_256SESS;
  537. #else /* ! CURL_HAVE_SHA512_256 */
  538. return CURLE_NOT_BUILT_IN;
  539. #endif /* ! CURL_HAVE_SHA512_256 */
  540. }
  541. else
  542. return CURLE_BAD_CONTENT_ENCODING;
  543. }
  544. else if(curl_strequal(value, "userhash")) {
  545. if(curl_strequal(content, "true")) {
  546. digest->userhash = TRUE;
  547. }
  548. }
  549. else {
  550. /* Unknown specifier, ignore it! */
  551. }
  552. }
  553. else
  554. break; /* We are done here */
  555. /* Pass all additional spaces here */
  556. while(*chlg && ISBLANK(*chlg))
  557. chlg++;
  558. /* Allow the list to be comma-separated */
  559. if(',' == *chlg)
  560. chlg++;
  561. }
  562. /* We had a nonce since before, and we got another one now without
  563. 'stale=true'. This means we provided bad credentials in the previous
  564. request */
  565. if(before && !digest->stale)
  566. return CURLE_BAD_CONTENT_ENCODING;
  567. /* We got this header without a nonce, that is a bad Digest line! */
  568. if(!digest->nonce)
  569. return CURLE_BAD_CONTENT_ENCODING;
  570. /* "<algo>-sess" protocol versions require "auth" or "auth-int" qop */
  571. if(!digest->qop && (digest->algo & SESSION_ALGO))
  572. return CURLE_BAD_CONTENT_ENCODING;
  573. return CURLE_OK;
  574. }
  575. /*
  576. * auth_create_digest_http_message()
  577. *
  578. * This is used to generate an HTTP DIGEST response message ready for sending
  579. * to the recipient.
  580. *
  581. * Parameters:
  582. *
  583. * data [in] - The session handle.
  584. * userp [in] - The username.
  585. * passwdp [in] - The user's password.
  586. * request [in] - The HTTP request.
  587. * uripath [in] - The path of the HTTP uri.
  588. * digest [in/out] - The digest data struct being used and modified.
  589. * outptr [in/out] - The address where a pointer to newly allocated memory
  590. * holding the result will be stored upon completion.
  591. * outlen [out] - The length of the output message.
  592. *
  593. * Returns CURLE_OK on success.
  594. */
  595. static CURLcode auth_create_digest_http_message(
  596. struct Curl_easy *data,
  597. const char *userp,
  598. const char *passwdp,
  599. const unsigned char *request,
  600. const unsigned char *uripath,
  601. struct digestdata *digest,
  602. char **outptr, size_t *outlen,
  603. void (*convert_to_ascii)(unsigned char *, unsigned char *),
  604. CURLcode (*hash)(unsigned char *, const unsigned char *,
  605. const size_t))
  606. {
  607. CURLcode result;
  608. unsigned char hashbuf[32]; /* 32 bytes/256 bits */
  609. unsigned char request_digest[65];
  610. unsigned char ha1[65]; /* 64 digits and 1 zero byte */
  611. unsigned char ha2[65]; /* 64 digits and 1 zero byte */
  612. char userh[65];
  613. char *cnonce = NULL;
  614. size_t cnonce_sz = 0;
  615. char *userp_quoted;
  616. char *realm_quoted;
  617. char *nonce_quoted;
  618. char *response = NULL;
  619. char *hashthis = NULL;
  620. char *tmp = NULL;
  621. memset(hashbuf, 0, sizeof(hashbuf));
  622. if(!digest->nc)
  623. digest->nc = 1;
  624. if(!digest->cnonce) {
  625. char cnoncebuf[12];
  626. result = Curl_rand_bytes(data,
  627. #ifdef DEBUGBUILD
  628. TRUE,
  629. #endif
  630. (unsigned char *)cnoncebuf,
  631. sizeof(cnoncebuf));
  632. if(result)
  633. return result;
  634. result = curlx_base64_encode(cnoncebuf, sizeof(cnoncebuf),
  635. &cnonce, &cnonce_sz);
  636. if(result)
  637. return result;
  638. digest->cnonce = cnonce;
  639. }
  640. if(digest->userhash) {
  641. hashthis = curl_maprintf("%s:%s", userp,
  642. digest->realm ? digest->realm : "");
  643. if(!hashthis)
  644. return CURLE_OUT_OF_MEMORY;
  645. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  646. free(hashthis);
  647. if(result)
  648. return result;
  649. convert_to_ascii(hashbuf, (unsigned char *)userh);
  650. }
  651. /*
  652. If the algorithm is "MD5" or unspecified (which then defaults to MD5):
  653. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  654. If the algorithm is "MD5-sess" then:
  655. A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":"
  656. unq(nonce-value) ":" unq(cnonce-value)
  657. */
  658. hashthis = curl_maprintf("%s:%s:%s", userp,
  659. digest->realm ? digest->realm : "", passwdp);
  660. if(!hashthis)
  661. return CURLE_OUT_OF_MEMORY;
  662. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  663. free(hashthis);
  664. if(result)
  665. return result;
  666. convert_to_ascii(hashbuf, ha1);
  667. if(digest->algo & SESSION_ALGO) {
  668. /* nonce and cnonce are OUTSIDE the hash */
  669. tmp = curl_maprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce);
  670. if(!tmp)
  671. return CURLE_OUT_OF_MEMORY;
  672. result = hash(hashbuf, (unsigned char *) tmp, strlen(tmp));
  673. free(tmp);
  674. if(result)
  675. return result;
  676. convert_to_ascii(hashbuf, ha1);
  677. }
  678. /*
  679. If the "qop" directive's value is "auth" or is unspecified, then A2 is:
  680. A2 = Method ":" digest-uri-value
  681. If the "qop" value is "auth-int", then A2 is:
  682. A2 = Method ":" digest-uri-value ":" H(entity-body)
  683. (The "Method" value is the HTTP request method as specified in section
  684. 5.1.1 of RFC 2616)
  685. */
  686. hashthis = curl_maprintf("%s:%s", request, uripath);
  687. if(!hashthis)
  688. return CURLE_OUT_OF_MEMORY;
  689. if(digest->qop && curl_strequal(digest->qop, "auth-int")) {
  690. /* We do not support auth-int for PUT or POST */
  691. char hashed[65];
  692. char *hashthis2;
  693. result = hash(hashbuf, (const unsigned char *)"", 0);
  694. if(result) {
  695. free(hashthis);
  696. return result;
  697. }
  698. convert_to_ascii(hashbuf, (unsigned char *)hashed);
  699. hashthis2 = curl_maprintf("%s:%s", hashthis, hashed);
  700. free(hashthis);
  701. hashthis = hashthis2;
  702. }
  703. if(!hashthis)
  704. return CURLE_OUT_OF_MEMORY;
  705. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  706. free(hashthis);
  707. if(result)
  708. return result;
  709. convert_to_ascii(hashbuf, ha2);
  710. if(digest->qop) {
  711. hashthis = curl_maprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce,
  712. digest->nc, digest->cnonce, digest->qop, ha2);
  713. }
  714. else {
  715. hashthis = curl_maprintf("%s:%s:%s", ha1, digest->nonce, ha2);
  716. }
  717. if(!hashthis)
  718. return CURLE_OUT_OF_MEMORY;
  719. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  720. free(hashthis);
  721. if(result)
  722. return result;
  723. convert_to_ascii(hashbuf, request_digest);
  724. /* For test case 64 (snooped from a Mozilla 1.3a request)
  725. Authorization: Digest username="testuser", realm="testrealm", \
  726. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  727. Digest parameters are all quoted strings. Username which is provided by
  728. the user will need double quotes and backslashes within it escaped.
  729. realm, nonce, and opaque will need backslashes as well as they were
  730. de-escaped when copied from request header. cnonce is generated with
  731. web-safe characters. uri is already percent encoded. nc is 8 hex
  732. characters. algorithm and qop with standard values only contain web-safe
  733. characters.
  734. */
  735. userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp);
  736. if(!userp_quoted)
  737. return CURLE_OUT_OF_MEMORY;
  738. if(digest->realm)
  739. realm_quoted = auth_digest_string_quoted(digest->realm);
  740. else {
  741. realm_quoted = malloc(1);
  742. if(realm_quoted)
  743. realm_quoted[0] = 0;
  744. }
  745. if(!realm_quoted) {
  746. free(userp_quoted);
  747. return CURLE_OUT_OF_MEMORY;
  748. }
  749. nonce_quoted = auth_digest_string_quoted(digest->nonce);
  750. if(!nonce_quoted) {
  751. free(realm_quoted);
  752. free(userp_quoted);
  753. return CURLE_OUT_OF_MEMORY;
  754. }
  755. if(digest->qop) {
  756. response = curl_maprintf("username=\"%s\", "
  757. "realm=\"%s\", "
  758. "nonce=\"%s\", "
  759. "uri=\"%s\", "
  760. "cnonce=\"%s\", "
  761. "nc=%08x, "
  762. "qop=%s, "
  763. "response=\"%s\"",
  764. userp_quoted,
  765. realm_quoted,
  766. nonce_quoted,
  767. uripath,
  768. digest->cnonce,
  769. digest->nc,
  770. digest->qop,
  771. request_digest);
  772. /* Increment nonce-count to use another nc value for the next request */
  773. digest->nc++;
  774. }
  775. else {
  776. response = curl_maprintf("username=\"%s\", "
  777. "realm=\"%s\", "
  778. "nonce=\"%s\", "
  779. "uri=\"%s\", "
  780. "response=\"%s\"",
  781. userp_quoted,
  782. realm_quoted,
  783. nonce_quoted,
  784. uripath,
  785. request_digest);
  786. }
  787. free(nonce_quoted);
  788. free(realm_quoted);
  789. free(userp_quoted);
  790. if(!response)
  791. return CURLE_OUT_OF_MEMORY;
  792. /* Add the optional fields */
  793. if(digest->opaque) {
  794. char *opaque_quoted;
  795. /* Append the opaque */
  796. opaque_quoted = auth_digest_string_quoted(digest->opaque);
  797. if(!opaque_quoted) {
  798. free(response);
  799. return CURLE_OUT_OF_MEMORY;
  800. }
  801. tmp = curl_maprintf("%s, opaque=\"%s\"", response, opaque_quoted);
  802. free(response);
  803. free(opaque_quoted);
  804. if(!tmp)
  805. return CURLE_OUT_OF_MEMORY;
  806. response = tmp;
  807. }
  808. if(digest->algorithm) {
  809. /* Append the algorithm */
  810. tmp = curl_maprintf("%s, algorithm=%s", response, digest->algorithm);
  811. free(response);
  812. if(!tmp)
  813. return CURLE_OUT_OF_MEMORY;
  814. response = tmp;
  815. }
  816. if(digest->userhash) {
  817. /* Append the userhash */
  818. tmp = curl_maprintf("%s, userhash=true", response);
  819. free(response);
  820. if(!tmp)
  821. return CURLE_OUT_OF_MEMORY;
  822. response = tmp;
  823. }
  824. /* Return the output */
  825. *outptr = response;
  826. *outlen = strlen(response);
  827. return CURLE_OK;
  828. }
  829. /*
  830. * Curl_auth_create_digest_http_message()
  831. *
  832. * This is used to generate an HTTP DIGEST response message ready for sending
  833. * to the recipient.
  834. *
  835. * Parameters:
  836. *
  837. * data [in] - The session handle.
  838. * userp [in] - The username.
  839. * passwdp [in] - The user's password.
  840. * request [in] - The HTTP request.
  841. * uripath [in] - The path of the HTTP uri.
  842. * digest [in/out] - The digest data struct being used and modified.
  843. * outptr [in/out] - The address where a pointer to newly allocated memory
  844. * holding the result will be stored upon completion.
  845. * outlen [out] - The length of the output message.
  846. *
  847. * Returns CURLE_OK on success.
  848. */
  849. CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
  850. const char *userp,
  851. const char *passwdp,
  852. const unsigned char *request,
  853. const unsigned char *uripath,
  854. struct digestdata *digest,
  855. char **outptr, size_t *outlen)
  856. {
  857. if(digest->algo <= ALGO_MD5SESS)
  858. return auth_create_digest_http_message(data, userp, passwdp,
  859. request, uripath, digest,
  860. outptr, outlen,
  861. auth_digest_md5_to_ascii,
  862. Curl_md5it);
  863. if(digest->algo <= ALGO_SHA256SESS)
  864. return auth_create_digest_http_message(data, userp, passwdp,
  865. request, uripath, digest,
  866. outptr, outlen,
  867. auth_digest_sha256_to_ascii,
  868. Curl_sha256it);
  869. #ifdef CURL_HAVE_SHA512_256
  870. if(digest->algo <= ALGO_SHA512_256SESS)
  871. return auth_create_digest_http_message(data, userp, passwdp,
  872. request, uripath, digest,
  873. outptr, outlen,
  874. auth_digest_sha256_to_ascii,
  875. Curl_sha512_256it);
  876. #endif /* CURL_HAVE_SHA512_256 */
  877. /* Should be unreachable */
  878. return CURLE_BAD_CONTENT_ENCODING;
  879. }
  880. /*
  881. * Curl_auth_digest_cleanup()
  882. *
  883. * This is used to clean up the digest specific data.
  884. *
  885. * Parameters:
  886. *
  887. * digest [in/out] - The digest data struct being cleaned up.
  888. *
  889. */
  890. void Curl_auth_digest_cleanup(struct digestdata *digest)
  891. {
  892. Curl_safefree(digest->nonce);
  893. Curl_safefree(digest->cnonce);
  894. Curl_safefree(digest->realm);
  895. Curl_safefree(digest->opaque);
  896. Curl_safefree(digest->qop);
  897. Curl_safefree(digest->algorithm);
  898. digest->nc = 0;
  899. digest->algo = ALGO_MD5; /* default algorithm */
  900. digest->stale = FALSE; /* default means normal, not stale */
  901. digest->userhash = FALSE;
  902. }
  903. #endif /* !USE_WINDOWS_SSPI */
  904. #endif /* !CURL_DISABLE_DIGEST_AUTH */