Coverage Report

Created: 2026-10-15 06:25

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
37.9k
{
43
37.9k
  bool flush = FALSE;
44
37.9k
  if(!digest->origin || !Curl_peer_equal(peer, digest->origin)) {
45
9.23k
    CURL_TRC_M(data, "http_digest, reset on peer change to %s:%u",
46
9.23k
               peer ? peer->hostname : "-",
47
9.23k
               (unsigned int)(peer ? peer->port : 0));
48
9.23k
    flush = TRUE;
49
9.23k
  }
50
28.7k
  else if(digest->creds && !Curl_creds_same(creds, digest->creds)) {
51
192
    CURL_TRC_M(data, "http_digest, reset on creds change to %s",
52
192
               creds ? creds->user : "-");
53
192
    flush = TRUE;
54
192
  }
55
56
37.9k
  if(flush) {
57
    /* flush Digest state */
58
9.42k
    Curl_auth_digest_cleanup(digest);
59
9.42k
  }
60
61
37.9k
  Curl_peer_link(&digest->origin, peer);
62
37.9k
  Curl_creds_link(&digest->creds, creds);
63
37.9k
}
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
91.5k
{
75
  /* Point to the correct struct with this */
76
91.5k
  struct digestdata *digest;
77
91.5k
  struct Curl_peer *origin = NULL;
78
91.5k
  CURLcode result;
79
80
91.5k
  if(proxy) {
81
166
    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
166
    origin = data->conn->http_proxy.peer;
87
166
#endif
88
166
  }
89
91.3k
  else {
90
91.3k
    digest = &data->state.digest;
91
91.3k
    origin = data->state.origin;
92
91.3k
  }
93
94
91.5k
  if(!checkprefix("Digest", header) || !ISBLANK(header[6])) {
95
18.4k
    Curl_auth_digest_cleanup(digest);
96
18.4k
    return CURLE_AUTH_ERROR;
97
18.4k
  }
98
99
73.1k
  header += CURL_CSTRLEN("Digest");
100
73.1k
  curlx_str_passblanks(&header);
101
102
  /* This resets the digest struct before decoding */
103
73.1k
  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
73.1k
  Curl_peer_link(&digest->origin, origin);
107
73.1k
  return result;
108
91.5k
}
109
110
CURLcode Curl_output_digest(struct Curl_easy *data,
111
                            bool proxy,
112
                            const unsigned char *request,
113
                            const unsigned char *uripath)
114
37.9k
{
115
37.9k
  struct Curl_peer *origin = NULL;
116
37.9k
  CURLcode result;
117
37.9k
  char *response;
118
37.9k
  size_t len;
119
37.9k
  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
37.9k
  char **allocuserpwd;
124
125
  /* Point to the name and password for this */
126
37.9k
  struct Curl_creds *creds = NULL;
127
128
  /* Point to the correct struct with this */
129
37.9k
  struct digestdata *digest;
130
37.9k
  struct auth *authp;
131
132
37.9k
  if(proxy) {
133
#ifdef CURL_DISABLE_PROXY
134
    return CURLE_NOT_BUILT_IN;
135
#else
136
1.51k
    digest = &data->state.proxydigest;
137
1.51k
    origin = data->conn->http_proxy.peer;
138
1.51k
    creds = data->conn->http_proxy.creds;
139
1.51k
    allocuserpwd = &data->req.hd_proxy_auth;
140
1.51k
    authp = &data->state.authproxy;
141
1.51k
#endif
142
1.51k
  }
143
36.4k
  else {
144
36.4k
    DEBUGASSERT(data->state.origin);
145
36.4k
    digest = &data->state.digest;
146
36.4k
    origin = data->state.origin;
147
36.4k
    creds = data->state.creds;
148
36.4k
    allocuserpwd = &data->req.hd_auth;
149
36.4k
    authp = &data->state.authhost;
150
36.4k
  }
151
152
37.9k
  digest_flush_stale(data, digest, origin, creds);
153
37.9k
  curlx_safefree(*allocuserpwd);
154
155
#ifdef USE_WINDOWS_SSPI
156
  have_chlg = !!digest->input_token;
157
#else
158
37.9k
  have_chlg = !!digest->nonce;
159
37.9k
#endif
160
161
37.9k
  if(!have_chlg) {
162
18.9k
    authp->done = FALSE;
163
18.9k
    return CURLE_OK;
164
18.9k
  }
165
166
19.0k
  result = Curl_auth_create_digest_http_message(data, creds, request,
167
19.0k
                                                uripath, digest,
168
19.0k
                                                &response, &len);
169
19.0k
  if(result)
170
106
    return result;
171
172
18.9k
  *allocuserpwd = curl_maprintf("%sAuthorization: Digest %s\r\n",
173
18.9k
                                proxy ? "Proxy-" : "", response);
174
18.9k
  curlx_free(response);
175
18.9k
  if(!*allocuserpwd)
176
2
    return CURLE_OUT_OF_MEMORY;
177
178
18.9k
  authp->done = TRUE;
179
180
18.9k
  return CURLE_OK;
181
18.9k
}
182
183
void Curl_http_auth_cleanup_digest(struct Curl_easy *data)
184
1.27M
{
185
1.27M
  Curl_auth_digest_cleanup(&data->state.digest);
186
1.27M
  Curl_auth_digest_cleanup(&data->state.proxydigest);
187
1.27M
}
188
189
#endif