Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pbkdf2PasswordUsingOpenSSL
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
6.05
0.00% covered (danger)
0.00%
0 / 1
 isSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDigestAlgo
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 pbkdf2
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
1<?php
2/**
3 * Implements the Pbkdf2PasswordUsingOpenSSL class for the MediaWiki software.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23declare( strict_types = 1 );
24
25/**
26 * A PBKDF2-hashed password, using OpenSSL
27 *
28 * @since 1.40
29 */
30class Pbkdf2PasswordUsingOpenSSL extends AbstractPbkdf2Password {
31    /**
32     * @var array<string, string>
33     */
34    private static $digestAlgos;
35
36    /**
37     * List of hash algorithms we support and OpenSSL's names for them.
38     *
39     * We include only the algorithms that make sense to support, rather than
40     * all potentially available algorithms. In particular, we do not include:
41     *
42     * - Broken algorithms, such as "md5" and "sha1"
43     * - Algorithms no longer available by default, such as "whirlpool"
44     * - Algorithms that perform especially poorly on server CPUs relative
45     *   to other available hardware (as of 2022, this includes "sha3-512";
46     *   see <https://keccak.team/2017/is_sha3_slow.html>)
47     * - Variants for which there is no reason for use, such as "sha384"
48     *   (a truncated "sha512" that starts with a different initial state)
49     *
50     * The array keys should match the algorithm names known to hash_pbkdf2().
51     */
52    private const DIGEST_ALGOS = [
53        'sha256' => 'sha256',
54        'sha512' => 'sha512',
55    ];
56
57    protected function isSupported(): bool {
58        return self::canUseOpenSSL();
59    }
60
61    protected function getDigestAlgo( string $algo ): ?string {
62        if ( !isset( self::$digestAlgos ) ) {
63            self::$digestAlgos = array_intersect( self::DIGEST_ALGOS, openssl_get_md_methods() );
64        }
65        return self::$digestAlgos[$algo] ?? null;
66    }
67
68    protected function pbkdf2(
69        string $digestAlgo,
70        string $password,
71        string $salt,
72        int $rounds,
73        int $length
74    ): string {
75        // Clear error string
76        while ( openssl_error_string() !== false );
77        $hash = openssl_pbkdf2( $password, $salt, $length, $rounds, $digestAlgo );
78        if ( !is_string( $hash ) ) {
79            throw new PasswordError( 'Error when hashing password: ' . openssl_error_string() );
80        }
81        return $hash;
82    }
83}