Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/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
0
#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 will be stored.
259
 * nlen    [in]     - The length of the nonce buffer.
260
 * realm   [in/out] - The buffer where the realm will be stored.
261
 * rlen    [in]     - The length of the realm buffer.
262
 * alg     [in/out] - The buffer where the algorithm will be stored.
263
 * alen    [in]     - The length of the algorithm buffer.
264
 * qop     [in/out] - The buffer where the qop-options will be 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
360
  /* Decode the challenge message */
361
0
  CURLcode result = auth_decode_digest_md5_message(chlg,
362
0
                                                   nonce, sizeof(nonce),
363
0
                                                   realm, sizeof(realm),
364
0
                                                   algorithm,
365
0
                                                   sizeof(algorithm),
366
0
                                                   qop_options,
367
0
                                                   sizeof(qop_options));
368
0
  if(result)
369
0
    return result;
370
371
  /* We only support md5 sessions */
372
0
  if(strcmp(algorithm, "md5-sess") != 0)
373
0
    return CURLE_BAD_CONTENT_ENCODING;
374
375
  /* Get the qop-values from the qop-options */
376
0
  auth_digest_get_qop_values(qop_options, &qop_values);
377
378
  /* We only support auth quality-of-protection */
379
0
  if(!(qop_values & DIGEST_QOP_VALUE_AUTH))
380
0
    return CURLE_BAD_CONTENT_ENCODING;
381
382
  /* Generate 32 random hex chars, 32 bytes + 1 null-termination */
383
0
  result = Curl_rand_hex(data, (unsigned char *)cnonce, sizeof(cnonce));
384
0
  if(result)
385
0
    return result;
386
387
  /* So far so good, now calculate A1 and H(A1) according to RFC 2831 */
388
0
  ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
389
0
  if(!ctxt)
390
0
    return CURLE_OUT_OF_MEMORY;
391
392
0
  Curl_MD5_update(ctxt, (const unsigned char *)userp,
393
0
                  curlx_uztoui(strlen(userp)));
394
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
395
0
  Curl_MD5_update(ctxt, (const unsigned char *)realm,
396
0
                  curlx_uztoui(strlen(realm)));
397
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
398
0
  Curl_MD5_update(ctxt, (const unsigned char *)passwdp,
399
0
                  curlx_uztoui(strlen(passwdp)));
400
0
  Curl_MD5_final(ctxt, digest);
401
402
0
  ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
403
0
  if(!ctxt)
404
0
    return CURLE_OUT_OF_MEMORY;
405
406
0
  Curl_MD5_update(ctxt, (const unsigned char *)digest, MD5_DIGEST_LEN);
407
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
408
0
  Curl_MD5_update(ctxt, (const unsigned char *)nonce,
409
0
                  curlx_uztoui(strlen(nonce)));
410
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
411
0
  Curl_MD5_update(ctxt, (const unsigned char *)cnonce,
412
0
                  curlx_uztoui(strlen(cnonce)));
413
0
  Curl_MD5_final(ctxt, digest);
414
415
  /* Convert calculated 16 octet hex into 32 bytes string */
416
0
  for(i = 0; i < MD5_DIGEST_LEN; i++)
417
0
    curl_msnprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]);
418
419
  /* Generate our SPN */
420
0
  spn = Curl_auth_build_spn(service, data->conn->host.name, NULL);
421
0
  if(!spn)
422
0
    return CURLE_OUT_OF_MEMORY;
423
424
  /* Calculate H(A2) */
425
0
  ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
426
0
  if(!ctxt) {
427
0
    curlx_free(spn);
428
429
0
    return CURLE_OUT_OF_MEMORY;
430
0
  }
431
432
0
  Curl_MD5_update(ctxt, (const unsigned char *)method,
433
0
                  curlx_uztoui(strlen(method)));
434
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
435
0
  Curl_MD5_update(ctxt, (const unsigned char *)spn,
436
0
                  curlx_uztoui(strlen(spn)));
437
0
  Curl_MD5_final(ctxt, digest);
438
439
0
  for(i = 0; i < MD5_DIGEST_LEN; i++)
440
0
    curl_msnprintf(&HA2_hex[2 * i], 3, "%02x", digest[i]);
441
442
  /* Now calculate the response hash */
443
0
  ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
444
0
  if(!ctxt) {
445
0
    curlx_free(spn);
446
447
0
    return CURLE_OUT_OF_MEMORY;
448
0
  }
449
450
0
  Curl_MD5_update(ctxt, (const unsigned char *)HA1_hex, 2 * MD5_DIGEST_LEN);
451
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
452
0
  Curl_MD5_update(ctxt, (const unsigned char *)nonce,
453
0
                  curlx_uztoui(strlen(nonce)));
454
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
455
456
0
  Curl_MD5_update(ctxt, (const unsigned char *)nonceCount,
457
0
                  curlx_uztoui(strlen(nonceCount)));
458
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
459
0
  Curl_MD5_update(ctxt, (const unsigned char *)cnonce,
460
0
                  curlx_uztoui(strlen(cnonce)));
461
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
462
0
  Curl_MD5_update(ctxt, (const unsigned char *)qop,
463
0
                  curlx_uztoui(strlen(qop)));
464
0
  Curl_MD5_update(ctxt, (const unsigned char *)":", 1);
465
466
0
  Curl_MD5_update(ctxt, (const unsigned char *)HA2_hex, 2 * MD5_DIGEST_LEN);
467
0
  Curl_MD5_final(ctxt, digest);
468
469
0
  for(i = 0; i < MD5_DIGEST_LEN; i++)
470
0
    curl_msnprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]);
471
472
  /* escape double quotes and backslashes in the realm and nonce as
473
     necessary */
474
0
  qrealm = auth_digest_string_quoted(realm);
475
0
  qnonce = auth_digest_string_quoted(nonce);
476
0
  if(qrealm && qnonce)
477
    /* Generate the response */
478
0
    response = curl_maprintf("username=\"%s\",realm=\"%s\",nonce=\"%s\","
479
0
                             "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\","
480
0
                             "response=%s,qop=%s",
481
0
                             userp, qrealm, qnonce,
482
0
                             cnonce, nonceCount, spn, resp_hash_hex, qop);
483
484
0
  curlx_free(qrealm);
485
0
  curlx_free(qnonce);
486
0
  curlx_free(spn);
487
0
  if(!response)
488
0
    return CURLE_OUT_OF_MEMORY;
489
490
  /* Return the response. */
491
0
  Curl_bufref_set(out, response, strlen(response), curl_free);
492
0
  return result;
493
0
}
494
495
/*
496
 * Curl_auth_decode_digest_http_message()
497
 *
498
 * This is used to decode an HTTP DIGEST challenge message into the separate
499
 * attributes.
500
 *
501
 * Parameters:
502
 *
503
 * chlg    [in]     - The challenge message.
504
 * digest  [in/out] - The digest data struct being used and modified.
505
 *
506
 * Returns CURLE_OK on success.
507
 */
508
CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
509
                                              struct digestdata *digest)
510
0
{
511
0
  bool before = FALSE; /* got a nonce before */
512
513
  /* If we already have received a nonce, keep that in mind */
514
0
  if(digest->nonce)
515
0
    before = TRUE;
516
517
  /* Clean up any former leftovers and initialise to defaults */
518
0
  Curl_auth_digest_cleanup(digest);
519
520
0
  for(;;) {
521
0
    char value[DIGEST_MAX_VALUE_LENGTH];
522
0
    char content[DIGEST_MAX_CONTENT_LENGTH];
523
524
    /* Pass all additional spaces here */
525
0
    while(*chlg && ISBLANK(*chlg))
526
0
      chlg++;
527
528
    /* Extract a value=content pair */
529
0
    if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
530
0
      if(curl_strequal(value, "nonce")) {
531
0
        curlx_free(digest->nonce);
532
0
        digest->nonce = curlx_strdup(content);
533
0
        if(!digest->nonce)
534
0
          return CURLE_OUT_OF_MEMORY;
535
0
      }
536
0
      else if(curl_strequal(value, "stale")) {
537
0
        if(curl_strequal(content, "true")) {
538
0
          digest->stale = TRUE;
539
0
          digest->nc = 1; /* we make a new nonce now */
540
0
        }
541
0
      }
542
0
      else if(curl_strequal(value, "realm")) {
543
0
        curlx_free(digest->realm);
544
0
        digest->realm = curlx_strdup(content);
545
0
        if(!digest->realm)
546
0
          return CURLE_OUT_OF_MEMORY;
547
0
      }
548
0
      else if(curl_strequal(value, "opaque")) {
549
0
        curlx_free(digest->opaque);
550
0
        digest->opaque = curlx_strdup(content);
551
0
        if(!digest->opaque)
552
0
          return CURLE_OUT_OF_MEMORY;
553
0
      }
554
0
      else if(curl_strequal(value, "qop")) {
555
0
        const char *token = content;
556
0
        struct Curl_str out;
557
0
        bool foundAuth = FALSE;
558
0
        bool foundAuthInt = FALSE;
559
        /* Pass leading spaces */
560
0
        while(*token && ISBLANK(*token))
561
0
          token++;
562
0
        while(!curlx_str_until(&token, &out, 32, ',')) {
563
0
          if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH))
564
0
            foundAuth = TRUE;
565
0
          else if(curlx_str_casecompare(&out,
566
0
                                        DIGEST_QOP_VALUE_STRING_AUTH_INT))
567
0
            foundAuthInt = TRUE;
568
0
          if(curlx_str_single(&token, ','))
569
0
            break;
570
0
          while(*token && ISBLANK(*token))
571
0
            token++;
572
0
        }
573
574
        /* Select only auth or auth-int. Otherwise, ignore */
575
0
        if(foundAuth) {
576
0
          curlx_free(digest->qop);
577
0
          digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH);
578
0
          if(!digest->qop)
579
0
            return CURLE_OUT_OF_MEMORY;
580
0
        }
581
0
        else if(foundAuthInt) {
582
0
          curlx_free(digest->qop);
583
0
          digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH_INT);
584
0
          if(!digest->qop)
585
0
            return CURLE_OUT_OF_MEMORY;
586
0
        }
587
0
      }
588
0
      else if(curl_strequal(value, "algorithm")) {
589
0
        curlx_free(digest->algorithm);
590
0
        digest->algorithm = curlx_strdup(content);
591
0
        if(!digest->algorithm)
592
0
          return CURLE_OUT_OF_MEMORY;
593
594
0
        if(curl_strequal(content, "MD5-sess"))
595
0
          digest->algo = ALGO_MD5SESS;
596
0
        else if(curl_strequal(content, "MD5"))
597
0
          digest->algo = ALGO_MD5;
598
0
        else if(curl_strequal(content, "SHA-256"))
599
0
          digest->algo = ALGO_SHA256;
600
0
        else if(curl_strequal(content, "SHA-256-SESS"))
601
0
          digest->algo = ALGO_SHA256SESS;
602
0
        else if(curl_strequal(content, "SHA-512-256")) {
603
0
#ifdef CURL_HAVE_SHA512_256
604
0
          digest->algo = ALGO_SHA512_256;
605
#else /* !CURL_HAVE_SHA512_256 */
606
          return CURLE_NOT_BUILT_IN;
607
#endif /* CURL_HAVE_SHA512_256 */
608
0
        }
609
0
        else if(curl_strequal(content, "SHA-512-256-SESS")) {
610
0
#ifdef CURL_HAVE_SHA512_256
611
0
          digest->algo = ALGO_SHA512_256SESS;
612
#else /* !CURL_HAVE_SHA512_256 */
613
          return CURLE_NOT_BUILT_IN;
614
#endif /* CURL_HAVE_SHA512_256 */
615
0
        }
616
0
        else
617
0
          return CURLE_BAD_CONTENT_ENCODING;
618
0
      }
619
0
      else if(curl_strequal(value, "userhash")) {
620
0
        if(curl_strequal(content, "true")) {
621
0
          digest->userhash = TRUE;
622
0
        }
623
0
      }
624
0
      else {
625
        /* Unknown specifier, ignore it! */
626
0
      }
627
0
    }
628
0
    else
629
0
      break; /* We are done here */
630
631
    /* Pass all additional spaces here */
632
0
    while(*chlg && ISBLANK(*chlg))
633
0
      chlg++;
634
635
    /* Allow the list to be comma-separated */
636
0
    if(',' == *chlg)
637
0
      chlg++;
638
0
  }
639
640
  /* We had a nonce since before, and we got another one now without
641
     'stale=true'. This means we provided bad credentials in the previous
642
     request */
643
0
  if(before && !digest->stale)
644
0
    return CURLE_BAD_CONTENT_ENCODING;
645
646
  /* We got this header without a nonce, that is a bad Digest line! */
647
0
  if(!digest->nonce)
648
0
    return CURLE_BAD_CONTENT_ENCODING;
649
650
  /* "<algo>-sess" protocol versions require "auth" or "auth-int" qop */
651
0
  if(!digest->qop && (digest->algo & SESSION_ALGO))
652
0
    return CURLE_BAD_CONTENT_ENCODING;
653
654
0
  return CURLE_OK;
655
0
}
656
657
/*
658
 * auth_create_digest_http_message()
659
 *
660
 * This is used to generate an HTTP DIGEST response message ready for sending
661
 * to the recipient.
662
 *
663
 * Parameters:
664
 *
665
 * data    [in]     - The session handle.
666
 * userp   [in]     - The username.
667
 * passwdp [in]     - The user's password.
668
 * request [in]     - The HTTP request.
669
 * uripath [in]     - The path of the HTTP uri.
670
 * digest  [in/out] - The digest data struct being used and modified.
671
 * outptr  [in/out] - The address where a pointer to newly allocated memory
672
 *                    holding the result will be stored upon completion.
673
 * outlen  [out]    - The length of the output message.
674
 *
675
 * Returns CURLE_OK on success.
676
 */
677
static CURLcode auth_create_digest_http_message(
678
  struct Curl_easy *data,
679
  const char *userp,
680
  const char *passwdp,
681
  const unsigned char *request,
682
  const unsigned char *uripath,
683
  struct digestdata *digest,
684
  char **outptr, size_t *outlen,
685
  void (*convert_to_ascii)(const unsigned char *, unsigned char *),
686
  CURLcode (*hash)(unsigned char *, const unsigned char *, const size_t))
687
0
{
688
0
  CURLcode result;
689
0
  unsigned char hashbuf[32]; /* 32 bytes/256 bits */
690
0
  unsigned char request_digest[65];
691
0
  unsigned char ha1[65];    /* 64 digits and 1 zero byte */
692
0
  unsigned char ha2[65];    /* 64 digits and 1 zero byte */
693
0
  char userh[65];
694
0
  char *cnonce = NULL;
695
0
  size_t cnonce_sz = 0;
696
0
  char *userp_quoted = NULL;
697
0
  char *realm_quoted = NULL;
698
0
  char *nonce_quoted = NULL;
699
0
  char *hashthis = NULL;
700
0
  char *uri_quoted = NULL;
701
0
  struct dynbuf response;
702
0
  *outptr = NULL;
703
704
0
  curlx_dyn_init(&response, 4096); /* arbitrary max */
705
706
0
  memset(hashbuf, 0, sizeof(hashbuf));
707
0
  if(!digest->nc)
708
0
    digest->nc = 1;
709
710
0
  if(!digest->cnonce) {
711
0
    char cnoncebuf[12];
712
0
    result = Curl_rand_bytes(data,
713
#ifdef DEBUGBUILD
714
                             TRUE,
715
#endif
716
0
                             (unsigned char *)cnoncebuf,
717
0
                             sizeof(cnoncebuf));
718
0
    if(!result)
719
0
      result = curlx_base64_encode((uint8_t *)cnoncebuf, sizeof(cnoncebuf),
720
0
                                   &cnonce, &cnonce_sz);
721
0
    if(result)
722
0
      goto oom;
723
724
0
    digest->cnonce = cnonce;
725
0
  }
726
727
0
  if(digest->userhash) {
728
0
    char *hasht = curl_maprintf("%s:%s", userp,
729
0
                                digest->realm ? digest->realm : "");
730
0
    if(!hasht) {
731
0
      result = CURLE_OUT_OF_MEMORY;
732
0
      goto oom;
733
0
    }
734
735
0
    result = hash(hashbuf, (unsigned char *)hasht, strlen(hasht));
736
0
    curlx_free(hasht);
737
0
    if(result)
738
0
      goto oom;
739
0
    convert_to_ascii(hashbuf, (unsigned char *)userh);
740
0
  }
741
742
  /*
743
    If the algorithm is "MD5" or unspecified (which then defaults to MD5):
744
745
      A1 = unq(username-value) ":" unq(realm-value) ":" passwd
746
747
    If the algorithm is "MD5-sess" then:
748
749
      A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":"
750
           unq(nonce-value) ":" unq(cnonce-value)
751
  */
752
753
0
  hashthis = curl_maprintf("%s:%s:%s", userp, digest->realm ?
754
0
                           digest->realm : "", passwdp);
755
0
  if(!hashthis) {
756
0
    result = CURLE_OUT_OF_MEMORY;
757
0
    goto oom;
758
0
  }
759
760
0
  result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
761
0
  curlx_free(hashthis);
762
0
  if(result)
763
0
    goto oom;
764
0
  convert_to_ascii(hashbuf, ha1);
765
766
0
  if(digest->algo & SESSION_ALGO) {
767
    /* nonce and cnonce are OUTSIDE the hash */
768
0
    char *tmp = curl_maprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce);
769
0
    if(!tmp) {
770
0
      result = CURLE_OUT_OF_MEMORY;
771
0
      goto oom;
772
0
    }
773
774
0
    result = hash(hashbuf, (unsigned char *)tmp, strlen(tmp));
775
0
    curlx_free(tmp);
776
0
    if(result)
777
0
      goto oom;
778
0
    convert_to_ascii(hashbuf, ha1);
779
0
  }
780
781
  /*
782
    If the "qop" directive's value is "auth" or is unspecified, then A2 is:
783
784
      A2 = Method ":" digest-uri-value
785
786
    If the "qop" value is "auth-int", then A2 is:
787
788
      A2 = Method ":" digest-uri-value ":" H(entity-body)
789
790
    (The "Method" value is the HTTP request method as specified in section
791
    5.1.1 of RFC 2616)
792
  */
793
794
0
  uri_quoted = auth_digest_string_quoted((const char *)uripath);
795
0
  if(!uri_quoted) {
796
0
    result = CURLE_OUT_OF_MEMORY;
797
0
    goto oom;
798
0
  }
799
800
0
  hashthis = curl_maprintf("%s:%s", request, uripath);
801
0
  if(!hashthis) {
802
0
    result = CURLE_OUT_OF_MEMORY;
803
0
    goto oom;
804
0
  }
805
806
0
  if(digest->qop && curl_strequal(digest->qop, "auth-int")) {
807
    /* We do not support auth-int for PUT or POST */
808
0
    char hashed[65];
809
0
    char *hashthis2;
810
811
0
    result = hash(hashbuf, (const unsigned char *)"", 0);
812
0
    if(result) {
813
0
      curlx_free(hashthis);
814
0
      goto oom;
815
0
    }
816
0
    convert_to_ascii(hashbuf, (unsigned char *)hashed);
817
818
0
    hashthis2 = curl_maprintf("%s:%s", hashthis, hashed);
819
0
    curlx_free(hashthis);
820
0
    hashthis = hashthis2;
821
0
    if(!hashthis) {
822
0
      result = CURLE_OUT_OF_MEMORY;
823
0
      goto oom;
824
0
    }
825
0
  }
826
827
0
  result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
828
0
  curlx_free(hashthis);
829
0
  if(result)
830
0
    goto oom;
831
0
  convert_to_ascii(hashbuf, ha2);
832
833
0
  if(digest->qop)
834
0
    hashthis = curl_maprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce,
835
0
                             digest->nc, digest->cnonce, digest->qop, ha2);
836
0
  else
837
0
    hashthis = curl_maprintf("%s:%s:%s", ha1, digest->nonce, ha2);
838
839
0
  if(!hashthis) {
840
0
    result = CURLE_OUT_OF_MEMORY;
841
0
    goto oom;
842
0
  }
843
844
0
  result = hash(hashbuf, (unsigned char *)hashthis, strlen(hashthis));
845
0
  curlx_free(hashthis);
846
0
  if(result)
847
0
    goto oom;
848
0
  convert_to_ascii(hashbuf, request_digest);
849
850
  /* For test case 64 (snooped from a Mozilla 1.3a request)
851
852
     Authorization: Digest username="testuser", realm="testrealm", \
853
     nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
854
855
     Digest parameters are all quoted strings. Username which is provided by
856
     the user will need double quotes and backslashes within it escaped.
857
     realm, nonce, and opaque will need backslashes as well as they were
858
     de-escaped when copied from request header. cnonce is generated with
859
     web-safe characters. uri is already percent encoded. nc is 8 hex
860
     characters. algorithm and qop with standard values only contain web-safe
861
     characters.
862
  */
863
0
  userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp);
864
0
  if(!userp_quoted) {
865
0
    result = CURLE_OUT_OF_MEMORY;
866
0
    goto oom;
867
0
  }
868
0
  if(digest->realm)
869
0
    realm_quoted = auth_digest_string_quoted(digest->realm);
870
0
  else {
871
0
    realm_quoted = curlx_malloc(1);
872
0
    if(realm_quoted)
873
0
      realm_quoted[0] = 0;
874
0
  }
875
0
  if(!realm_quoted) {
876
0
    result = CURLE_OUT_OF_MEMORY;
877
0
    goto oom;
878
0
  }
879
880
0
  nonce_quoted = auth_digest_string_quoted(digest->nonce);
881
0
  if(!nonce_quoted) {
882
0
    result = CURLE_OUT_OF_MEMORY;
883
0
    goto oom;
884
0
  }
885
886
0
  if(digest->qop) {
887
0
    result = curlx_dyn_addf(&response, "username=\"%s\", "
888
0
                            "realm=\"%s\", "
889
0
                            "nonce=\"%s\", "
890
0
                            "uri=\"%s\", "
891
0
                            "cnonce=\"%s\", "
892
0
                            "nc=%08x, "
893
0
                            "qop=%s, "
894
0
                            "response=\"%s\"",
895
0
                            userp_quoted,
896
0
                            realm_quoted,
897
0
                            nonce_quoted,
898
0
                            uri_quoted,
899
0
                            digest->cnonce,
900
0
                            digest->nc,
901
0
                            digest->qop,
902
0
                            request_digest);
903
904
    /* Increment nonce-count to use another nc value for the next request */
905
0
    digest->nc++;
906
0
  }
907
0
  else {
908
0
    result = curlx_dyn_addf(&response, "username=\"%s\", "
909
0
                            "realm=\"%s\", "
910
0
                            "nonce=\"%s\", "
911
0
                            "uri=\"%s\", "
912
0
                            "response=\"%s\"",
913
0
                            userp_quoted,
914
0
                            realm_quoted,
915
0
                            nonce_quoted,
916
0
                            uri_quoted,
917
0
                            request_digest);
918
0
  }
919
0
  if(result)
920
0
    goto oom;
921
922
  /* Add the optional fields */
923
0
  if(digest->opaque) {
924
    /* Append the opaque */
925
0
    char *opaque_quoted = auth_digest_string_quoted(digest->opaque);
926
0
    if(!opaque_quoted) {
927
0
      result = CURLE_OUT_OF_MEMORY;
928
0
      goto oom;
929
0
    }
930
0
    result = curlx_dyn_addf(&response, ", opaque=\"%s\"", opaque_quoted);
931
0
    curlx_free(opaque_quoted);
932
0
    if(result)
933
0
      goto oom;
934
0
  }
935
936
0
  if(digest->algorithm) {
937
    /* Append the algorithm */
938
0
    result = curlx_dyn_addf(&response, ", algorithm=%s", digest->algorithm);
939
0
    if(result)
940
0
      goto oom;
941
0
  }
942
943
0
  if(digest->userhash) {
944
    /* Append the userhash */
945
0
    result = curlx_dyn_add(&response, ", userhash=true");
946
0
    if(result)
947
0
      goto oom;
948
0
  }
949
950
  /* Return the output */
951
0
  *outptr = curlx_dyn_ptr(&response);
952
0
  *outlen = curlx_dyn_len(&response);
953
0
  result = CURLE_OK;
954
955
0
oom:
956
0
  curlx_free(nonce_quoted);
957
0
  curlx_free(realm_quoted);
958
0
  curlx_free(uri_quoted);
959
0
  curlx_free(userp_quoted);
960
0
  if(result)
961
0
    curlx_dyn_free(&response);
962
0
  return result;
963
0
}
964
965
/*
966
 * Curl_auth_create_digest_http_message()
967
 *
968
 * This is used to generate an HTTP DIGEST response message ready for sending
969
 * to the recipient.
970
 *
971
 * Parameters:
972
 *
973
 * data    [in]     - The session handle.
974
 * userp   [in]     - The username.
975
 * passwdp [in]     - The user's password.
976
 * request [in]     - The HTTP request.
977
 * uripath [in]     - The path of the HTTP uri.
978
 * digest  [in/out] - The digest data struct being used and modified.
979
 * outptr  [in/out] - The address where a pointer to newly allocated memory
980
 *                    holding the result will be stored upon completion.
981
 * outlen  [out]    - The length of the output message.
982
 *
983
 * Returns CURLE_OK on success.
984
 */
985
CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
986
                                              const char *userp,
987
                                              const char *passwdp,
988
                                              const unsigned char *request,
989
                                              const unsigned char *uripath,
990
                                              struct digestdata *digest,
991
                                              char **outptr, size_t *outlen)
992
0
{
993
0
  if(digest->algo <= ALGO_MD5SESS)
994
0
    return auth_create_digest_http_message(data, userp, passwdp,
995
0
                                           request, uripath, digest,
996
0
                                           outptr, outlen,
997
0
                                           auth_digest_md5_to_ascii,
998
0
                                           Curl_md5it);
999
1000
0
  if(digest->algo <= ALGO_SHA256SESS)
1001
0
    return auth_create_digest_http_message(data, userp, passwdp,
1002
0
                                           request, uripath, digest,
1003
0
                                           outptr, outlen,
1004
0
                                           auth_digest_sha256_to_ascii,
1005
0
                                           Curl_sha256it);
1006
0
#ifdef CURL_HAVE_SHA512_256
1007
0
  if(digest->algo <= ALGO_SHA512_256SESS)
1008
0
    return auth_create_digest_http_message(data, userp, passwdp,
1009
0
                                           request, uripath, digest,
1010
0
                                           outptr, outlen,
1011
0
                                           auth_digest_sha256_to_ascii,
1012
0
                                           Curl_sha512_256it);
1013
0
#endif /* CURL_HAVE_SHA512_256 */
1014
1015
  /* Should be unreachable */
1016
0
  return CURLE_BAD_CONTENT_ENCODING;
1017
0
}
1018
1019
/*
1020
 * Curl_auth_digest_cleanup()
1021
 *
1022
 * This is used to clean up the digest specific data.
1023
 *
1024
 * Parameters:
1025
 *
1026
 * digest    [in/out] - The digest data struct being cleaned up.
1027
 *
1028
 */
1029
void Curl_auth_digest_cleanup(struct digestdata *digest)
1030
0
{
1031
0
  Curl_safefree(digest->nonce);
1032
0
  Curl_safefree(digest->cnonce);
1033
0
  Curl_safefree(digest->realm);
1034
0
  Curl_safefree(digest->opaque);
1035
0
  Curl_safefree(digest->qop);
1036
0
  Curl_safefree(digest->algorithm);
1037
1038
0
  digest->nc = 0;
1039
0
  digest->algo = ALGO_MD5; /* default algorithm */
1040
0
  digest->stale = FALSE;   /* default means normal, not stale */
1041
  digest->userhash = FALSE;
1042
0
}
1043
#endif /* !USE_WINDOWS_SSPI */
1044
1045
#endif /* !CURL_DISABLE_DIGEST_AUTH */