digest.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #if !defined(CURL_DISABLE_CRYPTO_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 "vtls/vtls.h"
  38. #include "warnless.h"
  39. #include "strtok.h"
  40. #include "strcase.h"
  41. #include "curl_printf.h"
  42. #include "rand.h"
  43. /* The last #include files should be: */
  44. #include "curl_memory.h"
  45. #include "memdebug.h"
  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. #if !defined(USE_WINDOWS_SSPI)
  54. #define DIGEST_QOP_VALUE_AUTH (1 << 0)
  55. #define DIGEST_QOP_VALUE_AUTH_INT (1 << 1)
  56. #define DIGEST_QOP_VALUE_AUTH_CONF (1 << 2)
  57. #define DIGEST_QOP_VALUE_STRING_AUTH "auth"
  58. #define DIGEST_QOP_VALUE_STRING_AUTH_INT "auth-int"
  59. #define DIGEST_QOP_VALUE_STRING_AUTH_CONF "auth-conf"
  60. #endif
  61. bool Curl_auth_digest_get_pair(const char *str, char *value, char *content,
  62. const char **endptr)
  63. {
  64. int c;
  65. bool starts_with_quote = FALSE;
  66. bool escape = FALSE;
  67. for(c = DIGEST_MAX_VALUE_LENGTH - 1; (*str && (*str != '=') && c--);)
  68. *value++ = *str++;
  69. *value = 0;
  70. if('=' != *str++)
  71. /* eek, no match */
  72. return FALSE;
  73. if('\"' == *str) {
  74. /* This starts with a quote so it must end with one as well! */
  75. str++;
  76. starts_with_quote = TRUE;
  77. }
  78. for(c = DIGEST_MAX_CONTENT_LENGTH - 1; *str && c--; str++) {
  79. if(!escape) {
  80. switch(*str) {
  81. case '\\':
  82. if(starts_with_quote) {
  83. /* the start of an escaped quote */
  84. escape = TRUE;
  85. continue;
  86. }
  87. break;
  88. case ',':
  89. if(!starts_with_quote) {
  90. /* This signals the end of the content if we didn't get a starting
  91. quote and then we do "sloppy" parsing */
  92. c = 0; /* the end */
  93. continue;
  94. }
  95. break;
  96. case '\r':
  97. case '\n':
  98. /* end of string */
  99. if(starts_with_quote)
  100. return FALSE; /* No closing quote */
  101. c = 0;
  102. continue;
  103. case '\"':
  104. if(starts_with_quote) {
  105. /* end of string */
  106. c = 0;
  107. continue;
  108. }
  109. else
  110. return FALSE;
  111. break;
  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 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. strtok_r() ruins it. */
  197. tmp = strdup(options);
  198. if(!tmp)
  199. return CURLE_OUT_OF_MEMORY;
  200. token = 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 = 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. strcpy(realm, "");
  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 user name.
  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, realm, 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 strtok_r() ruins it */
  481. tmp = strdup(content);
  482. if(!tmp)
  483. return CURLE_OUT_OF_MEMORY;
  484. token = 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 = 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. digest->algo = ALGO_SHA512_256;
  527. else if(strcasecompare(content, "SHA-512-256-SESS"))
  528. digest->algo = ALGO_SHA512_256SESS;
  529. else
  530. return CURLE_BAD_CONTENT_ENCODING;
  531. }
  532. else if(strcasecompare(value, "userhash")) {
  533. if(strcasecompare(content, "true")) {
  534. digest->userhash = TRUE;
  535. }
  536. }
  537. else {
  538. /* Unknown specifier, ignore it! */
  539. }
  540. }
  541. else
  542. break; /* We're done here */
  543. /* Pass all additional spaces here */
  544. while(*chlg && ISBLANK(*chlg))
  545. chlg++;
  546. /* Allow the list to be comma-separated */
  547. if(',' == *chlg)
  548. chlg++;
  549. }
  550. /* We had a nonce since before, and we got another one now without
  551. 'stale=true'. This means we provided bad credentials in the previous
  552. request */
  553. if(before && !digest->stale)
  554. return CURLE_BAD_CONTENT_ENCODING;
  555. /* We got this header without a nonce, that's a bad Digest line! */
  556. if(!digest->nonce)
  557. return CURLE_BAD_CONTENT_ENCODING;
  558. /* "<algo>-sess" protocol versions require "auth" or "auth-int" qop */
  559. if(!digest->qop && (digest->algo & SESSION_ALGO))
  560. return CURLE_BAD_CONTENT_ENCODING;
  561. return CURLE_OK;
  562. }
  563. /*
  564. * auth_create_digest_http_message()
  565. *
  566. * This is used to generate an HTTP DIGEST response message ready for sending
  567. * to the recipient.
  568. *
  569. * Parameters:
  570. *
  571. * data [in] - The session handle.
  572. * userp [in] - The user name.
  573. * passwdp [in] - The user's password.
  574. * request [in] - The HTTP request.
  575. * uripath [in] - The path of the HTTP uri.
  576. * digest [in/out] - The digest data struct being used and modified.
  577. * outptr [in/out] - The address where a pointer to newly allocated memory
  578. * holding the result will be stored upon completion.
  579. * outlen [out] - The length of the output message.
  580. *
  581. * Returns CURLE_OK on success.
  582. */
  583. static CURLcode auth_create_digest_http_message(
  584. struct Curl_easy *data,
  585. const char *userp,
  586. const char *passwdp,
  587. const unsigned char *request,
  588. const unsigned char *uripath,
  589. struct digestdata *digest,
  590. char **outptr, size_t *outlen,
  591. void (*convert_to_ascii)(unsigned char *, unsigned char *),
  592. CURLcode (*hash)(unsigned char *, const unsigned char *,
  593. const size_t))
  594. {
  595. CURLcode result;
  596. unsigned char hashbuf[32]; /* 32 bytes/256 bits */
  597. unsigned char request_digest[65];
  598. unsigned char ha1[65]; /* 64 digits and 1 zero byte */
  599. unsigned char ha2[65]; /* 64 digits and 1 zero byte */
  600. char userh[65];
  601. char *cnonce = NULL;
  602. size_t cnonce_sz = 0;
  603. char *userp_quoted;
  604. char *realm_quoted;
  605. char *nonce_quoted;
  606. char *response = NULL;
  607. char *hashthis = NULL;
  608. char *tmp = NULL;
  609. memset(hashbuf, 0, sizeof(hashbuf));
  610. if(!digest->nc)
  611. digest->nc = 1;
  612. if(!digest->cnonce) {
  613. char cnoncebuf[33];
  614. result = Curl_rand_hex(data, (unsigned char *)cnoncebuf,
  615. sizeof(cnoncebuf));
  616. if(result)
  617. return result;
  618. result = Curl_base64_encode(cnoncebuf, strlen(cnoncebuf),
  619. &cnonce, &cnonce_sz);
  620. if(result)
  621. return result;
  622. digest->cnonce = cnonce;
  623. }
  624. if(digest->userhash) {
  625. hashthis = aprintf("%s:%s", userp, digest->realm ? digest->realm : "");
  626. if(!hashthis)
  627. return CURLE_OUT_OF_MEMORY;
  628. hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  629. free(hashthis);
  630. convert_to_ascii(hashbuf, (unsigned char *)userh);
  631. }
  632. /*
  633. If the algorithm is "MD5" or unspecified (which then defaults to MD5):
  634. A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  635. If the algorithm is "MD5-sess" then:
  636. A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":"
  637. unq(nonce-value) ":" unq(cnonce-value)
  638. */
  639. hashthis = aprintf("%s:%s:%s", userp, digest->realm ? digest->realm : "",
  640. passwdp);
  641. if(!hashthis)
  642. return CURLE_OUT_OF_MEMORY;
  643. hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  644. free(hashthis);
  645. convert_to_ascii(hashbuf, ha1);
  646. if(digest->algo & SESSION_ALGO) {
  647. /* nonce and cnonce are OUTSIDE the hash */
  648. tmp = aprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce);
  649. if(!tmp)
  650. return CURLE_OUT_OF_MEMORY;
  651. hash(hashbuf, (unsigned char *) tmp, strlen(tmp));
  652. free(tmp);
  653. convert_to_ascii(hashbuf, ha1);
  654. }
  655. /*
  656. If the "qop" directive's value is "auth" or is unspecified, then A2 is:
  657. A2 = Method ":" digest-uri-value
  658. If the "qop" value is "auth-int", then A2 is:
  659. A2 = Method ":" digest-uri-value ":" H(entity-body)
  660. (The "Method" value is the HTTP request method as specified in section
  661. 5.1.1 of RFC 2616)
  662. */
  663. hashthis = aprintf("%s:%s", request, uripath);
  664. if(!hashthis)
  665. return CURLE_OUT_OF_MEMORY;
  666. if(digest->qop && strcasecompare(digest->qop, "auth-int")) {
  667. /* We don't support auth-int for PUT or POST */
  668. char hashed[65];
  669. char *hashthis2;
  670. hash(hashbuf, (const unsigned char *)"", 0);
  671. convert_to_ascii(hashbuf, (unsigned char *)hashed);
  672. hashthis2 = aprintf("%s:%s", hashthis, hashed);
  673. free(hashthis);
  674. hashthis = hashthis2;
  675. }
  676. if(!hashthis)
  677. return CURLE_OUT_OF_MEMORY;
  678. hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  679. free(hashthis);
  680. convert_to_ascii(hashbuf, ha2);
  681. if(digest->qop) {
  682. hashthis = aprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce, digest->nc,
  683. digest->cnonce, digest->qop, ha2);
  684. }
  685. else {
  686. hashthis = aprintf("%s:%s:%s", ha1, digest->nonce, ha2);
  687. }
  688. if(!hashthis)
  689. return CURLE_OUT_OF_MEMORY;
  690. hash(hashbuf, (unsigned char *) hashthis, strlen(hashthis));
  691. free(hashthis);
  692. convert_to_ascii(hashbuf, request_digest);
  693. /* For test case 64 (snooped from a Mozilla 1.3a request)
  694. Authorization: Digest username="testuser", realm="testrealm", \
  695. nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
  696. Digest parameters are all quoted strings. Username which is provided by
  697. the user will need double quotes and backslashes within it escaped.
  698. realm, nonce, and opaque will need backslashes as well as they were
  699. de-escaped when copied from request header. cnonce is generated with
  700. web-safe characters. uri is already percent encoded. nc is 8 hex
  701. characters. algorithm and qop with standard values only contain web-safe
  702. characters.
  703. */
  704. userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp);
  705. if(!userp_quoted)
  706. return CURLE_OUT_OF_MEMORY;
  707. if(digest->realm)
  708. realm_quoted = auth_digest_string_quoted(digest->realm);
  709. else {
  710. realm_quoted = malloc(1);
  711. if(realm_quoted)
  712. realm_quoted[0] = 0;
  713. }
  714. if(!realm_quoted) {
  715. free(userp_quoted);
  716. return CURLE_OUT_OF_MEMORY;
  717. }
  718. nonce_quoted = auth_digest_string_quoted(digest->nonce);
  719. if(!nonce_quoted) {
  720. free(realm_quoted);
  721. free(userp_quoted);
  722. return CURLE_OUT_OF_MEMORY;
  723. }
  724. if(digest->qop) {
  725. response = aprintf("username=\"%s\", "
  726. "realm=\"%s\", "
  727. "nonce=\"%s\", "
  728. "uri=\"%s\", "
  729. "cnonce=\"%s\", "
  730. "nc=%08x, "
  731. "qop=%s, "
  732. "response=\"%s\"",
  733. userp_quoted,
  734. realm_quoted,
  735. nonce_quoted,
  736. uripath,
  737. digest->cnonce,
  738. digest->nc,
  739. digest->qop,
  740. request_digest);
  741. /* Increment nonce-count to use another nc value for the next request */
  742. digest->nc++;
  743. }
  744. else {
  745. response = aprintf("username=\"%s\", "
  746. "realm=\"%s\", "
  747. "nonce=\"%s\", "
  748. "uri=\"%s\", "
  749. "response=\"%s\"",
  750. userp_quoted,
  751. realm_quoted,
  752. nonce_quoted,
  753. uripath,
  754. request_digest);
  755. }
  756. free(nonce_quoted);
  757. free(realm_quoted);
  758. free(userp_quoted);
  759. if(!response)
  760. return CURLE_OUT_OF_MEMORY;
  761. /* Add the optional fields */
  762. if(digest->opaque) {
  763. char *opaque_quoted;
  764. /* Append the opaque */
  765. opaque_quoted = auth_digest_string_quoted(digest->opaque);
  766. if(!opaque_quoted) {
  767. free(response);
  768. return CURLE_OUT_OF_MEMORY;
  769. }
  770. tmp = aprintf("%s, opaque=\"%s\"", response, opaque_quoted);
  771. free(response);
  772. free(opaque_quoted);
  773. if(!tmp)
  774. return CURLE_OUT_OF_MEMORY;
  775. response = tmp;
  776. }
  777. if(digest->algorithm) {
  778. /* Append the algorithm */
  779. tmp = aprintf("%s, algorithm=%s", response, digest->algorithm);
  780. free(response);
  781. if(!tmp)
  782. return CURLE_OUT_OF_MEMORY;
  783. response = tmp;
  784. }
  785. if(digest->userhash) {
  786. /* Append the userhash */
  787. tmp = aprintf("%s, userhash=true", response);
  788. free(response);
  789. if(!tmp)
  790. return CURLE_OUT_OF_MEMORY;
  791. response = tmp;
  792. }
  793. /* Return the output */
  794. *outptr = response;
  795. *outlen = strlen(response);
  796. return CURLE_OK;
  797. }
  798. /*
  799. * Curl_auth_create_digest_http_message()
  800. *
  801. * This is used to generate an HTTP DIGEST response message ready for sending
  802. * to the recipient.
  803. *
  804. * Parameters:
  805. *
  806. * data [in] - The session handle.
  807. * userp [in] - The user name.
  808. * passwdp [in] - The user's password.
  809. * request [in] - The HTTP request.
  810. * uripath [in] - The path of the HTTP uri.
  811. * digest [in/out] - The digest data struct being used and modified.
  812. * outptr [in/out] - The address where a pointer to newly allocated memory
  813. * holding the result will be stored upon completion.
  814. * outlen [out] - The length of the output message.
  815. *
  816. * Returns CURLE_OK on success.
  817. */
  818. CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
  819. const char *userp,
  820. const char *passwdp,
  821. const unsigned char *request,
  822. const unsigned char *uripath,
  823. struct digestdata *digest,
  824. char **outptr, size_t *outlen)
  825. {
  826. if(digest->algo <= ALGO_MD5SESS)
  827. return auth_create_digest_http_message(data, userp, passwdp,
  828. request, uripath, digest,
  829. outptr, outlen,
  830. auth_digest_md5_to_ascii,
  831. Curl_md5it);
  832. DEBUGASSERT(digest->algo <= ALGO_SHA512_256SESS);
  833. return auth_create_digest_http_message(data, userp, passwdp,
  834. request, uripath, digest,
  835. outptr, outlen,
  836. auth_digest_sha256_to_ascii,
  837. Curl_sha256it);
  838. }
  839. /*
  840. * Curl_auth_digest_cleanup()
  841. *
  842. * This is used to clean up the digest specific data.
  843. *
  844. * Parameters:
  845. *
  846. * digest [in/out] - The digest data struct being cleaned up.
  847. *
  848. */
  849. void Curl_auth_digest_cleanup(struct digestdata *digest)
  850. {
  851. Curl_safefree(digest->nonce);
  852. Curl_safefree(digest->cnonce);
  853. Curl_safefree(digest->realm);
  854. Curl_safefree(digest->opaque);
  855. Curl_safefree(digest->qop);
  856. Curl_safefree(digest->algorithm);
  857. digest->nc = 0;
  858. digest->algo = ALGO_MD5; /* default algorithm */
  859. digest->stale = FALSE; /* default means normal, not stale */
  860. digest->userhash = FALSE;
  861. }
  862. #endif /* !USE_WINDOWS_SSPI */
  863. #endif /* CURL_DISABLE_CRYPTO_AUTH */