Coverage Report

Created: 2026-09-01 06:58

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