xref: /lighttpd1.4/src/mod_auth.c (revision c8cd7cf4)
18abd06a7SGlenn Strauss #include "first.h"
28abd06a7SGlenn Strauss 
304d76e7aSGlenn Strauss #include "base.h"
422e8b456SStefan Bühler #include "plugin.h"
522e8b456SStefan Bühler #include "http_auth.h"
63dd3cde9SGlenn Strauss #include "http_header.h"
722e8b456SStefan Bühler #include "log.h"
8bcdc6a3bSJan Kneschke 
9b9e2be50SGlenn Strauss #include "sys-crypto.h" /* USE_OPENSSL_CRYPTO */
10b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO
11b9e2be50SGlenn Strauss #include <openssl/sha.h>
12b9e2be50SGlenn Strauss #endif
13b9e2be50SGlenn Strauss 
14bcdc6a3bSJan Kneschke #include <stdlib.h>
15bcdc6a3bSJan Kneschke #include <string.h>
16bcdc6a3bSJan Kneschke 
17bcdc6a3bSJan Kneschke /**
187ba06c71SGlenn Strauss  * auth framework
19bcdc6a3bSJan Kneschke  */
20bcdc6a3bSJan Kneschke 
214b3a91e6SGlenn Strauss typedef struct {
224b3a91e6SGlenn Strauss     const http_auth_backend_t *auth_backend;
238e713130SGlenn Strauss     const array *auth_require;
248e713130SGlenn Strauss     unsigned int auth_extern_authn;
257ba06c71SGlenn Strauss } plugin_config;
264b3a91e6SGlenn Strauss 
274b3a91e6SGlenn Strauss typedef struct {
284b3a91e6SGlenn Strauss     PLUGIN_DATA;
298e713130SGlenn Strauss     plugin_config defaults;
307ba06c71SGlenn Strauss     plugin_config conf;
317ba06c71SGlenn Strauss } plugin_data;
327ba06c71SGlenn Strauss 
33010c2894SGlenn Strauss static handler_t mod_auth_check_basic(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
34010c2894SGlenn Strauss static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
35010c2894SGlenn Strauss static handler_t mod_auth_check_extern(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
364b3a91e6SGlenn Strauss 
37bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) {
387ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_basic  = { "basic",  mod_auth_check_basic,  NULL };
397ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL };
407ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL };
417ba06c71SGlenn Strauss 	plugin_data *p;
427ba06c71SGlenn Strauss 
437ba06c71SGlenn Strauss 	/* register http_auth_scheme_* */
447ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_basic);
457ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_digest);
467ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_extern);
47bcdc6a3bSJan Kneschke 
48bcdc6a3bSJan Kneschke 	p = calloc(1, sizeof(*p));
49bcdc6a3bSJan Kneschke 
50bcdc6a3bSJan Kneschke 	return p;
51bcdc6a3bSJan Kneschke }
52bcdc6a3bSJan Kneschke 
53b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) {
54b73949e0SGlenn Strauss     plugin_data * const p = p_d;
558e713130SGlenn Strauss     if (NULL == p->cvlist) return;
568e713130SGlenn Strauss     /* (init i to 0 if global context; to 1 to skip empty global context) */
578e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
588e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
598e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
608e713130SGlenn Strauss             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
618e713130SGlenn Strauss             switch (cpv->k_id) {
628e713130SGlenn Strauss               case 1: /* auth.require */
638e713130SGlenn Strauss                 array_free(cpv->v.v);
648e713130SGlenn Strauss                 break;
658e713130SGlenn Strauss               default:
668e713130SGlenn Strauss                 break;
678e713130SGlenn Strauss             }
688e713130SGlenn Strauss         }
698e713130SGlenn Strauss     }
708e713130SGlenn Strauss }
718e713130SGlenn Strauss 
727ba06c71SGlenn Strauss /* data type for mod_auth structured data
737ba06c71SGlenn Strauss  * (parsed from auth.require array of strings) */
747ba06c71SGlenn Strauss typedef struct {
757ba06c71SGlenn Strauss     DATA_UNSET;
767ba06c71SGlenn Strauss     http_auth_require_t *require;
777ba06c71SGlenn Strauss } data_auth;
787ba06c71SGlenn Strauss 
797ba06c71SGlenn Strauss static void data_auth_free(data_unset *d)
807ba06c71SGlenn Strauss {
817ba06c71SGlenn Strauss     data_auth * const dauth = (data_auth *)d;
82ad9b7e00SGlenn Strauss     free(dauth->key.ptr);
837ba06c71SGlenn Strauss     http_auth_require_free(dauth->require);
847ba06c71SGlenn Strauss     free(dauth);
857ba06c71SGlenn Strauss }
867ba06c71SGlenn Strauss 
877ba06c71SGlenn Strauss static data_auth *data_auth_init(void)
887ba06c71SGlenn Strauss {
898c7f1dfbSGlenn Strauss     static const struct data_methods fn = {
908c7f1dfbSGlenn Strauss       NULL, /* copy must not be called on this data */
918c7f1dfbSGlenn Strauss       data_auth_free,
928c7f1dfbSGlenn Strauss       NULL, /* insert_dup must not be called on this data */
938c7f1dfbSGlenn Strauss       NULL  /* print must not be called on this data */
948c7f1dfbSGlenn Strauss     };
957ba06c71SGlenn Strauss     data_auth * const dauth = calloc(1, sizeof(*dauth));
967ba06c71SGlenn Strauss     force_assert(NULL != dauth);
977ba06c71SGlenn Strauss     dauth->type       = TYPE_OTHER;
988c7f1dfbSGlenn Strauss     dauth->fn         = &fn;
997ba06c71SGlenn Strauss 
1007ba06c71SGlenn Strauss     dauth->require = http_auth_require_init();
1017ba06c71SGlenn Strauss 
1027ba06c71SGlenn Strauss     return dauth;
1037ba06c71SGlenn Strauss }
1047ba06c71SGlenn Strauss 
105b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) {
106b9e2be50SGlenn Strauss     size_t len;
107b9e2be50SGlenn Strauss     if (NULL == s) {
108b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_MD5;
109b9e2be50SGlenn Strauss         ai->dlen  = HTTP_AUTH_DIGEST_MD5_BINLEN;
110b9e2be50SGlenn Strauss         return 1;
111b9e2be50SGlenn Strauss     }
112b9e2be50SGlenn Strauss 
113b9e2be50SGlenn Strauss     len = strlen(s);
114b9e2be50SGlenn Strauss     if (len > 5
115b9e2be50SGlenn Strauss         && (s[len-5]       ) == '-'
116b9e2be50SGlenn Strauss         && (s[len-4] | 0x20) == 's'
117b9e2be50SGlenn Strauss         && (s[len-3] | 0x20) == 'e'
118b9e2be50SGlenn Strauss         && (s[len-2] | 0x20) == 's'
119b9e2be50SGlenn Strauss         && (s[len-1] | 0x20) == 's') {
120b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_SESS;
121b9e2be50SGlenn Strauss         len -= 5;
122b9e2be50SGlenn Strauss     }
123b9e2be50SGlenn Strauss     else {
124b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_NONE;
125b9e2be50SGlenn Strauss     }
126b9e2be50SGlenn Strauss 
127b9e2be50SGlenn Strauss     if (3 == len
128b9e2be50SGlenn Strauss         && 'm' == (s[0] | 0x20)
129b9e2be50SGlenn Strauss         && 'd' == (s[1] | 0x20)
130b9e2be50SGlenn Strauss         && '5' == (s[2]       )) {
131b9e2be50SGlenn Strauss         ai->dalgo |= HTTP_AUTH_DIGEST_MD5;
132b9e2be50SGlenn Strauss         ai->dlen   = HTTP_AUTH_DIGEST_MD5_BINLEN;
133b9e2be50SGlenn Strauss         return 1;
134b9e2be50SGlenn Strauss     }
135b9e2be50SGlenn Strauss   #ifdef USE_OPENSSL_CRYPTO
136b9e2be50SGlenn Strauss     else if (len >= 7
137b9e2be50SGlenn Strauss              && 's' == (s[0] | 0x20)
138b9e2be50SGlenn Strauss              && 'h' == (s[1] | 0x20)
139b9e2be50SGlenn Strauss              && 'a' == (s[2] | 0x20)
140b9e2be50SGlenn Strauss              && '-' == (s[3]       )) {
141b9e2be50SGlenn Strauss         if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') {
142b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA256;
143b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA256_BINLEN;
144b9e2be50SGlenn Strauss             return 1;
145b9e2be50SGlenn Strauss         }
146b9e2be50SGlenn Strauss       #ifdef SHA512_256_DIGEST_LENGTH
147b9e2be50SGlenn Strauss         if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) {
148b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256;
149b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA512_256_BINLEN;
150b9e2be50SGlenn Strauss             return 1;
151b9e2be50SGlenn Strauss         }
152b9e2be50SGlenn Strauss       #endif
153b9e2be50SGlenn Strauss     }
154b9e2be50SGlenn Strauss   #endif
155b9e2be50SGlenn Strauss     return 0; /*(error)*/
156b9e2be50SGlenn Strauss }
157b9e2be50SGlenn Strauss 
158b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) {
159b9e2be50SGlenn Strauss     for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) {
160b9e2be50SGlenn Strauss         http_auth_info_t ai;
161b9e2be50SGlenn Strauss         int rc;
162b9e2be50SGlenn Strauss         p = strchr(s, '|');
163b9e2be50SGlenn Strauss         if (p) *p = '\0';
164b9e2be50SGlenn Strauss         rc = mod_auth_algorithm_parse(&ai, s);
165b9e2be50SGlenn Strauss         if (p) *p = '|';
166b9e2be50SGlenn Strauss         if (!rc) return 0;
167b9e2be50SGlenn Strauss         *algorithm |= ai.dalgo;
168b9e2be50SGlenn Strauss     }
169b9e2be50SGlenn Strauss     return 1;
170b9e2be50SGlenn Strauss }
171b9e2be50SGlenn Strauss 
1727ba06c71SGlenn Strauss static int mod_auth_require_parse (server *srv, http_auth_require_t * const require, const buffer *b)
1737ba06c71SGlenn Strauss {
1747ba06c71SGlenn Strauss     /* user=name1|user=name2|group=name3|host=name4 */
1757ba06c71SGlenn Strauss 
1767ba06c71SGlenn Strauss     const char *str = b->ptr;
1777ba06c71SGlenn Strauss     const char *p;
1787ba06c71SGlenn Strauss 
1797ba06c71SGlenn Strauss     if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) {
1807ba06c71SGlenn Strauss         require->valid_user = 1;
1817ba06c71SGlenn Strauss         return 1; /* success */
1827ba06c71SGlenn Strauss     }
1837ba06c71SGlenn Strauss 
1847ba06c71SGlenn Strauss     do {
1857ba06c71SGlenn Strauss         const char *eq;
1867ba06c71SGlenn Strauss         size_t len;
1877ba06c71SGlenn Strauss         p = strchr(str, '|');
1887ba06c71SGlenn Strauss         len = NULL != p ? (size_t)(p - str) : strlen(str);
1897ba06c71SGlenn Strauss         eq = memchr(str, '=', len);
1907ba06c71SGlenn Strauss         if (NULL == eq) {
191010c2894SGlenn Strauss             log_error(srv->errh, __FILE__, __LINE__,
192010c2894SGlenn Strauss               "error parsing auth.require 'require' field: missing '=' "
193010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
194010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
1957ba06c71SGlenn Strauss             return 0;
1967ba06c71SGlenn Strauss         }
197b2c18f48SGlenn Strauss         if (eq[1] == '|' || eq[1] == '\0') {
198010c2894SGlenn Strauss             log_error(srv->errh, __FILE__, __LINE__,
199010c2894SGlenn Strauss               "error parsing auth.require 'require' field: "
200010c2894SGlenn Strauss               "missing token after '=' "
201010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
202010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
2037ba06c71SGlenn Strauss             return 0;
2047ba06c71SGlenn Strauss         }
2057ba06c71SGlenn Strauss 
2067ba06c71SGlenn Strauss         switch ((int)(eq - str)) {
2077ba06c71SGlenn Strauss           case 4:
2087ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("user"))) {
209810109ccSGlenn Strauss                 /*("user=" is 5)*/
210a03afc90SGlenn Strauss                 array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN(""));
2117ba06c71SGlenn Strauss                 continue;
2127ba06c71SGlenn Strauss             }
2137ba06c71SGlenn Strauss             else if (0 == memcmp(str, CONST_STR_LEN("host"))) {
214810109ccSGlenn Strauss                 /*("host=" is 5)*/
215a03afc90SGlenn Strauss                 array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN(""));
216010c2894SGlenn Strauss                 log_error(srv->errh, __FILE__, __LINE__,
217010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
218010c2894SGlenn Strauss                   "'host' not implemented; field value: %s", b->ptr);
2197ba06c71SGlenn Strauss                 continue;
2207ba06c71SGlenn Strauss             }
2217ba06c71SGlenn Strauss             break; /* to error */
2227ba06c71SGlenn Strauss           case 5:
2237ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("group"))) {
224810109ccSGlenn Strauss                 /*("group=" is 6)*/
225a03afc90SGlenn Strauss                 array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN(""));
2269c91af0cSGlenn Strauss               #if 0/*(supported by mod_authn_ldap, but not all other backends)*/
227010c2894SGlenn Strauss                 log_error(srv->errh, __FILE__, __LINE__,
228010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
229010c2894SGlenn Strauss                   "'group' not implemented; field value: %s", b->ptr);
2309c91af0cSGlenn Strauss               #endif
2317ba06c71SGlenn Strauss                 continue;
2327ba06c71SGlenn Strauss             }
2337ba06c71SGlenn Strauss             break; /* to error */
2347ba06c71SGlenn Strauss           case 10:
2357ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) {
236010c2894SGlenn Strauss                 log_error(srv->errh, __FILE__, __LINE__,
237010c2894SGlenn Strauss                   "error parsing auth.require 'require' field: "
238010c2894SGlenn Strauss                   "valid user can not be combined with other require rules "
239010c2894SGlenn Strauss                   "(expecting \"valid-user\" or "
240010c2894SGlenn Strauss                   "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr);
2417ba06c71SGlenn Strauss                 return 0;
2427ba06c71SGlenn Strauss             }
2437ba06c71SGlenn Strauss             break; /* to error */
2447ba06c71SGlenn Strauss           default:
2457ba06c71SGlenn Strauss             break; /* to error */
2467ba06c71SGlenn Strauss         }
2477ba06c71SGlenn Strauss 
248010c2894SGlenn Strauss         log_error(srv->errh, __FILE__, __LINE__,
249010c2894SGlenn Strauss           "error parsing auth.require 'require' field: "
250010c2894SGlenn Strauss           "invalid/unsupported token "
251010c2894SGlenn Strauss           "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
252010c2894SGlenn Strauss           "error value: %s error near: %s", b->ptr, str);
2537ba06c71SGlenn Strauss         return 0;
2547ba06c71SGlenn Strauss 
2557ba06c71SGlenn Strauss     } while (p && *((str = p+1)));
2567ba06c71SGlenn Strauss 
2577ba06c71SGlenn Strauss     return 1; /* success */
2587ba06c71SGlenn Strauss }
2597ba06c71SGlenn Strauss 
2608e713130SGlenn Strauss static handler_t mod_auth_require_parse_array(server *srv, const array *value, array * const auth_require)
2618e713130SGlenn Strauss {
2628e713130SGlenn Strauss 		for (uint32_t n = 0; n < value->used; ++n) {
2637ba06c71SGlenn Strauss 			size_t m;
2648e713130SGlenn Strauss 			data_array *da_file = (data_array *)value->data[n];
2657ba06c71SGlenn Strauss 			const buffer *method = NULL, *realm = NULL, *require = NULL;
2667ba06c71SGlenn Strauss 			const http_auth_scheme_t *auth_scheme;
267b9e2be50SGlenn Strauss 			buffer *algos = NULL;
268b9e2be50SGlenn Strauss 			int algorithm = HTTP_AUTH_DIGEST_SESS;
2697ba06c71SGlenn Strauss 
270c2238256SGlenn Strauss 			if (!array_is_kvstring(&da_file->value)) {
271010c2894SGlenn Strauss 				log_error(srv->errh, __FILE__, __LINE__,
272010c2894SGlenn Strauss 				  "unexpected value for auth.require; expected "
273bd77abe0SGlenn Strauss 				  "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )");
2747ba06c71SGlenn Strauss 
2757ba06c71SGlenn Strauss 				return HANDLER_ERROR;
2767ba06c71SGlenn Strauss 			}
2777ba06c71SGlenn Strauss 
278c2238256SGlenn Strauss 			for (m = 0; m < da_file->value.used; m++) {
279c2238256SGlenn Strauss 				if (da_file->value.data[m]->type == TYPE_STRING) {
280c2238256SGlenn Strauss 					data_string *ds = (data_string *)da_file->value.data[m];
281ad9b7e00SGlenn Strauss 					if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) {
282601c572cSGlenn Strauss 						method = &ds->value;
283ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) {
284601c572cSGlenn Strauss 						realm = &ds->value;
285ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) {
286601c572cSGlenn Strauss 						require = &ds->value;
287ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) {
288601c572cSGlenn Strauss 						algos = &ds->value;
2897ba06c71SGlenn Strauss 					} else {
290010c2894SGlenn Strauss 						log_error(srv->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 {
298010c2894SGlenn Strauss 					log_error(srv->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)) {
308010c2894SGlenn Strauss 				log_error(srv->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) {
315010c2894SGlenn Strauss 					log_error(srv->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)) {
323010c2894SGlenn Strauss 				log_error(srv->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)) {
330010c2894SGlenn Strauss 				log_error(srv->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)) {
339010c2894SGlenn Strauss 				log_error(srv->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;
3517ba06c71SGlenn Strauss 				if (!mod_auth_require_parse(srv, dauth->require, require)) {
3528c7f1dfbSGlenn Strauss 					dauth->fn->free((data_unset *)dauth);
3537ba06c71SGlenn Strauss 					return HANDLER_ERROR;
3547ba06c71SGlenn Strauss 				}
3558e713130SGlenn Strauss 				array_insert_unique(auth_require, (data_unset *)dauth);
3567ba06c71SGlenn Strauss 			}
3577ba06c71SGlenn Strauss 		}
3587ba06c71SGlenn Strauss 
3597ba06c71SGlenn Strauss 	return HANDLER_GO_ON;
3607ba06c71SGlenn Strauss }
3617ba06c71SGlenn Strauss 
3628e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
3638e713130SGlenn Strauss     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
3648e713130SGlenn Strauss       case 0: /* auth.backend */
3658e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
3668e713130SGlenn Strauss             pconf->auth_backend = cpv->v.v;
3678e713130SGlenn Strauss         break;
3688e713130SGlenn Strauss       case 1: /* auth.require */
3698e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
3708e713130SGlenn Strauss             pconf->auth_require = cpv->v.v;
3718e713130SGlenn Strauss         break;
3728e713130SGlenn Strauss       case 2: /* auth.extern-authn */
3738e713130SGlenn Strauss         pconf->auth_extern_authn = cpv->v.u;
3748e713130SGlenn Strauss       default:/* should not happen */
3758e713130SGlenn Strauss         return;
3768e713130SGlenn Strauss     }
3778e713130SGlenn Strauss }
3784a6ce4aeSJan Kneschke 
3798e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
3808e713130SGlenn Strauss     do {
3818e713130SGlenn Strauss         mod_auth_merge_config_cpv(pconf, cpv);
3828e713130SGlenn Strauss     } while ((++cpv)->k_id != -1);
3838e713130SGlenn Strauss }
384bcdc6a3bSJan Kneschke 
3858e713130SGlenn Strauss static void mod_auth_patch_config(connection * const con, plugin_data * const p) {
3868e713130SGlenn Strauss     memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
3878e713130SGlenn Strauss     for (int i = 1, used = p->nconfig; i < used; ++i) {
3888e713130SGlenn Strauss         if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id))
3898e713130SGlenn Strauss             mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
3908e713130SGlenn Strauss     }
3918e713130SGlenn Strauss }
39236f64b26SGlenn Strauss 
3938e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) {
3948e713130SGlenn Strauss     static const config_plugin_keys_t cpk[] = {
3958e713130SGlenn Strauss       { CONST_STR_LEN("auth.backend"),
3968e713130SGlenn Strauss         T_CONFIG_STRING,
3978e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
3988e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.require"),
3998e713130SGlenn Strauss         T_CONFIG_ARRAY,
4008e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
4018e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.extern-authn"),
4028e713130SGlenn Strauss         T_CONFIG_BOOL,
4038e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
4048e713130SGlenn Strauss      ,{ NULL, 0,
4058e713130SGlenn Strauss         T_CONFIG_UNSET,
4068e713130SGlenn Strauss         T_CONFIG_SCOPE_UNSET }
4078e713130SGlenn Strauss     };
408bcdc6a3bSJan Kneschke 
4098e713130SGlenn Strauss     plugin_data * const p = p_d;
4108e713130SGlenn Strauss     if (!config_plugin_values_init(srv, p, cpk, "mod_auth"))
4118e713130SGlenn Strauss         return HANDLER_ERROR;
412bcdc6a3bSJan Kneschke 
4138e713130SGlenn Strauss     /* process and validate config directives
4148e713130SGlenn Strauss      * (init i to 0 if global context; to 1 to skip empty global context) */
4158e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
4168e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
4178e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
4188e713130SGlenn Strauss             switch (cpv->k_id) {
4198e713130SGlenn Strauss               case 0: /* auth.backend */
4208e713130SGlenn Strauss                 if (!buffer_string_is_empty(cpv->v.b)) {
4218e713130SGlenn Strauss                     const http_auth_backend_t * const auth_backend =
4228e713130SGlenn Strauss                       http_auth_backend_get(cpv->v.b);
4238e713130SGlenn Strauss                     if (NULL == auth_backend) {
4248e713130SGlenn Strauss                         log_error(srv->errh, __FILE__, __LINE__,
4258e713130SGlenn Strauss                           "auth.backend not supported: %s", cpv->v.b->ptr);
4268e713130SGlenn Strauss                         return HANDLER_ERROR;
4278e713130SGlenn Strauss                     }
4288e713130SGlenn Strauss                     *(const http_auth_backend_t **)&cpv->v.v = auth_backend;
4298e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
4308e713130SGlenn Strauss                 }
4318e713130SGlenn Strauss                 break;
4328e713130SGlenn Strauss               case 1: /* auth.require */
4338e713130SGlenn Strauss                 if (array_is_kvarray(cpv->v.a)) {
43424680a91SGlenn Strauss                     array * const a = array_init(4);
4358e713130SGlenn Strauss                     if (HANDLER_GO_ON !=
4368e713130SGlenn Strauss                         mod_auth_require_parse_array(srv, cpv->v.a, a)) {
4378e713130SGlenn Strauss                         array_free(a);
4388e713130SGlenn Strauss                         return HANDLER_ERROR;
4398e713130SGlenn Strauss                     }
4408e713130SGlenn Strauss                     cpv->v.a = a;
4418e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
4428e713130SGlenn Strauss                 }
4438e713130SGlenn Strauss                 else {
4448e713130SGlenn Strauss                     log_error(srv->errh, __FILE__, __LINE__,
4458e713130SGlenn Strauss                       "unexpected value for %s; expected "
4468e713130SGlenn Strauss                       "%s = ( \"urlpath\" => ( \"option\" => \"value\" ) )",
4478e713130SGlenn Strauss                       cpk[cpv->k_id].k, cpk[cpv->k_id].k);
4488e713130SGlenn Strauss                     return HANDLER_ERROR;
4498e713130SGlenn Strauss                 }
4508e713130SGlenn Strauss                 break;
4518e713130SGlenn Strauss               case 2: /* auth.extern-authn */
4528e713130SGlenn Strauss                 break;
4538e713130SGlenn Strauss               default:/* should not happen */
4548e713130SGlenn Strauss                 break;
455bcdc6a3bSJan Kneschke             }
456bcdc6a3bSJan Kneschke         }
457bcdc6a3bSJan Kneschke     }
458bcdc6a3bSJan Kneschke 
4598e713130SGlenn Strauss     /* initialize p->defaults from global config context */
4608e713130SGlenn Strauss     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
4618e713130SGlenn Strauss         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
4628e713130SGlenn Strauss         if (-1 != cpv->k_id)
4638e713130SGlenn Strauss             mod_auth_merge_config(&p->defaults, cpv);
464bcdc6a3bSJan Kneschke     }
4658e713130SGlenn Strauss 
4668e713130SGlenn Strauss     return HANDLER_GO_ON;
4678e713130SGlenn Strauss }
468bcdc6a3bSJan Kneschke 
46950bdb55dSGlenn Strauss static handler_t mod_auth_uri_handler(connection *con, void *p_d) {
4707ba06c71SGlenn Strauss 	plugin_data *p = p_d;
471d61f3381SGlenn Strauss 	data_auth *dauth;
47281b2d1f0SGlenn Strauss 
4738e713130SGlenn Strauss 	mod_auth_patch_config(con, p);
47481b2d1f0SGlenn Strauss 
4757ba06c71SGlenn Strauss 	if (p->conf.auth_require == NULL) return HANDLER_GO_ON;
47681b2d1f0SGlenn Strauss 
4777ba06c71SGlenn Strauss 	/* search auth directives for first prefix match against URL path */
4787ba06c71SGlenn Strauss 	/* if we have a case-insensitive FS we have to lower-case the URI here too */
479d61f3381SGlenn Strauss 	dauth = (!con->conf.force_lowercase_filenames)
480d61f3381SGlenn Strauss 	   ? (data_auth *)array_match_key_prefix(p->conf.auth_require, con->uri.path)
481d61f3381SGlenn Strauss 	   : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, con->uri.path);
482d61f3381SGlenn Strauss 	if (NULL == dauth) return HANDLER_GO_ON;
4837ba06c71SGlenn Strauss 
484d61f3381SGlenn Strauss 	{
4857ba06c71SGlenn Strauss 			const http_auth_scheme_t * const scheme = dauth->require->scheme;
486f54d628cSGlenn Strauss 			if (p->conf.auth_extern_authn) {
4876eb34ef5SGlenn Strauss 				const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER"));
4883dd3cde9SGlenn Strauss 				if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) {
489f54d628cSGlenn Strauss 					return HANDLER_GO_ON;
490f54d628cSGlenn Strauss 				}
491f54d628cSGlenn Strauss 			}
492010c2894SGlenn Strauss 			return scheme->checkfn(con, scheme->p_d, dauth->require, p->conf.auth_backend);
4937ba06c71SGlenn Strauss 	}
4947ba06c71SGlenn Strauss }
4957ba06c71SGlenn Strauss 
4967ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p);
4977ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) {
4987ba06c71SGlenn Strauss 	p->version     = LIGHTTPD_VERSION_ID;
499e2de4e58SGlenn Strauss 	p->name        = "auth";
5007ba06c71SGlenn Strauss 	p->init        = mod_auth_init;
5017ba06c71SGlenn Strauss 	p->set_defaults = mod_auth_set_defaults;
5027ba06c71SGlenn Strauss 	p->handle_uri_clean = mod_auth_uri_handler;
5037ba06c71SGlenn Strauss 	p->cleanup     = mod_auth_free;
5047ba06c71SGlenn Strauss 
50581b2d1f0SGlenn Strauss 	return 0;
50681b2d1f0SGlenn Strauss }
50781b2d1f0SGlenn Strauss 
50881b2d1f0SGlenn Strauss 
50981b2d1f0SGlenn Strauss 
51081b2d1f0SGlenn Strauss 
5117ba06c71SGlenn Strauss /*
5127ba06c71SGlenn Strauss  * auth schemes (basic, digest, extern)
5137ba06c71SGlenn Strauss  *
5147ba06c71SGlenn Strauss  * (could be in separate file from mod_auth.c as long as registration occurs)
5157ba06c71SGlenn Strauss  */
51681b2d1f0SGlenn Strauss 
5177ba06c71SGlenn Strauss #include "base64.h"
5187ba06c71SGlenn Strauss #include "md5.h"
5197f4e156eSGlenn Strauss #include "rand.h"
5203dd3cde9SGlenn Strauss #include "http_header.h"
52181b2d1f0SGlenn Strauss 
522010c2894SGlenn Strauss __attribute_cold__
523010c2894SGlenn Strauss static handler_t mod_auth_send_400_bad_request(connection *con) {
5244b3a91e6SGlenn Strauss 
5254b3a91e6SGlenn Strauss 	/* a field was missing or invalid */
5264b3a91e6SGlenn Strauss 	con->http_status = 400; /* Bad Request */
5274b3a91e6SGlenn Strauss 	con->mode = DIRECT;
5284b3a91e6SGlenn Strauss 
5294b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
5304b3a91e6SGlenn Strauss }
5314b3a91e6SGlenn Strauss 
532010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(connection *con, const buffer *realm) {
5334b3a91e6SGlenn Strauss 	con->http_status = 401;
5344b3a91e6SGlenn Strauss 	con->mode = DIRECT;
5354b3a91e6SGlenn Strauss 
536010c2894SGlenn Strauss 	buffer * const tb = con->srv->tmp_buf;
537010c2894SGlenn Strauss 	buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\""));
538010c2894SGlenn Strauss 	buffer_append_string_buffer(tb, realm);
539010c2894SGlenn Strauss 	buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\""));
5404b3a91e6SGlenn Strauss 
541010c2894SGlenn Strauss 	http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(tb));
5424b3a91e6SGlenn Strauss 
5434b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
5444b3a91e6SGlenn Strauss }
5454b3a91e6SGlenn Strauss 
546010c2894SGlenn Strauss static handler_t mod_auth_check_basic(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) {
5476eb34ef5SGlenn Strauss 	const buffer *b = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
54881b2d1f0SGlenn Strauss 	buffer *username;
54981b2d1f0SGlenn Strauss 	char *pw;
5507ba06c71SGlenn Strauss 	handler_t rc = HANDLER_UNSET;
55181b2d1f0SGlenn Strauss 
5527ba06c71SGlenn Strauss 	UNUSED(p_d);
55381b2d1f0SGlenn Strauss 
5547ba06c71SGlenn Strauss 	if (NULL == backend) {
555010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__, "auth.backend not configured for %s", con->uri.path->ptr);
5567ba06c71SGlenn Strauss 		con->http_status = 500;
5577ba06c71SGlenn Strauss 		con->mode = DIRECT;
5587ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
5597ba06c71SGlenn Strauss 	}
5607ba06c71SGlenn Strauss 
5613dd3cde9SGlenn Strauss 	if (NULL == b) {
562010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(con, require->realm);
5637ba06c71SGlenn Strauss 	}
5647ba06c71SGlenn Strauss 
56513008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) {
566010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(con, require->realm);
5674b3a91e6SGlenn Strauss 	}
5689bc61f16SGlenn Strauss       #ifdef __COVERITY__
5693dd3cde9SGlenn Strauss 	if (buffer_string_length(b) < sizeof("Basic ")-1) {
570010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
5719bc61f16SGlenn Strauss 	}
5729bc61f16SGlenn Strauss       #endif
57381b2d1f0SGlenn Strauss 
57481b2d1f0SGlenn Strauss 	username = buffer_init();
57581b2d1f0SGlenn Strauss 
5768047c2f4SGlenn Strauss 	/* coverity[overflow_sink : FALSE] */
5777ba06c71SGlenn Strauss 	if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) {
578010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1);
57981b2d1f0SGlenn Strauss 
58081b2d1f0SGlenn Strauss 		buffer_free(username);
581010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
58281b2d1f0SGlenn Strauss 	}
58381b2d1f0SGlenn Strauss 
58481b2d1f0SGlenn Strauss 	/* r2 == user:password */
58581b2d1f0SGlenn Strauss 	if (NULL == (pw = strchr(username->ptr, ':'))) {
586010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr);
58781b2d1f0SGlenn Strauss 
58881b2d1f0SGlenn Strauss 		buffer_free(username);
589010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
59081b2d1f0SGlenn Strauss 	}
59181b2d1f0SGlenn Strauss 
59281b2d1f0SGlenn Strauss 	buffer_string_set_length(username, pw - username->ptr);
59381b2d1f0SGlenn Strauss 	pw++;
59481b2d1f0SGlenn Strauss 
595010c2894SGlenn Strauss 	rc = backend->basic(con, backend->p_d, require, username, pw);
5967ba06c71SGlenn Strauss 	switch (rc) {
5974b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
5983dd3cde9SGlenn Strauss 		http_auth_setenv(con, CONST_BUF_LEN(username), CONST_STR_LEN("Basic"));
5994b3a91e6SGlenn Strauss 		break;
6004b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
6014b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
6027ba06c71SGlenn Strauss 		break;
6034b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
6044b3a91e6SGlenn Strauss 	default:
605010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
606010c2894SGlenn Strauss 		  "password doesn't match for %s username: %s IP: %s",
607010c2894SGlenn Strauss 		  con->uri.path->ptr, username->ptr, con->dst_addr_buf->ptr);
6088bddac92SGlenn Strauss 		con->keep_alive = 0; /*(disable keep-alive if bad password)*/
6097ba06c71SGlenn Strauss 		rc = HANDLER_UNSET;
6107ba06c71SGlenn Strauss 		break;
61181b2d1f0SGlenn Strauss 	}
61281b2d1f0SGlenn Strauss 
6134b3a91e6SGlenn Strauss 	buffer_free(username);
614010c2894SGlenn Strauss 	return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(con, require->realm);
61581b2d1f0SGlenn Strauss }
61681b2d1f0SGlenn Strauss 
61781b2d1f0SGlenn Strauss 
618b9e2be50SGlenn Strauss #ifdef USE_OPENSSL_CRYPTO
619b9e2be50SGlenn Strauss 
620b9e2be50SGlenn 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) {
621b9e2be50SGlenn Strauss     SHA256_CTX ctx;
622b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
623b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
624b9e2be50SGlenn Strauss 
625b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
626b9e2be50SGlenn Strauss         SHA256_Init(&ctx);
627b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
628b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
629b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
630b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
631b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
632b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
633b9e2be50SGlenn Strauss         SHA256_Final(ai->digest, &ctx);
634b9e2be50SGlenn Strauss     }
635b9e2be50SGlenn Strauss 
636b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
637b9e2be50SGlenn Strauss 
638b9e2be50SGlenn Strauss     /* calculate H(A2) */
639b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
640b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)m, strlen(m));
641b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
642b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri));
643b9e2be50SGlenn Strauss   #if 0
644b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
645fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
646b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
647b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
648b9e2be50SGlenn Strauss     }
649b9e2be50SGlenn Strauss   #endif
650b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
651b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
652b9e2be50SGlenn Strauss 
653b9e2be50SGlenn Strauss     /* calculate response */
654b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
655b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
656b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
657b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
658b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
659b9e2be50SGlenn Strauss     if (qop && *qop) {
660b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc));
661b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
662b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
663b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
664b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop));
665b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
666b9e2be50SGlenn Strauss     }
667b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
668b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
669b9e2be50SGlenn Strauss }
670b9e2be50SGlenn Strauss 
671b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd) {
672b9e2be50SGlenn Strauss     SHA256_CTX ctx;
673f24e6d69SGlenn Strauss     size_t len;
674b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN];
675b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
676b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
677b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
678f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
679f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
680f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
681f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
682b9e2be50SGlenn Strauss     SHA256_Final(h, &ctx);
683b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
684b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
685b9e2be50SGlenn Strauss }
686b9e2be50SGlenn Strauss 
687b9e2be50SGlenn Strauss #ifdef SHA512_256_DIGEST_LENGTH
688b9e2be50SGlenn Strauss 
689b9e2be50SGlenn 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) {
690b9e2be50SGlenn Strauss     SHA512_CTX ctx;
691b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
692b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
693b9e2be50SGlenn Strauss 
694b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
695b9e2be50SGlenn Strauss         SHA512_256_Init(&ctx);
696b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
697b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
698b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
699b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
700b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
701b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
702b9e2be50SGlenn Strauss         SHA512_256_Final(ai->digest, &ctx);
703b9e2be50SGlenn Strauss     }
704b9e2be50SGlenn Strauss 
705b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
706b9e2be50SGlenn Strauss 
707b9e2be50SGlenn Strauss     /* calculate H(A2) */
708b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
709b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m));
710b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
711b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri));
712b9e2be50SGlenn Strauss   #if 0
713b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
714fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
715b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
716b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2);
717b9e2be50SGlenn Strauss     }
718b9e2be50SGlenn Strauss   #endif
719b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
720b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
721b9e2be50SGlenn Strauss 
722b9e2be50SGlenn Strauss     /* calculate response */
723b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
724b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
725b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
726b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
727b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
728b9e2be50SGlenn Strauss     if (qop && *qop) {
729b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc));
730b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
731b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
732b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
733b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop));
734b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
735b9e2be50SGlenn Strauss     }
736b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
737b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
738b9e2be50SGlenn Strauss }
739b9e2be50SGlenn Strauss 
740b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd) {
741b9e2be50SGlenn Strauss     SHA512_CTX ctx;
742f24e6d69SGlenn Strauss     size_t len;
743b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN];
744b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
745b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
746b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
747f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
748f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
749f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
750f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
751b9e2be50SGlenn Strauss     SHA512_256_Final(h, &ctx);
752b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
753b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
754b9e2be50SGlenn Strauss }
755b9e2be50SGlenn Strauss 
756b9e2be50SGlenn Strauss #endif /* SHA512_256_DIGEST_LENGTH */
757b9e2be50SGlenn Strauss 
758b9e2be50SGlenn Strauss #endif /* USE_OPENSSL_CRYPTO */
759b9e2be50SGlenn Strauss 
760b9e2be50SGlenn 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) {
761b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
762b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
763b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
764b9e2be50SGlenn Strauss 
765b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
766b9e2be50SGlenn Strauss         li_MD5_Init(&ctx);
767b9e2be50SGlenn Strauss         /* http://www.rfc-editor.org/errata_search.php?rfc=2617
768b9e2be50SGlenn Strauss          * Errata ID: 1649 */
769b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
770b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
771b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
772b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
773b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
774b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
775b9e2be50SGlenn Strauss         li_MD5_Final(ai->digest, &ctx);
776b9e2be50SGlenn Strauss     }
777b9e2be50SGlenn Strauss 
778b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
779b9e2be50SGlenn Strauss 
780b9e2be50SGlenn Strauss     /* calculate H(A2) */
781b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
782b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)m, strlen(m));
783b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
784b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri));
785b9e2be50SGlenn Strauss   #if 0
786b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
787fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
788b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
789b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
790b9e2be50SGlenn Strauss     }
791b9e2be50SGlenn Strauss   #endif
792b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
793b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
794b9e2be50SGlenn Strauss 
795b9e2be50SGlenn Strauss     /* calculate response */
796b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
797b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
798b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
799b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
800b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
801b9e2be50SGlenn Strauss     if (qop && *qop) {
802b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc));
803b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
804b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
805b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
806b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop));
807b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
808b9e2be50SGlenn Strauss     }
809b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
810b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
811b9e2be50SGlenn Strauss }
812b9e2be50SGlenn Strauss 
813b9e2be50SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd) {
814b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
815f24e6d69SGlenn Strauss     size_t len;
816b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN];
817b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
818b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
819b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
820f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
821f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
822f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
823f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
824b9e2be50SGlenn Strauss     li_MD5_Final(h, &ctx);
825b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
826b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
827b9e2be50SGlenn Strauss }
828b9e2be50SGlenn Strauss 
829b9e2be50SGlenn 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) {
830b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_MD5)
831b9e2be50SGlenn Strauss         mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop);
832b9e2be50SGlenn Strauss   #ifdef USE_OPENSSL_CRYPTO
833b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256)
834b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop);
835b9e2be50SGlenn Strauss    #ifdef SHA512_256_DIGEST_LENGTH
836b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256)
837b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop);
838b9e2be50SGlenn Strauss    #endif
839b9e2be50SGlenn Strauss   #endif
840b9e2be50SGlenn Strauss }
841b9e2be50SGlenn Strauss 
842b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) {
843b9e2be50SGlenn Strauss     const int rnd = li_rand_pseudo();
844b9e2be50SGlenn Strauss     int algos = nonce_stale ? nonce_stale : require->algorithm;
845b9e2be50SGlenn Strauss     int n = 0;
846b9e2be50SGlenn Strauss     void(*append_nonce[3])(buffer *, time_t, int);
847b9e2be50SGlenn Strauss     unsigned int algolen[3];
848b9e2be50SGlenn Strauss     const char *algoname[3];
849b9e2be50SGlenn Strauss   #ifdef USE_OPENSSL_CRYPTO
850b9e2be50SGlenn Strauss    #ifdef SHA512_256_DIGEST_LENGTH
851b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA512_256) {
852b9e2be50SGlenn Strauss         append_nonce[n] = mod_auth_digest_nonce_sha512_256;
853b9e2be50SGlenn Strauss         algoname[n] = "SHA-512-256";
854b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-512-256")-1;
855b9e2be50SGlenn Strauss         ++n;
856b9e2be50SGlenn Strauss     }
857b9e2be50SGlenn Strauss    #endif
858b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA256) {
859b9e2be50SGlenn Strauss         append_nonce[n] = mod_auth_digest_nonce_sha256;
860b9e2be50SGlenn Strauss         algoname[n] = "SHA-256";
861b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-256")-1;
862b9e2be50SGlenn Strauss         ++n;
863b9e2be50SGlenn Strauss     }
864b9e2be50SGlenn Strauss   #endif
865b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_MD5) {
866b9e2be50SGlenn Strauss         append_nonce[n] = mod_auth_digest_nonce_md5;
867b9e2be50SGlenn Strauss         algoname[n] = "MD5";
868b9e2be50SGlenn Strauss         algolen[n] = sizeof("MD5")-1;
869b9e2be50SGlenn Strauss         ++n;
870b9e2be50SGlenn Strauss     }
871b9e2be50SGlenn Strauss 
872b9e2be50SGlenn Strauss     buffer_clear(b);
873b9e2be50SGlenn Strauss     for (int i = 0; i < n; ++i) {
874b9e2be50SGlenn Strauss         if (i > 0) {
875b9e2be50SGlenn Strauss             buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: "));
876b9e2be50SGlenn Strauss         }
877b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\""));
878b9e2be50SGlenn Strauss         buffer_append_string_buffer(b, require->realm);
8791799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm="));
880b9e2be50SGlenn Strauss         buffer_append_string_len(b, algoname[i], algolen[i]);
8811799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(", nonce=\""));
882b9e2be50SGlenn Strauss         buffer_append_uint_hex(b, (uintmax_t)cur_ts);
883b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(":"));
884b9e2be50SGlenn Strauss         (append_nonce[i])(b, cur_ts, rnd);
885b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\""));
886b9e2be50SGlenn Strauss         if (nonce_stale) {
887b9e2be50SGlenn Strauss             buffer_append_string_len(b, CONST_STR_LEN(", stale=true"));
888b9e2be50SGlenn Strauss         }
889b9e2be50SGlenn Strauss     }
89081b2d1f0SGlenn Strauss }
89181b2d1f0SGlenn Strauss 
8926ad325c6SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, int dalgo) {
8936ad325c6SGlenn Strauss     const int rnd = li_rand_pseudo();
8946ad325c6SGlenn Strauss     void(*append_nonce)(buffer *, time_t, int);
8956ad325c6SGlenn Strauss     switch (dalgo) {
8966ad325c6SGlenn Strauss      #ifdef USE_OPENSSL_CRYPTO
8976ad325c6SGlenn Strauss       #ifdef SHA512_256_DIGEST_LENGTH
8986ad325c6SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA512_256:
8996ad325c6SGlenn Strauss         append_nonce = mod_auth_digest_nonce_sha512_256;
9006ad325c6SGlenn Strauss         break;
9016ad325c6SGlenn Strauss       #endif
9026ad325c6SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA256:
9036ad325c6SGlenn Strauss         append_nonce = mod_auth_digest_nonce_sha256;
9046ad325c6SGlenn Strauss         break;
9056ad325c6SGlenn Strauss      #endif
9066ad325c6SGlenn Strauss       /*case HTTP_AUTH_DIGEST_MD5:*/
9076ad325c6SGlenn Strauss       default:
9086ad325c6SGlenn Strauss         append_nonce = mod_auth_digest_nonce_md5;
9096ad325c6SGlenn Strauss         break;
9106ad325c6SGlenn Strauss     }
9116ad325c6SGlenn Strauss     buffer_clear(b);
9126ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\""));
9136ad325c6SGlenn Strauss     buffer_append_uint_hex(b, (uintmax_t)cur_ts);
9146ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN(":"));
9156ad325c6SGlenn Strauss     (append_nonce)(b, cur_ts, rnd);
9166ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("\""));
9176ad325c6SGlenn Strauss }
9186ad325c6SGlenn Strauss 
91981b2d1f0SGlenn Strauss typedef struct {
92081b2d1f0SGlenn Strauss 	const char *key;
92181b2d1f0SGlenn Strauss 	int key_len;
92281b2d1f0SGlenn Strauss 	char **ptr;
92381b2d1f0SGlenn Strauss } digest_kv;
92481b2d1f0SGlenn Strauss 
925010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale);
9267ba06c71SGlenn Strauss 
927010c2894SGlenn Strauss static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) {
9286eb34ef5SGlenn Strauss 	const buffer *vb = http_header_request_get(con, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
9297ba06c71SGlenn Strauss 
93081b2d1f0SGlenn Strauss 	char *username = NULL;
93181b2d1f0SGlenn Strauss 	char *realm = NULL;
93281b2d1f0SGlenn Strauss 	char *nonce = NULL;
93381b2d1f0SGlenn Strauss 	char *uri = NULL;
93481b2d1f0SGlenn Strauss 	char *algorithm = NULL;
93581b2d1f0SGlenn Strauss 	char *qop = NULL;
93681b2d1f0SGlenn Strauss 	char *cnonce = NULL;
93781b2d1f0SGlenn Strauss 	char *nc = NULL;
93881b2d1f0SGlenn Strauss 	char *respons = NULL;
93981b2d1f0SGlenn Strauss 
94081b2d1f0SGlenn Strauss 	char *e, *c;
94181b2d1f0SGlenn Strauss 	const char *m = NULL;
94281b2d1f0SGlenn Strauss 	int i;
94381b2d1f0SGlenn Strauss 	buffer *b;
94460f4cf3aSGlenn Strauss 	http_auth_info_t ai;
945b9e2be50SGlenn Strauss 	unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN];
94681b2d1f0SGlenn Strauss 
94781b2d1f0SGlenn Strauss 
94881b2d1f0SGlenn Strauss 	/* init pointers */
94981b2d1f0SGlenn Strauss #define S(x) \
95081b2d1f0SGlenn Strauss 	x, sizeof(x)-1, NULL
95181b2d1f0SGlenn Strauss 	digest_kv dkv[10] = {
95281b2d1f0SGlenn Strauss 		{ S("username=") },
95381b2d1f0SGlenn Strauss 		{ S("realm=") },
95481b2d1f0SGlenn Strauss 		{ S("nonce=") },
95581b2d1f0SGlenn Strauss 		{ S("uri=") },
95681b2d1f0SGlenn Strauss 		{ S("algorithm=") },
95781b2d1f0SGlenn Strauss 		{ S("qop=") },
95881b2d1f0SGlenn Strauss 		{ S("cnonce=") },
95981b2d1f0SGlenn Strauss 		{ S("nc=") },
96081b2d1f0SGlenn Strauss 		{ S("response=") },
96181b2d1f0SGlenn Strauss 
96281b2d1f0SGlenn Strauss 		{ NULL, 0, NULL }
96381b2d1f0SGlenn Strauss 	};
96481b2d1f0SGlenn Strauss #undef S
96581b2d1f0SGlenn Strauss 
96681b2d1f0SGlenn Strauss 	dkv[0].ptr = &username;
96781b2d1f0SGlenn Strauss 	dkv[1].ptr = &realm;
96881b2d1f0SGlenn Strauss 	dkv[2].ptr = &nonce;
96981b2d1f0SGlenn Strauss 	dkv[3].ptr = &uri;
97081b2d1f0SGlenn Strauss 	dkv[4].ptr = &algorithm;
97181b2d1f0SGlenn Strauss 	dkv[5].ptr = &qop;
97281b2d1f0SGlenn Strauss 	dkv[6].ptr = &cnonce;
97381b2d1f0SGlenn Strauss 	dkv[7].ptr = &nc;
97481b2d1f0SGlenn Strauss 	dkv[8].ptr = &respons;
97581b2d1f0SGlenn Strauss 
9767ba06c71SGlenn Strauss 	UNUSED(p_d);
9777ba06c71SGlenn Strauss 
9787ba06c71SGlenn Strauss 	if (NULL == backend) {
979010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
980010c2894SGlenn Strauss 		  "auth.backend not configured for %s", con->uri.path->ptr);
9817ba06c71SGlenn Strauss 		con->http_status = 500;
9827ba06c71SGlenn Strauss 		con->mode = DIRECT;
9837ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
9847ba06c71SGlenn Strauss 	}
9857ba06c71SGlenn Strauss 
9863dd3cde9SGlenn Strauss 	if (NULL == vb) {
987010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
9887ba06c71SGlenn Strauss 	}
9897ba06c71SGlenn Strauss 
99013008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) {
991010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
992dadfb5fcSGlenn Strauss 	} else {
9933dd3cde9SGlenn Strauss 		size_t n = buffer_string_length(vb);
9949bc61f16SGlenn Strauss 	      #ifdef __COVERITY__
995dadfb5fcSGlenn Strauss 		if (n < sizeof("Digest ")-1) {
996010c2894SGlenn Strauss 			return mod_auth_send_400_bad_request(con);
9979bc61f16SGlenn Strauss 		}
9989bc61f16SGlenn Strauss 	      #endif
999dadfb5fcSGlenn Strauss 		n -= (sizeof("Digest ")-1);
10007ba06c71SGlenn Strauss 		b = buffer_init();
10013dd3cde9SGlenn Strauss 		buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n);
1002dadfb5fcSGlenn Strauss 	}
100381b2d1f0SGlenn Strauss 
100481b2d1f0SGlenn Strauss 	/* parse credentials from client */
100581b2d1f0SGlenn Strauss 	for (c = b->ptr; *c; c++) {
100681b2d1f0SGlenn Strauss 		/* skip whitespaces */
100781b2d1f0SGlenn Strauss 		while (*c == ' ' || *c == '\t') c++;
100881b2d1f0SGlenn Strauss 		if (!*c) break;
100981b2d1f0SGlenn Strauss 
101081b2d1f0SGlenn Strauss 		for (i = 0; dkv[i].key; i++) {
101181b2d1f0SGlenn Strauss 			if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) {
101281b2d1f0SGlenn Strauss 				if ((c[dkv[i].key_len] == '"') &&
101381b2d1f0SGlenn Strauss 				    (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) {
101481b2d1f0SGlenn Strauss 					/* value with "..." */
101581b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len + 1;
101681b2d1f0SGlenn Strauss 					c = e;
101781b2d1f0SGlenn Strauss 
101881b2d1f0SGlenn Strauss 					*e = '\0';
101981b2d1f0SGlenn Strauss 				} else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) {
102081b2d1f0SGlenn Strauss 					/* value without "...", terminated by ',' */
102181b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
102281b2d1f0SGlenn Strauss 					c = e;
102381b2d1f0SGlenn Strauss 
102481b2d1f0SGlenn Strauss 					*e = '\0';
102581b2d1f0SGlenn Strauss 				} else {
102681b2d1f0SGlenn Strauss 					/* value without "...", terminated by EOL */
102781b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
102881b2d1f0SGlenn Strauss 					c += strlen(c) - 1;
102981b2d1f0SGlenn Strauss 				}
103081b2d1f0SGlenn Strauss 				break;
103181b2d1f0SGlenn Strauss 			}
103281b2d1f0SGlenn Strauss 		}
103381b2d1f0SGlenn Strauss 	}
103481b2d1f0SGlenn Strauss 
103581b2d1f0SGlenn Strauss 	/* check if everything is transmitted */
103681b2d1f0SGlenn Strauss 	if (!username ||
103781b2d1f0SGlenn Strauss 	    !realm ||
103881b2d1f0SGlenn Strauss 	    !nonce ||
103981b2d1f0SGlenn Strauss 	    !uri ||
104081b2d1f0SGlenn Strauss 	    (qop && (!nc || !cnonce)) ||
104181b2d1f0SGlenn Strauss 	    !respons ) {
104281b2d1f0SGlenn Strauss 		/* missing field */
104381b2d1f0SGlenn Strauss 
1044010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
104581b2d1f0SGlenn Strauss 		  "digest: missing field");
104681b2d1f0SGlenn Strauss 
104781b2d1f0SGlenn Strauss 		buffer_free(b);
1048010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
104981b2d1f0SGlenn Strauss 	}
105081b2d1f0SGlenn Strauss 
105160f4cf3aSGlenn Strauss 	ai.username = username;
105260f4cf3aSGlenn Strauss 	ai.ulen     = strlen(username);
105360f4cf3aSGlenn Strauss 	ai.realm    = realm;
105460f4cf3aSGlenn Strauss 	ai.rlen     = strlen(realm);
105560f4cf3aSGlenn Strauss 
105660f4cf3aSGlenn Strauss 	if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) {
1057010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
10585a6e4df8SGlenn Strauss 		  "digest: realm mismatch");
10595a6e4df8SGlenn Strauss 		buffer_free(b);
1060010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
10615a6e4df8SGlenn Strauss 	}
10625a6e4df8SGlenn Strauss 
1063b9e2be50SGlenn Strauss 	if (!mod_auth_algorithm_parse(&ai, algorithm)
1064b9e2be50SGlenn Strauss 	    || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) {
1065010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
1066010c2894SGlenn Strauss 		  "digest: (%s): invalid", algorithm);
1067b9e2be50SGlenn Strauss 		buffer_free(b);
1068010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
1069b9e2be50SGlenn Strauss 	}
1070b9e2be50SGlenn Strauss 
107181b2d1f0SGlenn Strauss 	/**
107281b2d1f0SGlenn Strauss 	 * protect the md5-sess against missing cnonce and nonce
107381b2d1f0SGlenn Strauss 	 */
1074b9e2be50SGlenn Strauss 	if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) {
1075010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
1076010c2894SGlenn Strauss 		  "digest: (%s): missing field", algorithm);
107781b2d1f0SGlenn Strauss 
107881b2d1f0SGlenn Strauss 		buffer_free(b);
1079010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
108081b2d1f0SGlenn Strauss 	}
108181b2d1f0SGlenn Strauss 
1082b9e2be50SGlenn Strauss 	{
1083b9e2be50SGlenn Strauss 		size_t resplen = strlen(respons);
1084b9e2be50SGlenn Strauss 		if (0 != http_auth_digest_hex2bin(respons, resplen,
1085b9e2be50SGlenn Strauss 						  rdigest, sizeof(rdigest))
1086b9e2be50SGlenn Strauss 		    || resplen != (ai.dlen << 1)) {
1087010c2894SGlenn Strauss 			log_error(con->conf.errh, __FILE__, __LINE__,
1088010c2894SGlenn Strauss 			  "digest: (%s): invalid format", respons);
1089b9e2be50SGlenn Strauss 			buffer_free(b);
1090010c2894SGlenn Strauss 			return mod_auth_send_400_bad_request(con);
1091b9e2be50SGlenn Strauss 		}
1092b9e2be50SGlenn Strauss 	}
1093b9e2be50SGlenn Strauss 
1094fc42ec12SGlenn Strauss 	if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
1095010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
109681b2d1f0SGlenn Strauss 		  "digest: qop=auth-int not supported");
109781b2d1f0SGlenn Strauss 
109881b2d1f0SGlenn Strauss 		buffer_free(b);
1099010c2894SGlenn Strauss 		return mod_auth_send_400_bad_request(con);
110081b2d1f0SGlenn Strauss 	}
110181b2d1f0SGlenn Strauss 
110281b2d1f0SGlenn Strauss 	m = get_http_method_name(con->request.http_method);
110381b2d1f0SGlenn Strauss 	force_assert(m);
110481b2d1f0SGlenn Strauss 
110581b2d1f0SGlenn Strauss 	/* detect if attacker is attempting to reuse valid digest for one uri
110681b2d1f0SGlenn Strauss 	 * on a different request uri.  Might also happen if intermediate proxy
110781b2d1f0SGlenn Strauss 	 * altered client request line.  (Altered request would not result in
110881b2d1f0SGlenn Strauss 	 * the same digest as that calculated by the client.)
110981b2d1f0SGlenn Strauss 	 * Internal redirects such as with mod_rewrite will modify request uri.
111081b2d1f0SGlenn Strauss 	 * Reauthentication is done to detect crossing auth realms, but this
111181b2d1f0SGlenn Strauss 	 * uri validation step is bypassed.  con->request.orig_uri is original
111281b2d1f0SGlenn Strauss 	 * uri sent in client request. */
111381b2d1f0SGlenn Strauss 	{
111481b2d1f0SGlenn Strauss 		const size_t ulen = strlen(uri);
1115c81bd354SGlenn Strauss 		if (!buffer_is_equal_string(con->request.orig_uri, uri, ulen)) {
1116010c2894SGlenn Strauss 			log_error(con->conf.errh, __FILE__, __LINE__,
1117010c2894SGlenn Strauss 			  "digest: auth failed: uri mismatch (%s != %s), IP: %s",
1118010c2894SGlenn Strauss 			  con->request.orig_uri->ptr, uri, con->dst_addr_buf->ptr);
111981b2d1f0SGlenn Strauss 			buffer_free(b);
1120010c2894SGlenn Strauss 			return mod_auth_send_400_bad_request(con);
112181b2d1f0SGlenn Strauss 		}
112281b2d1f0SGlenn Strauss 	}
112381b2d1f0SGlenn Strauss 
1124010c2894SGlenn Strauss 	switch (backend->digest(con, backend->p_d, &ai)) {
11254b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
11264b3a91e6SGlenn Strauss 		break;
11274b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
112881b2d1f0SGlenn Strauss 		buffer_free(b);
11294b3a91e6SGlenn Strauss 		return HANDLER_WAIT_FOR_EVENT;
11304b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
11314b3a91e6SGlenn Strauss 		buffer_free(b);
11324b3a91e6SGlenn Strauss 		return HANDLER_FINISHED;
11334b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
11344b3a91e6SGlenn Strauss 	default:
11358bddac92SGlenn Strauss 		con->keep_alive = 0; /*(disable keep-alive if unknown user)*/
11364b3a91e6SGlenn Strauss 		buffer_free(b);
1137010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
113881b2d1f0SGlenn Strauss 	}
113981b2d1f0SGlenn Strauss 
1140b9e2be50SGlenn Strauss 	mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop);
114181b2d1f0SGlenn Strauss 
11420e749c1cSGlenn Strauss 	if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) {
114381b2d1f0SGlenn Strauss 		/* digest not ok */
1144010c2894SGlenn Strauss 		log_error(con->conf.errh, __FILE__, __LINE__,
1145010c2894SGlenn Strauss 		  "digest: auth failed for %s: wrong password, IP: %s",
1146010c2894SGlenn Strauss 		  username, con->dst_addr_buf->ptr);
11478bddac92SGlenn Strauss 		con->keep_alive = 0; /*(disable keep-alive if bad password)*/
114881b2d1f0SGlenn Strauss 
114981b2d1f0SGlenn Strauss 		buffer_free(b);
1150010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
115181b2d1f0SGlenn Strauss 	}
115281b2d1f0SGlenn Strauss 
115381b2d1f0SGlenn Strauss 	/* value is our allow-rules */
11547ba06c71SGlenn Strauss 	if (!http_auth_match_rules(require, username, NULL, NULL)) {
11554b3a91e6SGlenn Strauss 		buffer_free(b);
1156010c2894SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(con, require, 0);
115781b2d1f0SGlenn Strauss 	}
115881b2d1f0SGlenn Strauss 
11597f4e156eSGlenn Strauss 	/* check age of nonce.  Note, random data is used in nonce generation
11607ba06c71SGlenn Strauss 	 * in mod_auth_send_401_unauthorized_digest().  If that were replaced
116181b2d1f0SGlenn Strauss 	 * with nanosecond time, then nonce secret would remain unique enough
116281b2d1f0SGlenn Strauss 	 * for the purposes of Digest auth, and would be reproducible (and
116381b2d1f0SGlenn Strauss 	 * verifiable) if nanoseconds were inclued with seconds as part of the
116481b2d1f0SGlenn Strauss 	 * nonce "timestamp:secret".  Since that is not done, timestamp in
116581b2d1f0SGlenn Strauss 	 * nonce could theoretically be modified and still produce same md5sum,
116681b2d1f0SGlenn Strauss 	 * but that is highly unlikely within a 10 min (moving) window of valid
116781b2d1f0SGlenn Strauss 	 * time relative to current time (now) */
116881b2d1f0SGlenn Strauss 	{
116981b2d1f0SGlenn Strauss 		time_t ts = 0;
117081b2d1f0SGlenn Strauss 		const unsigned char * const nonce_uns = (unsigned char *)nonce;
117181b2d1f0SGlenn Strauss 		for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) {
117281b2d1f0SGlenn Strauss 			ts = (ts << 4) + hex2int(nonce_uns[i]);
117381b2d1f0SGlenn Strauss 		}
1174*c8cd7cf4SGlenn Strauss 		const time_t cur_ts = log_epoch_secs;
117583759aedSGlenn Strauss 		if (nonce[i] != ':'
1176409bba80SGlenn Strauss 		    || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/
11774b3a91e6SGlenn Strauss 			/* nonce is stale; have client regenerate digest */
117881b2d1f0SGlenn Strauss 			buffer_free(b);
1179010c2894SGlenn Strauss 			return mod_auth_send_401_unauthorized_digest(con, require, ai.dalgo);
11806ad325c6SGlenn Strauss 		}
1181409bba80SGlenn Strauss 		else if (cur_ts - ts > 540) { /*(9 mins)*/
11826ad325c6SGlenn Strauss 			/*(send nextnonce when expiration is approaching)*/
1183409bba80SGlenn Strauss 			buffer * const tb = con->srv->tmp_buf;
1184409bba80SGlenn Strauss 			mod_auth_digest_authentication_info(tb, cur_ts, ai.dalgo);
1185409bba80SGlenn Strauss 			http_header_response_set(con, HTTP_HEADER_OTHER,
1186409bba80SGlenn Strauss 			                         CONST_STR_LEN("Authentication-Info"),
1187409bba80SGlenn Strauss 			                         CONST_BUF_LEN(tb));
11886ad325c6SGlenn Strauss 		}
118981b2d1f0SGlenn Strauss 	}
119081b2d1f0SGlenn Strauss 
119160f4cf3aSGlenn Strauss 	http_auth_setenv(con, ai.username, ai.ulen, CONST_STR_LEN("Digest"));
119281b2d1f0SGlenn Strauss 
119381b2d1f0SGlenn Strauss 	buffer_free(b);
119481b2d1f0SGlenn Strauss 
11954b3a91e6SGlenn Strauss 	return HANDLER_GO_ON;
119681b2d1f0SGlenn Strauss }
119781b2d1f0SGlenn Strauss 
1198010c2894SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(connection *con, const struct http_auth_require_t *require, int nonce_stale) {
1199010c2894SGlenn Strauss 	server *srv = con->srv;
1200*c8cd7cf4SGlenn Strauss 	mod_auth_digest_www_authenticate(srv->tmp_buf, log_epoch_secs, require, nonce_stale);
1201b9e2be50SGlenn Strauss 	http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(srv->tmp_buf));
120281b2d1f0SGlenn Strauss 
1203f9d58670SStefan Bühler 	con->http_status = 401;
1204f9d58670SStefan Bühler 	con->mode = DIRECT;
1205f9d58670SStefan Bühler 	return HANDLER_FINISHED;
12067ba06c71SGlenn Strauss }
12077ba06c71SGlenn Strauss 
1208010c2894SGlenn Strauss static handler_t mod_auth_check_extern(connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) {
12097ba06c71SGlenn Strauss 	/* require REMOTE_USER already set */
12106eb34ef5SGlenn Strauss 	const buffer *vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER"));
12117ba06c71SGlenn Strauss 	UNUSED(p_d);
12127ba06c71SGlenn Strauss 	UNUSED(backend);
12133dd3cde9SGlenn Strauss 	if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) {
1214f9d58670SStefan Bühler 		return HANDLER_GO_ON;
1215bcdc6a3bSJan Kneschke 	} else {
12164b3a91e6SGlenn Strauss 		con->http_status = 401;
12174b3a91e6SGlenn Strauss 		con->mode = DIRECT;
1218bcdc6a3bSJan Kneschke 		return HANDLER_FINISHED;
12197187271fSStefan Bühler 	}
1220bcdc6a3bSJan Kneschke }
1221