1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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.haxx.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 ***************************************************************************/
23 #include "curl_setup.h"
25 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
29 #include "curl_sasl.h"
30 #include "http_digest.h"
31 #include "curl_printf.h"
33 /* The last #include files should be: */
34 #include "curl_memory.h"
37 /* Test example headers:
39 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
40 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
44 CURLcode Curl_input_digest(struct connectdata *conn,
46 const char *header) /* rest of the *-authenticate:
49 struct SessionHandle *data = conn->data;
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))
62 return CURLE_BAD_CONTENT_ENCODING;
64 header += strlen("Digest");
65 while(*header && ISSPACE(*header))
68 return Curl_sasl_decode_digest_http_message(header, digest);
71 CURLcode Curl_output_digest(struct connectdata *conn,
73 const unsigned char *request,
74 const unsigned char *uripath)
77 struct SessionHandle *data = conn->data;
84 /* Point to the address of the pointer that holds the string to send to the
85 server, which is for a plain host or for a HTTP proxy */
88 /* Point to the name and password for this */
92 /* Point to the correct struct with this */
93 struct digestdata *digest;
97 digest = &data->state.proxydigest;
98 allocuserpwd = &conn->allocptr.proxyuserpwd;
99 userp = conn->proxyuser;
100 passwdp = conn->proxypasswd;
101 authp = &data->state.authproxy;
104 digest = &data->state.digest;
105 allocuserpwd = &conn->allocptr.userpwd;
107 passwdp = conn->passwd;
108 authp = &data->state.authhost;
111 Curl_safefree(*allocuserpwd);
113 /* not set means empty */
120 #if defined(USE_WINDOWS_SSPI)
121 have_chlg = digest->input_token ? TRUE : FALSE;
123 have_chlg = digest->nonce ? TRUE : FALSE;
131 /* So IE browsers < v7 cut off the URI part at the query part when they
132 evaluate the MD5 and some (IIS?) servers work with them so we may need to
133 do the Digest IE-style. Note that the different ways cause different MD5
136 Apache servers can be set to do the Digest IE-style automatically using
137 the BrowserMatch feature:
138 https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
140 Further details on Digest implementation differences:
141 http://www.fngtps.com/2006/09/http-authentication
144 if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
145 size_t urilen = tmp - (char *)uripath;
147 path = (unsigned char *) aprintf("%.*s", urilen, uripath);
150 path = (unsigned char *) strdup((char *) uripath);
153 return CURLE_OUT_OF_MEMORY;
155 result = Curl_sasl_create_digest_http_message(data, userp, passwdp, request,
156 path, digest, &response, &len);
161 *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
162 proxy ? "Proxy-" : "",
166 return CURLE_OUT_OF_MEMORY;
173 void Curl_digest_cleanup(struct SessionHandle *data)
175 Curl_sasl_digest_cleanup(&data->state.digest);
176 Curl_sasl_digest_cleanup(&data->state.proxydigest);