digest.c 31 KB

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