30 #define HASH_MD5_HEX_LEN (2 * MD5_DIGEST_SIZE)
35 #define _BASE "Digest "
40 #define MAX_USERNAME_LENGTH 128
45 #define MAX_REALM_LENGTH 256
50 #define MAX_AUTH_RESPONSE_LENGTH 128
68 for (i = 0; i < len; ++i)
70 j = (bin[i] >> 4) & 0x0f;
71 hex[i * 2] = j <= 9 ? (j +
'0') : (j +
'a' - 10);
73 hex[i * 2 + 1] = j <= 9 ? (j +
'0') : (j +
'a' - 10);
104 MD5Update (&md5, username, strlen (username));
108 MD5Update (&md5, password, strlen (password));
110 if (0 == strcasecmp (alg,
"md5-sess"))
117 MD5Update (&md5, cnonce, strlen (cnonce));
120 cvthex (ha1,
sizeof (ha1), sessionkey);
140 const char *noncecount,
154 MD5Update (&md5, method, strlen(method));
158 if (0 == strcasecmp(qop,
"auth-int"))
164 MD5Update (&md5, hentity, strlen(hentity));
177 MD5Update (&md5, noncecount, strlen(noncecount));
179 MD5Update (&md5, cnonce, strlen(cnonce));
186 cvthex (resphash,
sizeof (resphash), response);
220 keylen = strlen (key);
224 if (
NULL == (eq = strchr (ptr,
'=')))
231 q2 = strchr (q1,
',');
237 q2 = strchr (q1,
'\"');
242 if ( (0 == strncasecmp (ptr,
245 (eq == &ptr[keylen]) )
249 len = strlen (q1) + 1;
261 if (size > (
size_t) ((q2 - q1) + 1))
262 size = (q2 - q1) + 1;
273 ptr = strchr (qn,
',');
296 unsigned long int nc)
302 mod = connection->
daemon->nonce_nc_size;
310 off = (off << 8) | (*np ^ (off >> 24));
320 (void) pthread_mutex_lock (&connection->
daemon->nnc_lock);
323 strcpy(connection->
daemon->nnc[off].nonce,
325 connection->
daemon->nnc[off].nc = 0;
326 (void) pthread_mutex_unlock (&connection->
daemon->nnc_lock);
329 if ( (nc <= connection->daemon->nnc[off].nc) ||
330 (0 != strcmp(connection->
daemon->nnc[off].nonce, nonce)) )
332 (void) pthread_mutex_unlock (&connection->
daemon->nnc_lock);
334 MHD_DLOG (connection->
daemon,
335 "Stale nonce received. If this happens a lot, you should probably increase the size of the nonce array.\n");
339 connection->
daemon->nnc[off].nc = nc;
340 (void) pthread_mutex_unlock (&connection->
daemon->nnc_lock);
364 if (0 != strncmp (header,
_BASE, strlen (
_BASE)))
366 header += strlen (
_BASE);
372 return strdup (user);
399 unsigned char timestamp[4];
401 char timestamphex[
sizeof(timestamp) * 2 + 1];
404 timestamp[0] = (nonce_time & 0xff000000) >> 0x18;
405 timestamp[1] = (nonce_time & 0x00ff0000) >> 0x10;
406 timestamp[2] = (nonce_time & 0x0000ff00) >> 0x08;
407 timestamp[3] = (nonce_time & 0x000000ff);
410 MD5Update (&md5, method, strlen (method));
419 cvthex (tmpnonce,
sizeof (tmpnonce), nonce);
420 cvthex (timestamp, 4, timestamphex);
421 strncat (nonce, timestamphex, 8);
446 if (0 != strcmp (key, pos->
header))
448 if ( (
NULL == value) &&
451 if ( (
NULL == value) ||
453 (0 != strcmp (value, pos->
value)) )
476 size_t slen = strlen (args) + 1;
481 unsigned int num_headers;
484 memcpy (argb, args, slen);
486 while ( (
NULL != argp) &&
489 equals = strchr (argp,
'=');
503 amper = strchr (equals,
'&');
528 if (0 != num_headers)
550 const char *username,
551 const char *password,
552 unsigned int nonce_timeout)
562 const char *hentity =
NULL;
569 unsigned long int nci;
576 if (0 != strncmp(header,
_BASE, strlen(
_BASE)))
578 header += strlen (
_BASE);
579 left = strlen (header);
588 (0 != strcmp(username, un)) )
590 left -= strlen (
"username") + len;
600 (0 != strcmp(realm, r)) )
602 left -= strlen (
"realm") + len;
609 left -= strlen (
"nonce") + len;
610 if (left > 32 * 1024)
630 nonce_time = strtoul (nonce + len - 8, (
char **)
NULL, 16);
637 if ( (t > nonce_time + nonce_timeout) ||
638 (nonce_time + nonce_timeout < nonce_time) )
640 if (0 != strncmp (uri,
642 strlen (connection->
url)))
645 MHD_DLOG (connection->
daemon,
646 "Authentication failed, URI does not match.\n");
651 const char *args = strchr (uri,
'?');
662 MHD_DLOG (connection->
daemon,
663 "Authentication failed, arguments do not match.\n");
670 connection->
daemon->digest_auth_random,
671 connection->
daemon->digest_auth_rand_size,
685 if (0 != strcmp (nonce, noncehashexp))
689 header,
"cnonce")) ||
691 ( (0 != strcmp (qop,
"auth")) &&
692 (0 != strcmp (qop,
"")) ) ||
697 MHD_DLOG (connection->
daemon,
698 "Authentication failed, invalid format.\n");
702 nci = strtoul (nc, &end, 16);
703 if ( (
'\0' != *end) ||
704 ( (LONG_MAX == nci) &&
705 (ERANGE == errno) ) )
708 MHD_DLOG (connection->
daemon,
709 "Authentication failed, invalid format.\n");
738 return (0 == strcmp(response, respexp))
773 connection->
daemon->digest_auth_random,
774 connection->
daemon->digest_auth_rand_size,
781 MHD_DLOG (connection->
daemon,
782 "Could not register nonce (is the nonce array size zero?).\n");
787 hlen = snprintf (
NULL,
789 "Digest realm=\"%s\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\"%s",
801 "Digest realm=\"%s\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\"%s",
void * unescape_callback_cls
#define MAX_AUTH_RESPONSE_LENGTH
_MHD_EXTERN const char * MHD_lookup_connection_value(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
static void cvthex(const unsigned char *bin, size_t len, char *hex)
static int check_nonce_nc(struct MHD_Connection *connection, const char *nonce, unsigned long int nc)
static size_t lookup_sub_value(char *dest, size_t size, const char *data, const char *key)
#define MHD_INVALID_NONCE
_MHD_EXTERN int MHD_digest_auth_check(struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout)
static int test_header(struct MHD_Connection *connection, const char *key, const char *value)
struct MHD_Daemon * daemon
static void calculate_nonce(uint32_t nonce_time, const char *method, const char *rnd, size_t rnd_size, const char *uri, const char *realm, char *nonce)
static void digest_calc_ha1(const char *alg, const char *username, const char *realm, const char *password, const char *nonce, const char *cnonce, char *sessionkey)
_MHD_EXTERN int MHD_queue_auth_fail_response(struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale)
internal shared structures
time_t MHD_monotonic_time(void)
_MHD_EXTERN char * MHD_digest_auth_get_username(struct MHD_Connection *connection)
static int check_argument_match(struct MHD_Connection *connection, const char *args)
UnescapeCallback unescape_callback
_MHD_EXTERN int MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
#define MHD_HTTP_UNAUTHORIZED
void MD5Update(struct MD5Context *ctx, const void *data, unsigned len)
void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
void MD5Init(struct MD5Context *ctx)
static void digest_calc_response(const char *ha1, const char *nonce, const char *noncecount, const char *cnonce, const char *qop, const char *method, const char *uri, const char *hentity, char *response)
#define MAX_USERNAME_LENGTH
struct MHD_HTTP_Header * headers_received