diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-06 17:55:00 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-06 18:29:44 +0300 |
commit | d9ebd051d2877ba108e369a3c040f29c849ec0e0 (patch) | |
tree | 8e7240329b91374f219470ed624537a1d58f28fe /src/microhttpd/digestauth.c | |
parent | eb48c2586572dd4bae8db4b2d395ef27d72af7a0 (diff) | |
download | libmicrohttpd-d9ebd051d2877ba108e369a3c040f29c849ec0e0.tar.gz libmicrohttpd-d9ebd051d2877ba108e369a3c040f29c849ec0e0.zip |
digestauth: avoid repetitive calculations of some strings' length
Diffstat (limited to 'src/microhttpd/digestauth.c')
-rw-r--r-- | src/microhttpd/digestauth.c | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index efc53130..c3717d47 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -485,7 +485,9 @@ digest_calc_ha1_from_digest (const char *alg, | |||
485 | * @param alg The hash algorithm used, can be "MD5" or "MD5-sess" | 485 | * @param alg The hash algorithm used, can be "MD5" or "MD5-sess" |
486 | * or "SHA-256" or "SHA-256-sess" | 486 | * or "SHA-256" or "SHA-256-sess" |
487 | * @param username A `char *' pointer to the username value | 487 | * @param username A `char *' pointer to the username value |
488 | * @param username the length of the @a username | ||
488 | * @param realm A `char *' pointer to the realm value | 489 | * @param realm A `char *' pointer to the realm value |
490 | * @param realm_len the length of the @a realm | ||
489 | * @param password A `char *' pointer to the password value | 491 | * @param password A `char *' pointer to the password value |
490 | * @param nonce A `char *' pointer to the nonce value | 492 | * @param nonce A `char *' pointer to the nonce value |
491 | * @param cnonce A `char *' pointer to the cnonce value | 493 | * @param cnonce A `char *' pointer to the cnonce value |
@@ -495,7 +497,9 @@ digest_calc_ha1_from_digest (const char *alg, | |||
495 | static void | 497 | static void |
496 | digest_calc_ha1_from_user (const char *alg, | 498 | digest_calc_ha1_from_user (const char *alg, |
497 | const char *username, | 499 | const char *username, |
500 | size_t username_len, | ||
498 | const char *realm, | 501 | const char *realm, |
502 | size_t realm_len, | ||
499 | const char *password, | 503 | const char *password, |
500 | const char *nonce, | 504 | const char *nonce, |
501 | const char *cnonce, | 505 | const char *cnonce, |
@@ -504,13 +508,13 @@ digest_calc_ha1_from_user (const char *alg, | |||
504 | digest_init (da); | 508 | digest_init (da); |
505 | digest_update (da, | 509 | digest_update (da, |
506 | (const unsigned char *) username, | 510 | (const unsigned char *) username, |
507 | strlen (username)); | 511 | username_len); |
508 | digest_update (da, | 512 | digest_update (da, |
509 | (const unsigned char *) ":", | 513 | (const unsigned char *) ":", |
510 | 1); | 514 | 1); |
511 | digest_update (da, | 515 | digest_update (da, |
512 | (const unsigned char *) realm, | 516 | (const unsigned char *) realm, |
513 | strlen (realm)); | 517 | realm_len); |
514 | digest_update (da, | 518 | digest_update (da, |
515 | (const unsigned char *) ":", | 519 | (const unsigned char *) ":", |
516 | 1); | 520 | 1); |
@@ -914,6 +918,7 @@ MHD_digest_auth_get_username (struct MHD_Connection *connection) | |||
914 | * @param rnd_size The size of the random seed array @a rnd | 918 | * @param rnd_size The size of the random seed array @a rnd |
915 | * @param uri HTTP URI (in MHD, without the arguments ("?k=v") | 919 | * @param uri HTTP URI (in MHD, without the arguments ("?k=v") |
916 | * @param realm A string of characters that describes the realm of auth. | 920 | * @param realm A string of characters that describes the realm of auth. |
921 | * @param realm_len the length of the @a realm. | ||
917 | * @param da digest algorithm to use | 922 | * @param da digest algorithm to use |
918 | * @param[out] nonce A pointer to a character array for the nonce to put in, | 923 | * @param[out] nonce A pointer to a character array for the nonce to put in, |
919 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes | 924 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes |
@@ -925,6 +930,7 @@ calculate_nonce (uint64_t nonce_time, | |||
925 | size_t rnd_size, | 930 | size_t rnd_size, |
926 | const char *uri, | 931 | const char *uri, |
927 | const char *realm, | 932 | const char *realm, |
933 | size_t realm_len, | ||
928 | struct DigestAlgorithm *da, | 934 | struct DigestAlgorithm *da, |
929 | char *nonce) | 935 | char *nonce) |
930 | { | 936 | { |
@@ -969,7 +975,7 @@ calculate_nonce (uint64_t nonce_time, | |||
969 | 1); | 975 | 1); |
970 | digest_update (da, | 976 | digest_update (da, |
971 | (const unsigned char *) realm, | 977 | (const unsigned char *) realm, |
972 | strlen (realm)); | 978 | realm_len); |
973 | digest_calc_hash (da); | 979 | digest_calc_hash (da); |
974 | MHD_bin_to_hex (digest_get_bin (da), | 980 | MHD_bin_to_hex (digest_get_bin (da), |
975 | digest_get_size (da), | 981 | digest_get_size (da), |
@@ -1045,6 +1051,7 @@ is_slot_available (const struct MHD_NonceNc *const nn, | |||
1045 | * @param connection the MHD connection structure | 1051 | * @param connection the MHD connection structure |
1046 | * @param timestamp the current timestamp | 1052 | * @param timestamp the current timestamp |
1047 | * @param realm the string of characters that describes the realm of auth | 1053 | * @param realm the string of characters that describes the realm of auth |
1054 | * @param realm_len the lenght of the @a realm | ||
1048 | * @param da the digest algorithm to use | 1055 | * @param da the digest algorithm to use |
1049 | * @param[out] nonce the pointer to a character array for the nonce to put in, | 1056 | * @param[out] nonce the pointer to a character array for the nonce to put in, |
1050 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes | 1057 | * must provide NONCE_STD_LEN(da->digest_size)+1 bytes |
@@ -1055,6 +1062,7 @@ static bool | |||
1055 | calculate_add_nonce (struct MHD_Connection *const connection, | 1062 | calculate_add_nonce (struct MHD_Connection *const connection, |
1056 | uint64_t timestamp, | 1063 | uint64_t timestamp, |
1057 | const char *realm, | 1064 | const char *realm, |
1065 | size_t realm_len, | ||
1058 | struct DigestAlgorithm *da, | 1066 | struct DigestAlgorithm *da, |
1059 | char *nonce) | 1067 | char *nonce) |
1060 | { | 1068 | { |
@@ -1072,6 +1080,7 @@ calculate_add_nonce (struct MHD_Connection *const connection, | |||
1072 | daemon->digest_auth_rand_size, | 1080 | daemon->digest_auth_rand_size, |
1073 | connection->url, | 1081 | connection->url, |
1074 | realm, | 1082 | realm, |
1083 | realm_len, | ||
1075 | da, | 1084 | da, |
1076 | nonce); | 1085 | nonce); |
1077 | 1086 | ||
@@ -1118,8 +1127,10 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, | |||
1118 | char *nonce) | 1127 | char *nonce) |
1119 | { | 1128 | { |
1120 | const uint64_t timestamp1 = MHD_monotonic_msec_counter (); | 1129 | const uint64_t timestamp1 = MHD_monotonic_msec_counter (); |
1130 | const size_t realm_len = strlen (realm); | ||
1121 | 1131 | ||
1122 | if (! calculate_add_nonce (connection, timestamp1, realm, da, nonce)) | 1132 | if (! calculate_add_nonce (connection, timestamp1, realm, realm_len, da, |
1133 | nonce)) | ||
1123 | { | 1134 | { |
1124 | /* Either: | 1135 | /* Either: |
1125 | * 1. The same nonce was already generated. If it will be used then one | 1136 | * 1. The same nonce was already generated. If it will be used then one |
@@ -1156,9 +1167,10 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, | |||
1156 | /* Use up to 127 ms difference */ | 1167 | /* Use up to 127 ms difference */ |
1157 | timestamp2 -= (base4 & DAUTH_JUMPBACK_MAX); | 1168 | timestamp2 -= (base4 & DAUTH_JUMPBACK_MAX); |
1158 | if (timestamp1 == timestamp2) | 1169 | if (timestamp1 == timestamp2) |
1159 | timestamp2 -= 2; | 1170 | timestamp2 -= 2; /* Fallback value */ |
1160 | } | 1171 | } |
1161 | if (! calculate_add_nonce (connection, timestamp2, realm, da, nonce2)) | 1172 | if (! calculate_add_nonce (connection, timestamp2, realm, realm_len, da, |
1173 | nonce2)) | ||
1162 | { | 1174 | { |
1163 | /* No free slot has been found. Re-tries are expensive, just use | 1175 | /* No free slot has been found. Re-tries are expensive, just use |
1164 | * the generated nonce. As it is not stored in nonce-nc map array, | 1176 | * the generated nonce. As it is not stored in nonce-nc map array, |
@@ -1534,6 +1546,7 @@ digest_auth_check_all (struct MHD_Connection *connection, | |||
1534 | daemon->digest_auth_rand_size, | 1546 | daemon->digest_auth_rand_size, |
1535 | connection->url, | 1547 | connection->url, |
1536 | realm, | 1548 | realm, |
1549 | realm_len, | ||
1537 | da, | 1550 | da, |
1538 | noncehashexp); | 1551 | noncehashexp); |
1539 | /* | 1552 | /* |
@@ -1779,7 +1792,9 @@ digest_auth_check_all (struct MHD_Connection *connection, | |||
1779 | mhd_assert (NULL != password); /* NULL == digest => password != NULL */ | 1792 | mhd_assert (NULL != password); /* NULL == digest => password != NULL */ |
1780 | digest_calc_ha1_from_user (digest_get_algo_name (da), | 1793 | digest_calc_ha1_from_user (digest_get_algo_name (da), |
1781 | username, | 1794 | username, |
1795 | username_len, | ||
1782 | realm, | 1796 | realm, |
1797 | realm_len, | ||
1783 | password, | 1798 | password, |
1784 | noncehashexp, | 1799 | noncehashexp, |
1785 | cnonce, | 1800 | cnonce, |