digest.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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[12];
  624. result = Curl_rand_bytes(data,
  625. #ifdef DEBUGBUILD
  626. TRUE,
  627. #endif
  628. (unsigned char *)cnoncebuf,
  629. sizeof(cnoncebuf));
  630. if(result)
  631. return result;
  632. result = Curl_base64_encode(cnoncebuf, sizeof(cnoncebuf),
  633. &cnonce, &cnonce_sz);
  634. if(result)
  635. return result;
  636. digest->cnonce = cnonce;
  637. }
  638. if(digest->userhash) {
  639. hashthis = aprintf("%s:%s", userp, digest->realm ? digest->realm : "");
  640. if(!hashthis)
  641. return CURLE_OUT_OF_MEMORY;
  642. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  643. free(hashthis);
  644. if(result)
  645. return result;
  646. convert_to_ascii(hashbuf, (unsigned char *)userh);
  647. }
  648. /*
  649. If the algorithm is "MD5" or unspecified (which then defaults to MD5):
  650. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  651. If the algorithm is "MD5-sess" then:
  652. A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":"
  653. unq(nonce-value) ":" unq(cnonce-value)
  654. */
  655. hashthis = aprintf("%s:%s:%s", userp, digest->realm ? digest->realm : "",
  656. passwdp);
  657. if(!hashthis)
  658. return CURLE_OUT_OF_MEMORY;
  659. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  660. free(hashthis);
  661. if(result)
  662. return result;
  663. convert_to_ascii(hashbuf, ha1);
  664. if(digest->algo & SESSION_ALGO) {
  665. /* nonce and cnonce are OUTSIDE the hash */
  666. tmp = aprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce);
  667. if(!tmp)
  668. return CURLE_OUT_OF_MEMORY;
  669. result = hash(hashbuf, (unsigned char *) tmp, strlen(tmp));
  670. free(tmp);
  671. if(result)
  672. return result;
  673. convert_to_ascii(hashbuf, ha1);
  674. }
  675. /*
  676. If the "qop" directive's value is "auth" or is unspecified, then A2 is:
  677. A2 = Method ":" digest-uri-value
  678. If the "qop" value is "auth-int", then A2 is:
  679. A2 = Method ":" digest-uri-value ":" H(entity-body)
  680. (The "Method" value is the HTTP request method as specified in section
  681. 5.1.1 of RFC 2616)
  682. */
  683. hashthis = aprintf("%s:%s", request, uripath);
  684. if(!hashthis)
  685. return CURLE_OUT_OF_MEMORY;
  686. if(digest->qop && strcasecompare(digest->qop, "auth-int")) {
  687. /* We do not support auth-int for PUT or POST */
  688. char hashed[65];
  689. char *hashthis2;
  690. result = hash(hashbuf, (const unsigned char *)"", 0);
  691. if(result) {
  692. free(hashthis);
  693. return result;
  694. }
  695. convert_to_ascii(hashbuf, (unsigned char *)hashed);
  696. hashthis2 = aprintf("%s:%s", hashthis, hashed);
  697. free(hashthis);
  698. hashthis = hashthis2;
  699. }
  700. if(!hashthis)
  701. return CURLE_OUT_OF_MEMORY;
  702. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  703. free(hashthis);
  704. if(result)
  705. return result;
  706. convert_to_ascii(hashbuf, ha2);
  707. if(digest->qop) {
  708. hashthis = aprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce, digest->nc,
  709. digest->cnonce, digest->qop, ha2);
  710. }
  711. else {
  712. hashthis = aprintf("%s:%s:%s", ha1, digest->nonce, ha2);
  713. }
  714. if(!hashthis)
  715. return CURLE_OUT_OF_MEMORY;
  716. result = hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  717. free(hashthis);
  718. if(result)
  719. return result;
  720. convert_to_ascii(hashbuf, request_digest);
  721. /* For test case 64 (snooped from a Mozilla 1.3a request)
  722. Authorization: Digest username="testuser", realm="testrealm", \
  723. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  724. Digest parameters are all quoted strings. Username which is provided by
  725. the user will need double quotes and backslashes within it escaped.
  726. realm, nonce, and opaque will need backslashes as well as they were
  727. de-escaped when copied from request header. cnonce is generated with
  728. web-safe characters. uri is already percent encoded. nc is 8 hex
  729. characters. algorithm and qop with standard values only contain web-safe
  730. characters.
  731. */
  732. userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp);
  733. if(!userp_quoted)
  734. return CURLE_OUT_OF_MEMORY;
  735. if(digest->realm)
  736. realm_quoted = auth_digest_string_quoted(digest->realm);
  737. else {
  738. realm_quoted = malloc(1);
  739. if(realm_quoted)
  740. realm_quoted[0] = 0;
  741. }
  742. if(!realm_quoted) {
  743. free(userp_quoted);
  744. return CURLE_OUT_OF_MEMORY;
  745. }
  746. nonce_quoted = auth_digest_string_quoted(digest->nonce);
  747. if(!nonce_quoted) {
  748. free(realm_quoted);
  749. free(userp_quoted);
  750. return CURLE_OUT_OF_MEMORY;
  751. }
  752. if(digest->qop) {
  753. response = aprintf("username=\"%s\", "
  754. "realm=\"%s\", "
  755. "nonce=\"%s\", "
  756. "uri=\"%s\", "
  757. "cnonce=\"%s\", "
  758. "nc=%08x, "
  759. "qop=%s, "
  760. "response=\"%s\"",
  761. userp_quoted,
  762. realm_quoted,
  763. nonce_quoted,
  764. uripath,
  765. digest->cnonce,
  766. digest->nc,
  767. digest->qop,
  768. request_digest);
  769. /* Increment nonce-count to use another nc value for the next request */
  770. digest->nc++;
  771. }
  772. else {
  773. response = aprintf("username=\"%s\", "
  774. "realm=\"%s\", "
  775. "nonce=\"%s\", "
  776. "uri=\"%s\", "
  777. "response=\"%s\"",
  778. userp_quoted,
  779. realm_quoted,
  780. nonce_quoted,
  781. uripath,
  782. request_digest);
  783. }
  784. free(nonce_quoted);
  785. free(realm_quoted);
  786. free(userp_quoted);
  787. if(!response)
  788. return CURLE_OUT_OF_MEMORY;
  789. /* Add the optional fields */
  790. if(digest->opaque) {
  791. char *opaque_quoted;
  792. /* Append the opaque */
  793. opaque_quoted = auth_digest_string_quoted(digest->opaque);
  794. if(!opaque_quoted) {
  795. free(response);
  796. return CURLE_OUT_OF_MEMORY;
  797. }
  798. tmp = aprintf("%s, opaque=\"%s\"", response, opaque_quoted);
  799. free(response);
  800. free(opaque_quoted);
  801. if(!tmp)
  802. return CURLE_OUT_OF_MEMORY;
  803. response = tmp;
  804. }
  805. if(digest->algorithm) {
  806. /* Append the algorithm */
  807. tmp = aprintf("%s, algorithm=%s", response, digest->algorithm);
  808. free(response);
  809. if(!tmp)
  810. return CURLE_OUT_OF_MEMORY;
  811. response = tmp;
  812. }
  813. if(digest->userhash) {
  814. /* Append the userhash */
  815. tmp = aprintf("%s, userhash=true", response);
  816. free(response);
  817. if(!tmp)
  818. return CURLE_OUT_OF_MEMORY;
  819. response = tmp;
  820. }
  821. /* Return the output */
  822. *outptr = response;
  823. *outlen = strlen(response);
  824. return CURLE_OK;
  825. }
  826. /*
  827. * Curl_auth_create_digest_http_message()
  828. *
  829. * This is used to generate an HTTP DIGEST response message ready for sending
  830. * to the recipient.
  831. *
  832. * Parameters:
  833. *
  834. * data [in] - The session handle.
  835. * userp [in] - The username.
  836. * passwdp [in] - The user's password.
  837. * request [in] - The HTTP request.
  838. * uripath [in] - The path of the HTTP uri.
  839. * digest [in/out] - The digest data struct being used and modified.
  840. * outptr [in/out] - The address where a pointer to newly allocated memory
  841. * holding the result will be stored upon completion.
  842. * outlen [out] - The length of the output message.
  843. *
  844. * Returns CURLE_OK on success.
  845. */
  846. CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
  847. const char *userp,
  848. const char *passwdp,
  849. const unsigned char *request,
  850. const unsigned char *uripath,
  851. struct digestdata *digest,
  852. char **outptr, size_t *outlen)
  853. {
  854. if(digest->algo <= ALGO_MD5SESS)
  855. return auth_create_digest_http_message(data, userp, passwdp,
  856. request, uripath, digest,
  857. outptr, outlen,
  858. auth_digest_md5_to_ascii,
  859. Curl_md5it);
  860. if(digest->algo <= ALGO_SHA256SESS)
  861. return auth_create_digest_http_message(data, userp, passwdp,
  862. request, uripath, digest,
  863. outptr, outlen,
  864. auth_digest_sha256_to_ascii,
  865. Curl_sha256it);
  866. #ifdef CURL_HAVE_SHA512_256
  867. if(digest->algo <= ALGO_SHA512_256SESS)
  868. return auth_create_digest_http_message(data, userp, passwdp,
  869. request, uripath, digest,
  870. outptr, outlen,
  871. auth_digest_sha256_to_ascii,
  872. Curl_sha512_256it);
  873. #endif /* CURL_HAVE_SHA512_256 */
  874. /* Should be unreachable */
  875. return CURLE_BAD_CONTENT_ENCODING;
  876. }
  877. /*
  878. * Curl_auth_digest_cleanup()
  879. *
  880. * This is used to clean up the digest specific data.
  881. *
  882. * Parameters:
  883. *
  884. * digest [in/out] - The digest data struct being cleaned up.
  885. *
  886. */
  887. void Curl_auth_digest_cleanup(struct digestdata *digest)
  888. {
  889. Curl_safefree(digest->nonce);
  890. Curl_safefree(digest->cnonce);
  891. Curl_safefree(digest->realm);
  892. Curl_safefree(digest->opaque);
  893. Curl_safefree(digest->qop);
  894. Curl_safefree(digest->algorithm);
  895. digest->nc = 0;
  896. digest->algo = ALGO_MD5; /* default algorithm */
  897. digest->stale = FALSE; /* default means normal, not stale */
  898. digest->userhash = FALSE;
  899. }
  900. #endif /* !USE_WINDOWS_SSPI */
  901. #endif /* !CURL_DISABLE_DIGEST_AUTH */