Coverage Report

Created: 2026-01-25 06:10

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