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) { 386*cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */ 387*cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/ 3888e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 3898e713130SGlenn Strauss if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) 3908e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 3918e713130SGlenn Strauss } 3928e713130SGlenn Strauss } 39336f64b26SGlenn Strauss 3948e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 3958e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 3968e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 3978e713130SGlenn Strauss T_CONFIG_STRING, 3988e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 3998e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 40003b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 4018e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4028e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 4038e713130SGlenn Strauss T_CONFIG_BOOL, 4048e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 4058e713130SGlenn Strauss ,{ NULL, 0, 4068e713130SGlenn Strauss T_CONFIG_UNSET, 4078e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 4088e713130SGlenn Strauss }; 409bcdc6a3bSJan Kneschke 4108e713130SGlenn Strauss plugin_data * const p = p_d; 4118e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 4128e713130SGlenn Strauss return HANDLER_ERROR; 413bcdc6a3bSJan Kneschke 4148e713130SGlenn Strauss /* process and validate config directives 4158e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 4168e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 4178e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 4188e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 4198e713130SGlenn Strauss switch (cpv->k_id) { 4208e713130SGlenn Strauss case 0: /* auth.backend */ 4218e713130SGlenn Strauss if (!buffer_string_is_empty(cpv->v.b)) { 4228e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 4238e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 4248e713130SGlenn Strauss if (NULL == auth_backend) { 4258e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 4268e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 4278e713130SGlenn Strauss return HANDLER_ERROR; 4288e713130SGlenn Strauss } 4298e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 4308e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4318e713130SGlenn Strauss } 4328e713130SGlenn Strauss break; 4338e713130SGlenn Strauss case 1: /* auth.require */ 43403b4c993SGlenn Strauss { 43524680a91SGlenn Strauss array * const a = array_init(4); 4368e713130SGlenn Strauss if (HANDLER_GO_ON != 437b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 4388e713130SGlenn Strauss array_free(a); 4398e713130SGlenn Strauss return HANDLER_ERROR; 4408e713130SGlenn Strauss } 4418e713130SGlenn Strauss cpv->v.a = a; 4428e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 4438e713130SGlenn Strauss } 4448e713130SGlenn Strauss break; 4458e713130SGlenn Strauss case 2: /* auth.extern-authn */ 4468e713130SGlenn Strauss break; 4478e713130SGlenn Strauss default:/* should not happen */ 4488e713130SGlenn Strauss break; 449bcdc6a3bSJan Kneschke } 450bcdc6a3bSJan Kneschke } 451bcdc6a3bSJan Kneschke } 452bcdc6a3bSJan Kneschke 4538e713130SGlenn Strauss /* initialize p->defaults from global config context */ 4548e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 4558e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 4568e713130SGlenn Strauss if (-1 != cpv->k_id) 4578e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 458bcdc6a3bSJan Kneschke } 4598e713130SGlenn Strauss 4608e713130SGlenn Strauss return HANDLER_GO_ON; 4618e713130SGlenn Strauss } 462bcdc6a3bSJan Kneschke 46350bdb55dSGlenn Strauss static handler_t mod_auth_uri_handler(connection *con, void *p_d) { 4647ba06c71SGlenn Strauss plugin_data *p = p_d; 465d61f3381SGlenn Strauss data_auth *dauth; 46681b2d1f0SGlenn Strauss 4678e713130SGlenn Strauss mod_auth_patch_config(con, p); 46881b2d1f0SGlenn Strauss 4697ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 47081b2d1f0SGlenn Strauss 4717ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 4727ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 473d61f3381SGlenn Strauss dauth = (!con->conf.force_lowercase_filenames) 474d61f3381SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, con->uri.path) 475d61f3381SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, con->uri.path); 476d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 4777ba06c71SGlenn Strauss 478d61f3381SGlenn Strauss { 4797ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 480f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 4816eb34ef5SGlenn Strauss const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")); 4823dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 483f54d628cSGlenn Strauss return HANDLER_GO_ON; 484f54d628cSGlenn Strauss } 485f54d628cSGlenn Strauss } 486010c2894SGlenn Strauss return scheme->checkfn(con, scheme->p_d, dauth->require, p->conf.auth_backend); 4877ba06c71SGlenn Strauss } 4887ba06c71SGlenn Strauss } 4897ba06c71SGlenn Strauss 4907ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 4917ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 4927ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 493e2de4e58SGlenn Strauss p->name = "auth"; 4947ba06c71SGlenn Strauss p->init = mod_auth_init; 4957ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 4967ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 4977ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 4987ba06c71SGlenn Strauss 49981b2d1f0SGlenn Strauss return 0; 50081b2d1f0SGlenn Strauss } 50181b2d1f0SGlenn Strauss 50281b2d1f0SGlenn Strauss 50381b2d1f0SGlenn Strauss 50481b2d1f0SGlenn Strauss 5057ba06c71SGlenn Strauss /* 5067ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 5077ba06c71SGlenn Strauss * 5087ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 5097ba06c71SGlenn Strauss */ 51081b2d1f0SGlenn Strauss 5117ba06c71SGlenn Strauss #include "base64.h" 5127ba06c71SGlenn Strauss #include "md5.h" 5137f4e156eSGlenn Strauss #include "rand.h" 5143dd3cde9SGlenn Strauss #include "http_header.h" 51581b2d1f0SGlenn Strauss 516010c2894SGlenn Strauss __attribute_cold__ 517010c2894SGlenn Strauss static handler_t mod_auth_send_400_bad_request(connection *con) { 5184b3a91e6SGlenn Strauss 5194b3a91e6SGlenn Strauss /* a field was missing or invalid */ 5204b3a91e6SGlenn Strauss con->http_status = 400; /* Bad Request */ 52131d94953SGlenn Strauss con->response.handler_module = NULL; 5224b3a91e6SGlenn Strauss 5234b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5244b3a91e6SGlenn Strauss } 5254b3a91e6SGlenn Strauss 526010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(connection *con, const buffer *realm) { 5274b3a91e6SGlenn Strauss con->http_status = 401; 52831d94953SGlenn Strauss con->response.handler_module = NULL; 5294b3a91e6SGlenn Strauss 530010c2894SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 531010c2894SGlenn Strauss buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\"")); 532010c2894SGlenn Strauss buffer_append_string_buffer(tb, realm); 533010c2894SGlenn Strauss buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\"")); 5344b3a91e6SGlenn Strauss 535010c2894SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 5364b3a91e6SGlenn Strauss 5374b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5384b3a91e6SGlenn Strauss } 5394b3a91e6SGlenn Strauss 540010c2894SGlenn 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) { 5416eb34ef5SGlenn Strauss const buffer *b = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 54281b2d1f0SGlenn Strauss buffer *username; 54381b2d1f0SGlenn Strauss char *pw; 5447ba06c71SGlenn Strauss handler_t rc = HANDLER_UNSET; 54581b2d1f0SGlenn Strauss 5467ba06c71SGlenn Strauss UNUSED(p_d); 54781b2d1f0SGlenn Strauss 5487ba06c71SGlenn Strauss if (NULL == backend) { 549010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "auth.backend not configured for %s", con->uri.path->ptr); 5507ba06c71SGlenn Strauss con->http_status = 500; 55131d94953SGlenn Strauss con->response.handler_module = NULL; 5527ba06c71SGlenn Strauss return HANDLER_FINISHED; 5537ba06c71SGlenn Strauss } 5547ba06c71SGlenn Strauss 5553dd3cde9SGlenn Strauss if (NULL == b) { 556010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_basic(con, require->realm); 5577ba06c71SGlenn Strauss } 5587ba06c71SGlenn Strauss 55913008156SGlenn Strauss if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) { 560010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_basic(con, require->realm); 5614b3a91e6SGlenn Strauss } 5629bc61f16SGlenn Strauss #ifdef __COVERITY__ 5633dd3cde9SGlenn Strauss if (buffer_string_length(b) < sizeof("Basic ")-1) { 564010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 5659bc61f16SGlenn Strauss } 5669bc61f16SGlenn Strauss #endif 56781b2d1f0SGlenn Strauss 56881b2d1f0SGlenn Strauss username = buffer_init(); 56981b2d1f0SGlenn Strauss 5708047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 5717ba06c71SGlenn Strauss if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) { 572010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1); 57381b2d1f0SGlenn Strauss 57481b2d1f0SGlenn Strauss buffer_free(username); 575010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 57681b2d1f0SGlenn Strauss } 57781b2d1f0SGlenn Strauss 57881b2d1f0SGlenn Strauss /* r2 == user:password */ 57981b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 580010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr); 58181b2d1f0SGlenn Strauss 58281b2d1f0SGlenn Strauss buffer_free(username); 583010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 58481b2d1f0SGlenn Strauss } 58581b2d1f0SGlenn Strauss 58681b2d1f0SGlenn Strauss buffer_string_set_length(username, pw - username->ptr); 58781b2d1f0SGlenn Strauss pw++; 58881b2d1f0SGlenn Strauss 589010c2894SGlenn Strauss rc = backend->basic(con, backend->p_d, require, username, pw); 5907ba06c71SGlenn Strauss switch (rc) { 5914b3a91e6SGlenn Strauss case HANDLER_GO_ON: 5923dd3cde9SGlenn Strauss http_auth_setenv(con, CONST_BUF_LEN(username), CONST_STR_LEN("Basic")); 5934b3a91e6SGlenn Strauss break; 5944b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 5954b3a91e6SGlenn Strauss case HANDLER_FINISHED: 5967ba06c71SGlenn Strauss break; 5974b3a91e6SGlenn Strauss case HANDLER_ERROR: 5984b3a91e6SGlenn Strauss default: 599010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 600010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 601010c2894SGlenn Strauss con->uri.path->ptr, username->ptr, con->dst_addr_buf->ptr); 602057d83c5SGlenn Strauss con->request.keep_alive = 0; /*(disable keep-alive if bad password)*/ 6037ba06c71SGlenn Strauss rc = HANDLER_UNSET; 6047ba06c71SGlenn Strauss break; 60581b2d1f0SGlenn Strauss } 60681b2d1f0SGlenn Strauss 6074b3a91e6SGlenn Strauss buffer_free(username); 608010c2894SGlenn Strauss return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(con, require->realm); 60981b2d1f0SGlenn Strauss } 61081b2d1f0SGlenn Strauss 61181b2d1f0SGlenn Strauss 612b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 613b9e2be50SGlenn Strauss 614b9e2be50SGlenn 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) { 615b9e2be50SGlenn Strauss SHA256_CTX ctx; 616b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 617b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 618b9e2be50SGlenn Strauss 619b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 620b9e2be50SGlenn Strauss SHA256_Init(&ctx); 621b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 622b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 623b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 624b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 625b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 626b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 627b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 628b9e2be50SGlenn Strauss } 629b9e2be50SGlenn Strauss 630b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 631b9e2be50SGlenn Strauss 632b9e2be50SGlenn Strauss /* calculate H(A2) */ 633b9e2be50SGlenn Strauss SHA256_Init(&ctx); 634b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)m, strlen(m)); 635b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 636b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 637b9e2be50SGlenn Strauss #if 0 638b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 639fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 640b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 641b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 642b9e2be50SGlenn Strauss } 643b9e2be50SGlenn Strauss #endif 644b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 645b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 646b9e2be50SGlenn Strauss 647b9e2be50SGlenn Strauss /* calculate response */ 648b9e2be50SGlenn Strauss SHA256_Init(&ctx); 649b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 650b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 651b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 652b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 653b9e2be50SGlenn Strauss if (qop && *qop) { 654b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 655b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 656b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 657b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 658b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 659b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 660b9e2be50SGlenn Strauss } 661b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 662b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 663b9e2be50SGlenn Strauss } 664b9e2be50SGlenn Strauss 665b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd) { 666b9e2be50SGlenn Strauss SHA256_CTX ctx; 667f24e6d69SGlenn Strauss size_t len; 668b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 669b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 670b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 671b9e2be50SGlenn Strauss SHA256_Init(&ctx); 672f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 673f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 674f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 675f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 676b9e2be50SGlenn Strauss SHA256_Final(h, &ctx); 677b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 678b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 679b9e2be50SGlenn Strauss } 680b9e2be50SGlenn Strauss 681b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 682b9e2be50SGlenn Strauss 683b9e2be50SGlenn 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) { 684b9e2be50SGlenn Strauss SHA512_CTX ctx; 685b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 686b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 687b9e2be50SGlenn Strauss 688b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 689b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 690b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 691b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 692b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 693b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 694b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 695b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 696b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 697b9e2be50SGlenn Strauss } 698b9e2be50SGlenn Strauss 699b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 700b9e2be50SGlenn Strauss 701b9e2be50SGlenn Strauss /* calculate H(A2) */ 702b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 703b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m)); 704b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 705b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 706b9e2be50SGlenn Strauss #if 0 707b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 708fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 709b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 710b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2); 711b9e2be50SGlenn Strauss } 712b9e2be50SGlenn Strauss #endif 713b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 714b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 715b9e2be50SGlenn Strauss 716b9e2be50SGlenn Strauss /* calculate response */ 717b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 718b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 719b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 720b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 721b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 722b9e2be50SGlenn Strauss if (qop && *qop) { 723b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 724b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 725b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 726b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 727b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 728b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 729b9e2be50SGlenn Strauss } 730b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 731b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 732b9e2be50SGlenn Strauss } 733b9e2be50SGlenn Strauss 734b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd) { 735b9e2be50SGlenn Strauss SHA512_CTX ctx; 736f24e6d69SGlenn Strauss size_t len; 737b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN]; 738b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 739b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 740b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 741f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 742f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 743f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 744f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 745b9e2be50SGlenn Strauss SHA512_256_Final(h, &ctx); 746b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 747b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 748b9e2be50SGlenn Strauss } 749b9e2be50SGlenn Strauss 750b9e2be50SGlenn Strauss #endif /* SHA512_256_DIGEST_LENGTH */ 751b9e2be50SGlenn Strauss 752b9e2be50SGlenn Strauss #endif /* USE_OPENSSL_CRYPTO */ 753b9e2be50SGlenn Strauss 754b9e2be50SGlenn 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) { 755b9e2be50SGlenn Strauss li_MD5_CTX ctx; 756b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 757b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 758b9e2be50SGlenn Strauss 759b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 760b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 761b9e2be50SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 762b9e2be50SGlenn Strauss * Errata ID: 1649 */ 763b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 764b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 765b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 766b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 767b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 768b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 769b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 770b9e2be50SGlenn Strauss } 771b9e2be50SGlenn Strauss 772b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 773b9e2be50SGlenn Strauss 774b9e2be50SGlenn Strauss /* calculate H(A2) */ 775b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 776b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)m, strlen(m)); 777b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 778b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri)); 779b9e2be50SGlenn Strauss #if 0 780b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 781fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 782b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 783b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 784b9e2be50SGlenn Strauss } 785b9e2be50SGlenn Strauss #endif 786b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 787b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 788b9e2be50SGlenn Strauss 789b9e2be50SGlenn Strauss /* calculate response */ 790b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 791b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 792b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 793b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 794b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 795b9e2be50SGlenn Strauss if (qop && *qop) { 796b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc)); 797b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 798b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 799b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 800b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop)); 801b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 802b9e2be50SGlenn Strauss } 803b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 804b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 805b9e2be50SGlenn Strauss } 806b9e2be50SGlenn Strauss 807b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd) { 808b9e2be50SGlenn Strauss li_MD5_CTX ctx; 809f24e6d69SGlenn Strauss size_t len; 810b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN]; 811b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 812b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 813b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 814f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 815f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 816f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 817f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 818b9e2be50SGlenn Strauss li_MD5_Final(h, &ctx); 819b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 820b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 821b9e2be50SGlenn Strauss } 822b9e2be50SGlenn Strauss 823b9e2be50SGlenn 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) { 824b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_MD5) 825b9e2be50SGlenn Strauss mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop); 826b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 827b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 828b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop); 829b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 830b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 831b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop); 832b9e2be50SGlenn Strauss #endif 833b9e2be50SGlenn Strauss #endif 834b9e2be50SGlenn Strauss } 835b9e2be50SGlenn Strauss 836b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) { 837b9e2be50SGlenn Strauss const int rnd = li_rand_pseudo(); 838b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 839b9e2be50SGlenn Strauss int n = 0; 840b9e2be50SGlenn Strauss void(*append_nonce[3])(buffer *, time_t, int); 841b9e2be50SGlenn Strauss unsigned int algolen[3]; 842b9e2be50SGlenn Strauss const char *algoname[3]; 843b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 844b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 845b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 846b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha512_256; 847b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 848b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 849b9e2be50SGlenn Strauss ++n; 850b9e2be50SGlenn Strauss } 851b9e2be50SGlenn Strauss #endif 852b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 853b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha256; 854b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 855b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 856b9e2be50SGlenn Strauss ++n; 857b9e2be50SGlenn Strauss } 858b9e2be50SGlenn Strauss #endif 859b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 860b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_md5; 861b9e2be50SGlenn Strauss algoname[n] = "MD5"; 862b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 863b9e2be50SGlenn Strauss ++n; 864b9e2be50SGlenn Strauss } 865b9e2be50SGlenn Strauss 866b9e2be50SGlenn Strauss buffer_clear(b); 867b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 868b9e2be50SGlenn Strauss if (i > 0) { 869b9e2be50SGlenn Strauss buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: ")); 870b9e2be50SGlenn Strauss } 871b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\"")); 872b9e2be50SGlenn Strauss buffer_append_string_buffer(b, require->realm); 8731799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=")); 874b9e2be50SGlenn Strauss buffer_append_string_len(b, algoname[i], algolen[i]); 8751799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", nonce=\"")); 876b9e2be50SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 877b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 878b9e2be50SGlenn Strauss (append_nonce[i])(b, cur_ts, rnd); 879b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 880b9e2be50SGlenn Strauss if (nonce_stale) { 881b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 882b9e2be50SGlenn Strauss } 883b9e2be50SGlenn Strauss } 88481b2d1f0SGlenn Strauss } 88581b2d1f0SGlenn Strauss 8866ad325c6SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, int dalgo) { 8876ad325c6SGlenn Strauss const int rnd = li_rand_pseudo(); 8886ad325c6SGlenn Strauss void(*append_nonce)(buffer *, time_t, int); 8896ad325c6SGlenn Strauss switch (dalgo) { 8906ad325c6SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 8916ad325c6SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 8926ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 8936ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha512_256; 8946ad325c6SGlenn Strauss break; 8956ad325c6SGlenn Strauss #endif 8966ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 8976ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha256; 8986ad325c6SGlenn Strauss break; 8996ad325c6SGlenn Strauss #endif 9006ad325c6SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 9016ad325c6SGlenn Strauss default: 9026ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_md5; 9036ad325c6SGlenn Strauss break; 9046ad325c6SGlenn Strauss } 9056ad325c6SGlenn Strauss buffer_clear(b); 9066ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 9076ad325c6SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 9086ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 9096ad325c6SGlenn Strauss (append_nonce)(b, cur_ts, rnd); 9106ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\"")); 9116ad325c6SGlenn Strauss } 9126ad325c6SGlenn Strauss 91381b2d1f0SGlenn Strauss typedef struct { 91481b2d1f0SGlenn Strauss const char *key; 91581b2d1f0SGlenn Strauss int key_len; 91681b2d1f0SGlenn Strauss char **ptr; 91781b2d1f0SGlenn Strauss } digest_kv; 91881b2d1f0SGlenn Strauss 919010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale); 9207ba06c71SGlenn Strauss 921010c2894SGlenn 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) { 9226eb34ef5SGlenn Strauss const buffer *vb = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 9237ba06c71SGlenn Strauss 92481b2d1f0SGlenn Strauss char *username = NULL; 92581b2d1f0SGlenn Strauss char *realm = NULL; 92681b2d1f0SGlenn Strauss char *nonce = NULL; 92781b2d1f0SGlenn Strauss char *uri = NULL; 92881b2d1f0SGlenn Strauss char *algorithm = NULL; 92981b2d1f0SGlenn Strauss char *qop = NULL; 93081b2d1f0SGlenn Strauss char *cnonce = NULL; 93181b2d1f0SGlenn Strauss char *nc = NULL; 93281b2d1f0SGlenn Strauss char *respons = NULL; 93381b2d1f0SGlenn Strauss 93481b2d1f0SGlenn Strauss char *e, *c; 93581b2d1f0SGlenn Strauss const char *m = NULL; 93681b2d1f0SGlenn Strauss int i; 93781b2d1f0SGlenn Strauss buffer *b; 93860f4cf3aSGlenn Strauss http_auth_info_t ai; 939b9e2be50SGlenn Strauss unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 94081b2d1f0SGlenn Strauss 94181b2d1f0SGlenn Strauss 94281b2d1f0SGlenn Strauss /* init pointers */ 94381b2d1f0SGlenn Strauss #define S(x) \ 94481b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 94581b2d1f0SGlenn Strauss digest_kv dkv[10] = { 94681b2d1f0SGlenn Strauss { S("username=") }, 94781b2d1f0SGlenn Strauss { S("realm=") }, 94881b2d1f0SGlenn Strauss { S("nonce=") }, 94981b2d1f0SGlenn Strauss { S("uri=") }, 95081b2d1f0SGlenn Strauss { S("algorithm=") }, 95181b2d1f0SGlenn Strauss { S("qop=") }, 95281b2d1f0SGlenn Strauss { S("cnonce=") }, 95381b2d1f0SGlenn Strauss { S("nc=") }, 95481b2d1f0SGlenn Strauss { S("response=") }, 95581b2d1f0SGlenn Strauss 95681b2d1f0SGlenn Strauss { NULL, 0, NULL } 95781b2d1f0SGlenn Strauss }; 95881b2d1f0SGlenn Strauss #undef S 95981b2d1f0SGlenn Strauss 96081b2d1f0SGlenn Strauss dkv[0].ptr = &username; 96181b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 96281b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 96381b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 96481b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 96581b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 96681b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 96781b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 96881b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 96981b2d1f0SGlenn Strauss 9707ba06c71SGlenn Strauss UNUSED(p_d); 9717ba06c71SGlenn Strauss 9727ba06c71SGlenn Strauss if (NULL == backend) { 973010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 974010c2894SGlenn Strauss "auth.backend not configured for %s", con->uri.path->ptr); 9757ba06c71SGlenn Strauss con->http_status = 500; 97631d94953SGlenn Strauss con->response.handler_module = NULL; 9777ba06c71SGlenn Strauss return HANDLER_FINISHED; 9787ba06c71SGlenn Strauss } 9797ba06c71SGlenn Strauss 9803dd3cde9SGlenn Strauss if (NULL == vb) { 981010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 9827ba06c71SGlenn Strauss } 9837ba06c71SGlenn Strauss 98413008156SGlenn Strauss if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) { 985010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 986dadfb5fcSGlenn Strauss } else { 9873dd3cde9SGlenn Strauss size_t n = buffer_string_length(vb); 9889bc61f16SGlenn Strauss #ifdef __COVERITY__ 989dadfb5fcSGlenn Strauss if (n < sizeof("Digest ")-1) { 990010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 9919bc61f16SGlenn Strauss } 9929bc61f16SGlenn Strauss #endif 993dadfb5fcSGlenn Strauss n -= (sizeof("Digest ")-1); 9947ba06c71SGlenn Strauss b = buffer_init(); 9953dd3cde9SGlenn Strauss buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n); 996dadfb5fcSGlenn Strauss } 99781b2d1f0SGlenn Strauss 99881b2d1f0SGlenn Strauss /* parse credentials from client */ 99981b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 100081b2d1f0SGlenn Strauss /* skip whitespaces */ 100181b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 100281b2d1f0SGlenn Strauss if (!*c) break; 100381b2d1f0SGlenn Strauss 100481b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 100581b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 100681b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 100781b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 100881b2d1f0SGlenn Strauss /* value with "..." */ 100981b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 101081b2d1f0SGlenn Strauss c = e; 101181b2d1f0SGlenn Strauss 101281b2d1f0SGlenn Strauss *e = '\0'; 101381b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 101481b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 101581b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 101681b2d1f0SGlenn Strauss c = e; 101781b2d1f0SGlenn Strauss 101881b2d1f0SGlenn Strauss *e = '\0'; 101981b2d1f0SGlenn Strauss } else { 102081b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 102181b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 102281b2d1f0SGlenn Strauss c += strlen(c) - 1; 102381b2d1f0SGlenn Strauss } 102481b2d1f0SGlenn Strauss break; 102581b2d1f0SGlenn Strauss } 102681b2d1f0SGlenn Strauss } 102781b2d1f0SGlenn Strauss } 102881b2d1f0SGlenn Strauss 102981b2d1f0SGlenn Strauss /* check if everything is transmitted */ 103081b2d1f0SGlenn Strauss if (!username || 103181b2d1f0SGlenn Strauss !realm || 103281b2d1f0SGlenn Strauss !nonce || 103381b2d1f0SGlenn Strauss !uri || 103481b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 103581b2d1f0SGlenn Strauss !respons ) { 103681b2d1f0SGlenn Strauss /* missing field */ 103781b2d1f0SGlenn Strauss 1038010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 103981b2d1f0SGlenn Strauss "digest: missing field"); 104081b2d1f0SGlenn Strauss 104181b2d1f0SGlenn Strauss buffer_free(b); 1042010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 104381b2d1f0SGlenn Strauss } 104481b2d1f0SGlenn Strauss 104560f4cf3aSGlenn Strauss ai.username = username; 104660f4cf3aSGlenn Strauss ai.ulen = strlen(username); 104760f4cf3aSGlenn Strauss ai.realm = realm; 104860f4cf3aSGlenn Strauss ai.rlen = strlen(realm); 104960f4cf3aSGlenn Strauss 105060f4cf3aSGlenn Strauss if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) { 1051010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 10525a6e4df8SGlenn Strauss "digest: realm mismatch"); 10535a6e4df8SGlenn Strauss buffer_free(b); 1054010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 10555a6e4df8SGlenn Strauss } 10565a6e4df8SGlenn Strauss 1057b9e2be50SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, algorithm) 1058b9e2be50SGlenn Strauss || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 1059010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1060010c2894SGlenn Strauss "digest: (%s): invalid", algorithm); 1061b9e2be50SGlenn Strauss buffer_free(b); 1062010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 1063b9e2be50SGlenn Strauss } 1064b9e2be50SGlenn Strauss 106581b2d1f0SGlenn Strauss /** 106681b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 106781b2d1f0SGlenn Strauss */ 1068b9e2be50SGlenn Strauss if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) { 1069010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1070010c2894SGlenn Strauss "digest: (%s): missing field", algorithm); 107181b2d1f0SGlenn Strauss 107281b2d1f0SGlenn Strauss buffer_free(b); 1073010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 107481b2d1f0SGlenn Strauss } 107581b2d1f0SGlenn Strauss 1076b9e2be50SGlenn Strauss { 1077b9e2be50SGlenn Strauss size_t resplen = strlen(respons); 1078b9e2be50SGlenn Strauss if (0 != http_auth_digest_hex2bin(respons, resplen, 1079b9e2be50SGlenn Strauss rdigest, sizeof(rdigest)) 1080b9e2be50SGlenn Strauss || resplen != (ai.dlen << 1)) { 1081010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1082010c2894SGlenn Strauss "digest: (%s): invalid format", respons); 1083b9e2be50SGlenn Strauss buffer_free(b); 1084010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 1085b9e2be50SGlenn Strauss } 1086b9e2be50SGlenn Strauss } 1087b9e2be50SGlenn Strauss 1088fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 1089010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 109081b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 109181b2d1f0SGlenn Strauss 109281b2d1f0SGlenn Strauss buffer_free(b); 1093010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 109481b2d1f0SGlenn Strauss } 109581b2d1f0SGlenn Strauss 109681b2d1f0SGlenn Strauss m = get_http_method_name(con->request.http_method); 109781b2d1f0SGlenn Strauss force_assert(m); 109881b2d1f0SGlenn Strauss 109981b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 110081b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 110181b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 110281b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 110381b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 110481b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 11058131e439SGlenn Strauss * uri validation step is bypassed. con->request.target_orig is 11068131e439SGlenn Strauss * original request-target sent in client request. */ 110781b2d1f0SGlenn Strauss { 110881b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 11098131e439SGlenn Strauss if (!buffer_is_equal_string(con->request.target_orig, uri, ulen)) { 1110010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1111010c2894SGlenn Strauss "digest: auth failed: uri mismatch (%s != %s), IP: %s", 11128131e439SGlenn Strauss con->request.target_orig->ptr, uri, con->dst_addr_buf->ptr); 111381b2d1f0SGlenn Strauss buffer_free(b); 1114010c2894SGlenn Strauss return mod_auth_send_400_bad_request(con); 111581b2d1f0SGlenn Strauss } 111681b2d1f0SGlenn Strauss } 111781b2d1f0SGlenn Strauss 1118010c2894SGlenn Strauss switch (backend->digest(con, backend->p_d, &ai)) { 11194b3a91e6SGlenn Strauss case HANDLER_GO_ON: 11204b3a91e6SGlenn Strauss break; 11214b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 112281b2d1f0SGlenn Strauss buffer_free(b); 11234b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 11244b3a91e6SGlenn Strauss case HANDLER_FINISHED: 11254b3a91e6SGlenn Strauss buffer_free(b); 11264b3a91e6SGlenn Strauss return HANDLER_FINISHED; 11274b3a91e6SGlenn Strauss case HANDLER_ERROR: 11284b3a91e6SGlenn Strauss default: 1129057d83c5SGlenn Strauss con->request.keep_alive = 0; /*(disable keep-alive if unknown user)*/ 11304b3a91e6SGlenn Strauss buffer_free(b); 1131010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 113281b2d1f0SGlenn Strauss } 113381b2d1f0SGlenn Strauss 1134b9e2be50SGlenn Strauss mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop); 113581b2d1f0SGlenn Strauss 11360e749c1cSGlenn Strauss if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) { 113781b2d1f0SGlenn Strauss /* digest not ok */ 1138010c2894SGlenn Strauss log_error(con->conf.errh, __FILE__, __LINE__, 1139010c2894SGlenn Strauss "digest: auth failed for %s: wrong password, IP: %s", 1140010c2894SGlenn Strauss username, con->dst_addr_buf->ptr); 1141057d83c5SGlenn Strauss con->request.keep_alive = 0; /*(disable keep-alive if bad password)*/ 114281b2d1f0SGlenn Strauss 114381b2d1f0SGlenn Strauss buffer_free(b); 1144010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 114581b2d1f0SGlenn Strauss } 114681b2d1f0SGlenn Strauss 114781b2d1f0SGlenn Strauss /* value is our allow-rules */ 11487ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 11494b3a91e6SGlenn Strauss buffer_free(b); 1150010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, 0); 115181b2d1f0SGlenn Strauss } 115281b2d1f0SGlenn Strauss 11537f4e156eSGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 11547ba06c71SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 115581b2d1f0SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 115681b2d1f0SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 115781b2d1f0SGlenn Strauss * verifiable) if nanoseconds were inclued with seconds as part of the 115881b2d1f0SGlenn Strauss * nonce "timestamp:secret". Since that is not done, timestamp in 115981b2d1f0SGlenn Strauss * nonce could theoretically be modified and still produce same md5sum, 116081b2d1f0SGlenn Strauss * but that is highly unlikely within a 10 min (moving) window of valid 116181b2d1f0SGlenn Strauss * time relative to current time (now) */ 116281b2d1f0SGlenn Strauss { 116381b2d1f0SGlenn Strauss time_t ts = 0; 116481b2d1f0SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 116581b2d1f0SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 116681b2d1f0SGlenn Strauss ts = (ts << 4) + hex2int(nonce_uns[i]); 116781b2d1f0SGlenn Strauss } 1168c8cd7cf4SGlenn Strauss const time_t cur_ts = log_epoch_secs; 116983759aedSGlenn Strauss if (nonce[i] != ':' 1170409bba80SGlenn Strauss || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 11714b3a91e6SGlenn Strauss /* nonce is stale; have client regenerate digest */ 117281b2d1f0SGlenn Strauss buffer_free(b); 1173010c2894SGlenn Strauss return mod_auth_send_401_unauthorized_digest(con, require, ai.dalgo); 11746ad325c6SGlenn Strauss } 1175409bba80SGlenn Strauss else if (cur_ts - ts > 540) { /*(9 mins)*/ 11766ad325c6SGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 1177409bba80SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 1178409bba80SGlenn Strauss mod_auth_digest_authentication_info(tb, cur_ts, ai.dalgo); 1179409bba80SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, 1180409bba80SGlenn Strauss CONST_STR_LEN("Authentication-Info"), 1181409bba80SGlenn Strauss CONST_BUF_LEN(tb)); 11826ad325c6SGlenn Strauss } 118381b2d1f0SGlenn Strauss } 118481b2d1f0SGlenn Strauss 118560f4cf3aSGlenn Strauss http_auth_setenv(con, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 118681b2d1f0SGlenn Strauss 118781b2d1f0SGlenn Strauss buffer_free(b); 118881b2d1f0SGlenn Strauss 11894b3a91e6SGlenn Strauss return HANDLER_GO_ON; 119081b2d1f0SGlenn Strauss } 119181b2d1f0SGlenn Strauss 1192010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale) { 1193b5775b99SGlenn Strauss buffer * const tb = con->srv->tmp_buf; 1194b5775b99SGlenn Strauss mod_auth_digest_www_authenticate(tb, log_epoch_secs, require, nonce_stale); 1195b5775b99SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb)); 119681b2d1f0SGlenn Strauss 1197f9d58670SStefan Bühler con->http_status = 401; 119831d94953SGlenn Strauss con->response.handler_module = NULL; 1199f9d58670SStefan Bühler return HANDLER_FINISHED; 12007ba06c71SGlenn Strauss } 12017ba06c71SGlenn Strauss 1202010c2894SGlenn 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) { 12037ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 12046eb34ef5SGlenn Strauss const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")); 12057ba06c71SGlenn Strauss UNUSED(p_d); 12067ba06c71SGlenn Strauss UNUSED(backend); 12073dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1208f9d58670SStefan Bühler return HANDLER_GO_ON; 1209bcdc6a3bSJan Kneschke } else { 12104b3a91e6SGlenn Strauss con->http_status = 401; 121131d94953SGlenn Strauss con->response.handler_module = NULL; 1212bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 12137187271fSStefan Bühler } 1214bcdc6a3bSJan Kneschke } 1215