Coverage Report

Created: 2026-10-15 06:19

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