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 9b9e2be50SGlenn Strauss #include "sys-crypto.h" /* USE_OPENSSL_CRYPTO */ 10b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 11b9e2be50SGlenn Strauss #include <openssl/sha.h> 12b9e2be50SGlenn Strauss #endif 13b9e2be50SGlenn Strauss 14bcdc6a3bSJan Kneschke #include <stdlib.h> 15bcdc6a3bSJan Kneschke #include <string.h> 16bcdc6a3bSJan Kneschke 17bcdc6a3bSJan Kneschke /** 187ba06c71SGlenn Strauss * auth framework 19bcdc6a3bSJan Kneschke */ 20bcdc6a3bSJan Kneschke 214b3a91e6SGlenn Strauss typedef struct { 224b3a91e6SGlenn Strauss const http_auth_backend_t *auth_backend; 238e713130SGlenn Strauss const array *auth_require; 248e713130SGlenn Strauss unsigned int auth_extern_authn; 257ba06c71SGlenn Strauss } plugin_config; 264b3a91e6SGlenn Strauss 274b3a91e6SGlenn Strauss typedef struct { 284b3a91e6SGlenn Strauss PLUGIN_DATA; 298e713130SGlenn Strauss plugin_config defaults; 307ba06c71SGlenn Strauss plugin_config conf; 317ba06c71SGlenn Strauss } plugin_data; 327ba06c71SGlenn Strauss 33010c2894SGlenn Strauss static handler_t mod_auth_check_basic(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 34010c2894SGlenn Strauss static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 35010c2894SGlenn Strauss static handler_t mod_auth_check_extern(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 364b3a91e6SGlenn Strauss 37bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 387ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 397ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 407ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 417ba06c71SGlenn Strauss plugin_data *p; 427ba06c71SGlenn Strauss 437ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 447ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 457ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 467ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 47bcdc6a3bSJan Kneschke 48bcdc6a3bSJan Kneschke p = calloc(1, sizeof(*p)); 49bcdc6a3bSJan Kneschke 50bcdc6a3bSJan Kneschke return p; 51bcdc6a3bSJan Kneschke } 52bcdc6a3bSJan Kneschke 53b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) { 54b73949e0SGlenn Strauss plugin_data * const p = p_d; 558e713130SGlenn Strauss if (NULL == p->cvlist) return; 568e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 578e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 588e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 598e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 608e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 618e713130SGlenn Strauss switch (cpv->k_id) { 628e713130SGlenn Strauss case 1: /* auth.require */ 638e713130SGlenn Strauss array_free(cpv->v.v); 648e713130SGlenn Strauss break; 658e713130SGlenn Strauss default: 668e713130SGlenn Strauss break; 678e713130SGlenn Strauss } 688e713130SGlenn Strauss } 698e713130SGlenn Strauss } 708e713130SGlenn Strauss } 718e713130SGlenn Strauss 727ba06c71SGlenn Strauss /* data type for mod_auth structured data 737ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 747ba06c71SGlenn Strauss typedef struct { 757ba06c71SGlenn Strauss DATA_UNSET; 767ba06c71SGlenn Strauss http_auth_require_t *require; 777ba06c71SGlenn Strauss } data_auth; 787ba06c71SGlenn Strauss 797ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 807ba06c71SGlenn Strauss { 817ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 82ad9b7e00SGlenn Strauss free(dauth->key.ptr); 837ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 847ba06c71SGlenn Strauss free(dauth); 857ba06c71SGlenn Strauss } 867ba06c71SGlenn Strauss 877ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 887ba06c71SGlenn Strauss { 898c7f1dfbSGlenn Strauss static const struct data_methods fn = { 908c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 918c7f1dfbSGlenn Strauss data_auth_free, 928c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 938c7f1dfbSGlenn Strauss NULL /* print must not be called on this data */ 948c7f1dfbSGlenn Strauss }; 957ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 967ba06c71SGlenn Strauss force_assert(NULL != dauth); 977ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 988c7f1dfbSGlenn Strauss dauth->fn = &fn; 997ba06c71SGlenn Strauss 1007ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 1017ba06c71SGlenn Strauss 1027ba06c71SGlenn Strauss return dauth; 1037ba06c71SGlenn Strauss } 1047ba06c71SGlenn Strauss 105b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) { 106b9e2be50SGlenn Strauss size_t len; 107b9e2be50SGlenn Strauss if (NULL == s) { 108b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 109b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 110b9e2be50SGlenn Strauss return 1; 111b9e2be50SGlenn Strauss } 112b9e2be50SGlenn Strauss 113b9e2be50SGlenn Strauss len = strlen(s); 114b9e2be50SGlenn Strauss if (len > 5 115b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 116b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 117b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 118b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 119b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 120b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 121b9e2be50SGlenn Strauss len -= 5; 122b9e2be50SGlenn Strauss } 123b9e2be50SGlenn Strauss else { 124b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 125b9e2be50SGlenn Strauss } 126b9e2be50SGlenn Strauss 127b9e2be50SGlenn Strauss if (3 == len 128b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 129b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 130b9e2be50SGlenn Strauss && '5' == (s[2] )) { 131b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 132b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 133b9e2be50SGlenn Strauss return 1; 134b9e2be50SGlenn Strauss } 135b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 136b9e2be50SGlenn Strauss else if (len >= 7 137b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 138b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 139b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 140b9e2be50SGlenn Strauss && '-' == (s[3] )) { 141b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 142b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 143b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 144b9e2be50SGlenn Strauss return 1; 145b9e2be50SGlenn Strauss } 146b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 147b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 148b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 149b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 150b9e2be50SGlenn Strauss return 1; 151b9e2be50SGlenn Strauss } 152b9e2be50SGlenn Strauss #endif 153b9e2be50SGlenn Strauss } 154b9e2be50SGlenn Strauss #endif 155b9e2be50SGlenn Strauss return 0; /*(error)*/ 156b9e2be50SGlenn Strauss } 157b9e2be50SGlenn Strauss 158b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 159b9e2be50SGlenn Strauss for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 160b9e2be50SGlenn Strauss http_auth_info_t ai; 161b9e2be50SGlenn Strauss int rc; 162b9e2be50SGlenn Strauss p = strchr(s, '|'); 163b9e2be50SGlenn Strauss if (p) *p = '\0'; 164b9e2be50SGlenn Strauss rc = mod_auth_algorithm_parse(&ai, s); 165b9e2be50SGlenn Strauss if (p) *p = '|'; 166b9e2be50SGlenn Strauss if (!rc) return 0; 167b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 168b9e2be50SGlenn Strauss } 169b9e2be50SGlenn Strauss return 1; 170b9e2be50SGlenn Strauss } 171b9e2be50SGlenn Strauss 172b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh) 1737ba06c71SGlenn Strauss { 1747ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 1757ba06c71SGlenn Strauss 1767ba06c71SGlenn Strauss const char *str = b->ptr; 1777ba06c71SGlenn Strauss const char *p; 1787ba06c71SGlenn Strauss 1797ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 1807ba06c71SGlenn Strauss require->valid_user = 1; 1817ba06c71SGlenn Strauss return 1; /* success */ 1827ba06c71SGlenn Strauss } 1837ba06c71SGlenn Strauss 1847ba06c71SGlenn Strauss do { 1857ba06c71SGlenn Strauss const char *eq; 1867ba06c71SGlenn Strauss size_t len; 1877ba06c71SGlenn Strauss p = strchr(str, '|'); 1887ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 1897ba06c71SGlenn Strauss eq = memchr(str, '=', len); 1907ba06c71SGlenn Strauss if (NULL == eq) { 191b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 192010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' " 193010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 194010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 1957ba06c71SGlenn Strauss return 0; 1967ba06c71SGlenn Strauss } 197b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 198b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 199010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 200010c2894SGlenn Strauss "missing token after '=' " 201010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 202010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 2037ba06c71SGlenn Strauss return 0; 2047ba06c71SGlenn Strauss } 2057ba06c71SGlenn Strauss 2067ba06c71SGlenn Strauss switch ((int)(eq - str)) { 2077ba06c71SGlenn Strauss case 4: 2087ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 209810109ccSGlenn Strauss /*("user=" is 5)*/ 210a03afc90SGlenn Strauss array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN("")); 2117ba06c71SGlenn Strauss continue; 2127ba06c71SGlenn Strauss } 2137ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 214810109ccSGlenn Strauss /*("host=" is 5)*/ 215a03afc90SGlenn Strauss array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN("")); 216b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 217010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 218010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr); 2197ba06c71SGlenn Strauss continue; 2207ba06c71SGlenn Strauss } 2217ba06c71SGlenn Strauss break; /* to error */ 2227ba06c71SGlenn Strauss case 5: 2237ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 224810109ccSGlenn Strauss /*("group=" is 6)*/ 225a03afc90SGlenn Strauss array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN("")); 2269c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 227b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 228010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 229010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr); 2309c91af0cSGlenn Strauss #endif 2317ba06c71SGlenn Strauss continue; 2327ba06c71SGlenn Strauss } 2337ba06c71SGlenn Strauss break; /* to error */ 2347ba06c71SGlenn Strauss case 10: 2357ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 236b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 237010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 238010c2894SGlenn Strauss "valid user can not be combined with other require rules " 239010c2894SGlenn Strauss "(expecting \"valid-user\" or " 240010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr); 2417ba06c71SGlenn Strauss return 0; 2427ba06c71SGlenn Strauss } 2437ba06c71SGlenn Strauss break; /* to error */ 2447ba06c71SGlenn Strauss default: 2457ba06c71SGlenn Strauss break; /* to error */ 2467ba06c71SGlenn Strauss } 2477ba06c71SGlenn Strauss 248b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 249010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 250010c2894SGlenn Strauss "invalid/unsupported token " 251010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 252010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 2537ba06c71SGlenn Strauss return 0; 2547ba06c71SGlenn Strauss 2557ba06c71SGlenn Strauss } while (p && *((str = p+1))); 2567ba06c71SGlenn Strauss 2577ba06c71SGlenn Strauss return 1; /* success */ 2587ba06c71SGlenn Strauss } 2597ba06c71SGlenn Strauss 260b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh) 2618e713130SGlenn Strauss { 2628e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) { 2637ba06c71SGlenn Strauss size_t m; 2648e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n]; 2657ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 2667ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 267b9e2be50SGlenn Strauss buffer *algos = NULL; 268b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 2697ba06c71SGlenn Strauss 270c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 271b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 272010c2894SGlenn Strauss "unexpected value for auth.require; expected " 273bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 2747ba06c71SGlenn Strauss 2757ba06c71SGlenn Strauss return HANDLER_ERROR; 2767ba06c71SGlenn Strauss } 2777ba06c71SGlenn Strauss 278c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 279c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 280c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 281ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 282601c572cSGlenn Strauss method = &ds->value; 283ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 284601c572cSGlenn Strauss realm = &ds->value; 285ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 286601c572cSGlenn Strauss require = &ds->value; 287ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 288601c572cSGlenn Strauss algos = &ds->value; 2897ba06c71SGlenn Strauss } else { 290b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 291010c2894SGlenn Strauss "the field is unknown in: " 292010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 293010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 2947ba06c71SGlenn Strauss 2957ba06c71SGlenn Strauss return HANDLER_ERROR; 2967ba06c71SGlenn Strauss } 2977ba06c71SGlenn Strauss } else { 298b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 299010c2894SGlenn Strauss "a string was expected for: " 300010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 301010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 3027ba06c71SGlenn Strauss 3037ba06c71SGlenn Strauss return HANDLER_ERROR; 3047ba06c71SGlenn Strauss } 3057ba06c71SGlenn Strauss } 3067ba06c71SGlenn Strauss 3077ba06c71SGlenn Strauss if (buffer_string_is_empty(method)) { 308b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 309010c2894SGlenn Strauss "the method field is missing or blank in: " 3107ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 3117ba06c71SGlenn Strauss return HANDLER_ERROR; 3127ba06c71SGlenn Strauss } else { 3137ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 3147ba06c71SGlenn Strauss if (NULL == auth_scheme) { 315b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 316010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in " 317010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr); 3187ba06c71SGlenn Strauss return HANDLER_ERROR; 3197ba06c71SGlenn Strauss } 3207ba06c71SGlenn Strauss } 3217ba06c71SGlenn Strauss 3227ba06c71SGlenn Strauss if (buffer_is_empty(realm)) { 323b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 324010c2894SGlenn Strauss "the realm field is missing in: " 3257ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 3267ba06c71SGlenn Strauss return HANDLER_ERROR; 3277ba06c71SGlenn Strauss } 3287ba06c71SGlenn Strauss 3297ba06c71SGlenn Strauss if (buffer_string_is_empty(require)) { 330b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 331010c2894SGlenn Strauss "the require field is missing or blank in: " 3327ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 3337ba06c71SGlenn Strauss return HANDLER_ERROR; 3347ba06c71SGlenn Strauss } 3357ba06c71SGlenn Strauss 336b9e2be50SGlenn Strauss if (buffer_string_is_empty(algos)) { 337b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 338b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 339b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 340010c2894SGlenn Strauss "invalid algorithm in: " 341b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 342b9e2be50SGlenn Strauss return HANDLER_ERROR; 343b9e2be50SGlenn Strauss } 344b9e2be50SGlenn Strauss 345609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 3467ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 347ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 3487ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 349b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 350a03afc90SGlenn Strauss dauth->require->realm = realm; 351b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) { 3528c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 3537ba06c71SGlenn Strauss return HANDLER_ERROR; 3547ba06c71SGlenn Strauss } 3558e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth); 3567ba06c71SGlenn Strauss } 3577ba06c71SGlenn Strauss } 3587ba06c71SGlenn Strauss 3597ba06c71SGlenn Strauss return HANDLER_GO_ON; 3607ba06c71SGlenn Strauss } 3617ba06c71SGlenn Strauss 3628e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 3638e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 3648e713130SGlenn Strauss case 0: /* auth.backend */ 3658e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 3668e713130SGlenn Strauss pconf->auth_backend = cpv->v.v; 3678e713130SGlenn Strauss break; 3688e713130SGlenn Strauss case 1: /* auth.require */ 3698e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 3708e713130SGlenn Strauss pconf->auth_require = cpv->v.v; 3718e713130SGlenn Strauss break; 3728e713130SGlenn Strauss case 2: /* auth.extern-authn */ 3738e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u; 3748e713130SGlenn Strauss default:/* should not happen */ 3758e713130SGlenn Strauss return; 3768e713130SGlenn Strauss } 3778e713130SGlenn Strauss } 3784a6ce4aeSJan Kneschke 3798e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 3808e713130SGlenn Strauss do { 3818e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv); 3828e713130SGlenn Strauss } while ((++cpv)->k_id != -1); 3838e713130SGlenn Strauss } 384bcdc6a3bSJan Kneschke 3858e713130SGlenn Strauss static void mod_auth_patch_config(connection * const con, plugin_data * const p) { 3868e713130SGlenn Strauss memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); 3878e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 3888e713130SGlenn Strauss if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) 3898e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 3908e713130SGlenn Strauss } 3918e713130SGlenn Strauss } 39236f64b26SGlenn Strauss 3938e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 3948e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 3958e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 3968e713130SGlenn Strauss T_CONFIG_STRING, 3978e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 3988e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 399*03b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 4008e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4018e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 4028e713130SGlenn Strauss T_CONFIG_BOOL, 4038e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4048e713130SGlenn Strauss ,{ NULL, 0, 4058e713130SGlenn Strauss T_CONFIG_UNSET, 4068e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 4078e713130SGlenn Strauss }; 408bcdc6a3bSJan Kneschke 4098e713130SGlenn Strauss plugin_data * const p = p_d; 4108e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 4118e713130SGlenn Strauss return HANDLER_ERROR; 412bcdc6a3bSJan Kneschke 4138e713130SGlenn Strauss /* process and validate config directives 4148e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 4158e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 4168e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 4178e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 4188e713130SGlenn Strauss switch (cpv->k_id) { 4198e713130SGlenn Strauss case 0: /* auth.backend */ 4208e713130SGlenn Strauss if (!buffer_string_is_empty(cpv->v.b)) { 4218e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 4228e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 4238e713130SGlenn Strauss if (NULL == auth_backend) { 4248e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 4258e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 4268e713130SGlenn Strauss return HANDLER_ERROR; 4278e713130SGlenn Strauss } 4288e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 4298e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4308e713130SGlenn Strauss } 4318e713130SGlenn Strauss break; 4328e713130SGlenn Strauss case 1: /* auth.require */ 433*03b4c993SGlenn Strauss { 43424680a91SGlenn Strauss array * const a = array_init(4); 4358e713130SGlenn Strauss if (HANDLER_GO_ON != 436b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 4378e713130SGlenn Strauss array_free(a); 4388e713130SGlenn Strauss return HANDLER_ERROR; 4398e713130SGlenn Strauss } 4408e713130SGlenn Strauss cpv->v.a = a; 4418e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4428e713130SGlenn Strauss } 4438e713130SGlenn Strauss break; 4448e713130SGlenn Strauss case 2: /* auth.extern-authn */ 4458e713130SGlenn Strauss break; 4468e713130SGlenn Strauss default:/* should not happen */ 4478e713130SGlenn Strauss break; 448bcdc6a3bSJan Kneschke } 449bcdc6a3bSJan Kneschke } 450bcdc6a3bSJan Kneschke } 451bcdc6a3bSJan Kneschke 4528e713130SGlenn Strauss /* initialize p->defaults from global config context */ 4538e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 4548e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 4558e713130SGlenn Strauss if (-1 != cpv->k_id) 4568e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 457bcdc6a3bSJan Kneschke } 4588e713130SGlenn Strauss 4598e713130SGlenn Strauss return HANDLER_GO_ON; 4608e713130SGlenn Strauss } 461bcdc6a3bSJan Kneschke 46250bdb55dSGlenn Strauss static handler_t mod_auth_uri_handler(connection *con, void *p_d) { 4637ba06c71SGlenn Strauss plugin_data *p = p_d; 464d61f3381SGlenn Strauss data_auth *dauth; 46581b2d1f0SGlenn Strauss 4668e713130SGlenn Strauss mod_auth_patch_config(con, p); 46781b2d1f0SGlenn Strauss 4687ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 46981b2d1f0SGlenn Strauss 4707ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 4717ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 472d61f3381SGlenn Strauss dauth = (!con->conf.force_lowercase_filenames) 473d61f3381SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, con->uri.path) 474d61f3381SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, con->uri.path); 475d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 4767ba06c71SGlenn Strauss 477d61f3381SGlenn Strauss { 4787ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 479f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 4806eb34ef5SGlenn Strauss const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")); 4813dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 482f54d628cSGlenn Strauss return HANDLER_GO_ON; 483f54d628cSGlenn Strauss } 484f54d628cSGlenn Strauss } 485010c2894SGlenn Strauss return scheme->checkfn(con, scheme->p_d, dauth->require, p->conf.auth_backend); 4867ba06c71SGlenn Strauss } 4877ba06c71SGlenn Strauss } 4887ba06c71SGlenn Strauss 4897ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 4907ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 4917ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 492e2de4e58SGlenn Strauss p->name = "auth"; 4937ba06c71SGlenn Strauss p->init = mod_auth_init; 4947ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 4957ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 4967ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 4977ba06c71SGlenn Strauss 49881b2d1f0SGlenn Strauss return 0; 49981b2d1f0SGlenn Strauss } 50081b2d1f0SGlenn Strauss 50181b2d1f0SGlenn Strauss 50281b2d1f0SGlenn Strauss 50381b2d1f0SGlenn Strauss 5047ba06c71SGlenn Strauss /* 5057ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 5067ba06c71SGlenn Strauss * 5077ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 5087ba06c71SGlenn Strauss */ 50981b2d1f0SGlenn Strauss 5107ba06c71SGlenn Strauss #include "base64.h" 5117ba06c71SGlenn Strauss #include "md5.h" 5127f4e156eSGlenn Strauss #include "rand.h" 5133dd3cde9SGlenn Strauss #include "http_header.h" 51481b2d1f0SGlenn Strauss 515010c2894SGlenn Strauss __attribute_cold__ 516010c2894SGlenn Strauss static handler_t mod_auth_send_400_bad_request(connection *con) { 5174b3a91e6SGlenn Strauss 5184b3a91e6SGlenn Strauss /* a field was missing or invalid */ 5194b3a91e6SGlenn Strauss con->http_status = 400; /* Bad Request */ 5204b3a91e6SGlenn Strauss con->mode = DIRECT; 5214b3a91e6SGlenn Strauss 5224b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5234b3a91e6SGlenn Strauss } 5244b3a91e6SGlenn Strauss 525010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(connection *con, const buffer *realm) { 5264b3a91e6SGlenn Strauss con->http_status = 401; 5274b3a91e6SGlenn Strauss con->mode = DIRECT; 5284b3a91e6SGlenn Strauss 529010c2894SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 530010c2894SGlenn Strauss buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\"")); 531010c2894SGlenn Strauss buffer_append_string_buffer(tb, realm); 532010c2894SGlenn Strauss buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\"")); 5334b3a91e6SGlenn Strauss 534010c2894SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 5354b3a91e6SGlenn Strauss 5364b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5374b3a91e6SGlenn Strauss } 5384b3a91e6SGlenn Strauss 539010c2894SGlenn Strauss static handler_t mod_auth_check_basic(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 5406eb34ef5SGlenn Strauss const buffer *b = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 54181b2d1f0SGlenn Strauss buffer *username; 54281b2d1f0SGlenn Strauss char *pw; 5437ba06c71SGlenn Strauss handler_t rc = HANDLER_UNSET; 54481b2d1f0SGlenn Strauss 5457ba06c71SGlenn Strauss UNUSED(p_d); 54681b2d1f0SGlenn Strauss 5477ba06c71SGlenn Strauss if (NULL == backend) { 548010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "auth.backend not configured for %s", con->uri.path->ptr); 5497ba06c71SGlenn Strauss con->http_status = 500; 5507ba06c71SGlenn Strauss con->mode = DIRECT; 5517ba06c71SGlenn Strauss return HANDLER_FINISHED; 5527ba06c71SGlenn Strauss } 5537ba06c71SGlenn Strauss 5543dd3cde9SGlenn Strauss if (NULL == b) { 555010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_basic(con, require->realm); 5567ba06c71SGlenn Strauss } 5577ba06c71SGlenn Strauss 55813008156SGlenn Strauss if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) { 559010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_basic(con, require->realm); 5604b3a91e6SGlenn Strauss } 5619bc61f16SGlenn Strauss #ifdef __COVERITY__ 5623dd3cde9SGlenn Strauss if (buffer_string_length(b) < sizeof("Basic ")-1) { 563010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 5649bc61f16SGlenn Strauss } 5659bc61f16SGlenn Strauss #endif 56681b2d1f0SGlenn Strauss 56781b2d1f0SGlenn Strauss username = buffer_init(); 56881b2d1f0SGlenn Strauss 5698047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 5707ba06c71SGlenn Strauss if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) { 571010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1); 57281b2d1f0SGlenn Strauss 57381b2d1f0SGlenn Strauss buffer_free(username); 574010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 57581b2d1f0SGlenn Strauss } 57681b2d1f0SGlenn Strauss 57781b2d1f0SGlenn Strauss /* r2 == user:password */ 57881b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 579010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr); 58081b2d1f0SGlenn Strauss 58181b2d1f0SGlenn Strauss buffer_free(username); 582010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 58381b2d1f0SGlenn Strauss } 58481b2d1f0SGlenn Strauss 58581b2d1f0SGlenn Strauss buffer_string_set_length(username, pw - username->ptr); 58681b2d1f0SGlenn Strauss pw++; 58781b2d1f0SGlenn Strauss 588010c2894SGlenn Strauss rc = backend->basic(con, backend->p_d, require, username, pw); 5897ba06c71SGlenn Strauss switch (rc) { 5904b3a91e6SGlenn Strauss case HANDLER_GO_ON: 5913dd3cde9SGlenn Strauss http_auth_setenv(con, CONST_BUF_LEN(username), CONST_STR_LEN("Basic")); 5924b3a91e6SGlenn Strauss break; 5934b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 5944b3a91e6SGlenn Strauss case HANDLER_FINISHED: 5957ba06c71SGlenn Strauss break; 5964b3a91e6SGlenn Strauss case HANDLER_ERROR: 5974b3a91e6SGlenn Strauss default: 598010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 599010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 600010c2894SGlenn Strauss con->uri.path->ptr, username->ptr, con->dst_addr_buf->ptr); 6018bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if bad password)*/ 6027ba06c71SGlenn Strauss rc = HANDLER_UNSET; 6037ba06c71SGlenn Strauss break; 60481b2d1f0SGlenn Strauss } 60581b2d1f0SGlenn Strauss 6064b3a91e6SGlenn Strauss buffer_free(username); 607010c2894SGlenn Strauss return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(con, require->realm); 60881b2d1f0SGlenn Strauss } 60981b2d1f0SGlenn Strauss 61081b2d1f0SGlenn Strauss 611b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 612b9e2be50SGlenn Strauss 613b9e2be50SGlenn 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) { 614b9e2be50SGlenn Strauss SHA256_CTX ctx; 615b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 616b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 617b9e2be50SGlenn Strauss 618b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 619b9e2be50SGlenn Strauss SHA256_Init(&ctx); 620b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 621b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 622b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 623b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 624b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 625b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 626b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 627b9e2be50SGlenn Strauss } 628b9e2be50SGlenn Strauss 629b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 630b9e2be50SGlenn Strauss 631b9e2be50SGlenn Strauss /* calculate H(A2) */ 632b9e2be50SGlenn Strauss SHA256_Init(&ctx); 633b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)m, strlen(m)); 634b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 635b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 636b9e2be50SGlenn Strauss #if 0 637b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 638fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 639b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 640b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 641b9e2be50SGlenn Strauss } 642b9e2be50SGlenn Strauss #endif 643b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 644b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 645b9e2be50SGlenn Strauss 646b9e2be50SGlenn Strauss /* calculate response */ 647b9e2be50SGlenn Strauss SHA256_Init(&ctx); 648b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 649b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 650b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 651b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 652b9e2be50SGlenn Strauss if (qop && *qop) { 653b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 654b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 655b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 656b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 657b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 658b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 659b9e2be50SGlenn Strauss } 660b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 661b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 662b9e2be50SGlenn Strauss } 663b9e2be50SGlenn Strauss 664b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd) { 665b9e2be50SGlenn Strauss SHA256_CTX ctx; 666f24e6d69SGlenn Strauss size_t len; 667b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 668b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 669b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 670b9e2be50SGlenn Strauss SHA256_Init(&ctx); 671f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 672f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 673f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 674f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 675b9e2be50SGlenn Strauss SHA256_Final(h, &ctx); 676b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 677b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 678b9e2be50SGlenn Strauss } 679b9e2be50SGlenn Strauss 680b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 681b9e2be50SGlenn Strauss 682b9e2be50SGlenn 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) { 683b9e2be50SGlenn Strauss SHA512_CTX ctx; 684b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 685b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 686b9e2be50SGlenn Strauss 687b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 688b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 689b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 690b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 691b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 692b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 693b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 694b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 695b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 696b9e2be50SGlenn Strauss } 697b9e2be50SGlenn Strauss 698b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 699b9e2be50SGlenn Strauss 700b9e2be50SGlenn Strauss /* calculate H(A2) */ 701b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 702b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m)); 703b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 704b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 705b9e2be50SGlenn Strauss #if 0 706b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 707fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 708b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 709b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2); 710b9e2be50SGlenn Strauss } 711b9e2be50SGlenn Strauss #endif 712b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 713b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 714b9e2be50SGlenn Strauss 715b9e2be50SGlenn Strauss /* calculate response */ 716b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 717b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 718b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 719b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 720b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 721b9e2be50SGlenn Strauss if (qop && *qop) { 722b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 723b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 724b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 725b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 726b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 727b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 728b9e2be50SGlenn Strauss } 729b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 730b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 731b9e2be50SGlenn Strauss } 732b9e2be50SGlenn Strauss 733b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd) { 734b9e2be50SGlenn Strauss SHA512_CTX ctx; 735f24e6d69SGlenn Strauss size_t len; 736b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN]; 737b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 738b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 739b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 740f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 741f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 742f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 743f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 744b9e2be50SGlenn Strauss SHA512_256_Final(h, &ctx); 745b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 746b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 747b9e2be50SGlenn Strauss } 748b9e2be50SGlenn Strauss 749b9e2be50SGlenn Strauss #endif /* SHA512_256_DIGEST_LENGTH */ 750b9e2be50SGlenn Strauss 751b9e2be50SGlenn Strauss #endif /* USE_OPENSSL_CRYPTO */ 752b9e2be50SGlenn Strauss 753b9e2be50SGlenn 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) { 754b9e2be50SGlenn Strauss li_MD5_CTX ctx; 755b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 756b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 757b9e2be50SGlenn Strauss 758b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 759b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 760b9e2be50SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 761b9e2be50SGlenn Strauss * Errata ID: 1649 */ 762b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 763b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 764b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 765b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 766b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 767b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 768b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 769b9e2be50SGlenn Strauss } 770b9e2be50SGlenn Strauss 771b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 772b9e2be50SGlenn Strauss 773b9e2be50SGlenn Strauss /* calculate H(A2) */ 774b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 775b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)m, strlen(m)); 776b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 777b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri)); 778b9e2be50SGlenn Strauss #if 0 779b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 780fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 781b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 782b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 783b9e2be50SGlenn Strauss } 784b9e2be50SGlenn Strauss #endif 785b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 786b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 787b9e2be50SGlenn Strauss 788b9e2be50SGlenn Strauss /* calculate response */ 789b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 790b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 791b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 792b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 793b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 794b9e2be50SGlenn Strauss if (qop && *qop) { 795b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc)); 796b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 797b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 798b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 799b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop)); 800b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 801b9e2be50SGlenn Strauss } 802b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 803b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 804b9e2be50SGlenn Strauss } 805b9e2be50SGlenn Strauss 806b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd) { 807b9e2be50SGlenn Strauss li_MD5_CTX ctx; 808f24e6d69SGlenn Strauss size_t len; 809b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN]; 810b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 811b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 812b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 813f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 814f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 815f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 816f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 817b9e2be50SGlenn Strauss li_MD5_Final(h, &ctx); 818b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 819b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 820b9e2be50SGlenn Strauss } 821b9e2be50SGlenn Strauss 822b9e2be50SGlenn 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) { 823b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_MD5) 824b9e2be50SGlenn Strauss mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop); 825b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 826b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 827b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop); 828b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 829b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 830b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop); 831b9e2be50SGlenn Strauss #endif 832b9e2be50SGlenn Strauss #endif 833b9e2be50SGlenn Strauss } 834b9e2be50SGlenn Strauss 835b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) { 836b9e2be50SGlenn Strauss const int rnd = li_rand_pseudo(); 837b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 838b9e2be50SGlenn Strauss int n = 0; 839b9e2be50SGlenn Strauss void(*append_nonce[3])(buffer *, time_t, int); 840b9e2be50SGlenn Strauss unsigned int algolen[3]; 841b9e2be50SGlenn Strauss const char *algoname[3]; 842b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 843b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 844b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 845b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha512_256; 846b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 847b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 848b9e2be50SGlenn Strauss ++n; 849b9e2be50SGlenn Strauss } 850b9e2be50SGlenn Strauss #endif 851b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 852b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha256; 853b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 854b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 855b9e2be50SGlenn Strauss ++n; 856b9e2be50SGlenn Strauss } 857b9e2be50SGlenn Strauss #endif 858b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 859b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_md5; 860b9e2be50SGlenn Strauss algoname[n] = "MD5"; 861b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 862b9e2be50SGlenn Strauss ++n; 863b9e2be50SGlenn Strauss } 864b9e2be50SGlenn Strauss 865b9e2be50SGlenn Strauss buffer_clear(b); 866b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 867b9e2be50SGlenn Strauss if (i > 0) { 868b9e2be50SGlenn Strauss buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: ")); 869b9e2be50SGlenn Strauss } 870b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\"")); 871b9e2be50SGlenn Strauss buffer_append_string_buffer(b, require->realm); 8721799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=")); 873b9e2be50SGlenn Strauss buffer_append_string_len(b, algoname[i], algolen[i]); 8741799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", nonce=\"")); 875b9e2be50SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 876b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 877b9e2be50SGlenn Strauss (append_nonce[i])(b, cur_ts, rnd); 878b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 879b9e2be50SGlenn Strauss if (nonce_stale) { 880b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 881b9e2be50SGlenn Strauss } 882b9e2be50SGlenn Strauss } 88381b2d1f0SGlenn Strauss } 88481b2d1f0SGlenn Strauss 8856ad325c6SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, int dalgo) { 8866ad325c6SGlenn Strauss const int rnd = li_rand_pseudo(); 8876ad325c6SGlenn Strauss void(*append_nonce)(buffer *, time_t, int); 8886ad325c6SGlenn Strauss switch (dalgo) { 8896ad325c6SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 8906ad325c6SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 8916ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 8926ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha512_256; 8936ad325c6SGlenn Strauss break; 8946ad325c6SGlenn Strauss #endif 8956ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 8966ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha256; 8976ad325c6SGlenn Strauss break; 8986ad325c6SGlenn Strauss #endif 8996ad325c6SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 9006ad325c6SGlenn Strauss default: 9016ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_md5; 9026ad325c6SGlenn Strauss break; 9036ad325c6SGlenn Strauss } 9046ad325c6SGlenn Strauss buffer_clear(b); 9056ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 9066ad325c6SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 9076ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 9086ad325c6SGlenn Strauss (append_nonce)(b, cur_ts, rnd); 9096ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\"")); 9106ad325c6SGlenn Strauss } 9116ad325c6SGlenn Strauss 91281b2d1f0SGlenn Strauss typedef struct { 91381b2d1f0SGlenn Strauss const char *key; 91481b2d1f0SGlenn Strauss int key_len; 91581b2d1f0SGlenn Strauss char **ptr; 91681b2d1f0SGlenn Strauss } digest_kv; 91781b2d1f0SGlenn Strauss 918010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale); 9197ba06c71SGlenn Strauss 920010c2894SGlenn Strauss static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 9216eb34ef5SGlenn Strauss const buffer *vb = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 9227ba06c71SGlenn Strauss 92381b2d1f0SGlenn Strauss char *username = NULL; 92481b2d1f0SGlenn Strauss char *realm = NULL; 92581b2d1f0SGlenn Strauss char *nonce = NULL; 92681b2d1f0SGlenn Strauss char *uri = NULL; 92781b2d1f0SGlenn Strauss char *algorithm = NULL; 92881b2d1f0SGlenn Strauss char *qop = NULL; 92981b2d1f0SGlenn Strauss char *cnonce = NULL; 93081b2d1f0SGlenn Strauss char *nc = NULL; 93181b2d1f0SGlenn Strauss char *respons = NULL; 93281b2d1f0SGlenn Strauss 93381b2d1f0SGlenn Strauss char *e, *c; 93481b2d1f0SGlenn Strauss const char *m = NULL; 93581b2d1f0SGlenn Strauss int i; 93681b2d1f0SGlenn Strauss buffer *b; 93760f4cf3aSGlenn Strauss http_auth_info_t ai; 938b9e2be50SGlenn Strauss unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 93981b2d1f0SGlenn Strauss 94081b2d1f0SGlenn Strauss 94181b2d1f0SGlenn Strauss /* init pointers */ 94281b2d1f0SGlenn Strauss #define S(x) \ 94381b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 94481b2d1f0SGlenn Strauss digest_kv dkv[10] = { 94581b2d1f0SGlenn Strauss { S("username=") }, 94681b2d1f0SGlenn Strauss { S("realm=") }, 94781b2d1f0SGlenn Strauss { S("nonce=") }, 94881b2d1f0SGlenn Strauss { S("uri=") }, 94981b2d1f0SGlenn Strauss { S("algorithm=") }, 95081b2d1f0SGlenn Strauss { S("qop=") }, 95181b2d1f0SGlenn Strauss { S("cnonce=") }, 95281b2d1f0SGlenn Strauss { S("nc=") }, 95381b2d1f0SGlenn Strauss { S("response=") }, 95481b2d1f0SGlenn Strauss 95581b2d1f0SGlenn Strauss { NULL, 0, NULL } 95681b2d1f0SGlenn Strauss }; 95781b2d1f0SGlenn Strauss #undef S 95881b2d1f0SGlenn Strauss 95981b2d1f0SGlenn Strauss dkv[0].ptr = &username; 96081b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 96181b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 96281b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 96381b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 96481b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 96581b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 96681b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 96781b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 96881b2d1f0SGlenn Strauss 9697ba06c71SGlenn Strauss UNUSED(p_d); 9707ba06c71SGlenn Strauss 9717ba06c71SGlenn Strauss if (NULL == backend) { 972010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 973010c2894SGlenn Strauss "auth.backend not configured for %s", con->uri.path->ptr); 9747ba06c71SGlenn Strauss con->http_status = 500; 9757ba06c71SGlenn Strauss con->mode = DIRECT; 9767ba06c71SGlenn Strauss return HANDLER_FINISHED; 9777ba06c71SGlenn Strauss } 9787ba06c71SGlenn Strauss 9793dd3cde9SGlenn Strauss if (NULL == vb) { 980010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 9817ba06c71SGlenn Strauss } 9827ba06c71SGlenn Strauss 98313008156SGlenn Strauss if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) { 984010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 985dadfb5fcSGlenn Strauss } else { 9863dd3cde9SGlenn Strauss size_t n = buffer_string_length(vb); 9879bc61f16SGlenn Strauss #ifdef __COVERITY__ 988dadfb5fcSGlenn Strauss if (n < sizeof("Digest ")-1) { 989010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 9909bc61f16SGlenn Strauss } 9919bc61f16SGlenn Strauss #endif 992dadfb5fcSGlenn Strauss n -= (sizeof("Digest ")-1); 9937ba06c71SGlenn Strauss b = buffer_init(); 9943dd3cde9SGlenn Strauss buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n); 995dadfb5fcSGlenn Strauss } 99681b2d1f0SGlenn Strauss 99781b2d1f0SGlenn Strauss /* parse credentials from client */ 99881b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 99981b2d1f0SGlenn Strauss /* skip whitespaces */ 100081b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 100181b2d1f0SGlenn Strauss if (!*c) break; 100281b2d1f0SGlenn Strauss 100381b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 100481b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 100581b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 100681b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 100781b2d1f0SGlenn Strauss /* value with "..." */ 100881b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 100981b2d1f0SGlenn Strauss c = e; 101081b2d1f0SGlenn Strauss 101181b2d1f0SGlenn Strauss *e = '\0'; 101281b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 101381b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 101481b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 101581b2d1f0SGlenn Strauss c = e; 101681b2d1f0SGlenn Strauss 101781b2d1f0SGlenn Strauss *e = '\0'; 101881b2d1f0SGlenn Strauss } else { 101981b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 102081b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 102181b2d1f0SGlenn Strauss c += strlen(c) - 1; 102281b2d1f0SGlenn Strauss } 102381b2d1f0SGlenn Strauss break; 102481b2d1f0SGlenn Strauss } 102581b2d1f0SGlenn Strauss } 102681b2d1f0SGlenn Strauss } 102781b2d1f0SGlenn Strauss 102881b2d1f0SGlenn Strauss /* check if everything is transmitted */ 102981b2d1f0SGlenn Strauss if (!username || 103081b2d1f0SGlenn Strauss !realm || 103181b2d1f0SGlenn Strauss !nonce || 103281b2d1f0SGlenn Strauss !uri || 103381b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 103481b2d1f0SGlenn Strauss !respons ) { 103581b2d1f0SGlenn Strauss /* missing field */ 103681b2d1f0SGlenn Strauss 1037010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 103881b2d1f0SGlenn Strauss "digest: missing field"); 103981b2d1f0SGlenn Strauss 104081b2d1f0SGlenn Strauss buffer_free(b); 1041010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 104281b2d1f0SGlenn Strauss } 104381b2d1f0SGlenn Strauss 104460f4cf3aSGlenn Strauss ai.username = username; 104560f4cf3aSGlenn Strauss ai.ulen = strlen(username); 104660f4cf3aSGlenn Strauss ai.realm = realm; 104760f4cf3aSGlenn Strauss ai.rlen = strlen(realm); 104860f4cf3aSGlenn Strauss 104960f4cf3aSGlenn Strauss if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) { 1050010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 10515a6e4df8SGlenn Strauss "digest: realm mismatch"); 10525a6e4df8SGlenn Strauss buffer_free(b); 1053010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 10545a6e4df8SGlenn Strauss } 10555a6e4df8SGlenn Strauss 1056b9e2be50SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, algorithm) 1057b9e2be50SGlenn Strauss || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 1058010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1059010c2894SGlenn Strauss "digest: (%s): invalid", algorithm); 1060b9e2be50SGlenn Strauss buffer_free(b); 1061010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 1062b9e2be50SGlenn Strauss } 1063b9e2be50SGlenn Strauss 106481b2d1f0SGlenn Strauss /** 106581b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 106681b2d1f0SGlenn Strauss */ 1067b9e2be50SGlenn Strauss if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) { 1068010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1069010c2894SGlenn Strauss "digest: (%s): missing field", algorithm); 107081b2d1f0SGlenn Strauss 107181b2d1f0SGlenn Strauss buffer_free(b); 1072010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 107381b2d1f0SGlenn Strauss } 107481b2d1f0SGlenn Strauss 1075b9e2be50SGlenn Strauss { 1076b9e2be50SGlenn Strauss size_t resplen = strlen(respons); 1077b9e2be50SGlenn Strauss if (0 != http_auth_digest_hex2bin(respons, resplen, 1078b9e2be50SGlenn Strauss rdigest, sizeof(rdigest)) 1079b9e2be50SGlenn Strauss || resplen != (ai.dlen << 1)) { 1080010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1081010c2894SGlenn Strauss "digest: (%s): invalid format", respons); 1082b9e2be50SGlenn Strauss buffer_free(b); 1083010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 1084b9e2be50SGlenn Strauss } 1085b9e2be50SGlenn Strauss } 1086b9e2be50SGlenn Strauss 1087fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 1088010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 108981b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 109081b2d1f0SGlenn Strauss 109181b2d1f0SGlenn Strauss buffer_free(b); 1092010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 109381b2d1f0SGlenn Strauss } 109481b2d1f0SGlenn Strauss 109581b2d1f0SGlenn Strauss m = get_http_method_name(con->request.http_method); 109681b2d1f0SGlenn Strauss force_assert(m); 109781b2d1f0SGlenn Strauss 109881b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 109981b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 110081b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 110181b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 110281b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 110381b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 110481b2d1f0SGlenn Strauss * uri validation step is bypassed. con->request.orig_uri is original 110581b2d1f0SGlenn Strauss * uri sent in client request. */ 110681b2d1f0SGlenn Strauss { 110781b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 1108c81bd354SGlenn Strauss if (!buffer_is_equal_string(con->request.orig_uri, uri, ulen)) { 1109010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1110010c2894SGlenn Strauss "digest: auth failed: uri mismatch (%s != %s), IP: %s", 1111010c2894SGlenn Strauss con->request.orig_uri->ptr, uri, con->dst_addr_buf->ptr); 111281b2d1f0SGlenn Strauss buffer_free(b); 1113010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 111481b2d1f0SGlenn Strauss } 111581b2d1f0SGlenn Strauss } 111681b2d1f0SGlenn Strauss 1117010c2894SGlenn Strauss switch (backend->digest(con, backend->p_d, &ai)) { 11184b3a91e6SGlenn Strauss case HANDLER_GO_ON: 11194b3a91e6SGlenn Strauss break; 11204b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 112181b2d1f0SGlenn Strauss buffer_free(b); 11224b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 11234b3a91e6SGlenn Strauss case HANDLER_FINISHED: 11244b3a91e6SGlenn Strauss buffer_free(b); 11254b3a91e6SGlenn Strauss return HANDLER_FINISHED; 11264b3a91e6SGlenn Strauss case HANDLER_ERROR: 11274b3a91e6SGlenn Strauss default: 11288bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if unknown user)*/ 11294b3a91e6SGlenn Strauss buffer_free(b); 1130010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 113181b2d1f0SGlenn Strauss } 113281b2d1f0SGlenn Strauss 1133b9e2be50SGlenn Strauss mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop); 113481b2d1f0SGlenn Strauss 11350e749c1cSGlenn Strauss if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) { 113681b2d1f0SGlenn Strauss /* digest not ok */ 1137010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1138010c2894SGlenn Strauss "digest: auth failed for %s: wrong password, IP: %s", 1139010c2894SGlenn Strauss username, con->dst_addr_buf->ptr); 11408bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if bad password)*/ 114181b2d1f0SGlenn Strauss 114281b2d1f0SGlenn Strauss buffer_free(b); 1143010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 114481b2d1f0SGlenn Strauss } 114581b2d1f0SGlenn Strauss 114681b2d1f0SGlenn Strauss /* value is our allow-rules */ 11477ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 11484b3a91e6SGlenn Strauss buffer_free(b); 1149010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 115081b2d1f0SGlenn Strauss } 115181b2d1f0SGlenn Strauss 11527f4e156eSGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 11537ba06c71SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 115481b2d1f0SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 115581b2d1f0SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 115681b2d1f0SGlenn Strauss * verifiable) if nanoseconds were inclued with seconds as part of the 115781b2d1f0SGlenn Strauss * nonce "timestamp:secret". Since that is not done, timestamp in 115881b2d1f0SGlenn Strauss * nonce could theoretically be modified and still produce same md5sum, 115981b2d1f0SGlenn Strauss * but that is highly unlikely within a 10 min (moving) window of valid 116081b2d1f0SGlenn Strauss * time relative to current time (now) */ 116181b2d1f0SGlenn Strauss { 116281b2d1f0SGlenn Strauss time_t ts = 0; 116381b2d1f0SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 116481b2d1f0SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 116581b2d1f0SGlenn Strauss ts = (ts << 4) + hex2int(nonce_uns[i]); 116681b2d1f0SGlenn Strauss } 1167c8cd7cf4SGlenn Strauss const time_t cur_ts = log_epoch_secs; 116883759aedSGlenn Strauss if (nonce[i] != ':' 1169409bba80SGlenn Strauss || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 11704b3a91e6SGlenn Strauss /* nonce is stale; have client regenerate digest */ 117181b2d1f0SGlenn Strauss buffer_free(b); 1172010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, ai.dalgo); 11736ad325c6SGlenn Strauss } 1174409bba80SGlenn Strauss else if (cur_ts - ts > 540) { /*(9 mins)*/ 11756ad325c6SGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 1176409bba80SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 1177409bba80SGlenn Strauss mod_auth_digest_authentication_info(tb, cur_ts, ai.dalgo); 1178409bba80SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, 1179409bba80SGlenn Strauss CONST_STR_LEN("Authentication-Info"), 1180409bba80SGlenn Strauss CONST_BUF_LEN(tb)); 11816ad325c6SGlenn Strauss } 118281b2d1f0SGlenn Strauss } 118381b2d1f0SGlenn Strauss 118460f4cf3aSGlenn Strauss http_auth_setenv(con, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 118581b2d1f0SGlenn Strauss 118681b2d1f0SGlenn Strauss buffer_free(b); 118781b2d1f0SGlenn Strauss 11884b3a91e6SGlenn Strauss return HANDLER_GO_ON; 118981b2d1f0SGlenn Strauss } 119081b2d1f0SGlenn Strauss 1191010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale) { 1192b5775b99SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 1193b5775b99SGlenn Strauss mod_auth_digest_www_authenticate(tb, log_epoch_secs, require, nonce_stale); 1194b5775b99SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 119581b2d1f0SGlenn Strauss 1196f9d58670SStefan Bühler con->http_status = 401; 1197f9d58670SStefan Bühler con->mode = DIRECT; 1198f9d58670SStefan Bühler return HANDLER_FINISHED; 11997ba06c71SGlenn Strauss } 12007ba06c71SGlenn Strauss 1201010c2894SGlenn Strauss static handler_t mod_auth_check_extern(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 12027ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 12036eb34ef5SGlenn Strauss const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")); 12047ba06c71SGlenn Strauss UNUSED(p_d); 12057ba06c71SGlenn Strauss UNUSED(backend); 12063dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1207f9d58670SStefan Bühler return HANDLER_GO_ON; 1208bcdc6a3bSJan Kneschke } else { 12094b3a91e6SGlenn Strauss con->http_status = 401; 12104b3a91e6SGlenn Strauss con->mode = DIRECT; 1211bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 12127187271fSStefan Bühler } 1213bcdc6a3bSJan Kneschke } 1214