1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, 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.
22 ***************************************************************************/
25 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
26 /* -- WIN32 approved -- */
36 #include "curl_base64.h"
38 #include "http_digest.h"
40 #include "url.h" /* for Curl_safefree() */
41 #include "curl_memory.h"
42 #include "easyif.h" /* included for Curl_convert_... prototypes */
44 #define _MPRINTF_REPLACE /* use our functions only */
45 #include <curl/mprintf.h>
47 /* The last #include file should be: */
50 #define MAX_VALUE_LENGTH 256
51 #define MAX_CONTENT_LENGTH 1024
54 * Return 0 on success and then the buffers are filled in fine.
56 * Non-zero means failure to parse.
58 static int get_pair(const char *str, char *value, char *content,
62 bool starts_with_quote = FALSE;
65 for(c=MAX_VALUE_LENGTH-1; (*str && (*str != '=') && c--); )
74 /* this starts with a quote so it must end with one as well! */
76 starts_with_quote = TRUE;
79 for(c=MAX_CONTENT_LENGTH-1; *str && c--; str++) {
83 /* possibly the start of an escaped quote */
85 *content++ = '\\'; /* even though this is an escape character, we still
86 store it as-is in the target buffer */
91 if(!starts_with_quote) {
92 /* this signals the end of the content if we didn't get a starting quote
93 and then we do "sloppy" parsing */
104 if(!escape && starts_with_quote) {
118 return 0; /* all is fine! */
121 /* Test example headers:
123 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
124 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
128 CURLdigest Curl_input_digest(struct connectdata *conn,
130 const char *header) /* rest of the *-authenticate:
136 bool foundAuth = FALSE;
137 bool foundAuthInt = FALSE;
138 struct SessionHandle *data=conn->data;
139 bool before = FALSE; /* got a nonce before */
140 struct digestdata *d;
143 d = &data->state.proxydigest;
146 d = &data->state.digest;
149 /* skip initial whitespaces */
150 while(*header && ISSPACE(*header))
153 if(checkprefix("Digest", header)) {
154 header += strlen("Digest");
156 /* If we already have received a nonce, keep that in mind */
160 /* clear off any former leftovers and init to defaults */
161 Curl_digest_cleanup_one(d);
164 char value[MAX_VALUE_LENGTH];
165 char content[MAX_CONTENT_LENGTH];
167 while(*header && ISSPACE(*header))
170 /* extract a value=content pair */
171 if(!get_pair(header, value, content, &header)) {
172 if(Curl_raw_equal(value, "nonce")) {
173 d->nonce = strdup(content);
175 return CURLDIGEST_NOMEM;
177 else if(Curl_raw_equal(value, "stale")) {
178 if(Curl_raw_equal(content, "true")) {
180 d->nc = 1; /* we make a new nonce now */
183 else if(Curl_raw_equal(value, "realm")) {
184 d->realm = strdup(content);
186 return CURLDIGEST_NOMEM;
188 else if(Curl_raw_equal(value, "opaque")) {
189 d->opaque = strdup(content);
191 return CURLDIGEST_NOMEM;
193 else if(Curl_raw_equal(value, "qop")) {
195 /* tokenize the list and choose auth if possible, use a temporary
196 clone of the buffer since strtok_r() ruins it */
197 tmp = strdup(content);
199 return CURLDIGEST_NOMEM;
200 token = strtok_r(tmp, ",", &tok_buf);
201 while(token != NULL) {
202 if(Curl_raw_equal(token, "auth")) {
205 else if(Curl_raw_equal(token, "auth-int")) {
208 token = strtok_r(NULL, ",", &tok_buf);
211 /*select only auth o auth-int. Otherwise, ignore*/
213 d->qop = strdup("auth");
215 return CURLDIGEST_NOMEM;
217 else if(foundAuthInt) {
218 d->qop = strdup("auth-int");
220 return CURLDIGEST_NOMEM;
223 else if(Curl_raw_equal(value, "algorithm")) {
224 d->algorithm = strdup(content);
226 return CURLDIGEST_NOMEM;
227 if(Curl_raw_equal(content, "MD5-sess"))
228 d->algo = CURLDIGESTALGO_MD5SESS;
229 else if(Curl_raw_equal(content, "MD5"))
230 d->algo = CURLDIGESTALGO_MD5;
232 return CURLDIGEST_BADALGO;
235 /* unknown specifier, ignore it! */
239 break; /* we're done here */
241 /* pass all additional spaces here */
242 while(*header && ISSPACE(*header))
245 /* allow the list to be comma-separated */
248 /* We had a nonce since before, and we got another one now without
249 'stale=true'. This means we provided bad credentials in the previous
251 if(before && !d->stale)
252 return CURLDIGEST_BAD;
254 /* We got this header without a nonce, that's a bad Digest line! */
256 return CURLDIGEST_BAD;
259 /* else not a digest, get out */
260 return CURLDIGEST_NONE;
262 return CURLDIGEST_FINE;
265 /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
266 static void md5_to_ascii(unsigned char *source, /* 16 bytes */
267 unsigned char *dest) /* 33 bytes */
271 snprintf((char *)&dest[i*2], 3, "%02x", source[i]);
274 CURLcode Curl_output_digest(struct connectdata *conn,
276 const unsigned char *request,
277 const unsigned char *uripath)
279 /* We have a Digest setup for this, use it! Now, to get all the details for
280 this sorted out, I must urge you dear friend to read up on the RFC2617
282 unsigned char md5buf[16]; /* 16 bytes/128 bits */
283 unsigned char request_digest[33];
284 unsigned char *md5this;
286 unsigned char ha2[33];/* 32 digits and 1 zero byte */
297 struct SessionHandle *data = conn->data;
298 struct digestdata *d;
299 #ifdef CURL_DOES_CONVERSIONS
301 /* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
302 It converts digest text to ASCII so the MD5 will be correct for
303 what ultimately goes over the network.
305 #define CURL_OUTPUT_DIGEST_CONV(a, b) \
306 rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
307 if(rc != CURLE_OK) { \
312 #define CURL_OUTPUT_DIGEST_CONV(a, b)
313 #endif /* CURL_DOES_CONVERSIONS */
316 d = &data->state.proxydigest;
317 allocuserpwd = &conn->allocptr.proxyuserpwd;
318 userp = conn->proxyuser;
319 passwdp = conn->proxypasswd;
320 authp = &data->state.authproxy;
323 d = &data->state.digest;
324 allocuserpwd = &conn->allocptr.userpwd;
326 passwdp = conn->passwd;
327 authp = &data->state.authhost;
331 Curl_safefree(*allocuserpwd);
332 *allocuserpwd = NULL;
335 /* not set means empty */
352 /* Generate a cnonce */
354 snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", (long)now.tv_sec);
355 if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
358 return CURLE_OUT_OF_MEMORY;
362 if the algorithm is "MD5" or unspecified (which then defaults to MD5):
364 A1 = unq(username-value) ":" unq(realm-value) ":" passwd
366 if the algorithm is "MD5-sess" then:
368 A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
369 ":" unq(nonce-value) ":" unq(cnonce-value)
372 md5this = (unsigned char *)
373 aprintf("%s:%s:%s", userp, d->realm, passwdp);
375 return CURLE_OUT_OF_MEMORY;
377 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
378 Curl_md5it(md5buf, md5this);
379 free(md5this); /* free this again */
381 ha1 = malloc(33); /* 32 digits and 1 zero byte */
383 return CURLE_OUT_OF_MEMORY;
385 md5_to_ascii(md5buf, ha1);
387 if(d->algo == CURLDIGESTALGO_MD5SESS) {
388 /* nonce and cnonce are OUTSIDE the hash */
389 tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
391 return CURLE_OUT_OF_MEMORY;
392 CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
393 Curl_md5it(md5buf, (unsigned char *)tmp);
394 free(tmp); /* free this again */
395 md5_to_ascii(md5buf, ha1);
399 If the "qop" directive's value is "auth" or is unspecified, then A2 is:
401 A2 = Method ":" digest-uri-value
403 If the "qop" value is "auth-int", then A2 is:
405 A2 = Method ":" digest-uri-value ":" H(entity-body)
407 (The "Method" value is the HTTP request method as specified in section
411 /* So IE browsers < v7 cut off the URI part at the query part when they
412 evaluate the MD5 and some (IIS?) servers work with them so we may need to
413 do the Digest IE-style. Note that the different ways cause different MD5
416 Apache servers can be set to do the Digest IE-style automatically using
417 the BrowserMatch feature:
418 http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
420 Further details on Digest implementation differences:
421 http://www.fngtps.com/2006/09/http-authentication
423 if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
424 md5this = (unsigned char *)aprintf("%s:%.*s", request,
425 (int)(tmp - (char *)uripath), uripath);
428 md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
432 return CURLE_OUT_OF_MEMORY;
435 if(d->qop && Curl_raw_equal(d->qop, "auth-int")) {
436 /* We don't support auth-int at the moment. I can't see a easy way to get
438 /* TODO: Append H(entity-body)*/
440 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
441 Curl_md5it(md5buf, md5this);
442 free(md5this); /* free this again */
443 md5_to_ascii(md5buf, ha2);
446 md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
455 md5this = (unsigned char *)aprintf("%s:%s:%s",
462 return CURLE_OUT_OF_MEMORY;
464 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
465 Curl_md5it(md5buf, md5this);
466 free(md5this); /* free this again */
467 md5_to_ascii(md5buf, request_digest);
469 /* for test case 64 (snooped from a Mozilla 1.3a request)
471 Authorization: Digest username="testuser", realm="testrealm", \
472 nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
477 aprintf( "%sAuthorization: Digest "
490 uripath, /* this is the PATH part of the URL */
496 if(Curl_raw_equal(d->qop, "auth"))
497 d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
498 which tells to the server how many times you are using the
499 same nonce in the qop=auth mode. */
503 aprintf( "%sAuthorization: Digest "
513 uripath, /* this is the PATH part of the URL */
517 return CURLE_OUT_OF_MEMORY;
519 /* Add optional fields */
522 tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
524 return CURLE_OUT_OF_MEMORY;
530 /* append algorithm */
531 tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
533 return CURLE_OUT_OF_MEMORY;
538 /* append CRLF + zero (3 bytes) to the userpwd header */
539 tmp = realloc(*allocuserpwd, strlen(*allocuserpwd) + 3);
541 return CURLE_OUT_OF_MEMORY;
548 void Curl_digest_cleanup_one(struct digestdata *d)
575 d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
576 d->stale = FALSE; /* default means normal, not stale */
580 void Curl_digest_cleanup(struct SessionHandle *data)
582 Curl_digest_cleanup_one(&data->state.digest);
583 Curl_digest_cleanup_one(&data->state.proxydigest);