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 /* auth */ 234b3a91e6SGlenn Strauss array *auth_require; 244b3a91e6SGlenn Strauss buffer *auth_backend_conf; 25f54d628cSGlenn Strauss unsigned short auth_extern_authn; 264b3a91e6SGlenn Strauss 274b3a91e6SGlenn Strauss /* generated */ 284b3a91e6SGlenn Strauss const http_auth_backend_t *auth_backend; 297ba06c71SGlenn Strauss } plugin_config; 304b3a91e6SGlenn Strauss 314b3a91e6SGlenn Strauss typedef struct { 324b3a91e6SGlenn Strauss PLUGIN_DATA; 334b3a91e6SGlenn Strauss 347ba06c71SGlenn Strauss plugin_config **config_storage; 354b3a91e6SGlenn Strauss 367ba06c71SGlenn Strauss plugin_config conf; 377ba06c71SGlenn Strauss } plugin_data; 387ba06c71SGlenn Strauss 397ba06c71SGlenn Strauss static handler_t mod_auth_check_basic(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 407ba06c71SGlenn Strauss static handler_t mod_auth_check_digest(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 417ba06c71SGlenn Strauss static handler_t mod_auth_check_extern(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 424b3a91e6SGlenn Strauss 43bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 447ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 457ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 467ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 477ba06c71SGlenn Strauss plugin_data *p; 487ba06c71SGlenn Strauss 497ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 507ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 517ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 527ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 53bcdc6a3bSJan Kneschke 54bcdc6a3bSJan Kneschke p = calloc(1, sizeof(*p)); 55bcdc6a3bSJan Kneschke 56bcdc6a3bSJan Kneschke return p; 57bcdc6a3bSJan Kneschke } 58bcdc6a3bSJan Kneschke 59bcdc6a3bSJan Kneschke FREE_FUNC(mod_auth_free) { 607ba06c71SGlenn Strauss plugin_data *p = p_d; 61bcdc6a3bSJan Kneschke 62bcdc6a3bSJan Kneschke UNUSED(srv); 63bcdc6a3bSJan Kneschke 64bcdc6a3bSJan Kneschke if (!p) return HANDLER_GO_ON; 65bcdc6a3bSJan Kneschke 66bcdc6a3bSJan Kneschke if (p->config_storage) { 67bcdc6a3bSJan Kneschke size_t i; 68bcdc6a3bSJan Kneschke for (i = 0; i < srv->config_context->used; i++) { 697ba06c71SGlenn Strauss plugin_config *s = p->config_storage[i]; 70bcdc6a3bSJan Kneschke 7133cebeb0SStefan Bühler if (NULL == s) continue; 724a6ce4aeSJan Kneschke 73bcdc6a3bSJan Kneschke array_free(s->auth_require); 74bcdc6a3bSJan Kneschke buffer_free(s->auth_backend_conf); 75bcdc6a3bSJan Kneschke 76bcdc6a3bSJan Kneschke free(s); 77bcdc6a3bSJan Kneschke } 78bcdc6a3bSJan Kneschke free(p->config_storage); 79bcdc6a3bSJan Kneschke } 80bcdc6a3bSJan Kneschke 81bcdc6a3bSJan Kneschke free(p); 82bcdc6a3bSJan Kneschke 83bcdc6a3bSJan Kneschke return HANDLER_GO_ON; 84bcdc6a3bSJan Kneschke } 85bcdc6a3bSJan Kneschke 867ba06c71SGlenn Strauss /* data type for mod_auth structured data 877ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 887ba06c71SGlenn Strauss typedef struct { 897ba06c71SGlenn Strauss DATA_UNSET; 907ba06c71SGlenn Strauss http_auth_require_t *require; 917ba06c71SGlenn Strauss } data_auth; 927ba06c71SGlenn Strauss 937ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 947ba06c71SGlenn Strauss { 957ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 96ad9b7e00SGlenn Strauss free(dauth->key.ptr); 977ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 987ba06c71SGlenn Strauss free(dauth); 997ba06c71SGlenn Strauss } 1007ba06c71SGlenn Strauss 1017ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 1027ba06c71SGlenn Strauss { 1038c7f1dfbSGlenn Strauss static const struct data_methods fn = { 1048c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 1058c7f1dfbSGlenn Strauss data_auth_free, 1068c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 1078c7f1dfbSGlenn Strauss NULL /* print must not be called on this data */ 1088c7f1dfbSGlenn Strauss }; 1097ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 1107ba06c71SGlenn Strauss force_assert(NULL != dauth); 1117ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 1128c7f1dfbSGlenn Strauss dauth->fn = &fn; 1137ba06c71SGlenn Strauss 1147ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 1157ba06c71SGlenn Strauss 1167ba06c71SGlenn Strauss return dauth; 1177ba06c71SGlenn Strauss } 1187ba06c71SGlenn Strauss 119b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) { 120b9e2be50SGlenn Strauss size_t len; 121b9e2be50SGlenn Strauss if (NULL == s) { 122b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 123b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 124b9e2be50SGlenn Strauss return 1; 125b9e2be50SGlenn Strauss } 126b9e2be50SGlenn Strauss 127b9e2be50SGlenn Strauss len = strlen(s); 128b9e2be50SGlenn Strauss if (len > 5 129b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 130b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 131b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 132b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 133b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 134b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 135b9e2be50SGlenn Strauss len -= 5; 136b9e2be50SGlenn Strauss } 137b9e2be50SGlenn Strauss else { 138b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 139b9e2be50SGlenn Strauss } 140b9e2be50SGlenn Strauss 141b9e2be50SGlenn Strauss if (3 == len 142b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 143b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 144b9e2be50SGlenn Strauss && '5' == (s[2] )) { 145b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 146b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 147b9e2be50SGlenn Strauss return 1; 148b9e2be50SGlenn Strauss } 149b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 150b9e2be50SGlenn Strauss else if (len >= 7 151b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 152b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 153b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 154b9e2be50SGlenn Strauss && '-' == (s[3] )) { 155b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 156b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 157b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 158b9e2be50SGlenn Strauss return 1; 159b9e2be50SGlenn Strauss } 160b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 161b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 162b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 163b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 164b9e2be50SGlenn Strauss return 1; 165b9e2be50SGlenn Strauss } 166b9e2be50SGlenn Strauss #endif 167b9e2be50SGlenn Strauss } 168b9e2be50SGlenn Strauss #endif 169b9e2be50SGlenn Strauss return 0; /*(error)*/ 170b9e2be50SGlenn Strauss } 171b9e2be50SGlenn Strauss 172b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 173b9e2be50SGlenn Strauss for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 174b9e2be50SGlenn Strauss http_auth_info_t ai; 175b9e2be50SGlenn Strauss int rc; 176b9e2be50SGlenn Strauss p = strchr(s, '|'); 177b9e2be50SGlenn Strauss if (p) *p = '\0'; 178b9e2be50SGlenn Strauss rc = mod_auth_algorithm_parse(&ai, s); 179b9e2be50SGlenn Strauss if (p) *p = '|'; 180b9e2be50SGlenn Strauss if (!rc) return 0; 181b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 182b9e2be50SGlenn Strauss } 183b9e2be50SGlenn Strauss return 1; 184b9e2be50SGlenn Strauss } 185b9e2be50SGlenn Strauss 1867ba06c71SGlenn Strauss static int mod_auth_require_parse (server *srv, http_auth_require_t * const require, const buffer *b) 1877ba06c71SGlenn Strauss { 1887ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 1897ba06c71SGlenn Strauss 1907ba06c71SGlenn Strauss const char *str = b->ptr; 1917ba06c71SGlenn Strauss const char *p; 1927ba06c71SGlenn Strauss 1937ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 1947ba06c71SGlenn Strauss require->valid_user = 1; 1957ba06c71SGlenn Strauss return 1; /* success */ 1967ba06c71SGlenn Strauss } 1977ba06c71SGlenn Strauss 1987ba06c71SGlenn Strauss do { 1997ba06c71SGlenn Strauss const char *eq; 2007ba06c71SGlenn Strauss size_t len; 2017ba06c71SGlenn Strauss p = strchr(str, '|'); 2027ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 2037ba06c71SGlenn Strauss eq = memchr(str, '=', len); 2047ba06c71SGlenn Strauss if (NULL == eq) { 2057ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 2067ba06c71SGlenn Strauss "error parsing auth.require 'require' field: missing '='", 2077ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 2087ba06c71SGlenn Strauss "error value:", b, "error near:", str); 2097ba06c71SGlenn Strauss return 0; 2107ba06c71SGlenn Strauss } 211b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 2127ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 2137ba06c71SGlenn Strauss "error parsing auth.require 'require' field: missing token after '='", 2147ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 2157ba06c71SGlenn Strauss "error value:", b, "error near:", str); 2167ba06c71SGlenn Strauss return 0; 2177ba06c71SGlenn Strauss } 2187ba06c71SGlenn Strauss 2197ba06c71SGlenn Strauss switch ((int)(eq - str)) { 2207ba06c71SGlenn Strauss case 4: 2217ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 222810109ccSGlenn Strauss /*("user=" is 5)*/ 223810109ccSGlenn Strauss array_set_key_value(require->user, str+5, len-5, CONST_STR_LEN("")); 2247ba06c71SGlenn Strauss continue; 2257ba06c71SGlenn Strauss } 2267ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 227810109ccSGlenn Strauss /*("host=" is 5)*/ 228810109ccSGlenn Strauss array_set_key_value(require->host, str+5, len-5, CONST_STR_LEN("")); 2297ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssb", 2307ba06c71SGlenn Strauss "warning parsing auth.require 'require' field: 'host' not implemented;", 2317ba06c71SGlenn Strauss "field value:", b); 2327ba06c71SGlenn Strauss continue; 2337ba06c71SGlenn Strauss } 2347ba06c71SGlenn Strauss break; /* to error */ 2357ba06c71SGlenn Strauss case 5: 2367ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 237810109ccSGlenn Strauss /*("group=" is 6)*/ 238810109ccSGlenn Strauss array_set_key_value(require->group, str+6, len-6, CONST_STR_LEN("")); 2399c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 2407ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssb", 2417ba06c71SGlenn Strauss "warning parsing auth.require 'require' field: 'group' not implemented;", 2427ba06c71SGlenn Strauss "field value:", b); 2439c91af0cSGlenn Strauss #endif 2447ba06c71SGlenn Strauss continue; 2457ba06c71SGlenn Strauss } 2467ba06c71SGlenn Strauss break; /* to error */ 2477ba06c71SGlenn Strauss case 10: 2487ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 2497ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssb", 2507ba06c71SGlenn Strauss "error parsing auth.require 'require' field: valid user can not be combined with other require rules", 2517ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 2527ba06c71SGlenn Strauss "error value:", b); 2537ba06c71SGlenn Strauss return 0; 2547ba06c71SGlenn Strauss } 2557ba06c71SGlenn Strauss break; /* to error */ 2567ba06c71SGlenn Strauss default: 2577ba06c71SGlenn Strauss break; /* to error */ 2587ba06c71SGlenn Strauss } 2597ba06c71SGlenn Strauss 2607ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 2617ba06c71SGlenn Strauss "error parsing auth.require 'require' field: invalid/unsupported token", 2627ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 2637ba06c71SGlenn Strauss "error value:", b, "error near:", str); 2647ba06c71SGlenn Strauss return 0; 2657ba06c71SGlenn Strauss 2667ba06c71SGlenn Strauss } while (p && *((str = p+1))); 2677ba06c71SGlenn Strauss 2687ba06c71SGlenn Strauss return 1; /* success */ 2697ba06c71SGlenn Strauss } 2707ba06c71SGlenn Strauss 2717ba06c71SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 2727ba06c71SGlenn Strauss plugin_data *p = p_d; 2737ba06c71SGlenn Strauss size_t i; 2747ba06c71SGlenn Strauss 2757ba06c71SGlenn Strauss config_values_t cv[] = { 2767ba06c71SGlenn Strauss { "auth.backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ 2777ba06c71SGlenn Strauss { "auth.require", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 1 */ 278f54d628cSGlenn Strauss { "auth.extern-authn", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },/* 2 */ 2797ba06c71SGlenn Strauss { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } 2807ba06c71SGlenn Strauss }; 2817ba06c71SGlenn Strauss 2826a988bb0SMohammed Sadiq p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); 2837ba06c71SGlenn Strauss 2847ba06c71SGlenn Strauss for (i = 0; i < srv->config_context->used; i++) { 2857ba06c71SGlenn Strauss data_config const* config = (data_config const*)srv->config_context->data[i]; 2867ba06c71SGlenn Strauss plugin_config *s; 2877ba06c71SGlenn Strauss size_t n; 28883535bbeSGlenn Strauss const data_array *da; 2897ba06c71SGlenn Strauss 2907ba06c71SGlenn Strauss s = calloc(1, sizeof(plugin_config)); 2917ba06c71SGlenn Strauss s->auth_backend_conf = buffer_init(); 2927ba06c71SGlenn Strauss 2937ba06c71SGlenn Strauss s->auth_require = array_init(); 2947ba06c71SGlenn Strauss 2957ba06c71SGlenn Strauss cv[0].destination = s->auth_backend_conf; 2967ba06c71SGlenn Strauss cv[1].destination = s->auth_require; /* T_CONFIG_LOCAL; not modified by config_insert_values_global() */ 297f54d628cSGlenn Strauss cv[2].destination = &s->auth_extern_authn; 2987ba06c71SGlenn Strauss 2997ba06c71SGlenn Strauss p->config_storage[i] = s; 3007ba06c71SGlenn Strauss 3017ba06c71SGlenn Strauss if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { 3027ba06c71SGlenn Strauss return HANDLER_ERROR; 3037ba06c71SGlenn Strauss } 3047ba06c71SGlenn Strauss 3057ba06c71SGlenn Strauss if (!buffer_string_is_empty(s->auth_backend_conf)) { 3067ba06c71SGlenn Strauss s->auth_backend = http_auth_backend_get(s->auth_backend_conf); 3077ba06c71SGlenn Strauss if (NULL == s->auth_backend) { 3087ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not supported:", s->auth_backend_conf); 3097ba06c71SGlenn Strauss 3107ba06c71SGlenn Strauss return HANDLER_ERROR; 3117ba06c71SGlenn Strauss } 3127ba06c71SGlenn Strauss } 3137ba06c71SGlenn Strauss 3147ba06c71SGlenn Strauss /* no auth.require for this section */ 31583535bbeSGlenn Strauss if (NULL == (da = (const data_array *)array_get_element_klen(config->value, CONST_STR_LEN("auth.require")))) continue; 3167ba06c71SGlenn Strauss 317c2238256SGlenn Strauss if (da->type != TYPE_ARRAY || !array_is_kvarray(&da->value)) { 318bd77abe0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 319bd77abe0SGlenn Strauss "unexpected value for auth.require; expected ", 320bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 321bd77abe0SGlenn Strauss return HANDLER_ERROR; 322bd77abe0SGlenn Strauss } 323bd77abe0SGlenn Strauss 3247ba06c71SGlenn Strauss 325c2238256SGlenn Strauss for (n = 0; n < da->value.used; n++) { 3267ba06c71SGlenn Strauss size_t m; 327c2238256SGlenn Strauss data_array *da_file = (data_array *)da->value.data[n]; 3287ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 3297ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 330b9e2be50SGlenn Strauss buffer *algos = NULL; 331b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 3327ba06c71SGlenn Strauss 333c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 3347ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 335bd77abe0SGlenn Strauss "unexpected value for auth.require; expected ", 336bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 3377ba06c71SGlenn Strauss 3387ba06c71SGlenn Strauss return HANDLER_ERROR; 3397ba06c71SGlenn Strauss } 3407ba06c71SGlenn Strauss 341c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 342c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 343c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 344ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 345601c572cSGlenn Strauss method = &ds->value; 346ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 347601c572cSGlenn Strauss realm = &ds->value; 348ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 349601c572cSGlenn Strauss require = &ds->value; 350ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 351601c572cSGlenn Strauss algos = &ds->value; 3527ba06c71SGlenn Strauss } else { 3537ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssbs", 3547ba06c71SGlenn Strauss "the field is unknown in:", 3557ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"", 356c2238256SGlenn Strauss &da_file->value.data[m]->key, 3577ba06c71SGlenn Strauss "\" <- => \"...\" ) )"); 3587ba06c71SGlenn Strauss 3597ba06c71SGlenn Strauss return HANDLER_ERROR; 3607ba06c71SGlenn Strauss } 3617ba06c71SGlenn Strauss } else { 3627ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssbs", 3637ba06c71SGlenn Strauss "a string was expected for:", 3647ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"", 365c2238256SGlenn Strauss &da_file->value.data[m]->key, 3667ba06c71SGlenn Strauss "\" <- => \"...\" ) )"); 3677ba06c71SGlenn Strauss 3687ba06c71SGlenn Strauss return HANDLER_ERROR; 3697ba06c71SGlenn Strauss } 3707ba06c71SGlenn Strauss } 3717ba06c71SGlenn Strauss 3727ba06c71SGlenn Strauss if (buffer_string_is_empty(method)) { 3737ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 3747ba06c71SGlenn Strauss "the method field is missing or blank in:", 3757ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 3767ba06c71SGlenn Strauss return HANDLER_ERROR; 3777ba06c71SGlenn Strauss } else { 3787ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 3797ba06c71SGlenn Strauss if (NULL == auth_scheme) { 3807ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbss", 3817ba06c71SGlenn Strauss "unknown method", method, "(e.g. \"basic\", \"digest\" or \"extern\") in", 3827ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )"); 3837ba06c71SGlenn Strauss return HANDLER_ERROR; 3847ba06c71SGlenn Strauss } 3857ba06c71SGlenn Strauss } 3867ba06c71SGlenn Strauss 3877ba06c71SGlenn Strauss if (buffer_is_empty(realm)) { 3887ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 3897ba06c71SGlenn Strauss "the realm field is missing in:", 3907ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 3917ba06c71SGlenn Strauss return HANDLER_ERROR; 3927ba06c71SGlenn Strauss } 3937ba06c71SGlenn Strauss 3947ba06c71SGlenn Strauss if (buffer_string_is_empty(require)) { 3957ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 3967ba06c71SGlenn Strauss "the require field is missing or blank in:", 3977ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 3987ba06c71SGlenn Strauss return HANDLER_ERROR; 3997ba06c71SGlenn Strauss } 4007ba06c71SGlenn Strauss 401b9e2be50SGlenn Strauss if (buffer_string_is_empty(algos)) { 402b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 403b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 404b9e2be50SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 405b9e2be50SGlenn Strauss "invalid algorithm in:", 406b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 407b9e2be50SGlenn Strauss return HANDLER_ERROR; 408b9e2be50SGlenn Strauss } 409b9e2be50SGlenn Strauss 410609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 4117ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 412ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 4137ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 414b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 4157ba06c71SGlenn Strauss buffer_copy_buffer(dauth->require->realm, realm); 4167ba06c71SGlenn Strauss if (!mod_auth_require_parse(srv, dauth->require, require)) { 4178c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 4187ba06c71SGlenn Strauss return HANDLER_ERROR; 4197ba06c71SGlenn Strauss } 4207ba06c71SGlenn Strauss array_insert_unique(s->auth_require, (data_unset *)dauth); 4217ba06c71SGlenn Strauss } 4227ba06c71SGlenn Strauss } 4237ba06c71SGlenn Strauss } 4247ba06c71SGlenn Strauss 4257ba06c71SGlenn Strauss return HANDLER_GO_ON; 4267ba06c71SGlenn Strauss } 4277ba06c71SGlenn Strauss 428bcdc6a3bSJan Kneschke #define PATCH(x) \ 429bcdc6a3bSJan Kneschke p->conf.x = s->x; 4307ba06c71SGlenn Strauss static int mod_auth_patch_connection(server *srv, connection *con, plugin_data *p) { 431bcdc6a3bSJan Kneschke size_t i, j; 4327ba06c71SGlenn Strauss plugin_config *s = p->config_storage[0]; 4334a6ce4aeSJan Kneschke 4344a6ce4aeSJan Kneschke PATCH(auth_backend); 4354a6ce4aeSJan Kneschke PATCH(auth_require); 436f54d628cSGlenn Strauss PATCH(auth_extern_authn); 437bcdc6a3bSJan Kneschke 438bcdc6a3bSJan Kneschke /* skip the first, the global context */ 439bcdc6a3bSJan Kneschke for (i = 1; i < srv->config_context->used; i++) { 44036f64b26SGlenn Strauss if (!config_check_cond(con, i)) continue; /* condition not matched */ 44136f64b26SGlenn Strauss 442bcdc6a3bSJan Kneschke data_config *dc = (data_config *)srv->config_context->data[i]; 4434a6ce4aeSJan Kneschke s = p->config_storage[i]; 444bcdc6a3bSJan Kneschke 445bcdc6a3bSJan Kneschke /* merge config */ 446bcdc6a3bSJan Kneschke for (j = 0; j < dc->value->used; j++) { 447bcdc6a3bSJan Kneschke data_unset *du = dc->value->data[j]; 448bcdc6a3bSJan Kneschke 44947a758f9SGlenn Strauss if (buffer_is_equal_string(&du->key, CONST_STR_LEN("auth.backend"))) { 450bcdc6a3bSJan Kneschke PATCH(auth_backend); 45147a758f9SGlenn Strauss } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("auth.require"))) { 452bcdc6a3bSJan Kneschke PATCH(auth_require); 45347a758f9SGlenn Strauss } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("auth.extern-authn"))) { 454f54d628cSGlenn Strauss PATCH(auth_extern_authn); 455bcdc6a3bSJan Kneschke } 456bcdc6a3bSJan Kneschke } 457bcdc6a3bSJan Kneschke } 458bcdc6a3bSJan Kneschke 459bcdc6a3bSJan Kneschke return 0; 460bcdc6a3bSJan Kneschke } 461bcdc6a3bSJan Kneschke #undef PATCH 462bcdc6a3bSJan Kneschke 4637ba06c71SGlenn Strauss static handler_t mod_auth_uri_handler(server *srv, connection *con, void *p_d) { 4647ba06c71SGlenn Strauss plugin_data *p = p_d; 465d61f3381SGlenn Strauss data_auth *dauth; 46681b2d1f0SGlenn Strauss 4677ba06c71SGlenn Strauss mod_auth_patch_connection(srv, 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 } 4867ba06c71SGlenn Strauss return scheme->checkfn(srv, 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; 493*e2de4e58SGlenn 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 5164b3a91e6SGlenn Strauss static handler_t mod_auth_send_400_bad_request(server *srv, connection *con) { 5174b3a91e6SGlenn Strauss UNUSED(srv); 5184b3a91e6SGlenn Strauss 5194b3a91e6SGlenn Strauss /* a field was missing or invalid */ 5204b3a91e6SGlenn Strauss con->http_status = 400; /* Bad Request */ 5214b3a91e6SGlenn Strauss con->mode = DIRECT; 5224b3a91e6SGlenn Strauss 5234b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5244b3a91e6SGlenn Strauss } 5254b3a91e6SGlenn Strauss 5267ba06c71SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(server *srv, connection *con, buffer *realm) { 5274b3a91e6SGlenn Strauss con->http_status = 401; 5284b3a91e6SGlenn Strauss con->mode = DIRECT; 5294b3a91e6SGlenn Strauss 5307ba06c71SGlenn Strauss buffer_copy_string_len(srv->tmp_buf, CONST_STR_LEN("Basic realm=\"")); 5317ba06c71SGlenn Strauss buffer_append_string_buffer(srv->tmp_buf, realm); 5327ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("\", charset=\"UTF-8\"")); 5334b3a91e6SGlenn Strauss 5343dd3cde9SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(srv->tmp_buf)); 5354b3a91e6SGlenn Strauss 5364b3a91e6SGlenn Strauss return HANDLER_FINISHED; 5374b3a91e6SGlenn Strauss } 5384b3a91e6SGlenn Strauss 5397ba06c71SGlenn Strauss static handler_t mod_auth_check_basic(server *srv, 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) { 5487ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not configured for", con->uri.path); 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) { 5557ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_basic(srv, con, require->realm); 5567ba06c71SGlenn Strauss } 5577ba06c71SGlenn Strauss 55813008156SGlenn Strauss if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) { 5596b887f35SGlenn Strauss return mod_auth_send_401_unauthorized_basic(srv, con, require->realm); 5604b3a91e6SGlenn Strauss } 5619bc61f16SGlenn Strauss #ifdef __COVERITY__ 5623dd3cde9SGlenn Strauss if (buffer_string_length(b) < sizeof("Basic ")-1) { 5639bc61f16SGlenn Strauss return mod_auth_send_400_bad_request(srv, 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)) { 5717ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "decoding base64-string failed", username); 57281b2d1f0SGlenn Strauss 57381b2d1f0SGlenn Strauss buffer_free(username); 5744b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 57581b2d1f0SGlenn Strauss } 57681b2d1f0SGlenn Strauss 57781b2d1f0SGlenn Strauss /* r2 == user:password */ 57881b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 5797ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "missing ':' in", username); 58081b2d1f0SGlenn Strauss 58181b2d1f0SGlenn Strauss buffer_free(username); 5824b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 58381b2d1f0SGlenn Strauss } 58481b2d1f0SGlenn Strauss 58581b2d1f0SGlenn Strauss buffer_string_set_length(username, pw - username->ptr); 58681b2d1f0SGlenn Strauss pw++; 58781b2d1f0SGlenn Strauss 588a401c946SGlenn Strauss rc = backend->basic(srv, 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: 59828d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbsBsB", "password doesn't match for", con->uri.path, "username:", username, ", IP:", con->dst_addr_buf); 5998bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if bad password)*/ 6007ba06c71SGlenn Strauss rc = HANDLER_UNSET; 6017ba06c71SGlenn Strauss break; 60281b2d1f0SGlenn Strauss } 60381b2d1f0SGlenn Strauss 6044b3a91e6SGlenn Strauss buffer_free(username); 6057ba06c71SGlenn Strauss return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(srv, con, require->realm); 60681b2d1f0SGlenn Strauss } 60781b2d1f0SGlenn Strauss 60881b2d1f0SGlenn Strauss 609b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 610b9e2be50SGlenn Strauss 611b9e2be50SGlenn 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) { 612b9e2be50SGlenn Strauss SHA256_CTX ctx; 613b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 614b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 615b9e2be50SGlenn Strauss 616b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 617b9e2be50SGlenn Strauss SHA256_Init(&ctx); 618b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 619b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 620b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 621b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 622b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 623b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 624b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 625b9e2be50SGlenn Strauss } 626b9e2be50SGlenn Strauss 627b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 628b9e2be50SGlenn Strauss 629b9e2be50SGlenn Strauss /* calculate H(A2) */ 630b9e2be50SGlenn Strauss SHA256_Init(&ctx); 631b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)m, strlen(m)); 632b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 633b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 634b9e2be50SGlenn Strauss #if 0 635b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 636fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 637b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 638b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 639b9e2be50SGlenn Strauss } 640b9e2be50SGlenn Strauss #endif 641b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 642b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 643b9e2be50SGlenn Strauss 644b9e2be50SGlenn Strauss /* calculate response */ 645b9e2be50SGlenn Strauss SHA256_Init(&ctx); 646b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 647b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 648b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 649b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 650b9e2be50SGlenn Strauss if (qop && *qop) { 651b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 652b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 653b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 654b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 655b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 656b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 657b9e2be50SGlenn Strauss } 658b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 659b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 660b9e2be50SGlenn Strauss } 661b9e2be50SGlenn Strauss 662b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd) { 663b9e2be50SGlenn Strauss SHA256_CTX ctx; 664b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 665b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 666b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 667b9e2be50SGlenn Strauss SHA256_Init(&ctx); 668b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), cur_ts); 669b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, strlen(hh)); 670b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), rnd); 671b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, strlen(hh)); 672b9e2be50SGlenn Strauss SHA256_Final(h, &ctx); 673b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 674b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 675b9e2be50SGlenn Strauss } 676b9e2be50SGlenn Strauss 677b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 678b9e2be50SGlenn Strauss 679b9e2be50SGlenn 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) { 680b9e2be50SGlenn Strauss SHA512_CTX ctx; 681b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 682b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 683b9e2be50SGlenn Strauss 684b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 685b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 686b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 687b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 688b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 689b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 690b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 691b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 692b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 693b9e2be50SGlenn Strauss } 694b9e2be50SGlenn Strauss 695b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 696b9e2be50SGlenn Strauss 697b9e2be50SGlenn Strauss /* calculate H(A2) */ 698b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 699b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m)); 700b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 701b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 702b9e2be50SGlenn Strauss #if 0 703b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 704fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 705b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 706b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2); 707b9e2be50SGlenn Strauss } 708b9e2be50SGlenn Strauss #endif 709b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 710b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 711b9e2be50SGlenn Strauss 712b9e2be50SGlenn Strauss /* calculate response */ 713b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 714b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 715b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 716b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 717b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 718b9e2be50SGlenn Strauss if (qop && *qop) { 719b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 720b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 721b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 722b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 723b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 724b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 725b9e2be50SGlenn Strauss } 726b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 727b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 728b9e2be50SGlenn Strauss } 729b9e2be50SGlenn Strauss 730b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd) { 731b9e2be50SGlenn Strauss SHA512_CTX ctx; 732b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN]; 733b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 734b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 735b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 736b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), cur_ts); 737b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, strlen(hh)); 738b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), rnd); 739b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, strlen(hh)); 740b9e2be50SGlenn Strauss SHA512_256_Final(h, &ctx); 741b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 742b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 743b9e2be50SGlenn Strauss } 744b9e2be50SGlenn Strauss 745b9e2be50SGlenn Strauss #endif /* SHA512_256_DIGEST_LENGTH */ 746b9e2be50SGlenn Strauss 747b9e2be50SGlenn Strauss #endif /* USE_OPENSSL_CRYPTO */ 748b9e2be50SGlenn Strauss 749b9e2be50SGlenn 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) { 750b9e2be50SGlenn Strauss li_MD5_CTX ctx; 751b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 752b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 753b9e2be50SGlenn Strauss 754b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 755b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 756b9e2be50SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 757b9e2be50SGlenn Strauss * Errata ID: 1649 */ 758b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 759b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 760b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 761b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 762b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 763b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 764b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 765b9e2be50SGlenn Strauss } 766b9e2be50SGlenn Strauss 767b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 768b9e2be50SGlenn Strauss 769b9e2be50SGlenn Strauss /* calculate H(A2) */ 770b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 771b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)m, strlen(m)); 772b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 773b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri)); 774b9e2be50SGlenn Strauss #if 0 775b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 776fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 777b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 778b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 779b9e2be50SGlenn Strauss } 780b9e2be50SGlenn Strauss #endif 781b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 782b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 783b9e2be50SGlenn Strauss 784b9e2be50SGlenn Strauss /* calculate response */ 785b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 786b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 787b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 788b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 789b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 790b9e2be50SGlenn Strauss if (qop && *qop) { 791b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc)); 792b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 793b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 794b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 795b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop)); 796b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 797b9e2be50SGlenn Strauss } 798b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 799b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 800b9e2be50SGlenn Strauss } 801b9e2be50SGlenn Strauss 802b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd) { 803b9e2be50SGlenn Strauss li_MD5_CTX ctx; 804b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN]; 805b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 806b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 807b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 808b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), cur_ts); 809b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, strlen(hh)); 810b9e2be50SGlenn Strauss li_itostrn(hh, sizeof(hh), rnd); 811b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, strlen(hh)); 812b9e2be50SGlenn Strauss li_MD5_Final(h, &ctx); 813b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 814b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 815b9e2be50SGlenn Strauss } 816b9e2be50SGlenn Strauss 817b9e2be50SGlenn 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) { 818b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_MD5) 819b9e2be50SGlenn Strauss mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop); 820b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 821b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 822b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop); 823b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 824b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 825b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop); 826b9e2be50SGlenn Strauss #endif 827b9e2be50SGlenn Strauss #endif 828b9e2be50SGlenn Strauss } 829b9e2be50SGlenn Strauss 830b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) { 831b9e2be50SGlenn Strauss const int rnd = li_rand_pseudo(); 832b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 833b9e2be50SGlenn Strauss int n = 0; 834b9e2be50SGlenn Strauss void(*append_nonce[3])(buffer *, time_t, int); 835b9e2be50SGlenn Strauss unsigned int algolen[3]; 836b9e2be50SGlenn Strauss const char *algoname[3]; 837b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 838b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 839b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 840b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha512_256; 841b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 842b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 843b9e2be50SGlenn Strauss ++n; 844b9e2be50SGlenn Strauss } 845b9e2be50SGlenn Strauss #endif 846b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 847b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_sha256; 848b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 849b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 850b9e2be50SGlenn Strauss ++n; 851b9e2be50SGlenn Strauss } 852b9e2be50SGlenn Strauss #endif 853b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 854b9e2be50SGlenn Strauss append_nonce[n] = mod_auth_digest_nonce_md5; 855b9e2be50SGlenn Strauss algoname[n] = "MD5"; 856b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 857b9e2be50SGlenn Strauss ++n; 858b9e2be50SGlenn Strauss } 859b9e2be50SGlenn Strauss 860b9e2be50SGlenn Strauss buffer_clear(b); 861b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 862b9e2be50SGlenn Strauss if (i > 0) { 863b9e2be50SGlenn Strauss buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: ")); 864b9e2be50SGlenn Strauss } 865b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\"")); 866b9e2be50SGlenn Strauss buffer_append_string_buffer(b, require->realm); 8671799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=")); 868b9e2be50SGlenn Strauss buffer_append_string_len(b, algoname[i], algolen[i]); 8691799e4c3SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", nonce=\"")); 870b9e2be50SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 871b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 872b9e2be50SGlenn Strauss (append_nonce[i])(b, cur_ts, rnd); 873b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 874b9e2be50SGlenn Strauss if (nonce_stale) { 875b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 876b9e2be50SGlenn Strauss } 877b9e2be50SGlenn Strauss } 87881b2d1f0SGlenn Strauss } 87981b2d1f0SGlenn Strauss 8806ad325c6SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, int dalgo) { 8816ad325c6SGlenn Strauss const int rnd = li_rand_pseudo(); 8826ad325c6SGlenn Strauss void(*append_nonce)(buffer *, time_t, int); 8836ad325c6SGlenn Strauss switch (dalgo) { 8846ad325c6SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO 8856ad325c6SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH 8866ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 8876ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha512_256; 8886ad325c6SGlenn Strauss break; 8896ad325c6SGlenn Strauss #endif 8906ad325c6SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 8916ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_sha256; 8926ad325c6SGlenn Strauss break; 8936ad325c6SGlenn Strauss #endif 8946ad325c6SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 8956ad325c6SGlenn Strauss default: 8966ad325c6SGlenn Strauss append_nonce = mod_auth_digest_nonce_md5; 8976ad325c6SGlenn Strauss break; 8986ad325c6SGlenn Strauss } 8996ad325c6SGlenn Strauss buffer_clear(b); 9006ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 9016ad325c6SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 9026ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 9036ad325c6SGlenn Strauss (append_nonce)(b, cur_ts, rnd); 9046ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\"")); 9056ad325c6SGlenn Strauss } 9066ad325c6SGlenn Strauss 90781b2d1f0SGlenn Strauss typedef struct { 90881b2d1f0SGlenn Strauss const char *key; 90981b2d1f0SGlenn Strauss int key_len; 91081b2d1f0SGlenn Strauss char **ptr; 91181b2d1f0SGlenn Strauss } digest_kv; 91281b2d1f0SGlenn Strauss 91328895ab2SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(server *srv, connection *con, const struct http_auth_require_t *require, int nonce_stale); 9147ba06c71SGlenn Strauss 9157ba06c71SGlenn Strauss static handler_t mod_auth_check_digest(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 9166eb34ef5SGlenn Strauss const buffer *vb = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 9177ba06c71SGlenn Strauss 91881b2d1f0SGlenn Strauss char *username = NULL; 91981b2d1f0SGlenn Strauss char *realm = NULL; 92081b2d1f0SGlenn Strauss char *nonce = NULL; 92181b2d1f0SGlenn Strauss char *uri = NULL; 92281b2d1f0SGlenn Strauss char *algorithm = NULL; 92381b2d1f0SGlenn Strauss char *qop = NULL; 92481b2d1f0SGlenn Strauss char *cnonce = NULL; 92581b2d1f0SGlenn Strauss char *nc = NULL; 92681b2d1f0SGlenn Strauss char *respons = NULL; 92781b2d1f0SGlenn Strauss 92881b2d1f0SGlenn Strauss char *e, *c; 92981b2d1f0SGlenn Strauss const char *m = NULL; 93081b2d1f0SGlenn Strauss int i; 93181b2d1f0SGlenn Strauss buffer *b; 93260f4cf3aSGlenn Strauss http_auth_info_t ai; 933b9e2be50SGlenn Strauss unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 93481b2d1f0SGlenn Strauss 93581b2d1f0SGlenn Strauss 93681b2d1f0SGlenn Strauss /* init pointers */ 93781b2d1f0SGlenn Strauss #define S(x) \ 93881b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 93981b2d1f0SGlenn Strauss digest_kv dkv[10] = { 94081b2d1f0SGlenn Strauss { S("username=") }, 94181b2d1f0SGlenn Strauss { S("realm=") }, 94281b2d1f0SGlenn Strauss { S("nonce=") }, 94381b2d1f0SGlenn Strauss { S("uri=") }, 94481b2d1f0SGlenn Strauss { S("algorithm=") }, 94581b2d1f0SGlenn Strauss { S("qop=") }, 94681b2d1f0SGlenn Strauss { S("cnonce=") }, 94781b2d1f0SGlenn Strauss { S("nc=") }, 94881b2d1f0SGlenn Strauss { S("response=") }, 94981b2d1f0SGlenn Strauss 95081b2d1f0SGlenn Strauss { NULL, 0, NULL } 95181b2d1f0SGlenn Strauss }; 95281b2d1f0SGlenn Strauss #undef S 95381b2d1f0SGlenn Strauss 95481b2d1f0SGlenn Strauss dkv[0].ptr = &username; 95581b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 95681b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 95781b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 95881b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 95981b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 96081b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 96181b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 96281b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 96381b2d1f0SGlenn Strauss 9647ba06c71SGlenn Strauss UNUSED(p_d); 9657ba06c71SGlenn Strauss 9667ba06c71SGlenn Strauss if (NULL == backend) { 9677ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not configured for", con->uri.path); 9687ba06c71SGlenn Strauss con->http_status = 500; 9697ba06c71SGlenn Strauss con->mode = DIRECT; 9707ba06c71SGlenn Strauss return HANDLER_FINISHED; 9717ba06c71SGlenn Strauss } 9727ba06c71SGlenn Strauss 9733dd3cde9SGlenn Strauss if (NULL == vb) { 97428895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 9757ba06c71SGlenn Strauss } 9767ba06c71SGlenn Strauss 97713008156SGlenn Strauss if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) { 97828895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 979dadfb5fcSGlenn Strauss } else { 9803dd3cde9SGlenn Strauss size_t n = buffer_string_length(vb); 9819bc61f16SGlenn Strauss #ifdef __COVERITY__ 982dadfb5fcSGlenn Strauss if (n < sizeof("Digest ")-1) { 9839bc61f16SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 9849bc61f16SGlenn Strauss } 9859bc61f16SGlenn Strauss #endif 986dadfb5fcSGlenn Strauss n -= (sizeof("Digest ")-1); 9877ba06c71SGlenn Strauss b = buffer_init(); 9883dd3cde9SGlenn Strauss buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n); 989dadfb5fcSGlenn Strauss } 99081b2d1f0SGlenn Strauss 99181b2d1f0SGlenn Strauss /* parse credentials from client */ 99281b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 99381b2d1f0SGlenn Strauss /* skip whitespaces */ 99481b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 99581b2d1f0SGlenn Strauss if (!*c) break; 99681b2d1f0SGlenn Strauss 99781b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 99881b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 99981b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 100081b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 100181b2d1f0SGlenn Strauss /* value with "..." */ 100281b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 100381b2d1f0SGlenn Strauss c = e; 100481b2d1f0SGlenn Strauss 100581b2d1f0SGlenn Strauss *e = '\0'; 100681b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 100781b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 100881b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 100981b2d1f0SGlenn Strauss c = e; 101081b2d1f0SGlenn Strauss 101181b2d1f0SGlenn Strauss *e = '\0'; 101281b2d1f0SGlenn Strauss } else { 101381b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 101481b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 101581b2d1f0SGlenn Strauss c += strlen(c) - 1; 101681b2d1f0SGlenn Strauss } 101781b2d1f0SGlenn Strauss break; 101881b2d1f0SGlenn Strauss } 101981b2d1f0SGlenn Strauss } 102081b2d1f0SGlenn Strauss } 102181b2d1f0SGlenn Strauss 102281b2d1f0SGlenn Strauss /* check if everything is transmitted */ 102381b2d1f0SGlenn Strauss if (!username || 102481b2d1f0SGlenn Strauss !realm || 102581b2d1f0SGlenn Strauss !nonce || 102681b2d1f0SGlenn Strauss !uri || 102781b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 102881b2d1f0SGlenn Strauss !respons ) { 102981b2d1f0SGlenn Strauss /* missing field */ 103081b2d1f0SGlenn Strauss 103181b2d1f0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 103281b2d1f0SGlenn Strauss "digest: missing field"); 103381b2d1f0SGlenn Strauss 103481b2d1f0SGlenn Strauss buffer_free(b); 10354b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 103681b2d1f0SGlenn Strauss } 103781b2d1f0SGlenn Strauss 103860f4cf3aSGlenn Strauss ai.username = username; 103960f4cf3aSGlenn Strauss ai.ulen = strlen(username); 104060f4cf3aSGlenn Strauss ai.realm = realm; 104160f4cf3aSGlenn Strauss ai.rlen = strlen(realm); 104260f4cf3aSGlenn Strauss 104360f4cf3aSGlenn Strauss if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) { 10445a6e4df8SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 10455a6e4df8SGlenn Strauss "digest: realm mismatch"); 10465a6e4df8SGlenn Strauss buffer_free(b); 104728895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 10485a6e4df8SGlenn Strauss } 10495a6e4df8SGlenn Strauss 1050b9e2be50SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, algorithm) 1051b9e2be50SGlenn Strauss || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 1052b9e2be50SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "SSS", 1053b9e2be50SGlenn Strauss "digest: (", algorithm, "): invalid"); 1054b9e2be50SGlenn Strauss buffer_free(b); 1055b9e2be50SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 1056b9e2be50SGlenn Strauss } 1057b9e2be50SGlenn Strauss 105881b2d1f0SGlenn Strauss /** 105981b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 106081b2d1f0SGlenn Strauss */ 1061b9e2be50SGlenn Strauss if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) { 1062b9e2be50SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "SSS", 1063b9e2be50SGlenn Strauss "digest: (", algorithm, "): missing field"); 106481b2d1f0SGlenn Strauss 106581b2d1f0SGlenn Strauss buffer_free(b); 10664b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 106781b2d1f0SGlenn Strauss } 106881b2d1f0SGlenn Strauss 1069b9e2be50SGlenn Strauss { 1070b9e2be50SGlenn Strauss size_t resplen = strlen(respons); 1071b9e2be50SGlenn Strauss if (0 != http_auth_digest_hex2bin(respons, resplen, 1072b9e2be50SGlenn Strauss rdigest, sizeof(rdigest)) 1073b9e2be50SGlenn Strauss || resplen != (ai.dlen << 1)) { 1074b9e2be50SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "SSS", 1075b9e2be50SGlenn Strauss "digest: (", respons, "): invalid format"); 1076b9e2be50SGlenn Strauss buffer_free(b); 1077b9e2be50SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 1078b9e2be50SGlenn Strauss } 1079b9e2be50SGlenn Strauss } 1080b9e2be50SGlenn Strauss 1081fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 108281b2d1f0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 108381b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 108481b2d1f0SGlenn Strauss 108581b2d1f0SGlenn Strauss buffer_free(b); 10864b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 108781b2d1f0SGlenn Strauss } 108881b2d1f0SGlenn Strauss 108981b2d1f0SGlenn Strauss m = get_http_method_name(con->request.http_method); 109081b2d1f0SGlenn Strauss force_assert(m); 109181b2d1f0SGlenn Strauss 109281b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 109381b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 109481b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 109581b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 109681b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 109781b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 109881b2d1f0SGlenn Strauss * uri validation step is bypassed. con->request.orig_uri is original 109981b2d1f0SGlenn Strauss * uri sent in client request. */ 110081b2d1f0SGlenn Strauss { 110181b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 1102c81bd354SGlenn Strauss if (!buffer_is_equal_string(con->request.orig_uri, uri, ulen)) { 110328d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbsssB", 11047ba06c71SGlenn Strauss "digest: auth failed: uri mismatch (", con->request.orig_uri, "!=", uri, "), IP:", con->dst_addr_buf); 110581b2d1f0SGlenn Strauss buffer_free(b); 11064b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 110781b2d1f0SGlenn Strauss } 110881b2d1f0SGlenn Strauss } 110981b2d1f0SGlenn Strauss 111060f4cf3aSGlenn Strauss switch (backend->digest(srv, con, backend->p_d, &ai)) { 11114b3a91e6SGlenn Strauss case HANDLER_GO_ON: 11124b3a91e6SGlenn Strauss break; 11134b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 111481b2d1f0SGlenn Strauss buffer_free(b); 11154b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 11164b3a91e6SGlenn Strauss case HANDLER_FINISHED: 11174b3a91e6SGlenn Strauss buffer_free(b); 11184b3a91e6SGlenn Strauss return HANDLER_FINISHED; 11194b3a91e6SGlenn Strauss case HANDLER_ERROR: 11204b3a91e6SGlenn Strauss default: 11218bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if unknown user)*/ 11224b3a91e6SGlenn Strauss buffer_free(b); 112328895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 112481b2d1f0SGlenn Strauss } 112581b2d1f0SGlenn Strauss 1126b9e2be50SGlenn Strauss mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop); 112781b2d1f0SGlenn Strauss 11280e749c1cSGlenn Strauss if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) { 112981b2d1f0SGlenn Strauss /* digest not ok */ 113028d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssB", 11317ba06c71SGlenn Strauss "digest: auth failed for ", username, ": wrong password, IP:", con->dst_addr_buf); 11328bddac92SGlenn Strauss con->keep_alive = 0; /*(disable keep-alive if bad password)*/ 113381b2d1f0SGlenn Strauss 113481b2d1f0SGlenn Strauss buffer_free(b); 113528895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 113681b2d1f0SGlenn Strauss } 113781b2d1f0SGlenn Strauss 113881b2d1f0SGlenn Strauss /* value is our allow-rules */ 11397ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 11404b3a91e6SGlenn Strauss buffer_free(b); 114128895ab2SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, 0); 114281b2d1f0SGlenn Strauss } 114381b2d1f0SGlenn Strauss 11447f4e156eSGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 11457ba06c71SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 114681b2d1f0SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 114781b2d1f0SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 114881b2d1f0SGlenn Strauss * verifiable) if nanoseconds were inclued with seconds as part of the 114981b2d1f0SGlenn Strauss * nonce "timestamp:secret". Since that is not done, timestamp in 115081b2d1f0SGlenn Strauss * nonce could theoretically be modified and still produce same md5sum, 115181b2d1f0SGlenn Strauss * but that is highly unlikely within a 10 min (moving) window of valid 115281b2d1f0SGlenn Strauss * time relative to current time (now) */ 115381b2d1f0SGlenn Strauss { 115481b2d1f0SGlenn Strauss time_t ts = 0; 115581b2d1f0SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 115681b2d1f0SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 115781b2d1f0SGlenn Strauss ts = (ts << 4) + hex2int(nonce_uns[i]); 115881b2d1f0SGlenn Strauss } 115983759aedSGlenn Strauss if (nonce[i] != ':' 116081b2d1f0SGlenn Strauss || ts > srv->cur_ts || srv->cur_ts - ts > 600) { /*(10 mins)*/ 11614b3a91e6SGlenn Strauss /* nonce is stale; have client regenerate digest */ 116281b2d1f0SGlenn Strauss buffer_free(b); 1163b9e2be50SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require, ai.dalgo); 11646ad325c6SGlenn Strauss } 11656ad325c6SGlenn Strauss else if (srv->cur_ts - ts > 540) { /*(9 mins)*/ 11666ad325c6SGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 11676ad325c6SGlenn Strauss mod_auth_digest_authentication_info(srv->tmp_buf, srv->cur_ts, ai.dalgo); 11686ad325c6SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Authentication-Info"), CONST_BUF_LEN(srv->tmp_buf)); 11696ad325c6SGlenn Strauss } 117081b2d1f0SGlenn Strauss } 117181b2d1f0SGlenn Strauss 117260f4cf3aSGlenn Strauss http_auth_setenv(con, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 117381b2d1f0SGlenn Strauss 117481b2d1f0SGlenn Strauss buffer_free(b); 117581b2d1f0SGlenn Strauss 11764b3a91e6SGlenn Strauss return HANDLER_GO_ON; 117781b2d1f0SGlenn Strauss } 117881b2d1f0SGlenn Strauss 117928895ab2SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(server *srv, connection *con, const struct http_auth_require_t *require, int nonce_stale) { 1180b9e2be50SGlenn Strauss mod_auth_digest_www_authenticate(srv->tmp_buf, srv->cur_ts, require, nonce_stale); 1181b9e2be50SGlenn Strauss http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(srv->tmp_buf)); 118281b2d1f0SGlenn Strauss 1183f9d58670SStefan Bühler con->http_status = 401; 1184f9d58670SStefan Bühler con->mode = DIRECT; 1185f9d58670SStefan Bühler return HANDLER_FINISHED; 11867ba06c71SGlenn Strauss } 11877ba06c71SGlenn Strauss 11887ba06c71SGlenn Strauss static handler_t mod_auth_check_extern(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 11897ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 11906eb34ef5SGlenn Strauss const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")); 11917ba06c71SGlenn Strauss UNUSED(srv); 11927ba06c71SGlenn Strauss UNUSED(p_d); 11937ba06c71SGlenn Strauss UNUSED(backend); 11943dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1195f9d58670SStefan Bühler return HANDLER_GO_ON; 1196bcdc6a3bSJan Kneschke } else { 11974b3a91e6SGlenn Strauss con->http_status = 401; 11984b3a91e6SGlenn Strauss con->mode = DIRECT; 1199bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 12007187271fSStefan Bühler } 1201bcdc6a3bSJan Kneschke } 1202