1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 "vauth/vauth.h"
30 #include "http_digest.h"
31 /* The last 3 #include files should be in this order */
32 #include "curl_printf.h"
33 #include "curl_memory.h"
36 /* Test example headers:
38 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
39 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
43 CURLcode Curl_input_digest(struct connectdata *conn,
45 const char *header) /* rest of the *-authenticate:
48 struct Curl_easy *data = conn->data;
50 /* Point to the correct struct with this */
51 struct digestdata *digest;
54 digest = &data->state.proxydigest;
57 digest = &data->state.digest;
60 if(!checkprefix("Digest", header))
61 return CURLE_BAD_CONTENT_ENCODING;
63 header += strlen("Digest");
64 while(*header && ISSPACE(*header))
67 return Curl_auth_decode_digest_http_message(header, digest);
70 CURLcode Curl_output_digest(struct connectdata *conn,
72 const unsigned char *request,
73 const unsigned char *uripath)
76 struct Curl_easy *data = conn->data;
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 digest = &data->state.proxydigest;
97 allocuserpwd = &conn->allocptr.proxyuserpwd;
98 userp = conn->http_proxy.user;
99 passwdp = conn->http_proxy.passwd;
100 authp = &data->state.authproxy;
103 digest = &data->state.digest;
104 allocuserpwd = &conn->allocptr.userpwd;
106 passwdp = conn->passwd;
107 authp = &data->state.authhost;
110 Curl_safefree(*allocuserpwd);
112 /* not set means empty */
119 #if defined(USE_WINDOWS_SSPI)
120 have_chlg = digest->input_token ? TRUE : FALSE;
122 have_chlg = digest->nonce ? TRUE : FALSE;
130 /* So IE browsers < v7 cut off the URI part at the query part when they
131 evaluate the MD5 and some (IIS?) servers work with them so we may need to
132 do the Digest IE-style. Note that the different ways cause different MD5
135 Apache servers can be set to do the Digest IE-style automatically using
136 the BrowserMatch feature:
137 https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
139 Further details on Digest implementation differences:
140 http://www.fngtps.com/2006/09/http-authentication
144 tmp = strchr((char *)uripath, '?');
146 size_t urilen = tmp - (char *)uripath;
147 path = (unsigned char *) aprintf("%.*s", urilen, uripath);
151 path = (unsigned char *) strdup((char *) uripath);
154 return CURLE_OUT_OF_MEMORY;
156 result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
157 path, digest, &response, &len);
162 *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
163 proxy ? "Proxy-" : "",
167 return CURLE_OUT_OF_MEMORY;
174 void Curl_digest_cleanup(struct Curl_easy *data)
176 Curl_auth_digest_cleanup(&data->state.digest);
177 Curl_auth_digest_cleanup(&data->state.proxydigest);