3 * Implements the EncryptedPassword class for the MediaWiki software.
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.
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.
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
23 declare( strict_types
= 1 );
25 namespace MediaWiki\Password
;
28 * Helper class for passwords that use another password hash underneath it
29 * and encrypts that hash with a configured secret.
33 class EncryptedPassword
extends ParameterizedPassword
{
34 protected function getDelimiter(): string {
38 protected function getDefaultParams(): array {
40 'cipher' => $this->config
['cipher'],
41 'secret' => (string)( count( $this->config
['secrets'] ) - 1 )
45 public function crypt( string $password ): void
{
46 $secret = $this->config
['secrets'][(int)$this->params
['secret']];
49 while ( openssl_error_string() !== false );
52 $decrypted = openssl_decrypt(
53 $this->hash
, $this->params
['cipher'],
54 $secret, 0, base64_decode( $this->args
[0] ) );
55 if ( $decrypted === false ) {
56 throw new PasswordError( 'Error decrypting password: ' . openssl_error_string() );
58 $underlyingPassword = $this->factory
->newFromCiphertext( $decrypted );
60 $underlyingPassword = $this->factory
->newFromType( $this->config
['underlying'] );
63 $underlyingPassword->crypt( $password );
64 if ( count( $this->args
) ) {
65 $iv = base64_decode( $this->args
[0] );
67 $iv = random_bytes( openssl_cipher_iv_length( $this->params
['cipher'] ) );
70 $this->hash
= openssl_encrypt(
71 $underlyingPassword->toString(), $this->params
['cipher'], $secret, 0, $iv );
72 if ( $this->hash
=== false ) {
73 throw new PasswordError( 'Error encrypting password: ' . openssl_error_string() );
75 $this->args
= [ base64_encode( $iv ) ];
79 * Updates the underlying hash by encrypting it with the newest secret.
81 * @throws PasswordError If the configuration is not valid
82 * @return bool True if the password was updated
84 public function update(): bool {
85 if ( count( $this->args
) != 1 ||
$this->params
== $this->getDefaultParams() ) {
86 // Hash does not need updating
91 while ( openssl_error_string() !== false );
93 // Decrypt the underlying hash
94 $underlyingHash = openssl_decrypt(
96 $this->params
['cipher'],
97 $this->config
['secrets'][(int)$this->params
['secret']],
99 base64_decode( $this->args
[0] )
101 if ( $underlyingHash === false ) {
102 throw new PasswordError( 'Error decrypting password: ' . openssl_error_string() );
106 $this->params
= $this->getDefaultParams();
108 // Check the key size with the new params
109 $iv = random_bytes( openssl_cipher_iv_length( $this->params
['cipher'] ) );
110 $this->hash
= openssl_encrypt(
112 $this->params
['cipher'],
113 $this->config
['secrets'][(int)$this->params
['secret']],
117 if ( $this->hash
=== false ) {
118 throw new PasswordError( 'Error encrypting password: ' . openssl_error_string() );
121 $this->args
= [ base64_encode( $iv ) ];
129 public function verify( string $password ): bool {
130 // Clear error string
131 while ( openssl_error_string() !== false );
133 // Decrypt the underlying hash
134 $underlyingHash = openssl_decrypt(
136 $this->params
['cipher'],
137 $this->config
['secrets'][(int)$this->params
['secret']],
139 base64_decode( $this->args
[0] )
141 if ( $underlyingHash === false ) {
142 throw new PasswordError( 'Error decrypting password: ' . openssl_error_string() );
145 $storedPassword = $this->factory
->newFromCiphertext( $underlyingHash );
146 return $storedPassword->verify( $password );
150 /** @deprecated since 1.43 use MediaWiki\\Password\\EncryptedPassword */
151 class_alias( EncryptedPassword
::class, 'EncryptedPassword' );