ba59e5d14824ab70b9328761caa36df8e84bb958
[platform/upstream/curl.git] / lib / http_digest.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
26
27 #include "urldata.h"
28 #include "rawstr.h"
29 #include "curl_sasl.h"
30 #include "http_digest.h"
31 #include "curl_memory.h"
32
33 #define _MPRINTF_REPLACE /* use our functions only */
34 #include <curl/mprintf.h>
35
36 /* The last #include file should be: */
37 #include "memdebug.h"
38
39 /* Test example headers:
40
41 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
42 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
43
44 */
45
46 CURLcode Curl_input_digest(struct connectdata *conn,
47                            bool proxy,
48                            const char *header) /* rest of the *-authenticate:
49                                                   header */
50 {
51   struct SessionHandle *data = conn->data;
52
53   /* Point to the correct struct with this */
54   struct digestdata *digest;
55
56   if(proxy) {
57     digest = &data->state.proxydigest;
58   }
59   else {
60     digest = &data->state.digest;
61   }
62
63   if(!checkprefix("Digest", header))
64     return CURLE_BAD_CONTENT_ENCODING;
65
66   header += strlen("Digest");
67   while(*header && ISSPACE(*header))
68     header++;
69
70   return Curl_sasl_decode_digest_http_message(header, digest);
71 }
72
73 CURLcode Curl_output_digest(struct connectdata *conn,
74                             bool proxy,
75                             const unsigned char *request,
76                             const unsigned char *uripath)
77 {
78   CURLcode result;
79   struct SessionHandle *data = conn->data;
80   unsigned char *path;
81   char *tmp;
82   char *response;
83   size_t len;
84   bool have_chlg;
85
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 */
88   char **allocuserpwd;
89
90   /* Point to the name and password for this */
91   const char *userp;
92   const char *passwdp;
93
94   /* Point to the correct struct with this */
95   struct digestdata *digest;
96   struct auth *authp;
97
98   if(proxy) {
99     digest = &data->state.proxydigest;
100     allocuserpwd = &conn->allocptr.proxyuserpwd;
101     userp = conn->proxyuser;
102     passwdp = conn->proxypasswd;
103     authp = &data->state.authproxy;
104   }
105   else {
106     digest = &data->state.digest;
107     allocuserpwd = &conn->allocptr.userpwd;
108     userp = conn->user;
109     passwdp = conn->passwd;
110     authp = &data->state.authhost;
111   }
112
113   Curl_safefree(*allocuserpwd);
114
115   /* not set means empty */
116   if(!userp)
117     userp = "";
118
119   if(!passwdp)
120     passwdp = "";
121
122 #if defined(USE_WINDOWS_SSPI)
123   have_chlg = digest->input_token ? TRUE : FALSE;
124 #else
125   have_chlg = digest->nonce ? TRUE : FALSE;
126 #endif
127
128   if(!have_chlg) {
129     authp->done = FALSE;
130     return CURLE_OK;
131   }
132
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
136      sums to get sent.
137
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
141
142      Further details on Digest implementation differences:
143      http://www.fngtps.com/2006/09/http-authentication
144   */
145
146   if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
147     size_t urilen = tmp - (char *)uripath;
148
149     path = (unsigned char *) aprintf("%.*s", urilen, uripath);
150   }
151   else
152     path = (unsigned char *) strdup((char *) uripath);
153
154   if(!path)
155     return CURLE_OUT_OF_MEMORY;
156
157   result = Curl_sasl_create_digest_http_message(data, userp, passwdp, request,
158                                                 path, digest, &response, &len);
159   free(path);
160   if(result)
161     return result;
162
163   *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
164                           proxy ? "Proxy-" : "",
165                           response);
166   free(response);
167   if(!*allocuserpwd)
168     return CURLE_OUT_OF_MEMORY;
169
170   authp->done = TRUE;
171
172   return CURLE_OK;
173 }
174
175 void Curl_digest_cleanup(struct SessionHandle *data)
176 {
177   Curl_sasl_digest_cleanup(&data->state.digest);
178   Curl_sasl_digest_cleanup(&data->state.proxydigest);
179 }
180
181 #endif