1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 "non-ascii.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
92 quote 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;
298 /* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
299 It converts digest text to ASCII so the MD5 will be correct for
300 what ultimately goes over the network.
302 #define CURL_OUTPUT_DIGEST_CONV(a, b) \
303 rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
304 if(rc != CURLE_OK) { \
310 d = &data->state.proxydigest;
311 allocuserpwd = &conn->allocptr.proxyuserpwd;
312 userp = conn->proxyuser;
313 passwdp = conn->proxypasswd;
314 authp = &data->state.authproxy;
317 d = &data->state.digest;
318 allocuserpwd = &conn->allocptr.userpwd;
320 passwdp = conn->passwd;
321 authp = &data->state.authhost;
325 Curl_safefree(*allocuserpwd);
326 *allocuserpwd = NULL;
329 /* not set means empty */
346 /* Generate a cnonce */
348 snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", (long)now.tv_sec);
349 if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
352 return CURLE_OUT_OF_MEMORY;
356 if the algorithm is "MD5" or unspecified (which then defaults to MD5):
358 A1 = unq(username-value) ":" unq(realm-value) ":" passwd
360 if the algorithm is "MD5-sess" then:
362 A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
363 ":" unq(nonce-value) ":" unq(cnonce-value)
366 md5this = (unsigned char *)
367 aprintf("%s:%s:%s", userp, d->realm, passwdp);
369 return CURLE_OUT_OF_MEMORY;
371 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
372 Curl_md5it(md5buf, md5this);
373 free(md5this); /* free this again */
375 ha1 = malloc(33); /* 32 digits and 1 zero byte */
377 return CURLE_OUT_OF_MEMORY;
379 md5_to_ascii(md5buf, ha1);
381 if(d->algo == CURLDIGESTALGO_MD5SESS) {
382 /* nonce and cnonce are OUTSIDE the hash */
383 tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
385 return CURLE_OUT_OF_MEMORY;
386 CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
387 Curl_md5it(md5buf, (unsigned char *)tmp);
388 free(tmp); /* free this again */
389 md5_to_ascii(md5buf, ha1);
393 If the "qop" directive's value is "auth" or is unspecified, then A2 is:
395 A2 = Method ":" digest-uri-value
397 If the "qop" value is "auth-int", then A2 is:
399 A2 = Method ":" digest-uri-value ":" H(entity-body)
401 (The "Method" value is the HTTP request method as specified in section
405 /* So IE browsers < v7 cut off the URI part at the query part when they
406 evaluate the MD5 and some (IIS?) servers work with them so we may need to
407 do the Digest IE-style. Note that the different ways cause different MD5
410 Apache servers can be set to do the Digest IE-style automatically using
411 the BrowserMatch feature:
412 http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
414 Further details on Digest implementation differences:
415 http://www.fngtps.com/2006/09/http-authentication
417 if(authp->iestyle && ((tmp = strchr((char *)uripath, '?')) != NULL)) {
418 md5this = (unsigned char *)aprintf("%s:%.*s", request,
419 (int)(tmp - (char *)uripath), uripath);
422 md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
426 return CURLE_OUT_OF_MEMORY;
429 if(d->qop && Curl_raw_equal(d->qop, "auth-int")) {
430 /* We don't support auth-int at the moment. I can't see a easy way to get
432 /* TODO: Append H(entity-body)*/
434 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
435 Curl_md5it(md5buf, md5this);
436 free(md5this); /* free this again */
437 md5_to_ascii(md5buf, ha2);
440 md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
449 md5this = (unsigned char *)aprintf("%s:%s:%s",
456 return CURLE_OUT_OF_MEMORY;
458 CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
459 Curl_md5it(md5buf, md5this);
460 free(md5this); /* free this again */
461 md5_to_ascii(md5buf, request_digest);
463 /* for test case 64 (snooped from a Mozilla 1.3a request)
465 Authorization: Digest username="testuser", realm="testrealm", \
466 nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
471 aprintf( "%sAuthorization: Digest "
484 uripath, /* this is the PATH part of the URL */
490 if(Curl_raw_equal(d->qop, "auth"))
491 d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
492 which tells to the server how many times you are using the
493 same nonce in the qop=auth mode. */
497 aprintf( "%sAuthorization: Digest "
507 uripath, /* this is the PATH part of the URL */
511 return CURLE_OUT_OF_MEMORY;
513 /* Add optional fields */
516 tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
518 return CURLE_OUT_OF_MEMORY;
524 /* append algorithm */
525 tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
527 return CURLE_OUT_OF_MEMORY;
532 /* append CRLF + zero (3 bytes) to the userpwd header */
533 tmp = realloc(*allocuserpwd, strlen(*allocuserpwd) + 3);
535 return CURLE_OUT_OF_MEMORY;
542 void Curl_digest_cleanup_one(struct digestdata *d)
569 d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
570 d->stale = FALSE; /* default means normal, not stale */
574 void Curl_digest_cleanup(struct SessionHandle *data)
576 Curl_digest_cleanup_one(&data->state.digest);
577 Curl_digest_cleanup_one(&data->state.proxydigest);