/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 | 0 | { |
43 | 0 | bool flush = FALSE; |
44 | 0 | if(!digest->origin || !Curl_peer_equal(peer, digest->origin)) { |
45 | 0 | CURL_TRC_M(data, "http_digest, reset on peer change to %s:%u", |
46 | 0 | peer ? peer->hostname : "-", |
47 | 0 | (unsigned int)(peer ? peer->port : 0)); |
48 | 0 | flush = TRUE; |
49 | 0 | } |
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 | 0 | if(flush) { |
57 | | /* flush Digest state */ |
58 | 0 | Curl_auth_digest_cleanup(digest); |
59 | 0 | } |
60 | |
|
61 | 0 | Curl_peer_link(&digest->origin, peer); |
62 | 0 | Curl_creds_link(&digest->creds, creds); |
63 | 0 | } |
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 | | struct Curl_creds *creds, |
112 | | const char *hd_name, |
113 | | bool proxy, |
114 | | const unsigned char *request, |
115 | | const unsigned char *uripath) |
116 | 0 | { |
117 | 0 | struct Curl_peer *origin = NULL; |
118 | 0 | CURLcode result; |
119 | 0 | char *response; |
120 | 0 | size_t len; |
121 | | /* Point to the address of the pointer that holds the header. */ |
122 | 0 | char **pauth_hd; |
123 | | /* Point to the correct struct with this */ |
124 | 0 | struct digestdata *digest; |
125 | 0 | struct auth *authp; |
126 | |
|
127 | 0 | if(proxy) { |
128 | | #ifdef CURL_DISABLE_PROXY |
129 | | return CURLE_NOT_BUILT_IN; |
130 | | #else |
131 | 0 | digest = &data->state.proxydigest; |
132 | 0 | origin = data->conn->http_proxy.peer; |
133 | 0 | pauth_hd = &data->req.hd_proxy_auth; |
134 | 0 | authp = &data->state.authproxy; |
135 | 0 | #endif |
136 | 0 | } |
137 | 0 | else { |
138 | 0 | DEBUGASSERT(data->state.origin); |
139 | 0 | digest = &data->state.digest; |
140 | 0 | origin = data->state.origin; |
141 | 0 | pauth_hd = &data->req.hd_auth; |
142 | 0 | authp = &data->state.authhost; |
143 | 0 | } |
144 | |
|
145 | 0 | digest_flush_stale(data, digest, origin, creds); |
146 | 0 | curlx_safefree(*pauth_hd); |
147 | |
|
148 | | #ifdef USE_WINDOWS_SSPI |
149 | | if(!digest->input_token) { |
150 | | #else |
151 | 0 | if(!digest->nonce) { |
152 | 0 | #endif |
153 | 0 | authp->done = FALSE; |
154 | 0 | return CURLE_OK; |
155 | 0 | } |
156 | | |
157 | 0 | result = Curl_auth_create_digest_http_message(data, creds, request, |
158 | 0 | uripath, digest, |
159 | 0 | &response, &len); |
160 | 0 | if(result) |
161 | 0 | return result; |
162 | | |
163 | 0 | *pauth_hd = curl_maprintf("%s: Digest %s\r\n", hd_name, response); |
164 | 0 | curlx_free(response); |
165 | 0 | if(!*pauth_hd) |
166 | 0 | return CURLE_OUT_OF_MEMORY; |
167 | | |
168 | 0 | authp->done = TRUE; |
169 | |
|
170 | 0 | return CURLE_OK; |
171 | 0 | } |
172 | | |
173 | | void Curl_http_auth_cleanup_digest(struct Curl_easy *data) |
174 | 0 | { |
175 | 0 | Curl_auth_digest_cleanup(&data->state.digest); |
176 | 0 | Curl_auth_digest_cleanup(&data->state.proxydigest); |
177 | 0 | } |
178 | | |
179 | | #endif |