Coverage Report

Created: 2025-10-10 06:31

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