/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 "strcase.h" |
30 | | #include "vauth/vauth.h" |
31 | | #include "http_digest.h" |
32 | | #include "curlx/strparse.h" |
33 | | |
34 | | /* Test example headers: |
35 | | |
36 | | WWW-Authenticate: Digest realm="testrealm", nonce="1053604598" |
37 | | Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598" |
38 | | |
39 | | */ |
40 | | |
41 | | CURLcode Curl_input_digest(struct Curl_easy *data, |
42 | | bool proxy, |
43 | | const char *header) /* rest of the *-authenticate: |
44 | | header */ |
45 | 0 | { |
46 | | /* Point to the correct struct with this */ |
47 | 0 | struct digestdata *digest; |
48 | |
|
49 | 0 | if(proxy) { |
50 | 0 | digest = &data->state.proxydigest; |
51 | 0 | } |
52 | 0 | else { |
53 | 0 | digest = &data->state.digest; |
54 | 0 | } |
55 | |
|
56 | 0 | if(!checkprefix("Digest", header) || !ISBLANK(header[6])) |
57 | 0 | return CURLE_AUTH_ERROR; |
58 | | |
59 | 0 | header += strlen("Digest"); |
60 | 0 | curlx_str_passblanks(&header); |
61 | |
|
62 | 0 | return Curl_auth_decode_digest_http_message(header, digest); |
63 | 0 | } |
64 | | |
65 | | /* Flush the Digest state if it was created for a different origin or with |
66 | | different credentials than the ones now in use, then link the current |
67 | | ones. */ |
68 | | static void digest_flush_stale(struct digestdata *digest, |
69 | | struct Curl_peer *peer, |
70 | | struct Curl_creds *creds) |
71 | 0 | { |
72 | 0 | bool flush = FALSE; |
73 | 0 | if(digest->origin && !Curl_peer_same_destination(peer, digest->origin)) |
74 | 0 | flush = TRUE; |
75 | 0 | else if(digest->creds && !Curl_creds_same(creds, digest->creds)) |
76 | 0 | flush = TRUE; |
77 | |
|
78 | 0 | if(flush) |
79 | | /* flush Digest state */ |
80 | 0 | Curl_auth_digest_cleanup(digest); |
81 | |
|
82 | 0 | Curl_peer_link(&digest->origin, peer); |
83 | 0 | Curl_creds_link(&digest->creds, creds); |
84 | 0 | } |
85 | | |
86 | | CURLcode Curl_output_digest(struct Curl_easy *data, |
87 | | bool proxy, |
88 | | const unsigned char *request, |
89 | | const unsigned char *uripath) |
90 | 0 | { |
91 | 0 | CURLcode result; |
92 | 0 | char *response; |
93 | 0 | size_t len; |
94 | 0 | bool have_chlg; |
95 | | |
96 | | /* Point to the address of the pointer that holds the string to send to the |
97 | | server, which is for a plain host or for an HTTP proxy */ |
98 | 0 | char **allocuserpwd; |
99 | | |
100 | | /* Point to the name and password for this */ |
101 | 0 | struct Curl_creds *creds = NULL; |
102 | | |
103 | | /* Point to the correct struct with this */ |
104 | 0 | struct digestdata *digest; |
105 | 0 | struct auth *authp; |
106 | |
|
107 | 0 | if(proxy) { |
108 | | #ifdef CURL_DISABLE_PROXY |
109 | | return CURLE_NOT_BUILT_IN; |
110 | | #else |
111 | 0 | digest = &data->state.proxydigest; |
112 | 0 | digest_flush_stale(digest, data->conn->http_proxy.peer, |
113 | 0 | data->conn->http_proxy.creds); |
114 | 0 | allocuserpwd = &data->req.hd_proxy_auth; |
115 | 0 | creds = data->conn->http_proxy.creds; |
116 | 0 | authp = &data->state.authproxy; |
117 | 0 | #endif |
118 | 0 | } |
119 | 0 | else { |
120 | 0 | DEBUGASSERT(data->state.origin); |
121 | 0 | digest = &data->state.digest; |
122 | 0 | digest_flush_stale(digest, data->state.origin, data->state.creds); |
123 | 0 | allocuserpwd = &data->req.hd_auth; |
124 | 0 | creds = data->state.creds; |
125 | 0 | authp = &data->state.authhost; |
126 | 0 | } |
127 | |
|
128 | 0 | curlx_safefree(*allocuserpwd); |
129 | |
|
130 | | #ifdef USE_WINDOWS_SSPI |
131 | | have_chlg = !!digest->input_token; |
132 | | #else |
133 | 0 | have_chlg = !!digest->nonce; |
134 | 0 | #endif |
135 | |
|
136 | 0 | if(!have_chlg) { |
137 | 0 | authp->done = FALSE; |
138 | 0 | return CURLE_OK; |
139 | 0 | } |
140 | | |
141 | 0 | result = Curl_auth_create_digest_http_message(data, creds, request, |
142 | 0 | uripath, digest, |
143 | 0 | &response, &len); |
144 | 0 | if(result) |
145 | 0 | return result; |
146 | | |
147 | 0 | *allocuserpwd = curl_maprintf("%sAuthorization: Digest %s\r\n", |
148 | 0 | proxy ? "Proxy-" : "", response); |
149 | 0 | curlx_free(response); |
150 | 0 | if(!*allocuserpwd) |
151 | 0 | return CURLE_OUT_OF_MEMORY; |
152 | | |
153 | 0 | authp->done = TRUE; |
154 | |
|
155 | 0 | return CURLE_OK; |
156 | 0 | } |
157 | | |
158 | | void Curl_http_auth_cleanup_digest(struct Curl_easy *data) |
159 | 0 | { |
160 | 0 | Curl_auth_digest_cleanup(&data->state.digest); |
161 | 0 | Curl_auth_digest_cleanup(&data->state.proxydigest); |
162 | 0 | } |
163 | | |
164 | | #endif |