2003-05-23 00:09:54 +08:00
|
|
|
/***************************************************************************
|
2004-04-29 16:18:32 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-05-23 00:09:54 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2008-01-10 17:17:07 +08:00
|
|
|
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-05-23 00:09:54 +08:00
|
|
|
*
|
|
|
|
* 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.
|
2004-04-29 16:18:32 +08:00
|
|
|
*
|
2003-05-23 00:09:54 +08:00
|
|
|
* 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"
|
|
|
|
|
2004-11-12 17:18:14 +08:00
|
|
|
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
|
2003-05-23 00:09:54 +08:00
|
|
|
/* -- WIN32 approved -- */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "strequal.h"
|
2004-04-29 16:18:32 +08:00
|
|
|
#include "base64.h"
|
2003-05-23 00:09:54 +08:00
|
|
|
#include "md5.h"
|
|
|
|
#include "http_digest.h"
|
2004-04-29 16:18:32 +08:00
|
|
|
#include "strtok.h"
|
2004-03-31 19:55:56 +08:00
|
|
|
#include "url.h" /* for Curl_safefree() */
|
2004-05-11 19:30:23 +08:00
|
|
|
#include "memory.h"
|
2007-01-14 22:57:51 +08:00
|
|
|
#include "easyif.h" /* included for Curl_convert_... prototypes */
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2004-05-04 15:52:53 +08:00
|
|
|
/* Test example headers:
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
|
2004-05-04 15:52:53 +08:00
|
|
|
Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
CURLdigest Curl_input_digest(struct connectdata *conn,
|
2004-05-04 15:52:53 +08:00
|
|
|
bool proxy,
|
2007-08-27 14:31:28 +08:00
|
|
|
const char *header) /* rest of the *-authenticate:
|
|
|
|
header */
|
2003-05-23 00:09:54 +08:00
|
|
|
{
|
|
|
|
bool more = TRUE;
|
2004-04-29 16:18:32 +08:00
|
|
|
char *token = NULL;
|
|
|
|
char *tmp = NULL;
|
|
|
|
bool foundAuth = FALSE;
|
|
|
|
bool foundAuthInt = FALSE;
|
2003-05-23 00:09:54 +08:00
|
|
|
struct SessionHandle *data=conn->data;
|
2004-03-30 21:00:32 +08:00
|
|
|
bool before = FALSE; /* got a nonce before */
|
2004-05-04 15:52:53 +08:00
|
|
|
struct digestdata *d;
|
2004-07-31 15:36:01 +08:00
|
|
|
|
2004-05-04 15:52:53 +08:00
|
|
|
if(proxy) {
|
|
|
|
d = &data->state.proxydigest;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d = &data->state.digest;
|
|
|
|
}
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
/* skip initial whitespaces */
|
2006-10-18 05:32:56 +08:00
|
|
|
while(*header && ISSPACE(*header))
|
2003-05-23 00:09:54 +08:00
|
|
|
header++;
|
|
|
|
|
|
|
|
if(checkprefix("Digest", header)) {
|
|
|
|
header += strlen("Digest");
|
|
|
|
|
2004-03-30 21:00:32 +08:00
|
|
|
/* If we already have received a nonce, keep that in mind */
|
2004-03-31 19:55:07 +08:00
|
|
|
if(d->nonce)
|
2004-03-30 21:00:32 +08:00
|
|
|
before = TRUE;
|
|
|
|
|
2003-05-23 06:39:38 +08:00
|
|
|
/* clear off any former leftovers and init to defaults */
|
2004-05-04 15:52:53 +08:00
|
|
|
Curl_digest_cleanup_one(d);
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
while(more) {
|
2007-11-30 06:14:48 +08:00
|
|
|
char value[256];
|
|
|
|
char content[1024];
|
2004-03-08 20:37:11 +08:00
|
|
|
size_t totlen=0;
|
2003-05-23 00:09:54 +08:00
|
|
|
|
2006-10-18 05:32:56 +08:00
|
|
|
while(*header && ISSPACE(*header))
|
2003-05-23 00:09:54 +08:00
|
|
|
header++;
|
2004-04-29 16:18:32 +08:00
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
/* how big can these strings be? */
|
2007-11-30 06:14:48 +08:00
|
|
|
if((2 == sscanf(header, "%255[^=]=\"%1023[^\"]\"",
|
2004-03-31 19:55:07 +08:00
|
|
|
value, content)) ||
|
|
|
|
/* try the same scan but without quotes around the content but don't
|
2007-02-27 06:03:01 +08:00
|
|
|
include the possibly trailing comma, newline or carriage return */
|
2007-11-30 06:14:48 +08:00
|
|
|
(2 == sscanf(header, "%255[^=]=%1023[^\r\n,]",
|
2004-03-31 19:55:07 +08:00
|
|
|
value, content)) ) {
|
2003-05-23 00:09:54 +08:00
|
|
|
if(strequal(value, "nonce")) {
|
2004-03-31 19:55:07 +08:00
|
|
|
d->nonce = strdup(content);
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->nonce)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
2004-03-30 21:00:32 +08:00
|
|
|
else if(strequal(value, "stale")) {
|
2004-04-29 16:18:32 +08:00
|
|
|
if(strequal(content, "true")) {
|
2004-03-31 19:55:07 +08:00
|
|
|
d->stale = TRUE;
|
2004-04-29 16:18:32 +08:00
|
|
|
d->nc = 1; /* we make a new nonce now */
|
|
|
|
}
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
|
|
|
else if(strequal(value, "realm")) {
|
2004-03-31 19:55:07 +08:00
|
|
|
d->realm = strdup(content);
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->realm)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
2004-04-29 16:18:32 +08:00
|
|
|
else if(strequal(value, "opaque")) {
|
|
|
|
d->opaque = strdup(content);
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->opaque)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
else if(strequal(value, "qop")) {
|
|
|
|
char *tok_buf;
|
|
|
|
/* tokenize the list and choose auth if possible, use a temporary
|
|
|
|
clone of the buffer since strtok_r() ruins it */
|
|
|
|
tmp = strdup(content);
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!tmp)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2004-04-29 16:18:32 +08:00
|
|
|
token = strtok_r(tmp, ",", &tok_buf);
|
2007-11-07 17:21:35 +08:00
|
|
|
while(token != NULL) {
|
|
|
|
if(strequal(token, "auth")) {
|
2004-04-29 16:18:32 +08:00
|
|
|
foundAuth = TRUE;
|
|
|
|
}
|
2007-11-07 17:21:35 +08:00
|
|
|
else if(strequal(token, "auth-int")) {
|
2004-04-29 16:18:32 +08:00
|
|
|
foundAuthInt = TRUE;
|
|
|
|
}
|
2004-04-30 13:53:42 +08:00
|
|
|
token = strtok_r(NULL, ",", &tok_buf);
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
/*select only auth o auth-int. Otherwise, ignore*/
|
2007-11-07 17:21:35 +08:00
|
|
|
if(foundAuth) {
|
2004-04-29 16:18:32 +08:00
|
|
|
d->qop = strdup("auth");
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->qop)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
2007-11-07 17:21:35 +08:00
|
|
|
else if(foundAuthInt) {
|
2004-04-29 16:18:32 +08:00
|
|
|
d->qop = strdup("auth-int");
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->qop)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
2003-05-23 00:09:54 +08:00
|
|
|
else if(strequal(value, "algorithm")) {
|
2004-04-29 16:18:32 +08:00
|
|
|
d->algorithm = strdup(content);
|
2004-05-13 22:14:03 +08:00
|
|
|
if(!d->algorithm)
|
|
|
|
return CURLDIGEST_NOMEM;
|
2003-05-23 00:09:54 +08:00
|
|
|
if(strequal(content, "MD5-sess"))
|
2004-03-31 19:55:07 +08:00
|
|
|
d->algo = CURLDIGESTALGO_MD5SESS;
|
|
|
|
else if(strequal(content, "MD5"))
|
|
|
|
d->algo = CURLDIGESTALGO_MD5;
|
|
|
|
else
|
|
|
|
return CURLDIGEST_BADALGO;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* unknown specifier, ignore it! */
|
|
|
|
}
|
2004-07-31 15:36:01 +08:00
|
|
|
totlen = strlen(value)+strlen(content)+1;
|
|
|
|
|
|
|
|
if(header[strlen(value)+1] == '\"')
|
|
|
|
/* the contents were within quotes, then add 2 for them to the
|
|
|
|
length */
|
|
|
|
totlen += 2;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
2004-04-29 16:18:32 +08:00
|
|
|
else
|
2003-05-23 00:09:54 +08:00
|
|
|
break; /* we're done here */
|
2004-04-29 16:18:32 +08:00
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
header += totlen;
|
2008-01-10 17:17:07 +08:00
|
|
|
/* pass all additional spaces here */
|
|
|
|
while(*header && ISSPACE(*header))
|
|
|
|
header++;
|
2003-05-23 00:09:54 +08:00
|
|
|
if(',' == *header)
|
|
|
|
/* allow the list to be comma-separated */
|
2004-04-29 16:18:32 +08:00
|
|
|
header++;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
2004-03-30 21:00:32 +08:00
|
|
|
/* We had a nonce since before, and we got another one now without
|
2004-04-29 16:18:32 +08:00
|
|
|
'stale=true'. This means we provided bad credentials in the previous
|
|
|
|
request */
|
2004-03-31 19:55:07 +08:00
|
|
|
if(before && !d->stale)
|
2004-03-30 21:00:32 +08:00
|
|
|
return CURLDIGEST_BAD;
|
2003-06-26 19:22:12 +08:00
|
|
|
|
2004-03-30 21:00:32 +08:00
|
|
|
/* We got this header without a nonce, that's a bad Digest line! */
|
2004-03-31 19:55:07 +08:00
|
|
|
if(!d->nonce)
|
2003-06-26 19:22:12 +08:00
|
|
|
return CURLDIGEST_BAD;
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
2004-04-29 16:18:32 +08:00
|
|
|
else
|
2003-05-23 00:09:54 +08:00
|
|
|
/* else not a digest, get out */
|
|
|
|
return CURLDIGEST_NONE;
|
|
|
|
|
|
|
|
return CURLDIGEST_FINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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++)
|
2004-06-24 19:54:11 +08:00
|
|
|
snprintf((char *)&dest[i*2], 3, "%02x", source[i]);
|
2003-05-23 00:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_output_digest(struct connectdata *conn,
|
2004-05-04 15:52:53 +08:00
|
|
|
bool proxy,
|
2007-08-27 14:31:28 +08:00
|
|
|
const unsigned char *request,
|
|
|
|
const unsigned char *uripath)
|
2003-05-23 00:09:54 +08:00
|
|
|
{
|
2003-09-02 15:48:58 +08:00
|
|
|
/* 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, */
|
2003-05-23 00:09:54 +08:00
|
|
|
unsigned char md5buf[16]; /* 16 bytes/128 bits */
|
|
|
|
unsigned char request_digest[33];
|
2003-05-23 00:12:30 +08:00
|
|
|
unsigned char *md5this;
|
2004-04-29 16:18:32 +08:00
|
|
|
unsigned char *ha1;
|
|
|
|
unsigned char ha2[33];/* 32 digits and 1 zero byte */
|
|
|
|
char cnoncebuf[7];
|
|
|
|
char *cnonce;
|
|
|
|
char *tmp = NULL;
|
|
|
|
struct timeval now;
|
2004-06-03 18:42:20 +08:00
|
|
|
|
|
|
|
char **allocuserpwd;
|
|
|
|
char *userp;
|
|
|
|
char *passwdp;
|
2004-05-04 15:52:53 +08:00
|
|
|
struct auth *authp;
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
struct SessionHandle *data = conn->data;
|
2004-05-04 15:52:53 +08:00
|
|
|
struct digestdata *d;
|
2007-01-14 22:57:51 +08:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
|
|
|
CURLcode rc;
|
|
|
|
/* The CURL_OUTPUT_DIGEST_CONV macro below is for non-ASCII machines.
|
|
|
|
It converts digest text to ASCII so the MD5 will be correct for
|
|
|
|
what ultimately goes over the network.
|
|
|
|
*/
|
|
|
|
#define CURL_OUTPUT_DIGEST_CONV(a, b) \
|
|
|
|
rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
|
2007-11-07 17:21:35 +08:00
|
|
|
if(rc != CURLE_OK) { \
|
2007-01-14 22:57:51 +08:00
|
|
|
free(b); \
|
|
|
|
return rc; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define CURL_OUTPUT_DIGEST_CONV(a, b)
|
|
|
|
#endif /* CURL_DOES_CONVERSIONS */
|
2004-05-04 15:52:53 +08:00
|
|
|
|
|
|
|
if(proxy) {
|
|
|
|
d = &data->state.proxydigest;
|
2004-06-03 18:42:20 +08:00
|
|
|
allocuserpwd = &conn->allocptr.proxyuserpwd;
|
|
|
|
userp = conn->proxyuser;
|
|
|
|
passwdp = conn->proxypasswd;
|
2004-05-04 15:52:53 +08:00
|
|
|
authp = &data->state.authproxy;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d = &data->state.digest;
|
2004-06-03 18:42:20 +08:00
|
|
|
allocuserpwd = &conn->allocptr.userpwd;
|
|
|
|
userp = conn->user;
|
|
|
|
passwdp = conn->passwd;
|
2004-05-04 15:52:53 +08:00
|
|
|
authp = &data->state.authhost;
|
|
|
|
}
|
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(*allocuserpwd) {
|
2007-07-22 18:17:52 +08:00
|
|
|
Curl_safefree(*allocuserpwd);
|
|
|
|
*allocuserpwd = NULL;
|
|
|
|
}
|
|
|
|
|
2004-06-03 18:42:20 +08:00
|
|
|
/* not set means empty */
|
|
|
|
if(!userp)
|
|
|
|
userp=(char *)"";
|
|
|
|
|
|
|
|
if(!passwdp)
|
|
|
|
passwdp=(char *)"";
|
|
|
|
|
2004-05-04 15:52:53 +08:00
|
|
|
if(!d->nonce) {
|
|
|
|
authp->done = FALSE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
authp->done = TRUE;
|
2003-05-23 00:09:54 +08:00
|
|
|
|
2004-04-29 16:18:32 +08:00
|
|
|
if(!d->nc)
|
|
|
|
d->nc = 1;
|
|
|
|
|
|
|
|
if(!d->cnonce) {
|
|
|
|
/* Generate a cnonce */
|
|
|
|
now = Curl_tvnow();
|
2004-05-06 15:21:19 +08:00
|
|
|
snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", now.tv_sec);
|
2007-01-04 07:04:38 +08:00
|
|
|
if(Curl_base64_encode(data, cnoncebuf, strlen(cnoncebuf), &cnonce))
|
2004-05-12 21:24:40 +08:00
|
|
|
d->cnonce = cnonce;
|
|
|
|
else
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
/*
|
|
|
|
if the algorithm is "MD5" or unspecified (which then defaults to MD5):
|
2004-04-29 16:18:32 +08:00
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
A1 = unq(username-value) ":" unq(realm-value) ":" passwd
|
|
|
|
|
|
|
|
if the algorithm is "MD5-sess" then:
|
|
|
|
|
|
|
|
A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
|
|
|
|
":" unq(nonce-value) ":" unq(cnonce-value)
|
|
|
|
*/
|
2004-04-29 16:18:32 +08:00
|
|
|
|
|
|
|
md5this = (unsigned char *)
|
2004-06-03 18:42:20 +08:00
|
|
|
aprintf("%s:%s:%s", userp, d->realm, passwdp);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!md5this)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2007-01-14 22:57:51 +08:00
|
|
|
|
|
|
|
CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
|
2003-05-23 00:09:54 +08:00
|
|
|
Curl_md5it(md5buf, md5this);
|
|
|
|
free(md5this); /* free this again */
|
2004-05-12 21:24:40 +08:00
|
|
|
|
|
|
|
ha1 = (unsigned char *)malloc(33); /* 32 digits and 1 zero byte */
|
|
|
|
if(!ha1)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
md5_to_ascii(md5buf, ha1);
|
|
|
|
|
2004-04-29 16:18:32 +08:00
|
|
|
if(d->algo == CURLDIGESTALGO_MD5SESS) {
|
|
|
|
/* nonce and cnonce are OUTSIDE the hash */
|
|
|
|
tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!tmp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2007-01-14 22:57:51 +08:00
|
|
|
CURL_OUTPUT_DIGEST_CONV(data, tmp); /* convert on non-ASCII machines */
|
2006-05-26 07:04:20 +08:00
|
|
|
Curl_md5it(md5buf, (unsigned char *)tmp);
|
|
|
|
free(tmp); /* free this again */
|
|
|
|
md5_to_ascii(md5buf, ha1);
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
/*
|
2004-04-29 16:18:32 +08:00
|
|
|
If the "qop" directive's value is "auth" or is unspecified, then A2 is:
|
|
|
|
|
|
|
|
A2 = Method ":" digest-uri-value
|
|
|
|
|
2004-10-06 15:50:18 +08:00
|
|
|
If the "qop" value is "auth-int", then A2 is:
|
2004-04-29 16:18:32 +08:00
|
|
|
|
|
|
|
A2 = Method ":" digest-uri-value ":" H(entity-body)
|
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
(The "Method" value is the HTTP request method as specified in section
|
|
|
|
5.1.1 of RFC 2616)
|
|
|
|
*/
|
|
|
|
|
2003-05-23 00:12:30 +08:00
|
|
|
md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!md5this) {
|
|
|
|
free(ha1);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(d->qop && strequal(d->qop, "auth-int")) {
|
2004-04-29 16:18:32 +08:00
|
|
|
/* We don't support auth-int at the moment. I can't see a easy way to get
|
|
|
|
entity-body here */
|
|
|
|
/* TODO: Append H(entity-body)*/
|
|
|
|
}
|
2007-01-14 22:57:51 +08:00
|
|
|
CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
|
2003-05-23 00:09:54 +08:00
|
|
|
Curl_md5it(md5buf, md5this);
|
|
|
|
free(md5this); /* free this again */
|
|
|
|
md5_to_ascii(md5buf, ha2);
|
2004-04-29 16:18:32 +08:00
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(d->qop) {
|
2004-04-29 16:18:32 +08:00
|
|
|
md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
|
|
|
|
ha1,
|
|
|
|
d->nonce,
|
|
|
|
d->nc,
|
|
|
|
d->cnonce,
|
|
|
|
d->qop,
|
|
|
|
ha2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
md5this = (unsigned char *)aprintf("%s:%s:%s",
|
|
|
|
ha1,
|
|
|
|
d->nonce,
|
|
|
|
ha2);
|
|
|
|
}
|
|
|
|
free(ha1);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!md5this)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-04-29 16:18:32 +08:00
|
|
|
|
2007-01-14 22:57:51 +08:00
|
|
|
CURL_OUTPUT_DIGEST_CONV(data, md5this); /* convert on non-ASCII machines */
|
2003-05-23 00:09:54 +08:00
|
|
|
Curl_md5it(md5buf, md5this);
|
|
|
|
free(md5this); /* free this again */
|
|
|
|
md5_to_ascii(md5buf, request_digest);
|
|
|
|
|
|
|
|
/* for test case 64 (snooped from a Mozilla 1.3a request)
|
|
|
|
|
|
|
|
Authorization: Digest username="testuser", realm="testrealm", \
|
|
|
|
nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
|
|
|
|
*/
|
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(d->qop) {
|
2004-06-03 18:42:20 +08:00
|
|
|
*allocuserpwd =
|
2004-05-04 15:52:53 +08:00
|
|
|
aprintf( "%sAuthorization: Digest "
|
2004-04-29 16:18:32 +08:00
|
|
|
"username=\"%s\", "
|
|
|
|
"realm=\"%s\", "
|
|
|
|
"nonce=\"%s\", "
|
|
|
|
"uri=\"%s\", "
|
|
|
|
"cnonce=\"%s\", "
|
2004-05-10 16:09:15 +08:00
|
|
|
"nc=%08x, "
|
2004-04-29 16:18:32 +08:00
|
|
|
"qop=\"%s\", "
|
|
|
|
"response=\"%s\"",
|
2004-05-04 15:52:53 +08:00
|
|
|
proxy?"Proxy-":"",
|
2004-06-03 18:42:20 +08:00
|
|
|
userp,
|
2004-04-29 16:18:32 +08:00
|
|
|
d->realm,
|
|
|
|
d->nonce,
|
|
|
|
uripath, /* this is the PATH part of the URL */
|
|
|
|
d->cnonce,
|
|
|
|
d->nc,
|
|
|
|
d->qop,
|
|
|
|
request_digest);
|
|
|
|
|
|
|
|
if(strequal(d->qop, "auth"))
|
|
|
|
d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
|
|
|
|
which tells to the server how many times you are using the
|
|
|
|
same nonce in the qop=auth mode. */
|
|
|
|
}
|
|
|
|
else {
|
2004-06-03 18:42:20 +08:00
|
|
|
*allocuserpwd =
|
2004-05-04 15:52:53 +08:00
|
|
|
aprintf( "%sAuthorization: Digest "
|
2004-04-29 16:18:32 +08:00
|
|
|
"username=\"%s\", "
|
|
|
|
"realm=\"%s\", "
|
|
|
|
"nonce=\"%s\", "
|
|
|
|
"uri=\"%s\", "
|
|
|
|
"response=\"%s\"",
|
2004-05-04 15:52:53 +08:00
|
|
|
proxy?"Proxy-":"",
|
2004-06-03 18:42:20 +08:00
|
|
|
userp,
|
2004-04-29 16:18:32 +08:00
|
|
|
d->realm,
|
|
|
|
d->nonce,
|
|
|
|
uripath, /* this is the PATH part of the URL */
|
|
|
|
request_digest);
|
|
|
|
}
|
2004-06-03 18:42:20 +08:00
|
|
|
if(!*allocuserpwd)
|
2004-05-12 21:24:40 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-04-29 16:18:32 +08:00
|
|
|
|
|
|
|
/* Add optional fields */
|
|
|
|
if(d->opaque) {
|
|
|
|
/* append opaque */
|
2004-06-03 18:42:20 +08:00
|
|
|
tmp = aprintf("%s, opaque=\"%s\"", *allocuserpwd, d->opaque);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!tmp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-06-03 18:42:20 +08:00
|
|
|
free(*allocuserpwd);
|
|
|
|
*allocuserpwd = tmp;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(d->algorithm) {
|
|
|
|
/* append algorithm */
|
2004-06-03 18:42:20 +08:00
|
|
|
tmp = aprintf("%s, algorithm=\"%s\"", *allocuserpwd, d->algorithm);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!tmp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-06-03 18:42:20 +08:00
|
|
|
free(*allocuserpwd);
|
|
|
|
*allocuserpwd = tmp;
|
2004-04-29 16:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* append CRLF to the userpwd header */
|
2004-06-03 18:42:20 +08:00
|
|
|
tmp = (char*) realloc(*allocuserpwd, strlen(*allocuserpwd) + 3 + 1);
|
2004-05-12 21:24:40 +08:00
|
|
|
if(!tmp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
strcat(tmp, "\r\n");
|
2004-06-03 18:42:20 +08:00
|
|
|
*allocuserpwd = tmp;
|
2003-05-23 00:09:54 +08:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-04 15:52:53 +08:00
|
|
|
void Curl_digest_cleanup_one(struct digestdata *d)
|
2003-05-23 06:39:38 +08:00
|
|
|
{
|
2004-03-30 21:00:32 +08:00
|
|
|
if(d->nonce)
|
|
|
|
free(d->nonce);
|
|
|
|
d->nonce = NULL;
|
|
|
|
|
|
|
|
if(d->cnonce)
|
|
|
|
free(d->cnonce);
|
|
|
|
d->cnonce = NULL;
|
2003-05-23 06:39:38 +08:00
|
|
|
|
2004-03-30 21:00:32 +08:00
|
|
|
if(d->realm)
|
|
|
|
free(d->realm);
|
|
|
|
d->realm = NULL;
|
2003-05-23 06:39:38 +08:00
|
|
|
|
2004-04-29 16:18:32 +08:00
|
|
|
if(d->opaque)
|
|
|
|
free(d->opaque);
|
|
|
|
d->opaque = NULL;
|
|
|
|
|
|
|
|
if(d->qop)
|
|
|
|
free(d->qop);
|
|
|
|
d->qop = NULL;
|
2003-05-23 06:39:38 +08:00
|
|
|
|
2004-04-29 16:18:32 +08:00
|
|
|
if(d->algorithm)
|
|
|
|
free(d->algorithm);
|
|
|
|
d->algorithm = NULL;
|
|
|
|
|
|
|
|
d->nc = 0;
|
|
|
|
d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
|
2004-03-30 21:00:32 +08:00
|
|
|
d->stale = FALSE; /* default means normal, not stale */
|
2003-05-23 06:39:38 +08:00
|
|
|
}
|
|
|
|
|
2004-05-04 15:52:53 +08:00
|
|
|
|
|
|
|
void Curl_digest_cleanup(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
Curl_digest_cleanup_one(&data->state.digest);
|
|
|
|
Curl_digest_cleanup_one(&data->state.proxydigest);
|
|
|
|
}
|
|
|
|
|
2003-05-23 00:09:54 +08:00
|
|
|
#endif
|