Coverage Report

Created: 2026-01-17 06:34

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