1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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_memory.h"
33 #define _MPRINTF_REPLACE /* use our functions only */
34 #include <curl/mprintf.h>
36 /* The last #include file should be: */
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 connectdata *conn,
48 const char *header) /* rest of the *-authenticate:
51 struct SessionHandle *data = conn->data;
53 /* Point to the correct struct with this */
54 struct digestdata *digest;
57 digest = &data->state.proxydigest;
60 digest = &data->state.digest;
63 if(!checkprefix("Digest", header))
64 return CURLE_BAD_CONTENT_ENCODING;
66 header += strlen("Digest");
67 while(*header && ISSPACE(*header))
70 return Curl_sasl_decode_digest_http_message(header, digest);
73 CURLcode Curl_output_digest(struct connectdata *conn,
75 const unsigned char *request,
76 const unsigned char *uripath)
79 struct SessionHandle *data = conn->data;
86 /* Point to the address of the pointer that holds the string to send to the
87 server, which is for a plain host or for a HTTP proxy */
90 /* Point to the name and password for this */
94 /* Point to the correct struct with this */
95 struct digestdata *digest;
99 digest = &data->state.proxydigest;
100 allocuserpwd = &conn->allocptr.proxyuserpwd;
101 userp = conn->proxyuser;
102 passwdp = conn->proxypasswd;
103 authp = &data->state.authproxy;
106 digest = &data->state.digest;
107 allocuserpwd = &conn->allocptr.userpwd;
109 passwdp = conn->passwd;
110 authp = &data->state.authhost;
113 Curl_safefree(*allocuserpwd);
115 /* not set means empty */
122 #if defined(USE_WINDOWS_SSPI)
123 have_chlg = digest->input_token ? TRUE : FALSE;
125 have_chlg = digest->nonce ? TRUE : FALSE;
133 /* So IE browsers < v7 cut off the URI part at the query part when they
134 evaluate the MD5 and some (IIS?) servers work with them so we may need to
135 do the Digest IE-style. Note that the different ways cause different MD5
138 Apache servers can be set to do the Digest IE-style automatically using
139 the BrowserMatch feature:
140 http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
142 Further details on Digest implementation differences:
143 http://www.fngtps.com/2006/09/http-authentication
146 if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
147 size_t urilen = tmp - (char *)uripath;
149 path = (unsigned char *) aprintf("%.*s", urilen, uripath);
152 path = (unsigned char *) strdup((char *) uripath);
155 return CURLE_OUT_OF_MEMORY;
157 result = Curl_sasl_create_digest_http_message(data, userp, passwdp, request,
158 path, digest, &response, &len);
163 *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
164 proxy ? "Proxy-" : "",
168 return CURLE_OUT_OF_MEMORY;
175 void Curl_digest_cleanup(struct SessionHandle *data)
177 Curl_sasl_digest_cleanup(&data->state.digest);
178 Curl_sasl_digest_cleanup(&data->state.proxydigest);