diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-07-19 17:42:42 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-07-19 17:50:07 +0300 |
commit | 1a8a2b8d19758a59eb2bf0a4c981040250ba4e48 (patch) | |
tree | 65b570040b338dd1cc350c56bbe265b3856732bf | |
parent | f826bbca441828e7b1928f425f4777951290e7c7 (diff) | |
download | libmicrohttpd-1a8a2b8d19758a59eb2bf0a4c981040250ba4e48.tar.gz libmicrohttpd-1a8a2b8d19758a59eb2bf0a4c981040250ba4e48.zip |
digest: small internal refactoring to clarify used hash type
-rw-r--r-- | src/microhttpd/digestauth.c | 70 |
1 files changed, 49 insertions, 21 deletions
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index d282aa62..35dd0264 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -234,10 +234,11 @@ struct DigestAlgorithm | |||
234 | * Digest in binary form. | 234 | * Digest in binary form. |
235 | */ | 235 | */ |
236 | union DigestBin digest; | 236 | union DigestBin digest; |
237 | |||
237 | /** | 238 | /** |
238 | * The digest algorithm. | 239 | * The hash calculation algorithm. |
239 | */ | 240 | */ |
240 | enum MHD_DigestAuthAlgorithm algo; | 241 | enum MHD_DigestBaseAlgo algo; |
241 | 242 | ||
242 | /** | 243 | /** |
243 | * Buffer for hex-print of the final digest. | 244 | * Buffer for hex-print of the final digest. |
@@ -259,9 +260,9 @@ _MHD_static_inline const char * | |||
259 | digest_get_algo_name (struct DigestAlgorithm *da) | 260 | digest_get_algo_name (struct DigestAlgorithm *da) |
260 | { | 261 | { |
261 | mhd_assert (da->setup); | 262 | mhd_assert (da->setup); |
262 | if (MHD_DIGEST_ALG_MD5 == da->algo) | 263 | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
263 | return _MHD_MD5_TOKEN; | 264 | return _MHD_MD5_TOKEN; |
264 | if (MHD_DIGEST_ALG_SHA256 == da->algo) | 265 | if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
265 | return _MHD_SHA256_TOKEN; | 266 | return _MHD_SHA256_TOKEN; |
266 | mhd_assert (0); /* May not happen */ | 267 | mhd_assert (0); /* May not happen */ |
267 | return ""; | 268 | return ""; |
@@ -277,9 +278,9 @@ _MHD_static_inline unsigned int | |||
277 | digest_get_size (struct DigestAlgorithm *da) | 278 | digest_get_size (struct DigestAlgorithm *da) |
278 | { | 279 | { |
279 | mhd_assert (da->setup); | 280 | mhd_assert (da->setup); |
280 | if (MHD_DIGEST_ALG_MD5 == da->algo) | 281 | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
281 | return MD5_DIGEST_SIZE; | 282 | return MD5_DIGEST_SIZE; |
282 | if (MHD_DIGEST_ALG_SHA256 == da->algo) | 283 | if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
283 | return SHA256_DIGEST_SIZE; | 284 | return SHA256_DIGEST_SIZE; |
284 | mhd_assert (0); /* May not happen */ | 285 | mhd_assert (0); /* May not happen */ |
285 | return 0; | 286 | return 0; |
@@ -295,18 +296,15 @@ digest_get_size (struct DigestAlgorithm *da) | |||
295 | */ | 296 | */ |
296 | _MHD_static_inline bool | 297 | _MHD_static_inline bool |
297 | digest_setup (struct DigestAlgorithm *da, | 298 | digest_setup (struct DigestAlgorithm *da, |
298 | enum MHD_DigestAuthAlgorithm algo) | 299 | enum MHD_DigestBaseAlgo algo) |
299 | { | 300 | { |
300 | #ifdef _DEBUG | 301 | #ifdef _DEBUG |
301 | da->setup = false; | 302 | da->setup = false; |
302 | da->inited = false; | 303 | da->inited = false; |
303 | da->digest_calculated = false; | 304 | da->digest_calculated = false; |
304 | #endif /* _DEBUG */ | 305 | #endif /* _DEBUG */ |
305 | if (MHD_DIGEST_ALG_AUTO == algo) | 306 | if ((MHD_DIGEST_BASE_ALGO_MD5 == algo) || |
306 | algo = MHD_DIGEST_ALG_SHA256; | 307 | (MHD_DIGEST_BASE_ALGO_SHA256 == algo)) |
307 | |||
308 | if ((MHD_DIGEST_ALG_MD5 == algo) || | ||
309 | (MHD_DIGEST_ALG_SHA256 == algo)) | ||
310 | { | 308 | { |
311 | da->algo = algo; | 309 | da->algo = algo; |
312 | #ifdef _DEBUG | 310 | #ifdef _DEBUG |
@@ -330,14 +328,14 @@ digest_init (struct DigestAlgorithm *da) | |||
330 | #ifdef _DEBUG | 328 | #ifdef _DEBUG |
331 | da->digest_calculated = false; | 329 | da->digest_calculated = false; |
332 | #endif | 330 | #endif |
333 | if (MHD_DIGEST_ALG_MD5 == da->algo) | 331 | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
334 | { | 332 | { |
335 | MHD_MD5Init (&da->ctx.md5_ctx); | 333 | MHD_MD5Init (&da->ctx.md5_ctx); |
336 | #ifdef _DEBUG | 334 | #ifdef _DEBUG |
337 | da->inited = true; | 335 | da->inited = true; |
338 | #endif | 336 | #endif |
339 | } | 337 | } |
340 | else if (MHD_DIGEST_ALG_SHA256 == da->algo) | 338 | else if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
341 | { | 339 | { |
342 | MHD_SHA256_init (&da->ctx.sha256_ctx); | 340 | MHD_SHA256_init (&da->ctx.sha256_ctx); |
343 | #ifdef _DEBUG | 341 | #ifdef _DEBUG |
@@ -367,9 +365,9 @@ digest_update (struct DigestAlgorithm *da, | |||
367 | { | 365 | { |
368 | mhd_assert (da->inited); | 366 | mhd_assert (da->inited); |
369 | mhd_assert (! da->digest_calculated); | 367 | mhd_assert (! da->digest_calculated); |
370 | if (MHD_DIGEST_ALG_MD5 == da->algo) | 368 | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
371 | MHD_MD5Update (&da->ctx.md5_ctx, data, length); | 369 | MHD_MD5Update (&da->ctx.md5_ctx, data, length); |
372 | else if (MHD_DIGEST_ALG_SHA256 == da->algo) | 370 | else if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
373 | MHD_SHA256_update (&da->ctx.sha256_ctx, data, length); | 371 | MHD_SHA256_update (&da->ctx.sha256_ctx, data, length); |
374 | else | 372 | else |
375 | mhd_assert (0); /* May not happen */ | 373 | mhd_assert (0); /* May not happen */ |
@@ -399,9 +397,9 @@ digest_calc_hash (struct DigestAlgorithm *da) | |||
399 | { | 397 | { |
400 | mhd_assert (da->inited); | 398 | mhd_assert (da->inited); |
401 | mhd_assert (! da->digest_calculated); | 399 | mhd_assert (! da->digest_calculated); |
402 | if (MHD_DIGEST_ALG_MD5 == da->algo) | 400 | if (MHD_DIGEST_BASE_ALGO_MD5 == da->algo) |
403 | MHD_MD5Final (&da->ctx.md5_ctx, da->digest.md5); | 401 | MHD_MD5Final (&da->ctx.md5_ctx, da->digest.md5); |
404 | else if (MHD_DIGEST_ALG_SHA256 == da->algo) | 402 | else if (MHD_DIGEST_BASE_ALGO_SHA256 == da->algo) |
405 | MHD_SHA256_finish (&da->ctx.sha256_ctx, da->digest.sha256); | 403 | MHD_SHA256_finish (&da->ctx.sha256_ctx, da->digest.sha256); |
406 | else | 404 | else |
407 | mhd_assert (0); /* May not happen */ | 405 | mhd_assert (0); /* May not happen */ |
@@ -2331,7 +2329,17 @@ MHD_digest_auth_check3 (struct MHD_Connection *connection, | |||
2331 | 2329 | ||
2332 | mhd_assert (NULL != password); | 2330 | mhd_assert (NULL != password); |
2333 | 2331 | ||
2334 | if (! digest_setup (&da, algo)) | 2332 | if ((MHD_DIGEST_ALG_MD5 == algo) || (MHD_DIGEST_ALG_AUTO == algo)) |
2333 | { | ||
2334 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_MD5)) | ||
2335 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2336 | } | ||
2337 | else if (MHD_DIGEST_ALG_SHA256 == algo) | ||
2338 | { | ||
2339 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_SHA256)) | ||
2340 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2341 | } | ||
2342 | else | ||
2335 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ | 2343 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ |
2336 | 2344 | ||
2337 | return digest_auth_check_all (connection, | 2345 | return digest_auth_check_all (connection, |
@@ -2373,7 +2381,17 @@ MHD_digest_auth_check_digest3 (struct MHD_Connection *connection, | |||
2373 | struct DigestAlgorithm da; | 2381 | struct DigestAlgorithm da; |
2374 | 2382 | ||
2375 | mhd_assert (NULL != digest); | 2383 | mhd_assert (NULL != digest); |
2376 | if (! digest_setup (&da, algo)) | 2384 | if ((MHD_DIGEST_ALG_MD5 == algo) || (MHD_DIGEST_ALG_AUTO == algo)) |
2385 | { | ||
2386 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_MD5)) | ||
2387 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2388 | } | ||
2389 | else if (MHD_DIGEST_ALG_SHA256 == algo) | ||
2390 | { | ||
2391 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_SHA256)) | ||
2392 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2393 | } | ||
2394 | else | ||
2377 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ | 2395 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ |
2378 | 2396 | ||
2379 | if (digest_get_size (&da) != digest_size) | 2397 | if (digest_get_size (&da) != digest_size) |
@@ -2539,7 +2557,17 @@ MHD_queue_auth_fail_response2 (struct MHD_Connection *connection, | |||
2539 | 2557 | ||
2540 | struct DigestAlgorithm da; | 2558 | struct DigestAlgorithm da; |
2541 | 2559 | ||
2542 | if (! digest_setup (&da, algo)) | 2560 | if ((MHD_DIGEST_ALG_MD5 == algo) || (MHD_DIGEST_ALG_AUTO == algo)) |
2561 | { | ||
2562 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_MD5)) | ||
2563 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2564 | } | ||
2565 | else if (MHD_DIGEST_ALG_SHA256 == algo) | ||
2566 | { | ||
2567 | if (! digest_setup (&da, MHD_DIGEST_BASE_ALGO_SHA256)) | ||
2568 | MHD_PANIC (_ ("Error initialising hash algorithm.\n")); | ||
2569 | } | ||
2570 | else | ||
2543 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ | 2571 | MHD_PANIC (_ ("Wrong algo value.\n")); /* API violation! */ |
2544 | 2572 | ||
2545 | if (NULL == response) | 2573 | if (NULL == response) |