18abd06a7SGlenn Strauss #include "first.h" 28abd06a7SGlenn Strauss 304d76e7aSGlenn Strauss #include "base.h" 422e8b456SStefan Bühler #include "plugin.h" 522e8b456SStefan Bühler #include "http_auth.h" 63dd3cde9SGlenn Strauss #include "http_header.h" 722e8b456SStefan Bühler #include "log.h" 8bcdc6a3bSJan Kneschke 9*b28a3714SGlenn Strauss #include "sys-crypto.h" /* USE_LIB_CRYPTO */ 10*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 11*b28a3714SGlenn Strauss 12*b28a3714SGlenn Strauss #if defined(USE_NETTLE_CRYPTO) 13*b28a3714SGlenn Strauss 14*b28a3714SGlenn Strauss #include <nettle/sha.h> 15*b28a3714SGlenn Strauss typedef struct sha256_ctx SHA256_CTX; 16*b28a3714SGlenn Strauss #define SHA256_Init(ctx) \ 17*b28a3714SGlenn Strauss sha256_init(ctx) 18*b28a3714SGlenn Strauss #define SHA256_Final(digest, ctx) \ 19*b28a3714SGlenn Strauss sha256_digest((ctx),sizeof(digest),(digest)) 20*b28a3714SGlenn Strauss static void 21*b28a3714SGlenn Strauss SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length) 22*b28a3714SGlenn Strauss { 23*b28a3714SGlenn Strauss sha256_update(ctx, length, data); 24*b28a3714SGlenn Strauss } 25*b28a3714SGlenn Strauss 26*b28a3714SGlenn Strauss #ifndef SHA512_256_DIGEST_LENGTH 27*b28a3714SGlenn Strauss #define SHA512_256_DIGEST_LENGTH 32 28b9e2be50SGlenn Strauss #endif 29*b28a3714SGlenn Strauss typedef struct sha512_ctx SHA512_CTX; 30*b28a3714SGlenn Strauss #define SHA512_256_Init(ctx) \ 31*b28a3714SGlenn Strauss sha512_256_init(ctx) 32*b28a3714SGlenn Strauss #define SHA512_256_Final(digest, ctx) \ 33*b28a3714SGlenn Strauss sha512_256_digest((ctx),sizeof(digest),(digest)) 34*b28a3714SGlenn Strauss static void 35*b28a3714SGlenn Strauss SHA512_256_Update(SHA512_CTX *ctx, const void *data, size_t length) 36*b28a3714SGlenn Strauss { 37*b28a3714SGlenn Strauss sha512_256_update(ctx, length, data); 38*b28a3714SGlenn Strauss } 39*b28a3714SGlenn Strauss 40*b28a3714SGlenn Strauss #elif defined(USE_OPENSSL_CRYPTO) 41*b28a3714SGlenn Strauss 42*b28a3714SGlenn Strauss #include <openssl/sha.h> 43*b28a3714SGlenn Strauss 44*b28a3714SGlenn Strauss #endif 45*b28a3714SGlenn Strauss 46*b28a3714SGlenn Strauss #endif /* USE_LIB_CRYPTO */ 47b9e2be50SGlenn Strauss 48bcdc6a3bSJan Kneschke #include <stdlib.h> 49bcdc6a3bSJan Kneschke #include <string.h> 50bcdc6a3bSJan Kneschke 51bcdc6a3bSJan Kneschke /** 527ba06c71SGlenn Strauss * auth framework 53bcdc6a3bSJan Kneschke */ 54bcdc6a3bSJan Kneschke 554b3a91e6SGlenn Strauss typedef struct { 564b3a91e6SGlenn Strauss const http_auth_backend_t *auth_backend; 578e713130SGlenn Strauss const array *auth_require; 588e713130SGlenn Strauss unsigned int auth_extern_authn; 597ba06c71SGlenn Strauss } plugin_config; 604b3a91e6SGlenn Strauss 614b3a91e6SGlenn Strauss typedef struct { 624b3a91e6SGlenn Strauss PLUGIN_DATA; 638e713130SGlenn Strauss plugin_config defaults; 647ba06c71SGlenn Strauss plugin_config conf; 657ba06c71SGlenn Strauss } plugin_data; 667ba06c71SGlenn Strauss 677c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 687c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 697c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 704b3a91e6SGlenn Strauss 71bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 727ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 737ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 747ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 757ba06c71SGlenn Strauss plugin_data *p; 767ba06c71SGlenn Strauss 777ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 787ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 797ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 807ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 81bcdc6a3bSJan Kneschke 82bcdc6a3bSJan Kneschke p = calloc(1, sizeof(*p)); 83bcdc6a3bSJan Kneschke 84bcdc6a3bSJan Kneschke return p; 85bcdc6a3bSJan Kneschke } 86bcdc6a3bSJan Kneschke 87b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) { 88b73949e0SGlenn Strauss plugin_data * const p = p_d; 898e713130SGlenn Strauss if (NULL == p->cvlist) return; 908e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 918e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 928e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 938e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 948e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 958e713130SGlenn Strauss switch (cpv->k_id) { 968e713130SGlenn Strauss case 1: /* auth.require */ 978e713130SGlenn Strauss array_free(cpv->v.v); 988e713130SGlenn Strauss break; 998e713130SGlenn Strauss default: 1008e713130SGlenn Strauss break; 1018e713130SGlenn Strauss } 1028e713130SGlenn Strauss } 1038e713130SGlenn Strauss } 1048e713130SGlenn Strauss } 1058e713130SGlenn Strauss 1067ba06c71SGlenn Strauss /* data type for mod_auth structured data 1077ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 1087ba06c71SGlenn Strauss typedef struct { 1097ba06c71SGlenn Strauss DATA_UNSET; 1107ba06c71SGlenn Strauss http_auth_require_t *require; 1117ba06c71SGlenn Strauss } data_auth; 1127ba06c71SGlenn Strauss 1137ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 1147ba06c71SGlenn Strauss { 1157ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 116ad9b7e00SGlenn Strauss free(dauth->key.ptr); 1177ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 1187ba06c71SGlenn Strauss free(dauth); 1197ba06c71SGlenn Strauss } 1207ba06c71SGlenn Strauss 1217ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 1227ba06c71SGlenn Strauss { 1238c7f1dfbSGlenn Strauss static const struct data_methods fn = { 1248c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 1258c7f1dfbSGlenn Strauss data_auth_free, 1268c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 1278c7f1dfbSGlenn Strauss NULL /* print must not be called on this data */ 1288c7f1dfbSGlenn Strauss }; 1297ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 1307ba06c71SGlenn Strauss force_assert(NULL != dauth); 1317ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 1328c7f1dfbSGlenn Strauss dauth->fn = &fn; 1337ba06c71SGlenn Strauss 1347ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 1357ba06c71SGlenn Strauss 1367ba06c71SGlenn Strauss return dauth; 1377ba06c71SGlenn Strauss } 1387ba06c71SGlenn Strauss 139b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) { 140b9e2be50SGlenn Strauss size_t len; 141b9e2be50SGlenn Strauss if (NULL == s) { 142b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 143b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 144b9e2be50SGlenn Strauss return 1; 145b9e2be50SGlenn Strauss } 146b9e2be50SGlenn Strauss 147b9e2be50SGlenn Strauss len = strlen(s); 148b9e2be50SGlenn Strauss if (len > 5 149b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 150b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 151b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 152b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 153b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 154b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 155b9e2be50SGlenn Strauss len -= 5; 156b9e2be50SGlenn Strauss } 157b9e2be50SGlenn Strauss else { 158b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 159b9e2be50SGlenn Strauss } 160b9e2be50SGlenn Strauss 161b9e2be50SGlenn Strauss if (3 == len 162b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 163b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 164b9e2be50SGlenn Strauss && '5' == (s[2] )) { 165b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 166b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 167b9e2be50SGlenn Strauss return 1; 168b9e2be50SGlenn Strauss } 169*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 170b9e2be50SGlenn Strauss else if (len >= 7 171b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 172b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 173b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 174b9e2be50SGlenn Strauss && '-' == (s[3] )) { 175b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 176b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 177b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 178b9e2be50SGlenn Strauss return 1; 179b9e2be50SGlenn Strauss } 180b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 181b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 182b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 183b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 184b9e2be50SGlenn Strauss return 1; 185b9e2be50SGlenn Strauss } 186b9e2be50SGlenn Strauss #endif 187b9e2be50SGlenn Strauss } 188b9e2be50SGlenn Strauss #endif 189b9e2be50SGlenn Strauss return 0; /*(error)*/ 190b9e2be50SGlenn Strauss } 191b9e2be50SGlenn Strauss 192b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 193b9e2be50SGlenn Strauss for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 194b9e2be50SGlenn Strauss http_auth_info_t ai; 195b9e2be50SGlenn Strauss int rc; 196b9e2be50SGlenn Strauss p = strchr(s, '|'); 197b9e2be50SGlenn Strauss if (p) *p = '\0'; 198b9e2be50SGlenn Strauss rc = mod_auth_algorithm_parse(&ai, s); 199b9e2be50SGlenn Strauss if (p) *p = '|'; 200b9e2be50SGlenn Strauss if (!rc) return 0; 201b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 202b9e2be50SGlenn Strauss } 203b9e2be50SGlenn Strauss return 1; 204b9e2be50SGlenn Strauss } 205b9e2be50SGlenn Strauss 206b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh) 2077ba06c71SGlenn Strauss { 2087ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 2097ba06c71SGlenn Strauss 2107ba06c71SGlenn Strauss const char *str = b->ptr; 2117ba06c71SGlenn Strauss const char *p; 2127ba06c71SGlenn Strauss 2137ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 2147ba06c71SGlenn Strauss require->valid_user = 1; 2157ba06c71SGlenn Strauss return 1; /* success */ 2167ba06c71SGlenn Strauss } 2177ba06c71SGlenn Strauss 2187ba06c71SGlenn Strauss do { 2197ba06c71SGlenn Strauss const char *eq; 2207ba06c71SGlenn Strauss size_t len; 2217ba06c71SGlenn Strauss p = strchr(str, '|'); 2227ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 2237ba06c71SGlenn Strauss eq = memchr(str, '=', len); 2247ba06c71SGlenn Strauss if (NULL == eq) { 225b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 226010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' " 227010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 228010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 2297ba06c71SGlenn Strauss return 0; 2307ba06c71SGlenn Strauss } 231b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 232b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 233010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 234010c2894SGlenn Strauss "missing token after '=' " 235010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 236010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 2377ba06c71SGlenn Strauss return 0; 2387ba06c71SGlenn Strauss } 2397ba06c71SGlenn Strauss 2407ba06c71SGlenn Strauss switch ((int)(eq - str)) { 2417ba06c71SGlenn Strauss case 4: 2427ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 243810109ccSGlenn Strauss /*("user=" is 5)*/ 244a03afc90SGlenn Strauss array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN("")); 2457ba06c71SGlenn Strauss continue; 2467ba06c71SGlenn Strauss } 2477ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 248810109ccSGlenn Strauss /*("host=" is 5)*/ 249a03afc90SGlenn Strauss array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN("")); 250b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 251010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 252010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr); 2537ba06c71SGlenn Strauss continue; 2547ba06c71SGlenn Strauss } 2557ba06c71SGlenn Strauss break; /* to error */ 2567ba06c71SGlenn Strauss case 5: 2577ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 258810109ccSGlenn Strauss /*("group=" is 6)*/ 259a03afc90SGlenn Strauss array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN("")); 2609c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 261b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 262010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 263010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr); 2649c91af0cSGlenn Strauss #endif 2657ba06c71SGlenn Strauss continue; 2667ba06c71SGlenn Strauss } 2677ba06c71SGlenn Strauss break; /* to error */ 2687ba06c71SGlenn Strauss case 10: 2697ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 270b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 271010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 272010c2894SGlenn Strauss "valid user can not be combined with other require rules " 273010c2894SGlenn Strauss "(expecting \"valid-user\" or " 274010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr); 2757ba06c71SGlenn Strauss return 0; 2767ba06c71SGlenn Strauss } 2777ba06c71SGlenn Strauss break; /* to error */ 2787ba06c71SGlenn Strauss default: 2797ba06c71SGlenn Strauss break; /* to error */ 2807ba06c71SGlenn Strauss } 2817ba06c71SGlenn Strauss 282b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 283010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 284010c2894SGlenn Strauss "invalid/unsupported token " 285010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 286010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 2877ba06c71SGlenn Strauss return 0; 2887ba06c71SGlenn Strauss 2897ba06c71SGlenn Strauss } while (p && *((str = p+1))); 2907ba06c71SGlenn Strauss 2917ba06c71SGlenn Strauss return 1; /* success */ 2927ba06c71SGlenn Strauss } 2937ba06c71SGlenn Strauss 294b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh) 2958e713130SGlenn Strauss { 2968e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) { 2977ba06c71SGlenn Strauss size_t m; 2988e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n]; 2997ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 300e1188e77SGlenn Strauss const buffer *nonce_secret = NULL; 3017ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 302b9e2be50SGlenn Strauss buffer *algos = NULL; 303b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 3047ba06c71SGlenn Strauss 305c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 306b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 307010c2894SGlenn Strauss "unexpected value for auth.require; expected " 308bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 3097ba06c71SGlenn Strauss 3107ba06c71SGlenn Strauss return HANDLER_ERROR; 3117ba06c71SGlenn Strauss } 3127ba06c71SGlenn Strauss 313c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 314c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 315c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 316ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 317601c572cSGlenn Strauss method = &ds->value; 318ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 319601c572cSGlenn Strauss realm = &ds->value; 320ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 321601c572cSGlenn Strauss require = &ds->value; 322ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 323601c572cSGlenn Strauss algos = &ds->value; 324e1188e77SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret"))) { 325e1188e77SGlenn Strauss nonce_secret = &ds->value; 3267ba06c71SGlenn Strauss } else { 327b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 328010c2894SGlenn Strauss "the field is unknown in: " 329010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 330010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 3317ba06c71SGlenn Strauss 3327ba06c71SGlenn Strauss return HANDLER_ERROR; 3337ba06c71SGlenn Strauss } 3347ba06c71SGlenn Strauss } else { 335b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 336010c2894SGlenn Strauss "a string was expected for: " 337010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 338010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 3397ba06c71SGlenn Strauss 3407ba06c71SGlenn Strauss return HANDLER_ERROR; 3417ba06c71SGlenn Strauss } 3427ba06c71SGlenn Strauss } 3437ba06c71SGlenn Strauss 3447ba06c71SGlenn Strauss if (buffer_string_is_empty(method)) { 345b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 346010c2894SGlenn Strauss "the method field is missing or blank in: " 3477ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 3487ba06c71SGlenn Strauss return HANDLER_ERROR; 3497ba06c71SGlenn Strauss } else { 3507ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 3517ba06c71SGlenn Strauss if (NULL == auth_scheme) { 352b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 353010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in " 354010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr); 3557ba06c71SGlenn Strauss return HANDLER_ERROR; 3567ba06c71SGlenn Strauss } 3577ba06c71SGlenn Strauss } 3587ba06c71SGlenn Strauss 3597ba06c71SGlenn Strauss if (buffer_is_empty(realm)) { 360b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 361010c2894SGlenn Strauss "the realm field is missing in: " 3627ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 3637ba06c71SGlenn Strauss return HANDLER_ERROR; 3647ba06c71SGlenn Strauss } 3657ba06c71SGlenn Strauss 3667ba06c71SGlenn Strauss if (buffer_string_is_empty(require)) { 367b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 368010c2894SGlenn Strauss "the require field is missing or blank in: " 3697ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 3707ba06c71SGlenn Strauss return HANDLER_ERROR; 3717ba06c71SGlenn Strauss } 3727ba06c71SGlenn Strauss 373b9e2be50SGlenn Strauss if (buffer_string_is_empty(algos)) { 374b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 375b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 376b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 377010c2894SGlenn Strauss "invalid algorithm in: " 378b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 379b9e2be50SGlenn Strauss return HANDLER_ERROR; 380b9e2be50SGlenn Strauss } 381b9e2be50SGlenn Strauss 382609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 3837ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 384ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 3857ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 386b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 387a03afc90SGlenn Strauss dauth->require->realm = realm; 388e1188e77SGlenn Strauss dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/ 389b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) { 3908c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 3917ba06c71SGlenn Strauss return HANDLER_ERROR; 3927ba06c71SGlenn Strauss } 3938e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth); 3947ba06c71SGlenn Strauss } 3957ba06c71SGlenn Strauss } 3967ba06c71SGlenn Strauss 3977ba06c71SGlenn Strauss return HANDLER_GO_ON; 3987ba06c71SGlenn Strauss } 3997ba06c71SGlenn Strauss 4008e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 4018e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 4028e713130SGlenn Strauss case 0: /* auth.backend */ 4038e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 4048e713130SGlenn Strauss pconf->auth_backend = cpv->v.v; 4058e713130SGlenn Strauss break; 4068e713130SGlenn Strauss case 1: /* auth.require */ 4078e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 4088e713130SGlenn Strauss pconf->auth_require = cpv->v.v; 4098e713130SGlenn Strauss break; 4108e713130SGlenn Strauss case 2: /* auth.extern-authn */ 4118e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u; 4128e713130SGlenn Strauss default:/* should not happen */ 4138e713130SGlenn Strauss return; 4148e713130SGlenn Strauss } 4158e713130SGlenn Strauss } 4164a6ce4aeSJan Kneschke 4178e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 4188e713130SGlenn Strauss do { 4198e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv); 4208e713130SGlenn Strauss } while ((++cpv)->k_id != -1); 4218e713130SGlenn Strauss } 422bcdc6a3bSJan Kneschke 4237c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) { 424cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */ 425cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/ 4268e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 4277c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 4288e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 4298e713130SGlenn Strauss } 4308e713130SGlenn Strauss } 43136f64b26SGlenn Strauss 4328e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 4338e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 4348e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 4358e713130SGlenn Strauss T_CONFIG_STRING, 4368e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4378e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 43803b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 4398e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4408e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 4418e713130SGlenn Strauss T_CONFIG_BOOL, 4428e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4438e713130SGlenn Strauss ,{ NULL, 0, 4448e713130SGlenn Strauss T_CONFIG_UNSET, 4458e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 4468e713130SGlenn Strauss }; 447bcdc6a3bSJan Kneschke 4488e713130SGlenn Strauss plugin_data * const p = p_d; 4498e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 4508e713130SGlenn Strauss return HANDLER_ERROR; 451bcdc6a3bSJan Kneschke 4528e713130SGlenn Strauss /* process and validate config directives 4538e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 4548e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 4558e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 4568e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 4578e713130SGlenn Strauss switch (cpv->k_id) { 4588e713130SGlenn Strauss case 0: /* auth.backend */ 4598e713130SGlenn Strauss if (!buffer_string_is_empty(cpv->v.b)) { 4608e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 4618e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 4628e713130SGlenn Strauss if (NULL == auth_backend) { 4638e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 4648e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 4658e713130SGlenn Strauss return HANDLER_ERROR; 4668e713130SGlenn Strauss } 4678e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 4688e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4698e713130SGlenn Strauss } 4708e713130SGlenn Strauss break; 4718e713130SGlenn Strauss case 1: /* auth.require */ 47203b4c993SGlenn Strauss { 47324680a91SGlenn Strauss array * const a = array_init(4); 4748e713130SGlenn Strauss if (HANDLER_GO_ON != 475b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 4768e713130SGlenn Strauss array_free(a); 4778e713130SGlenn Strauss return HANDLER_ERROR; 4788e713130SGlenn Strauss } 4798e713130SGlenn Strauss cpv->v.a = a; 4808e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4818e713130SGlenn Strauss } 4828e713130SGlenn Strauss break; 4838e713130SGlenn Strauss case 2: /* auth.extern-authn */ 4848e713130SGlenn Strauss break; 4858e713130SGlenn Strauss default:/* should not happen */ 4868e713130SGlenn Strauss break; 487bcdc6a3bSJan Kneschke } 488bcdc6a3bSJan Kneschke } 489bcdc6a3bSJan Kneschke } 490bcdc6a3bSJan Kneschke 4918e713130SGlenn Strauss /* initialize p->defaults from global config context */ 4928e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 4938e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 4948e713130SGlenn Strauss if (-1 != cpv->k_id) 4958e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 496bcdc6a3bSJan Kneschke } 4978e713130SGlenn Strauss 4988e713130SGlenn Strauss return HANDLER_GO_ON; 4998e713130SGlenn Strauss } 500bcdc6a3bSJan Kneschke 5017c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) { 5027ba06c71SGlenn Strauss plugin_data *p = p_d; 503d61f3381SGlenn Strauss data_auth *dauth; 50481b2d1f0SGlenn Strauss 5057c7f8c46SGlenn Strauss mod_auth_patch_config(r, p); 50681b2d1f0SGlenn Strauss 5077ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 50881b2d1f0SGlenn Strauss 5097ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 5107ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 5117c7f8c46SGlenn Strauss dauth = (!r->conf.force_lowercase_filenames) 5127c7f8c46SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path) 5137c7f8c46SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path); 514d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 5157ba06c71SGlenn Strauss 516d61f3381SGlenn Strauss { 5177ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 518f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 5197c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 5203dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 521f54d628cSGlenn Strauss return HANDLER_GO_ON; 522f54d628cSGlenn Strauss } 523f54d628cSGlenn Strauss } 5247c7f8c46SGlenn Strauss return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend); 5257ba06c71SGlenn Strauss } 5267ba06c71SGlenn Strauss } 5277ba06c71SGlenn Strauss 5287ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 5297ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 5307ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 531e2de4e58SGlenn Strauss p->name = "auth"; 5327ba06c71SGlenn Strauss p->init = mod_auth_init; 5337ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 5347ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 5357ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 5367ba06c71SGlenn Strauss 53781b2d1f0SGlenn Strauss return 0; 53881b2d1f0SGlenn Strauss } 53981b2d1f0SGlenn Strauss 54081b2d1f0SGlenn Strauss 54181b2d1f0SGlenn Strauss 54281b2d1f0SGlenn Strauss 5437ba06c71SGlenn Strauss /* 5447ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 5457ba06c71SGlenn Strauss * 5467ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 5477ba06c71SGlenn Strauss */ 54881b2d1f0SGlenn Strauss 5497ba06c71SGlenn Strauss #include "base64.h" 5507ba06c71SGlenn Strauss #include "md5.h" 5517f4e156eSGlenn Strauss #include "rand.h" 5523dd3cde9SGlenn Strauss #include "http_header.h" 55381b2d1f0SGlenn Strauss 554010c2894SGlenn Strauss __attribute_cold__ 5557c7f8c46SGlenn Strauss static handler_t mod_auth_send_400_bad_request(request_st * const r) { 5564b3a91e6SGlenn Strauss 5574b3a91e6SGlenn Strauss /* a field was missing or invalid */ 5587c7f8c46SGlenn Strauss r->http_status = 400; /* Bad Request */ 5597c7f8c46SGlenn Strauss r->handler_module = NULL; 5604b3a91e6SGlenn Strauss 5614b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5624b3a91e6SGlenn Strauss } 5634b3a91e6SGlenn Strauss 5647c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(request_st * const r, const buffer * const realm) { 5657c7f8c46SGlenn Strauss r->http_status = 401; 5667c7f8c46SGlenn Strauss r->handler_module = NULL; 5674b3a91e6SGlenn Strauss 5687c7f8c46SGlenn Strauss buffer * const tb = r->tmp_buf; 569010c2894SGlenn Strauss buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\"")); 570010c2894SGlenn Strauss buffer_append_string_buffer(tb, realm); 571010c2894SGlenn Strauss buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\"")); 5724b3a91e6SGlenn Strauss 5737c7f8c46SGlenn Strauss http_header_response_set(r, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 5744b3a91e6SGlenn Strauss 5754b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5764b3a91e6SGlenn Strauss } 5774b3a91e6SGlenn Strauss 5787c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) { 5797c7f8c46SGlenn Strauss const buffer *b = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 58081b2d1f0SGlenn Strauss buffer *username; 58181b2d1f0SGlenn Strauss char *pw; 5827ba06c71SGlenn Strauss handler_t rc = HANDLER_UNSET; 58381b2d1f0SGlenn Strauss 5847ba06c71SGlenn Strauss UNUSED(p_d); 58581b2d1f0SGlenn Strauss 5867ba06c71SGlenn Strauss if (NULL == backend) { 5877c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "auth.backend not configured for %s", r->uri.path.ptr); 5887c7f8c46SGlenn Strauss r->http_status = 500; 5897c7f8c46SGlenn Strauss r->handler_module = NULL; 5907ba06c71SGlenn Strauss return HANDLER_FINISHED; 5917ba06c71SGlenn Strauss } 5927ba06c71SGlenn Strauss 5933dd3cde9SGlenn Strauss if (NULL == b) { 5947c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 5957ba06c71SGlenn Strauss } 5967ba06c71SGlenn Strauss 59713008156SGlenn Strauss if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) { 5987c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 5994b3a91e6SGlenn Strauss } 6009bc61f16SGlenn Strauss #ifdef __COVERITY__ 6013dd3cde9SGlenn Strauss if (buffer_string_length(b) < sizeof("Basic ")-1) { 6027c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 6039bc61f16SGlenn Strauss } 6049bc61f16SGlenn Strauss #endif 60581b2d1f0SGlenn Strauss 60681b2d1f0SGlenn Strauss username = buffer_init(); 60781b2d1f0SGlenn Strauss 6088047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 6097ba06c71SGlenn Strauss if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) { 6107c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1); 61181b2d1f0SGlenn Strauss 61281b2d1f0SGlenn Strauss buffer_free(username); 6137c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 61481b2d1f0SGlenn Strauss } 61581b2d1f0SGlenn Strauss 61681b2d1f0SGlenn Strauss /* r2 == user:password */ 61781b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 6187c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr); 61981b2d1f0SGlenn Strauss 62081b2d1f0SGlenn Strauss buffer_free(username); 6217c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 62281b2d1f0SGlenn Strauss } 62381b2d1f0SGlenn Strauss 62481b2d1f0SGlenn Strauss buffer_string_set_length(username, pw - username->ptr); 62581b2d1f0SGlenn Strauss pw++; 62681b2d1f0SGlenn Strauss 6277c7f8c46SGlenn Strauss rc = backend->basic(r, backend->p_d, require, username, pw); 6287ba06c71SGlenn Strauss switch (rc) { 6294b3a91e6SGlenn Strauss case HANDLER_GO_ON: 6307c7f8c46SGlenn Strauss http_auth_setenv(r, CONST_BUF_LEN(username), CONST_STR_LEN("Basic")); 6314b3a91e6SGlenn Strauss break; 6324b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 6334b3a91e6SGlenn Strauss case HANDLER_FINISHED: 6347ba06c71SGlenn Strauss break; 6354b3a91e6SGlenn Strauss case HANDLER_ERROR: 6364b3a91e6SGlenn Strauss default: 6377c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 638010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 6397c7f8c46SGlenn Strauss r->uri.path.ptr, username->ptr, r->con->dst_addr_buf->ptr); 6407c7f8c46SGlenn Strauss r->keep_alive = 0; /*(disable keep-alive if bad password)*/ 6417ba06c71SGlenn Strauss rc = HANDLER_UNSET; 6427ba06c71SGlenn Strauss break; 64381b2d1f0SGlenn Strauss } 64481b2d1f0SGlenn Strauss 6454b3a91e6SGlenn Strauss buffer_free(username); 6467c7f8c46SGlenn Strauss return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(r, require->realm); 64781b2d1f0SGlenn Strauss } 64881b2d1f0SGlenn Strauss 64981b2d1f0SGlenn Strauss 650*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 651b9e2be50SGlenn Strauss 652b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 653b9e2be50SGlenn Strauss SHA256_CTX ctx; 654b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 655b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 656b9e2be50SGlenn Strauss 657b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 658b9e2be50SGlenn Strauss SHA256_Init(&ctx); 659b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 660b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 661b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 662b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 663b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 664b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 665b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 666b9e2be50SGlenn Strauss } 667b9e2be50SGlenn Strauss 668b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 669b9e2be50SGlenn Strauss 670b9e2be50SGlenn Strauss /* calculate H(A2) */ 671b9e2be50SGlenn Strauss SHA256_Init(&ctx); 672b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)m, strlen(m)); 673b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 674b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 675b9e2be50SGlenn Strauss #if 0 676b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 677fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 678b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 679b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 680b9e2be50SGlenn Strauss } 681b9e2be50SGlenn Strauss #endif 682b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 683b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 684b9e2be50SGlenn Strauss 685b9e2be50SGlenn Strauss /* calculate response */ 686b9e2be50SGlenn Strauss SHA256_Init(&ctx); 687b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 688b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 689b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 690b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 691b9e2be50SGlenn Strauss if (qop && *qop) { 692b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 693b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 694b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 695b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 696b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 697b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 698b9e2be50SGlenn Strauss } 699b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 700b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 701b9e2be50SGlenn Strauss } 702b9e2be50SGlenn Strauss 703e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 704b9e2be50SGlenn Strauss SHA256_CTX ctx; 705f24e6d69SGlenn Strauss size_t len; 706b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 707b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 708b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 709b9e2be50SGlenn Strauss SHA256_Init(&ctx); 710f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 711f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 712f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 713f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 714e1188e77SGlenn Strauss if (secret) { 715e1188e77SGlenn Strauss len = buffer_string_length(secret); 716e1188e77SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)secret->ptr, len); 717e1188e77SGlenn Strauss } 718b9e2be50SGlenn Strauss SHA256_Final(h, &ctx); 719b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 720b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 721b9e2be50SGlenn Strauss } 722b9e2be50SGlenn Strauss 723b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 724b9e2be50SGlenn Strauss 725b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha512_256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 726b9e2be50SGlenn Strauss SHA512_CTX ctx; 727b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 728b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 729b9e2be50SGlenn Strauss 730b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 731b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 732b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 733b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 734b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 735b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 736b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 737b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 738b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 739b9e2be50SGlenn Strauss } 740b9e2be50SGlenn Strauss 741b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 742b9e2be50SGlenn Strauss 743b9e2be50SGlenn Strauss /* calculate H(A2) */ 744b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 745b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m)); 746b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 747b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 748b9e2be50SGlenn Strauss #if 0 749b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 750fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 751b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 752b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2); 753b9e2be50SGlenn Strauss } 754b9e2be50SGlenn Strauss #endif 755b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 756b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 757b9e2be50SGlenn Strauss 758b9e2be50SGlenn Strauss /* calculate response */ 759b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 760b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 761b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 762b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 763b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 764b9e2be50SGlenn Strauss if (qop && *qop) { 765b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 766b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 767b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 768b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 769b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 770b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 771b9e2be50SGlenn Strauss } 772b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 773b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 774b9e2be50SGlenn Strauss } 775b9e2be50SGlenn Strauss 776e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 777b9e2be50SGlenn Strauss SHA512_CTX ctx; 778f24e6d69SGlenn Strauss size_t len; 779b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN]; 780b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 781b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 782b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 783f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 784f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 785f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 786f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 787e1188e77SGlenn Strauss if (secret) { 788e1188e77SGlenn Strauss len = buffer_string_length(secret); 789e1188e77SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)secret->ptr, len); 790e1188e77SGlenn Strauss } 791b9e2be50SGlenn Strauss SHA512_256_Final(h, &ctx); 792b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 793b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 794b9e2be50SGlenn Strauss } 795b9e2be50SGlenn Strauss 796b9e2be50SGlenn Strauss #endif /* SHA512_256_DIGEST_LENGTH */ 797b9e2be50SGlenn Strauss 798*b28a3714SGlenn Strauss #endif /* USE_LIB_CRYPTO */ 799b9e2be50SGlenn Strauss 800b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_md5(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 801b9e2be50SGlenn Strauss li_MD5_CTX ctx; 802b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 803b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 804b9e2be50SGlenn Strauss 805b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 806b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 807b9e2be50SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 808b9e2be50SGlenn Strauss * Errata ID: 1649 */ 809b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 810b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 811b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 812b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 813b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 814b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 815b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 816b9e2be50SGlenn Strauss } 817b9e2be50SGlenn Strauss 818b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 819b9e2be50SGlenn Strauss 820b9e2be50SGlenn Strauss /* calculate H(A2) */ 821b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 822b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)m, strlen(m)); 823b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 824b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri)); 825b9e2be50SGlenn Strauss #if 0 826b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 827fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 828b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 829b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 830b9e2be50SGlenn Strauss } 831b9e2be50SGlenn Strauss #endif 832b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 833b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 834b9e2be50SGlenn Strauss 835b9e2be50SGlenn Strauss /* calculate response */ 836b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 837b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 838b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 839b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 840b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 841b9e2be50SGlenn Strauss if (qop && *qop) { 842b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc)); 843b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 844b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 845b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 846b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop)); 847b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 848b9e2be50SGlenn Strauss } 849b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 850b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 851b9e2be50SGlenn Strauss } 852b9e2be50SGlenn Strauss 853e1188e77SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 854b9e2be50SGlenn Strauss li_MD5_CTX ctx; 855f24e6d69SGlenn Strauss size_t len; 856b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN]; 857b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 858b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 859b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 860f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 861f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 862f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 863f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 864e1188e77SGlenn Strauss if (secret) { 865e1188e77SGlenn Strauss len = buffer_string_length(secret); 866e1188e77SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)secret->ptr, len); 867e1188e77SGlenn Strauss } 868b9e2be50SGlenn Strauss li_MD5_Final(h, &ctx); 869b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 870b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 871b9e2be50SGlenn Strauss } 872b9e2be50SGlenn Strauss 873b9e2be50SGlenn Strauss static void mod_auth_digest_mutate(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 874b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_MD5) 875b9e2be50SGlenn Strauss mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop); 876*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 877b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 878b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop); 879b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 880b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 881b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop); 882b9e2be50SGlenn Strauss #endif 883b9e2be50SGlenn Strauss #endif 884b9e2be50SGlenn Strauss } 885b9e2be50SGlenn Strauss 886e1188e77SGlenn Strauss static void mod_auth_append_nonce(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) { 887e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 888e1188e77SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 889e1188e77SGlenn Strauss const buffer * const nonce_secret = require->nonce_secret; 890e1188e77SGlenn Strauss int rnd; 891e1188e77SGlenn Strauss if (NULL == nonce_secret) 892e1188e77SGlenn Strauss rnd = rndptr ? *rndptr : li_rand_pseudo(); 893e1188e77SGlenn Strauss else { /*(do not directly expose random number generator single value)*/ 894e1188e77SGlenn Strauss rndptr 895e1188e77SGlenn Strauss ? (void)(rnd = *rndptr) 896e1188e77SGlenn Strauss : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd)); 897e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)rnd); 898e1188e77SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 899e1188e77SGlenn Strauss } 900e1188e77SGlenn Strauss switch (dalgo) { 901*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 902e1188e77SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 903e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 904e1188e77SGlenn Strauss mod_auth_digest_nonce_sha512_256(b, cur_ts, rnd, nonce_secret); 905e1188e77SGlenn Strauss break; 906e1188e77SGlenn Strauss #endif 907e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 908e1188e77SGlenn Strauss mod_auth_digest_nonce_sha256(b, cur_ts, rnd, nonce_secret); 909e1188e77SGlenn Strauss break; 910e1188e77SGlenn Strauss #endif 911e1188e77SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 912e1188e77SGlenn Strauss default: 913e1188e77SGlenn Strauss mod_auth_digest_nonce_md5(b, cur_ts, rnd, nonce_secret); 914e1188e77SGlenn Strauss break; 915e1188e77SGlenn Strauss } 916e1188e77SGlenn Strauss } 917e1188e77SGlenn Strauss 918b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) { 919b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 920b9e2be50SGlenn Strauss int n = 0; 921e1188e77SGlenn Strauss int algoid[3]; 922b9e2be50SGlenn Strauss unsigned int algolen[3]; 923b9e2be50SGlenn Strauss const char *algoname[3]; 924*b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 925b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 926b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 927e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA512_256; 928b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 929b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 930b9e2be50SGlenn Strauss ++n; 931b9e2be50SGlenn Strauss } 932b9e2be50SGlenn Strauss #endif 933b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 934e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA256; 935b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 936b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 937b9e2be50SGlenn Strauss ++n; 938b9e2be50SGlenn Strauss } 939b9e2be50SGlenn Strauss #endif 940b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 941e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_MD5; 942b9e2be50SGlenn Strauss algoname[n] = "MD5"; 943b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 944b9e2be50SGlenn Strauss ++n; 945b9e2be50SGlenn Strauss } 946b9e2be50SGlenn Strauss 947b9e2be50SGlenn Strauss buffer_clear(b); 948b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 949b9e2be50SGlenn Strauss if (i > 0) { 950b9e2be50SGlenn Strauss buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: ")); 951b9e2be50SGlenn Strauss } 952b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\"")); 953b9e2be50SGlenn Strauss buffer_append_string_buffer(b, require->realm); 9541799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=")); 955b9e2be50SGlenn Strauss buffer_append_string_len(b, algoname[i], algolen[i]); 9561799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", nonce=\"")); 957e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL); 958b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 959b9e2be50SGlenn Strauss if (nonce_stale) { 960b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 961b9e2be50SGlenn Strauss } 962b9e2be50SGlenn Strauss } 96381b2d1f0SGlenn Strauss } 96481b2d1f0SGlenn Strauss 965e1188e77SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo) { 9666ad325c6SGlenn Strauss buffer_clear(b); 9676ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 968e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL); 9696ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\"")); 9706ad325c6SGlenn Strauss } 9716ad325c6SGlenn Strauss 97281b2d1f0SGlenn Strauss typedef struct { 97381b2d1f0SGlenn Strauss const char *key; 97481b2d1f0SGlenn Strauss int key_len; 97581b2d1f0SGlenn Strauss char **ptr; 97681b2d1f0SGlenn Strauss } digest_kv; 97781b2d1f0SGlenn Strauss 9787c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st *r, const struct http_auth_require_t *require, int nonce_stale); 9797ba06c71SGlenn Strauss 9807c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) { 9817c7f8c46SGlenn Strauss const buffer *vb = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 9827ba06c71SGlenn Strauss 98381b2d1f0SGlenn Strauss char *username = NULL; 98481b2d1f0SGlenn Strauss char *realm = NULL; 98581b2d1f0SGlenn Strauss char *nonce = NULL; 98681b2d1f0SGlenn Strauss char *uri = NULL; 98781b2d1f0SGlenn Strauss char *algorithm = NULL; 98881b2d1f0SGlenn Strauss char *qop = NULL; 98981b2d1f0SGlenn Strauss char *cnonce = NULL; 99081b2d1f0SGlenn Strauss char *nc = NULL; 99181b2d1f0SGlenn Strauss char *respons = NULL; 99281b2d1f0SGlenn Strauss 99381b2d1f0SGlenn Strauss char *e, *c; 99481b2d1f0SGlenn Strauss int i; 99581b2d1f0SGlenn Strauss buffer *b; 99660f4cf3aSGlenn Strauss http_auth_info_t ai; 997b9e2be50SGlenn Strauss unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 99881b2d1f0SGlenn Strauss 99981b2d1f0SGlenn Strauss 100081b2d1f0SGlenn Strauss /* init pointers */ 100181b2d1f0SGlenn Strauss #define S(x) \ 100281b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 100381b2d1f0SGlenn Strauss digest_kv dkv[10] = { 100481b2d1f0SGlenn Strauss { S("username=") }, 100581b2d1f0SGlenn Strauss { S("realm=") }, 100681b2d1f0SGlenn Strauss { S("nonce=") }, 100781b2d1f0SGlenn Strauss { S("uri=") }, 100881b2d1f0SGlenn Strauss { S("algorithm=") }, 100981b2d1f0SGlenn Strauss { S("qop=") }, 101081b2d1f0SGlenn Strauss { S("cnonce=") }, 101181b2d1f0SGlenn Strauss { S("nc=") }, 101281b2d1f0SGlenn Strauss { S("response=") }, 101381b2d1f0SGlenn Strauss 101481b2d1f0SGlenn Strauss { NULL, 0, NULL } 101581b2d1f0SGlenn Strauss }; 101681b2d1f0SGlenn Strauss #undef S 101781b2d1f0SGlenn Strauss 101881b2d1f0SGlenn Strauss dkv[0].ptr = &username; 101981b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 102081b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 102181b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 102281b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 102381b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 102481b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 102581b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 102681b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 102781b2d1f0SGlenn Strauss 10287ba06c71SGlenn Strauss UNUSED(p_d); 10297ba06c71SGlenn Strauss 10307ba06c71SGlenn Strauss if (NULL == backend) { 10317c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 10327c7f8c46SGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 10337c7f8c46SGlenn Strauss r->http_status = 500; 10347c7f8c46SGlenn Strauss r->handler_module = NULL; 10357ba06c71SGlenn Strauss return HANDLER_FINISHED; 10367ba06c71SGlenn Strauss } 10377ba06c71SGlenn Strauss 10383dd3cde9SGlenn Strauss if (NULL == vb) { 10397c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 10407ba06c71SGlenn Strauss } 10417ba06c71SGlenn Strauss 104213008156SGlenn Strauss if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) { 10437c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1044dadfb5fcSGlenn Strauss } else { 10453dd3cde9SGlenn Strauss size_t n = buffer_string_length(vb); 10469bc61f16SGlenn Strauss #ifdef __COVERITY__ 1047dadfb5fcSGlenn Strauss if (n < sizeof("Digest ")-1) { 10487c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 10499bc61f16SGlenn Strauss } 10509bc61f16SGlenn Strauss #endif 1051dadfb5fcSGlenn Strauss n -= (sizeof("Digest ")-1); 10527ba06c71SGlenn Strauss b = buffer_init(); 10533dd3cde9SGlenn Strauss buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n); 1054dadfb5fcSGlenn Strauss } 105581b2d1f0SGlenn Strauss 105681b2d1f0SGlenn Strauss /* parse credentials from client */ 105781b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 105881b2d1f0SGlenn Strauss /* skip whitespaces */ 105981b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 106081b2d1f0SGlenn Strauss if (!*c) break; 106181b2d1f0SGlenn Strauss 106281b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 106381b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 106481b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 106581b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 106681b2d1f0SGlenn Strauss /* value with "..." */ 106781b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 106881b2d1f0SGlenn Strauss c = e; 106981b2d1f0SGlenn Strauss 107081b2d1f0SGlenn Strauss *e = '\0'; 107181b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 107281b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 107381b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 107481b2d1f0SGlenn Strauss c = e; 107581b2d1f0SGlenn Strauss 107681b2d1f0SGlenn Strauss *e = '\0'; 107781b2d1f0SGlenn Strauss } else { 107881b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 107981b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 108081b2d1f0SGlenn Strauss c += strlen(c) - 1; 108181b2d1f0SGlenn Strauss } 108281b2d1f0SGlenn Strauss break; 108381b2d1f0SGlenn Strauss } 108481b2d1f0SGlenn Strauss } 108581b2d1f0SGlenn Strauss } 108681b2d1f0SGlenn Strauss 108781b2d1f0SGlenn Strauss /* check if everything is transmitted */ 108881b2d1f0SGlenn Strauss if (!username || 108981b2d1f0SGlenn Strauss !realm || 109081b2d1f0SGlenn Strauss !nonce || 109181b2d1f0SGlenn Strauss !uri || 109281b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 109381b2d1f0SGlenn Strauss !respons ) { 109481b2d1f0SGlenn Strauss /* missing field */ 109581b2d1f0SGlenn Strauss 10967c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 109781b2d1f0SGlenn Strauss "digest: missing field"); 109881b2d1f0SGlenn Strauss 109981b2d1f0SGlenn Strauss buffer_free(b); 11007c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 110181b2d1f0SGlenn Strauss } 110281b2d1f0SGlenn Strauss 110360f4cf3aSGlenn Strauss ai.username = username; 110460f4cf3aSGlenn Strauss ai.ulen = strlen(username); 110560f4cf3aSGlenn Strauss ai.realm = realm; 110660f4cf3aSGlenn Strauss ai.rlen = strlen(realm); 110760f4cf3aSGlenn Strauss 110860f4cf3aSGlenn Strauss if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) { 11097c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 11105a6e4df8SGlenn Strauss "digest: realm mismatch"); 11115a6e4df8SGlenn Strauss buffer_free(b); 11127c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 11135a6e4df8SGlenn Strauss } 11145a6e4df8SGlenn Strauss 1115b9e2be50SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, algorithm) 1116b9e2be50SGlenn Strauss || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 11177c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1118010c2894SGlenn Strauss "digest: (%s): invalid", algorithm); 1119b9e2be50SGlenn Strauss buffer_free(b); 11207c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 1121b9e2be50SGlenn Strauss } 1122b9e2be50SGlenn Strauss 112381b2d1f0SGlenn Strauss /** 112481b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 112581b2d1f0SGlenn Strauss */ 1126b9e2be50SGlenn Strauss if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) { 11277c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1128010c2894SGlenn Strauss "digest: (%s): missing field", algorithm); 112981b2d1f0SGlenn Strauss 113081b2d1f0SGlenn Strauss buffer_free(b); 11317c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 113281b2d1f0SGlenn Strauss } 113381b2d1f0SGlenn Strauss 1134b9e2be50SGlenn Strauss { 1135b9e2be50SGlenn Strauss size_t resplen = strlen(respons); 1136b9e2be50SGlenn Strauss if (0 != http_auth_digest_hex2bin(respons, resplen, 1137b9e2be50SGlenn Strauss rdigest, sizeof(rdigest)) 1138b9e2be50SGlenn Strauss || resplen != (ai.dlen << 1)) { 11397c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1140010c2894SGlenn Strauss "digest: (%s): invalid format", respons); 1141b9e2be50SGlenn Strauss buffer_free(b); 11427c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 1143b9e2be50SGlenn Strauss } 1144b9e2be50SGlenn Strauss } 1145b9e2be50SGlenn Strauss 1146fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 11477c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 114881b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 114981b2d1f0SGlenn Strauss 115081b2d1f0SGlenn Strauss buffer_free(b); 11517c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 115281b2d1f0SGlenn Strauss } 115381b2d1f0SGlenn Strauss 115481b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 115581b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 115681b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 115781b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 115881b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 115981b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 11607c7f8c46SGlenn Strauss * uri validation step is bypassed. r->target_orig is 11618131e439SGlenn Strauss * original request-target sent in client request. */ 116281b2d1f0SGlenn Strauss { 116381b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 11647c7f8c46SGlenn Strauss if (!buffer_is_equal_string(&r->target_orig, uri, ulen)) { 11657c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1166010c2894SGlenn Strauss "digest: auth failed: uri mismatch (%s != %s), IP: %s", 11677c7f8c46SGlenn Strauss r->target_orig.ptr, uri, r->con->dst_addr_buf->ptr); 116881b2d1f0SGlenn Strauss buffer_free(b); 11697c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 117081b2d1f0SGlenn Strauss } 117181b2d1f0SGlenn Strauss } 117281b2d1f0SGlenn Strauss 1173e1188e77SGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 1174e1188e77SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 1175e1188e77SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 1176e1188e77SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 1177e1188e77SGlenn Strauss * verifiable) if nanoseconds were included with seconds as part of the 1178e1188e77SGlenn Strauss * nonce "timestamp:secret". However, doing so would expose a high 1179e1188e77SGlenn Strauss * precision timestamp of the system to attackers. timestamp in nonce 1180e1188e77SGlenn Strauss * could theoretically be modified and still produce same md5sum, but 1181e1188e77SGlenn Strauss * that is highly unlikely within a 10 min (moving) window of valid 1182e1188e77SGlenn Strauss * time relative to current time (now). 1183e1188e77SGlenn Strauss * If it is desired to validate that nonces were generated by server, 1184e1188e77SGlenn Strauss * then specify auth.require = ( ... => ( "secret" => "..." ) ) 1185e1188e77SGlenn Strauss * When secret is specified, then instead of nanoseconds, the random 1186e1188e77SGlenn Strauss * data value (included for unique nonces) will be exposed in the nonce 1187e1188e77SGlenn Strauss * along with the timestamp, and the additional secret will be used to 1188e1188e77SGlenn Strauss * validate that the server generated the nonce using that secret. */ 1189e1188e77SGlenn Strauss int send_nextnonce; 1190e1188e77SGlenn Strauss { 1191e1188e77SGlenn Strauss time_t ts = 0; 1192e1188e77SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 1193e1188e77SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 1194e1188e77SGlenn Strauss ts = (ts << 4) + hex2int(nonce_uns[i]); 1195e1188e77SGlenn Strauss } 1196e1188e77SGlenn Strauss const time_t cur_ts = log_epoch_secs; 1197e1188e77SGlenn Strauss if (nonce[i] != ':' 1198e1188e77SGlenn Strauss || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 1199e1188e77SGlenn Strauss /* nonce is stale; have client regenerate digest */ 1200e1188e77SGlenn Strauss buffer_free(b); 1201e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, ai.dalgo); 1202e1188e77SGlenn Strauss } 1203e1188e77SGlenn Strauss 1204e1188e77SGlenn Strauss send_nextnonce = (cur_ts - ts > 540); /*(9 mins)*/ 1205e1188e77SGlenn Strauss 1206e1188e77SGlenn Strauss if (require->nonce_secret) { 1207e1188e77SGlenn Strauss unsigned int rnd = 0; 1208e1188e77SGlenn Strauss for (int j = i+8; i < j && light_isxdigit(nonce_uns[i]); ++i) { 1209e1188e77SGlenn Strauss rnd = (rnd << 4) + hex2int(nonce_uns[i]); 1210e1188e77SGlenn Strauss } 1211e1188e77SGlenn Strauss if (nonce[i] != ':') { 1212e1188e77SGlenn Strauss /* nonce is invalid; 1213e1188e77SGlenn Strauss * expect extra field w/ require->nonce_secret */ 1214e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1215e1188e77SGlenn Strauss "digest: nonce invalid"); 1216e1188e77SGlenn Strauss buffer_free(b); 1217e1188e77SGlenn Strauss return mod_auth_send_400_bad_request(r); 1218e1188e77SGlenn Strauss } 1219e1188e77SGlenn Strauss buffer * const tb = r->tmp_buf; 1220e1188e77SGlenn Strauss buffer_clear(tb); 1221e1188e77SGlenn Strauss mod_auth_append_nonce(tb, cur_ts, require, ai.dalgo, (int *)&rnd); 1222e1188e77SGlenn Strauss if (!buffer_eq_slen(tb, nonce, strlen(nonce))) { 1223e1188e77SGlenn Strauss /* nonce not generated using current require->nonce_secret */ 1224e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1225e1188e77SGlenn Strauss "digest: nonce mismatch"); 1226e1188e77SGlenn Strauss buffer_free(b); 1227e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1228e1188e77SGlenn Strauss } 1229e1188e77SGlenn Strauss } 1230e1188e77SGlenn Strauss } 1231e1188e77SGlenn Strauss 12327c7f8c46SGlenn Strauss switch (backend->digest(r, backend->p_d, &ai)) { 12334b3a91e6SGlenn Strauss case HANDLER_GO_ON: 12344b3a91e6SGlenn Strauss break; 12354b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 123681b2d1f0SGlenn Strauss buffer_free(b); 12374b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 12384b3a91e6SGlenn Strauss case HANDLER_FINISHED: 12394b3a91e6SGlenn Strauss buffer_free(b); 12404b3a91e6SGlenn Strauss return HANDLER_FINISHED; 12414b3a91e6SGlenn Strauss case HANDLER_ERROR: 12424b3a91e6SGlenn Strauss default: 12437c7f8c46SGlenn Strauss r->keep_alive = 0; /*(disable keep-alive if unknown user)*/ 12444b3a91e6SGlenn Strauss buffer_free(b); 12457c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 124681b2d1f0SGlenn Strauss } 124781b2d1f0SGlenn Strauss 1248e1188e77SGlenn Strauss const char *m = get_http_method_name(r->http_method); 1249e1188e77SGlenn Strauss force_assert(m); 1250e1188e77SGlenn Strauss 1251b9e2be50SGlenn Strauss mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop); 125281b2d1f0SGlenn Strauss 12530e749c1cSGlenn Strauss if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) { 125481b2d1f0SGlenn Strauss /* digest not ok */ 12557c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1256010c2894SGlenn Strauss "digest: auth failed for %s: wrong password, IP: %s", 12577c7f8c46SGlenn Strauss username, r->con->dst_addr_buf->ptr); 12587c7f8c46SGlenn Strauss r->keep_alive = 0; /*(disable keep-alive if bad password)*/ 125981b2d1f0SGlenn Strauss 126081b2d1f0SGlenn Strauss buffer_free(b); 12617c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 126281b2d1f0SGlenn Strauss } 126381b2d1f0SGlenn Strauss 126481b2d1f0SGlenn Strauss /* value is our allow-rules */ 12657ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 12664b3a91e6SGlenn Strauss buffer_free(b); 12677c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 126881b2d1f0SGlenn Strauss } 126981b2d1f0SGlenn Strauss 1270e1188e77SGlenn Strauss if (send_nextnonce) { 12716ad325c6SGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 12727c7f8c46SGlenn Strauss buffer * const tb = r->tmp_buf; 1273e1188e77SGlenn Strauss const time_t cur_ts = log_epoch_secs; 1274e1188e77SGlenn Strauss mod_auth_digest_authentication_info(tb, cur_ts, require, ai.dalgo); 12757c7f8c46SGlenn Strauss http_header_response_set(r, HTTP_HEADER_OTHER, 1276409bba80SGlenn Strauss CONST_STR_LEN("Authentication-Info"), 1277409bba80SGlenn Strauss CONST_BUF_LEN(tb)); 12786ad325c6SGlenn Strauss } 127981b2d1f0SGlenn Strauss 12807c7f8c46SGlenn Strauss http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 128181b2d1f0SGlenn Strauss 128281b2d1f0SGlenn Strauss buffer_free(b); 128381b2d1f0SGlenn Strauss 12844b3a91e6SGlenn Strauss return HANDLER_GO_ON; 128581b2d1f0SGlenn Strauss } 128681b2d1f0SGlenn Strauss 12877c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) { 12887c7f8c46SGlenn Strauss buffer * const tb = r->tmp_buf; 1289b5775b99SGlenn Strauss mod_auth_digest_www_authenticate(tb, log_epoch_secs, require, nonce_stale); 12907c7f8c46SGlenn Strauss http_header_response_set(r, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 129181b2d1f0SGlenn Strauss 12927c7f8c46SGlenn Strauss r->http_status = 401; 12937c7f8c46SGlenn Strauss r->handler_module = NULL; 1294f9d58670SStefan Bühler return HANDLER_FINISHED; 12957ba06c71SGlenn Strauss } 12967ba06c71SGlenn Strauss 12977c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) { 12987ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 12997c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 13007ba06c71SGlenn Strauss UNUSED(p_d); 13017ba06c71SGlenn Strauss UNUSED(backend); 13023dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1303f9d58670SStefan Bühler return HANDLER_GO_ON; 1304bcdc6a3bSJan Kneschke } else { 13057c7f8c46SGlenn Strauss r->http_status = 401; 13067c7f8c46SGlenn Strauss r->handler_module = NULL; 1307bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 13087187271fSStefan Bühler } 1309bcdc6a3bSJan Kneschke } 1310