/***************************************************************************
- * _ _ ____ _
- * Project ___| | | | _ \| |
- * / __| | | | |_) | |
- * | (__| |_| | _ <| |___
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2003, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
- *
+ *
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
- * $Id$
***************************************************************************/
-#include "setup.h"
-#ifndef CURL_DISABLE_HTTP
-/* -- WIN32 approved -- */
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <ctype.h>
+#include "curl_setup.h"
-#include "urldata.h"
-#include "sendf.h"
-#include "strequal.h"
+#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
-#include "md5.h"
+#include "urldata.h"
+#include "rawstr.h"
+#include "curl_sasl.h"
#include "http_digest.h"
+#include "curl_memory.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
/* The last #include file should be: */
-#ifdef MALLOCDEBUG
#include "memdebug.h"
-#endif
-/* Test example header:
+/* Test example headers:
WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
+Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
*/
-CURLdigest Curl_input_digest(struct connectdata *conn,
- char *header) /* rest of the www-authenticate:
- header */
+CURLcode Curl_input_digest(struct connectdata *conn,
+ bool proxy,
+ const char *header) /* rest of the *-authenticate:
+ header */
{
- bool more = TRUE;
- struct SessionHandle *data=conn->data;
+ struct SessionHandle *data = conn->data;
- /* skip initial whitespaces */
- while(*header && isspace((int)*header))
- header++;
+ /* Point to the correct struct with this */
+ struct digestdata *digest;
- if(checkprefix("Digest", header)) {
- header += strlen("Digest");
-
- data->state.digest.algo = CURLDIGESTALGO_MD5; /* default algorithm */
-
- while(more) {
- char value[32];
- char content[128];
- int totlen=0;
-
- while(*header && isspace((int)*header))
- header++;
-
- /* how big can these strings be? */
- if(2 == sscanf(header, "%31[^=]=\"%127[^\"]\"",
- value, content)) {
- if(strequal(value, "nonce")) {
- data->state.digest.nonce = strdup(content);
- }
- else if(strequal(value, "cnonce")) {
- data->state.digest.cnonce = strdup(content);
- }
- else if(strequal(value, "realm")) {
- data->state.digest.realm = strdup(content);
- }
- else if(strequal(value, "algorithm")) {
- if(strequal(content, "MD5-sess"))
- data->state.digest.algo = CURLDIGESTALGO_MD5SESS;
- /* else, remain using the default md5 */
- }
- else {
- /* unknown specifier, ignore it! */
- }
- totlen = strlen(value)+strlen(content)+3;
- }
- else
- break; /* we're done here */
-
- header += totlen;
- if(',' == *header)
- /* allow the list to be comma-separated */
- header++;
- }
+ if(proxy) {
+ digest = &data->state.proxydigest;
+ }
+ else {
+ digest = &data->state.digest;
}
- else
- /* else not a digest, get out */
- return CURLDIGEST_NONE;
- return CURLDIGEST_FINE;
-}
+ if(!checkprefix("Digest", header))
+ return CURLE_BAD_CONTENT_ENCODING;
-/* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
-static void md5_to_ascii(unsigned char *source, /* 16 bytes */
- unsigned char *dest) /* 33 bytes */
-{
- int i;
- for(i=0; i<16; i++)
- sprintf((char *)&dest[i*2], "%02x", source[i]);
+ header += strlen("Digest");
+ while(*header && ISSPACE(*header))
+ header++;
+
+ return Curl_sasl_decode_digest_http_message(header, digest);
}
CURLcode Curl_output_digest(struct connectdata *conn,
- unsigned char *request,
- unsigned char *uripath)
+ bool proxy,
+ const unsigned char *request,
+ const unsigned char *uripath)
{
- /* We have a Digest setup for this, use it!
- Now, to get all the details for this sorted out, I must urge you dear friend
- to read up on the RFC2617 section 3.2.2, */
- unsigned char md5buf[16]; /* 16 bytes/128 bits */
- unsigned char ha1[33]; /* 32 digits and 1 zero byte */
- unsigned char ha2[33];
- unsigned char request_digest[33];
- char *md5this;
-
+ CURLcode result;
struct SessionHandle *data = conn->data;
+ unsigned char *path;
+ char *tmp;
+ char *response;
+ size_t len;
+ bool have_chlg;
+
+ /* Point to the address of the pointer that holds the string to send to the
+ server, which is for a plain host or for a HTTP proxy */
+ char **allocuserpwd;
+
+ /* Point to the name and password for this */
+ const char *userp;
+ const char *passwdp;
+
+ /* Point to the correct struct with this */
+ struct digestdata *digest;
+ struct auth *authp;
+
+ if(proxy) {
+ digest = &data->state.proxydigest;
+ allocuserpwd = &conn->allocptr.proxyuserpwd;
+ userp = conn->proxyuser;
+ passwdp = conn->proxypasswd;
+ authp = &data->state.authproxy;
+ }
+ else {
+ digest = &data->state.digest;
+ allocuserpwd = &conn->allocptr.userpwd;
+ userp = conn->user;
+ passwdp = conn->passwd;
+ authp = &data->state.authhost;
+ }
- /*
- if the algorithm is "MD5" or unspecified (which then defaults to MD5):
-
- A1 = unq(username-value) ":" unq(realm-value) ":" passwd
+ Curl_safefree(*allocuserpwd);
- if the algorithm is "MD5-sess" then:
+ /* not set means empty */
+ if(!userp)
+ userp = "";
- A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
- ":" unq(nonce-value) ":" unq(cnonce-value)
- */
- if(data->state.digest.algo == CURLDIGESTALGO_MD5SESS) {
- md5this = aprintf("%s:%s:%s:%s:%s",
- data->state.user,
- data->state.digest.realm,
- data->state.passwd,
- data->state.digest.nonce,
- data->state.digest.cnonce);
- }
- else {
- md5this = aprintf("%s:%s:%s",
- data->state.user,
- data->state.digest.realm,
- data->state.passwd);
+ if(!passwdp)
+ passwdp = "";
+
+#if defined(USE_WINDOWS_SSPI)
+ have_chlg = digest->input_token ? TRUE : FALSE;
+#else
+ have_chlg = digest->nonce ? TRUE : FALSE;
+#endif
+
+ if(!have_chlg) {
+ authp->done = FALSE;
+ return CURLE_OK;
}
- Curl_md5it(md5buf, md5this);
- free(md5this); /* free this again */
- md5_to_ascii(md5buf, ha1);
-
- /*
- A2 = Method ":" digest-uri-value
-
- (The "Method" value is the HTTP request method as specified in section
- 5.1.1 of RFC 2616)
+
+ /* So IE browsers < v7 cut off the URI part at the query part when they
+ evaluate the MD5 and some (IIS?) servers work with them so we may need to
+ do the Digest IE-style. Note that the different ways cause different MD5
+ sums to get sent.
+
+ Apache servers can be set to do the Digest IE-style automatically using
+ the BrowserMatch feature:
+ http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
+
+ Further details on Digest implementation differences:
+ http://www.fngtps.com/2006/09/http-authentication
*/
- md5this = aprintf("%s:%s", request, uripath);
- Curl_md5it(md5buf, md5this);
- free(md5this); /* free this again */
- md5_to_ascii(md5buf, ha2);
+ if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
+ size_t urilen = tmp - (char *)uripath;
+
+ path = (unsigned char *) aprintf("%.*s", urilen, uripath);
+ }
+ else
+ path = (unsigned char *) strdup((char *) uripath);
- md5this = aprintf("%s:%s:%s",
- ha1,
- data->state.digest.nonce,
- ha2);
- Curl_md5it(md5buf, md5this);
- free(md5this); /* free this again */
- md5_to_ascii(md5buf, request_digest);
+ if(!path)
+ return CURLE_OUT_OF_MEMORY;
- /* for test case 64 (snooped from a Mozilla 1.3a request)
+ result = Curl_sasl_create_digest_http_message(data, userp, passwdp, request,
+ path, digest, &response, &len);
+ free(path);
+ if(result)
+ return result;
- Authorization: Digest username="testuser", realm="testrealm", \
- nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
- */
+ *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
+ proxy ? "Proxy-" : "",
+ response);
+ free(response);
+ if(!*allocuserpwd)
+ return CURLE_OUT_OF_MEMORY;
- conn->allocptr.userpwd =
- aprintf( "Authorization: Digest "
- "username=\"%s\", "
- "realm=\"%s\", "
- "nonce=\"%s\", "
- "uri=\"%s\", "
- "response=\"%s\"\r\n",
- data->state.user,
- data->state.digest.realm,
- data->state.digest.nonce,
- uripath, /* this is the PATH part of the URL */
- request_digest );
+ authp->done = TRUE;
return CURLE_OK;
}
+void Curl_digest_cleanup(struct SessionHandle *data)
+{
+ Curl_sasl_digest_cleanup(&data->state.digest);
+ Curl_sasl_digest_cleanup(&data->state.proxydigest);
+}
+
#endif