xref: /lighttpd1.4/src/mod_auth.c (revision 1fc8a3e1)
18abd06a7SGlenn Strauss #include "first.h"
28abd06a7SGlenn Strauss 
3*1fc8a3e1SGlenn Strauss #include <stdlib.h>
4*1fc8a3e1SGlenn Strauss #include <string.h>
5*1fc8a3e1SGlenn Strauss 
6*1fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" /* USE_LIB_CRYPTO */
7*1fc8a3e1SGlenn Strauss 
804d76e7aSGlenn Strauss #include "base.h"
922e8b456SStefan Bühler #include "plugin.h"
1022e8b456SStefan Bühler #include "http_auth.h"
113dd3cde9SGlenn Strauss #include "http_header.h"
1222e8b456SStefan Bühler #include "log.h"
13bcdc6a3bSJan Kneschke 
14bcdc6a3bSJan Kneschke /**
157ba06c71SGlenn Strauss  * auth framework
16bcdc6a3bSJan Kneschke  */
17bcdc6a3bSJan Kneschke 
184b3a91e6SGlenn Strauss typedef struct {
194b3a91e6SGlenn Strauss     const http_auth_backend_t *auth_backend;
208e713130SGlenn Strauss     const array *auth_require;
218e713130SGlenn Strauss     unsigned int auth_extern_authn;
227ba06c71SGlenn Strauss } plugin_config;
234b3a91e6SGlenn Strauss 
244b3a91e6SGlenn Strauss typedef struct {
254b3a91e6SGlenn Strauss     PLUGIN_DATA;
268e713130SGlenn Strauss     plugin_config defaults;
277ba06c71SGlenn Strauss     plugin_config conf;
287ba06c71SGlenn Strauss } plugin_data;
297ba06c71SGlenn Strauss 
307c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
317c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
327c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
334b3a91e6SGlenn Strauss 
34bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) {
357ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_basic  = { "basic",  mod_auth_check_basic,  NULL };
367ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL };
377ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL };
387ba06c71SGlenn Strauss 	plugin_data *p;
397ba06c71SGlenn Strauss 
407ba06c71SGlenn Strauss 	/* register http_auth_scheme_* */
417ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_basic);
427ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_digest);
437ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_extern);
44bcdc6a3bSJan Kneschke 
45bcdc6a3bSJan Kneschke 	p = calloc(1, sizeof(*p));
46bcdc6a3bSJan Kneschke 
47bcdc6a3bSJan Kneschke 	return p;
48bcdc6a3bSJan Kneschke }
49bcdc6a3bSJan Kneschke 
50b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) {
51b73949e0SGlenn Strauss     plugin_data * const p = p_d;
528e713130SGlenn Strauss     if (NULL == p->cvlist) return;
538e713130SGlenn Strauss     /* (init i to 0 if global context; to 1 to skip empty global context) */
548e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
558e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
568e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
578e713130SGlenn Strauss             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
588e713130SGlenn Strauss             switch (cpv->k_id) {
598e713130SGlenn Strauss               case 1: /* auth.require */
608e713130SGlenn Strauss                 array_free(cpv->v.v);
618e713130SGlenn Strauss                 break;
628e713130SGlenn Strauss               default:
638e713130SGlenn Strauss                 break;
648e713130SGlenn Strauss             }
658e713130SGlenn Strauss         }
668e713130SGlenn Strauss     }
678e713130SGlenn Strauss }
688e713130SGlenn Strauss 
697ba06c71SGlenn Strauss /* data type for mod_auth structured data
707ba06c71SGlenn Strauss  * (parsed from auth.require array of strings) */
717ba06c71SGlenn Strauss typedef struct {
727ba06c71SGlenn Strauss     DATA_UNSET;
737ba06c71SGlenn Strauss     http_auth_require_t *require;
747ba06c71SGlenn Strauss } data_auth;
757ba06c71SGlenn Strauss 
767ba06c71SGlenn Strauss static void data_auth_free(data_unset *d)
777ba06c71SGlenn Strauss {
787ba06c71SGlenn Strauss     data_auth * const dauth = (data_auth *)d;
79ad9b7e00SGlenn Strauss     free(dauth->key.ptr);
807ba06c71SGlenn Strauss     http_auth_require_free(dauth->require);
817ba06c71SGlenn Strauss     free(dauth);
827ba06c71SGlenn Strauss }
837ba06c71SGlenn Strauss 
847ba06c71SGlenn Strauss static data_auth *data_auth_init(void)
857ba06c71SGlenn Strauss {
868c7f1dfbSGlenn Strauss     static const struct data_methods fn = {
878c7f1dfbSGlenn Strauss       NULL, /* copy must not be called on this data */
888c7f1dfbSGlenn Strauss       data_auth_free,
898c7f1dfbSGlenn Strauss       NULL, /* insert_dup must not be called on this data */
908c7f1dfbSGlenn Strauss       NULL  /* print must not be called on this data */
918c7f1dfbSGlenn Strauss     };
927ba06c71SGlenn Strauss     data_auth * const dauth = calloc(1, sizeof(*dauth));
937ba06c71SGlenn Strauss     force_assert(NULL != dauth);
947ba06c71SGlenn Strauss     dauth->type       = TYPE_OTHER;
958c7f1dfbSGlenn Strauss     dauth->fn         = &fn;
967ba06c71SGlenn Strauss 
977ba06c71SGlenn Strauss     dauth->require = http_auth_require_init();
987ba06c71SGlenn Strauss 
997ba06c71SGlenn Strauss     return dauth;
1007ba06c71SGlenn Strauss }
1017ba06c71SGlenn Strauss 
102b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) {
103b9e2be50SGlenn Strauss     size_t len;
104b9e2be50SGlenn Strauss     if (NULL == s) {
105b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_MD5;
106b9e2be50SGlenn Strauss         ai->dlen  = HTTP_AUTH_DIGEST_MD5_BINLEN;
107b9e2be50SGlenn Strauss         return 1;
108b9e2be50SGlenn Strauss     }
109b9e2be50SGlenn Strauss 
110b9e2be50SGlenn Strauss     len = strlen(s);
111b9e2be50SGlenn Strauss     if (len > 5
112b9e2be50SGlenn Strauss         && (s[len-5]       ) == '-'
113b9e2be50SGlenn Strauss         && (s[len-4] | 0x20) == 's'
114b9e2be50SGlenn Strauss         && (s[len-3] | 0x20) == 'e'
115b9e2be50SGlenn Strauss         && (s[len-2] | 0x20) == 's'
116b9e2be50SGlenn Strauss         && (s[len-1] | 0x20) == 's') {
117b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_SESS;
118b9e2be50SGlenn Strauss         len -= 5;
119b9e2be50SGlenn Strauss     }
120b9e2be50SGlenn Strauss     else {
121b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_NONE;
122b9e2be50SGlenn Strauss     }
123b9e2be50SGlenn Strauss 
124b9e2be50SGlenn Strauss     if (3 == len
125b9e2be50SGlenn Strauss         && 'm' == (s[0] | 0x20)
126b9e2be50SGlenn Strauss         && 'd' == (s[1] | 0x20)
127b9e2be50SGlenn Strauss         && '5' == (s[2]       )) {
128b9e2be50SGlenn Strauss         ai->dalgo |= HTTP_AUTH_DIGEST_MD5;
129b9e2be50SGlenn Strauss         ai->dlen   = HTTP_AUTH_DIGEST_MD5_BINLEN;
130b9e2be50SGlenn Strauss         return 1;
131b9e2be50SGlenn Strauss     }
132b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
133b9e2be50SGlenn Strauss     else if (len >= 7
134b9e2be50SGlenn Strauss              && 's' == (s[0] | 0x20)
135b9e2be50SGlenn Strauss              && 'h' == (s[1] | 0x20)
136b9e2be50SGlenn Strauss              && 'a' == (s[2] | 0x20)
137b9e2be50SGlenn Strauss              && '-' == (s[3]       )) {
138b9e2be50SGlenn Strauss         if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') {
139b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA256;
140b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA256_BINLEN;
141b9e2be50SGlenn Strauss             return 1;
142b9e2be50SGlenn Strauss         }
143*1fc8a3e1SGlenn Strauss       #ifdef USE_LIB_CRYPTO_SHA512_256
144b9e2be50SGlenn Strauss         if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) {
145b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256;
146b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA512_256_BINLEN;
147b9e2be50SGlenn Strauss             return 1;
148b9e2be50SGlenn Strauss         }
149b9e2be50SGlenn Strauss       #endif
150b9e2be50SGlenn Strauss     }
151b9e2be50SGlenn Strauss   #endif
152b9e2be50SGlenn Strauss     return 0; /*(error)*/
153b9e2be50SGlenn Strauss }
154b9e2be50SGlenn Strauss 
155b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) {
156b9e2be50SGlenn Strauss     for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) {
157b9e2be50SGlenn Strauss         http_auth_info_t ai;
158b9e2be50SGlenn Strauss         int rc;
159b9e2be50SGlenn Strauss         p = strchr(s, '|');
160b9e2be50SGlenn Strauss         if (p) *p = '\0';
161b9e2be50SGlenn Strauss         rc = mod_auth_algorithm_parse(&ai, s);
162b9e2be50SGlenn Strauss         if (p) *p = '|';
163b9e2be50SGlenn Strauss         if (!rc) return 0;
164b9e2be50SGlenn Strauss         *algorithm |= ai.dalgo;
165b9e2be50SGlenn Strauss     }
166b9e2be50SGlenn Strauss     return 1;
167b9e2be50SGlenn Strauss }
168b9e2be50SGlenn Strauss 
169b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh)
1707ba06c71SGlenn Strauss {
1717ba06c71SGlenn Strauss     /* user=name1|user=name2|group=name3|host=name4 */
1727ba06c71SGlenn Strauss 
1737ba06c71SGlenn Strauss     const char *str = b->ptr;
1747ba06c71SGlenn Strauss     const char *p;
1757ba06c71SGlenn Strauss 
1767ba06c71SGlenn Strauss     if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) {
1777ba06c71SGlenn Strauss         require->valid_user = 1;
1787ba06c71SGlenn Strauss         return 1; /* success */
1797ba06c71SGlenn Strauss     }
1807ba06c71SGlenn Strauss 
1817ba06c71SGlenn Strauss     do {
1827ba06c71SGlenn Strauss         const char *eq;
1837ba06c71SGlenn Strauss         size_t len;
1847ba06c71SGlenn Strauss         p = strchr(str, '|');
1857ba06c71SGlenn Strauss         len = NULL != p ? (size_t)(p - str) : strlen(str);
1867ba06c71SGlenn Strauss         eq = memchr(str, '=', len);
1877ba06c71SGlenn Strauss         if (NULL == eq) {
188b5775b99SGlenn Strauss             log_error(errh, __FILE__, __LINE__,
189010c2894SGlenn Strauss               "error parsing auth.require 'require' field: missing '=' "
190010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
191010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
1927ba06c71SGlenn Strauss             return 0;
1937ba06c71SGlenn Strauss         }
194b2c18f48SGlenn Strauss         if (eq[1] == '|' || eq[1] == '\0') {
195b5775b99SGlenn Strauss             log_error(errh, __FILE__, __LINE__,
196010c2894SGlenn Strauss               "error parsing auth.require 'require' field: "
197010c2894SGlenn Strauss               "missing token after '=' "
198010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
199010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
2007ba06c71SGlenn Strauss             return 0;
2017ba06c71SGlenn Strauss         }
2027ba06c71SGlenn Strauss 
2037ba06c71SGlenn Strauss         switch ((int)(eq - str)) {
2047ba06c71SGlenn Strauss           case 4:
2057ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("user"))) {
206810109ccSGlenn Strauss                 /*("user=" is 5)*/
207a03afc90SGlenn Strauss                 array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN(""));
2087ba06c71SGlenn Strauss                 continue;
2097ba06c71SGlenn Strauss             }
2107ba06c71SGlenn Strauss             else if (0 == memcmp(str, CONST_STR_LEN("host"))) {
211810109ccSGlenn Strauss                 /*("host=" is 5)*/
212a03afc90SGlenn Strauss                 array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN(""));
213b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
214010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
215010c2894SGlenn Strauss                   "'host' not implemented; field value: %s", b->ptr);
2167ba06c71SGlenn Strauss                 continue;
2177ba06c71SGlenn Strauss             }
2187ba06c71SGlenn Strauss             break; /* to error */
2197ba06c71SGlenn Strauss           case 5:
2207ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("group"))) {
221810109ccSGlenn Strauss                 /*("group=" is 6)*/
222a03afc90SGlenn Strauss                 array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN(""));
2239c91af0cSGlenn Strauss               #if 0/*(supported by mod_authn_ldap, but not all other backends)*/
224b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
225010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
226010c2894SGlenn Strauss                   "'group' not implemented; field value: %s", b->ptr);
2279c91af0cSGlenn Strauss               #endif
2287ba06c71SGlenn Strauss                 continue;
2297ba06c71SGlenn Strauss             }
2307ba06c71SGlenn Strauss             break; /* to error */
2317ba06c71SGlenn Strauss           case 10:
2327ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) {
233b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
234010c2894SGlenn Strauss                   "error parsing auth.require 'require' field: "
235010c2894SGlenn Strauss                   "valid user can not be combined with other require rules "
236010c2894SGlenn Strauss                   "(expecting \"valid-user\" or "
237010c2894SGlenn Strauss                   "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr);
2387ba06c71SGlenn Strauss                 return 0;
2397ba06c71SGlenn Strauss             }
2407ba06c71SGlenn Strauss             break; /* to error */
2417ba06c71SGlenn Strauss           default:
2427ba06c71SGlenn Strauss             break; /* to error */
2437ba06c71SGlenn Strauss         }
2447ba06c71SGlenn Strauss 
245b5775b99SGlenn Strauss         log_error(errh, __FILE__, __LINE__,
246010c2894SGlenn Strauss           "error parsing auth.require 'require' field: "
247010c2894SGlenn Strauss           "invalid/unsupported token "
248010c2894SGlenn Strauss           "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
249010c2894SGlenn Strauss           "error value: %s error near: %s", b->ptr, str);
2507ba06c71SGlenn Strauss         return 0;
2517ba06c71SGlenn Strauss 
2527ba06c71SGlenn Strauss     } while (p && *((str = p+1)));
2537ba06c71SGlenn Strauss 
2547ba06c71SGlenn Strauss     return 1; /* success */
2557ba06c71SGlenn Strauss }
2567ba06c71SGlenn Strauss 
257b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh)
2588e713130SGlenn Strauss {
2598e713130SGlenn Strauss 		for (uint32_t n = 0; n < value->used; ++n) {
2607ba06c71SGlenn Strauss 			size_t m;
2618e713130SGlenn Strauss 			data_array *da_file = (data_array *)value->data[n];
2627ba06c71SGlenn Strauss 			const buffer *method = NULL, *realm = NULL, *require = NULL;
263e1188e77SGlenn Strauss 			const buffer *nonce_secret = NULL;
2647ba06c71SGlenn Strauss 			const http_auth_scheme_t *auth_scheme;
265b9e2be50SGlenn Strauss 			buffer *algos = NULL;
266b9e2be50SGlenn Strauss 			int algorithm = HTTP_AUTH_DIGEST_SESS;
2677ba06c71SGlenn Strauss 
268c2238256SGlenn Strauss 			if (!array_is_kvstring(&da_file->value)) {
269b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
270010c2894SGlenn Strauss 				  "unexpected value for auth.require; expected "
271bd77abe0SGlenn Strauss 				  "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )");
2727ba06c71SGlenn Strauss 
2737ba06c71SGlenn Strauss 				return HANDLER_ERROR;
2747ba06c71SGlenn Strauss 			}
2757ba06c71SGlenn Strauss 
276c2238256SGlenn Strauss 			for (m = 0; m < da_file->value.used; m++) {
277c2238256SGlenn Strauss 				if (da_file->value.data[m]->type == TYPE_STRING) {
278c2238256SGlenn Strauss 					data_string *ds = (data_string *)da_file->value.data[m];
279ad9b7e00SGlenn Strauss 					if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) {
280601c572cSGlenn Strauss 						method = &ds->value;
281ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) {
282601c572cSGlenn Strauss 						realm = &ds->value;
283ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) {
284601c572cSGlenn Strauss 						require = &ds->value;
285ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) {
286601c572cSGlenn Strauss 						algos = &ds->value;
287e1188e77SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret"))) {
288e1188e77SGlenn Strauss 						nonce_secret = &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;
351e1188e77SGlenn Strauss 				dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/
352b5775b99SGlenn Strauss 				if (!mod_auth_require_parse(dauth->require, require, errh)) {
3538c7f1dfbSGlenn Strauss 					dauth->fn->free((data_unset *)dauth);
3547ba06c71SGlenn Strauss 					return HANDLER_ERROR;
3557ba06c71SGlenn Strauss 				}
3568e713130SGlenn Strauss 				array_insert_unique(auth_require, (data_unset *)dauth);
3577ba06c71SGlenn Strauss 			}
3587ba06c71SGlenn Strauss 		}
3597ba06c71SGlenn Strauss 
3607ba06c71SGlenn Strauss 	return HANDLER_GO_ON;
3617ba06c71SGlenn Strauss }
3627ba06c71SGlenn Strauss 
3638e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
3648e713130SGlenn Strauss     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
3658e713130SGlenn Strauss       case 0: /* auth.backend */
3668e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
3678e713130SGlenn Strauss             pconf->auth_backend = cpv->v.v;
3688e713130SGlenn Strauss         break;
3698e713130SGlenn Strauss       case 1: /* auth.require */
3708e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
3718e713130SGlenn Strauss             pconf->auth_require = cpv->v.v;
3728e713130SGlenn Strauss         break;
3738e713130SGlenn Strauss       case 2: /* auth.extern-authn */
3748e713130SGlenn Strauss         pconf->auth_extern_authn = cpv->v.u;
3758e713130SGlenn Strauss       default:/* should not happen */
3768e713130SGlenn Strauss         return;
3778e713130SGlenn Strauss     }
3788e713130SGlenn Strauss }
3794a6ce4aeSJan Kneschke 
3808e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
3818e713130SGlenn Strauss     do {
3828e713130SGlenn Strauss         mod_auth_merge_config_cpv(pconf, cpv);
3838e713130SGlenn Strauss     } while ((++cpv)->k_id != -1);
3848e713130SGlenn Strauss }
385bcdc6a3bSJan Kneschke 
3867c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) {
387cc2134c8SGlenn Strauss     p->conf = p->defaults; /* copy small struct instead of memcpy() */
388cc2134c8SGlenn Strauss     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
3898e713130SGlenn Strauss     for (int i = 1, used = p->nconfig; i < used; ++i) {
3907c7f8c46SGlenn Strauss         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
3918e713130SGlenn Strauss             mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
3928e713130SGlenn Strauss     }
3938e713130SGlenn Strauss }
39436f64b26SGlenn Strauss 
3958e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) {
3968e713130SGlenn Strauss     static const config_plugin_keys_t cpk[] = {
3978e713130SGlenn Strauss       { CONST_STR_LEN("auth.backend"),
3988e713130SGlenn Strauss         T_CONFIG_STRING,
3998e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
4008e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.require"),
40103b4c993SGlenn Strauss         T_CONFIG_ARRAY_KVARRAY,
4028e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
4038e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.extern-authn"),
4048e713130SGlenn Strauss         T_CONFIG_BOOL,
4058e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
4068e713130SGlenn Strauss      ,{ NULL, 0,
4078e713130SGlenn Strauss         T_CONFIG_UNSET,
4088e713130SGlenn Strauss         T_CONFIG_SCOPE_UNSET }
4098e713130SGlenn Strauss     };
410bcdc6a3bSJan Kneschke 
4118e713130SGlenn Strauss     plugin_data * const p = p_d;
4128e713130SGlenn Strauss     if (!config_plugin_values_init(srv, p, cpk, "mod_auth"))
4138e713130SGlenn Strauss         return HANDLER_ERROR;
414bcdc6a3bSJan Kneschke 
4158e713130SGlenn Strauss     /* process and validate config directives
4168e713130SGlenn Strauss      * (init i to 0 if global context; to 1 to skip empty global context) */
4178e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
4188e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
4198e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
4208e713130SGlenn Strauss             switch (cpv->k_id) {
4218e713130SGlenn Strauss               case 0: /* auth.backend */
4228e713130SGlenn Strauss                 if (!buffer_string_is_empty(cpv->v.b)) {
4238e713130SGlenn Strauss                     const http_auth_backend_t * const auth_backend =
4248e713130SGlenn Strauss                       http_auth_backend_get(cpv->v.b);
4258e713130SGlenn Strauss                     if (NULL == auth_backend) {
4268e713130SGlenn Strauss                         log_error(srv->errh, __FILE__, __LINE__,
4278e713130SGlenn Strauss                           "auth.backend not supported: %s", cpv->v.b->ptr);
4288e713130SGlenn Strauss                         return HANDLER_ERROR;
4298e713130SGlenn Strauss                     }
4308e713130SGlenn Strauss                     *(const http_auth_backend_t **)&cpv->v.v = auth_backend;
4318e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
4328e713130SGlenn Strauss                 }
4338e713130SGlenn Strauss                 break;
4348e713130SGlenn Strauss               case 1: /* auth.require */
43503b4c993SGlenn Strauss                 {
43624680a91SGlenn Strauss                     array * const a = array_init(4);
4378e713130SGlenn Strauss                     if (HANDLER_GO_ON !=
438b5775b99SGlenn Strauss                         mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) {
4398e713130SGlenn Strauss                         array_free(a);
4408e713130SGlenn Strauss                         return HANDLER_ERROR;
4418e713130SGlenn Strauss                     }
4428e713130SGlenn Strauss                     cpv->v.a = a;
4438e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
4448e713130SGlenn Strauss                 }
4458e713130SGlenn Strauss                 break;
4468e713130SGlenn Strauss               case 2: /* auth.extern-authn */
4478e713130SGlenn Strauss                 break;
4488e713130SGlenn Strauss               default:/* should not happen */
4498e713130SGlenn Strauss                 break;
450bcdc6a3bSJan Kneschke             }
451bcdc6a3bSJan Kneschke         }
452bcdc6a3bSJan Kneschke     }
453bcdc6a3bSJan Kneschke 
4548e713130SGlenn Strauss     /* initialize p->defaults from global config context */
4558e713130SGlenn Strauss     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
4568e713130SGlenn Strauss         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
4578e713130SGlenn Strauss         if (-1 != cpv->k_id)
4588e713130SGlenn Strauss             mod_auth_merge_config(&p->defaults, cpv);
459bcdc6a3bSJan Kneschke     }
4608e713130SGlenn Strauss 
4618e713130SGlenn Strauss     return HANDLER_GO_ON;
4628e713130SGlenn Strauss }
463bcdc6a3bSJan Kneschke 
4647c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) {
4657ba06c71SGlenn Strauss 	plugin_data *p = p_d;
466d61f3381SGlenn Strauss 	data_auth *dauth;
46781b2d1f0SGlenn Strauss 
4687c7f8c46SGlenn Strauss 	mod_auth_patch_config(r, p);
46981b2d1f0SGlenn Strauss 
4707ba06c71SGlenn Strauss 	if (p->conf.auth_require == NULL) return HANDLER_GO_ON;
47181b2d1f0SGlenn Strauss 
4727ba06c71SGlenn Strauss 	/* search auth directives for first prefix match against URL path */
4737ba06c71SGlenn Strauss 	/* if we have a case-insensitive FS we have to lower-case the URI here too */
4747c7f8c46SGlenn Strauss 	dauth = (!r->conf.force_lowercase_filenames)
4757c7f8c46SGlenn Strauss 	   ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path)
4767c7f8c46SGlenn Strauss 	   : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path);
477d61f3381SGlenn Strauss 	if (NULL == dauth) return HANDLER_GO_ON;
4787ba06c71SGlenn Strauss 
479d61f3381SGlenn Strauss 	{
4807ba06c71SGlenn Strauss 			const http_auth_scheme_t * const scheme = dauth->require->scheme;
481f54d628cSGlenn Strauss 			if (p->conf.auth_extern_authn) {
4827c7f8c46SGlenn Strauss 				const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
4833dd3cde9SGlenn Strauss 				if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) {
484f54d628cSGlenn Strauss 					return HANDLER_GO_ON;
485f54d628cSGlenn Strauss 				}
486f54d628cSGlenn Strauss 			}
4877c7f8c46SGlenn Strauss 			return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend);
4887ba06c71SGlenn Strauss 	}
4897ba06c71SGlenn Strauss }
4907ba06c71SGlenn Strauss 
4917ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p);
4927ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) {
4937ba06c71SGlenn Strauss 	p->version     = LIGHTTPD_VERSION_ID;
494e2de4e58SGlenn Strauss 	p->name        = "auth";
4957ba06c71SGlenn Strauss 	p->init        = mod_auth_init;
4967ba06c71SGlenn Strauss 	p->set_defaults = mod_auth_set_defaults;
4977ba06c71SGlenn Strauss 	p->handle_uri_clean = mod_auth_uri_handler;
4987ba06c71SGlenn Strauss 	p->cleanup     = mod_auth_free;
4997ba06c71SGlenn Strauss 
50081b2d1f0SGlenn Strauss 	return 0;
50181b2d1f0SGlenn Strauss }
50281b2d1f0SGlenn Strauss 
50381b2d1f0SGlenn Strauss 
50481b2d1f0SGlenn Strauss 
50581b2d1f0SGlenn Strauss 
5067ba06c71SGlenn Strauss /*
5077ba06c71SGlenn Strauss  * auth schemes (basic, digest, extern)
5087ba06c71SGlenn Strauss  *
5097ba06c71SGlenn Strauss  * (could be in separate file from mod_auth.c as long as registration occurs)
5107ba06c71SGlenn Strauss  */
51181b2d1f0SGlenn Strauss 
512*1fc8a3e1SGlenn Strauss #include "sys-crypto-md.h"
5137ba06c71SGlenn Strauss #include "base64.h"
5147f4e156eSGlenn Strauss #include "rand.h"
5153dd3cde9SGlenn Strauss #include "http_header.h"
51681b2d1f0SGlenn Strauss 
517010c2894SGlenn Strauss __attribute_cold__
5187c7f8c46SGlenn Strauss static handler_t mod_auth_send_400_bad_request(request_st * const r) {
5194b3a91e6SGlenn Strauss 
5204b3a91e6SGlenn Strauss 	/* a field was missing or invalid */
5217c7f8c46SGlenn Strauss 	r->http_status = 400; /* Bad Request */
5227c7f8c46SGlenn Strauss 	r->handler_module = NULL;
5234b3a91e6SGlenn Strauss 
5244b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
5254b3a91e6SGlenn Strauss }
5264b3a91e6SGlenn Strauss 
5277c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(request_st * const r, const buffer * const realm) {
5287c7f8c46SGlenn Strauss 	r->http_status = 401;
5297c7f8c46SGlenn Strauss 	r->handler_module = NULL;
5304b3a91e6SGlenn Strauss 
5317c7f8c46SGlenn Strauss 	buffer * const tb = r->tmp_buf;
532010c2894SGlenn Strauss 	buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\""));
533010c2894SGlenn Strauss 	buffer_append_string_buffer(tb, realm);
534010c2894SGlenn Strauss 	buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\""));
5354b3a91e6SGlenn Strauss 
5367c7f8c46SGlenn Strauss 	http_header_response_set(r, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb));
5374b3a91e6SGlenn Strauss 
5384b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
5394b3a91e6SGlenn Strauss }
5404b3a91e6SGlenn Strauss 
5417c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
5427c7f8c46SGlenn Strauss 	const buffer *b = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
54381b2d1f0SGlenn Strauss 	buffer *username;
54481b2d1f0SGlenn Strauss 	char *pw;
5457ba06c71SGlenn Strauss 	handler_t rc = HANDLER_UNSET;
54681b2d1f0SGlenn Strauss 
5477ba06c71SGlenn Strauss 	UNUSED(p_d);
54881b2d1f0SGlenn Strauss 
5497ba06c71SGlenn Strauss 	if (NULL == backend) {
5507c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__, "auth.backend not configured for %s", r->uri.path.ptr);
5517c7f8c46SGlenn Strauss 		r->http_status = 500;
5527c7f8c46SGlenn Strauss 		r->handler_module = NULL;
5537ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
5547ba06c71SGlenn Strauss 	}
5557ba06c71SGlenn Strauss 
5563dd3cde9SGlenn Strauss 	if (NULL == b) {
5577c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(r, require->realm);
5587ba06c71SGlenn Strauss 	}
5597ba06c71SGlenn Strauss 
56013008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) {
5617c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(r, require->realm);
5624b3a91e6SGlenn Strauss 	}
5639bc61f16SGlenn Strauss       #ifdef __COVERITY__
5643dd3cde9SGlenn Strauss 	if (buffer_string_length(b) < sizeof("Basic ")-1) {
5657c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
5669bc61f16SGlenn Strauss 	}
5679bc61f16SGlenn Strauss       #endif
56881b2d1f0SGlenn Strauss 
56981b2d1f0SGlenn Strauss 	username = buffer_init();
57081b2d1f0SGlenn Strauss 
5718047c2f4SGlenn Strauss 	/* coverity[overflow_sink : FALSE] */
5727ba06c71SGlenn Strauss 	if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) {
5737c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1);
57481b2d1f0SGlenn Strauss 
57581b2d1f0SGlenn Strauss 		buffer_free(username);
5767c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
57781b2d1f0SGlenn Strauss 	}
57881b2d1f0SGlenn Strauss 
57981b2d1f0SGlenn Strauss 	/* r2 == user:password */
58081b2d1f0SGlenn Strauss 	if (NULL == (pw = strchr(username->ptr, ':'))) {
5817c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr);
58281b2d1f0SGlenn Strauss 
58381b2d1f0SGlenn Strauss 		buffer_free(username);
5847c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
58581b2d1f0SGlenn Strauss 	}
58681b2d1f0SGlenn Strauss 
58781b2d1f0SGlenn Strauss 	buffer_string_set_length(username, pw - username->ptr);
58881b2d1f0SGlenn Strauss 	pw++;
58981b2d1f0SGlenn Strauss 
5907c7f8c46SGlenn Strauss 	rc = backend->basic(r, backend->p_d, require, username, pw);
5917ba06c71SGlenn Strauss 	switch (rc) {
5924b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
5937c7f8c46SGlenn Strauss 		http_auth_setenv(r, CONST_BUF_LEN(username), CONST_STR_LEN("Basic"));
5944b3a91e6SGlenn Strauss 		break;
5954b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
5964b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
5977ba06c71SGlenn Strauss 		break;
5984b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
5994b3a91e6SGlenn Strauss 	default:
6007c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
601010c2894SGlenn Strauss 		  "password doesn't match for %s username: %s IP: %s",
6027c7f8c46SGlenn Strauss 		  r->uri.path.ptr, username->ptr, r->con->dst_addr_buf->ptr);
6037c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if bad password)*/
6047ba06c71SGlenn Strauss 		rc = HANDLER_UNSET;
6057ba06c71SGlenn Strauss 		break;
60681b2d1f0SGlenn Strauss 	}
60781b2d1f0SGlenn Strauss 
6084b3a91e6SGlenn Strauss 	buffer_free(username);
6097c7f8c46SGlenn Strauss 	return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(r, require->realm);
61081b2d1f0SGlenn Strauss }
61181b2d1f0SGlenn Strauss 
61281b2d1f0SGlenn Strauss 
613b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
614b9e2be50SGlenn Strauss 
615b9e2be50SGlenn 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) {
616b9e2be50SGlenn Strauss     SHA256_CTX ctx;
617b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
618b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
619b9e2be50SGlenn Strauss 
620b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
621b9e2be50SGlenn Strauss         SHA256_Init(&ctx);
622b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
623b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
624b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
625b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
626b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
627b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
628b9e2be50SGlenn Strauss         SHA256_Final(ai->digest, &ctx);
629b9e2be50SGlenn Strauss     }
630b9e2be50SGlenn Strauss 
631b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
632b9e2be50SGlenn Strauss 
633b9e2be50SGlenn Strauss     /* calculate H(A2) */
634b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
635b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)m, strlen(m));
636b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
637b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri));
638b9e2be50SGlenn Strauss   #if 0
639b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
640fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
641b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
642b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
643b9e2be50SGlenn Strauss     }
644b9e2be50SGlenn Strauss   #endif
645b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
646b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
647b9e2be50SGlenn Strauss 
648b9e2be50SGlenn Strauss     /* calculate response */
649b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
650b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
651b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
652b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
653b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
654b9e2be50SGlenn Strauss     if (qop && *qop) {
655b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc));
656b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
657b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
658b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
659b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop));
660b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
661b9e2be50SGlenn Strauss     }
662b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
663b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
664b9e2be50SGlenn Strauss }
665b9e2be50SGlenn Strauss 
666e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
667b9e2be50SGlenn Strauss     SHA256_CTX ctx;
668f24e6d69SGlenn Strauss     size_t len;
669b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN];
670b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
671b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
672b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
673f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
674f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
675f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
676f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
677e1188e77SGlenn Strauss     if (secret) {
678e1188e77SGlenn Strauss         len = buffer_string_length(secret);
679e1188e77SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)secret->ptr, len);
680e1188e77SGlenn Strauss     }
681b9e2be50SGlenn Strauss     SHA256_Final(h, &ctx);
682b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
683b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
684b9e2be50SGlenn Strauss }
685b9e2be50SGlenn Strauss 
686*1fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
687b9e2be50SGlenn Strauss 
688b9e2be50SGlenn 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) {
689b9e2be50SGlenn Strauss     SHA512_CTX ctx;
690b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
691b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
692b9e2be50SGlenn Strauss 
693b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
694b9e2be50SGlenn Strauss         SHA512_256_Init(&ctx);
695b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
696b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
697b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
698b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
699b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
700b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
701b9e2be50SGlenn Strauss         SHA512_256_Final(ai->digest, &ctx);
702b9e2be50SGlenn Strauss     }
703b9e2be50SGlenn Strauss 
704b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
705b9e2be50SGlenn Strauss 
706b9e2be50SGlenn Strauss     /* calculate H(A2) */
707b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
708b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m));
709b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
710b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri));
711b9e2be50SGlenn Strauss   #if 0
712b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
713fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
714b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
715b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2);
716b9e2be50SGlenn Strauss     }
717b9e2be50SGlenn Strauss   #endif
718b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
719b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
720b9e2be50SGlenn Strauss 
721b9e2be50SGlenn Strauss     /* calculate response */
722b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
723b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
724b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
725b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
726b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
727b9e2be50SGlenn Strauss     if (qop && *qop) {
728b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc));
729b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
730b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
731b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
732b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop));
733b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
734b9e2be50SGlenn Strauss     }
735b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
736b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
737b9e2be50SGlenn Strauss }
738b9e2be50SGlenn Strauss 
739e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
740b9e2be50SGlenn Strauss     SHA512_CTX ctx;
741f24e6d69SGlenn Strauss     size_t len;
742b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN];
743b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
744b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
745b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
746f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
747f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
748f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
749f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
750e1188e77SGlenn Strauss     if (secret) {
751e1188e77SGlenn Strauss         len = buffer_string_length(secret);
752e1188e77SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)secret->ptr, len);
753e1188e77SGlenn Strauss     }
754b9e2be50SGlenn Strauss     SHA512_256_Final(h, &ctx);
755b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
756b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
757b9e2be50SGlenn Strauss }
758b9e2be50SGlenn Strauss 
759*1fc8a3e1SGlenn Strauss #endif /* USE_LIB_CRYPTO_SHA512_256 */
760b9e2be50SGlenn Strauss 
761b28a3714SGlenn Strauss #endif /* USE_LIB_CRYPTO */
762b9e2be50SGlenn Strauss 
763b9e2be50SGlenn 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) {
764b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
765b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
766b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
767b9e2be50SGlenn Strauss 
768b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
769b9e2be50SGlenn Strauss         li_MD5_Init(&ctx);
770b9e2be50SGlenn Strauss         /* http://www.rfc-editor.org/errata_search.php?rfc=2617
771b9e2be50SGlenn Strauss          * Errata ID: 1649 */
772b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
773b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
774b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
775b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
776b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
777b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
778b9e2be50SGlenn Strauss         li_MD5_Final(ai->digest, &ctx);
779b9e2be50SGlenn Strauss     }
780b9e2be50SGlenn Strauss 
781b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
782b9e2be50SGlenn Strauss 
783b9e2be50SGlenn Strauss     /* calculate H(A2) */
784b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
785b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)m, strlen(m));
786b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
787b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri));
788b9e2be50SGlenn Strauss   #if 0
789b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
790fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
791b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
792b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
793b9e2be50SGlenn Strauss     }
794b9e2be50SGlenn Strauss   #endif
795b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
796b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
797b9e2be50SGlenn Strauss 
798b9e2be50SGlenn Strauss     /* calculate response */
799b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
800b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
801b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
802b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
803b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
804b9e2be50SGlenn Strauss     if (qop && *qop) {
805b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc));
806b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
807b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
808b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
809b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop));
810b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
811b9e2be50SGlenn Strauss     }
812b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
813b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
814b9e2be50SGlenn Strauss }
815b9e2be50SGlenn Strauss 
816e1188e77SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
817b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
818f24e6d69SGlenn Strauss     size_t len;
819b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN];
820b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
821b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
822b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
823f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
824f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
825f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
826f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
827e1188e77SGlenn Strauss     if (secret) {
828e1188e77SGlenn Strauss         len = buffer_string_length(secret);
829e1188e77SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)secret->ptr, len);
830e1188e77SGlenn Strauss     }
831b9e2be50SGlenn Strauss     li_MD5_Final(h, &ctx);
832b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
833b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
834b9e2be50SGlenn Strauss }
835b9e2be50SGlenn Strauss 
836b9e2be50SGlenn 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) {
837b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_MD5)
838b9e2be50SGlenn Strauss         mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop);
839b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
840b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256)
841b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop);
842*1fc8a3e1SGlenn Strauss    #ifdef USE_LIB_CRYPTO_SHA512_256
843b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256)
844b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop);
845b9e2be50SGlenn Strauss    #endif
846b9e2be50SGlenn Strauss   #endif
847b9e2be50SGlenn Strauss }
848b9e2be50SGlenn Strauss 
849e1188e77SGlenn Strauss static void mod_auth_append_nonce(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) {
850e1188e77SGlenn Strauss     buffer_append_uint_hex(b, (uintmax_t)cur_ts);
851e1188e77SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN(":"));
852e1188e77SGlenn Strauss     const buffer * const nonce_secret = require->nonce_secret;
853e1188e77SGlenn Strauss     int rnd;
854e1188e77SGlenn Strauss     if (NULL == nonce_secret)
855e1188e77SGlenn Strauss         rnd = rndptr ? *rndptr : li_rand_pseudo();
856e1188e77SGlenn Strauss     else { /*(do not directly expose random number generator single value)*/
857e1188e77SGlenn Strauss         rndptr
858e1188e77SGlenn Strauss           ? (void)(rnd = *rndptr)
859e1188e77SGlenn Strauss           : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd));
860e1188e77SGlenn Strauss         buffer_append_uint_hex(b, (uintmax_t)rnd);
861e1188e77SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(":"));
862e1188e77SGlenn Strauss     }
863e1188e77SGlenn Strauss     switch (dalgo) {
864b28a3714SGlenn Strauss      #ifdef USE_LIB_CRYPTO
865*1fc8a3e1SGlenn Strauss       #ifdef USE_LIB_CRYPTO_SHA512_256
866e1188e77SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA512_256:
867e1188e77SGlenn Strauss         mod_auth_digest_nonce_sha512_256(b, cur_ts, rnd, nonce_secret);
868e1188e77SGlenn Strauss         break;
869e1188e77SGlenn Strauss       #endif
870e1188e77SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA256:
871e1188e77SGlenn Strauss         mod_auth_digest_nonce_sha256(b, cur_ts, rnd, nonce_secret);
872e1188e77SGlenn Strauss         break;
873e1188e77SGlenn Strauss      #endif
874e1188e77SGlenn Strauss       /*case HTTP_AUTH_DIGEST_MD5:*/
875e1188e77SGlenn Strauss       default:
876e1188e77SGlenn Strauss         mod_auth_digest_nonce_md5(b, cur_ts, rnd, nonce_secret);
877e1188e77SGlenn Strauss         break;
878e1188e77SGlenn Strauss     }
879e1188e77SGlenn Strauss }
880e1188e77SGlenn Strauss 
881b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) {
882b9e2be50SGlenn Strauss     int algos = nonce_stale ? nonce_stale : require->algorithm;
883b9e2be50SGlenn Strauss     int n = 0;
884e1188e77SGlenn Strauss     int algoid[3];
885b9e2be50SGlenn Strauss     unsigned int algolen[3];
886b9e2be50SGlenn Strauss     const char *algoname[3];
887b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
888*1fc8a3e1SGlenn Strauss    #ifdef USE_LIB_CRYPTO_SHA512_256
889b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA512_256) {
890e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_SHA512_256;
891b9e2be50SGlenn Strauss         algoname[n] = "SHA-512-256";
892b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-512-256")-1;
893b9e2be50SGlenn Strauss         ++n;
894b9e2be50SGlenn Strauss     }
895b9e2be50SGlenn Strauss    #endif
896b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA256) {
897e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_SHA256;
898b9e2be50SGlenn Strauss         algoname[n] = "SHA-256";
899b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-256")-1;
900b9e2be50SGlenn Strauss         ++n;
901b9e2be50SGlenn Strauss     }
902b9e2be50SGlenn Strauss   #endif
903b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_MD5) {
904e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_MD5;
905b9e2be50SGlenn Strauss         algoname[n] = "MD5";
906b9e2be50SGlenn Strauss         algolen[n] = sizeof("MD5")-1;
907b9e2be50SGlenn Strauss         ++n;
908b9e2be50SGlenn Strauss     }
909b9e2be50SGlenn Strauss 
910b9e2be50SGlenn Strauss     buffer_clear(b);
911b9e2be50SGlenn Strauss     for (int i = 0; i < n; ++i) {
912b9e2be50SGlenn Strauss         if (i > 0) {
913b9e2be50SGlenn Strauss             buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: "));
914b9e2be50SGlenn Strauss         }
915b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\""));
916b9e2be50SGlenn Strauss         buffer_append_string_buffer(b, require->realm);
9171799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm="));
918b9e2be50SGlenn Strauss         buffer_append_string_len(b, algoname[i], algolen[i]);
9191799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(", nonce=\""));
920e1188e77SGlenn Strauss         mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL);
921b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\""));
922b9e2be50SGlenn Strauss         if (nonce_stale) {
923b9e2be50SGlenn Strauss             buffer_append_string_len(b, CONST_STR_LEN(", stale=true"));
924b9e2be50SGlenn Strauss         }
925b9e2be50SGlenn Strauss     }
92681b2d1f0SGlenn Strauss }
92781b2d1f0SGlenn Strauss 
928e1188e77SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo) {
9296ad325c6SGlenn Strauss     buffer_clear(b);
9306ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\""));
931e1188e77SGlenn Strauss     mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL);
9326ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("\""));
9336ad325c6SGlenn Strauss }
9346ad325c6SGlenn Strauss 
93581b2d1f0SGlenn Strauss typedef struct {
93681b2d1f0SGlenn Strauss 	const char *key;
93781b2d1f0SGlenn Strauss 	int key_len;
93881b2d1f0SGlenn Strauss 	char **ptr;
93981b2d1f0SGlenn Strauss } digest_kv;
94081b2d1f0SGlenn Strauss 
9417c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st *r, const struct http_auth_require_t *require, int nonce_stale);
9427ba06c71SGlenn Strauss 
9437c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
9447c7f8c46SGlenn Strauss 	const buffer *vb = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
9457ba06c71SGlenn Strauss 
94681b2d1f0SGlenn Strauss 	char *username = NULL;
94781b2d1f0SGlenn Strauss 	char *realm = NULL;
94881b2d1f0SGlenn Strauss 	char *nonce = NULL;
94981b2d1f0SGlenn Strauss 	char *uri = NULL;
95081b2d1f0SGlenn Strauss 	char *algorithm = NULL;
95181b2d1f0SGlenn Strauss 	char *qop = NULL;
95281b2d1f0SGlenn Strauss 	char *cnonce = NULL;
95381b2d1f0SGlenn Strauss 	char *nc = NULL;
95481b2d1f0SGlenn Strauss 	char *respons = NULL;
95581b2d1f0SGlenn Strauss 
95681b2d1f0SGlenn Strauss 	char *e, *c;
95781b2d1f0SGlenn Strauss 	int i;
95881b2d1f0SGlenn Strauss 	buffer *b;
95960f4cf3aSGlenn Strauss 	http_auth_info_t ai;
960b9e2be50SGlenn Strauss 	unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN];
96181b2d1f0SGlenn Strauss 
96281b2d1f0SGlenn Strauss 
96381b2d1f0SGlenn Strauss 	/* init pointers */
96481b2d1f0SGlenn Strauss #define S(x) \
96581b2d1f0SGlenn Strauss 	x, sizeof(x)-1, NULL
96681b2d1f0SGlenn Strauss 	digest_kv dkv[10] = {
96781b2d1f0SGlenn Strauss 		{ S("username=") },
96881b2d1f0SGlenn Strauss 		{ S("realm=") },
96981b2d1f0SGlenn Strauss 		{ S("nonce=") },
97081b2d1f0SGlenn Strauss 		{ S("uri=") },
97181b2d1f0SGlenn Strauss 		{ S("algorithm=") },
97281b2d1f0SGlenn Strauss 		{ S("qop=") },
97381b2d1f0SGlenn Strauss 		{ S("cnonce=") },
97481b2d1f0SGlenn Strauss 		{ S("nc=") },
97581b2d1f0SGlenn Strauss 		{ S("response=") },
97681b2d1f0SGlenn Strauss 
97781b2d1f0SGlenn Strauss 		{ NULL, 0, NULL }
97881b2d1f0SGlenn Strauss 	};
97981b2d1f0SGlenn Strauss #undef S
98081b2d1f0SGlenn Strauss 
98181b2d1f0SGlenn Strauss 	dkv[0].ptr = &username;
98281b2d1f0SGlenn Strauss 	dkv[1].ptr = &realm;
98381b2d1f0SGlenn Strauss 	dkv[2].ptr = &nonce;
98481b2d1f0SGlenn Strauss 	dkv[3].ptr = &uri;
98581b2d1f0SGlenn Strauss 	dkv[4].ptr = &algorithm;
98681b2d1f0SGlenn Strauss 	dkv[5].ptr = &qop;
98781b2d1f0SGlenn Strauss 	dkv[6].ptr = &cnonce;
98881b2d1f0SGlenn Strauss 	dkv[7].ptr = &nc;
98981b2d1f0SGlenn Strauss 	dkv[8].ptr = &respons;
99081b2d1f0SGlenn Strauss 
9917ba06c71SGlenn Strauss 	UNUSED(p_d);
9927ba06c71SGlenn Strauss 
9937ba06c71SGlenn Strauss 	if (NULL == backend) {
9947c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
9957c7f8c46SGlenn Strauss 		  "auth.backend not configured for %s", r->uri.path.ptr);
9967c7f8c46SGlenn Strauss 		r->http_status = 500;
9977c7f8c46SGlenn Strauss 		r->handler_module = NULL;
9987ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
9997ba06c71SGlenn Strauss 	}
10007ba06c71SGlenn Strauss 
10013dd3cde9SGlenn Strauss 	if (NULL == vb) {
10027c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
10037ba06c71SGlenn Strauss 	}
10047ba06c71SGlenn Strauss 
100513008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) {
10067c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
1007dadfb5fcSGlenn Strauss 	} else {
10083dd3cde9SGlenn Strauss 		size_t n = buffer_string_length(vb);
10099bc61f16SGlenn Strauss 	      #ifdef __COVERITY__
1010dadfb5fcSGlenn Strauss 		if (n < sizeof("Digest ")-1) {
10117c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
10129bc61f16SGlenn Strauss 		}
10139bc61f16SGlenn Strauss 	      #endif
1014dadfb5fcSGlenn Strauss 		n -= (sizeof("Digest ")-1);
10157ba06c71SGlenn Strauss 		b = buffer_init();
10163dd3cde9SGlenn Strauss 		buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n);
1017dadfb5fcSGlenn Strauss 	}
101881b2d1f0SGlenn Strauss 
101981b2d1f0SGlenn Strauss 	/* parse credentials from client */
102081b2d1f0SGlenn Strauss 	for (c = b->ptr; *c; c++) {
102181b2d1f0SGlenn Strauss 		/* skip whitespaces */
102281b2d1f0SGlenn Strauss 		while (*c == ' ' || *c == '\t') c++;
102381b2d1f0SGlenn Strauss 		if (!*c) break;
102481b2d1f0SGlenn Strauss 
102581b2d1f0SGlenn Strauss 		for (i = 0; dkv[i].key; i++) {
102681b2d1f0SGlenn Strauss 			if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) {
102781b2d1f0SGlenn Strauss 				if ((c[dkv[i].key_len] == '"') &&
102881b2d1f0SGlenn Strauss 				    (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) {
102981b2d1f0SGlenn Strauss 					/* value with "..." */
103081b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len + 1;
103181b2d1f0SGlenn Strauss 					c = e;
103281b2d1f0SGlenn Strauss 
103381b2d1f0SGlenn Strauss 					*e = '\0';
103481b2d1f0SGlenn Strauss 				} else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) {
103581b2d1f0SGlenn Strauss 					/* value without "...", terminated by ',' */
103681b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
103781b2d1f0SGlenn Strauss 					c = e;
103881b2d1f0SGlenn Strauss 
103981b2d1f0SGlenn Strauss 					*e = '\0';
104081b2d1f0SGlenn Strauss 				} else {
104181b2d1f0SGlenn Strauss 					/* value without "...", terminated by EOL */
104281b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
104381b2d1f0SGlenn Strauss 					c += strlen(c) - 1;
104481b2d1f0SGlenn Strauss 				}
104581b2d1f0SGlenn Strauss 				break;
104681b2d1f0SGlenn Strauss 			}
104781b2d1f0SGlenn Strauss 		}
104881b2d1f0SGlenn Strauss 	}
104981b2d1f0SGlenn Strauss 
105081b2d1f0SGlenn Strauss 	/* check if everything is transmitted */
105181b2d1f0SGlenn Strauss 	if (!username ||
105281b2d1f0SGlenn Strauss 	    !realm ||
105381b2d1f0SGlenn Strauss 	    !nonce ||
105481b2d1f0SGlenn Strauss 	    !uri ||
105581b2d1f0SGlenn Strauss 	    (qop && (!nc || !cnonce)) ||
105681b2d1f0SGlenn Strauss 	    !respons ) {
105781b2d1f0SGlenn Strauss 		/* missing field */
105881b2d1f0SGlenn Strauss 
10597c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
106081b2d1f0SGlenn Strauss 		  "digest: missing field");
106181b2d1f0SGlenn Strauss 
106281b2d1f0SGlenn Strauss 		buffer_free(b);
10637c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
106481b2d1f0SGlenn Strauss 	}
106581b2d1f0SGlenn Strauss 
106660f4cf3aSGlenn Strauss 	ai.username = username;
106760f4cf3aSGlenn Strauss 	ai.ulen     = strlen(username);
106860f4cf3aSGlenn Strauss 	ai.realm    = realm;
106960f4cf3aSGlenn Strauss 	ai.rlen     = strlen(realm);
107060f4cf3aSGlenn Strauss 
107160f4cf3aSGlenn Strauss 	if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) {
10727c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
10735a6e4df8SGlenn Strauss 		  "digest: realm mismatch");
10745a6e4df8SGlenn Strauss 		buffer_free(b);
10757c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
10765a6e4df8SGlenn Strauss 	}
10775a6e4df8SGlenn Strauss 
1078b9e2be50SGlenn Strauss 	if (!mod_auth_algorithm_parse(&ai, algorithm)
1079b9e2be50SGlenn Strauss 	    || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) {
10807c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1081010c2894SGlenn Strauss 		  "digest: (%s): invalid", algorithm);
1082b9e2be50SGlenn Strauss 		buffer_free(b);
10837c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
1084b9e2be50SGlenn Strauss 	}
1085b9e2be50SGlenn Strauss 
108681b2d1f0SGlenn Strauss 	/**
108781b2d1f0SGlenn Strauss 	 * protect the md5-sess against missing cnonce and nonce
108881b2d1f0SGlenn Strauss 	 */
1089b9e2be50SGlenn Strauss 	if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) {
10907c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1091010c2894SGlenn Strauss 		  "digest: (%s): missing field", algorithm);
109281b2d1f0SGlenn Strauss 
109381b2d1f0SGlenn Strauss 		buffer_free(b);
10947c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
109581b2d1f0SGlenn Strauss 	}
109681b2d1f0SGlenn Strauss 
1097b9e2be50SGlenn Strauss 	{
1098b9e2be50SGlenn Strauss 		size_t resplen = strlen(respons);
1099b9e2be50SGlenn Strauss 		if (0 != http_auth_digest_hex2bin(respons, resplen,
1100b9e2be50SGlenn Strauss 						  rdigest, sizeof(rdigest))
1101b9e2be50SGlenn Strauss 		    || resplen != (ai.dlen << 1)) {
11027c7f8c46SGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
1103010c2894SGlenn Strauss 			  "digest: (%s): invalid format", respons);
1104b9e2be50SGlenn Strauss 			buffer_free(b);
11057c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
1106b9e2be50SGlenn Strauss 		}
1107b9e2be50SGlenn Strauss 	}
1108b9e2be50SGlenn Strauss 
1109fc42ec12SGlenn Strauss 	if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
11107c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
111181b2d1f0SGlenn Strauss 		  "digest: qop=auth-int not supported");
111281b2d1f0SGlenn Strauss 
111381b2d1f0SGlenn Strauss 		buffer_free(b);
11147c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
111581b2d1f0SGlenn Strauss 	}
111681b2d1f0SGlenn Strauss 
111781b2d1f0SGlenn Strauss 	/* detect if attacker is attempting to reuse valid digest for one uri
111881b2d1f0SGlenn Strauss 	 * on a different request uri.  Might also happen if intermediate proxy
111981b2d1f0SGlenn Strauss 	 * altered client request line.  (Altered request would not result in
112081b2d1f0SGlenn Strauss 	 * the same digest as that calculated by the client.)
112181b2d1f0SGlenn Strauss 	 * Internal redirects such as with mod_rewrite will modify request uri.
112281b2d1f0SGlenn Strauss 	 * Reauthentication is done to detect crossing auth realms, but this
11237c7f8c46SGlenn Strauss 	 * uri validation step is bypassed.  r->target_orig is
11248131e439SGlenn Strauss 	 * original request-target sent in client request. */
112581b2d1f0SGlenn Strauss 	{
112681b2d1f0SGlenn Strauss 		const size_t ulen = strlen(uri);
11277c7f8c46SGlenn Strauss 		if (!buffer_is_equal_string(&r->target_orig, uri, ulen)) {
11287c7f8c46SGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
1129010c2894SGlenn Strauss 			  "digest: auth failed: uri mismatch (%s != %s), IP: %s",
11307c7f8c46SGlenn Strauss 			  r->target_orig.ptr, uri, r->con->dst_addr_buf->ptr);
113181b2d1f0SGlenn Strauss 			buffer_free(b);
11327c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
113381b2d1f0SGlenn Strauss 		}
113481b2d1f0SGlenn Strauss 	}
113581b2d1f0SGlenn Strauss 
1136e1188e77SGlenn Strauss 	/* check age of nonce.  Note, random data is used in nonce generation
1137e1188e77SGlenn Strauss 	 * in mod_auth_send_401_unauthorized_digest().  If that were replaced
1138e1188e77SGlenn Strauss 	 * with nanosecond time, then nonce secret would remain unique enough
1139e1188e77SGlenn Strauss 	 * for the purposes of Digest auth, and would be reproducible (and
1140e1188e77SGlenn Strauss 	 * verifiable) if nanoseconds were included with seconds as part of the
1141e1188e77SGlenn Strauss 	 * nonce "timestamp:secret".  However, doing so would expose a high
1142e1188e77SGlenn Strauss 	 * precision timestamp of the system to attackers.  timestamp in nonce
1143e1188e77SGlenn Strauss 	 * could theoretically be modified and still produce same md5sum, but
1144e1188e77SGlenn Strauss 	 * that is highly unlikely within a 10 min (moving) window of valid
1145e1188e77SGlenn Strauss 	 * time relative to current time (now).
1146e1188e77SGlenn Strauss 	 * If it is desired to validate that nonces were generated by server,
1147e1188e77SGlenn Strauss 	 * then specify auth.require = ( ... => ( "secret" => "..." ) )
1148e1188e77SGlenn Strauss 	 * When secret is specified, then instead of nanoseconds, the random
1149e1188e77SGlenn Strauss 	 * data value (included for unique nonces) will be exposed in the nonce
1150e1188e77SGlenn Strauss 	 * along with the timestamp, and the additional secret will be used to
1151e1188e77SGlenn Strauss 	 * validate that the server generated the nonce using that secret. */
1152e1188e77SGlenn Strauss 	int send_nextnonce;
1153e1188e77SGlenn Strauss 	{
1154e1188e77SGlenn Strauss 		time_t ts = 0;
1155e1188e77SGlenn Strauss 		const unsigned char * const nonce_uns = (unsigned char *)nonce;
1156e1188e77SGlenn Strauss 		for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) {
1157e1188e77SGlenn Strauss 			ts = (ts << 4) + hex2int(nonce_uns[i]);
1158e1188e77SGlenn Strauss 		}
1159e1188e77SGlenn Strauss 		const time_t cur_ts = log_epoch_secs;
1160e1188e77SGlenn Strauss 		if (nonce[i] != ':'
1161e1188e77SGlenn Strauss 		    || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/
1162e1188e77SGlenn Strauss 			/* nonce is stale; have client regenerate digest */
1163e1188e77SGlenn Strauss 			buffer_free(b);
1164e1188e77SGlenn Strauss 			return mod_auth_send_401_unauthorized_digest(r, require, ai.dalgo);
1165e1188e77SGlenn Strauss 		}
1166e1188e77SGlenn Strauss 
1167e1188e77SGlenn Strauss 		send_nextnonce = (cur_ts - ts > 540); /*(9 mins)*/
1168e1188e77SGlenn Strauss 
1169e1188e77SGlenn Strauss 		if (require->nonce_secret) {
1170e1188e77SGlenn Strauss 			unsigned int rnd = 0;
1171e1188e77SGlenn Strauss 			for (int j = i+8; i < j && light_isxdigit(nonce_uns[i]); ++i) {
1172e1188e77SGlenn Strauss 				rnd = (rnd << 4) + hex2int(nonce_uns[i]);
1173e1188e77SGlenn Strauss 			}
1174e1188e77SGlenn Strauss 			if (nonce[i] != ':') {
1175e1188e77SGlenn Strauss 				/* nonce is invalid;
1176e1188e77SGlenn Strauss 				 * expect extra field w/ require->nonce_secret */
1177e1188e77SGlenn Strauss 				log_error(r->conf.errh, __FILE__, __LINE__,
1178e1188e77SGlenn Strauss 				  "digest: nonce invalid");
1179e1188e77SGlenn Strauss 				buffer_free(b);
1180e1188e77SGlenn Strauss 				return mod_auth_send_400_bad_request(r);
1181e1188e77SGlenn Strauss 			}
1182e1188e77SGlenn Strauss 			buffer * const tb = r->tmp_buf;
1183e1188e77SGlenn Strauss 			buffer_clear(tb);
1184e1188e77SGlenn Strauss 			mod_auth_append_nonce(tb, cur_ts, require, ai.dalgo, (int *)&rnd);
1185e1188e77SGlenn Strauss 			if (!buffer_eq_slen(tb, nonce, strlen(nonce))) {
1186e1188e77SGlenn Strauss 				/* nonce not generated using current require->nonce_secret */
1187e1188e77SGlenn Strauss 				log_error(r->conf.errh, __FILE__, __LINE__,
1188e1188e77SGlenn Strauss 				  "digest: nonce mismatch");
1189e1188e77SGlenn Strauss 				buffer_free(b);
1190e1188e77SGlenn Strauss 				return mod_auth_send_401_unauthorized_digest(r, require, 0);
1191e1188e77SGlenn Strauss 			}
1192e1188e77SGlenn Strauss 		}
1193e1188e77SGlenn Strauss 	}
1194e1188e77SGlenn Strauss 
11957c7f8c46SGlenn Strauss 	switch (backend->digest(r, backend->p_d, &ai)) {
11964b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
11974b3a91e6SGlenn Strauss 		break;
11984b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
119981b2d1f0SGlenn Strauss 		buffer_free(b);
12004b3a91e6SGlenn Strauss 		return HANDLER_WAIT_FOR_EVENT;
12014b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
12024b3a91e6SGlenn Strauss 		buffer_free(b);
12034b3a91e6SGlenn Strauss 		return HANDLER_FINISHED;
12044b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
12054b3a91e6SGlenn Strauss 	default:
12067c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if unknown user)*/
12074b3a91e6SGlenn Strauss 		buffer_free(b);
12087c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
120981b2d1f0SGlenn Strauss 	}
121081b2d1f0SGlenn Strauss 
1211e1188e77SGlenn Strauss 	const char *m = get_http_method_name(r->http_method);
1212e1188e77SGlenn Strauss 	force_assert(m);
1213e1188e77SGlenn Strauss 
1214b9e2be50SGlenn Strauss 	mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop);
121581b2d1f0SGlenn Strauss 
12160e749c1cSGlenn Strauss 	if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) {
121781b2d1f0SGlenn Strauss 		/* digest not ok */
12187c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1219010c2894SGlenn Strauss 		  "digest: auth failed for %s: wrong password, IP: %s",
12207c7f8c46SGlenn Strauss 		  username, r->con->dst_addr_buf->ptr);
12217c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if bad password)*/
122281b2d1f0SGlenn Strauss 
122381b2d1f0SGlenn Strauss 		buffer_free(b);
12247c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
122581b2d1f0SGlenn Strauss 	}
122681b2d1f0SGlenn Strauss 
122781b2d1f0SGlenn Strauss 	/* value is our allow-rules */
12287ba06c71SGlenn Strauss 	if (!http_auth_match_rules(require, username, NULL, NULL)) {
12294b3a91e6SGlenn Strauss 		buffer_free(b);
12307c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
123181b2d1f0SGlenn Strauss 	}
123281b2d1f0SGlenn Strauss 
1233e1188e77SGlenn Strauss 	if (send_nextnonce) {
12346ad325c6SGlenn Strauss 			/*(send nextnonce when expiration is approaching)*/
12357c7f8c46SGlenn Strauss 			buffer * const tb = r->tmp_buf;
1236e1188e77SGlenn Strauss 			const time_t cur_ts = log_epoch_secs;
1237e1188e77SGlenn Strauss 			mod_auth_digest_authentication_info(tb, cur_ts, require, ai.dalgo);
12387c7f8c46SGlenn Strauss 			http_header_response_set(r, HTTP_HEADER_OTHER,
1239409bba80SGlenn Strauss 			                         CONST_STR_LEN("Authentication-Info"),
1240409bba80SGlenn Strauss 			                         CONST_BUF_LEN(tb));
12416ad325c6SGlenn Strauss 	}
124281b2d1f0SGlenn Strauss 
12437c7f8c46SGlenn Strauss 	http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest"));
124481b2d1f0SGlenn Strauss 
124581b2d1f0SGlenn Strauss 	buffer_free(b);
124681b2d1f0SGlenn Strauss 
12474b3a91e6SGlenn Strauss 	return HANDLER_GO_ON;
124881b2d1f0SGlenn Strauss }
124981b2d1f0SGlenn Strauss 
12507c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) {
12517c7f8c46SGlenn Strauss 	buffer * const tb = r->tmp_buf;
1252b5775b99SGlenn Strauss 	mod_auth_digest_www_authenticate(tb, log_epoch_secs, require, nonce_stale);
12537c7f8c46SGlenn Strauss 	http_header_response_set(r, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb));
125481b2d1f0SGlenn Strauss 
12557c7f8c46SGlenn Strauss 	r->http_status = 401;
12567c7f8c46SGlenn Strauss 	r->handler_module = NULL;
1257f9d58670SStefan Bühler 	return HANDLER_FINISHED;
12587ba06c71SGlenn Strauss }
12597ba06c71SGlenn Strauss 
12607c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
12617ba06c71SGlenn Strauss 	/* require REMOTE_USER already set */
12627c7f8c46SGlenn Strauss 	const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
12637ba06c71SGlenn Strauss 	UNUSED(p_d);
12647ba06c71SGlenn Strauss 	UNUSED(backend);
12653dd3cde9SGlenn Strauss 	if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) {
1266f9d58670SStefan Bühler 		return HANDLER_GO_ON;
1267bcdc6a3bSJan Kneschke 	} else {
12687c7f8c46SGlenn Strauss 		r->http_status = 401;
12697c7f8c46SGlenn Strauss 		r->handler_module = NULL;
1270bcdc6a3bSJan Kneschke 		return HANDLER_FINISHED;
12717187271fSStefan Bühler 	}
1272bcdc6a3bSJan Kneschke }
1273