Coverage Report

Created: 2026-10-28 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http_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
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH)
27
28
#include "urldata.h"
29
#include "curl_trc.h"
30
#include "strcase.h"
31
#include "vauth/vauth.h"
32
#include "http_digest.h"
33
#include "curlx/strparse.h"
34
35
/* Flush the Digest state if it was created for a different origin or with
36
   different credentials than the ones now in use, then link the current
37
   ones. */
38
static void digest_flush_stale(struct Curl_easy *data,
39
                               struct digestdata *digest,
40
                               struct Curl_peer *peer,
41
                               struct Curl_creds *creds)
42
12.3k
{
43
12.3k
  bool flush = FALSE;
44
12.3k
  if(!digest->origin || !Curl_peer_equal(peer, digest->origin)) {
45
4.46k
    CURL_TRC_M(data, "http_digest, reset on peer change to %s:%u",
46
4.46k
               peer ? peer->hostname : "-",
47
4.46k
               (unsigned int)(peer ? peer->port : 0));
48
4.46k
    flush = TRUE;
49
4.46k
  }
50
7.84k
  else if(digest->creds && !Curl_creds_same(creds, digest->creds)) {
51
13
    CURL_TRC_M(data, "http_digest, reset on creds change to %s",
52
13
               creds ? creds->user : "-");
53
13
    flush = TRUE;
54
13
  }
55
56
12.3k
  if(flush) {
57
    /* flush Digest state */
58
4.47k
    Curl_auth_digest_cleanup(digest);
59
4.47k
  }
60
61
12.3k
  Curl_peer_link(&digest->origin, peer);
62
12.3k
  Curl_creds_link(&digest->creds, creds);
63
12.3k
}
64
65
/* Test example headers:
66
67
   WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
68
   Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
69
 */
70
CURLcode Curl_input_digest(struct Curl_easy *data,
71
                           bool proxy,
72
                           const char *header) /* rest of the *-authenticate:
73
                                                  header */
74
61.1k
{
75
  /* Point to the correct struct with this */
76
61.1k
  struct digestdata *digest;
77
61.1k
  struct Curl_peer *origin = NULL;
78
61.1k
  CURLcode result;
79
80
61.1k
  if(proxy) {
81
185
    digest = &data->state.proxydigest;
82
#ifdef CURL_DISABLE_PROXY
83
    Curl_auth_digest_cleanup(digest);
84
    return CURLE_OK;  /* just ignore such a header without proxy support */
85
#else
86
185
    origin = data->conn->http_proxy.peer;
87
185
#endif
88
185
  }
89
60.9k
  else {
90
60.9k
    digest = &data->state.digest;
91
60.9k
    origin = data->state.origin;
92
60.9k
  }
93
94
61.1k
  if(!checkprefix("Digest", header) || !ISBLANK(header[6])) {
95
13.1k
    Curl_auth_digest_cleanup(digest);
96
13.1k
    return CURLE_AUTH_ERROR;
97
13.1k
  }
98
99
48.0k
  header += CURL_CSTRLEN("Digest");
100
48.0k
  curlx_str_passblanks(&header);
101
102
  /* This resets the digest struct before decoding */
103
48.0k
  result = Curl_auth_decode_digest_http_message(header, digest);
104
  /* Remember only the peer, the data we take in has no relation to creds
105
   * at this time. We can use it even if creds change. */
106
48.0k
  Curl_peer_link(&digest->origin, origin);
107
48.0k
  return result;
108
61.1k
}
109
110
CURLcode Curl_output_digest(struct Curl_easy *data,
111
                            bool proxy,
112
                            const unsigned char *request,
113
                            const unsigned char *uripath)
114
12.3k
{
115
12.3k
  struct Curl_peer *origin = NULL;
116
12.3k
  CURLcode result;
117
12.3k
  char *response;
118
12.3k
  size_t len;
119
12.3k
  bool have_chlg;
120
121
  /* Point to the address of the pointer that holds the string to send to the
122
     server, which is for a plain host or for an HTTP proxy */
123
12.3k
  char **allocuserpwd;
124
125
  /* Point to the name and password for this */
126
12.3k
  struct Curl_creds *creds = NULL;
127
128
  /* Point to the correct struct with this */
129
12.3k
  struct digestdata *digest;
130
12.3k
  struct auth *authp;
131
132
12.3k
  if(proxy) {
133
#ifdef CURL_DISABLE_PROXY
134
    return CURLE_NOT_BUILT_IN;
135
#else
136
1.33k
    digest = &data->state.proxydigest;
137
1.33k
    origin = data->conn->http_proxy.peer;
138
1.33k
    creds = data->conn->http_proxy.creds;
139
1.33k
    allocuserpwd = &data->req.hd_proxy_auth;
140
1.33k
    authp = &data->state.authproxy;
141
1.33k
#endif
142
1.33k
  }
143
10.9k
  else {
144
10.9k
    DEBUGASSERT(data->state.origin);
145
10.9k
    digest = &data->state.digest;
146
10.9k
    origin = data->state.origin;
147
10.9k
    creds = data->state.creds;
148
10.9k
    allocuserpwd = &data->req.hd_auth;
149
10.9k
    authp = &data->state.authhost;
150
10.9k
  }
151
152
12.3k
  digest_flush_stale(data, digest, origin, creds);
153
12.3k
  curlx_safefree(*allocuserpwd);
154
155
#ifdef USE_WINDOWS_SSPI
156
  have_chlg = !!digest->input_token;
157
#else
158
12.3k
  have_chlg = !!digest->nonce;
159
12.3k
#endif
160
161
12.3k
  if(!have_chlg) {
162
8.72k
    authp->done = FALSE;
163
8.72k
    return CURLE_OK;
164
8.72k
  }
165
166
3.58k
  result = Curl_auth_create_digest_http_message(data, creds, request,
167
3.58k
                                                uripath, digest,
168
3.58k
                                                &response, &len);
169
3.58k
  if(result)
170
30
    return result;
171
172
3.55k
  *allocuserpwd = curl_maprintf("%sAuthorization: Digest %s\r\n",
173
3.55k
                                proxy ? "Proxy-" : "", response);
174
3.55k
  curlx_free(response);
175
3.55k
  if(!*allocuserpwd)
176
1
    return CURLE_OUT_OF_MEMORY;
177
178
3.55k
  authp->done = TRUE;
179
180
3.55k
  return CURLE_OK;
181
3.55k
}
182
183
void Curl_http_auth_cleanup_digest(struct Curl_easy *data)
184
964k
{
185
964k
  Curl_auth_digest_cleanup(&data->state.digest);
186
964k
  Curl_auth_digest_cleanup(&data->state.proxydigest);
187
964k
}
188
189
#endif