1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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.
21 ***************************************************************************/
24 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
25 /* -- WIN32 approved -- */
35 #include "curl_base64.h"
37 #include "http_digest.h"
39 #include "url.h" /* for Curl_safefree() */
40 #include "curl_memory.h"
41 #include "easyif.h" /* included for Curl_convert_... prototypes */
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
46 /* The last #include file should be: */
49 #define MAX_VALUE_LENGTH 256
50 #define MAX_CONTENT_LENGTH 1024
53 * Return 0 on success and then the buffers are filled in fine.
55 * Non-zero means failure to parse.
57 static int get_pair(const char *str, char *value, char *content,
61 bool starts_with_quote = FALSE;
64 for(c=MAX_VALUE_LENGTH-1; (*str && (*str != '=') && c--); )
73 /* this starts with a quote so it must end with one as well! */
75 starts_with_quote = TRUE;
78 for(c=MAX_CONTENT_LENGTH-1; *str && c--; str++) {
82 /* possibly the start of an escaped quote */
84 *content++ = '\\'; /* even though this is an escape character, we still
85 store it as-is in the target buffer */
90 if(!starts_with_quote) {
91 /* this signals the end of the content if we didn't get a starting quote
92 and then we do "sloppy" parsing */
103 if(!escape && starts_with_quote) {
117 return 0; /* all is fine! */
120 /* Test example headers:
122 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
123 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
127 CURLdigest Curl_input_digest(struct connectdata *conn,
129 const char *header) /* rest of the *-authenticate:
134 bool foundAuth = FALSE;
135 bool foundAuthInt = FALSE;
136 struct SessionHandle *data=conn->data;
137 bool before = FALSE; /* got a nonce before */
138 struct digestdata *d;
141 d = &data->state.proxydigest;
144 d = &data->state.digest;
147 /* skip initial whitespaces */
148 while(*header && ISSPACE(*header))
151 if(checkprefix("Digest", header)) {
152 header += strlen("Digest");
154 /* If we already have received a nonce, keep that in mind */
158 /* clear off any former leftovers and init to defaults */
159 Curl_digest_cleanup_one(d);
162 char value[MAX_VALUE_LENGTH];
163 char content[MAX_CONTENT_LENGTH];
165 while(*header && ISSPACE(*header))
168 /* extract a value=content pair */
169 if(!get_pair(header, value, content, &header)) {
170 if(Curl_raw_equal(value, "nonce")) {
171 d->nonce = strdup(content);
173 return CURLDIGEST_NOMEM;
175 else if(Curl_raw_equal(value, "stale")) {
176 if(Curl_raw_equal(content, "true")) {
178 d->nc = 1; /* we make a new nonce now */
181 else if(Curl_raw_equal(value, "realm")) {
182 d->realm = strdup(content);
184 return CURLDIGEST_NOMEM;
186 else if(Curl_raw_equal(value, "opaque")) {
187 d->opaque = strdup(content);
189 return CURLDIGEST_NOMEM;
191 else if(Curl_raw_equal(value, "qop")) {
193 /* tokenize the list and choose auth if possible, use a temporary
194 clone of the buffer since strtok_r() ruins it */
195 tmp = strdup(content);
197 return CURLDIGEST_NOMEM;
198 token = strtok_r(tmp, ",", &tok_buf);
199 while(token != NULL) {
200 if(Curl_raw_equal(token, "auth")) {
203 else if(Curl_raw_equal(token, "auth-int")) {
206 token = strtok_r(NULL, ",", &tok_buf);
209 /*select only auth o auth-int. Otherwise, ignore*/
211 d->qop = strdup("auth");
213 return CURLDIGEST_NOMEM;
215 else if(foundAuthInt) {
216 d->qop = strdup("auth-int");
218 return CURLDIGEST_NOMEM;
221 else if(Curl_raw_equal(value, "algorithm")) {
222 d->algorithm = strdup(content);
224 return CURLDIGEST_NOMEM;
225 if(Curl_raw_equal(content, "MD5-sess"))
226 d->algo = CURLDIGESTALGO_MD5SESS;
227 else if(Curl_raw_equal(content, "MD5"))
228 d->algo = CURLDIGESTALGO_MD5;
230 return CURLDIGEST_BADALGO;
233 /* unknown specifier, ignore it! */
237 break; /* we're done here */
239 /* pass all additional spaces here */
240 while(*header && ISSPACE(*header))
243 /* allow the list to be comma-separated */
246 /* We had a nonce since before, and we got another one now without
247 'stale=true'. This means we provided bad credentials in the previous
249 if(before && !d->stale)
250 return CURLDIGEST_BAD;
252 /* We got this header without a nonce, that's a bad Digest line! */
254 return CURLDIGEST_BAD;
257 /* else not a digest, get out */
258 return CURLDIGEST_NONE;
260 return CURLDIGEST_FINE;
263 /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
264 static void md5_to_ascii(unsigned char *source, /* 16 bytes */
265 unsigned char *dest) /* 33 bytes */
269 snprintf((char *)&dest[i*2], 3, "%02x", source[i]);
272 CURLcode Curl_output_digest(struct connectdata *conn,
274 const unsigned char *request,
275 const unsigned char *uripath)
277 /* We have a Digest setup for this, use it! Now, to get all the details for
278 this sorted out, I must urge you dear friend to read up on the RFC2617
280 unsigned char md5buf[16]; /* 16 bytes/128 bits */
281 unsigned char request_digest[33];
282 unsigned char *md5this;
284 unsigned char ha2[33];/* 32 digits and 1 zero byte */
295 struct SessionHandle *data = conn->data;
296 struct digestdata *d;
297 #ifdef CURL_DOES_CONVERSIONS
299 /* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
300 It converts digest text to ASCII so the MD5 will be correct for
301 what ultimately goes over the network.
303 #define CURL_OUTPUT_DIGEST_CONV(a, b) \
304 rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
305 if(rc != CURLE_OK) { \
310 #define CURL_OUTPUT_DIGEST_CONV(a, b)
311 #endif /* CURL_DOES_CONVERSIONS */
314 d = &data->state.proxydigest;
315 allocuserpwd = &conn->allocptr.proxyuserpwd;
316 userp = conn->proxyuser;
317 passwdp = conn->proxypasswd;
318 authp = &data->state.authproxy;
321 d = &data->state.digest;
322 allocuserpwd = &conn->allocptr.userpwd;
324 passwdp = conn->passwd;
325 authp = &data->state.authhost;
329 Curl_safefree(*allocuserpwd);
330 *allocuserpwd = NULL;
333 /* not set means empty */
350 /* Generate a cnonce */
352 snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", (long)now.tv_sec);
353 if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
356 return CURLE_OUT_OF_MEMORY;
360 if the algorithm is "MD5" or unspecified (which then defaults to MD5):
362 A1 = unq(username-value) ":" unq(realm-value) ":" passwd
364 if the algorithm is "MD5-sess" then:
366 A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
367 ":" unq(nonce-value) ":" unq(cnonce-value)
370 md5this = (unsigned char *)
371 aprintf("%s:%s:%s", userp, d->realm, passwdp);
373 return CURLE_OUT_OF_MEMORY;
375 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
376 Curl_md5it(md5buf, md5this);
377 free(md5this); /* free this again */
379 ha1 = malloc(33); /* 32 digits and 1 zero byte */
381 return CURLE_OUT_OF_MEMORY;
383 md5_to_ascii(md5buf, ha1);
385 if(d->algo == CURLDIGESTALGO_MD5SESS) {
386 /* nonce and cnonce are OUTSIDE the hash */
387 tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
389 return CURLE_OUT_OF_MEMORY;
390 CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
391 Curl_md5it(md5buf, (unsigned char *)tmp);
392 free(tmp); /* free this again */
393 md5_to_ascii(md5buf, ha1);
397 If the "qop" directive's value is "auth" or is unspecified, then A2 is:
399 A2 = Method ":" digest-uri-value
401 If the "qop" value is "auth-int", then A2 is:
403 A2 = Method ":" digest-uri-value ":" H(entity-body)
405 (The "Method" value is the HTTP request method as specified in section
409 /* So IE browsers < v7 cut off the URI part at the query part when they
410 evaluate the MD5 and some (IIS?) servers work with them so we may need to
411 do the Digest IE-style. Note that the different ways cause different MD5
414 Apache servers can be set to do the Digest IE-style automatically using
415 the BrowserMatch feature:
416 http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
418 Further details on Digest implementation differences:
419 http://www.fngtps.com/2006/09/http-authentication
421 if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
422 md5this = (unsigned char *)aprintf("%s:%.*s", request,
423 (int)(tmp - (char *)uripath), uripath);
426 md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
430 return CURLE_OUT_OF_MEMORY;
433 if(d->qop && Curl_raw_equal(d->qop, "auth-int")) {
434 /* We don't support auth-int at the moment. I can't see a easy way to get
436 /* TODO: Append H(entity-body)*/
438 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
439 Curl_md5it(md5buf, md5this);
440 free(md5this); /* free this again */
441 md5_to_ascii(md5buf, ha2);
444 md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
453 md5this = (unsigned char *)aprintf("%s:%s:%s",
460 return CURLE_OUT_OF_MEMORY;
462 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
463 Curl_md5it(md5buf, md5this);
464 free(md5this); /* free this again */
465 md5_to_ascii(md5buf, request_digest);
467 /* for test case 64 (snooped from a Mozilla 1.3a request)
469 Authorization: Digest username="testuser", realm="testrealm", \
470 nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
475 aprintf( "%sAuthorization: Digest "
488 uripath, /* this is the PATH part of the URL */
494 if(Curl_raw_equal(d->qop, "auth"))
495 d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
496 which tells to the server how many times you are using the
497 same nonce in the qop=auth mode. */
501 aprintf( "%sAuthorization: Digest "
511 uripath, /* this is the PATH part of the URL */
515 return CURLE_OUT_OF_MEMORY;
517 /* Add optional fields */
520 tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
522 return CURLE_OUT_OF_MEMORY;
528 /* append algorithm */
529 tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
531 return CURLE_OUT_OF_MEMORY;
536 /* append CRLF + zero (3 bytes) to the userpwd header */
537 tmp = realloc(*allocuserpwd, strlen(*allocuserpwd) + 3);
539 return CURLE_OUT_OF_MEMORY;
546 void Curl_digest_cleanup_one(struct digestdata *d)
573 d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
574 d->stale = FALSE; /* default means normal, not stale */
578 void Curl_digest_cleanup(struct SessionHandle *data)
580 Curl_digest_cleanup_one(&data->state.digest);
581 Curl_digest_cleanup_one(&data->state.proxydigest);