Coverage Report

Created: 2026-10-22 06:49

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