Artifactspublic-v0

curl

master
ply4 git calls57 ms295 KiB2 cachedprotocol v2
1061 lines33 KiBRaw
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"
29#ifndef CURL_DISABLE_DIGEST_AUTH
31#include "vauth/vauth.h"
32#include "vauth/digest.h"
33#include "curlx/base64.h"
34#include "curl_md5.h"
35#include "curl_sha256.h"
36#include "curl_sha512_256.h"
37#include "curlx/strparse.h"
38#include "rand.h"
39#include "escape.h"
41#ifndef USE_WINDOWS_SSPI
42#define SESSION_ALGO 1 /* for algos with this bit set */
44#define ALGO_MD5 0
45#define ALGO_MD5SESS (ALGO_MD5 | SESSION_ALGO)
46#define ALGO_SHA256 2
47#define ALGO_SHA256SESS (ALGO_SHA256 | SESSION_ALGO)
48#define ALGO_SHA512_256 4
49#define ALGO_SHA512_256SESS (ALGO_SHA512_256 | SESSION_ALGO)
51#define DIGEST_QOP_VALUE_AUTH (1 << 0)
52#define DIGEST_QOP_VALUE_AUTH_INT (1 << 1)
53#define DIGEST_QOP_VALUE_AUTH_CONF (1 << 2)
55#define DIGEST_QOP_VALUE_STRING_AUTH "auth"
56#define DIGEST_QOP_VALUE_STRING_AUTH_INT "auth-int"
57#define DIGEST_QOP_VALUE_STRING_AUTH_CONF "auth-conf"
58#endif
60bool Curl_auth_digest_get_pair(const char *str, struct Curl_str *value,
61 char *content, const char **endptr)
63 int c;
64 size_t content_length = 0;
65 bool starts_with_quote = FALSE;
66 bool escape = FALSE;
68 if(curlx_str_until(&str, value, DIGEST_MAX_VALUE_LENGTH, '=') ||
69 curlx_str_single(&str, '='))
70 /* eek, no match */
71 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 }
79 for(c = DIGEST_MAX_CONTENT_LENGTH; *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;
90 case ',':
91 if(!starts_with_quote) {
92 /* This signals the end of the content if we did not get a starting
93 quote and then we do "sloppy" parsing */
94 c = 0; /* the end */
95 continue;
96 }
97 break;
99 case '\r':
100 case '\n':
101 /* end of string */
102 if(starts_with_quote)
103 return FALSE; /* No closing quote */
104 c = 0;
105 continue;
107 case '\"':
108 if(starts_with_quote) {
109 /* end of string */
110 c = 0;
111 continue;
112 }
113 else
114 return FALSE;
115 }
116 }
118 if(content_length >= DIGEST_MAX_CONTENT_LENGTH - 1) {
119 /* no room for this byte plus the terminator */
120 return FALSE;
121 }
123 escape = FALSE;
124 *content++ = *str;
125 content_length++;
126 }
128 if(escape)
129 return FALSE; /* No character after backslash */
131 *content = 0;
132 *endptr = str;
134 return TRUE;
137#ifndef USE_WINDOWS_SSPI
138/* Convert MD5 chunk to RFC2617 (section 3.1.3) -suitable ASCII string */
139static void auth_digest_md5_to_ascii(
140 const unsigned char *source, /* 16 bytes */
141 unsigned char *dest) /* 33 bytes */
143 int i;
144 for(i = 0; i < 16; i++)
145 curl_msnprintf((char *)&dest[i * 2], 3, "%02x", source[i]);
148/* Convert sha256 or SHA-512/256 chunk to RFC7616 -suitable ASCII string */
149static void auth_digest_sha256_to_ascii(
150 const unsigned char *source, /* 32 bytes */
151 unsigned char *dest) /* 65 bytes */
153 int i;
154 for(i = 0; i < 32; i++)
155 curl_msnprintf((char *)&dest[i * 2], 3, "%02x", source[i]);
158/* Perform quoted-string escaping as described in RFC2616 and its errata */
159static char *auth_digest_string_quoted(const char *s)
161 struct dynbuf out;
162 curlx_dyn_init(&out, 2048);
163 if(!*s) /* for zero length input, make sure we return an empty string */
164 return curlx_strdup("");
165 while(*s) {
166 CURLcode result;
167 if(*s == '"' || *s == '\\') {
168 result = curlx_dyn_addn(&out, "\\", 1);
169 if(!result)
170 result = curlx_dyn_addn(&out, s, 1);
171 }
172 else if((*s < ' ') || (*s > 0x7e)) {
173 unsigned char buf[3] = { '%' };
174 Curl_hexbyte(&buf[1], (unsigned char)*s);
175 result = curlx_dyn_addn(&out, buf, 3);
176 }
177 else
178 result = curlx_dyn_addn(&out, s, 1);
179 if(result)
180 return NULL;
181 s++;
182 }
183 return curlx_dyn_ptr(&out);
186/* Retrieves the value for a corresponding key from the challenge string
187 * returns TRUE if the key could be found, FALSE if it does not exist
188 */
189static bool auth_digest_get_key_value(const char *chlg, const char *key,
190 char *buf, size_t buflen)
192 /* keyword=[value],keyword2=[value]
193 The values may or may not be quoted.
194 */
196 do {
197 struct Curl_str data;
198 struct Curl_str name;
200 curlx_str_passblanks(&chlg);
202 if(!curlx_str_until(&chlg, &name, 64, '=') &&
203 !curlx_str_single(&chlg, '=')) {
204 /* this is the key, get the value, possibly quoted */
205 int rc = curlx_str_quotedword(&chlg, &data, 256);
206 if(rc == STRE_BEGQUOTE)
207 /* try unquoted until comma */
208 rc = curlx_str_until(&chlg, &data, 256, ',');
209 if(rc)
210 return FALSE; /* weird */
212 if(curlx_str_cmp(&name, key)) {
213 /* if this is our key, return the value */
214 size_t len = curlx_strlen(&data);
215 const char *src = curlx_str(&data);
216 size_t i;
217 size_t outlen = 0;
219 if(len >= buflen)
220 /* does not fit */
221 return FALSE;
223 for(i = 0; i < len; i++) {
224 if(src[i] == '\\' && i + 1 < len) {
225 i++; /* skip backslash */
226 }
227 buf[outlen++] = src[i];
228 }
229 buf[outlen] = 0;
230 return TRUE;
231 }
232 if(curlx_str_single(&chlg, ','))
233 return FALSE;
234 }
235 else /* odd syntax */
236 break;
237 } while(1);
239 return FALSE;
242static void auth_digest_get_qop_values(const char *options, int *value)
244 struct Curl_str out;
245 /* Initialize the output */
246 *value = 0;
248 while(!curlx_str_until(&options, &out, 32, ',')) {
249 if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH))
250 *value |= DIGEST_QOP_VALUE_AUTH;
251 else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_INT))
252 *value |= DIGEST_QOP_VALUE_AUTH_INT;
253 else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_CONF))
254 *value |= DIGEST_QOP_VALUE_AUTH_CONF;
255 if(curlx_str_single(&options, ','))
256 break;
257 }
260/*
261 * auth_decode_digest_md5_message()
262 *
263 * This is used internally to decode an already encoded DIGEST-MD5 challenge
264 * message into the separate attributes.
265 *
266 * Parameters:
267 *
268 * chlgref [in] - The challenge message.
269 * nonce [in/out] - The buffer where the nonce is stored.
270 * nlen [in] - The length of the nonce buffer.
271 * realm [in/out] - The buffer where the realm is stored.
272 * rlen [in] - The length of the realm buffer.
273 * alg [in/out] - The buffer where the algorithm is stored.
274 * alen [in] - The length of the algorithm buffer.
275 * qop [in/out] - The buffer where the qop-options is stored.
276 * qlen [in] - The length of the qop buffer.
277 *
278 * Returns CURLE_OK on success.
279 */
280static CURLcode auth_decode_digest_md5_message(const struct bufref *chlgref,
281 char *nonce, size_t nlen,
282 char *realm, size_t rlen,
283 char *alg, size_t alen,
284 char *qop, size_t qlen)
286 const char *chlg = Curl_bufref_ptr(chlgref);
288 /* Ensure we have a valid challenge message */
289 if(!Curl_bufref_len(chlgref))
290 return CURLE_BAD_CONTENT_ENCODING;
292 /* Retrieve nonce string from the challenge */
293 if(!auth_digest_get_key_value(chlg, "nonce", nonce, nlen))
294 return CURLE_BAD_CONTENT_ENCODING;
296 /* Retrieve realm string from the challenge */
297 if(!auth_digest_get_key_value(chlg, "realm", realm, rlen)) {
298 /* Challenge does not have a realm, set empty string [RFC2831] page 6 */
299 *realm = '\0';
300 }
302 /* Retrieve algorithm string from the challenge */
303 if(!auth_digest_get_key_value(chlg, "algorithm", alg, alen))
304 return CURLE_BAD_CONTENT_ENCODING;
306 /* Retrieve qop-options string from the challenge */
307 if(!auth_digest_get_key_value(chlg, "qop", qop, qlen))
308 return CURLE_BAD_CONTENT_ENCODING;
310 return CURLE_OK;
313/*
314 * Curl_auth_is_digest_supported()
315 *
316 * This is used to evaluate if DIGEST is supported.
317 *
318 * Parameters: None
319 *
320 * Returns TRUE as DIGEST as handled by libcurl.
321 */
322bool Curl_auth_is_digest_supported(void)
324 return TRUE;
327/*
328 * Curl_auth_create_digest_md5_message()
329 *
330 * This is used to generate an already encoded DIGEST-MD5 response message
331 * ready for sending to the recipient.
332 *
333 * Parameters:
334 *
335 * data [in] - The session handle.
336 * chlg [in] - The challenge message.
337 * userp [in] - The username.
338 * passwdp [in] - The user's password.
339 * service [in] - The service type such as http, smtp, pop or imap.
340 * out [out] - The result storage.
341 *
342 * Returns CURLE_OK on success.
343 */
344CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
345 const struct bufref *chlg,
346 struct Curl_creds *creds,
347 const char *default_service,
348 struct bufref *out)
350 const char *service = Curl_creds_has_sasl_service(creds) ?
351 Curl_creds_sasl_service(creds) : default_service;
352 size_t i;
353 struct MD5_context *ctxt;
354 const char *userp = Curl_creds_user(creds);
355 const char *passwdp = Curl_creds_passwd(creds);
356 char *response = NULL;
357 unsigned char digest[MD5_DIGEST_LEN];
358 char HA1_hex[(2 * MD5_DIGEST_LEN) + 1];
359 char HA2_hex[(2 * MD5_DIGEST_LEN) + 1];
360 char resp_hash_hex[(2 * MD5_DIGEST_LEN) + 1];
361 char nonce[64];
362 char realm[128];
363 char algorithm[64];
364 char qop_options[64];
365 int qop_values;
366 char cnonce[33];
367 char nonceCount[] = "00000001";
368 char method[] = "AUTHENTICATE";
369 char qop[] = DIGEST_QOP_VALUE_STRING_AUTH;
370 char *spn = NULL;
371 char *qrealm;
372 char *qnonce;
373 char *quserp;
374 char *qspn;
376 /* Decode the challenge message */
377 CURLcode result = auth_decode_digest_md5_message(chlg,
378 nonce, sizeof(nonce),
379 realm, sizeof(realm),
380 algorithm,
381 sizeof(algorithm),
382 qop_options,
383 sizeof(qop_options));
384 if(result)
385 return result;
387 /* We only support md5 sessions */
388 if(strcmp(algorithm, "md5-sess"))
389 return CURLE_BAD_CONTENT_ENCODING;
391 /* Get the qop-values from the qop-options */
392 auth_digest_get_qop_values(qop_options, &qop_values);
394 /* We only support auth quality-of-protection */
395 if(!(qop_values & DIGEST_QOP_VALUE_AUTH))
396 return CURLE_BAD_CONTENT_ENCODING;
398 /* Generate 32 random hex chars, 32 bytes + 1 null-termination */
399 result = Curl_rand_hex(data, (unsigned char *)cnonce, sizeof(cnonce));
400 if(result)
401 return result;
403 /* Good so far, now calculate A1 and H(A1) according to RFC 2831 */
404 ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
405 if(!ctxt)
406 return CURLE_OUT_OF_MEMORY;
408 Curl_MD5_update(ctxt, (const unsigned char *)userp,
409 curlx_uztoui(strlen(userp)));
410 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
411 Curl_MD5_update(ctxt, (const unsigned char *)realm,
412 curlx_uztoui(strlen(realm)));
413 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
414 Curl_MD5_update(ctxt, (const unsigned char *)passwdp,
415 curlx_uztoui(strlen(passwdp)));
416 Curl_MD5_final(ctxt, digest);
418 ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
419 if(!ctxt)
420 return CURLE_OUT_OF_MEMORY;
422 Curl_MD5_update(ctxt, (const unsigned char *)digest, MD5_DIGEST_LEN);
423 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
424 Curl_MD5_update(ctxt, (const unsigned char *)nonce,
425 curlx_uztoui(strlen(nonce)));
426 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
427 Curl_MD5_update(ctxt, (const unsigned char *)cnonce,
428 curlx_uztoui(strlen(cnonce)));
429 Curl_MD5_final(ctxt, digest);
431 /* Convert calculated 16 octet hex into 32 bytes string */
432 for(i = 0; i < MD5_DIGEST_LEN; i++)
433 curl_msnprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]);
435 /* Generate our SPN */
436 spn = Curl_auth_build_spn(service, data->state.origin->hostname, NULL);
437 if(!spn)
438 return CURLE_OUT_OF_MEMORY;
440 /* Calculate H(A2) */
441 ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
442 if(!ctxt) {
443 curlx_free(spn);
445 return CURLE_OUT_OF_MEMORY;
446 }
448 Curl_MD5_update(ctxt, (const unsigned char *)method,
449 curlx_uztoui(strlen(method)));
450 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
451 Curl_MD5_update(ctxt, (const unsigned char *)spn,
452 curlx_uztoui(strlen(spn)));
453 Curl_MD5_final(ctxt, digest);
455 for(i = 0; i < MD5_DIGEST_LEN; i++)
456 curl_msnprintf(&HA2_hex[2 * i], 3, "%02x", digest[i]);
458 /* Now calculate the response hash */
459 ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
460 if(!ctxt) {
461 curlx_free(spn);
463 return CURLE_OUT_OF_MEMORY;
464 }
466 Curl_MD5_update(ctxt, (const unsigned char *)HA1_hex, 2 * MD5_DIGEST_LEN);
467 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
468 Curl_MD5_update(ctxt, (const unsigned char *)nonce,
469 curlx_uztoui(strlen(nonce)));
470 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
472 Curl_MD5_update(ctxt, (const unsigned char *)nonceCount,
473 curlx_uztoui(strlen(nonceCount)));
474 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
475 Curl_MD5_update(ctxt, (const unsigned char *)cnonce,
476 curlx_uztoui(strlen(cnonce)));
477 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
478 Curl_MD5_update(ctxt, (const unsigned char *)qop,
479 curlx_uztoui(strlen(qop)));
480 Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
482 Curl_MD5_update(ctxt, (const unsigned char *)HA2_hex, 2 * MD5_DIGEST_LEN);
483 Curl_MD5_final(ctxt, digest);
485 for(i = 0; i < MD5_DIGEST_LEN; i++)
486 curl_msnprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]);
488 /* escape double quotes and backslashes in the username, realm, nonce and
489 spn as necessary */
490 qrealm = auth_digest_string_quoted(realm);
491 qnonce = auth_digest_string_quoted(nonce);
492 quserp = auth_digest_string_quoted(userp);
493 qspn = auth_digest_string_quoted(spn);
494 if(qrealm && qnonce && quserp && qspn)
495 /* Generate the response */
496 response = curl_maprintf("username=\"%s\",realm=\"%s\",nonce=\"%s\","
497 "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\","
498 "response=%s,qop=%s",
499 quserp, qrealm, qnonce,
500 cnonce, nonceCount, qspn, resp_hash_hex, qop);
502 curlx_free(qrealm);
503 curlx_free(qnonce);
504 curlx_free(quserp);
505 curlx_free(spn);
506 curlx_free(qspn);
507 if(!response)
508 return CURLE_OUT_OF_MEMORY;
510 /* Return the response. */
511 Curl_bufref_set(out, response, strlen(response), curl_free);
512 return result;
515/*
516 * Curl_auth_decode_digest_http_message()
517 *
518 * This is used to decode an HTTP DIGEST challenge message into the separate
519 * attributes.
520 *
521 * Parameters:
522 *
523 * chlg [in] - The challenge message.
524 * digest [in/out] - The digest data struct being used and modified.
525 *
526 * Returns CURLE_OK on success.
527 */
528CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
529 struct digestdata *digest)
531 bool before = FALSE; /* got a nonce before */
533 /* If we already have received a nonce, keep that in mind */
534 if(digest->nonce)
535 before = TRUE;
537 /* Clean up any former leftovers and initialize to defaults */
538 Curl_auth_digest_cleanup(digest);
540 for(;;) {
541 struct Curl_str value;
542 char content[DIGEST_MAX_CONTENT_LENGTH];
544 /* Pass all additional spaces here */
545 curlx_str_passblanks(&chlg);
547 /* Extract a value=content pair */
548 if(Curl_auth_digest_get_pair(chlg, &value, content, &chlg)) {
549 if(curlx_str_casecompare(&value, "nonce")) {
550 curlx_free(digest->nonce);
551 digest->nonce = curlx_strdup(content);
552 if(!digest->nonce)
553 return CURLE_OUT_OF_MEMORY;
554 }
555 else if(curlx_str_casecompare(&value, "stale")) {
556 if(curl_strequal(content, "true")) {
557 digest->stale = TRUE;
558 digest->nc = 1; /* we make a new nonce now */
559 }
560 }
561 else if(curlx_str_casecompare(&value, "realm")) {
562 curlx_free(digest->realm);
563 digest->realm = curlx_strdup(content);
564 if(!digest->realm)
565 return CURLE_OUT_OF_MEMORY;
566 }
567 else if(curlx_str_casecompare(&value, "opaque")) {
568 curlx_free(digest->opaque);
569 digest->opaque = curlx_strdup(content);
570 if(!digest->opaque)
571 return CURLE_OUT_OF_MEMORY;
572 }
573 else if(curlx_str_casecompare(&value, "qop")) {
574 const char *token = content;
575 struct Curl_str out;
576 bool foundAuth = FALSE;
577 bool foundAuthInt = FALSE;
578 /* Pass leading spaces */
579 curlx_str_passblanks(&token);
581 while(!curlx_str_until(&token, &out, 32, ',')) {
582 if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH))
583 foundAuth = TRUE;
584 else if(curlx_str_casecompare(&out,
585 DIGEST_QOP_VALUE_STRING_AUTH_INT))
586 foundAuthInt = TRUE;
587 if(curlx_str_single(&token, ','))
588 break;
589 curlx_str_passblanks(&token);
590 }
592 /* Select only auth or auth-int. Otherwise, ignore */
593 if(foundAuth) {
594 curlx_free(digest->qop);
595 digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH);
596 if(!digest->qop)
597 return CURLE_OUT_OF_MEMORY;
598 }
599 else if(foundAuthInt) {
600 curlx_free(digest->qop);
601 digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH_INT);
602 if(!digest->qop)
603 return CURLE_OUT_OF_MEMORY;
604 }
605 }
606 else if(curlx_str_casecompare(&value, "algorithm")) {
607 curlx_free(digest->algorithm);
608 digest->algorithm = curlx_strdup(content);
609 if(!digest->algorithm)
610 return CURLE_OUT_OF_MEMORY;
612 if(curl_strequal(content, "MD5-sess"))
613 digest->algo = ALGO_MD5SESS;
614 else if(curl_strequal(content, "MD5"))
615 digest->algo = ALGO_MD5;
616 else if(curl_strequal(content, "SHA-256"))
617 digest->algo = ALGO_SHA256;
618 else if(curl_strequal(content, "SHA-256-SESS"))
619 digest->algo = ALGO_SHA256SESS;
620 else if(curl_strequal(content, "SHA-512-256")) {
621#ifdef CURL_HAVE_SHA512_256
622 digest->algo = ALGO_SHA512_256;
623#else /* !CURL_HAVE_SHA512_256 */
624 return CURLE_NOT_BUILT_IN;
625#endif /* CURL_HAVE_SHA512_256 */
626 }
627 else if(curl_strequal(content, "SHA-512-256-SESS")) {
628#ifdef CURL_HAVE_SHA512_256
629 digest->algo = ALGO_SHA512_256SESS;
630#else /* !CURL_HAVE_SHA512_256 */
631 return CURLE_NOT_BUILT_IN;
632#endif /* CURL_HAVE_SHA512_256 */
633 }
634 else
635 return CURLE_BAD_CONTENT_ENCODING;
636 }
637 else if(curlx_str_casecompare(&value, "userhash")) {
638 if(curl_strequal(content, "true")) {
639 digest->userhash = TRUE;
640 }
641 }
642 else {
643 /* Unknown specifier, ignore it! */
644 }
645 }
646 else
647 break; /* We are done here */
649 /* Pass all additional spaces here */
650 curlx_str_passblanks(&chlg);
652 /* Allow the list to be comma-separated */
653 (void)curlx_str_single(&chlg, ',');
654 }
656 /* We had a nonce since before, and we got another one now without
657 'stale=true'. This means we provided bad credentials in the previous
658 request */
659 if(before && !digest->stale)
660 return CURLE_BAD_CONTENT_ENCODING;
662 /* We got this header without a nonce, that is a bad Digest line! */
663 if(!digest->nonce)
664 return CURLE_BAD_CONTENT_ENCODING;
666 /* "<algo>-sess" protocol versions require "auth" or "auth-int" qop */
667 if(!digest->qop && (digest->algo & SESSION_ALGO))
668 return CURLE_BAD_CONTENT_ENCODING;
670 return CURLE_OK;
673/*
674 * auth_create_digest_http_message()
675 *
676 * This is used to generate an HTTP DIGEST response message ready for sending
677 * to the recipient.
678 *
679 * Parameters:
680 *
681 * data [in] - The session handle.
682 * creds [in] - The credentials
683 * request [in] - The HTTP request.
684 * uripath [in] - The path of the HTTP uri.
685 * digest [in/out] - The digest data struct being used and modified.
686 * outptr [in/out] - The address where a pointer to newly allocated memory
687 * holding the result is stored upon completion.
688 * outlen [out] - The length of the output message.
689 *
690 * Returns CURLE_OK on success.
691 */
692static CURLcode auth_create_digest_http_message(
693 struct Curl_easy *data,
694 struct Curl_creds *creds,
695 const unsigned char *request,
696 const unsigned char *uripath,
697 struct digestdata *digest,
698 char **outptr, size_t *outlen,
699 void (*convert_to_ascii)(const unsigned char *, unsigned char *),
700 CURLcode (*hash)(unsigned char *, const unsigned char *, const size_t))
702 CURLcode result;
703 const char *userp = Curl_creds_user(creds);
704 const char *passwdp = Curl_creds_passwd(creds);
705 unsigned char hashbuf[32]; /* 32 bytes/256 bits */
706 unsigned char request_digest[65];
707 unsigned char ha1[65]; /* 64 digits and 1 zero byte */
708 unsigned char ha2[65]; /* 64 digits and 1 zero byte */
709 char userh[65];
710 char *cnonce = NULL;
711 size_t cnonce_sz = 0;
712 char *userp_quoted = NULL;
713 char *realm_quoted = NULL;
714 char *nonce_quoted = NULL;
715 char *hashthis = NULL;
716 char *uri_quoted = NULL;
717 struct dynbuf response;
718 *outptr = NULL;
720 curlx_dyn_init(&response, 4096); /* arbitrary max */
722 memset(hashbuf, 0, sizeof(hashbuf));
723 if(!digest->nc)
724 digest->nc = 1;
726 if(!digest->cnonce) {
727 char cnoncebuf[12];
728 result = Curl_rand_bytes(data,
729#ifdef DEBUGBUILD
730 TRUE,
731#endif
732 (unsigned char *)cnoncebuf,
733 sizeof(cnoncebuf));
734 if(!result)
735 result = curlx_base64_encode((uint8_t *)cnoncebuf, sizeof(cnoncebuf),
736 &cnonce, &cnonce_sz);
737 if(result)
738 goto oom;
740 digest->cnonce = cnonce;
741 }
743 if(digest->userhash) {
744 char *hasht = curl_maprintf("%s:%s", userp,
745 digest->realm ? digest->realm : "");
746 if(!hasht) {
747 result = CURLE_OUT_OF_MEMORY;
748 goto oom;
749 }
751 result = hash(hashbuf, (unsigned char *)hasht, strlen(hasht));
752 curlx_free(hasht);
753 if(result)
754 goto oom;
755 convert_to_ascii(hashbuf, (unsigned char *)userh);
756 }
758 /* If the algorithm is "MD5" or unspecified (which then defaults to MD5):
760 A1 = unq(username-value) ":" unq(realm-value) ":" passwd
762 If the algorithm is "MD5-sess" then:
764 A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":"
765 unq(nonce-value) ":" unq(cnonce-value)
766 */
768 hashthis = curl_maprintf("%s:%s:%s", userp, digest->realm ?
769 digest->realm : "", passwdp);
770 if(!hashthis) {
771 result = CURLE_OUT_OF_MEMORY;
772 goto oom;
773 }
775 result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
776 curlx_free(hashthis);
777 if(result)
778 goto oom;
779 convert_to_ascii(hashbuf, ha1);
781 if(digest->algo & SESSION_ALGO) {
782 /* nonce and cnonce are OUTSIDE the hash */
783 char *tmp = curl_maprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce);
784 if(!tmp) {
785 result = CURLE_OUT_OF_MEMORY;
786 goto oom;
787 }
789 result = hash(hashbuf, (unsigned char *)tmp, strlen(tmp));
790 curlx_free(tmp);
791 if(result)
792 goto oom;
793 convert_to_ascii(hashbuf, ha1);
794 }
796 /* If the "qop" directive's value is "auth" or is unspecified, then A2 is:
798 A2 = Method ":" digest-uri-value
800 If the "qop" value is "auth-int", then A2 is:
802 A2 = Method ":" digest-uri-value ":" H(entity-body)
804 (The "Method" value is the HTTP request method as specified in section
805 5.1.1 of RFC 2616)
806 */
808 uri_quoted = auth_digest_string_quoted((const char *)uripath);
809 if(!uri_quoted) {
810 result = CURLE_OUT_OF_MEMORY;
811 goto oom;
812 }
814 hashthis = curl_maprintf("%s:%s", request, uripath);
815 if(!hashthis) {
816 result = CURLE_OUT_OF_MEMORY;
817 goto oom;
818 }
820 if(digest->qop && curl_strequal(digest->qop, "auth-int")) {
821 /* We do not support auth-int for PUT or POST */
822 char hashed[65];
823 char *hashthis2;
825 result = hash(hashbuf, (const unsigned char *)"", 0);
826 if(result) {
827 curlx_free(hashthis);
828 goto oom;
829 }
830 convert_to_ascii(hashbuf, (unsigned char *)hashed);
832 hashthis2 = curl_maprintf("%s:%s", hashthis, hashed);
833 curlx_free(hashthis);
834 hashthis = hashthis2;
835 if(!hashthis) {
836 result = CURLE_OUT_OF_MEMORY;
837 goto oom;
838 }
839 }
841 result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
842 curlx_free(hashthis);
843 if(result)
844 goto oom;
845 convert_to_ascii(hashbuf, ha2);
847 if(digest->qop)
848 hashthis = curl_maprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce,
849 (unsigned int)digest->nc, digest->cnonce,
850 digest->qop, ha2);
851 else
852 hashthis = curl_maprintf("%s:%s:%s", ha1, digest->nonce, ha2);
854 if(!hashthis) {
855 result = CURLE_OUT_OF_MEMORY;
856 goto oom;
857 }
859 result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
860 curlx_free(hashthis);
861 if(result)
862 goto oom;
863 convert_to_ascii(hashbuf, request_digest);
865 /* For test case 64 (snooped from a Mozilla 1.3a request)
867 Authorization: Digest username="testuser", realm="testrealm", \
868 nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
870 Digest parameters are all quoted strings. Username which is provided by
871 the user needs double quotes and backslashes within it escaped.
872 realm, nonce, and opaque needs backslashes as well as they were
873 de-escaped when copied from request header. cnonce is generated with
874 web-safe characters. uri is already percent encoded. nc is 8 hex
875 characters. algorithm and qop with standard values only contain web-safe
876 characters.
877 */
878 userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp);
879 if(!userp_quoted) {
880 result = CURLE_OUT_OF_MEMORY;
881 goto oom;
882 }
883 if(digest->realm)
884 realm_quoted = auth_digest_string_quoted(digest->realm);
885 else {
886 realm_quoted = curlx_malloc(1);
887 if(realm_quoted)
888 realm_quoted[0] = 0;
889 }
890 if(!realm_quoted) {
891 result = CURLE_OUT_OF_MEMORY;
892 goto oom;
893 }
895 nonce_quoted = auth_digest_string_quoted(digest->nonce);
896 if(!nonce_quoted) {
897 result = CURLE_OUT_OF_MEMORY;
898 goto oom;
899 }
901 if(digest->qop) {
902 result = curlx_dyn_addf(&response, "username=\"%s\", "
903 "realm=\"%s\", "
904 "nonce=\"%s\", "
905 "uri=\"%s\", "
906 "cnonce=\"%s\", "
907 "nc=%08x, "
908 "qop=%s, "
909 "response=\"%s\"",
910 userp_quoted,
911 realm_quoted,
912 nonce_quoted,
913 uri_quoted,
914 digest->cnonce,
915 (unsigned int)digest->nc,
916 digest->qop,
917 request_digest);
919 /* Increment nonce-count to use another nc value for the next request */
920 digest->nc++;
921 }
922 else {
923 result = curlx_dyn_addf(&response, "username=\"%s\", "
924 "realm=\"%s\", "
925 "nonce=\"%s\", "
926 "uri=\"%s\", "
927 "response=\"%s\"",
928 userp_quoted,
929 realm_quoted,
930 nonce_quoted,
931 uri_quoted,
932 request_digest);
933 }
934 if(result)
935 goto oom;
937 /* Add the optional fields */
938 if(digest->opaque) {
939 /* Append the opaque */
940 char *opaque_quoted = auth_digest_string_quoted(digest->opaque);
941 if(!opaque_quoted) {
942 result = CURLE_OUT_OF_MEMORY;
943 goto oom;
944 }
945 result = curlx_dyn_addf(&response, ", opaque=\"%s\"", opaque_quoted);
946 curlx_free(opaque_quoted);
947 if(result)
948 goto oom;
949 }
951 if(digest->algorithm) {
952 /* Append the algorithm */
953 result = curlx_dyn_addf(&response, ", algorithm=%s", digest->algorithm);
954 if(result)
955 goto oom;
956 }
958 if(digest->userhash) {
959 /* Append the userhash */
960 result = curlx_dyn_add(&response, ", userhash=true");
961 if(result)
962 goto oom;
963 }
965 /* Return the output */
966 *outptr = curlx_dyn_ptr(&response);
967 *outlen = curlx_dyn_len(&response);
968 result = CURLE_OK;
970oom:
971 curlx_free(nonce_quoted);
972 curlx_free(realm_quoted);
973 curlx_free(uri_quoted);
974 curlx_free(userp_quoted);
975 if(result)
976 curlx_dyn_free(&response);
977 return result;
980/*
981 * Curl_auth_create_digest_http_message()
982 *
983 * This is used to generate an HTTP DIGEST response message ready for sending
984 * to the recipient.
985 *
986 * Parameters:
987 *
988 * data [in] - The session handle.
989 * userp [in] - The username.
990 * passwdp [in] - The user's password.
991 * request [in] - The HTTP request.
992 * uripath [in] - The path of the HTTP uri.
993 * digest [in/out] - The digest data struct being used and modified.
994 * outptr [in/out] - The address where a pointer to newly allocated memory
995 * holding the result is stored upon completion.
996 * outlen [out] - The length of the output message.
997 *
998 * Returns CURLE_OK on success.
999 */
1000CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
1001 struct Curl_creds *creds,
1002 const unsigned char *request,
1003 const unsigned char *uripath,
1004 struct digestdata *digest,
1005 char **outptr, size_t *outlen)
1007 if(digest->algo <= ALGO_MD5SESS)
1008 return auth_create_digest_http_message(data, creds,
1009 request, uripath, digest,
1010 outptr, outlen,
1011 auth_digest_md5_to_ascii,
1012 Curl_md5it);
1014 if(digest->algo <= ALGO_SHA256SESS)
1015 return auth_create_digest_http_message(data, creds,
1016 request, uripath, digest,
1017 outptr, outlen,
1018 auth_digest_sha256_to_ascii,
1019 Curl_sha256it);
1020#ifdef CURL_HAVE_SHA512_256
1021 if(digest->algo <= ALGO_SHA512_256SESS)
1022 return auth_create_digest_http_message(data, creds,
1023 request, uripath, digest,
1024 outptr, outlen,
1025 auth_digest_sha256_to_ascii,
1026 Curl_sha512_256it);
1027#endif /* CURL_HAVE_SHA512_256 */
1029 /* Should be unreachable */
1030 return CURLE_BAD_CONTENT_ENCODING;
1034 * Curl_auth_digest_cleanup()
1036 * This is used to clean up the digest specific data.
1038 * Parameters:
1040 * digest [in/out] - The digest data struct being cleaned up.
1042 */
1043void Curl_auth_digest_cleanup(struct digestdata *digest)
1045 Curl_peer_unlink(&digest->origin);
1046 Curl_creds_unlink(&digest->creds);
1047 curlx_safefree(digest->nonce);
1048 curlx_safefree(digest->cnonce);
1049 curlx_safefree(digest->realm);
1050 curlx_safefree(digest->opaque);
1051 curlx_safefree(digest->qop);
1052 curlx_safefree(digest->algorithm);
1054 digest->nc = 0;
1055 digest->algo = ALGO_MD5; /* default algorithm */
1056 digest->stale = FALSE; /* default means normal, not stale */
1057 digest->userhash = FALSE;
1059#endif /* !USE_WINDOWS_SSPI */
1061#endif /* !CURL_DISABLE_DIGEST_AUTH */
Diagnostics4 git calls · 57 ms
CallStatusSizeTime057 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B6 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2008.5 KiB · hit47 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=47
git.response.bytes=8666
git.response.cache=HIT
git.response.duration_ms=47
git.fetch.wants=3642dc714eb5db7d38162a402d5b0a1bca713693
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=8666
git.pkt.payload.max=8645
git.sideband.band1.bytes=8644
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5eafa3aae9eb1bb
Request
384710d1…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format