36 #if defined(MHD_W32_MUTEX_)
37 #ifndef WIN32_LEAN_AND_MEAN
38 #define WIN32_LEAN_AND_MEAN 1
46 #define TIMESTAMP_BIN_SIZE 4
53 #define NONCE_STD_LEN(digest_size) \
54 ((digest_size) * 2 + TIMESTAMP_BIN_SIZE * 2)
61 #define MAX_DIGEST SHA256_DIGEST_SIZE
72 #define VLA_ARRAY_LEN_DIGEST(n) (MAX_DIGEST)
80 #define VLA_ARRAY_LEN_DIGEST(n) (n)
86 #define VLA_CHECK_LEN_DIGEST(n) do { if ((n) > MAX_DIGEST) mhd_panic ( \
87 mhd_panic_cls, __FILE__, __LINE__, \
88 "VLA too big.\n"); } while (0)
94 #define _BASE "Digest "
99 #define MAX_USERNAME_LENGTH 128
104 #define MAX_REALM_LENGTH 256
109 #define MAX_AUTH_RESPONSE_LENGTH 256
117 struct DigestAlgorithm
122 unsigned int digest_size;
186 for (i = 0; i < len; ++i)
188 j = (bin[i] >> 4) & 0x0f;
189 hex[i * 2] = (char) ((j <= 9) ? (j +
'0') : (j - 10 +
'a'));
191 hex[i * 2 + 1] = (char) ((j <= 9) ? (j +
'0') : (j - 10 +
'a'));
214 struct DigestAlgorithm *da,
215 const uint8_t *digest,
232 (
const unsigned char *)
":",
235 (
const unsigned char *) nonce,
238 (
const unsigned char *)
":",
241 (
const unsigned char *) cnonce,
274 const char *username,
276 const char *password,
279 struct DigestAlgorithm *da)
286 (
const unsigned char *) username,
289 (
const unsigned char *)
":",
292 (
const unsigned char *) realm,
295 (
const unsigned char *)
":",
298 (
const unsigned char *) password,
329 const char *noncecount,
335 struct DigestAlgorithm *da)
344 (
const unsigned char *) method,
347 (
const unsigned char *)
":",
350 (
const unsigned char *) uri,
353 if (0 == strcasecmp (qop,
375 (
const unsigned char *) ha1,
376 da->digest_size * 2);
378 (
const unsigned char *)
":",
381 (
const unsigned char *) nonce,
384 (
const unsigned char*)
":",
389 (
const unsigned char *) noncecount,
390 strlen (noncecount));
392 (
const unsigned char *)
":",
395 (
const unsigned char *) cnonce,
398 (
const unsigned char *)
":",
401 (
const unsigned char *) qop,
404 (
const unsigned char *)
":",
408 (
const unsigned char *) da->sessionkey,
409 da->digest_size * 2);
448 keylen = strlen (key);
452 if (
NULL == (eq = strchr (ptr,
476 (eq == &ptr[keylen]) )
480 len = strlen (q1) + 1;
492 if (size > (
size_t) ((q2 - q1) + 1))
493 size = (q2 - q1) + 1;
537 noncelen = strlen (
nonce) + 1;
543 mod = daemon->nonce_nc_size;
551 off = (off << 8) | (*np ^ (off >> 24));
560 nn = &daemon->nnc[off];
561 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
572 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
579 if ( (nc < nn->
nc) &&
581 (
nc + 64 >= nn->
nc) &&
582 (0 == ((1LLU << (nn->
nc -
nc - 1)) & nn->
nmask)) )
585 nn->
nmask |= (1LLU << (nn->
nc -
nc - 1));
586 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
592 if ( (nc <= nn->
nc) ||
593 (0 != strcmp (nn->
nonce,
597 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
603 "Stale nonce received. If this happens a lot, you should probably increase the size of the nonce array.\n"));
608 if (64 >
nc - nn->
nc)
613 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
644 if (0 != strncmp (header,
654 return strdup (user);
680 struct DigestAlgorithm *da,
689 timestamp[0] = (
unsigned char) ((nonce_time & 0xff000000) >> 0x18);
690 timestamp[1] = (
unsigned char) ((nonce_time & 0x00ff0000) >> 0x10);
691 timestamp[2] = (
unsigned char) ((nonce_time & 0x0000ff00) >> 0x08);
692 timestamp[3] = (
unsigned char) ((nonce_time & 0x000000ff));
697 (
const unsigned char *)
":",
700 (
const unsigned char *) method,
703 (
const unsigned char *)
":",
707 (
const unsigned char *) rnd,
710 (
const unsigned char *)
":",
713 (
const unsigned char *) uri,
716 (
const unsigned char *)
":",
719 (
const unsigned char *) realm,
766 if (0 != memcmp (key,
801 unsigned int num_headers;
804 argb = strdup (args);
808 MHD_DLOG (connection->
daemon,
809 _ (
"Failed to allocate memory for copy of URI arguments.\n"));
830 if (0 != num_headers)
860 struct DigestAlgorithm *da,
862 const char *username,
863 const char *password,
864 const uint8_t *digest,
865 unsigned int nonce_timeout)
876 const char *hentity =
NULL;
893 if (0 != strncmp (header,
898 left = strlen (header);
908 (0 != strcmp (username,
911 left -= strlen (
"username") + len;
925 left -= strlen (
"realm") + len;
933 left -= strlen (
"nonce") + len;
934 if (left > 32 * 1024)
952 _ (
"Authentication failed, invalid timestamp format.\n"));
962 if ( (t > nonce_time + nonce_timeout) ||
963 (nonce_time + nonce_timeout < nonce_time) )
971 daemon->digest_auth_random,
972 daemon->digest_auth_rand_size,
986 if (0 != strcmp (nonce,
1012 #ifdef HAVE_MESSAGES
1014 _ (
"Authentication failed, invalid format.\n"));
1022 #ifdef HAVE_MESSAGES
1024 _ (
"Authentication failed, invalid nc format.\n"));
1045 uri = malloc (left + 1);
1048 #ifdef HAVE_MESSAGES
1050 _ (
"Failed to allocate memory for auth header processing.\n"));
1096 qmark = strchr (uri,
1105 if (0 != strcmp (uri,
1108 #ifdef HAVE_MESSAGES
1110 _ (
"Authentication failed, URI does not match.\n"));
1117 const char *args = qmark;
1127 #ifdef HAVE_MESSAGES
1129 _ (
"Authentication failed, arguments do not match.\n"));
1136 return (0 == strcmp (response,
1164 const char *username,
1165 const char *password,
1166 unsigned int nonce_timeout)
1185 #define SETUP_DA(algo,da) \
1187 struct MD5Context md5; \
1188 struct sha256_ctx sha256; \
1191 char md5[MD5_DIGEST_SIZE * 2 + 1]; \
1192 char sha256[SHA256_DIGEST_SIZE * 2 + 1]; \
1194 struct DigestAlgorithm da; \
1198 case MHD_DIGEST_ALG_MD5: \
1199 da.digest_size = MD5_DIGEST_SIZE; \
1200 da.ctx = &ctx.md5; \
1202 da.sessionkey = skey.md5; \
1203 da.init = &MHD_MD5Init; \
1204 da.update = &MHD_MD5Update; \
1205 da.digest = &MHD_MD5Final; \
1207 case MHD_DIGEST_ALG_AUTO: \
1209 case MHD_DIGEST_ALG_SHA256: \
1210 da.digest_size = SHA256_DIGEST_SIZE; \
1211 da.ctx = &ctx.sha256; \
1212 da.alg = "sha-256"; \
1213 da.sessionkey = skey.sha256; \
1214 da.init = &MHD_SHA256_init; \
1215 da.update = &MHD_SHA256_update; \
1216 da.digest = &sha256_finish; \
1219 mhd_assert (false); \
1242 const char *username,
1243 const char *password,
1244 unsigned int nonce_timeout,
1280 const char *username,
1281 const uint8_t *digest,
1283 unsigned int nonce_timeout,
1289 if (da.digest_size != digest_size)
1321 const char *username,
1323 unsigned int nonce_timeout)
1369 connection->
daemon->digest_auth_random,
1370 connection->
daemon->digest_auth_rand_size,
1380 #ifdef HAVE_MESSAGES
1381 MHD_DLOG (connection->
daemon,
1383 "Could not register nonce (is the nonce array size zero?).\n"));
1388 hlen = MHD_snprintf_ (
NULL,
1390 "Digest realm=\"%s\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\",algorithm=%s%s",
1406 #ifdef HAVE_MESSAGES
1407 MHD_DLOG (connection->
daemon,
1408 _ (
"Failed to allocate memory for auth response header.\n"));
1413 if (MHD_snprintf_ (header,
1415 "Digest realm=\"%s\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\",algorithm=%s%s",
1429 if ( (
MHD_NO != ret) && (AND in state : 100
continue aborting ...))
1448 #ifdef HAVE_MESSAGES
1449 MHD_DLOG (connection->
daemon,
1450 _ (
"Failed to add Digest auth header.\n"));
#define VLA_CHECK_LEN_DIGEST(n)
#define SETUP_DA(algo, da)
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, struct DigestAlgorithm *da)
#define NONCE_STD_LEN(digest_size)
static enum MHD_Result check_argument_match(struct MHD_Connection *connection, const char *args)
#define MAX_AUTH_RESPONSE_LENGTH
#define TIMESTAMP_BIN_SIZE
static void digest_calc_ha1_from_user(const char *alg, const char *username, const char *realm, const char *password, const char *nonce, const char *cnonce, struct DigestAlgorithm *da)
static size_t lookup_sub_value(char *dest, size_t size, const char *data, const char *key)
static enum MHD_Result check_nonce_nc(struct MHD_Connection *connection, const char *nonce, uint64_t nc)
#define MAX_USERNAME_LENGTH
static void calculate_nonce(uint32_t nonce_time, const char *method, const char *rnd, size_t rnd_size, const char *uri, const char *realm, struct DigestAlgorithm *da, char *nonce)
static enum MHD_Result test_header(struct MHD_Connection *connection, const char *key, size_t key_size, const char *value, size_t value_size, enum MHD_ValueKind kind)
#define VLA_ARRAY_LEN_DIGEST(n)
static void digest_calc_ha1_from_digest(const char *alg, struct DigestAlgorithm *da, const uint8_t *digest, const char *nonce, const char *cnonce)
static void cvthex(const unsigned char *bin, size_t len, char *hex)
_MHD_EXTERN char * MHD_digest_auth_get_username(struct MHD_Connection *connection)
_MHD_EXTERN int MHD_digest_auth_check2(struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
_MHD_EXTERN enum MHD_Result MHD_queue_auth_fail_response2(struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale, enum MHD_DigestAuthAlgorithm algo)
_MHD_EXTERN int MHD_digest_auth_check_digest2(struct MHD_Connection *connection, const char *realm, const char *username, const uint8_t *digest, size_t digest_size, unsigned int nonce_timeout, enum MHD_DigestAuthAlgorithm algo)
_MHD_EXTERN enum MHD_Result MHD_queue_auth_fail_response(struct MHD_Connection *connection, const char *realm, const char *opaque, struct MHD_Response *response, int signal_stale)
static int digest_auth_check_all(struct MHD_Connection *connection, struct DigestAlgorithm *da, const char *realm, const char *username, const char *password, const uint8_t *digest, unsigned int nonce_timeout)
_MHD_EXTERN int MHD_digest_auth_check_digest(struct MHD_Connection *connection, const char *realm, const char *username, const uint8_t digest[MHD_MD5_DIGEST_SIZE], unsigned int nonce_timeout)
_MHD_EXTERN int MHD_digest_auth_check(struct MHD_Connection *connection, const char *realm, const char *username, const char *password, unsigned int nonce_timeout)
#define MHD_INVALID_NONCE
#define MHD_HTTP_UNAUTHORIZED
_MHD_EXTERN enum MHD_Result MHD_lookup_connection_value_n(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, size_t key_size, const char **value_ptr, size_t *value_size_ptr)
_MHD_EXTERN enum MHD_Result MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
_MHD_EXTERN enum MHD_Result MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
bool MHD_parse_arguments_(struct MHD_Request *request, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, unsigned int *num_headers)
void * MHD_calloc_(size_t nelem, size_t elsize)
#define MHD_mutex_unlock_chk_(pmutex)
#define MHD_mutex_lock_chk_(pmutex)
time_t MHD_monotonic_sec_counter(void)
int MHD_str_equal_caseless_(const char *str1, const char *str2)
size_t MHD_strx_to_uint64_n_(const char *str, size_t maxlen, uint64_t *out_val)
int MHD_str_equal_caseless_n_(const char *const str1, const char *const str2, size_t maxlen)
size_t MHD_strx_to_uint32_n_(const char *str, size_t maxlen, uint32_t *out_val)
#define MHD_STATICSTR_LEN_(macro)
internal shared structures
Header for platform missing functions.
limits values definitions
internal monotonic clock functions implementations
Header for string manipulating helpers.
#define MHD_MD5_DIGEST_SIZE
Calculation of SHA-256 digest.
struct MHD_HTTP_Header * headers_received
struct MHD_Daemon * daemon
void * unescape_callback_cls
UnescapeCallback unescape_callback
char nonce[MAX_NONCE_LENGTH]