1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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 #ifndef CURL_DISABLE_HTTP
26 /* -- WIN32 approved -- */
38 #include "http_digest.h"
40 #include "url.h" /* for Curl_safefree() */
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
46 /* The last #include file should be: */
49 /* Test example headers:
51 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
52 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
56 CURLdigest Curl_input_digest(struct connectdata *conn,
58 char *header) /* rest of the *-authenticate:
64 bool foundAuth = FALSE;
65 bool foundAuthInt = FALSE;
66 struct SessionHandle *data=conn->data;
67 bool before = FALSE; /* got a nonce before */
71 d = &data->state.proxydigest;
74 d = &data->state.digest;
77 /* skip initial whitespaces */
78 while(*header && isspace((int)*header))
81 if(checkprefix("Digest", header)) {
82 header += strlen("Digest");
84 /* If we already have received a nonce, keep that in mind */
88 /* clear off any former leftovers and init to defaults */
89 Curl_digest_cleanup_one(d);
96 while(*header && isspace((int)*header))
99 /* how big can these strings be? */
100 if((2 == sscanf(header, "%31[^=]=\"%127[^\"]\"",
102 /* try the same scan but without quotes around the content but don't
103 include the possibly trailing comma */
104 (2 == sscanf(header, "%31[^=]=%127[^,]",
106 if(strequal(value, "nonce")) {
107 d->nonce = strdup(content);
109 else if(strequal(value, "stale")) {
110 if(strequal(content, "true")) {
112 d->nc = 1; /* we make a new nonce now */
115 else if(strequal(value, "realm")) {
116 d->realm = strdup(content);
118 else if(strequal(value, "opaque")) {
119 d->opaque = strdup(content);
121 else if(strequal(value, "qop")) {
123 /* tokenize the list and choose auth if possible, use a temporary
124 clone of the buffer since strtok_r() ruins it */
125 tmp = strdup(content);
126 token = strtok_r(tmp, ",", &tok_buf);
127 while (token != NULL) {
128 if (strequal(token, "auth")) {
131 else if (strequal(token, "auth-int")) {
134 token = strtok_r(NULL, ",", &tok_buf);
137 /*select only auth o auth-int. Otherwise, ignore*/
139 d->qop = strdup("auth");
141 else if (foundAuthInt) {
142 d->qop = strdup("auth-int");
145 else if(strequal(value, "algorithm")) {
146 d->algorithm = strdup(content);
147 if(strequal(content, "MD5-sess"))
148 d->algo = CURLDIGESTALGO_MD5SESS;
149 else if(strequal(content, "MD5"))
150 d->algo = CURLDIGESTALGO_MD5;
152 return CURLDIGEST_BADALGO;
155 /* unknown specifier, ignore it! */
157 totlen = strlen(value)+strlen(content)+3;
160 break; /* we're done here */
164 /* allow the list to be comma-separated */
167 /* We had a nonce since before, and we got another one now without
168 'stale=true'. This means we provided bad credentials in the previous
170 if(before && !d->stale)
171 return CURLDIGEST_BAD;
173 /* We got this header without a nonce, that's a bad Digest line! */
175 return CURLDIGEST_BAD;
178 /* else not a digest, get out */
179 return CURLDIGEST_NONE;
181 return CURLDIGEST_FINE;
184 /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
185 static void md5_to_ascii(unsigned char *source, /* 16 bytes */
186 unsigned char *dest) /* 33 bytes */
190 sprintf((char *)&dest[i*2], "%02x", source[i]);
193 CURLcode Curl_output_digest(struct connectdata *conn,
195 unsigned char *request,
196 unsigned char *uripath)
198 /* We have a Digest setup for this, use it! Now, to get all the details for
199 this sorted out, I must urge you dear friend to read up on the RFC2617
201 unsigned char md5buf[16]; /* 16 bytes/128 bits */
202 unsigned char request_digest[33];
203 unsigned char *md5this;
205 unsigned char ha2[33];/* 32 digits and 1 zero byte */
213 struct SessionHandle *data = conn->data;
214 struct digestdata *d;
217 d = &data->state.proxydigest;
218 authp = &data->state.authproxy;
219 userp = &conn->allocptr.proxyuserpwd;
222 d = &data->state.digest;
223 authp = &data->state.authhost;
224 userp = &conn->allocptr.userpwd;
237 /* Generate a cnonce */
239 snprintf(cnoncebuf, sizeof(cnoncebuf), "%06ld", now.tv_sec);
240 if(Curl_base64_encode(cnoncebuf, strlen(cnoncebuf), &cnonce))
243 return CURLE_OUT_OF_MEMORY;
247 if the algorithm is "MD5" or unspecified (which then defaults to MD5):
249 A1 = unq(username-value) ":" unq(realm-value) ":" passwd
251 if the algorithm is "MD5-sess" then:
253 A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
254 ":" unq(nonce-value) ":" unq(cnonce-value)
257 md5this = (unsigned char *)
258 aprintf("%s:%s:%s", conn->user, d->realm, conn->passwd);
260 return CURLE_OUT_OF_MEMORY;
261 Curl_md5it(md5buf, md5this);
262 free(md5this); /* free this again */
264 ha1 = (unsigned char *)malloc(33); /* 32 digits and 1 zero byte */
266 return CURLE_OUT_OF_MEMORY;
268 md5_to_ascii(md5buf, ha1);
270 if(d->algo == CURLDIGESTALGO_MD5SESS) {
271 /* nonce and cnonce are OUTSIDE the hash */
272 tmp = aprintf("%s:%s:%s", ha1, d->nonce, d->cnonce);
275 return CURLE_OUT_OF_MEMORY;
276 ha1 = (unsigned char *)tmp;
280 If the "qop" directive's value is "auth" or is unspecified, then A2 is:
282 A2 = Method ":" digest-uri-value
284 If the "qop" value is "auth-int", then A2 is:
286 A2 = Method ":" digest-uri-value ":" H(entity-body)
288 (The "Method" value is the HTTP request method as specified in section
292 md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
295 return CURLE_OUT_OF_MEMORY;
298 if (d->qop && strequal(d->qop, "auth-int")) {
299 /* We don't support auth-int at the moment. I can't see a easy way to get
301 /* TODO: Append H(entity-body)*/
303 Curl_md5it(md5buf, md5this);
304 free(md5this); /* free this again */
305 md5_to_ascii(md5buf, ha2);
308 md5this = (unsigned char *)aprintf("%s:%s:%08x:%s:%s:%s",
317 md5this = (unsigned char *)aprintf("%s:%s:%s",
324 return CURLE_OUT_OF_MEMORY;
326 Curl_md5it(md5buf, md5this);
327 free(md5this); /* free this again */
328 md5_to_ascii(md5buf, request_digest);
330 /* for test case 64 (snooped from a Mozilla 1.3a request)
332 Authorization: Digest username="testuser", realm="testrealm", \
333 nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
336 Curl_safefree(conn->allocptr.userpwd);
340 aprintf( "%sAuthorization: Digest "
353 uripath, /* this is the PATH part of the URL */
359 if(strequal(d->qop, "auth"))
360 d->nc++; /* The nc (from RFC) has to be a 8 hex digit number 0 padded
361 which tells to the server how many times you are using the
362 same nonce in the qop=auth mode. */
366 aprintf( "%sAuthorization: Digest "
376 uripath, /* this is the PATH part of the URL */
380 return CURLE_OUT_OF_MEMORY;
382 /* Add optional fields */
385 tmp = aprintf("%s, opaque=\"%s\"", *userp, d->opaque);
387 return CURLE_OUT_OF_MEMORY;
393 /* append algorithm */
394 tmp = aprintf("%s, algorithm=\"%s\"", *userp, d->algorithm);
396 return CURLE_OUT_OF_MEMORY;
401 /* append CRLF to the userpwd header */
402 tmp = (char*) realloc(*userp, strlen(*userp) + 3 + 1);
404 return CURLE_OUT_OF_MEMORY;
411 void Curl_digest_cleanup_one(struct digestdata *d)
438 d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
439 d->stale = FALSE; /* default means normal, not stale */
443 void Curl_digest_cleanup(struct SessionHandle *data)
445 Curl_digest_cleanup_one(&data->state.digest);
446 Curl_digest_cleanup_one(&data->state.proxydigest);