Coverage Report

Created: 2026-01-09 07:14

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