diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-07-20 13:54:51 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-07-21 15:07:06 +0300 |
commit | e1e5a395681d56289d2de5616b112a8e01ed9052 (patch) | |
tree | d69a75b0636e3fa1520a29f18c0aa1d5694cc0ac /src/microhttpd/digestauth.c | |
parent | c63e7544d8a17bbae4d1279572729e83a382ee22 (diff) | |
download | libmicrohttpd-e1e5a395681d56289d2de5616b112a8e01ed9052.tar.gz libmicrohttpd-e1e5a395681d56289d2de5616b112a8e01ed9052.zip |
digestauth: removed usage of variable-length arrays
This should improve efficiency, size and security:
* Code works just fine with fixed size arrays.
* The resulting binary size is smaller when fixed size arrays are used.
* GCC stack protector cannot be used with functions with variable-length
arrays.
Diffstat (limited to 'src/microhttpd/digestauth.c')
-rw-r--r-- | src/microhttpd/digestauth.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index 8705b922..fac12ec0 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -1239,7 +1239,7 @@ MHD_digest_auth_get_username (struct MHD_Connection *connection) | |||
1239 | * @param realm_len the length of the @a realm. | 1239 | * @param realm_len the length of the @a realm. |
1240 | * @param da digest algorithm to use | 1240 | * @param da digest algorithm to use |
1241 | * @param[out] nonce A pointer to a character array for the nonce to put in, | 1241 | * @param[out] nonce A pointer to a character array for the nonce to put in, |
1242 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes | 1242 | * must provide NONCE_STD_LEN(digest_get_size(da))+1 bytes |
1243 | */ | 1243 | */ |
1244 | static void | 1244 | static void |
1245 | calculate_nonce (uint64_t nonce_time, | 1245 | calculate_nonce (uint64_t nonce_time, |
@@ -1301,7 +1301,7 @@ calculate_nonce (uint64_t nonce_time, | |||
1301 | if (1) | 1301 | if (1) |
1302 | { | 1302 | { |
1303 | const unsigned int digest_size = digest_get_size (da); | 1303 | const unsigned int digest_size = digest_get_size (da); |
1304 | uint8_t hash[VLA_ARRAY_LEN_DIGEST (digest_size)]; | 1304 | uint8_t hash[MAX_DIGEST]; |
1305 | digest_calc_hash (da, hash); | 1305 | digest_calc_hash (da, hash); |
1306 | MHD_bin_to_hex (hash, | 1306 | MHD_bin_to_hex (hash, |
1307 | digest_size, | 1307 | digest_size, |
@@ -1381,7 +1381,7 @@ is_slot_available (const struct MHD_NonceNc *const nn, | |||
1381 | * @param realm_len the length of the @a realm | 1381 | * @param realm_len the length of the @a realm |
1382 | * @param da the digest algorithm to use | 1382 | * @param da the digest algorithm to use |
1383 | * @param[out] nonce the pointer to a character array for the nonce to put in, | 1383 | * @param[out] nonce the pointer to a character array for the nonce to put in, |
1384 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes | 1384 | * must provide NONCE_STD_LEN(digest_get_size(da))+1 bytes |
1385 | * @return true if the new nonce has been added to the nonce-nc map array, | 1385 | * @return true if the new nonce has been added to the nonce-nc map array, |
1386 | * false otherwise. | 1386 | * false otherwise. |
1387 | */ | 1387 | */ |
@@ -1450,7 +1450,7 @@ calculate_add_nonce (struct MHD_Connection *const connection, | |||
1450 | * @param realm A string of characters that describes the realm of auth. | 1450 | * @param realm A string of characters that describes the realm of auth. |
1451 | * @param da digest algorithm to use | 1451 | * @param da digest algorithm to use |
1452 | * @param[out] nonce A pointer to a character array for the nonce to put in, | 1452 | * @param[out] nonce A pointer to a character array for the nonce to put in, |
1453 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes | 1453 | * must provide NONCE_STD_LEN(digest_get_size(da))+1 bytes |
1454 | */ | 1454 | */ |
1455 | static bool | 1455 | static bool |
1456 | calculate_add_nonce_with_retry (struct MHD_Connection *const connection, | 1456 | calculate_add_nonce_with_retry (struct MHD_Connection *const connection, |
@@ -1472,7 +1472,7 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, | |||
1472 | * used by the client and this nonce is still fresh enough. | 1472 | * used by the client and this nonce is still fresh enough. |
1473 | */ | 1473 | */ |
1474 | const size_t digest_size = digest_get_size (da); | 1474 | const size_t digest_size = digest_get_size (da); |
1475 | char nonce2[NONCE_STD_LEN (VLA_ARRAY_LEN_DIGEST (digest_size)) + 1]; | 1475 | char nonce2[NONCE_STD_LEN (MAX_DIGEST) + 1]; |
1476 | uint64_t timestamp2; | 1476 | uint64_t timestamp2; |
1477 | if (0 == MHD_get_master (connection->daemon)->nonce_nc_size) | 1477 | if (0 == MHD_get_master (connection->daemon)->nonce_nc_size) |
1478 | return false; /* No need to re-try */ | 1478 | return false; /* No need to re-try */ |
@@ -1888,7 +1888,7 @@ is_param_equal_caseless (const struct MHD_RqDAuthParam *param, | |||
1888 | * @param password The password used in the authentication | 1888 | * @param password The password used in the authentication |
1889 | * @param digest An optional binary hash | 1889 | * @param digest An optional binary hash |
1890 | * of the precalculated hash value "username:realm:password" | 1890 | * of the precalculated hash value "username:realm:password" |
1891 | * (must contain "da->digest_size" bytes or be NULL) | 1891 | * (must contain "digest_get_size(da)" bytes or be NULL) |
1892 | * @param nonce_timeout The amount of time for a nonce to be | 1892 | * @param nonce_timeout The amount of time for a nonce to be |
1893 | * invalid in seconds | 1893 | * invalid in seconds |
1894 | * @param[out] pbuf the pointer to pointer to internally malloc'ed buffer, | 1894 | * @param[out] pbuf the pointer to pointer to internally malloc'ed buffer, |
@@ -1909,8 +1909,8 @@ digest_auth_check_all_inner (struct MHD_Connection *connection, | |||
1909 | { | 1909 | { |
1910 | struct MHD_Daemon *daemon = MHD_get_master (connection->daemon); | 1910 | struct MHD_Daemon *daemon = MHD_get_master (connection->daemon); |
1911 | const unsigned int digest_size = digest_get_size (da); | 1911 | const unsigned int digest_size = digest_get_size (da); |
1912 | uint8_t hash1_bin[VLA_ARRAY_LEN_DIGEST (digest_size)]; | 1912 | uint8_t hash1_bin[MAX_DIGEST]; |
1913 | uint8_t hash2_bin[VLA_ARRAY_LEN_DIGEST (digest_size)]; | 1913 | uint8_t hash2_bin[MAX_DIGEST]; |
1914 | #if 0 | 1914 | #if 0 |
1915 | const char *hentity = NULL; /* "auth-int" is not supported */ | 1915 | const char *hentity = NULL; /* "auth-int" is not supported */ |
1916 | #endif | 1916 | #endif |
@@ -2239,7 +2239,7 @@ digest_auth_check_all_inner (struct MHD_Connection *connection, | |||
2239 | * @param password The password used in the authentication | 2239 | * @param password The password used in the authentication |
2240 | * @param digest An optional binary hash | 2240 | * @param digest An optional binary hash |
2241 | * of the precalculated hash value "username:realm:password" | 2241 | * of the precalculated hash value "username:realm:password" |
2242 | * (must contain "da->digest_size" bytes or be NULL) | 2242 | * (must contain "digest_get_size(da)" bytes or be NULL) |
2243 | * @param nonce_timeout The amount of time for a nonce to be | 2243 | * @param nonce_timeout The amount of time for a nonce to be |
2244 | * invalid in seconds | 2244 | * invalid in seconds |
2245 | * @return #MHD_DAUTH_OK if authenticated, | 2245 | * @return #MHD_DAUTH_OK if authenticated, |
@@ -2583,10 +2583,9 @@ MHD_queue_auth_fail_response2 (struct MHD_Connection *connection, | |||
2583 | 2583 | ||
2584 | if (1) | 2584 | if (1) |
2585 | { | 2585 | { |
2586 | char nonce[NONCE_STD_LEN (VLA_ARRAY_LEN_DIGEST (digest_get_size (&da))) | 2586 | char nonce[NONCE_STD_LEN (MAX_DIGEST) + 1]; |
2587 | + 1]; | ||
2588 | 2587 | ||
2589 | VLA_CHECK_LEN_DIGEST (digest_get_size (&da)); | 2588 | /* VLA_CHECK_LEN_DIGEST (digest_get_size (&da)); */ |
2590 | if (! calculate_add_nonce_with_retry (connection, realm, &da, nonce)) | 2589 | if (! calculate_add_nonce_with_retry (connection, realm, &da, nonce)) |
2591 | { | 2590 | { |
2592 | #ifdef HAVE_MESSAGES | 2591 | #ifdef HAVE_MESSAGES |