Coverage Report

Created: 2026-04-12 06:56

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