Skip to content

Commit b786e1b

Browse files
SammyKnikic
authored andcommitted
Improve sodium "invalid parameters" error messages
1 parent fd07302 commit b786e1b

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

ext/sodium/libsodium.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ PHP_FUNCTION(sodium_crypto_stream)
16451645
return;
16461646
}
16471647
if (ciphertext_len <= 0 || ciphertext_len >= SIZE_MAX) {
1648-
zend_throw_exception(sodium_exception_ce, "invalid length", 0);
1648+
zend_throw_exception(sodium_exception_ce, "ciphertext length must be greater than 0", 0);
16491649
return;
16501650
}
16511651
if (nonce_len != crypto_stream_NONCEBYTES) {
@@ -1727,9 +1727,16 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256)
17271727
sodium_remove_param_values_from_backtrace(EG(exception));
17281728
return;
17291729
}
1730-
if (hash_len <= 0 || hash_len >= SIZE_MAX || hash_len > 0x1fffffffe0ULL ||
1731-
opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) {
1732-
zend_throw_exception(sodium_exception_ce, "invalid parameters", 0);
1730+
if (hash_len <= 0 || hash_len >= SIZE_MAX || hash_len > 0x1fffffffe0ULL) {
1731+
zend_throw_exception(sodium_exception_ce, "hash length must be greater than 0", 0);
1732+
return;
1733+
}
1734+
if (opslimit <= 0) {
1735+
zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
1736+
return;
1737+
}
1738+
if (memlimit <= 0 || memlimit > SIZE_MAX) {
1739+
zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
17331740
return;
17341741
}
17351742
if (passwd_len <= 0) {
@@ -1777,8 +1784,12 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str)
17771784
sodium_remove_param_values_from_backtrace(EG(exception));
17781785
return;
17791786
}
1780-
if (opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) {
1781-
zend_throw_exception(sodium_exception_ce, "invalid parameters", 0);
1787+
if (opslimit <= 0) {
1788+
zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
1789+
return;
1790+
}
1791+
if (memlimit <= 0 || memlimit > SIZE_MAX) {
1792+
zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
17821793
return;
17831794
}
17841795
if (passwd_len <= 0) {
@@ -1856,10 +1867,20 @@ PHP_FUNCTION(sodium_crypto_pwhash)
18561867
sodium_remove_param_values_from_backtrace(EG(exception));
18571868
return;
18581869
}
1859-
if (hash_len <= 0 || hash_len >= 0xffffffff ||
1860-
passwd_len >= 0xffffffff ||
1861-
opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) {
1862-
zend_throw_exception(sodium_exception_ce, "invalid parameters", 0);
1870+
if (hash_len <= 0 || hash_len >= 0xffffffff) {
1871+
zend_throw_exception(sodium_exception_ce, "hash length must be greater than 0", 0);
1872+
return;
1873+
}
1874+
if (passwd_len >= 0xffffffff) {
1875+
zend_throw_exception(sodium_exception_ce, "unsupported password length", 0);
1876+
return;
1877+
}
1878+
if (opslimit <= 0) {
1879+
zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
1880+
return;
1881+
}
1882+
if (memlimit <= 0 || memlimit > SIZE_MAX) {
1883+
zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
18631884
return;
18641885
}
18651886
if (alg != crypto_pwhash_ALG_ARGON2I13
@@ -1914,9 +1935,16 @@ PHP_FUNCTION(sodium_crypto_pwhash_str)
19141935
sodium_remove_param_values_from_backtrace(EG(exception));
19151936
return;
19161937
}
1917-
if (opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX ||
1918-
passwd_len >= 0xffffffff) {
1919-
zend_throw_exception(sodium_exception_ce, "invalid parameters", 0);
1938+
if (opslimit <= 0) {
1939+
zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
1940+
return;
1941+
}
1942+
if (memlimit <= 0 || memlimit > SIZE_MAX) {
1943+
zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
1944+
return;
1945+
}
1946+
if (passwd_len >= 0xffffffff) {
1947+
zend_throw_exception(sodium_exception_ce, "unsupported password length", 0);
19201948
return;
19211949
}
19221950
if (passwd_len <= 0) {

0 commit comments

Comments
 (0)