blob: e2d865b0af09ed075ac1fc8f3a70cd622ff64deb [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymod15eaac2016-06-28 14:49:26 -07008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
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 ***************************************************************************/
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070022
23#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
25#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
Kristian Monsen5ab50182010-05-14 18:53:44 +010026
27#include "urldata.h"
Elliott Hughescee03382017-06-23 12:17:18 -070028#include "strcase.h"
Alex Deymod15eaac2016-06-28 14:49:26 -070029#include "vauth/vauth.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010030#include "http_digest.h"
Alex Deymod15eaac2016-06-28 14:49:26 -070031/* The last 3 #include files should be in this order */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070032#include "curl_printf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010033#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010034#include "memdebug.h"
35
Kristian Monsen5ab50182010-05-14 18:53:44 +010036/* Test example headers:
37
38WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
39Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
40
41*/
42
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070043CURLcode Curl_input_digest(struct connectdata *conn,
44 bool proxy,
45 const char *header) /* rest of the *-authenticate:
46 header */
Kristian Monsen5ab50182010-05-14 18:53:44 +010047{
Alex Deymoe3149cc2016-10-05 11:18:42 -070048 struct Curl_easy *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070049
50 /* Point to the correct struct with this */
51 struct digestdata *digest;
Kristian Monsen5ab50182010-05-14 18:53:44 +010052
53 if(proxy) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070054 digest = &data->state.proxydigest;
Kristian Monsen5ab50182010-05-14 18:53:44 +010055 }
56 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070057 digest = &data->state.digest;
Kristian Monsen5ab50182010-05-14 18:53:44 +010058 }
59
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070060 if(!checkprefix("Digest", header))
61 return CURLE_BAD_CONTENT_ENCODING;
62
63 header += strlen("Digest");
Kristian Monsen5ab50182010-05-14 18:53:44 +010064 while(*header && ISSPACE(*header))
65 header++;
66
Alex Deymod15eaac2016-06-28 14:49:26 -070067 return Curl_auth_decode_digest_http_message(header, digest);
Kristian Monsen5ab50182010-05-14 18:53:44 +010068}
69
70CURLcode Curl_output_digest(struct connectdata *conn,
71 bool proxy,
72 const unsigned char *request,
73 const unsigned char *uripath)
74{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070075 CURLcode result;
Alex Deymoe3149cc2016-10-05 11:18:42 -070076 struct Curl_easy *data = conn->data;
Elliott Hughes82be86d2017-09-20 17:00:17 -070077 unsigned char *path = NULL;
78 char *tmp = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070079 char *response;
80 size_t len;
81 bool have_chlg;
Kristian Monsen5ab50182010-05-14 18:53:44 +010082
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070083 /* 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 */
Kristian Monsen5ab50182010-05-14 18:53:44 +010085 char **allocuserpwd;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070086
87 /* Point to the name and password for this */
Kristian Monsen5ab50182010-05-14 18:53:44 +010088 const char *userp;
89 const char *passwdp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070090
91 /* Point to the correct struct with this */
92 struct digestdata *digest;
Kristian Monsen5ab50182010-05-14 18:53:44 +010093 struct auth *authp;
94
Kristian Monsen5ab50182010-05-14 18:53:44 +010095 if(proxy) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070096 digest = &data->state.proxydigest;
Kristian Monsen5ab50182010-05-14 18:53:44 +010097 allocuserpwd = &conn->allocptr.proxyuserpwd;
Elliott Hughescee03382017-06-23 12:17:18 -070098 userp = conn->http_proxy.user;
99 passwdp = conn->http_proxy.passwd;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100100 authp = &data->state.authproxy;
101 }
102 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700103 digest = &data->state.digest;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100104 allocuserpwd = &conn->allocptr.userpwd;
105 userp = conn->user;
106 passwdp = conn->passwd;
107 authp = &data->state.authhost;
108 }
109
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700110 Curl_safefree(*allocuserpwd);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100111
112 /* not set means empty */
113 if(!userp)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700114 userp = "";
Kristian Monsen5ab50182010-05-14 18:53:44 +0100115
116 if(!passwdp)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700117 passwdp = "";
Kristian Monsen5ab50182010-05-14 18:53:44 +0100118
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700119#if defined(USE_WINDOWS_SSPI)
120 have_chlg = digest->input_token ? TRUE : FALSE;
121#else
122 have_chlg = digest->nonce ? TRUE : FALSE;
123#endif
124
125 if(!have_chlg) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100126 authp->done = FALSE;
127 return CURLE_OK;
128 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100129
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
133 sums to get sent.
134
135 Apache servers can be set to do the Digest IE-style automatically using
136 the BrowserMatch feature:
Alex Deymod15eaac2016-06-28 14:49:26 -0700137 https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
Kristian Monsen5ab50182010-05-14 18:53:44 +0100138
139 Further details on Digest implementation differences:
140 http://www.fngtps.com/2006/09/http-authentication
141 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700142
Elliott Hughes82be86d2017-09-20 17:00:17 -0700143 if(authp->iestyle) {
144 tmp = strchr((char *)uripath, '?');
145 if(tmp) {
146 size_t urilen = tmp - (char *)uripath;
147 path = (unsigned char *) aprintf("%.*s", urilen, uripath);
148 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100149 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700150 if(!tmp)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700151 path = (unsigned char *) strdup((char *) uripath);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100152
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700153 if(!path)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100154 return CURLE_OUT_OF_MEMORY;
155
Alex Deymod15eaac2016-06-28 14:49:26 -0700156 result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700157 path, digest, &response, &len);
158 free(path);
159 if(result)
160 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100161
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700162 *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
163 proxy ? "Proxy-" : "",
164 response);
165 free(response);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100166 if(!*allocuserpwd)
167 return CURLE_OUT_OF_MEMORY;
168
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700169 authp->done = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100170
171 return CURLE_OK;
172}
173
Alex Deymoe3149cc2016-10-05 11:18:42 -0700174void Curl_digest_cleanup(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100175{
Alex Deymod15eaac2016-06-28 14:49:26 -0700176 Curl_auth_digest_cleanup(&data->state.digest);
177 Curl_auth_digest_cleanup(&data->state.proxydigest);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100178}
179
180#endif