digest.c 31 KB

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