/src/mhd2/src/mhd2/auth_digest.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ |
2 | | /* |
3 | | This file is part of GNU libmicrohttpd. |
4 | | Copyright (C) 2014-2025 Evgeny Grin (Karlson2k) |
5 | | Copyright (C) 2010, 2011, 2012, 2015, 2018 Christian Grothoff |
6 | | |
7 | | GNU libmicrohttpd is free software; you can redistribute it and/or |
8 | | modify it under the terms of the GNU Lesser General Public |
9 | | License as published by the Free Software Foundation; either |
10 | | version 2.1 of the License, or (at your option) any later version. |
11 | | |
12 | | GNU libmicrohttpd is distributed in the hope that it will be useful, |
13 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | Lesser General Public License for more details. |
16 | | |
17 | | Alternatively, you can redistribute GNU libmicrohttpd and/or |
18 | | modify it under the terms of the GNU General Public License as |
19 | | published by the Free Software Foundation; either version 2 of |
20 | | the License, or (at your option) any later version, together |
21 | | with the eCos exception, as follows: |
22 | | |
23 | | As a special exception, if other files instantiate templates or |
24 | | use macros or inline functions from this file, or you compile this |
25 | | file and link it with other works to produce a work based on this |
26 | | file, this file does not by itself cause the resulting work to be |
27 | | covered by the GNU General Public License. However the source code |
28 | | for this file must still be made available in accordance with |
29 | | section (3) of the GNU General Public License v2. |
30 | | |
31 | | This exception does not invalidate any other reasons why a work |
32 | | based on this file might be covered by the GNU General Public |
33 | | License. |
34 | | |
35 | | You should have received copies of the GNU Lesser General Public |
36 | | License and the GNU General Public License along with this library; |
37 | | if not, see <https://www.gnu.org/licenses/>. |
38 | | */ |
39 | | |
40 | | /** |
41 | | * @file src/mhd2/auth_digest.c |
42 | | * @brief The implementation of the Digest Authorization internal functions |
43 | | * @author Karlson2k (Evgeny Grin) |
44 | | * Based on the MHD v0.xx code by Amr Ali, Matthieu Speder, Christian Grothoff, |
45 | | * Dirk Brinkmeier and Evgeny Grin. |
46 | | */ |
47 | | |
48 | | #include "mhd_sys_options.h" |
49 | | |
50 | | #include "mhd_digest_auth_data.h" |
51 | | |
52 | | #include "mhd_assert.h" |
53 | | #include "mhd_static_assert.h" |
54 | | #include "mhd_unreachable.h" |
55 | | #include "mhd_assume.h" |
56 | | |
57 | | #include <string.h> |
58 | | #include "sys_malloc.h" |
59 | | |
60 | | #include "mhd_str_macros.h" |
61 | | #include "mhd_bithelpers.h" |
62 | | #include "mhd_arr_num_elems.h" |
63 | | #include "mhd_cntnr_ptr.h" |
64 | | #include "mhd_limits.h" |
65 | | #include "mhd_rng.h" |
66 | | |
67 | | #include "mhd_str_types.h" |
68 | | #include "mhd_buffer.h" |
69 | | #include "mhd_daemon.h" |
70 | | #include "mhd_request.h" |
71 | | #include "mhd_connection.h" |
72 | | |
73 | | #ifdef MHD_SUPPORT_SHA512_256 |
74 | | # include "mhd_sha512_256.h" |
75 | | #endif /* MHD_SUPPORT_SHA512_256 */ |
76 | | #ifdef MHD_SUPPORT_SHA256 |
77 | | # include "mhd_sha256.h" |
78 | | #endif |
79 | | #ifdef MHD_SUPPORT_MD5 |
80 | | # include "mhd_md5.h" |
81 | | #endif |
82 | | |
83 | | #include "mhd_str.h" |
84 | | #include "mhd_mono_clock.h" |
85 | | #include "mhd_atomic_counter.h" |
86 | | #include "mhd_locks.h" |
87 | | |
88 | | #include "request_auth_get.h" |
89 | | #include "daemon_funcs.h" |
90 | | #include "stream_funcs.h" |
91 | | #include "stream_process_request.h" |
92 | | |
93 | | #include "auth_digest.h" |
94 | | |
95 | | /* |
96 | | * The maximum size of the hash digest, in bytes |
97 | | */ |
98 | | #if defined(MHD_SUPPORT_SHA512_256) |
99 | | # define mhd_MAX_DIGEST mhd_SHA512_256_DIGEST_SIZE |
100 | | #elif defined(MHD_SUPPORT_SHA256) |
101 | | # define mhd_MAX_DIGEST mhd_SHA256_DIGEST_SIZE |
102 | | #else |
103 | | # define mhd_MAX_DIGEST mhd_MD5_DIGEST_SIZE |
104 | | #endif |
105 | | |
106 | | /** |
107 | | * MD5 algorithm identifier for Digest Auth headers |
108 | | */ |
109 | | #define mhd_MD5_TOKEN "MD5" |
110 | | |
111 | | /** |
112 | | * SHA-256 algorithm identifier for Digest Auth headers |
113 | | */ |
114 | | #define mhd_SHA256_TOKEN "SHA-256" |
115 | | |
116 | | /** |
117 | | * SHA-512/256 algorithm for Digest Auth headers. |
118 | | */ |
119 | | #define mhd_SHA512_256_TOKEN "SHA-512-256" |
120 | | |
121 | | /** |
122 | | * The suffix token for "session" algorithms for Digest Auth headers. |
123 | | */ |
124 | | #define mhd_SESS_TOKEN "-sess" |
125 | | |
126 | | /** |
127 | | * The "auth" token for QOP for Digest Auth headers. |
128 | | */ |
129 | | #define mhd_TOKEN_AUTH "auth" |
130 | | |
131 | | /** |
132 | | * The "auth-int" token for QOP for Digest Auth headers. |
133 | | */ |
134 | | #define mhd_TOKEN_AUTH_INT "auth-int" |
135 | | |
136 | | |
137 | | /** |
138 | | * The required prefix of parameter with the extended notation |
139 | | */ |
140 | 0 | #define mhd_DAUTH_EXT_PARAM_PREFIX "UTF-8'" |
141 | | |
142 | | /** |
143 | | * The minimal size of the prefix for parameter with the extended notation |
144 | | */ |
145 | | #define mhd_DAUTH_EXT_PARAM_MIN_LEN \ |
146 | 0 | mhd_SSTR_LEN (mhd_DAUTH_EXT_PARAM_PREFIX "'") |
147 | | |
148 | | /** |
149 | | * The maximum supported size for Digest Auth parameters, like "realm", |
150 | | * "username" etc. |
151 | | * This limitation is used only for quoted parameters. |
152 | | * Parameters without quoted backslash character will be processed as long |
153 | | * as they fit connection memory pool (buffer) size. |
154 | | */ |
155 | 0 | #define mhd_AUTH_DIGEST_MAX_PARAM_SIZE (65535) |
156 | | |
157 | | /** |
158 | | * Parameter of request's Digest Authorization header |
159 | | */ |
160 | | struct mhd_RqDAuthParam |
161 | | { |
162 | | /** |
163 | | * The string with length, NOT zero-terminated |
164 | | */ |
165 | | struct MHD_StringNullable value; |
166 | | /** |
167 | | * True if string must be "unquoted" before processing. |
168 | | * This member is false if the string is used in DQUOTE marks, but no |
169 | | * backslash-escape is used in the string. |
170 | | */ |
171 | | bool quoted; |
172 | | }; |
173 | | |
174 | | /** |
175 | | * Client's Digest Authorization header parameters |
176 | | */ |
177 | | struct mhd_AuthDigesReqParams |
178 | | { |
179 | | struct mhd_RqDAuthParam nonce; |
180 | | struct mhd_RqDAuthParam opaque; |
181 | | struct mhd_RqDAuthParam response; |
182 | | struct mhd_RqDAuthParam username; |
183 | | struct mhd_RqDAuthParam username_ext; |
184 | | struct mhd_RqDAuthParam realm; |
185 | | struct mhd_RqDAuthParam uri; |
186 | | /* The raw QOP value, used in the 'response' calculation */ |
187 | | struct mhd_RqDAuthParam qop_raw; |
188 | | struct mhd_RqDAuthParam cnonce; |
189 | | struct mhd_RqDAuthParam nc; |
190 | | |
191 | | /* Decoded values are below */ |
192 | | bool userhash; /* True if 'userhash' parameter has value 'true'. */ |
193 | | enum MHD_DigestAuthAlgo algo; |
194 | | enum MHD_DigestAuthQOP qop; |
195 | | }; |
196 | | |
197 | | /** |
198 | | * Digest context data |
199 | | */ |
200 | | union DigestCtx |
201 | | { |
202 | | #ifdef MHD_SUPPORT_SHA512_256 |
203 | | struct mhd_Sha512_256Ctx sha512_256_ctx; |
204 | | #endif /* MHD_SUPPORT_SHA512_256 */ |
205 | | #ifdef MHD_SUPPORT_SHA256 |
206 | | struct mhd_Sha256Ctx sha256_ctx; |
207 | | #endif /* MHD_SUPPORT_SHA256 */ |
208 | | #ifdef MHD_SUPPORT_MD5 |
209 | | struct mhd_Md5Ctx md5_ctx; |
210 | | #endif /* MHD_SUPPORT_MD5 */ |
211 | | }; |
212 | | |
213 | | mhd_DATA_TRUNCATION_RUNTIME_CHECK_DISABLE |
214 | | |
215 | | /** |
216 | | * Generate simple hash. |
217 | | * Very limited avalanche effect. To be used mainly for the table slot choice. |
218 | | * @param data_size the size of the data to hash |
219 | | * @param data the data to hash |
220 | | * @return the hash value |
221 | | */ |
222 | | static MHD_FN_PAR_NONNULL_ALL_ |
223 | | MHD_FN_PAR_IN_SIZE_ (2, 1) uint_fast64_t |
224 | | simple_hash (size_t data_size, |
225 | | const uint8_t *restrict data) |
226 | 0 | { |
227 | 0 | static const uint_fast64_t c[] = { /* Some fractional parts of Euler's number */ |
228 | 0 | UINT64_C (0xCC64D3484C3475A1), |
229 | 0 | UINT64_C (0xCF4DEBCB9ED801F2), |
230 | 0 | UINT64_C (0x0C8737A803CF46AD), |
231 | 0 | UINT64_C (0x294C9E0E0F9F14AB), |
232 | 0 | UINT64_C (0xAD786D855D4EBB1A) |
233 | 0 | }; |
234 | 0 | uint_fast64_t res; |
235 | 0 | size_t i; |
236 | |
|
237 | 0 | res = UINT64_C (0x8316A8FE31A2228E); /* Some fractional part of Pi */ |
238 | 0 | i = 0; |
239 | 0 | while (1) |
240 | 0 | { |
241 | 0 | uint_fast64_t a = 0; |
242 | |
|
243 | 0 | if (8 <= data_size) |
244 | 0 | memcpy (&a, data, 8); |
245 | 0 | else |
246 | 0 | memcpy (&a, data, data_size); |
247 | 0 | a ^= c[(i++) % mhd_ARR_NUM_ELEMS (c)]; |
248 | 0 | a = (uint_fast64_t)mhd_ROTR64 ((uint64_t)a, \ |
249 | 0 | (unsigned int)(res >> 58u)); |
250 | 0 | res ^= a; |
251 | 0 | if (8 >= data_size) |
252 | 0 | break; |
253 | 0 | data_size -= 8; |
254 | 0 | data += 8; |
255 | 0 | } |
256 | 0 | return res; |
257 | 0 | } |
258 | | |
259 | | |
260 | | /** |
261 | | * Find index of the provided nonce in the nonces table |
262 | | * @param nonce the nonce to use |
263 | | * @param arr_size the size of the nonces table |
264 | | * @return the index |
265 | | */ |
266 | | static MHD_FN_PAR_NONNULL_ALL_ size_t |
267 | | nonce_to_index (const uint8_t nonce[mhd_AUTH_DIGEST_NONCE_BIN_SIZE], |
268 | | size_t arr_size) |
269 | 0 | { |
270 | 0 | uint_fast64_t hash; |
271 | 0 | hash = simple_hash (mhd_AUTH_DIGEST_NONCE_BIN_SIZE, |
272 | 0 | nonce); |
273 | 0 | if (arr_size == (arr_size & UINT32_C (0xFFFFFFFF))) |
274 | 0 | { /* 'arr_size' <=32-bit */ |
275 | 0 | hash = (hash ^ (hash >> 32)) & UINT32_C (0xFFFFFFFF); /* Fold hash */ |
276 | 0 | if (arr_size == (arr_size & UINT16_C (0xFFFF))) |
277 | 0 | { /* 'arr_size' <=16-bit */ |
278 | 0 | hash = (hash ^ (hash >> 16)) & UINT16_C (0xFFFF); /* Fold hash */ |
279 | 0 | if (arr_size == (arr_size & 0xFFu)) |
280 | 0 | hash = (hash ^ (hash >> 8)) & 0xFFu; /* 'arr_size' <=8-bit, fold hash */ |
281 | 0 | } |
282 | 0 | } |
283 | 0 | return ((size_t)hash) % arr_size; |
284 | 0 | } |
285 | | |
286 | | |
287 | | mhd_DATA_TRUNCATION_RUNTIME_CHECK_RESTORE |
288 | | |
289 | | |
290 | | /** |
291 | | * Generate a new nonce |
292 | | * @param d the daemon to use (must match @a c connection) |
293 | | * @param c the connection to generate nonce for |
294 | | * @param[out] out_buf the output buffer to pull full nonce, including |
295 | | * "expiration" tail |
296 | | * @param[out] expir the expiration mark, duplicated for convenience |
297 | | * @return 'true' if succeed, |
298 | | * 'false' if failed |
299 | | */ |
300 | | static MHD_FN_PAR_NONNULL_ALL_ |
301 | | MHD_FN_PAR_OUT_ (3) |
302 | | MHD_FN_PAR_OUT_ (4) bool |
303 | | gen_new_nonce (struct MHD_Daemon *restrict d, |
304 | | struct MHD_Connection *restrict c, |
305 | | uint8_t out_buf[mhd_AUTH_DIGEST_NONCE_BIN_SIZE], |
306 | | uint_fast32_t *restrict expir) |
307 | 0 | { |
308 | 0 | uint_fast64_t expiration; |
309 | |
|
310 | 0 | mhd_assert (!mhd_D_HAS_MASTER (d)); /* only master daemon should be used */ |
311 | 0 | mhd_assert (d == c->daemon); |
312 | 0 | mhd_assert (0 != d->auth_dg.cfg.nonce_tmout); |
313 | |
|
314 | 0 | expiration = mhd_monotonic_msec_counter () |
315 | 0 | + d->auth_dg.cfg.nonce_tmout * (uint_fast64_t)1000; |
316 | |
|
317 | 0 | if (!mhd_rng (mhd_AUTH_DIGEST_NONCE_BIN_SIZE, |
318 | 0 | out_buf)) |
319 | 0 | { |
320 | | /* Fallback to generating nonce from application-provided |
321 | | entropy. Note: this should fail if we do not have |
322 | | application-provided entropy. */ |
323 | 0 | size_t gen_num; |
324 | 0 | union DigestCtx d_ctx; |
325 | |
|
326 | 0 | gen_num = mhd_atomic_counter_get_inc_wrap (&(d->auth_dg.num_gen_nonces)); |
327 | |
|
328 | 0 | #if defined(MHD_SUPPORT_SHA512_256) |
329 | 0 | mhd_SHA512_256_init (&(d_ctx.sha512_256_ctx)); |
330 | 0 | mhd_SHA512_256_update (&(d_ctx.sha512_256_ctx), |
331 | 0 | d->auth_dg.entropy.size, |
332 | 0 | (const uint8_t *)d->auth_dg.entropy.data); |
333 | 0 | mhd_SHA512_256_update (&(d_ctx.sha512_256_ctx), |
334 | 0 | sizeof(gen_num), |
335 | 0 | (const uint8_t *)&gen_num); |
336 | 0 | if (0 != c->sk.addr.size) |
337 | 0 | mhd_SHA512_256_update (&(d_ctx.sha512_256_ctx), |
338 | 0 | c->sk.addr.size, |
339 | 0 | (const uint8_t *)c->sk.addr.data); |
340 | 0 | mhd_SHA512_256_update (&(d_ctx.sha512_256_ctx), |
341 | 0 | sizeof(expiration), |
342 | 0 | (const uint8_t *)&expiration); |
343 | 0 | mhd_SHA512_256_finish_deinit (&(d_ctx.sha512_256_ctx), \ |
344 | 0 | out_buf); |
345 | 0 | if (mhd_SHA512_256_has_err (&(d_ctx.sha512_256_ctx))) |
346 | 0 | return false; |
347 | | #elif defined(MHD_SUPPORT_SHA256) |
348 | | mhd_SHA256_init (&(d_ctx.sha256_ctx)); |
349 | | mhd_SHA256_update (&(d_ctx.sha256_ctx), |
350 | | d->auth_dg.entropy.size, |
351 | | (const void *)d->auth_dg.entropy.data); |
352 | | mhd_SHA256_update (&(d_ctx.sha256_ctx), |
353 | | sizeof(gen_num), |
354 | | (const void *)&gen_num); |
355 | | if (0 != c->sk.addr.size) |
356 | | mhd_SHA256_update (&(d_ctx.sha256_ctx), |
357 | | c->sk.addr.size, |
358 | | (const void *)c->sk.addr.data); |
359 | | mhd_SHA256_update (&(d_ctx.sha256_ctx), |
360 | | sizeof(expiration), |
361 | | (const void *)&expiration); |
362 | | mhd_SHA256_finish_deinit (&(d_ctx.sha256_ctx), \ |
363 | | out_buf); |
364 | | if (mhd_SHA256_has_err (&(d_ctx.sha256_ctx))) |
365 | | return false; |
366 | | #else /* MHD_SUPPORT_MD5 */ |
367 | | # ifndef MHD_SUPPORT_MD5 |
368 | | # error At least one hashing algorithm must be enabled |
369 | | # endif |
370 | | mhd_MD5_init (&(d_ctx.md5_ctx)); |
371 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
372 | | d->auth_dg.entropy.size, |
373 | | (const void *)d->auth_dg.entropy.data); |
374 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
375 | | sizeof(gen_num), |
376 | | (const void *)&gen_num); |
377 | | if (0 != c->sk.addr.size) |
378 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
379 | | c->sk.addr.size, |
380 | | (const void *)c->sk.addr.data); |
381 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
382 | | sizeof(expiration), |
383 | | (const void *)&expiration); |
384 | | mhd_MD5_finish_deinit (&(d_ctx.md5_ctx), \ |
385 | | out_buf); |
386 | | if (mhd_MD5_has_err (&(d_ctx.md5_ctx))) |
387 | | return false; |
388 | | |
389 | | /* One more hash, for the second part */ |
390 | | gen_num = mhd_atomic_counter_get_inc_wrap (&(d->auth_dg.num_gen_nonces)); |
391 | | |
392 | | mhd_MD5_init (&(d_ctx.md5_ctx)); |
393 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
394 | | d->auth_dg.entropy.size, |
395 | | (const void *)d->auth_dg.entropy.data); |
396 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
397 | | sizeof(gen_num), |
398 | | (const void *)&gen_num); |
399 | | if (0 != c->sk.addr.size) |
400 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
401 | | c->sk.addr.size, |
402 | | (const void *)c->sk.addr.data); |
403 | | mhd_MD5_update (&(d_ctx.md5_ctx), |
404 | | sizeof(expiration), |
405 | | (const void *)&expiration); |
406 | | mhd_MD5_finish_deinit (&(d_ctx.md5_ctx), \ |
407 | | out_buf + mhd_MD5_DIGEST_SIZE); |
408 | | if (mhd_MD5_has_err (&(d_ctx.md5_ctx))) |
409 | | return false; |
410 | | #endif /* MHD_SUPPORT_MD5 */ |
411 | |
|
412 | 0 | } |
413 | | |
414 | 0 | *expir = (uint_fast32_t)(expiration / 1000u); |
415 | 0 | mhd_PUT_32BIT_LE_UNALIGN (out_buf + mhd_AUTH_DIGEST_NONCE_RAND_BIN_SIZE, \ |
416 | 0 | (uint32_t)(*expir & UINT32_C (0xFFFFFFFF))); |
417 | |
|
418 | 0 | return true; |
419 | 0 | } |
420 | | |
421 | | |
422 | | MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ |
423 | | MHD_FN_PAR_OUT_ (2) bool |
424 | | mhd_auth_digest_get_new_nonce (struct MHD_Connection *restrict c, |
425 | | char out_buf[mhd_AUTH_DIGEST_NONCE_LEN]) |
426 | 0 | { |
427 | 0 | static const int max_retries = 3; |
428 | 0 | struct MHD_Daemon *restrict d = mhd_daemon_get_master_daemon (c->daemon); |
429 | 0 | uint8_t nonce_bin[mhd_AUTH_DIGEST_NONCE_BIN_SIZE]; |
430 | 0 | uint_fast32_t expir; |
431 | 0 | bool nonce_generated; |
432 | 0 | int i; |
433 | |
|
434 | 0 | mhd_assert (0 != d->auth_dg.cfg.nonces_num); |
435 | 0 | mhd_assert (NULL != d->auth_dg.nonces); |
436 | |
|
437 | 0 | nonce_generated = false; |
438 | 0 | for (i = 0; i < max_retries; ++i) |
439 | 0 | { |
440 | 0 | bool good_nonce; |
441 | 0 | struct mhd_DaemonAuthDigestNonceData *nonce_slot; |
442 | 0 | if (!gen_new_nonce (d, |
443 | 0 | c, |
444 | 0 | nonce_bin, |
445 | 0 | &expir)) |
446 | 0 | continue; /* Failed, re-try */ |
447 | | |
448 | 0 | nonce_generated = true; |
449 | 0 | nonce_slot = d->auth_dg.nonces |
450 | 0 | + nonce_to_index (nonce_bin, |
451 | 0 | d->auth_dg.cfg.nonces_num); |
452 | 0 | if (!mhd_mutex_lock (&(d->auth_dg.nonces_lock))) |
453 | 0 | return false; /* Failure exit point */ |
454 | | /* Check whether the same nonce has been used before */ |
455 | 0 | good_nonce = (0 != memcmp (nonce_slot->nonce, |
456 | 0 | nonce_bin, |
457 | 0 | sizeof(nonce_slot->nonce))); |
458 | 0 | if (good_nonce) |
459 | 0 | { |
460 | 0 | memcpy (nonce_slot->nonce, |
461 | 0 | nonce_bin, |
462 | 0 | sizeof(nonce_slot->nonce)); |
463 | 0 | nonce_slot->valid_time = expir; |
464 | 0 | nonce_slot->max_recvd_nc = 0; |
465 | 0 | nonce_slot->nmask = 0; |
466 | 0 | } |
467 | 0 | else |
468 | 0 | { |
469 | | /* Check whether the same nonce has been used with different expiration |
470 | | time. */ |
471 | 0 | nonce_generated = (nonce_slot->valid_time == expir); |
472 | 0 | } |
473 | 0 | mhd_mutex_unlock_chk (&(d->auth_dg.nonces_lock)); |
474 | 0 | if (good_nonce) |
475 | 0 | break; |
476 | 0 | } |
477 | 0 | if (!nonce_generated) |
478 | 0 | return false; /* Failure exit point */ |
479 | | |
480 | | /* Use the generated nonce even if it is duplicated. |
481 | | One of the clients will just get "nonce stale" response with |
482 | | the new nonce. */ |
483 | 0 | (void)mhd_bin_to_hex (nonce_bin, |
484 | 0 | sizeof(nonce_bin), |
485 | 0 | out_buf); |
486 | 0 | return true; /* Success exit point */ |
487 | 0 | } |
488 | | |
489 | | |
490 | | /** |
491 | | * Get client's Digest Authorization algorithm type. |
492 | | * If no algorithm is specified by client, MD5 is assumed. |
493 | | * @param algo_param the Digest Authorization 'algorithm' parameter |
494 | | * @return the algorithm type |
495 | | */ |
496 | | static enum MHD_DigestAuthAlgo |
497 | | get_rq_dauth_algo (const struct mhd_RqDAuthParam *const algo_param) |
498 | 0 | { |
499 | 0 | if (NULL == algo_param->value.cstr) |
500 | 0 | return MHD_DIGEST_AUTH_ALGO_MD5; /* Assume MD5 by default */ |
501 | | |
502 | 0 | if (algo_param->quoted) |
503 | 0 | { |
504 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
505 | 0 | algo_param->value.len, \ |
506 | 0 | mhd_MD5_TOKEN)) |
507 | 0 | return MHD_DIGEST_AUTH_ALGO_MD5; |
508 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
509 | 0 | algo_param->value.len, \ |
510 | 0 | mhd_SHA256_TOKEN)) |
511 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA256; |
512 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
513 | 0 | algo_param->value.len, \ |
514 | 0 | mhd_SHA512_256_TOKEN)) |
515 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA512_256; |
516 | | |
517 | | /* Algorithms below are not supported by MHD for authentication */ |
518 | | |
519 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
520 | 0 | algo_param->value.len, \ |
521 | 0 | mhd_MD5_TOKEN mhd_SESS_TOKEN)) |
522 | 0 | return MHD_DIGEST_AUTH_ALGO_MD5_SESSION; |
523 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
524 | 0 | algo_param->value.len, \ |
525 | 0 | mhd_SHA256_TOKEN \ |
526 | 0 | mhd_SESS_TOKEN)) |
527 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA256_SESSION; |
528 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (algo_param->value.cstr, \ |
529 | 0 | algo_param->value.len, \ |
530 | 0 | mhd_SHA512_256_TOKEN \ |
531 | 0 | mhd_SESS_TOKEN)) |
532 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA512_256_SESSION; |
533 | | |
534 | | /* No known algorithm has been detected */ |
535 | 0 | return MHD_DIGEST_AUTH_ALGO_INVALID; |
536 | 0 | } |
537 | | /* The algorithm value is not quoted */ |
538 | 0 | if (mhd_str_equal_caseless_n_st (mhd_MD5_TOKEN, \ |
539 | 0 | algo_param->value.cstr, \ |
540 | 0 | algo_param->value.len)) |
541 | 0 | return MHD_DIGEST_AUTH_ALGO_MD5; |
542 | 0 | if (mhd_str_equal_caseless_n_st (mhd_SHA256_TOKEN, \ |
543 | 0 | algo_param->value.cstr, \ |
544 | 0 | algo_param->value.len)) |
545 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA256; |
546 | 0 | if (mhd_str_equal_caseless_n_st (mhd_SHA512_256_TOKEN, \ |
547 | 0 | algo_param->value.cstr, \ |
548 | 0 | algo_param->value.len)) |
549 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA512_256; |
550 | | |
551 | | /* Algorithms below are not supported by MHD for authentication */ |
552 | | |
553 | 0 | if (mhd_str_equal_caseless_n_st (mhd_MD5_TOKEN mhd_SESS_TOKEN, \ |
554 | 0 | algo_param->value.cstr, \ |
555 | 0 | algo_param->value.len)) |
556 | 0 | return MHD_DIGEST_AUTH_ALGO_MD5_SESSION; |
557 | 0 | if (mhd_str_equal_caseless_n_st (mhd_SHA256_TOKEN mhd_SESS_TOKEN, \ |
558 | 0 | algo_param->value.cstr, \ |
559 | 0 | algo_param->value.len)) |
560 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA256_SESSION; |
561 | 0 | if (mhd_str_equal_caseless_n_st (mhd_SHA512_256_TOKEN mhd_SESS_TOKEN, \ |
562 | 0 | algo_param->value.cstr, \ |
563 | 0 | algo_param->value.len)) |
564 | 0 | return MHD_DIGEST_AUTH_ALGO_SHA512_256_SESSION; |
565 | | |
566 | | /* No known algorithm has been detected */ |
567 | 0 | return MHD_DIGEST_AUTH_ALGO_INVALID; |
568 | 0 | } |
569 | | |
570 | | |
571 | | /** |
572 | | * Get QOP ('quality of protection') type. |
573 | | * @param qop_param the Digest Authorization 'QOP' parameter |
574 | | * @return detected QOP ('quality of protection') type. |
575 | | */ |
576 | | static enum MHD_DigestAuthQOP |
577 | | get_rq_dauth_qop (const struct mhd_RqDAuthParam *const qop_param) |
578 | 0 | { |
579 | 0 | if (NULL == qop_param->value.cstr) |
580 | 0 | return MHD_DIGEST_AUTH_QOP_NONE; |
581 | 0 | if (qop_param->quoted) |
582 | 0 | { |
583 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (qop_param->value.cstr, \ |
584 | 0 | qop_param->value.len, \ |
585 | 0 | mhd_TOKEN_AUTH)) |
586 | 0 | return MHD_DIGEST_AUTH_QOP_AUTH; |
587 | 0 | if (mhd_str_equal_caseless_quoted_s_bin_n (qop_param->value.cstr, \ |
588 | 0 | qop_param->value.len, \ |
589 | 0 | mhd_TOKEN_AUTH_INT)) |
590 | 0 | return MHD_DIGEST_AUTH_QOP_AUTH_INT; |
591 | 0 | } |
592 | 0 | else |
593 | 0 | { |
594 | 0 | if (mhd_str_equal_caseless_n_st (mhd_TOKEN_AUTH, \ |
595 | 0 | qop_param->value.cstr, \ |
596 | 0 | qop_param->value.len)) |
597 | 0 | return MHD_DIGEST_AUTH_QOP_AUTH; |
598 | 0 | if (mhd_str_equal_caseless_n_st (mhd_TOKEN_AUTH_INT, \ |
599 | 0 | qop_param->value.cstr, \ |
600 | 0 | qop_param->value.len)) |
601 | 0 | return MHD_DIGEST_AUTH_QOP_AUTH_INT; |
602 | 0 | } |
603 | | /* No know QOP has been detected */ |
604 | 0 | return MHD_DIGEST_AUTH_QOP_INVALID; |
605 | 0 | } |
606 | | |
607 | | |
608 | | /** |
609 | | * Parse request Authorization header parameters for Digest Authentication |
610 | | * @param val the header string, everything after "Digest " substring |
611 | | * @param[out] pdauth the pointer to the structure with Digest Authentication |
612 | | * parameters |
613 | | * @return true if parameters has been successfully parsed, |
614 | | * false if format of the @a str is invalid |
615 | | */ |
616 | | static MHD_FN_PAR_NONNULL_ALL_ |
617 | | MHD_FN_PAR_OUT_ (2) bool |
618 | | parse_dauth_params (const struct MHD_String *restrict val, |
619 | | struct mhd_AuthDigesReqParams *restrict pdauth) |
620 | 0 | { |
621 | | /* The tokens */ |
622 | 0 | static const struct MHD_String nonce_tk = mhd_MSTR_INIT ("nonce"); |
623 | 0 | static const struct MHD_String opaque_tk = mhd_MSTR_INIT ("opaque"); |
624 | 0 | static const struct MHD_String algorithm_tk = mhd_MSTR_INIT ("algorithm"); |
625 | 0 | static const struct MHD_String response_tk = mhd_MSTR_INIT ("response"); |
626 | 0 | static const struct MHD_String username_tk = mhd_MSTR_INIT ("username"); |
627 | 0 | static const struct MHD_String username_ext_tk = mhd_MSTR_INIT ("username*"); |
628 | 0 | static const struct MHD_String realm_tk = mhd_MSTR_INIT ("realm"); |
629 | 0 | static const struct MHD_String uri_tk = mhd_MSTR_INIT ("uri"); |
630 | 0 | static const struct MHD_String qop_tk = mhd_MSTR_INIT ("qop"); |
631 | 0 | static const struct MHD_String cnonce_tk = mhd_MSTR_INIT ("cnonce"); |
632 | 0 | static const struct MHD_String nc_tk = mhd_MSTR_INIT ("nc"); |
633 | 0 | static const struct MHD_String userhash_tk = mhd_MSTR_INIT ("userhash"); |
634 | | /* The locally processed parameters */ |
635 | 0 | struct mhd_RqDAuthParam userhash = { {0, NULL}, false}; |
636 | 0 | struct mhd_RqDAuthParam algorithm = { {0, NULL}, false}; |
637 | | /* Indexes */ |
638 | 0 | size_t i; |
639 | 0 | size_t p; |
640 | | /* The list of the tokens. |
641 | | The order of the elements matches the next array. */ |
642 | 0 | static const struct MHD_String *const tk_names[] = { |
643 | 0 | &nonce_tk, /* 0 */ |
644 | 0 | &opaque_tk, /* 1 */ |
645 | 0 | &algorithm_tk, /* 2 */ |
646 | 0 | &response_tk, /* 3 */ |
647 | 0 | &username_tk, /* 4 */ |
648 | 0 | &username_ext_tk, /* 5 */ |
649 | 0 | &realm_tk, /* 6 */ |
650 | 0 | &uri_tk, /* 7 */ |
651 | 0 | &qop_tk, /* 8 */ |
652 | 0 | &cnonce_tk, /* 9 */ |
653 | 0 | &nc_tk, /* 10 */ |
654 | 0 | &userhash_tk /* 11 */ |
655 | 0 | }; |
656 | | /* The list of the parameters. |
657 | | The order of the elements matches the previous array. */ |
658 | 0 | struct mhd_RqDAuthParam *params[sizeof(tk_names) / sizeof(tk_names[0])]; |
659 | |
|
660 | 0 | params[0] = &(pdauth->nonce); /* 0 */ |
661 | 0 | params[1] = &(pdauth->opaque); /* 1 */ |
662 | 0 | params[2] = &algorithm; /* 2 */ |
663 | 0 | params[3] = &(pdauth->response); /* 3 */ |
664 | 0 | params[4] = &(pdauth->username); /* 4 */ |
665 | 0 | params[5] = &(pdauth->username_ext); /* 5 */ |
666 | 0 | params[6] = &(pdauth->realm); /* 6 */ |
667 | 0 | params[7] = &(pdauth->uri); /* 7 */ |
668 | 0 | params[8] = &(pdauth->qop_raw); /* 8 */ |
669 | 0 | params[9] = &(pdauth->cnonce); /* 9 */ |
670 | 0 | params[10] = &(pdauth->nc); /* 10 */ |
671 | 0 | params[11] = &userhash; /* 11 */ |
672 | |
|
673 | 0 | mhd_assert (mhd_ARR_NUM_ELEMS (tk_names) == \ |
674 | 0 | mhd_ARR_NUM_ELEMS (params)); |
675 | 0 | i = 0; |
676 | |
|
677 | 0 | mhd_assert (' ' != val->cstr[0]); |
678 | 0 | mhd_assert ('\t' != val->cstr[0]); |
679 | |
|
680 | 0 | while (val->len > i) |
681 | 0 | { |
682 | 0 | size_t left; |
683 | 0 | mhd_assert (' ' != val->cstr[i]); |
684 | 0 | mhd_assert ('\t' != val->cstr[i]); |
685 | |
|
686 | 0 | left = val->len - i; |
687 | 0 | if ('=' == val->cstr[i]) |
688 | 0 | return false; /* The equal sign is not allowed as the first character */ |
689 | 0 | for (p = 0; p < mhd_ARR_NUM_ELEMS (tk_names); ++p) |
690 | 0 | { |
691 | 0 | const struct MHD_String *const tk_name = tk_names[p]; |
692 | 0 | struct mhd_RqDAuthParam *const param = params[p]; |
693 | 0 | if ((tk_name->len <= left) |
694 | 0 | && mhd_str_equal_caseless_bin_n (val->cstr + i, tk_name->cstr, |
695 | 0 | tk_name->len) |
696 | 0 | && ((tk_name->len == left) |
697 | 0 | || ('=' == val->cstr[i + tk_name->len]) |
698 | 0 | || (' ' == val->cstr[i + tk_name->len]) |
699 | 0 | || ('\t' == val->cstr[i + tk_name->len]) |
700 | 0 | || (',' == val->cstr[i + tk_name->len]) |
701 | 0 | || (';' == val->cstr[i + tk_name->len]))) |
702 | 0 | { |
703 | 0 | size_t value_start; |
704 | 0 | size_t value_len; |
705 | 0 | bool quoted; /* Only mark as "quoted" if backslash-escape used */ |
706 | |
|
707 | 0 | if (tk_name->len == left) |
708 | 0 | return false; /* No equal sign after parameter name, broken data */ |
709 | | |
710 | 0 | quoted = false; |
711 | 0 | i += tk_name->len; |
712 | | /* Skip all whitespaces before '=' */ |
713 | 0 | while (val->len > i && (' ' == val->cstr[i] || '\t' == val->cstr[i])) |
714 | 0 | i++; |
715 | 0 | if ((i == val->len) || ('=' != val->cstr[i])) |
716 | 0 | return false; /* No equal sign, broken data */ |
717 | 0 | i++; |
718 | | /* Skip all whitespaces after '=' */ |
719 | 0 | while (val->len > i && (' ' == val->cstr[i] || '\t' == val->cstr[i])) |
720 | 0 | i++; |
721 | 0 | if ((val->len > i) && ('"' == val->cstr[i])) |
722 | 0 | { /* Value is in quotation marks */ |
723 | 0 | i++; /* Advance after the opening quote */ |
724 | 0 | value_start = i; |
725 | 0 | while (val->len > i && '"' != val->cstr[i]) |
726 | 0 | { |
727 | 0 | if ('\\' == val->cstr[i]) |
728 | 0 | { |
729 | 0 | i++; |
730 | 0 | quoted = true; /* Have escaped chars */ |
731 | 0 | } |
732 | 0 | if (0 == val->cstr[i]) |
733 | 0 | return false; /* Binary zero in parameter value */ |
734 | 0 | i++; |
735 | 0 | } |
736 | 0 | if (val->len <= i) |
737 | 0 | return false; /* No closing quote */ |
738 | 0 | mhd_assert ('"' == val->cstr[i]); |
739 | 0 | value_len = i - value_start; |
740 | 0 | i++; /* Advance after the closing quote */ |
741 | 0 | } |
742 | 0 | else |
743 | 0 | { |
744 | 0 | value_start = i; |
745 | 0 | while ((val->len > i) && (',' != val->cstr[i]) |
746 | 0 | && (' ' != val->cstr[i]) && ('\t' != val->cstr[i]) |
747 | 0 | && (';' != val->cstr[i])) |
748 | 0 | { |
749 | 0 | if (0 == val->cstr[i]) |
750 | 0 | return false; /* Binary zero in parameter value */ |
751 | 0 | i++; |
752 | 0 | } |
753 | 0 | if (';' == val->cstr[i]) |
754 | 0 | return false; /* Semicolon in parameter value */ |
755 | 0 | value_len = i - value_start; |
756 | 0 | } |
757 | | /* Skip all whitespaces after parameter value */ |
758 | 0 | while (val->len > i && (' ' == val->cstr[i] || '\t' == val->cstr[i])) |
759 | 0 | i++; |
760 | 0 | if ((val->len > i) && (',' != val->cstr[i])) |
761 | 0 | return false; /* Garbage after parameter value */ |
762 | | |
763 | | /* Have valid parameter name and value */ |
764 | 0 | mhd_assert (!quoted || 0 != value_len); |
765 | 0 | param->value.cstr = val->cstr + value_start; |
766 | 0 | param->value.len = value_len; |
767 | 0 | param->quoted = quoted; |
768 | |
|
769 | 0 | break; /* Found matching parameter name */ |
770 | 0 | } |
771 | 0 | } |
772 | 0 | if (p == mhd_ARR_NUM_ELEMS (tk_names)) |
773 | 0 | { |
774 | | /* No matching parameter name */ |
775 | 0 | while (val->len > i && ',' != val->cstr[i]) |
776 | 0 | { |
777 | 0 | if ((0 == val->cstr[i]) || (';' == val->cstr[i])) |
778 | 0 | return false; /* Not allowed characters */ |
779 | 0 | if ('"' == val->cstr[i]) |
780 | 0 | { /* Skip quoted part */ |
781 | 0 | i++; /* Advance after the opening quote */ |
782 | 0 | while (val->len > i && '"' != val->cstr[i]) |
783 | 0 | { |
784 | 0 | if (0 == val->cstr[i]) |
785 | 0 | return false; /* Binary zero is not allowed */ |
786 | 0 | if ('\\' == val->cstr[i]) |
787 | 0 | i++; /* Skip escaped char */ |
788 | 0 | i++; |
789 | 0 | } |
790 | 0 | if (val->len <= i) |
791 | 0 | return false; /* No closing quote */ |
792 | 0 | mhd_assert ('"' == val->cstr[i]); |
793 | 0 | } |
794 | 0 | i++; |
795 | 0 | } |
796 | 0 | } |
797 | 0 | mhd_assert (val->len == i || ',' == val->cstr[i]); |
798 | 0 | if (val->len > i) |
799 | 0 | i++; /* Advance after ',' */ |
800 | | /* Skip all whitespaces before next parameter name */ |
801 | 0 | while (i < val->len && (' ' == val->cstr[i] || '\t' == val->cstr[i])) |
802 | 0 | i++; |
803 | 0 | } |
804 | | |
805 | | /* Postprocess values */ |
806 | | |
807 | 0 | if (NULL != userhash.value.cstr) |
808 | 0 | { |
809 | 0 | if (userhash.quoted) |
810 | 0 | pdauth->userhash = |
811 | 0 | mhd_str_equal_caseless_quoted_s_bin_n (userhash.value.cstr, \ |
812 | 0 | userhash.value.len, \ |
813 | 0 | "true"); |
814 | 0 | else |
815 | 0 | pdauth->userhash = |
816 | 0 | mhd_str_equal_caseless_n_st ("true", userhash.value.cstr, \ |
817 | 0 | userhash.value.len); |
818 | |
|
819 | 0 | } |
820 | 0 | else |
821 | 0 | pdauth->userhash = false; |
822 | |
|
823 | 0 | pdauth->algo = get_rq_dauth_algo (&algorithm); |
824 | 0 | pdauth->qop = get_rq_dauth_qop (&pdauth->qop_raw); |
825 | |
|
826 | 0 | return true; |
827 | 0 | } |
828 | | |
829 | | |
830 | | /** |
831 | | * Find and pre-parse request's Digest Authorisation parameters. |
832 | | * |
833 | | * Function returns result of pre-parsing of the request's "Authorization" |
834 | | * header or returns cached result if the header has been already pre-parsed for |
835 | | * the current request. |
836 | | * @param req the request to process |
837 | | * @return #MHD_SC_OK if succeed, |
838 | | * #MHD_SC_AUTH_ABSENT if request has no Digest Authorisation, |
839 | | * #MHD_SC_CONNECTION_POOL_NO_MEM_AUTH_DATA if not enough memory, |
840 | | * #MHD_SC_REQ_AUTH_DATA_BROKEN if the header is broken. |
841 | | */ |
842 | | static MHD_FN_PAR_NONNULL_ALL_ enum MHD_StatusCode |
843 | | get_rq_auth_digest_params (struct MHD_Request *restrict req) |
844 | 0 | { |
845 | 0 | struct MHD_String h_auth_value; |
846 | 0 | struct mhd_AuthDigesReqParams *dauth; |
847 | |
|
848 | 0 | mhd_assert (mhd_HTTP_STAGE_HEADERS_PROCESSED <= \ |
849 | 0 | mhd_CNTNR_CPTR (req, struct MHD_Connection, rq)->stage); |
850 | 0 | mhd_assert (mhd_HTTP_STAGE_REQ_RECV_FINISHED >= \ |
851 | 0 | mhd_CNTNR_CPTR (req, struct MHD_Connection, rq)->stage); |
852 | |
|
853 | 0 | if (NULL != req->auth.digest.rqp) |
854 | 0 | return MHD_SC_OK; |
855 | | |
856 | 0 | if (!mhd_request_get_auth_header_value (req, |
857 | 0 | mhd_AUTH_HDR_DIGEST, |
858 | 0 | &h_auth_value)) |
859 | 0 | return MHD_SC_AUTH_ABSENT; |
860 | | |
861 | 0 | dauth = |
862 | 0 | (struct mhd_AuthDigesReqParams *) |
863 | 0 | mhd_stream_alloc_memory (mhd_CNTNR_PTR (req, \ |
864 | 0 | struct MHD_Connection, \ |
865 | 0 | rq), |
866 | 0 | sizeof (struct mhd_AuthDigesReqParams)); |
867 | |
|
868 | 0 | if (NULL == dauth) |
869 | 0 | return MHD_SC_CONNECTION_POOL_NO_MEM_AUTH_DATA; |
870 | | |
871 | 0 | memset (dauth, 0, sizeof(struct mhd_AuthDigesReqParams)); |
872 | | #ifndef HAVE_NULL_PTR_ALL_ZEROS |
873 | | dauth->nonce.value.cstr = NULL; |
874 | | dauth->opaque.value.cstr = NULL; |
875 | | dauth->response.value.cstr = NULL; |
876 | | dauth->username.value.cstr = NULL; |
877 | | dauth->username_ext.value.cstr = NULL; |
878 | | dauth->realm.value.cstr = NULL; |
879 | | dauth->uri.value.cstr = NULL; |
880 | | dauth->qop_raw.value.cstr = NULL; |
881 | | dauth->cnonce.value.cstr = NULL; |
882 | | dauth->nc.value.cstr = NULL; |
883 | | #endif /* ! HAVE_NULL_PTR_ALL_ZEROS */ |
884 | 0 | if (!parse_dauth_params (&h_auth_value, |
885 | 0 | dauth)) |
886 | 0 | return MHD_SC_REQ_AUTH_DATA_BROKEN; |
887 | | |
888 | 0 | req->auth.digest.rqp = dauth; |
889 | |
|
890 | 0 | return MHD_SC_OK; |
891 | 0 | } |
892 | | |
893 | | |
894 | | /** |
895 | | * Get username type used by the client. |
896 | | * This function does not check whether userhash can be decoded or |
897 | | * extended notation (if used) is valid. |
898 | | * @param params the Digest Authorization parameters |
899 | | * @return the type of username |
900 | | */ |
901 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ enum MHD_DigestAuthUsernameType |
902 | | get_rq_uname_type (const struct mhd_AuthDigesReqParams *params) |
903 | 0 | { |
904 | 0 | if (NULL != params->username.value.cstr) |
905 | 0 | { |
906 | 0 | if (NULL == params->username_ext.value.cstr) |
907 | 0 | return params->userhash ? |
908 | 0 | MHD_DIGEST_AUTH_UNAME_TYPE_USERHASH : |
909 | 0 | MHD_DIGEST_AUTH_UNAME_TYPE_STANDARD; |
910 | 0 | else /* Both 'username' and 'username*' are used */ |
911 | 0 | return MHD_DIGEST_AUTH_UNAME_TYPE_INVALID; |
912 | 0 | } |
913 | 0 | else if (NULL != params->username_ext.value.cstr) |
914 | 0 | { |
915 | 0 | if (!params->username_ext.quoted && !params->userhash |
916 | 0 | && (mhd_DAUTH_EXT_PARAM_MIN_LEN <= params->username_ext.value.len)) |
917 | 0 | return MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED; |
918 | 0 | else |
919 | 0 | return MHD_DIGEST_AUTH_UNAME_TYPE_INVALID; |
920 | 0 | } |
921 | | |
922 | 0 | return MHD_DIGEST_AUTH_UNAME_TYPE_MISSING; |
923 | 0 | } |
924 | | |
925 | | |
926 | | /** |
927 | | * Get total size required for 'username' and 'userhash_bin' |
928 | | * @param params the Digest Authorization parameters |
929 | | * @param uname_type the type of username |
930 | | * @return the total size required for 'username' and |
931 | | * 'userhash_bin' is userhash is used |
932 | | */ |
933 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ size_t |
934 | | get_rq_unames_size (const struct mhd_AuthDigesReqParams *params, |
935 | | enum MHD_DigestAuthUsernameType uname_type) |
936 | 0 | { |
937 | 0 | size_t s; |
938 | |
|
939 | 0 | mhd_assert (get_rq_uname_type (params) == uname_type); |
940 | 0 | s = 0; |
941 | 0 | if ((MHD_DIGEST_AUTH_UNAME_TYPE_STANDARD == uname_type) |
942 | 0 | || (MHD_DIGEST_AUTH_UNAME_TYPE_USERHASH == uname_type)) |
943 | 0 | { |
944 | 0 | s += params->username.value.len + 1; /* Add one byte for zero-termination */ |
945 | 0 | if (MHD_DIGEST_AUTH_UNAME_TYPE_USERHASH == uname_type) |
946 | 0 | s += (params->username.value.len + 1) / 2; |
947 | 0 | } |
948 | 0 | else if (MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED == uname_type) |
949 | 0 | s += params->username_ext.value.len |
950 | 0 | - mhd_DAUTH_EXT_PARAM_MIN_LEN + 1; /* Add one byte for zero-termination */ |
951 | 0 | return s; |
952 | 0 | } |
953 | | |
954 | | |
955 | | /** |
956 | | * Get unquoted version of Digest Authorization parameter. |
957 | | * This function automatically zero-teminate the result. |
958 | | * @param param the parameter to extract |
959 | | * @param[out] buf the output buffer, must have enough size to hold the result, |
960 | | * the recommended size is 'param->value.len + 1' |
961 | | * @return the size of the result, not including the terminating zero |
962 | | */ |
963 | | static MHD_FN_PAR_NONNULL_ALL_ |
964 | | MHD_FN_PAR_OUT_ (2) size_t |
965 | | get_rq_param_unquoted_copy_z (const struct mhd_RqDAuthParam *restrict param, |
966 | | char *restrict buf) |
967 | 0 | { |
968 | 0 | size_t len; |
969 | 0 | mhd_assert (NULL != param->value.cstr); |
970 | 0 | if (!param->quoted) |
971 | 0 | { |
972 | 0 | memcpy (buf, param->value.cstr, param->value.len); |
973 | 0 | buf[param->value.len] = 0; |
974 | 0 | return param->value.len; |
975 | 0 | } |
976 | | |
977 | 0 | len = mhd_str_unquote (param->value.cstr, param->value.len, buf); |
978 | 0 | mhd_assert (0 != len); |
979 | 0 | mhd_assert (len < param->value.len); |
980 | 0 | buf[len] = 0; |
981 | 0 | return len; |
982 | 0 | } |
983 | | |
984 | | |
985 | | /** |
986 | | * Get decoded version of username from extended notation. |
987 | | * This function automatically zero-teminate the result. |
988 | | * @param uname_ext the string of client's 'username*' parameter value |
989 | | * @param uname_ext_len the length of @a uname_ext in chars |
990 | | * @param[out] buf the output buffer to put decoded username value |
991 | | * @param buf_size the size of @a buf |
992 | | * @return the number of characters copied to the output buffer or |
993 | | * -1 if wrong extended notation is used. |
994 | | */ |
995 | | static MHD_FN_PAR_NONNULL_ALL_ |
996 | | MHD_FN_PAR_IN_SIZE_ (1, 2) |
997 | | MHD_FN_PAR_OUT_SIZE_ (3, 4) ssize_t |
998 | | get_rq_extended_uname_copy_z (const char *restrict uname_ext, |
999 | | size_t uname_ext_len, |
1000 | | char *restrict buf, |
1001 | | size_t buf_size) |
1002 | 0 | { |
1003 | 0 | size_t r; |
1004 | 0 | size_t w; |
1005 | 0 | if ((size_t)SSIZE_MAX < uname_ext_len) |
1006 | 0 | return -1; /* Too long input string */ |
1007 | | |
1008 | 0 | if (mhd_DAUTH_EXT_PARAM_MIN_LEN > uname_ext_len) |
1009 | 0 | return -1; /* Required prefix is missing */ |
1010 | | |
1011 | 0 | if (!mhd_str_equal_caseless_bin_n ( |
1012 | 0 | uname_ext, |
1013 | 0 | mhd_DAUTH_EXT_PARAM_PREFIX, |
1014 | 0 | mhd_SSTR_LEN (mhd_DAUTH_EXT_PARAM_PREFIX))) |
1015 | 0 | return -1; /* Only UTF-8 is supported, as it is implied by RFC 7616 */ |
1016 | | |
1017 | 0 | r = mhd_SSTR_LEN (mhd_DAUTH_EXT_PARAM_PREFIX); |
1018 | | /* Skip language tag */ |
1019 | 0 | while (r < uname_ext_len && '\'' != uname_ext[r]) |
1020 | 0 | { |
1021 | 0 | const char chr = uname_ext[r]; |
1022 | 0 | if ((' ' == chr) || ('\t' == chr) || ('\"' == chr) || (',' == chr) |
1023 | 0 | || (';' == chr)) |
1024 | 0 | return -1; /* Wrong char in language tag */ |
1025 | 0 | r++; |
1026 | 0 | } |
1027 | 0 | if (r >= uname_ext_len) |
1028 | 0 | return -1; /* The end of the language tag was not found */ |
1029 | 0 | r++; /* Advance to the next char */ |
1030 | |
|
1031 | 0 | w = mhd_str_pct_decode_strict_n (uname_ext + r, uname_ext_len - r, |
1032 | 0 | buf, buf_size); |
1033 | 0 | if ((0 == w) && (0 != uname_ext_len - r)) |
1034 | 0 | return -1; /* Broken percent encoding */ |
1035 | 0 | buf[w] = 0; /* Zero terminate the result */ |
1036 | 0 | mhd_assert (SSIZE_MAX > w); |
1037 | 0 | return (ssize_t)w; |
1038 | 0 | } |
1039 | | |
1040 | | |
1041 | | /** |
1042 | | * Get copy of username used by the client. |
1043 | | * @param params the Digest Authorization parameters |
1044 | | * @param uname_type the type of username |
1045 | | * @param[out] uname_info the pointer to the structure to be filled |
1046 | | * @param buf the buffer to be used for usernames |
1047 | | * @param buf_size the size of the @a buf |
1048 | | * @return the size of the @a buf used by pointers in @a unames structure |
1049 | | */ |
1050 | | static size_t |
1051 | | get_rq_uname (const struct mhd_AuthDigesReqParams *restrict params, |
1052 | | enum MHD_DigestAuthUsernameType uname_type, |
1053 | | struct MHD_AuthDigestInfo *restrict uname_info, |
1054 | | uint8_t *restrict buf, |
1055 | | size_t buf_size) |
1056 | 0 | { |
1057 | 0 | size_t buf_used; |
1058 | |
|
1059 | 0 | buf_used = 0; |
1060 | 0 | mhd_assert (get_rq_uname_type (params) == uname_type); |
1061 | 0 | mhd_assert (MHD_DIGEST_AUTH_UNAME_TYPE_INVALID != uname_type); |
1062 | 0 | mhd_assert (MHD_DIGEST_AUTH_UNAME_TYPE_MISSING != uname_type); |
1063 | |
|
1064 | 0 | uname_info->username.cstr = NULL; |
1065 | 0 | uname_info->username.len = 0; |
1066 | 0 | uname_info->userhash_hex.cstr = NULL; |
1067 | 0 | uname_info->userhash_hex.len = 0; |
1068 | 0 | uname_info->userhash_bin = NULL; |
1069 | |
|
1070 | 0 | if (MHD_DIGEST_AUTH_UNAME_TYPE_STANDARD == uname_type) |
1071 | 0 | { |
1072 | | // TODO: Avoid copying string if not quoted |
1073 | 0 | uname_info->username.cstr = (char *)(buf + buf_used); |
1074 | 0 | uname_info->username.len = |
1075 | 0 | get_rq_param_unquoted_copy_z (¶ms->username, |
1076 | 0 | (char *)(buf + buf_used)); |
1077 | 0 | buf_used += uname_info->username.len + 1; |
1078 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_STANDARD; |
1079 | 0 | } |
1080 | 0 | else if (MHD_DIGEST_AUTH_UNAME_TYPE_USERHASH == uname_type) |
1081 | 0 | { |
1082 | 0 | size_t res; |
1083 | |
|
1084 | 0 | uname_info->userhash_hex.cstr = (char *)(buf + buf_used); |
1085 | 0 | uname_info->userhash_hex.len = |
1086 | 0 | get_rq_param_unquoted_copy_z (¶ms->username, |
1087 | 0 | (char *)(buf + buf_used)); |
1088 | 0 | buf_used += uname_info->userhash_hex.len + 1; |
1089 | 0 | uname_info->userhash_bin = (uint8_t *)(buf + buf_used); |
1090 | 0 | res = mhd_hex_to_bin (uname_info->userhash_hex.cstr, |
1091 | 0 | uname_info->userhash_hex.len, |
1092 | 0 | (uint8_t *)(buf + buf_used)); |
1093 | 0 | if (res != uname_info->userhash_hex.len / 2) |
1094 | 0 | { |
1095 | 0 | uname_info->userhash_bin = NULL; |
1096 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_INVALID; |
1097 | 0 | } |
1098 | 0 | else |
1099 | 0 | { |
1100 | | /* Avoid pointers outside allocated region when the size is zero */ |
1101 | 0 | if (0 == res) |
1102 | 0 | uname_info->userhash_bin = (const uint8_t *)uname_info->username.cstr; |
1103 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_USERHASH; |
1104 | 0 | buf_used += res; |
1105 | 0 | } |
1106 | 0 | } |
1107 | 0 | else if (MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED == uname_type) |
1108 | 0 | { |
1109 | 0 | ssize_t res; |
1110 | 0 | res = get_rq_extended_uname_copy_z (params->username_ext.value.cstr, |
1111 | 0 | params->username_ext.value.len, |
1112 | 0 | (char *)(buf + buf_used), |
1113 | 0 | buf_size - buf_used); |
1114 | 0 | if (0 > res) |
1115 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_INVALID; |
1116 | 0 | else |
1117 | 0 | { |
1118 | 0 | uname_info->username.cstr = (char *)(buf + buf_used); |
1119 | 0 | uname_info->username.len = (size_t)res; |
1120 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED; |
1121 | 0 | buf_used += uname_info->username.len + 1; |
1122 | 0 | } |
1123 | 0 | } |
1124 | 0 | else |
1125 | 0 | { |
1126 | 0 | mhd_assert (0); |
1127 | 0 | uname_info->uname_type = MHD_DIGEST_AUTH_UNAME_TYPE_INVALID; |
1128 | 0 | } |
1129 | 0 | mhd_assert (buf_size >= buf_used); |
1130 | 0 | return buf_used; |
1131 | 0 | } |
1132 | | |
1133 | | |
1134 | | /** |
1135 | | * Result of request's Digest Authorization 'nc' value extraction |
1136 | | */ |
1137 | | enum MHD_FIXED_ENUM_ mhd_GetRqNCResult |
1138 | | { |
1139 | | mhd_GET_RQ_NC_NONE = MHD_DIGEST_AUTH_NC_NONE, /**< No 'nc' value */ |
1140 | | mhd_GET_RQ_NC_VALID = MHD_DIGEST_AUTH_NC_NUMBER, /**< Readable 'nc' value */ |
1141 | | mhd_GET_RQ_NC_TOO_LONG = MHD_DIGEST_AUTH_NC_TOO_LONG, /**< The 'nc' value is too long */ |
1142 | | mhd_GET_RQ_NC_TOO_LARGE = MHD_DIGEST_AUTH_NC_TOO_LARGE, /**< The 'nc' value is too big to fit uint32_t */ |
1143 | | mhd_GET_RQ_NC_BROKEN = 0 /**< The 'nc' value is not a number */ |
1144 | | }; |
1145 | | |
1146 | | |
1147 | | /** |
1148 | | * Get 'nc' value from request's Authorization header |
1149 | | * @param params the request digest authentication |
1150 | | * @param[out] nc the pointer to put nc value to |
1151 | | * @return enum value indicating the result |
1152 | | */ |
1153 | | static enum mhd_GetRqNCResult |
1154 | | get_rq_nc (const struct mhd_AuthDigesReqParams *params, |
1155 | | uint_fast32_t *nc) |
1156 | 0 | { |
1157 | 0 | const struct mhd_RqDAuthParam *const nc_param = |
1158 | 0 | ¶ms->nc; |
1159 | 0 | char unq[16]; |
1160 | 0 | const char *val; |
1161 | 0 | size_t val_len; |
1162 | 0 | size_t res; |
1163 | 0 | uint64_t nc_val; |
1164 | |
|
1165 | 0 | if (NULL == nc_param->value.cstr) |
1166 | 0 | return mhd_GET_RQ_NC_NONE; |
1167 | | |
1168 | 0 | if (0 == nc_param->value.len) |
1169 | 0 | return mhd_GET_RQ_NC_BROKEN; |
1170 | | |
1171 | 0 | if (!nc_param->quoted) |
1172 | 0 | { |
1173 | 0 | val = nc_param->value.cstr; |
1174 | 0 | val_len = nc_param->value.len; |
1175 | 0 | } |
1176 | 0 | else |
1177 | 0 | { |
1178 | | /* Actually no backslashes must be used in 'nc' */ |
1179 | 0 | if (sizeof(unq) < params->nc.value.len) |
1180 | 0 | return mhd_GET_RQ_NC_TOO_LONG; |
1181 | 0 | val_len = mhd_str_unquote (nc_param->value.cstr, nc_param->value.len, unq); |
1182 | 0 | if (0 == val_len) |
1183 | 0 | return mhd_GET_RQ_NC_BROKEN; |
1184 | 0 | val = unq; |
1185 | 0 | } |
1186 | | |
1187 | 0 | res = mhd_strx_to_uint64_n (val, |
1188 | 0 | val_len, |
1189 | 0 | &nc_val); |
1190 | 0 | if (0 == res) |
1191 | 0 | { |
1192 | 0 | const char f = val[0]; |
1193 | 0 | if ((('9' >= f) && ('0' <= f)) |
1194 | 0 | || (('F' >= f) && ('A' <= f)) |
1195 | 0 | || (('a' <= f) && ('f' >= f))) |
1196 | 0 | return mhd_GET_RQ_NC_TOO_LARGE; |
1197 | 0 | else |
1198 | 0 | return mhd_GET_RQ_NC_BROKEN; |
1199 | 0 | } |
1200 | 0 | if (val_len != res) |
1201 | 0 | return mhd_GET_RQ_NC_BROKEN; |
1202 | 0 | if (nc_val != (nc_val & UINT64_C (0xFFFFFFFF))) |
1203 | 0 | return mhd_GET_RQ_NC_TOO_LARGE; |
1204 | 0 | *nc = (uint_fast32_t)nc_val; |
1205 | 0 | return mhd_GET_RQ_NC_VALID; |
1206 | 0 | } |
1207 | | |
1208 | | |
1209 | | static MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
1210 | | enum MHD_StatusCode |
1211 | | find_and_parse_auth_digest_info (struct MHD_Request *restrict req) |
1212 | 0 | { |
1213 | 0 | enum MHD_StatusCode res; |
1214 | 0 | struct MHD_AuthDigestInfo *info; |
1215 | 0 | enum MHD_DigestAuthUsernameType uname_type; |
1216 | 0 | size_t unif_buf_size; |
1217 | 0 | uint8_t *unif_buf_ptr; |
1218 | 0 | size_t unif_buf_used; |
1219 | 0 | enum mhd_GetRqNCResult nc_res; |
1220 | |
|
1221 | 0 | mhd_assert (NULL == req->auth.digest.info); |
1222 | |
|
1223 | 0 | res = get_rq_auth_digest_params (req); |
1224 | 0 | if (MHD_SC_OK != res) |
1225 | 0 | return res; |
1226 | | |
1227 | 0 | unif_buf_size = 0; |
1228 | |
|
1229 | 0 | uname_type = get_rq_uname_type (req->auth.digest.rqp); |
1230 | |
|
1231 | 0 | unif_buf_size += get_rq_unames_size (req->auth.digest.rqp, |
1232 | 0 | uname_type); |
1233 | |
|
1234 | 0 | if (NULL != req->auth.digest.rqp->opaque.value.cstr) |
1235 | 0 | unif_buf_size += req->auth.digest.rqp->opaque.value.len + 1; /* Add one for zero-termination */ |
1236 | 0 | if (NULL != req->auth.digest.rqp->realm.value.cstr) |
1237 | 0 | unif_buf_size += req->auth.digest.rqp->realm.value.len + 1; /* Add one for zero-termination */ |
1238 | 0 | info = |
1239 | 0 | (struct MHD_AuthDigestInfo *) |
1240 | 0 | mhd_stream_alloc_memory (mhd_CNTNR_PTR (req, struct MHD_Connection, rq), |
1241 | 0 | (sizeof(struct MHD_AuthDigestInfo)) |
1242 | 0 | + unif_buf_size); |
1243 | 0 | if (NULL == info) |
1244 | 0 | return MHD_SC_CONNECTION_POOL_NO_MEM_AUTH_DATA; |
1245 | | |
1246 | 0 | memset (info, |
1247 | 0 | 0, |
1248 | 0 | (sizeof(struct MHD_AuthDigestInfo)) + unif_buf_size); |
1249 | | #ifndef HAVE_NULL_PTR_ALL_ZEROS |
1250 | | info->username.cstr = NULL; |
1251 | | info->userhash_hex.cstr = NULL; |
1252 | | info->userhash_bin = NULL; |
1253 | | info->opaque.cstr = NULL; |
1254 | | info->realm.cstr = NULL; |
1255 | | #endif |
1256 | |
|
1257 | 0 | unif_buf_ptr = (uint8_t *)(info + 1); |
1258 | 0 | unif_buf_used = 0; |
1259 | |
|
1260 | 0 | info->algo = req->auth.digest.rqp->algo; |
1261 | |
|
1262 | 0 | if ((MHD_DIGEST_AUTH_UNAME_TYPE_MISSING != uname_type) |
1263 | 0 | && (MHD_DIGEST_AUTH_UNAME_TYPE_INVALID != uname_type)) |
1264 | 0 | unif_buf_used += |
1265 | 0 | get_rq_uname (req->auth.digest.rqp, uname_type, info, |
1266 | 0 | unif_buf_ptr + unif_buf_used, |
1267 | 0 | unif_buf_size - unif_buf_used); |
1268 | 0 | else |
1269 | 0 | info->uname_type = uname_type; |
1270 | |
|
1271 | 0 | if (NULL != req->auth.digest.rqp->opaque.value.cstr) |
1272 | 0 | { |
1273 | 0 | info->opaque.cstr = (char *)(unif_buf_ptr + unif_buf_used); |
1274 | 0 | info->opaque.len = |
1275 | 0 | get_rq_param_unquoted_copy_z (&(req->auth.digest.rqp->opaque), |
1276 | 0 | (char *)(unif_buf_ptr + unif_buf_used)); |
1277 | 0 | unif_buf_used += info->opaque.len + 1; |
1278 | 0 | } |
1279 | 0 | if (NULL != req->auth.digest.rqp->realm.value.cstr) |
1280 | 0 | { |
1281 | 0 | info->realm.cstr = (char *)(unif_buf_ptr + unif_buf_used); |
1282 | 0 | info->realm.len = |
1283 | 0 | get_rq_param_unquoted_copy_z (&(req->auth.digest.rqp->realm), |
1284 | 0 | (char *)(unif_buf_ptr + unif_buf_used)); |
1285 | 0 | unif_buf_used += info->realm.len + 1; |
1286 | 0 | } |
1287 | |
|
1288 | 0 | mhd_assert (unif_buf_size >= unif_buf_used); |
1289 | |
|
1290 | 0 | info->qop = req->auth.digest.rqp->qop; |
1291 | |
|
1292 | 0 | if (NULL != req->auth.digest.rqp->cnonce.value.cstr) |
1293 | 0 | info->cnonce_len = req->auth.digest.rqp->cnonce.value.len; |
1294 | 0 | else |
1295 | 0 | info->cnonce_len = 0; |
1296 | |
|
1297 | 0 | nc_res = get_rq_nc (req->auth.digest.rqp, &info->nc); |
1298 | 0 | if (mhd_GET_RQ_NC_VALID == nc_res) |
1299 | 0 | { |
1300 | 0 | if (0 == info->nc) |
1301 | 0 | info->nc_type = MHD_DIGEST_AUTH_NC_ZERO; |
1302 | 0 | else |
1303 | 0 | info->nc_type = MHD_DIGEST_AUTH_NC_NUMBER; |
1304 | 0 | } |
1305 | 0 | else |
1306 | 0 | { |
1307 | 0 | info->nc = 0; |
1308 | 0 | if (mhd_GET_RQ_NC_BROKEN == nc_res) |
1309 | 0 | info->nc_type = MHD_DIGEST_AUTH_NC_NONE; |
1310 | 0 | else |
1311 | 0 | info->nc_type = (enum MHD_DigestAuthNC)nc_res; |
1312 | 0 | } |
1313 | |
|
1314 | 0 | req->auth.digest.info = info; |
1315 | |
|
1316 | 0 | mhd_assert (uname_type == info->uname_type); |
1317 | |
|
1318 | 0 | if ((MHD_DIGEST_AUTH_UNAME_TYPE_MISSING == uname_type) |
1319 | 0 | || (MHD_DIGEST_AUTH_UNAME_TYPE_INVALID == uname_type) |
1320 | 0 | || ((mhd_GET_RQ_NC_BROKEN == nc_res))) |
1321 | 0 | return MHD_SC_REQ_AUTH_DATA_BROKEN; |
1322 | | |
1323 | 0 | return MHD_SC_OK; |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | | MHD_INTERNAL MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
1328 | | MHD_FN_PAR_OUT_ (2) enum MHD_StatusCode |
1329 | | mhd_request_get_auth_digest_info ( |
1330 | | struct MHD_Request *restrict req, |
1331 | | const struct MHD_AuthDigestInfo **restrict v_auth_digest_info) |
1332 | 0 | { |
1333 | 0 | mhd_assert (mhd_HTTP_STAGE_HEADERS_PROCESSED <= \ |
1334 | 0 | mhd_CNTNR_CPTR (req, struct MHD_Connection, rq)->stage); |
1335 | 0 | mhd_assert (mhd_HTTP_STAGE_REQ_RECV_FINISHED >= \ |
1336 | 0 | mhd_CNTNR_CPTR (req, struct MHD_Connection, rq)->stage); |
1337 | |
|
1338 | 0 | if (MHD_SC_OK != req->auth.digest.parse_result) |
1339 | 0 | return req->auth.digest.parse_result; |
1340 | | |
1341 | 0 | if (NULL == req->auth.digest.info) |
1342 | 0 | req->auth.digest.parse_result = find_and_parse_auth_digest_info (req); |
1343 | |
|
1344 | 0 | if (MHD_SC_OK != req->auth.digest.parse_result) |
1345 | 0 | return req->auth.digest.parse_result; /* Failure exit point */ |
1346 | | |
1347 | 0 | mhd_assert (NULL != req->auth.digest.info); |
1348 | 0 | *v_auth_digest_info = req->auth.digest.info; |
1349 | |
|
1350 | 0 | return MHD_SC_OK; /* Success exit point */ |
1351 | 0 | } |
1352 | | |
1353 | | |
1354 | | /** |
1355 | | * Get base hash calculation algorithm from #MHD_DigestAuthAlgo value. |
1356 | | * @param algo the MHD_DigestAuthAlgo value |
1357 | | * @return the base hash calculation algorithm |
1358 | | */ |
1359 | | mhd_static_inline enum MHD_DigestBaseAlgo |
1360 | | get_base_digest_algo (enum MHD_DigestAuthAlgo algo) |
1361 | 3.25k | { |
1362 | 3.25k | unsigned int base_algo; |
1363 | | |
1364 | 3.25k | base_algo = |
1365 | 3.25k | ((unsigned int)algo) |
1366 | 3.25k | & ~((unsigned int) |
1367 | 3.25k | (MHD_DIGEST_AUTH_ALGO_NON_SESSION |
1368 | 3.25k | | MHD_DIGEST_AUTH_ALGO_SESSION)); |
1369 | 3.25k | return (enum MHD_DigestBaseAlgo)base_algo; |
1370 | 3.25k | } |
1371 | | |
1372 | | |
1373 | | /** |
1374 | | * Get digest size in bytes for specified algorithm. |
1375 | | * |
1376 | | * Internal inline version. |
1377 | | * @param algo the algorithm to check |
1378 | | * @return the size of the digest (in bytes) or zero if the input value is not |
1379 | | * supported/valid |
1380 | | */ |
1381 | | mhd_static_inline size_t |
1382 | | digest_get_hash_size (enum MHD_DigestAuthAlgo algo) |
1383 | 2.17k | { |
1384 | 2.17k | #ifdef MHD_SUPPORT_MD5 |
1385 | 2.17k | mhd_STATIC_ASSERT_STMT (MHD_MD5_DIGEST_SIZE == mhd_MD5_DIGEST_SIZE, |
1386 | 2.17k | "Public and internal MD5 digest sizes must match"); |
1387 | 2.17k | #endif /* MHD_SUPPORT_MD5 */ |
1388 | 2.17k | #ifdef MHD_SUPPORT_SHA256 |
1389 | 2.17k | mhd_STATIC_ASSERT_STMT ( |
1390 | 2.17k | MHD_SHA256_DIGEST_SIZE == mhd_SHA256_DIGEST_SIZE, |
1391 | 2.17k | "Public and internal SHA-256 digest sizes must match"); |
1392 | 2.17k | #endif /* MHD_SUPPORT_SHA256 */ |
1393 | 2.17k | #ifdef MHD_SUPPORT_SHA512_256 |
1394 | 2.17k | mhd_STATIC_ASSERT_STMT ( |
1395 | 2.17k | MHD_SHA512_256_DIGEST_SIZE == mhd_SHA512_256_DIGEST_SIZE, |
1396 | 2.17k | "Public and internal SHA-512/256 digest sizes must match"); |
1397 | 2.17k | # ifdef MHD_SUPPORT_SHA256 |
1398 | 2.17k | mhd_STATIC_ASSERT_STMT ( |
1399 | 2.17k | mhd_SHA256_DIGEST_SIZE == mhd_SHA512_256_DIGEST_SIZE, |
1400 | 2.17k | "SHA-256 and SHA-512/256 digest sizes must match"); |
1401 | 2.17k | # endif /* MHD_SUPPORT_SHA256 */ |
1402 | 2.17k | #endif /* MHD_SUPPORT_SHA512_256 */ |
1403 | | /* Only one algorithm must be specified */ |
1404 | 2.17k | mhd_assert (1 == \ |
1405 | 2.17k | (((0 != (((unsigned int)algo) \ |
1406 | 2.17k | & MHD_DIGEST_BASE_ALGO_MD5)) ? 1 : 0) \ |
1407 | 2.17k | + ((0 != (((unsigned int)algo) \ |
1408 | 2.17k | & MHD_DIGEST_BASE_ALGO_SHA256)) ? 1 : 0) \ |
1409 | 2.17k | + ((0 != (((unsigned int)algo) \ |
1410 | 2.17k | & MHD_DIGEST_BASE_ALGO_SHA512_256)) ? 1 : 0))); |
1411 | 2.17k | #ifdef MHD_SUPPORT_MD5 |
1412 | 2.17k | if (0 != (((unsigned int)algo) |
1413 | 2.17k | & ((unsigned int)MHD_DIGEST_BASE_ALGO_MD5))) |
1414 | 1.21k | return MHD_MD5_DIGEST_SIZE; |
1415 | 958 | else |
1416 | 958 | #endif /* MHD_SUPPORT_MD5 */ |
1417 | 958 | #if defined(MHD_SUPPORT_SHA256) && defined(MHD_SUPPORT_SHA512_256) |
1418 | 958 | if (0 != (((unsigned int)algo) |
1419 | 958 | & (((unsigned int)MHD_DIGEST_BASE_ALGO_SHA256) |
1420 | 958 | | ((unsigned int)MHD_DIGEST_BASE_ALGO_SHA512_256)))) |
1421 | 958 | return MHD_SHA256_DIGEST_SIZE; /* The same as mhd_SHA512_256_DIGEST_SIZE */ |
1422 | 0 | else |
1423 | | #elif defined(MHD_SUPPORT_SHA256) |
1424 | | if (0 != (((unsigned int)algo) |
1425 | | & ((unsigned int)MHD_DIGEST_BASE_ALGO_SHA256))) |
1426 | | return MHD_SHA256_DIGEST_SIZE; |
1427 | | else |
1428 | | #elif defined(MHD_SUPPORT_SHA512_256) |
1429 | | if (0 != (((unsigned int)algo) |
1430 | | & ((unsigned int)MHD_DIGEST_BASE_ALGO_SHA512_256))) |
1431 | | return MHD_SHA512_256_DIGEST_SIZE; |
1432 | | else |
1433 | | #endif /* MHD_SUPPORT_SHA512_256 */ |
1434 | 0 | (void)0; /* Unsupported algorithm */ |
1435 | | |
1436 | 0 | return 0; /* Wrong input or unsupported algorithm */ |
1437 | 2.17k | } |
1438 | | |
1439 | | |
1440 | | /** |
1441 | | * Get digest size for specified algorithm. |
1442 | | * |
1443 | | * The size of the digest specifies the size of the userhash, userdigest |
1444 | | * and other parameters which size depends on used hash algorithm. |
1445 | | * @param algo the algorithm to check |
1446 | | * @return the size of the digest (either #MHD_MD5_DIGEST_SIZE or |
1447 | | * #MHD_SHA256_DIGEST_SIZE/MHD_SHA512_256_DIGEST_SIZE) |
1448 | | * or zero if the input value is not supported or not valid |
1449 | | * @sa #MHD_digest_auth_calc_userdigest() |
1450 | | * @sa #MHD_digest_auth_calc_userhash(), #MHD_digest_auth_calc_userhash_hex() |
1451 | | * @note Available since #MHD_VERSION 0x00097701 |
1452 | | * @ingroup authentication |
1453 | | */ |
1454 | | MHD_EXTERN_ MHD_FN_CONST_ size_t |
1455 | | MHD_digest_get_hash_size (enum MHD_DigestAuthAlgo algo) |
1456 | 1.08k | { |
1457 | 1.08k | return digest_get_hash_size (algo); |
1458 | 1.08k | } |
1459 | | |
1460 | | |
1461 | | /** |
1462 | | * The digest calculation structure. |
1463 | | */ |
1464 | | struct DigestAlgorithm |
1465 | | { |
1466 | | /** |
1467 | | * A context for the digest algorithm, already initialized to be |
1468 | | * useful for @e init, @e update and @e digest. |
1469 | | */ |
1470 | | union DigestCtx ctx; |
1471 | | |
1472 | | /** |
1473 | | * The hash calculation algorithm. |
1474 | | */ |
1475 | | enum MHD_DigestBaseAlgo algo; |
1476 | | |
1477 | | /** |
1478 | | * Buffer for hex-print of the final digest. |
1479 | | */ |
1480 | | #ifndef NDEBUG |
1481 | | bool uninitialised; /**< The structure has been not set-up */ |
1482 | | bool algo_selected; /**< The algorithm has been selected */ |
1483 | | bool ready_for_hashing; /**< The structure is ready to hash data */ |
1484 | | bool hashing; /**< Some data has been hashed, but the digest has not finalised yet */ |
1485 | | #endif /* NDEBUG */ |
1486 | | }; |
1487 | | |
1488 | | |
1489 | | /** |
1490 | | * Return the size of the digest. |
1491 | | * @param da the digest calculation structure to identify |
1492 | | * @return the size of the digest. |
1493 | | */ |
1494 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ unsigned int |
1495 | | digest_get_size (struct DigestAlgorithm *da) |
1496 | 3.25k | { |
1497 | 3.25k | mhd_assert (!da->uninitialised); |
1498 | 3.25k | mhd_assert (da->algo_selected); |
1499 | 3.25k | #ifdef MHD_SUPPORT_MD5 |
1500 | 3.25k | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
1501 | 1.81k | return mhd_MD5_DIGEST_SIZE; |
1502 | 1.43k | #endif /* MHD_SUPPORT_MD5 */ |
1503 | 1.43k | #ifdef MHD_SUPPORT_SHA256 |
1504 | 1.43k | if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
1505 | 1.05k | return mhd_SHA256_DIGEST_SIZE; |
1506 | 384 | #endif /* MHD_SUPPORT_SHA256 */ |
1507 | 384 | #ifdef MHD_SUPPORT_SHA512_256 |
1508 | 384 | if (MHD_DIGEST_BASE_ALGO_SHA512_256 == da->algo) |
1509 | 384 | return mhd_SHA512_256_DIGEST_SIZE; |
1510 | 0 | #endif /* MHD_SUPPORT_SHA512_256 */ |
1511 | 0 | mhd_UNREACHABLE (); |
1512 | 0 | return 0; |
1513 | 384 | } |
1514 | | |
1515 | | |
1516 | | #if defined(mhd_MD5_HAS_DEINIT) \ |
1517 | | || defined(mhd_SHA256_HAS_DEINIT) \ |
1518 | | || defined(mhd_SHA512_256_HAS_DEINIT) |
1519 | | /** |
1520 | | * Indicates presence of digest_deinit() function |
1521 | | */ |
1522 | | # define mhd_DIGEST_HAS_DEINIT 1 |
1523 | | #endif /* mhd_MD5_HAS_DEINIT || mhd_SHA256_HAS_DEINIT */ |
1524 | | |
1525 | | #ifdef mhd_DIGEST_HAS_DEINIT |
1526 | | /** |
1527 | | * Zero-initialise digest calculation structure. |
1528 | | * |
1529 | | * This initialisation is enough to safely call #digest_deinit() only. |
1530 | | * To make any real digest calculation, #digest_setup_and_init() must be called. |
1531 | | * @param da the digest calculation |
1532 | | */ |
1533 | | mhd_static_inline void |
1534 | | digest_setup_zero (struct DigestAlgorithm *da) |
1535 | | { |
1536 | | # ifndef NDEBUG |
1537 | | da->uninitialised = false; |
1538 | | da->algo_selected = false; |
1539 | | da->ready_for_hashing = false; |
1540 | | da->hashing = false; |
1541 | | # endif /* _DEBUG */ |
1542 | | da->algo = MHD_DIGEST_BASE_ALGO_INVALID; |
1543 | | } |
1544 | | |
1545 | | |
1546 | | /** |
1547 | | * De-initialise digest calculation structure. |
1548 | | * |
1549 | | * This function must be called if #digest_setup_and_init() was called for |
1550 | | * @a da. |
1551 | | * This function must not be called if @a da was not initialised by |
1552 | | * #digest_setup_and_init() or by #digest_setup_zero(). |
1553 | | * @param da the digest calculation |
1554 | | */ |
1555 | | mhd_static_inline void |
1556 | | digest_deinit (struct DigestAlgorithm *da) |
1557 | | { |
1558 | | mhd_assert (!da->uninitialised); |
1559 | | # ifdef mhd_MD5_HAS_DEINIT |
1560 | | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
1561 | | mhd_MD5_deinit (&(da->ctx.md5_ctx)); |
1562 | | else |
1563 | | # endif /* mhd_MD5_HAS_DEINIT */ |
1564 | | # ifdef mhd_SHA256_HAS_DEINIT |
1565 | | if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
1566 | | mhd_SHA256_deinit (&(da->ctx.sha256_ctx)); |
1567 | | else |
1568 | | # endif /* mhd_SHA256_HAS_DEINIT */ |
1569 | | # ifdef mhd_SHA512_256_HAS_DEINIT |
1570 | | if (MHD_DIGEST_BASE_ALGO_SHA512_256 == da->algo) |
1571 | | mhd_SHA512_256_deinit (&(da->ctx.sha512_256_ctx)); |
1572 | | else |
1573 | | # endif /* mhd_SHA512_256_HAS_DEINIT */ |
1574 | | (void)0; |
1575 | | digest_setup_zero (da); |
1576 | | } |
1577 | | |
1578 | | |
1579 | | #else /* ! mhd_DIGEST_HAS_DEINIT */ |
1580 | 0 | # define digest_setup_zero(da) ((void) 0) |
1581 | 3.25k | # define digest_deinit(da) ((void) 0) |
1582 | | #endif /* ! mhd_DIGEST_HAS_DEINIT */ |
1583 | | |
1584 | | |
1585 | | /** |
1586 | | * Set-up the digest calculation structure and initialise with initial values. |
1587 | | * |
1588 | | * If @a da was successfully initialised, #digest_deinit() must be called |
1589 | | * after finishing using of the @a da. |
1590 | | * |
1591 | | * This function must not be called more than once for any @a da. |
1592 | | * |
1593 | | * @param da the structure to set-up |
1594 | | * @param algo the algorithm to use for digest calculation |
1595 | | * @return boolean 'true' if successfully set-up, |
1596 | | * false otherwise. |
1597 | | */ |
1598 | | mhd_static_inline MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ bool |
1599 | | digest_init_one_time (struct DigestAlgorithm *da, |
1600 | | enum MHD_DigestBaseAlgo algo) |
1601 | 3.25k | { |
1602 | | #ifndef NDEBUG |
1603 | | da->uninitialised = false; |
1604 | | da->algo_selected = false; |
1605 | | da->ready_for_hashing = false; |
1606 | | da->hashing = false; |
1607 | | #endif /* _DEBUG */ |
1608 | 3.25k | switch (algo) |
1609 | 3.25k | { |
1610 | 1.81k | case MHD_DIGEST_BASE_ALGO_MD5: |
1611 | 1.81k | #ifdef MHD_SUPPORT_MD5 |
1612 | 1.81k | da->algo = MHD_DIGEST_BASE_ALGO_MD5; |
1613 | | # ifndef NDEBUG |
1614 | | da->algo_selected = true; |
1615 | | # endif |
1616 | 1.81k | mhd_MD5_init (&(da->ctx.md5_ctx)); |
1617 | | # ifndef NDEBUG |
1618 | | da->ready_for_hashing = true; |
1619 | | # endif |
1620 | 1.81k | return true; |
1621 | 0 | #endif /* MHD_SUPPORT_MD5 */ |
1622 | 0 | break; |
1623 | | |
1624 | 1.05k | case MHD_DIGEST_BASE_ALGO_SHA256: |
1625 | 1.05k | #ifdef MHD_SUPPORT_SHA256 |
1626 | 1.05k | da->algo = MHD_DIGEST_BASE_ALGO_SHA256; |
1627 | | # ifndef NDEBUG |
1628 | | da->algo_selected = true; |
1629 | | # endif |
1630 | 1.05k | mhd_SHA256_init (&(da->ctx.sha256_ctx)); |
1631 | | # ifndef NDEBUG |
1632 | | da->ready_for_hashing = true; |
1633 | | # endif |
1634 | 1.05k | return true; |
1635 | 0 | #endif /* MHD_SUPPORT_SHA256 */ |
1636 | 0 | break; |
1637 | | |
1638 | 384 | case MHD_DIGEST_BASE_ALGO_SHA512_256: |
1639 | 384 | #ifdef MHD_SUPPORT_SHA512_256 |
1640 | 384 | da->algo = MHD_DIGEST_BASE_ALGO_SHA512_256; |
1641 | | # ifndef NDEBUG |
1642 | | da->algo_selected = true; |
1643 | | # endif |
1644 | 384 | mhd_SHA512_256_init (&(da->ctx.sha512_256_ctx)); |
1645 | | # ifndef NDEBUG |
1646 | | da->ready_for_hashing = true; |
1647 | | # endif |
1648 | 384 | return true; |
1649 | 0 | #endif /* MHD_SUPPORT_SHA512_256 */ |
1650 | 0 | break; |
1651 | | |
1652 | 0 | case MHD_DIGEST_BASE_ALGO_INVALID: |
1653 | 0 | default: |
1654 | 0 | mhd_UNREACHABLE (); |
1655 | 0 | break; |
1656 | 3.25k | } |
1657 | 0 | da->algo = MHD_DIGEST_BASE_ALGO_INVALID; |
1658 | 0 | return false; /* Unsupported or bad algorithm */ |
1659 | 3.25k | } |
1660 | | |
1661 | | |
1662 | | /** |
1663 | | * Hash more data for digest calculation. |
1664 | | * @param da the digest calculation |
1665 | | * @param size the size of the @a data in bytes |
1666 | | * @param data the data to process |
1667 | | */ |
1668 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1669 | | MHD_FN_PAR_IN_SIZE_ (3, 2) void |
1670 | | digest_update (struct DigestAlgorithm *restrict da, |
1671 | | size_t size, |
1672 | | const void *restrict data) |
1673 | 11.9k | { |
1674 | 11.9k | mhd_assert (!da->uninitialised); |
1675 | 11.9k | mhd_assert (da->algo_selected); |
1676 | 11.9k | mhd_assert (da->ready_for_hashing); |
1677 | 11.9k | switch (da->algo) |
1678 | 11.9k | { |
1679 | 6.66k | case MHD_DIGEST_BASE_ALGO_MD5: |
1680 | 6.66k | #ifdef MHD_SUPPORT_MD5 |
1681 | 6.66k | mhd_MD5_update (&da->ctx.md5_ctx, |
1682 | 6.66k | size, |
1683 | 6.66k | (const uint8_t *)data); |
1684 | | #else |
1685 | | mhd_UNREACHABLE (); |
1686 | | #endif |
1687 | 6.66k | break; |
1688 | 3.86k | case MHD_DIGEST_BASE_ALGO_SHA256: |
1689 | 3.86k | #ifdef MHD_SUPPORT_SHA256 |
1690 | 3.86k | mhd_SHA256_update (&da->ctx.sha256_ctx, |
1691 | 3.86k | size, |
1692 | 3.86k | (const uint8_t *)data); |
1693 | | #else |
1694 | | mhd_UNREACHABLE (); |
1695 | | #endif |
1696 | 3.86k | break; |
1697 | 1.40k | case MHD_DIGEST_BASE_ALGO_SHA512_256: |
1698 | 1.40k | #ifdef MHD_SUPPORT_SHA512_256 |
1699 | 1.40k | mhd_SHA512_256_update (&da->ctx.sha512_256_ctx, |
1700 | 1.40k | size, |
1701 | 1.40k | (const uint8_t *)data); |
1702 | | #else |
1703 | | mhd_UNREACHABLE (); |
1704 | | #endif |
1705 | 1.40k | break; |
1706 | 0 | case MHD_DIGEST_BASE_ALGO_INVALID: |
1707 | 0 | default: |
1708 | 0 | mhd_UNREACHABLE (); |
1709 | 0 | break; |
1710 | 11.9k | } |
1711 | | #ifndef NDEBUG |
1712 | | da->hashing = true; |
1713 | | #endif |
1714 | 11.9k | } |
1715 | | |
1716 | | |
1717 | | /** |
1718 | | * Feed digest calculation with more data from string. |
1719 | | * @param da the digest calculation |
1720 | | * @param str the zero-terminated string to process |
1721 | | */ |
1722 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1723 | | MHD_FN_PAR_CSTR_ (2) void |
1724 | | digest_update_str (struct DigestAlgorithm *restrict da, |
1725 | | const char *restrict str) |
1726 | 1.08k | { |
1727 | 1.08k | digest_update (da, |
1728 | 1.08k | strlen (str), |
1729 | 1.08k | (const uint8_t *)str); |
1730 | 1.08k | } |
1731 | | |
1732 | | |
1733 | | /** |
1734 | | * Feed digest calculation with more data from string. |
1735 | | * @param da the digest calculation |
1736 | | * @param buf the sized buffer with the data |
1737 | | */ |
1738 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1739 | | MHD_FN_PAR_CSTR_ (2) void |
1740 | | digest_update_cbuf (struct DigestAlgorithm *restrict da, |
1741 | | const struct mhd_BufferConst *restrict buf) |
1742 | 0 | { |
1743 | 0 | digest_update (da, |
1744 | 0 | buf->size, |
1745 | 0 | (const uint8_t *)buf->data); |
1746 | 0 | } |
1747 | | |
1748 | | |
1749 | | /** |
1750 | | * Feed digest calculation with more data from string. |
1751 | | * @param da the digest calculation |
1752 | | * @param buf the sized buffer with the data |
1753 | | */ |
1754 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1755 | | MHD_FN_PAR_CSTR_ (2) void |
1756 | | digest_update_buf (struct DigestAlgorithm *restrict da, |
1757 | | const struct mhd_Buffer *restrict buf) |
1758 | 0 | { |
1759 | 0 | digest_update (da, |
1760 | 0 | buf->size, |
1761 | 0 | (const uint8_t *)buf->data); |
1762 | 0 | } |
1763 | | |
1764 | | |
1765 | | /** |
1766 | | * Feed digest calculation with single colon ':' character. |
1767 | | * @param da the digest calculation |
1768 | | */ |
1769 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ void |
1770 | | digest_update_with_colon (struct DigestAlgorithm *da) |
1771 | 4.34k | { |
1772 | 4.34k | static const uint8_t colon = (uint8_t)':'; |
1773 | 4.34k | digest_update (da, |
1774 | 4.34k | 1, |
1775 | 4.34k | &colon); |
1776 | 4.34k | } |
1777 | | |
1778 | | |
1779 | | /** |
1780 | | * Finally calculate hash (the digest). |
1781 | | * @param da the digest calculation |
1782 | | * @param[out] digest the pointer to the buffer to put calculated digest, |
1783 | | * must be at least digest_get_size(da) bytes large |
1784 | | */ |
1785 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1786 | | MHD_FN_PAR_OUT_ (2) void |
1787 | | digest_calc_hash (struct DigestAlgorithm *da, |
1788 | | uint8_t *digest) |
1789 | 3.25k | { |
1790 | 3.25k | mhd_assert (!da->uninitialised); |
1791 | 3.25k | mhd_assert (da->algo_selected); |
1792 | 3.25k | mhd_assert (da->ready_for_hashing); |
1793 | 3.25k | switch (da->algo) |
1794 | 3.25k | { |
1795 | 1.81k | case MHD_DIGEST_BASE_ALGO_MD5: |
1796 | 1.81k | #ifdef MHD_SUPPORT_MD5 |
1797 | 1.81k | mhd_MD5_finish (&da->ctx.md5_ctx, digest); |
1798 | | # ifndef NDEBUG |
1799 | | da->ready_for_hashing = false; |
1800 | | # endif /* _DEBUG */ |
1801 | | #else /* ! MHD_SUPPORT_MD5 */ |
1802 | | mhd_UNREACHABLE (); |
1803 | | #endif /* ! MHD_SUPPORT_MD5 */ |
1804 | 1.81k | break; |
1805 | | |
1806 | 1.05k | case MHD_DIGEST_BASE_ALGO_SHA256: |
1807 | 1.05k | #ifdef MHD_SUPPORT_SHA256 |
1808 | 1.05k | mhd_SHA256_finish (&da->ctx.sha256_ctx, digest); |
1809 | | # ifndef NDEBUG |
1810 | | da->ready_for_hashing = false; |
1811 | | # endif /* _DEBUG */ |
1812 | | #else /* ! MHD_SUPPORT_SHA256 */ |
1813 | | mhd_UNREACHABLE (); |
1814 | | #endif /* ! MHD_SUPPORT_SHA256 */ |
1815 | 1.05k | break; |
1816 | | |
1817 | 384 | case MHD_DIGEST_BASE_ALGO_SHA512_256: |
1818 | 384 | #ifdef MHD_SUPPORT_SHA512_256 |
1819 | 384 | mhd_SHA512_256_finish (&da->ctx.sha512_256_ctx, digest); |
1820 | | # ifndef NDEBUG |
1821 | | da->ready_for_hashing = false; |
1822 | | # endif /* _DEBUG */ |
1823 | | #else /* ! MHD_SUPPORT_SHA512_256 */ |
1824 | | mhd_UNREACHABLE (); |
1825 | | #endif /* ! MHD_SUPPORT_SHA512_256 */ |
1826 | 384 | break; |
1827 | | |
1828 | 0 | case MHD_DIGEST_BASE_ALGO_INVALID: |
1829 | 0 | default: |
1830 | 0 | mhd_UNREACHABLE (); |
1831 | 0 | break; |
1832 | 3.25k | } |
1833 | | #ifndef NDEBUG |
1834 | | da->hashing = false; |
1835 | | #endif /* _DEBUG */ |
1836 | 3.25k | } |
1837 | | |
1838 | | |
1839 | | /** |
1840 | | * Reset the digest calculation structure and prepare for new calculation. |
1841 | | * |
1842 | | * @param da the structure to reset |
1843 | | */ |
1844 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ void |
1845 | | digest_reset (struct DigestAlgorithm *da) |
1846 | 0 | { |
1847 | 0 | mhd_assert (!da->uninitialised); |
1848 | 0 | mhd_assert (da->algo_selected); |
1849 | 0 | mhd_assert (!da->hashing); |
1850 | 0 | switch (da->algo) |
1851 | 0 | { |
1852 | 0 | case MHD_DIGEST_BASE_ALGO_MD5: |
1853 | 0 | #ifdef MHD_SUPPORT_MD5 |
1854 | 0 | mhd_assert (!da->ready_for_hashing); |
1855 | 0 | mhd_MD5_reset (&(da->ctx.md5_ctx)); |
1856 | | # ifndef NDEBUG |
1857 | | da->ready_for_hashing = true; |
1858 | | # endif /* _DEBUG */ |
1859 | | #else /* ! MHD_SUPPORT_MD5 */ |
1860 | | mhd_UNREACHABLE (); |
1861 | | #endif /* ! MHD_SUPPORT_MD5 */ |
1862 | 0 | break; |
1863 | | |
1864 | 0 | case MHD_DIGEST_BASE_ALGO_SHA256: |
1865 | 0 | #ifdef MHD_SUPPORT_SHA256 |
1866 | 0 | mhd_assert (!da->ready_for_hashing); |
1867 | 0 | mhd_SHA256_reset (&(da->ctx.sha256_ctx)); |
1868 | | # ifndef NDEBUG |
1869 | | da->ready_for_hashing = true; |
1870 | | # endif /* _DEBUG */ |
1871 | | #else /* ! MHD_SUPPORT_SHA256 */ |
1872 | | mhd_UNREACHABLE (); |
1873 | | #endif /* ! MHD_SUPPORT_SHA256 */ |
1874 | 0 | break; |
1875 | | |
1876 | 0 | case MHD_DIGEST_BASE_ALGO_SHA512_256: |
1877 | 0 | #ifdef MHD_SUPPORT_SHA512_256 |
1878 | 0 | mhd_assert (!da->ready_for_hashing); |
1879 | 0 | mhd_SHA512_256_reset (&(da->ctx.sha512_256_ctx)); |
1880 | | # ifndef NDEBUG |
1881 | | da->ready_for_hashing = true; |
1882 | | # endif /* _DEBUG */ |
1883 | | #else /* ! MHD_SUPPORT_SHA512_256 */ |
1884 | | mhd_UNREACHABLE (); |
1885 | | #endif /* ! MHD_SUPPORT_SHA512_256 */ |
1886 | 0 | break; |
1887 | | |
1888 | 0 | case MHD_DIGEST_BASE_ALGO_INVALID: |
1889 | 0 | default: |
1890 | | #ifndef NDEBUG |
1891 | | da->ready_for_hashing = false; |
1892 | | #endif |
1893 | 0 | mhd_UNREACHABLE (); |
1894 | 0 | break; |
1895 | 0 | } |
1896 | 0 | } |
1897 | | |
1898 | | |
1899 | | #if defined(mhd_MD5_HAS_EXT_ERROR) \ |
1900 | | || defined(mhd_SHA256_HAS_EXT_ERROR) \ |
1901 | | || defined(mhd_SHA512_256_HAS_EXT_ERROR) |
1902 | | /** |
1903 | | * Indicates that digest algorithm has external error status |
1904 | | */ |
1905 | | # define mhd_DIGEST_HAS_EXT_ERROR 1 |
1906 | | #endif /* mhd_MD5_HAS_EXT_ERROR || mhd_SHA256_HAS_EXT_ERROR |
1907 | | || mhd_SHA512_256_HAS_EXT_ERROR*/ |
1908 | | |
1909 | | #ifdef mhd_DIGEST_HAS_EXT_ERROR |
1910 | | /** |
1911 | | * Get external error state. |
1912 | | * |
1913 | | * When external digest calculation used, an error may occur during |
1914 | | * initialisation or hashing data. This function checks whether external |
1915 | | * error has been reported for digest calculation. |
1916 | | * @param da the digest calculation |
1917 | | * @return 'true' if external error occurs, |
1918 | | * 'false' otherwise |
1919 | | */ |
1920 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ bool |
1921 | | digest_has_error (struct DigestAlgorithm *da) |
1922 | | { |
1923 | | mhd_assert (!da->uninitialised); |
1924 | | mhd_assert (da->algo_selected); |
1925 | | switch (da->algo) |
1926 | | { |
1927 | | case MHD_DIGEST_BASE_ALGO_MD5: |
1928 | | # ifdef MHD_SUPPORT_MD5 |
1929 | | return mhd_MD5_has_err (&(da->ctx.md5_ctx)); |
1930 | | # else /* ! MHD_SUPPORT_MD5 */ |
1931 | | mhd_UNREACHABLE (); |
1932 | | # endif /* ! MHD_SUPPORT_MD5 */ |
1933 | | break; |
1934 | | |
1935 | | case MHD_DIGEST_BASE_ALGO_SHA256: |
1936 | | # ifdef MHD_SUPPORT_SHA256 |
1937 | | return mhd_SHA256_has_err (&(da->ctx.sha256_ctx)); |
1938 | | # else /* ! MHD_SUPPORT_SHA256 */ |
1939 | | mhd_UNREACHABLE (); |
1940 | | # endif /* ! MHD_SUPPORT_SHA256 */ |
1941 | | break; |
1942 | | |
1943 | | case MHD_DIGEST_BASE_ALGO_SHA512_256: |
1944 | | # ifdef MHD_SUPPORT_SHA512_256 |
1945 | | return mhd_SHA512_256_has_err (&(da->ctx.sha512_256_ctx)); |
1946 | | # else /* ! MHD_SUPPORT_SHA512_256 */ |
1947 | | mhd_UNREACHABLE (); |
1948 | | # endif /* ! MHD_SUPPORT_SHA512_256 */ |
1949 | | break; |
1950 | | |
1951 | | case MHD_DIGEST_BASE_ALGO_INVALID: |
1952 | | default: |
1953 | | break; |
1954 | | } |
1955 | | mhd_UNREACHABLE (); |
1956 | | return true; |
1957 | | } |
1958 | | |
1959 | | |
1960 | | #else /* ! mhd_DIGEST_HAS_EXT_ERROR */ |
1961 | 3.25k | # define digest_has_error(da) (((void) (da)), ! ! 0) |
1962 | | #endif /* ! mhd_DIGEST_HAS_EXT_ERROR */ |
1963 | | |
1964 | | |
1965 | | /** |
1966 | | * Calculate userdigest, return it as binary data. |
1967 | | * |
1968 | | * It is equal to H(A1) for non-session algorithms. |
1969 | | * |
1970 | | * MHD internal version. |
1971 | | * |
1972 | | * @param da the digest algorithm |
1973 | | * @param username the username to use |
1974 | | * @param username_len the length of the @a username |
1975 | | * @param realm the realm to use |
1976 | | * @param realm_len the length of the @a realm |
1977 | | * @param password the password, must be zero-terminated |
1978 | | * @param[out] ha1_bin the output buffer, must have at least |
1979 | | * #digest_get_size(da) bytes available |
1980 | | */ |
1981 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
1982 | | MHD_FN_PAR_IN_SIZE_ (2, 3) MHD_FN_PAR_IN_SIZE_ (4, 5) |
1983 | | MHD_FN_PAR_CSTR_ (6) MHD_FN_PAR_OUT_ (7) void |
1984 | | calc_userdigest (struct DigestAlgorithm *restrict da, |
1985 | | const char *restrict username, const size_t username_len, |
1986 | | const char *restrict realm, const size_t realm_len, |
1987 | | const char *restrict password, |
1988 | | uint8_t *ha1_bin) |
1989 | 1.08k | { |
1990 | 1.08k | mhd_assert (!da->uninitialised); |
1991 | 1.08k | mhd_assert (da->algo_selected); |
1992 | 1.08k | mhd_assert (!da->hashing); |
1993 | 1.08k | digest_update (da, username_len, username); |
1994 | 1.08k | digest_update_with_colon (da); |
1995 | 1.08k | digest_update (da, realm_len, realm); |
1996 | 1.08k | digest_update_with_colon (da); |
1997 | 1.08k | digest_update_str (da, password); |
1998 | 1.08k | digest_calc_hash (da, ha1_bin); |
1999 | 1.08k | } |
2000 | | |
2001 | | |
2002 | | MHD_EXTERN_ MHD_FN_PAR_NONNULL_ALL_ |
2003 | | MHD_FN_PAR_CSTR_ (2) MHD_FN_PAR_CSTR_ (3) MHD_FN_PAR_CSTR_ (4) |
2004 | | MHD_FN_PAR_OUT_SIZE_ (6, 5) enum MHD_StatusCode |
2005 | | MHD_digest_auth_calc_userdigest (enum MHD_DigestAuthAlgo algo, |
2006 | | const char *MHD_RESTRICT username, |
2007 | | const char *MHD_RESTRICT realm, |
2008 | | const char *MHD_RESTRICT password, |
2009 | | size_t bin_buf_size, |
2010 | | void *MHD_RESTRICT userdigest_bin) |
2011 | 1.08k | { |
2012 | 1.08k | struct DigestAlgorithm da; |
2013 | 1.08k | enum MHD_StatusCode ret; |
2014 | 1.08k | if (!digest_init_one_time (&da, get_base_digest_algo (algo))) |
2015 | 0 | return MHD_SC_AUTH_DIGEST_ALGO_NOT_SUPPORTED; |
2016 | | |
2017 | 1.08k | if (digest_get_size (&da) > bin_buf_size) |
2018 | 0 | ret = MHD_SC_OUT_BUFF_TOO_SMALL; |
2019 | 1.08k | else |
2020 | 1.08k | { |
2021 | 1.08k | calc_userdigest (&da, |
2022 | 1.08k | username, |
2023 | 1.08k | strlen (username), |
2024 | 1.08k | realm, |
2025 | 1.08k | strlen (realm), |
2026 | 1.08k | password, |
2027 | 1.08k | (uint8_t *)userdigest_bin); |
2028 | 1.08k | ret = digest_has_error (&da) ? MHD_SC_HASH_FAILED : MHD_SC_OK; |
2029 | 1.08k | } |
2030 | 1.08k | digest_deinit (&da); |
2031 | | |
2032 | 1.08k | return ret; |
2033 | 1.08k | } |
2034 | | |
2035 | | |
2036 | | /** |
2037 | | * Calculate userhash, return it as binary data. |
2038 | | * |
2039 | | * MHD internal version. |
2040 | | * |
2041 | | * @param da the digest algorithm |
2042 | | * @param username_len the length of the @a username |
2043 | | * @param username the username to use |
2044 | | * @param realm_len the length of the @a realm |
2045 | | * @param realm the realm to use |
2046 | | * @param[out] digest_bin the output buffer, must have at least |
2047 | | * #MHD_digest_get_hash_size(algo) bytes available |
2048 | | */ |
2049 | | mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ |
2050 | | MHD_FN_PAR_IN_SIZE_ (3, 2) MHD_FN_PAR_IN_SIZE_ (5, 4) MHD_FN_PAR_OUT_ (6) void |
2051 | | calc_userhash (struct DigestAlgorithm *da, |
2052 | | const size_t username_len, |
2053 | | const char *username, |
2054 | | const size_t realm_len, |
2055 | | const char *realm, |
2056 | | uint8_t *digest_bin) |
2057 | 2.17k | { |
2058 | 2.17k | mhd_assert (!da->uninitialised); |
2059 | 2.17k | mhd_assert (da->algo_selected); |
2060 | 2.17k | mhd_assert (!da->hashing); |
2061 | 2.17k | digest_update (da, username_len, username); |
2062 | 2.17k | digest_update_with_colon (da); |
2063 | 2.17k | digest_update (da, realm_len, realm); |
2064 | 2.17k | digest_calc_hash (da, digest_bin); |
2065 | 2.17k | } |
2066 | | |
2067 | | |
2068 | | MHD_EXTERN_ MHD_FN_PAR_NONNULL_ALL_ |
2069 | | MHD_FN_PAR_CSTR_ (2) |
2070 | | MHD_FN_PAR_CSTR_ (3) MHD_FN_PAR_OUT_SIZE_ (5, 4) enum MHD_StatusCode |
2071 | | MHD_digest_auth_calc_userhash (enum MHD_DigestAuthAlgo algo, |
2072 | | const char *MHD_RESTRICT username, |
2073 | | const char *MHD_RESTRICT realm, |
2074 | | size_t bin_buf_size, |
2075 | | void *MHD_RESTRICT userhash_bin) |
2076 | 2.17k | { |
2077 | 2.17k | struct DigestAlgorithm da; |
2078 | 2.17k | enum MHD_StatusCode ret; |
2079 | | |
2080 | 2.17k | if (!digest_init_one_time (&da, get_base_digest_algo (algo))) |
2081 | 0 | return MHD_SC_AUTH_DIGEST_ALGO_NOT_SUPPORTED; |
2082 | 2.17k | if (digest_get_size (&da) > bin_buf_size) |
2083 | 0 | ret = MHD_SC_OUT_BUFF_TOO_SMALL; |
2084 | 2.17k | else |
2085 | 2.17k | { |
2086 | 2.17k | calc_userhash (&da, |
2087 | 2.17k | strlen (username), |
2088 | 2.17k | username, |
2089 | 2.17k | strlen (realm), |
2090 | 2.17k | realm, |
2091 | 2.17k | (uint8_t *)userhash_bin); |
2092 | | |
2093 | 2.17k | ret = digest_has_error (&da) ? MHD_SC_HASH_FAILED : MHD_SC_OK; |
2094 | 2.17k | } |
2095 | 2.17k | digest_deinit (&da); |
2096 | | |
2097 | 2.17k | return ret; |
2098 | 2.17k | } |
2099 | | |
2100 | | |
2101 | | MHD_EXTERN_ MHD_FN_PAR_NONNULL_ALL_ |
2102 | | MHD_FN_PAR_CSTR_ (2) |
2103 | | MHD_FN_PAR_CSTR_ (3) MHD_FN_PAR_OUT_SIZE_ (5, 4) enum MHD_StatusCode |
2104 | | MHD_digest_auth_calc_userhash_hex ( |
2105 | | enum MHD_DigestAuthAlgo algo, |
2106 | | const char *MHD_RESTRICT username, |
2107 | | const char *MHD_RESTRICT realm, |
2108 | | size_t hex_buf_size, |
2109 | | char *userhash_hex) |
2110 | 1.08k | { |
2111 | 1.08k | uint8_t userhash_bin[mhd_MAX_DIGEST] = { 0u /* mute compiler warning */ }; |
2112 | 1.08k | size_t digest_size; |
2113 | 1.08k | enum MHD_StatusCode res; |
2114 | | |
2115 | 1.08k | digest_size = digest_get_hash_size (algo); |
2116 | 1.08k | if (digest_size * 2 + 1 > hex_buf_size) |
2117 | 0 | return MHD_SC_OUT_BUFF_TOO_SMALL; |
2118 | 1.08k | res = MHD_digest_auth_calc_userhash (algo, |
2119 | 1.08k | username, |
2120 | 1.08k | realm, |
2121 | 1.08k | sizeof(userhash_bin), |
2122 | 1.08k | userhash_bin); |
2123 | 1.08k | if (MHD_SC_OK != res) |
2124 | 0 | return res; |
2125 | | |
2126 | 1.08k | (void)mhd_bin_to_hex_z (userhash_bin, |
2127 | 1.08k | digest_size, |
2128 | 1.08k | userhash_hex); |
2129 | 1.08k | return MHD_SC_OK; |
2130 | 1.08k | } |
2131 | | |
2132 | | |
2133 | | /** |
2134 | | * Extract timestamp from the given nonce. |
2135 | | * @param nonce the nonce to check in binary form |
2136 | | * @return 'true' if timestamp was extracted, |
2137 | | * 'false' if nonce does not have valid timestamp. |
2138 | | */ |
2139 | | mhd_static_inline uint_fast32_t |
2140 | | get_nonce_timestamp (const uint8_t nonce[mhd_AUTH_DIGEST_NONCE_BIN_SIZE]) |
2141 | 0 | { |
2142 | 0 | return (uint_fast32_t) |
2143 | 0 | mhd_GET_32BIT_LE_UNALIGN (nonce + mhd_AUTH_DIGEST_NONCE_RAND_BIN_SIZE); |
2144 | 0 | } |
2145 | | |
2146 | | |
2147 | | /** |
2148 | | * The result of nonce-nc map array check. |
2149 | | */ |
2150 | | enum mhd_CheckNonceNC |
2151 | | { |
2152 | | /** |
2153 | | * The nonce and NC are OK (valid and NC was not used before). |
2154 | | */ |
2155 | | mhd_CHECK_NONCENC_OK = MHD_DAUTH_OK, |
2156 | | |
2157 | | /** |
2158 | | * The 'nonce' is too old, has been overwritten with newer 'nonce' in |
2159 | | * the same slot or 'nc' value has been used already. |
2160 | | * The validity of the 'nonce' was not be checked. |
2161 | | */ |
2162 | | mhd_CHECK_NONCENC_STALE = MHD_DAUTH_NONCE_STALE, |
2163 | | |
2164 | | /** |
2165 | | * The 'nonce' is wrong, it was not generated before. |
2166 | | */ |
2167 | | mhd_CHECK_NONCENC_WRONG = MHD_DAUTH_NONCE_WRONG |
2168 | | }; |
2169 | | |
2170 | | |
2171 | | /** |
2172 | | * Check nonce-nc map array with the new nonce counter. |
2173 | | * |
2174 | | * @param d the master daemon object |
2175 | | * @param noncelen the length of @a nonce, in characters |
2176 | | * @param nonce the pointer that referenced hex nonce, does not need to be |
2177 | | * zero-terminated |
2178 | | * @param nc the nonce counter |
2179 | | * @param time_now the current timestamp |
2180 | | * @return #MHD_DAUTH_NONCENC_OK if successful, |
2181 | | * #MHD_DAUTH_NONCENC_STALE if nonce is stale (or no nonce-nc array |
2182 | | * is available), |
2183 | | * #MHD_DAUTH_NONCENC_WRONG if nonce was not recodered in nonce-nc map |
2184 | | * array, while it should. |
2185 | | */ |
2186 | | static MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
2187 | | MHD_FN_PAR_IN_SIZE_ (3, 2) enum mhd_CheckNonceNC |
2188 | | check_nonce_nc (struct MHD_Daemon *restrict d, |
2189 | | size_t noncelen, |
2190 | | const char *restrict nonce, |
2191 | | uint_fast32_t nc, |
2192 | | uint_fast32_t time_now) |
2193 | 0 | { |
2194 | 0 | uint8_t nonce_bin[mhd_AUTH_DIGEST_NONCE_BIN_SIZE]; |
2195 | 0 | struct mhd_DaemonAuthDigestNonceData *nonce_slot; |
2196 | 0 | uint_fast32_t valid_time; |
2197 | 0 | uint_fast32_t slot_valid_time; |
2198 | 0 | enum mhd_CheckNonceNC ret; |
2199 | |
|
2200 | 0 | mhd_assert (!mhd_D_HAS_MASTER (d)); /* only master daemon should be used */ |
2201 | 0 | mhd_assert (0 != noncelen); |
2202 | 0 | mhd_assert (0 != nc); |
2203 | 0 | if (mhd_AUTH_DIGEST_NONCE_LEN != noncelen) |
2204 | 0 | return mhd_CHECK_NONCENC_WRONG; |
2205 | | |
2206 | 0 | if (mhd_AUTH_DIGEST_NONCE_BIN_SIZE != |
2207 | 0 | mhd_hex_to_bin (nonce, |
2208 | 0 | mhd_AUTH_DIGEST_NONCE_LEN, |
2209 | 0 | nonce_bin)) |
2210 | 0 | return mhd_CHECK_NONCENC_WRONG; |
2211 | | |
2212 | 0 | if ((NULL != memchr (nonce, 'A', mhd_AUTH_DIGEST_NONCE_LEN)) |
2213 | 0 | || (NULL != memchr (nonce, 'B', mhd_AUTH_DIGEST_NONCE_LEN)) |
2214 | 0 | || (NULL != memchr (nonce, 'C', mhd_AUTH_DIGEST_NONCE_LEN)) |
2215 | 0 | || (NULL != memchr (nonce, 'D', mhd_AUTH_DIGEST_NONCE_LEN)) |
2216 | 0 | || (NULL != memchr (nonce, 'E', mhd_AUTH_DIGEST_NONCE_LEN)) |
2217 | 0 | || (NULL != memchr (nonce, 'F', mhd_AUTH_DIGEST_NONCE_LEN))) |
2218 | 0 | return mhd_CHECK_NONCENC_WRONG; /* Upper case chars are not produced by MHD */ |
2219 | | |
2220 | 0 | valid_time = get_nonce_timestamp (nonce_bin); |
2221 | |
|
2222 | 0 | nonce_slot = d->auth_dg.nonces |
2223 | 0 | + nonce_to_index (nonce_bin, |
2224 | 0 | d->auth_dg.cfg.nonces_num); |
2225 | |
|
2226 | 0 | mhd_mutex_lock_chk (&(d->auth_dg.nonces_lock)); |
2227 | |
|
2228 | 0 | slot_valid_time = nonce_slot->valid_time; |
2229 | 0 | if ((0 == memcmp (nonce_slot->nonce, |
2230 | 0 | nonce_bin, |
2231 | 0 | sizeof(nonce_slot->nonce))) |
2232 | 0 | && (slot_valid_time == valid_time)) |
2233 | 0 | { |
2234 | | /* The nonce matches the stored nonce */ |
2235 | 0 | if (nonce_slot->max_recvd_nc < nc) |
2236 | 0 | { |
2237 | | /* 'nc' is larger, shift bitmask and bump limit */ |
2238 | 0 | const uint_fast32_t jump_size = |
2239 | 0 | (uint_fast32_t)nc - nonce_slot->max_recvd_nc; |
2240 | 0 | if (64 > jump_size) |
2241 | 0 | { |
2242 | | /* small jump, less than mask width */ |
2243 | 0 | nonce_slot->nmask <<= jump_size; |
2244 | | /* Set bit for the old 'nc' value */ |
2245 | 0 | nonce_slot->nmask |= (UINT64_C (1) << (jump_size - 1)); |
2246 | 0 | } |
2247 | 0 | else if (64 == jump_size) |
2248 | 0 | nonce_slot->nmask = (UINT64_C (1) << 63); |
2249 | 0 | else |
2250 | 0 | nonce_slot->nmask = 0; /* big jump, unset all bits in the mask */ |
2251 | 0 | nonce_slot->max_recvd_nc = nc; |
2252 | 0 | ret = mhd_CHECK_NONCENC_OK; |
2253 | 0 | } |
2254 | 0 | else if (nonce_slot->max_recvd_nc == nc) |
2255 | | /* 'nc' was already used */ |
2256 | 0 | ret = mhd_CHECK_NONCENC_STALE; |
2257 | 0 | else /* (nonce_slot->max_recvd_nc > nc) */ |
2258 | 0 | { |
2259 | | /* Out-of-order 'nc' value. Check whether it was used before */ |
2260 | 0 | const uint_fast32_t nc_delta = nonce_slot->max_recvd_nc - nc; |
2261 | |
|
2262 | 0 | mhd_ASSUME (0u != nc_delta); /* Guaranteed by if() branches */ |
2263 | |
|
2264 | 0 | if (64u >= nc_delta) |
2265 | 0 | { |
2266 | 0 | const uint_fast64_t nc_bit = (UINT64_C (1) << (nc_delta - 1)); |
2267 | |
|
2268 | 0 | mhd_ASSUME (0u != nc_bit); |
2269 | 0 | mhd_ASSUME (0u != (nc_bit & UINT64_C (0xFFFFFFFFFFFFFFFF))); |
2270 | |
|
2271 | 0 | if (0u == (nc_bit & nonce_slot->nmask)) |
2272 | 0 | { |
2273 | | /* 'nc' has not been used before. Set the bit. */ |
2274 | 0 | nonce_slot->nmask |= nc_bit; |
2275 | 0 | ret = mhd_CHECK_NONCENC_OK; |
2276 | 0 | } |
2277 | 0 | else |
2278 | 0 | ret = mhd_CHECK_NONCENC_STALE; /* 'nc' has been used before */ |
2279 | 0 | } |
2280 | 0 | else |
2281 | 0 | ret = mhd_CHECK_NONCENC_STALE; /* 'nc' is too old (more than 64 value before) */ |
2282 | 0 | } |
2283 | 0 | } |
2284 | 0 | else |
2285 | 0 | { |
2286 | | /* The nonce does not match the stored nonce */ |
2287 | 0 | if (((valid_time - slot_valid_time) & UINT32_C (0xFFFFFFFF)) <= |
2288 | 0 | ((slot_valid_time - valid_time) & UINT32_C (0xFFFFFFFF))) |
2289 | 0 | { |
2290 | | /* The stored nonce was generated before the checked nonce */ |
2291 | 0 | ret = mhd_CHECK_NONCENC_WRONG; |
2292 | 0 | } |
2293 | 0 | else |
2294 | 0 | { |
2295 | | /* The stored nonce was generated after the checked nonce */ |
2296 | 0 | const uint_fast32_t nonce_gen_time = |
2297 | 0 | ((valid_time - d->auth_dg.cfg.nonce_tmout) & UINT32_C (0xFFFFFFFF)); |
2298 | 0 | if (((time_now - nonce_gen_time) & UINT32_C (0xFFFFFFFF)) < |
2299 | 0 | ((nonce_gen_time - time_now) & UINT32_C (0xFFFFFFFF))) |
2300 | 0 | ret = mhd_CHECK_NONCENC_WRONG; /* The nonce is generated in "future" */ |
2301 | 0 | else |
2302 | | /* Probably the nonce has been overwritten with a newer nonce */ |
2303 | 0 | ret = mhd_CHECK_NONCENC_STALE; |
2304 | 0 | } |
2305 | 0 | } |
2306 | |
|
2307 | 0 | mhd_mutex_unlock_chk (&(d->auth_dg.nonces_lock)); |
2308 | |
|
2309 | 0 | return ret; |
2310 | 0 | } |
2311 | | |
2312 | | |
2313 | | struct test_header_param |
2314 | | { |
2315 | | struct MHD_Request *request; |
2316 | | size_t num_get_params; |
2317 | | }; |
2318 | | |
2319 | | /** |
2320 | | * Test if the given key-value pair is in the headers for the |
2321 | | * given request. |
2322 | | * |
2323 | | * @param cls the test context |
2324 | | * @param name the name of the key |
2325 | | * @param value the value of the key |
2326 | | * @return 'true' if the key-value pair is in the headers, |
2327 | | * 'false' if not |
2328 | | */ |
2329 | | static MHD_FN_PAR_NONNULL_ (2) MHD_FN_PAR_NONNULL_ (3) bool |
2330 | | test_header (void *restrict cls, |
2331 | | const struct MHD_String *restrict name, |
2332 | | const struct MHD_StringNullable *restrict value) |
2333 | 0 | { |
2334 | 0 | struct test_header_param *const param = (struct test_header_param *)cls; |
2335 | 0 | struct MHD_Request *req = param->request; |
2336 | 0 | struct mhd_RequestField *pos; |
2337 | 0 | size_t i; |
2338 | |
|
2339 | 0 | param->num_get_params++; |
2340 | 0 | i = 0; |
2341 | 0 | for (pos = mhd_DLINKEDL_GET_FIRST (req, fields); |
2342 | 0 | NULL != pos; |
2343 | 0 | pos = mhd_DLINKEDL_GET_NEXT (pos, fields)) |
2344 | 0 | { |
2345 | 0 | if (MHD_VK_URI_QUERY_PARAM != pos->field.kind) |
2346 | 0 | continue; |
2347 | 0 | if (++i == param->num_get_params) |
2348 | 0 | { |
2349 | 0 | if (name->len != pos->field.nv.name.len) |
2350 | 0 | return false; |
2351 | 0 | if (value->len != pos->field.nv.value.len) |
2352 | 0 | return false; |
2353 | 0 | if (0 != name->len) |
2354 | 0 | { |
2355 | 0 | mhd_assert (NULL != name->cstr); |
2356 | 0 | mhd_assert (NULL != pos->field.nv.name.cstr); |
2357 | 0 | if (0 != memcmp (name->cstr, |
2358 | 0 | pos->field.nv.name.cstr, |
2359 | 0 | name->len)) |
2360 | 0 | return false; |
2361 | 0 | } |
2362 | 0 | if (0 != value->len) |
2363 | 0 | { |
2364 | 0 | mhd_assert (NULL != value->cstr); |
2365 | 0 | mhd_assert (NULL != pos->field.nv.value.cstr); |
2366 | 0 | if (0 != memcmp (value->cstr, |
2367 | 0 | pos->field.nv.value.cstr, |
2368 | 0 | value->len)) |
2369 | 0 | return false; |
2370 | 0 | } |
2371 | 0 | return true; |
2372 | 0 | } |
2373 | 0 | } |
2374 | 0 | return false; |
2375 | 0 | } |
2376 | | |
2377 | | |
2378 | | /** |
2379 | | * Check that the arguments given by the client as part |
2380 | | * of the authentication header match the arguments we |
2381 | | * got as part of the HTTP request URI. |
2382 | | * |
2383 | | * @param req the request with get arguments to compare against |
2384 | | * @param args the copy of argument URI string (after "?" in URI), will be |
2385 | | * modified by this function |
2386 | | * @return 'true' if the arguments match, |
2387 | | * 'false' if not |
2388 | | */ |
2389 | | static MHD_FN_PAR_NONNULL_ALL_ |
2390 | | MHD_FN_PAR_CSTR_ (3) |
2391 | | MHD_FN_PAR_INOUT_SIZE_ (3, 2) bool |
2392 | | check_argument_match (struct MHD_Request *restrict req, |
2393 | | size_t args_len, |
2394 | | char *restrict args) |
2395 | 0 | { |
2396 | 0 | struct mhd_RequestField *pos; |
2397 | 0 | struct test_header_param param; |
2398 | |
|
2399 | 0 | param.request = req; |
2400 | 0 | param.num_get_params = 0; |
2401 | 0 | if (!mhd_parse_uri_args (args_len, |
2402 | 0 | args, |
2403 | 0 | &test_header, |
2404 | 0 | ¶m)) |
2405 | 0 | return false; |
2406 | | |
2407 | | /* Check that the number of arguments matches */ |
2408 | 0 | for (pos = mhd_DLINKEDL_GET_FIRST (req, fields); |
2409 | 0 | NULL != pos; |
2410 | 0 | pos = mhd_DLINKEDL_GET_NEXT (pos, fields)) |
2411 | 0 | { |
2412 | 0 | if (MHD_VK_URI_QUERY_PARAM != pos->field.kind) |
2413 | 0 | continue; |
2414 | 0 | param.num_get_params--; |
2415 | 0 | } |
2416 | |
|
2417 | 0 | if (0 != param.num_get_params) |
2418 | 0 | return false; /* argument count mismatch */ |
2419 | | |
2420 | 0 | return true; |
2421 | 0 | } |
2422 | | |
2423 | | |
2424 | | /** |
2425 | | * Check that the URI provided by the client as part |
2426 | | * of the authentication header match the real HTTP request URI. |
2427 | | * |
2428 | | * @param req the request to compare URI |
2429 | | * @param uri the copy of URI in the authentication header, should point to |
2430 | | * modifiable buffer at least @a uri_len + 1 characters long, |
2431 | | * will be modified by this function, not valid upon return |
2432 | | * @param uri_len the length of the @a uri string in characters |
2433 | | * @return boolean true if the URIs match, |
2434 | | * boolean false if not |
2435 | | */ |
2436 | | static MHD_FN_PAR_NONNULL_ALL_ |
2437 | | MHD_FN_PAR_INOUT_ (3) bool |
2438 | | check_uri_match (struct MHD_Request *restrict req, |
2439 | | const size_t uri_len, |
2440 | | char *restrict uri) |
2441 | 0 | { |
2442 | 0 | char *qmark; |
2443 | 0 | char *args; |
2444 | 0 | size_t url_len; /* The part before '?' char */ |
2445 | 0 | size_t args_len; |
2446 | |
|
2447 | 0 | if (uri_len != req->req_target_len) |
2448 | 0 | return false; |
2449 | | |
2450 | 0 | uri[uri_len] = 0; |
2451 | 0 | qmark = (char *)memchr (uri, |
2452 | 0 | '?', |
2453 | 0 | uri_len); |
2454 | 0 | if (NULL != qmark) |
2455 | 0 | { |
2456 | 0 | *qmark = 0; |
2457 | 0 | url_len = (size_t)(qmark - uri); |
2458 | 0 | } |
2459 | 0 | else |
2460 | 0 | url_len = uri_len; |
2461 | | |
2462 | | /* Need to unescape URI before comparing with req->url */ |
2463 | 0 | url_len = mhd_str_pct_decode_lenient_n (uri, |
2464 | 0 | url_len, |
2465 | 0 | uri, |
2466 | 0 | url_len, |
2467 | 0 | NULL); |
2468 | 0 | if ((url_len != req->url_len) |
2469 | 0 | || (0 != memcmp (uri, |
2470 | 0 | req->url, |
2471 | 0 | url_len))) |
2472 | 0 | return false; |
2473 | | |
2474 | 0 | args = (NULL != qmark) ? (qmark + 1) : uri + uri_len; |
2475 | 0 | args_len = (size_t)(uri + uri_len - args); |
2476 | |
|
2477 | 0 | if (!check_argument_match (req, |
2478 | 0 | args_len, |
2479 | 0 | args)) |
2480 | 0 | return false; |
2481 | | |
2482 | 0 | return true; |
2483 | 0 | } |
2484 | | |
2485 | | |
2486 | | /** |
2487 | | * The size of the unquoting buffer in stack |
2488 | | */ |
2489 | 0 | #define mhd_STATIC_UNQ_BUFFER_SIZE 128 |
2490 | | |
2491 | | |
2492 | | /** |
2493 | | * Get the pointer to buffer with required size |
2494 | | * @param tmp1 the first buffer with fixed size |
2495 | | * @param[in,out] ptmp2 the pointer to pointer to malloc'ed buffer |
2496 | | * @param[in,out] ptmp2_size the pointer to the size of the buffer pointed by @a ptmp2 |
2497 | | * @param required_size the required size in buffer |
2498 | | * @return the pointer to the buffer or NULL if failed to allocate buffer with |
2499 | | * requested size |
2500 | | */ |
2501 | | static MHD_FN_PAR_NONNULL_ALL_ |
2502 | | MHD_FN_PAR_INOUT_ (2) MHD_FN_PAR_INOUT_ (3) char * |
2503 | | get_buffer_for_size (char tmp1[mhd_STATIC_UNQ_BUFFER_SIZE], |
2504 | | char **restrict ptmp2, |
2505 | | size_t *restrict ptmp2_size, |
2506 | | size_t required_size) |
2507 | 0 | { |
2508 | 0 | mhd_assert ((0 == *ptmp2_size) || (NULL != *ptmp2)); |
2509 | 0 | mhd_assert ((NULL != *ptmp2) || (0 == *ptmp2_size)); |
2510 | 0 | mhd_assert ((0 == *ptmp2_size) \ |
2511 | 0 | || (mhd_STATIC_UNQ_BUFFER_SIZE < *ptmp2_size)); |
2512 | |
|
2513 | 0 | if (required_size <= mhd_STATIC_UNQ_BUFFER_SIZE) |
2514 | 0 | return tmp1; |
2515 | | |
2516 | 0 | if (required_size <= *ptmp2_size) |
2517 | 0 | return *ptmp2; |
2518 | | |
2519 | 0 | if (required_size > mhd_AUTH_DIGEST_MAX_PARAM_SIZE) |
2520 | 0 | return NULL; |
2521 | 0 | if (NULL != *ptmp2) |
2522 | 0 | free (*ptmp2); |
2523 | 0 | *ptmp2 = (char *)malloc (required_size); |
2524 | 0 | if (NULL == *ptmp2) |
2525 | 0 | *ptmp2_size = 0; |
2526 | 0 | else |
2527 | 0 | *ptmp2_size = required_size; |
2528 | 0 | return *ptmp2; |
2529 | 0 | } |
2530 | | |
2531 | | |
2532 | | /** |
2533 | | * The result of parameter unquoting |
2534 | | */ |
2535 | | enum mhd_GetUnqResult |
2536 | | { |
2537 | | mhd_UNQ_OK = MHD_DAUTH_OK, /**< Got unquoted string */ |
2538 | | mhd_UNQ_TOO_LARGE = MHD_DAUTH_TOO_LARGE, /**< The string is too large to unquote */ |
2539 | | mhd_UNQ_OUT_OF_MEM = MHD_DAUTH_ERROR /**< Out of memory error */ |
2540 | | }; |
2541 | | |
2542 | | /** |
2543 | | * Get Digest authorisation parameter as unquoted string. |
2544 | | * @param param the parameter to process |
2545 | | * @param[in,out] tmp1 the small buffer in stack |
2546 | | * @param[in,out] ptmp2 the pointer to pointer to malloc'ed buffer |
2547 | | * @param[in,out] ptmp2_size the pointer to the size of the buffer pointed by @a ptmp2 |
2548 | | * @param[out] unquoted the pointer to store the result, NOT zero terminated |
2549 | | * @return enum code indicating result of the process |
2550 | | */ |
2551 | | static MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
2552 | | MHD_FN_PAR_OUT_ (2) MHD_FN_PAR_INOUT_ (3) MHD_FN_PAR_INOUT_ (4) |
2553 | | MHD_FN_PAR_OUT_ (5) enum mhd_GetUnqResult |
2554 | | get_unquoted_param (const struct mhd_RqDAuthParam *param, |
2555 | | char tmp1[mhd_STATIC_UNQ_BUFFER_SIZE], |
2556 | | char **restrict ptmp2, |
2557 | | size_t *restrict ptmp2_size, |
2558 | | struct mhd_BufferConst *restrict unquoted) |
2559 | 0 | { |
2560 | 0 | char *str; |
2561 | 0 | size_t len; |
2562 | 0 | mhd_assert (NULL != param->value.cstr); |
2563 | 0 | mhd_assert (0 != param->value.len); |
2564 | |
|
2565 | 0 | if (!param->quoted) |
2566 | 0 | { |
2567 | 0 | unquoted->data = param->value.cstr; |
2568 | 0 | unquoted->size = param->value.len; |
2569 | 0 | return mhd_UNQ_OK; |
2570 | 0 | } |
2571 | | /* The value is present and is quoted, needs to be copied and unquoted */ |
2572 | 0 | str = get_buffer_for_size (tmp1, |
2573 | 0 | ptmp2, |
2574 | 0 | ptmp2_size, |
2575 | 0 | param->value.len); |
2576 | 0 | if (NULL == str) |
2577 | 0 | return (param->value.len > mhd_AUTH_DIGEST_MAX_PARAM_SIZE) ? |
2578 | 0 | mhd_UNQ_TOO_LARGE : mhd_UNQ_OUT_OF_MEM; |
2579 | | |
2580 | 0 | len = mhd_str_unquote (param->value.cstr, |
2581 | 0 | param->value.len, |
2582 | 0 | str); |
2583 | 0 | unquoted->data = str; |
2584 | 0 | unquoted->size = len; |
2585 | 0 | mhd_assert (0 != unquoted->size); |
2586 | 0 | mhd_assert (unquoted->size < param->value.len); |
2587 | 0 | return mhd_UNQ_OK; |
2588 | 0 | } |
2589 | | |
2590 | | |
2591 | | /** |
2592 | | * Get copy of Digest authorisation parameter as unquoted string. |
2593 | | * @param param the parameter to process |
2594 | | * @param[in,out] tmp1 the small buffer in stack |
2595 | | * @param[in,out] ptmp2 the pointer to pointer to malloc'ed buffer |
2596 | | * @param[in,out] ptmp2_size the pointer to the size of the buffer pointed by @a ptmp2 |
2597 | | * @param[out] unquoted the pointer to store the result, NOT zero terminated, |
2598 | | * but with enough space to zero-terminate |
2599 | | * @return enum code indicating result of the process |
2600 | | */ |
2601 | | static MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
2602 | | MHD_FN_PAR_OUT_ (2) MHD_FN_PAR_INOUT_ (3) MHD_FN_PAR_INOUT_ (4) |
2603 | | MHD_FN_PAR_OUT_ (5) enum mhd_GetUnqResult |
2604 | | get_unquoted_param_copy (const struct mhd_RqDAuthParam *param, |
2605 | | char tmp1[mhd_STATIC_UNQ_BUFFER_SIZE], |
2606 | | char **restrict ptmp2, |
2607 | | size_t *restrict ptmp2_size, |
2608 | | struct mhd_Buffer *restrict unquoted) |
2609 | 0 | { |
2610 | 0 | mhd_assert (NULL != param->value.cstr); |
2611 | 0 | mhd_assert (0 != param->value.len); |
2612 | | |
2613 | | /* The value is present and is quoted, needs to be copied and unquoted */ |
2614 | | /* Allocate buffer with one more additional byte for zero-termination */ |
2615 | 0 | unquoted->data = |
2616 | 0 | get_buffer_for_size (tmp1, |
2617 | 0 | ptmp2, |
2618 | 0 | ptmp2_size, |
2619 | 0 | param->value.len + 1); |
2620 | |
|
2621 | 0 | if (NULL == unquoted->data) |
2622 | 0 | return (param->value.len + 1 > mhd_AUTH_DIGEST_MAX_PARAM_SIZE) ? |
2623 | 0 | mhd_UNQ_TOO_LARGE : mhd_UNQ_OUT_OF_MEM; |
2624 | | |
2625 | 0 | if (!param->quoted) |
2626 | 0 | { |
2627 | 0 | memcpy (unquoted->data, |
2628 | 0 | param->value.cstr, |
2629 | 0 | param->value.len); |
2630 | 0 | unquoted->size = param->value.len; |
2631 | 0 | return mhd_UNQ_OK; |
2632 | 0 | } |
2633 | | |
2634 | 0 | unquoted->size = |
2635 | 0 | mhd_str_unquote (param->value.cstr, |
2636 | 0 | param->value.len, |
2637 | 0 | unquoted->data); |
2638 | 0 | mhd_assert (0 != unquoted->size); |
2639 | 0 | mhd_assert (unquoted->size < param->value.len); |
2640 | 0 | return mhd_UNQ_OK; |
2641 | 0 | } |
2642 | | |
2643 | | |
2644 | | /** |
2645 | | * Check whether Digest Auth request parameter is equal to given string |
2646 | | * @param param the parameter to check |
2647 | | * @param str_len the length of the @a str |
2648 | | * @param str the string to compare with, does not need to be zero-terminated |
2649 | | * @return true is parameter is equal to the given string, |
2650 | | * false otherwise |
2651 | | */ |
2652 | | mhd_static_inline MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
2653 | | MHD_FN_PAR_IN_SIZE_ (3, 2) bool |
2654 | | is_param_equal (const struct mhd_RqDAuthParam *restrict param, |
2655 | | const size_t str_len, |
2656 | | const char *restrict str) |
2657 | 0 | { |
2658 | 0 | mhd_assert (NULL != param->value.cstr); |
2659 | 0 | mhd_assert (0 != param->value.len); |
2660 | 0 | if (param->quoted) |
2661 | 0 | return mhd_str_equal_quoted_bin_n (param->value.cstr, |
2662 | 0 | param->value.len, |
2663 | 0 | str, |
2664 | 0 | str_len); |
2665 | 0 | return (str_len == param->value.len) |
2666 | 0 | && (0 == memcmp (str, param->value.cstr, str_len)); |
2667 | 0 | } |
2668 | | |
2669 | | |
2670 | | /** |
2671 | | * Check whether Digest Auth request parameter is caseless equal to given string |
2672 | | * @param param the parameter to check |
2673 | | * @param str_len the length of the @a str |
2674 | | * @param str the string to compare with, does not need to be zero-terminated |
2675 | | * @return true is parameter is caseless equal to the given string, |
2676 | | * false otherwise |
2677 | | */ |
2678 | | mhd_static_inline MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ |
2679 | | MHD_FN_PAR_IN_SIZE_ (3, 2) bool |
2680 | | is_param_equal_caseless (const struct mhd_RqDAuthParam *restrict param, |
2681 | | const size_t str_len, |
2682 | | const char *restrict str) |
2683 | 0 | { |
2684 | 0 | mhd_assert (NULL != param->value.cstr); |
2685 | 0 | mhd_assert (0 != param->value.len); |
2686 | 0 | if (param->quoted) |
2687 | 0 | return mhd_str_equal_caseless_quoted_bin_n (param->value.cstr, |
2688 | 0 | param->value.len, |
2689 | 0 | str, |
2690 | 0 | str_len); |
2691 | 0 | return (str_len == param->value.len) |
2692 | 0 | && (mhd_str_equal_caseless_bin_n (str, param->value.cstr, str_len)); |
2693 | 0 | } |
2694 | | |
2695 | | |
2696 | | /** |
2697 | | * Authenticates the authorization header sent by the client |
2698 | | * |
2699 | | * If RFC2069 mode is allowed by setting bit #MHD_DIGEST_AUTH_QOP_NONE in |
2700 | | * @a mqop and the client uses this mode, then server generated nonces are |
2701 | | * used as one-time nonces because nonce-count is not supported in this old RFC. |
2702 | | * Communication in this mode is very inefficient, especially if the client |
2703 | | * requests several resources one-by-one as for every request new nonce must be |
2704 | | * generated and client repeat all requests twice (the first time to get a new |
2705 | | * nonce and the second time to perform an authorised request). |
2706 | | * |
2707 | | * @param req the request handle |
2708 | | * @param realm the realm for authorization of the client |
2709 | | * @param username the username to be authenticated, must be in clear text |
2710 | | * even if userhash is used by the client |
2711 | | * @param password the password used in the authentication, |
2712 | | * must be NULL if @a userdigest is not NULL |
2713 | | * @param userdigest the precalculated binary hash of the string |
2714 | | * "username:realm:password", |
2715 | | * must be NULL if @a password is not NULL |
2716 | | * @param max_nc the maximum allowed nc (Nonce Count) value, if client's nc |
2717 | | * exceeds the specified value then MHD_DAUTH_NONCE_STALE is |
2718 | | * returned; |
2719 | | * unlike #digest_auth_check_all() zero is treated as "no limit" |
2720 | | * @param mqop the QOP to use |
2721 | | * @param malgo digest algorithms allowed to use, fail if algorithm specified |
2722 | | * by the client is not allowed by this parameter |
2723 | | * @param[out] pbuf the pointer to pointer to internally malloc'ed buffer, |
2724 | | * to be freed if not NULL upon return |
2725 | | * @return #MHD_DAUTH_OK if authenticated, |
2726 | | * error code otherwise. |
2727 | | * @ingroup authentication |
2728 | | */ |
2729 | | static MHD_FN_MUST_CHECK_RESULT_ |
2730 | | MHD_FN_PAR_NONNULL_ (1) |
2731 | | MHD_FN_PAR_NONNULL_ (2) MHD_FN_PAR_CSTR_ (2) |
2732 | | MHD_FN_PAR_NONNULL_ (3) MHD_FN_PAR_CSTR_ (3) |
2733 | | MHD_FN_PAR_CSTR_ (4) |
2734 | | enum MHD_DigestAuthResult |
2735 | | digest_auth_check_all_inner (struct MHD_Request *restrict req, |
2736 | | const char *restrict realm, |
2737 | | const char *restrict username, |
2738 | | const char *restrict password, |
2739 | | const uint8_t *restrict userdigest, |
2740 | | uint_fast32_t max_nc, |
2741 | | enum MHD_DigestAuthMultiQOP mqop, |
2742 | | enum MHD_DigestAuthMultiAlgo malgo, |
2743 | | char **pbuf, |
2744 | | struct DigestAlgorithm *da) |
2745 | 0 | { |
2746 | 0 | struct MHD_Daemon *const daemon = |
2747 | 0 | mhd_daemon_get_master_daemon ( |
2748 | 0 | mhd_CNTNR_PTR (req, struct MHD_Connection, rq)->daemon); |
2749 | 0 | enum MHD_DigestAuthAlgo c_algo; /**< Client's algorithm */ |
2750 | 0 | enum MHD_DigestAuthQOP c_qop; /**< Client's QOP */ |
2751 | 0 | unsigned int digest_size; |
2752 | 0 | uint8_t hash1_bin[mhd_MAX_DIGEST]; |
2753 | 0 | uint8_t hash2_bin[mhd_MAX_DIGEST]; |
2754 | 0 | uint_fast32_t nc; |
2755 | 0 | const struct mhd_AuthDigesReqParams *restrict params; |
2756 | | /** |
2757 | | * Temporal buffer in stack for unquoting and other needs |
2758 | | */ |
2759 | 0 | char tmp1[mhd_STATIC_UNQ_BUFFER_SIZE]; |
2760 | 0 | char **const ptmp2 = pbuf; /**< Temporal malloc'ed buffer for unquoting */ |
2761 | 0 | size_t tmp2_size; /**< The size of @a tmp2 buffer */ |
2762 | 0 | struct mhd_BufferConst unquoted; |
2763 | 0 | struct mhd_Buffer unq_copy; |
2764 | 0 | enum mhd_GetUnqResult unq_res; |
2765 | 0 | size_t username_len; |
2766 | 0 | size_t realm_len; |
2767 | |
|
2768 | 0 | mhd_assert ((NULL == password) != (NULL == userdigest)); |
2769 | |
|
2770 | 0 | tmp2_size = 0; |
2771 | |
|
2772 | 0 | if (1) |
2773 | 0 | { |
2774 | 0 | enum MHD_StatusCode res; |
2775 | |
|
2776 | 0 | res = get_rq_auth_digest_params (req); |
2777 | 0 | if (MHD_SC_OK != res) |
2778 | 0 | { |
2779 | 0 | if (MHD_SC_AUTH_ABSENT == res) |
2780 | 0 | return MHD_DAUTH_HEADER_MISSING; |
2781 | 0 | else if (MHD_SC_CONNECTION_POOL_NO_MEM_AUTH_DATA == res) |
2782 | 0 | return MHD_DAUTH_ERROR; |
2783 | 0 | else if (MHD_SC_REQ_AUTH_DATA_BROKEN == res) |
2784 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2785 | 0 | else |
2786 | 0 | mhd_UNREACHABLE (); |
2787 | 0 | } |
2788 | 0 | params = req->auth.digest.rqp; |
2789 | 0 | } |
2790 | 0 | mhd_assert (NULL != params); |
2791 | | |
2792 | | /* ** Initial parameters checks and setup ** */ |
2793 | | /* Get client's algorithm */ |
2794 | 0 | c_algo = params->algo; |
2795 | | /* Check whether client's algorithm is allowed by function parameter */ |
2796 | 0 | if (((unsigned int)c_algo) != |
2797 | 0 | (((unsigned int)c_algo) & ((unsigned int)malgo))) |
2798 | 0 | return MHD_DAUTH_WRONG_ALGO; |
2799 | | /* Check whether client's algorithm is supported */ |
2800 | 0 | if (0 != (((unsigned int)c_algo) & MHD_DIGEST_AUTH_ALGO_SESSION)) |
2801 | 0 | return MHD_DAUTH_UNSUPPORTED_ALGO; |
2802 | | #ifndef MHD_SUPPORT_MD5 |
2803 | | if (0 != (((unsigned int)c_algo) & MHD_DIGEST_BASE_ALGO_MD5)) |
2804 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
2805 | | #endif /* ! MHD_SUPPORT_MD5 */ |
2806 | | #ifndef MHD_SUPPORT_SHA256 |
2807 | | if (0 != (((unsigned int)c_algo) & MHD_DIGEST_BASE_ALGO_SHA256)) |
2808 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
2809 | | #endif /* ! MHD_SUPPORT_SHA256 */ |
2810 | | #ifndef MHD_SUPPORT_SHA512_256 |
2811 | | if (0 != (((unsigned int)c_algo) & MHD_DIGEST_BASE_ALGO_SHA512_256)) |
2812 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
2813 | | #endif /* ! MHD_SUPPORT_SHA512_256 */ |
2814 | 0 | if (!digest_init_one_time (da, get_base_digest_algo (c_algo))) |
2815 | 0 | mhd_UNREACHABLE (); |
2816 | | /* Check 'mqop' value */ |
2817 | 0 | c_qop = params->qop; |
2818 | | /* Check whether client's QOP is allowed by function parameter */ |
2819 | 0 | if (((unsigned int)c_qop) != |
2820 | 0 | (((unsigned int)c_qop) & ((unsigned int)mqop))) |
2821 | 0 | return MHD_DAUTH_WRONG_QOP; |
2822 | 0 | if (0 != (((unsigned int)c_qop) & MHD_DIGEST_AUTH_QOP_AUTH_INT)) |
2823 | 0 | return MHD_DAUTH_UNSUPPORTED_QOP; |
2824 | | |
2825 | 0 | digest_size = digest_get_size (da); |
2826 | | |
2827 | | /* ** A quick check for presence of all required parameters ** */ |
2828 | |
|
2829 | 0 | if ((NULL == params->username.value.cstr) |
2830 | 0 | && (NULL == params->username_ext.value.cstr)) |
2831 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2832 | 0 | else if ((NULL != params->username.value.cstr) |
2833 | 0 | && (NULL != params->username_ext.value.cstr)) |
2834 | 0 | return MHD_DAUTH_HEADER_BROKEN; /* Parameters cannot be used together */ |
2835 | 0 | else if ((NULL != params->username_ext.value.cstr) |
2836 | 0 | && (mhd_DAUTH_EXT_PARAM_MIN_LEN > params->username_ext.value.len)) |
2837 | 0 | return MHD_DAUTH_HEADER_BROKEN; /* Broken extended notation */ |
2838 | 0 | else if (params->userhash && (NULL == params->username.value.cstr)) |
2839 | 0 | return MHD_DAUTH_HEADER_BROKEN; /* Userhash cannot be used with extended notation */ |
2840 | 0 | else if (params->userhash && (digest_size * 2 > params->username.value.len)) |
2841 | 0 | return MHD_DAUTH_WRONG_USERNAME; /* Too few chars for correct userhash */ |
2842 | 0 | else if (params->userhash && (digest_size * 4 < params->username.value.len)) |
2843 | 0 | return MHD_DAUTH_WRONG_USERNAME; /* Too many chars for correct userhash */ |
2844 | | |
2845 | 0 | if (NULL == params->realm.value.cstr) |
2846 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2847 | 0 | else if (((NULL == userdigest) || params->userhash) |
2848 | 0 | && (mhd_AUTH_DIGEST_MAX_PARAM_SIZE < params->realm.value.len)) |
2849 | 0 | return MHD_DAUTH_TOO_LARGE; /* Realm is too large and should be used in hash calculations */ |
2850 | | |
2851 | 0 | if (MHD_DIGEST_AUTH_QOP_NONE != c_qop) |
2852 | 0 | { |
2853 | 0 | if (NULL == params->nc.value.cstr) |
2854 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2855 | 0 | else if (0 == params->nc.value.len) |
2856 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2857 | 0 | else if (4 * 8 < params->nc.value.len) /* Four times more than needed */ |
2858 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2859 | | |
2860 | 0 | if (NULL == params->cnonce.value.cstr) |
2861 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2862 | 0 | else if (0 == params->cnonce.value.len) |
2863 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2864 | 0 | else if (mhd_AUTH_DIGEST_MAX_PARAM_SIZE < params->cnonce.value.len) |
2865 | 0 | return MHD_DAUTH_TOO_LARGE; |
2866 | 0 | } |
2867 | | |
2868 | | /* The QOP parameter was checked already */ |
2869 | | |
2870 | 0 | if (NULL == params->uri.value.cstr) |
2871 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2872 | 0 | else if (0 == params->uri.value.len) |
2873 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2874 | 0 | else if (mhd_AUTH_DIGEST_MAX_PARAM_SIZE < params->uri.value.len) |
2875 | 0 | return MHD_DAUTH_TOO_LARGE; |
2876 | | |
2877 | 0 | if (NULL == params->nonce.value.cstr) |
2878 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2879 | 0 | else if (0 == params->nonce.value.len) |
2880 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2881 | 0 | else if (mhd_AUTH_DIGEST_NONCE_LEN * 2 < params->nonce.value.len) |
2882 | 0 | return MHD_DAUTH_NONCE_WRONG; |
2883 | | |
2884 | 0 | if (NULL == params->response.value.cstr) |
2885 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2886 | 0 | else if (0 == params->response.value.len) |
2887 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2888 | 0 | else if (digest_size * 4 < params->response.value.len) |
2889 | 0 | return MHD_DAUTH_RESPONSE_WRONG; |
2890 | | |
2891 | | /* ** Check simple parameters match ** */ |
2892 | | |
2893 | | /* Check 'algorithm' */ |
2894 | | /* The 'algorithm' was checked at the start of the function */ |
2895 | | /* 'algorithm' valid */ |
2896 | | |
2897 | | /* Check 'qop' */ |
2898 | | /* The 'qop' was checked at the start of the function */ |
2899 | | /* 'qop' valid */ |
2900 | | |
2901 | | /* Check 'realm' */ |
2902 | 0 | realm_len = strlen (realm); |
2903 | 0 | if (!is_param_equal (¶ms->realm, |
2904 | 0 | realm_len, |
2905 | 0 | realm)) |
2906 | 0 | return MHD_DAUTH_WRONG_REALM; |
2907 | | /* 'realm' valid */ |
2908 | | |
2909 | | /* Check 'username' */ |
2910 | 0 | username_len = strlen (username); |
2911 | 0 | if (!params->userhash) |
2912 | 0 | { |
2913 | 0 | if (NULL != params->username.value.cstr) |
2914 | 0 | { /* Username in standard notation */ |
2915 | 0 | if (!is_param_equal (¶ms->username, username_len, username)) |
2916 | 0 | return MHD_DAUTH_WRONG_USERNAME; |
2917 | 0 | } |
2918 | 0 | else |
2919 | 0 | { /* Username in extended notation */ |
2920 | 0 | char *r_uname; |
2921 | 0 | size_t buf_size = params->username_ext.value.len; |
2922 | 0 | ssize_t res; |
2923 | |
|
2924 | 0 | mhd_assert (NULL != params->username_ext.value.cstr); |
2925 | 0 | mhd_assert (mhd_DAUTH_EXT_PARAM_MIN_LEN <= buf_size); /* It was checked already */ |
2926 | 0 | buf_size += 1; /* For zero-termination */ |
2927 | 0 | buf_size -= mhd_DAUTH_EXT_PARAM_MIN_LEN; |
2928 | 0 | r_uname = get_buffer_for_size (tmp1, ptmp2, &tmp2_size, buf_size); |
2929 | 0 | if (NULL == r_uname) |
2930 | 0 | return (mhd_AUTH_DIGEST_MAX_PARAM_SIZE < buf_size) ? |
2931 | 0 | MHD_DAUTH_TOO_LARGE : MHD_DAUTH_ERROR; |
2932 | 0 | res = get_rq_extended_uname_copy_z (params->username_ext.value.cstr, |
2933 | 0 | params->username_ext.value.len, |
2934 | 0 | r_uname, buf_size); |
2935 | 0 | if (0 > res) |
2936 | 0 | return MHD_DAUTH_HEADER_BROKEN; /* Broken extended notation */ |
2937 | 0 | if ((username_len != (size_t)res) |
2938 | 0 | || (0 != memcmp (username, r_uname, username_len))) |
2939 | 0 | return MHD_DAUTH_WRONG_USERNAME; |
2940 | 0 | } |
2941 | 0 | } |
2942 | 0 | else |
2943 | 0 | { /* Userhash */ |
2944 | 0 | mhd_assert (NULL != params->username.value.cstr); |
2945 | 0 | calc_userhash (da, |
2946 | 0 | username_len, |
2947 | 0 | username, |
2948 | 0 | realm_len, |
2949 | 0 | realm, |
2950 | 0 | hash1_bin); |
2951 | 0 | if (digest_has_error (da)) |
2952 | 0 | return MHD_DAUTH_ERROR; |
2953 | 0 | mhd_assert (sizeof (tmp1) >= (2 * digest_size)); |
2954 | 0 | mhd_bin_to_hex (hash1_bin, digest_size, tmp1); |
2955 | 0 | if (!is_param_equal_caseless (¶ms->username, 2 * digest_size, tmp1)) |
2956 | 0 | return MHD_DAUTH_WRONG_USERNAME; |
2957 | | /* To simplify the logic, the digest is reset here instead of resetting |
2958 | | before the next hash calculation. */ |
2959 | 0 | digest_reset (da); |
2960 | 0 | } |
2961 | | /* 'username' valid */ |
2962 | | |
2963 | | /* ** Do basic nonce and nonce-counter checks (size, timestamp) ** */ |
2964 | | |
2965 | | /* Get 'nc' digital value */ |
2966 | 0 | nc = 0; |
2967 | 0 | switch (get_rq_nc (params, |
2968 | 0 | &nc)) |
2969 | 0 | { |
2970 | 0 | case mhd_GET_RQ_NC_NONE: |
2971 | 0 | if (MHD_DIGEST_AUTH_QOP_NONE != c_qop) |
2972 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2973 | 0 | nc = 1; /* Force 'nc' value */ |
2974 | 0 | break; |
2975 | 0 | case mhd_GET_RQ_NC_VALID: |
2976 | 0 | if (MHD_DIGEST_AUTH_QOP_NONE == c_qop) |
2977 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2978 | 0 | break; |
2979 | 0 | case mhd_GET_RQ_NC_TOO_LONG: |
2980 | 0 | case mhd_GET_RQ_NC_TOO_LARGE: |
2981 | 0 | return MHD_DAUTH_NONCE_STALE; |
2982 | 0 | break; |
2983 | 0 | case mhd_GET_RQ_NC_BROKEN: |
2984 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2985 | 0 | break; |
2986 | 0 | default: |
2987 | 0 | mhd_UNREACHABLE (); |
2988 | 0 | break; |
2989 | 0 | } |
2990 | 0 | if (0 == nc) |
2991 | 0 | return MHD_DAUTH_HEADER_BROKEN; |
2992 | 0 | if (0 == max_nc) |
2993 | 0 | max_nc = daemon->auth_dg.cfg.def_max_nc; |
2994 | 0 | if (max_nc < nc) |
2995 | 0 | return MHD_DAUTH_NONCE_STALE; /* Too large 'nc' value */ |
2996 | | /* Got 'nc' digital value */ |
2997 | | |
2998 | | /* Get 'nonce' with basic checks */ |
2999 | 0 | unq_res = get_unquoted_param (¶ms->nonce, tmp1, ptmp2, &tmp2_size, |
3000 | 0 | &unquoted); |
3001 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3002 | 0 | return MHD_DAUTH_TOO_LARGE; |
3003 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3004 | 0 | return MHD_DAUTH_ERROR; |
3005 | | |
3006 | | |
3007 | 0 | switch (check_nonce_nc (daemon, |
3008 | 0 | unquoted.size, |
3009 | 0 | unquoted.data, |
3010 | 0 | nc, |
3011 | 0 | (uint_fast32_t) |
3012 | 0 | ((mhd_monotonic_msec_counter () / 1000) |
3013 | 0 | & UINT32_C (0xFFFFFFFF)))) |
3014 | 0 | { |
3015 | 0 | case mhd_CHECK_NONCENC_OK: |
3016 | 0 | break; |
3017 | 0 | case mhd_CHECK_NONCENC_STALE: |
3018 | 0 | return MHD_DAUTH_NONCE_STALE; |
3019 | 0 | case mhd_CHECK_NONCENC_WRONG: |
3020 | 0 | return MHD_DAUTH_NONCE_WRONG; |
3021 | 0 | default: |
3022 | 0 | mhd_UNREACHABLE (); |
3023 | 0 | break; |
3024 | 0 | } |
3025 | | /* The nonce was generated by MHD, is not stale and nonce-nc combination has |
3026 | | not been used before */ |
3027 | | |
3028 | | /* ** Build H(A2) and check URI match in the header and in the request ** */ |
3029 | | |
3030 | | /* Get 'uri' */ |
3031 | 0 | mhd_assert (!da->hashing); |
3032 | 0 | digest_update (da, req->method.len, req->method.cstr); |
3033 | 0 | digest_update_with_colon (da); |
3034 | | #if 0 |
3035 | | /* TODO: add support for "auth-int" */ |
3036 | | digest_update_str (da, hentity); |
3037 | | digest_update_with_colon (da); |
3038 | | #endif |
3039 | 0 | unq_res = get_unquoted_param_copy (¶ms->uri, tmp1, ptmp2, &tmp2_size, |
3040 | 0 | &unq_copy); |
3041 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3042 | 0 | return MHD_DAUTH_TOO_LARGE; |
3043 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3044 | 0 | return MHD_DAUTH_ERROR; |
3045 | | |
3046 | 0 | digest_update_buf (da, &unq_copy); |
3047 | | /* The next check will modify copied URI string */ |
3048 | 0 | if (!check_uri_match (req, unq_copy.size, unq_copy.data)) |
3049 | 0 | return MHD_DAUTH_WRONG_URI; |
3050 | 0 | digest_calc_hash (da, hash2_bin); |
3051 | | #ifdef mhd_DIGEST_HAS_EXT_ERROR |
3052 | | /* Skip digest calculation external error check, the next one checks both */ |
3053 | | #endif /* mhd_DIGEST_HAS_EXT_ERROR */ |
3054 | | /* Got H(A2) */ |
3055 | | |
3056 | | /* ** Build H(A1) ** */ |
3057 | 0 | if (NULL == userdigest) |
3058 | 0 | { |
3059 | 0 | mhd_assert (!da->hashing); |
3060 | 0 | digest_reset (da); |
3061 | 0 | calc_userdigest (da, |
3062 | 0 | username, username_len, |
3063 | 0 | realm, realm_len, |
3064 | 0 | password, |
3065 | 0 | hash1_bin); |
3066 | 0 | } |
3067 | | /* TODO: support '-sess' versions */ |
3068 | | #ifdef mhd_DIGEST_HAS_EXT_ERROR |
3069 | | if (digest_has_error (da)) |
3070 | | return MHD_DAUTH_ERROR; |
3071 | | #endif /* mhd_DIGEST_HAS_EXT_ERROR */ |
3072 | | /* Got H(A1) */ |
3073 | | |
3074 | | /* ** Check 'response' ** */ |
3075 | |
|
3076 | 0 | mhd_assert (!da->hashing); |
3077 | 0 | digest_reset (da); |
3078 | | /* Update digest with H(A1) */ |
3079 | 0 | mhd_assert (sizeof (tmp1) >= (digest_size * 2)); |
3080 | 0 | if (NULL == userdigest) |
3081 | 0 | mhd_bin_to_hex (hash1_bin, digest_size, tmp1); |
3082 | 0 | else |
3083 | 0 | mhd_bin_to_hex (userdigest, digest_size, tmp1); |
3084 | 0 | digest_update (da, digest_size * 2, (const uint8_t *)tmp1); |
3085 | | |
3086 | | /* H(A1) is not needed anymore, reuse the buffer. |
3087 | | * Use hash1_bin for the client's 'response' decoded to binary form. */ |
3088 | 0 | unq_res = get_unquoted_param (¶ms->response, tmp1, ptmp2, &tmp2_size, |
3089 | 0 | &unquoted); |
3090 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3091 | 0 | return MHD_DAUTH_TOO_LARGE; |
3092 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3093 | 0 | return MHD_DAUTH_ERROR; |
3094 | 0 | if (digest_size != mhd_hex_to_bin (unquoted.data, unquoted.size, hash1_bin)) |
3095 | 0 | return MHD_DAUTH_RESPONSE_WRONG; |
3096 | | |
3097 | | /* Update digest with ':' */ |
3098 | 0 | digest_update_with_colon (da); |
3099 | | /* Update digest with 'nonce' text value */ |
3100 | 0 | unq_res = get_unquoted_param (¶ms->nonce, tmp1, ptmp2, &tmp2_size, |
3101 | 0 | &unquoted); |
3102 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3103 | 0 | return MHD_DAUTH_TOO_LARGE; |
3104 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3105 | 0 | return MHD_DAUTH_ERROR; |
3106 | 0 | digest_update_cbuf (da, &unquoted); |
3107 | | /* Update digest with ':' */ |
3108 | 0 | digest_update_with_colon (da); |
3109 | 0 | if (MHD_DIGEST_AUTH_QOP_NONE != c_qop) |
3110 | 0 | { |
3111 | | /* Update digest with 'nc' text value */ |
3112 | 0 | unq_res = get_unquoted_param (¶ms->nc, tmp1, ptmp2, &tmp2_size, |
3113 | 0 | &unquoted); |
3114 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3115 | 0 | return MHD_DAUTH_TOO_LARGE; |
3116 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3117 | 0 | return MHD_DAUTH_ERROR; |
3118 | 0 | digest_update_cbuf (da, &unquoted); |
3119 | | /* Update digest with ':' */ |
3120 | 0 | digest_update_with_colon (da); |
3121 | | /* Update digest with 'cnonce' value */ |
3122 | 0 | unq_res = get_unquoted_param (¶ms->cnonce, tmp1, ptmp2, &tmp2_size, |
3123 | 0 | &unquoted); |
3124 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3125 | 0 | return MHD_DAUTH_TOO_LARGE; |
3126 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3127 | 0 | return MHD_DAUTH_ERROR; |
3128 | 0 | digest_update_cbuf (da, &unquoted); |
3129 | | /* Update digest with ':' */ |
3130 | 0 | digest_update_with_colon (da); |
3131 | | /* Update digest with 'qop' value */ |
3132 | 0 | unq_res = get_unquoted_param (¶ms->qop_raw, tmp1, ptmp2, &tmp2_size, |
3133 | 0 | &unquoted); |
3134 | 0 | if (mhd_UNQ_TOO_LARGE == unq_res) |
3135 | 0 | return MHD_DAUTH_TOO_LARGE; |
3136 | 0 | if (mhd_UNQ_OUT_OF_MEM == unq_res) |
3137 | 0 | return MHD_DAUTH_ERROR; |
3138 | 0 | digest_update_cbuf (da, &unquoted); |
3139 | | /* Update digest with ':' */ |
3140 | 0 | digest_update_with_colon (da); |
3141 | 0 | } |
3142 | | /* Update digest with H(A2) */ |
3143 | 0 | mhd_bin_to_hex (hash2_bin, digest_size, tmp1); |
3144 | 0 | digest_update (da, digest_size * 2, (const uint8_t *)tmp1); |
3145 | | |
3146 | | /* H(A2) is not needed anymore, reuse the buffer. |
3147 | | * Use hash2_bin for the calculated response in binary form */ |
3148 | 0 | digest_calc_hash (da, hash2_bin); |
3149 | | #ifdef mhd_DIGEST_HAS_EXT_ERROR |
3150 | | if (digest_has_error (da)) |
3151 | | return MHD_DAUTH_ERROR; |
3152 | | #endif /* mhd_DIGEST_HAS_EXT_ERROR */ |
3153 | |
|
3154 | 0 | if (0 != memcmp (hash1_bin, hash2_bin, digest_size)) |
3155 | 0 | return MHD_DAUTH_RESPONSE_WRONG; |
3156 | | |
3157 | 0 | return MHD_DAUTH_OK; |
3158 | 0 | } |
3159 | | |
3160 | | |
3161 | | /** |
3162 | | * Authenticates the authorization header sent by the client |
3163 | | * |
3164 | | * If RFC2069 mode is allowed by setting bit #MHD_DIGEST_AUTH_QOP_NONE in |
3165 | | * @a mqop and the client uses this mode, then server generated nonces are |
3166 | | * used as one-time nonces because nonce-count is not supported in this old RFC. |
3167 | | * Communication in this mode is very inefficient, especially if the client |
3168 | | * requests several resources one-by-one as for every request new nonce must be |
3169 | | * generated and client repeat all requests twice (the first time to get a new |
3170 | | * nonce and the second time to perform an authorised request). |
3171 | | * |
3172 | | * @param req the request handle |
3173 | | * @param realm the realm for authorization of the client |
3174 | | * @param username the username to be authenticated, must be in clear text |
3175 | | * even if userhash is used by the client |
3176 | | * @param password the password used in the authentication, |
3177 | | * must be NULL if @a userdigest is not NULL |
3178 | | * @param userdigest the precalculated binary hash of the string |
3179 | | * "username:realm:password", |
3180 | | * must be NULL if @a password is not NULL |
3181 | | * @param max_nc the maximum allowed nc (Nonce Count) value, if client's nc |
3182 | | * exceeds the specified value then MHD_DAUTH_NONCE_STALE is |
3183 | | * returned; |
3184 | | * if set to zero then daemon's default value is used |
3185 | | * @param mqop the QOP to use |
3186 | | * @param malgo digest algorithms allowed to use, fail if algorithm specified |
3187 | | * by the client is not allowed by this parameter |
3188 | | * @return #MHD_DAUTH_OK if authenticated, |
3189 | | * error code otherwise. |
3190 | | * @ingroup authentication |
3191 | | */ |
3192 | | static enum MHD_DigestAuthResult |
3193 | | digest_auth_check_all (struct MHD_Request *restrict req, |
3194 | | const char *restrict realm, |
3195 | | const char *restrict username, |
3196 | | const char *restrict password, |
3197 | | const uint8_t *restrict userdigest, |
3198 | | uint_fast32_t max_nc, |
3199 | | enum MHD_DigestAuthMultiQOP mqop, |
3200 | | enum MHD_DigestAuthMultiAlgo malgo) |
3201 | 0 | { |
3202 | 0 | enum MHD_DigestAuthResult res; |
3203 | 0 | char *buf; |
3204 | 0 | struct DigestAlgorithm da; |
3205 | |
|
3206 | 0 | buf = NULL; |
3207 | 0 | digest_setup_zero (&da); |
3208 | 0 | res = digest_auth_check_all_inner (req, |
3209 | 0 | realm, |
3210 | 0 | username, |
3211 | 0 | password, |
3212 | 0 | userdigest, |
3213 | 0 | max_nc, |
3214 | 0 | mqop, |
3215 | 0 | malgo, |
3216 | 0 | &buf, |
3217 | 0 | &da); |
3218 | 0 | digest_deinit (&da); |
3219 | 0 | if (NULL != buf) |
3220 | 0 | free (buf); |
3221 | |
|
3222 | 0 | return res; |
3223 | 0 | } |
3224 | | |
3225 | | |
3226 | | /** |
3227 | | * Authenticates the authorization header sent by the client. |
3228 | | * |
3229 | | * If RFC2069 mode is allowed by setting bit #MHD_DIGEST_AUTH_QOP_NONE in |
3230 | | * @a mqop and the client uses this mode, then server generated nonces are |
3231 | | * used as one-time nonces because nonce-count is not supported in this old RFC. |
3232 | | * Communication in this mode is very inefficient, especially if the client |
3233 | | * requests several resources one-by-one as for every request a new nonce must |
3234 | | * be generated and client repeats all requests twice (first time to get a new |
3235 | | * nonce and second time to perform an authorised request). |
3236 | | * |
3237 | | * @param request the request |
3238 | | * @param realm the realm for authorization of the client |
3239 | | * @param username the username to be authenticated, must be in clear text |
3240 | | * even if userhash is used by the client |
3241 | | * @param password the password matching the @a username (and the @a realm) |
3242 | | * @param max_nc the maximum allowed nc (Nonce Count) value, if client's nc |
3243 | | * exceeds the specified value then MHD_DAUTH_NONCE_STALE is |
3244 | | * returned; |
3245 | | * if zero is specified then daemon default value is used. |
3246 | | * @param mqop the QOP to use |
3247 | | * @param malgo digest algorithms allowed to use, fail if algorithm used |
3248 | | * by the client is not allowed by this parameter |
3249 | | * @return #MHD_DAUTH_OK if authenticated, |
3250 | | * the error code otherwise |
3251 | | * @ingroup authentication |
3252 | | */ |
3253 | | MHD_EXTERN_ MHD_FN_PAR_NONNULL_ALL_ |
3254 | | MHD_FN_PAR_CSTR_ (2) MHD_FN_PAR_CSTR_ (3) MHD_FN_PAR_CSTR_ (4) |
3255 | | enum MHD_DigestAuthResult |
3256 | | MHD_digest_auth_check (struct MHD_Request *MHD_RESTRICT request, |
3257 | | const char *MHD_RESTRICT realm, |
3258 | | const char *MHD_RESTRICT username, |
3259 | | const char *MHD_RESTRICT password, |
3260 | | uint_fast32_t max_nc, |
3261 | | enum MHD_DigestAuthMultiQOP mqop, |
3262 | | enum MHD_DigestAuthMultiAlgo malgo) |
3263 | 0 | { |
3264 | 0 | return digest_auth_check_all (request, |
3265 | 0 | realm, |
3266 | 0 | username, |
3267 | 0 | password, |
3268 | 0 | NULL, |
3269 | 0 | max_nc, |
3270 | 0 | mqop, |
3271 | 0 | malgo); |
3272 | 0 | } |
3273 | | |
3274 | | |
3275 | | MHD_EXTERN_ MHD_FN_PAR_NONNULL_ALL_ |
3276 | | MHD_FN_PAR_CSTR_ (2) |
3277 | | MHD_FN_PAR_CSTR_ (3) |
3278 | | MHD_FN_PAR_IN_SIZE_ (5, 4) enum MHD_DigestAuthResult |
3279 | | MHD_digest_auth_check_digest (struct MHD_Request *MHD_RESTRICT request, |
3280 | | const char *MHD_RESTRICT realm, |
3281 | | const char *MHD_RESTRICT username, |
3282 | | size_t userdigest_size, |
3283 | | const void *MHD_RESTRICT userdigest, |
3284 | | uint_fast32_t max_nc, |
3285 | | enum MHD_DigestAuthMultiQOP mqop, |
3286 | | enum MHD_DigestAuthMultiAlgo malgo) |
3287 | 0 | { |
3288 | 0 | if (1 != (((0 != (((unsigned int)malgo) \ |
3289 | 0 | & MHD_DIGEST_BASE_ALGO_MD5)) ? 1 : 0) |
3290 | 0 | + ((0 != (((unsigned int)malgo) \ |
3291 | 0 | & MHD_DIGEST_BASE_ALGO_SHA256)) ? 1 : 0) |
3292 | 0 | + ((0 != (((unsigned int)malgo) \ |
3293 | 0 | & MHD_DIGEST_BASE_ALGO_SHA512_256)) ? 1 : 0))) |
3294 | 0 | return MHD_DAUTH_UNSUPPORTED_ALGO; |
3295 | | |
3296 | | #ifndef MHD_SUPPORT_MD5 |
3297 | | if (0 != (((unsigned int)malgo) & MHD_DIGEST_BASE_ALGO_MD5)) |
3298 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
3299 | | #endif /* ! MHD_SUPPORT_MD5 */ |
3300 | | #ifndef MHD_SUPPORT_SHA256 |
3301 | | if (0 != (((unsigned int)malgo) & MHD_DIGEST_BASE_ALGO_SHA256)) |
3302 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
3303 | | #endif /* ! MHD_SUPPORT_SHA256 */ |
3304 | | #ifndef MHD_SUPPORT_SHA512_256 |
3305 | | if (0 != (((unsigned int)malgo) & MHD_DIGEST_BASE_ALGO_SHA512_256)) |
3306 | | return MHD_DAUTH_UNSUPPORTED_ALGO; |
3307 | | #endif /* ! MHD_SUPPORT_SHA512_256 */ |
3308 | | |
3309 | 0 | if (digest_get_hash_size ((enum MHD_DigestAuthAlgo)malgo) != |
3310 | 0 | userdigest_size) |
3311 | 0 | return MHD_DAUTH_INVALID_USERDIGEST_SIZE; |
3312 | | |
3313 | 0 | return digest_auth_check_all (request, |
3314 | 0 | realm, |
3315 | 0 | username, |
3316 | | NULL, |
3317 | 0 | (const uint8_t *)userdigest, |
3318 | 0 | max_nc, |
3319 | 0 | mqop, |
3320 | 0 | malgo); |
3321 | 0 | } |