1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
31 #include "vauth/vauth.h"
32 #include "http_digest.h"
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
39 /* Test example headers:
41 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
42 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
46 CURLcode Curl_input_digest(struct Curl_easy *data,
48 const char *header) /* rest of the *-authenticate:
51 /* Point to the correct struct with this */
52 struct digestdata *digest;
55 digest = &data->state.proxydigest;
58 digest = &data->state.digest;
61 if(!checkprefix("Digest", header) || !ISBLANK(header[6]))
62 return CURLE_BAD_CONTENT_ENCODING;
64 header += strlen("Digest");
65 while(*header && ISBLANK(*header))
68 return Curl_auth_decode_digest_http_message(header, digest);
71 CURLcode Curl_output_digest(struct Curl_easy *data,
73 const unsigned char *request,
74 const unsigned char *uripath)
77 unsigned char *path = NULL;
83 /* Point to the address of the pointer that holds the string to send to the
84 server, which is for a plain host or for a HTTP proxy */
87 /* Point to the name and password for this */
91 /* Point to the correct struct with this */
92 struct digestdata *digest;
96 #ifdef CURL_DISABLE_PROXY
97 return CURLE_NOT_BUILT_IN;
99 digest = &data->state.proxydigest;
100 allocuserpwd = &data->state.aptr.proxyuserpwd;
101 userp = data->state.aptr.proxyuser;
102 passwdp = data->state.aptr.proxypasswd;
103 authp = &data->state.authproxy;
107 digest = &data->state.digest;
108 allocuserpwd = &data->state.aptr.userpwd;
109 userp = data->state.aptr.user;
110 passwdp = data->state.aptr.passwd;
111 authp = &data->state.authhost;
114 Curl_safefree(*allocuserpwd);
116 /* not set means empty */
123 #if defined(USE_WINDOWS_SSPI)
124 have_chlg = digest->input_token ? TRUE : FALSE;
126 have_chlg = digest->nonce ? TRUE : FALSE;
134 /* So IE browsers < v7 cut off the URI part at the query part when they
135 evaluate the MD5 and some (IIS?) servers work with them so we may need to
136 do the Digest IE-style. Note that the different ways cause different MD5
139 Apache servers can be set to do the Digest IE-style automatically using
140 the BrowserMatch feature:
141 https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
143 Further details on Digest implementation differences:
144 http://www.fngtps.com/2006/09/http-authentication
148 tmp = strchr((char *)uripath, '?');
150 size_t urilen = tmp - (char *)uripath;
151 /* typecast is fine here since the value is always less than 32 bits */
152 path = (unsigned char *) aprintf("%.*s", (int)urilen, uripath);
156 path = (unsigned char *) strdup((char *) uripath);
159 return CURLE_OUT_OF_MEMORY;
161 result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
162 path, digest, &response, &len);
167 *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
168 proxy ? "Proxy-" : "",
172 return CURLE_OUT_OF_MEMORY;
179 void Curl_http_auth_cleanup_digest(struct Curl_easy *data)
181 Curl_auth_digest_cleanup(&data->state.digest);
182 Curl_auth_digest_cleanup(&data->state.proxydigest);