| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
declare( strict_types = 1 ); |
| 24 |
|
| 25 |
namespace MediaWiki\Password; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class EncryptedPassword extends ParameterizedPassword { |
| 34 |
protected function getDelimiter(): string { |
| 35 |
return ':'; |
| 36 |
} |
| 37 |
|
| 38 |
protected function getDefaultParams(): array { |
| 39 |
return [ |
| 40 |
'cipher' => $this->config['cipher'], |
| 41 |
'secret' => (string)( count( $this->config['secrets'] ) - 1 ) |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
public function crypt( string $password ): void { |
| 46 |
$secret = $this->config['secrets'][(int)$this->params['secret']]; |
| 47 |
|
| 48 |
|
| 49 |
while ( openssl_error_string() !== false ); |
| 50 |
|
| 51 |
if ( $this->hash ) { |
| 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() ); |
| 57 |
} |
| 58 |
$underlyingPassword = $this->factory->newFromCiphertext( $decrypted ); |
| 59 |
} else { |
| 60 |
$underlyingPassword = $this->factory->newFromType( $this->config['underlying'] ); |
| 61 |
} |
| 62 |
|
| 63 |
$underlyingPassword->crypt( $password ); |
| 64 |
if ( count( $this->args ) ) { |
| 65 |
$iv = base64_decode( $this->args[0] ); |
| 66 |
} else { |
| 67 |
$iv = random_bytes( openssl_cipher_iv_length( $this->params['cipher'] ) ); |
| 68 |
} |
| 69 |
|
| 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() ); |
| 74 |
} |
| 75 |
$this->args = [ base64_encode( $iv ) ]; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function update(): bool { |
| 85 |
if ( count( $this->args ) != 1 || $this->params == $this->getDefaultParams() ) { |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
while ( openssl_error_string() !== false ); |
| 92 |
|
| 93 |
|
| 94 |
$underlyingHash = openssl_decrypt( |
| 95 |
$this->hash, |
| 96 |
$this->params['cipher'], |
| 97 |
$this->config['secrets'][(int)$this->params['secret']], |
| 98 |
0, |
| 99 |
base64_decode( $this->args[0] ) |
| 100 |
); |
| 101 |
if ( $underlyingHash === false ) { |
| 102 |
throw new PasswordError( 'Error decrypting password: ' . openssl_error_string() ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
$this->params = $this->getDefaultParams(); |
| 107 |
|
| 108 |
|
| 109 |
$iv = random_bytes( openssl_cipher_iv_length( $this->params['cipher'] ) ); |
| 110 |
$this->hash = openssl_encrypt( |
| 111 |
$underlyingHash, |
| 112 |
$this->params['cipher'], |
| 113 |
$this->config['secrets'][(int)$this->params['secret']], |
| 114 |
0, |
| 115 |
$iv |
| 116 |
); |
| 117 |
if ( $this->hash === false ) { |
| 118 |
throw new PasswordError( 'Error encrypting password: ' . openssl_error_string() ); |
| 119 |
} |
| 120 |
|
| 121 |
$this->args = [ base64_encode( $iv ) ]; |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function verify( string $password ): bool { |
| 130 |
|
| 131 |
while ( openssl_error_string() !== false ); |
| 132 |
|
| 133 |
|
| 134 |
$underlyingHash = openssl_decrypt( |
| 135 |
$this->hash, |
| 136 |
$this->params['cipher'], |
| 137 |
$this->config['secrets'][(int)$this->params['secret']], |
| 138 |
0, |
| 139 |
base64_decode( $this->args[0] ) |
| 140 |
); |
| 141 |
if ( $underlyingHash === false ) { |
| 142 |
throw new PasswordError( 'Error decrypting password: ' . openssl_error_string() ); |
| 143 |
} |
| 144 |
|
| 145 |
$storedPassword = $this->factory->newFromCiphertext( $underlyingHash ); |
| 146 |
return $storedPassword->verify( $password ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
class_alias( EncryptedPassword::class, 'EncryptedPassword' ); |