]> granicus.if.org Git - php/commitdiff
Improve error messages of ext/hash
authorMáté Kocsis <kocsismate@woohoolabs.com>
Wed, 18 Mar 2020 19:58:45 +0000 (20:58 +0100)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Tue, 24 Mar 2020 09:23:11 +0000 (10:23 +0100)
Closes GH-5275

ext/hash/hash.c
ext/hash/tests/hash_equals.phpt
ext/hash/tests/hash_hkdf_edges.phpt
ext/hash/tests/hash_hkdf_error.phpt
ext/hash/tests/hash_hmac_error.phpt
ext/hash/tests/hash_hmac_file_error.phpt
ext/hash/tests/hash_init_error.phpt
ext/hash/tests/hash_pbkdf2_error.phpt

index 7ff7a312b960da1454dd708d7a4663c6a03bf6fc..7f58060158055ec95f53f3294253112de01174ff 100644 (file)
@@ -137,7 +137,7 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
        }
        if (isfilename) {
                if (CHECK_NULL_PATH(data, data_len)) {
-                       zend_argument_value_error(1, "must be a valid path");
+                       zend_argument_type_error(1, "must be a valid path");
                        RETURN_THROWS();
                }
                stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
@@ -254,18 +254,14 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
        }
 
        ops = php_hash_fetch_ops(algo);
-       if (!ops) {
-               zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
-               RETURN_THROWS();
-       }
-       else if (!ops->is_crypto) {
-               zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+       if (!ops || !ops->is_crypto) {
+               zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
                RETURN_THROWS();
        }
 
        if (isfilename) {
                if (CHECK_NULL_PATH(data, data_len)) {
-                       zend_throw_error(NULL, "Invalid path");
+                       zend_argument_type_error(2, "must be a valid path");
                        RETURN_THROWS();
                }
                stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
@@ -361,18 +357,18 @@ PHP_FUNCTION(hash_init)
 
        ops = php_hash_fetch_ops(algo);
        if (!ops) {
-               zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
+               zend_argument_value_error(1, "must be a valid hashing algorithm");
                RETURN_THROWS();
        }
 
        if (options & PHP_HASH_HMAC) {
                if (!ops->is_crypto) {
-                       zend_throw_error(NULL, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+                       zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
                        RETURN_THROWS();
                }
                if (!key || (ZSTR_LEN(key) == 0)) {
                        /* Note: a zero length key is no key at all */
-                       zend_throw_error(NULL, "HMAC requested without a key");
+                       zend_argument_value_error(3, "cannot be empty when HMAC is requested");
                        RETURN_THROWS();
                }
        }
@@ -649,28 +645,23 @@ PHP_FUNCTION(hash_hkdf)
        }
 
        ops = php_hash_fetch_ops(algo);
-       if (!ops) {
-               zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
-               RETURN_THROWS();
-       }
-
-       if (!ops->is_crypto) {
-               zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+       if (!ops || !ops->is_crypto) {
+               zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
                RETURN_THROWS();
        }
 
        if (ZSTR_LEN(ikm) == 0) {
-               zend_throw_error(NULL, "Input keying material cannot be empty");
+               zend_argument_value_error(2, "cannot be empty");
                RETURN_THROWS();
        }
 
        if (length < 0) {
-               zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
+               zend_argument_value_error(3, "must be greater than or equal to 0");
                RETURN_THROWS();
        } else if (length == 0) {
                length = ops->digest_size;
        } else if (length > (zend_long) (ops->digest_size * 255)) {
-               zend_throw_error(NULL, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
+               zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
                RETURN_THROWS();
        }
 
@@ -749,27 +740,23 @@ PHP_FUNCTION(hash_pbkdf2)
        }
 
        ops = php_hash_fetch_ops(algo);
-       if (!ops) {
-               zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
+       if (!ops || !ops->is_crypto) {
+               zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
                RETURN_THROWS();
        }
-       else if (!ops->is_crypto) {
-               zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+
+       if (salt_len > INT_MAX - 4) {
+               zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
                RETURN_THROWS();
        }
 
        if (iterations <= 0) {
-               zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
+               zend_argument_value_error(4, "must be greater than 0");
                RETURN_THROWS();
        }
 
        if (length < 0) {
-               zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
-               RETURN_THROWS();
-       }
-
-       if (salt_len > INT_MAX - 4) {
-               zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
+               zend_argument_value_error(5, "must be greater than or equal to 0");
                RETURN_THROWS();
        }
 
@@ -875,12 +862,12 @@ PHP_FUNCTION(hash_equals)
 
        /* We only allow comparing string to prevent unexpected results. */
        if (Z_TYPE_P(known_zval) != IS_STRING) {
-               zend_type_error("Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
+               zend_argument_type_error(1, "must be of type string, %s given", zend_zval_type_name(known_zval));
                RETURN_THROWS();
        }
 
        if (Z_TYPE_P(user_zval) != IS_STRING) {
-               zend_type_error("Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
+               zend_argument_type_error(2, "must be of type string, %s given", zend_zval_type_name(user_zval));
                RETURN_THROWS();
        }
 
index 1716d3be6629ba28313c21e62accbde490004be5..69e23eea0fa3894f451f485cdd13d76e824f5e56 100644 (file)
@@ -39,9 +39,9 @@ bool(false)
 bool(false)
 bool(false)
 bool(true)
-[TypeError] Expected known_string to be a string, int given
-[TypeError] Expected user_string to be a string, int given
-[TypeError] Expected known_string to be a string, int given
-[TypeError] Expected known_string to be a string, null given
-[TypeError] Expected known_string to be a string, null given
-[TypeError] Expected known_string to be a string, null given
+[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
+[TypeError] hash_equals(): Argument #2 ($user_string) must be of type string, int given
+[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
+[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
+[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
+[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
index 4a69c7c13419bf91266dddf33d9d4043ff0fb423..cee86ae82ead4637a74de469a4b0f1772d720ffb 100644 (file)
@@ -31,4 +31,4 @@ Length < digestSize: 98b16391063ece
 Length % digestSize != 0: 98b16391063ecee006a3ca8ee5776b1e5f
 Algo name case-sensitivity: true
 Non-crypto algo name case-sensitivity:
-[Error] Non-cryptographic hashing algorithm: jOaAt
+[Error] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
index 411d0637ab0d1a17ab55e44268630ba2e44a666b..7ed631251f40709b7974bfe88bf88f441455d78e 100644 (file)
@@ -53,19 +53,19 @@ trycatch_dump(
 *** Testing hash_hkdf(): error conditions ***
 
 -- Testing hash_hkdf() function with invalid hash algorithm --
-[Error] Unknown hashing algorithm: foo
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_hkdf() function with non-cryptographic hash algorithm --
-[Error] Non-cryptographic hashing algorithm: adler32
-[Error] Non-cryptographic hashing algorithm: crc32
-[Error] Non-cryptographic hashing algorithm: crc32b
-[Error] Non-cryptographic hashing algorithm: fnv132
-[Error] Non-cryptographic hashing algorithm: fnv1a32
-[Error] Non-cryptographic hashing algorithm: fnv164
-[Error] Non-cryptographic hashing algorithm: fnv1a64
-[Error] Non-cryptographic hashing algorithm: joaat
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
+[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_hkdf() function with invalid parameters --
-[Error] Input keying material cannot be empty
-[Error] Length must be greater than or equal to 0: -1
-[Error] Length must be less than or equal to 5100: 5101
+[ValueError] hash_hkdf(): Argument #2 ($ikm) cannot be empty
+[ValueError] hash_hkdf(): Argument #3 ($length) must be greater than or equal to 0
+[ValueError] hash_hkdf(): Argument #3 ($length) must be less than or equal to 5100
index 3b14243fb292d4d54c0bb99ac7a12c6f34397359..9d303467ef12e1d09c752aad243bb9ef77171082 100644 (file)
@@ -33,7 +33,7 @@ catch (\Error $e) {
 *** Testing hash_hmac() : error conditions ***
 
 -- Testing hash_hmac() function with invalid hash algorithm --
-Unknown hashing algorithm: foo
+hash_hmac(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_hmac() function with non-cryptographic hash algorithm --
-Non-cryptographic hashing algorithm: crc32
+hash_hmac(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
index f23d33569922f075d3d46e6a3cc53a449a2b8f76..8208c6ebd4fe1bef52522c8429f8ab17b2a5c401 100644 (file)
@@ -34,7 +34,7 @@ echo "\n-- Testing hash_hmac_file() function with bad path --\n";
 try {
     var_dump(hash_hmac_file('md5', $file.chr(0).$file, $key, TRUE));
 }
-catch (\Error $e) {
+catch (TypeError $e) {
     echo $e->getMessage() . "\n";
 }
 
@@ -43,10 +43,10 @@ catch (\Error $e) {
 *** Testing hash() : error conditions ***
 
 -- Testing hash_hmac_file() function with invalid hash algorithm --
-Unknown hashing algorithm: foo
+hash_hmac_file(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_hmac_file() function with non-cryptographic hash algorithm --
-Non-cryptographic hashing algorithm: crc32
+hash_hmac_file(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_hmac_file() function with bad path --
-Invalid path
+hash_hmac_file(): Argument #2 ($data) must be a valid path
index f9d0362dd2b8497f233767a7d1131a333ffd23f5..fdea4174d6fc9168a73221604c76ae9a9d06e134 100644 (file)
@@ -41,11 +41,11 @@ catch (\Error $e) {
 *** Testing hash_init(): error conditions ***
 
 -- Testing hash_init() function with unknown algorithms --
-Unknown hashing algorithm: dummy
+hash_init(): Argument #1 ($algo) must be a valid hashing algorithm
 
 -- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --
-HMAC requested with a non-cryptographic hashing algorithm: crc32
+hash_init(): Argument #1 ($algo) must be a cryptographic hashing algorithm if HMAC is requested
 
 -- Testing hash_init() function with HASH_HMAC and no key --
-HMAC requested without a key
-HMAC requested without a key
+hash_init(): Argument #3 ($key) cannot be empty when HMAC is requested
+hash_init(): Argument #3 ($key) cannot be empty when HMAC is requested
index 795dea1ce316929f1776d1d1c88aba5dfc7e32e0..7631eeef906768a6ac9156343aa0d2c097e89608 100644 (file)
@@ -57,14 +57,14 @@ catch (\Error $e) {
 *** Testing hash_pbkdf2() : error conditions ***
 
 -- Testing hash_pbkdf2() function with invalid hash algorithm --
-Unknown hashing algorithm: foo
+hash_pbkdf2(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --
-Non-cryptographic hashing algorithm: crc32
+hash_pbkdf2(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
 
 -- Testing hash_pbkdf2() function with invalid iterations --
-Iterations must be a positive integer: 0
-Iterations must be a positive integer: -1
+hash_pbkdf2(): Argument #4 ($iterations) must be greater than 0
+hash_pbkdf2(): Argument #4 ($iterations) must be greater than 0
 
 -- Testing hash_pbkdf2() function with invalid length --
-Length must be greater than or equal to 0: -1
+hash_pbkdf2(): Argument #5 ($length) must be greater than or equal to 0