ILIAS  release_7 Revision v7.30-3-g800a261c036
ilBcryptPasswordEncoder Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoder:
+ Collaboration diagram for ilBcryptPasswordEncoder:

Public Member Functions

 __construct (array $config=[])
 
 getDataDirectory ()
 
 setDataDirectory (string $data_directory)
 
 isBackwardCompatibilityEnabled ()
 
 setBackwardCompatibility (bool $backward_compatibility)
 Set the backward compatibility $2a$ instead of $2y$ for PHP 5.3.7+. More...
 
 isSecurityFlawIgnored ()
 
 setIsSecurityFlawIgnored (bool $is_security_flaw_ignored)
 
 getClientSalt ()
 
 setClientSalt (?string $client_salt)
 
 encodePassword (string $raw, string $salt)
 @inheritDoc More...
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 @inheritDoc More...
 
 getName ()
 @inheritDoc More...
 
 requiresSalt ()
 @inheritDoc More...
 
 requiresReencoding (string $encoded)
 @inheritDoc More...
 
 getClientSaltLocation ()
 
- Public Member Functions inherited from ilBcryptPhpPasswordEncoder
 __construct (array $config=[])
 
 benchmarkCost (float $time_target=0.05)
 
 getName ()
 
 isSupportedByRuntime ()
 @inheritDoc More...
 
 getCosts ()
 
 setCosts (string $costs)
 
 encodePassword (string $raw, string $salt)
 @inheritDoc More...
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 @inheritDoc More...
 
 requiresReencoding (string $encoded)
 @inheritDoc More...
 
 isSupportedByRuntime ()
 @inheritDoc More...
 
 requiresSalt ()
 @inheritDoc More...
 
 requiresReencoding (string $encoded)
 @inheritDoc More...
 
 encodePassword (string $raw, string $salt)
 Encodes the raw password. More...
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 Checks a raw password against an encoded password. More...
 
 getName ()
 Returns a unique name/id of the concrete password encoder. More...
 
 requiresSalt ()
 Returns whether or not the encoder requires a salt. More...
 
 requiresReencoding (string $encoded)
 Returns whether or not the a encoded password needs to be re-encoded. More...
 
 isSupportedByRuntime ()
 Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...) More...
 

Data Fields

const MIN_SALT_SIZE = 16
 
const SALT_STORAGE_FILENAME = 'pwsalt.txt'
 
- Data Fields inherited from ilBasePasswordEncoder
const MAX_PASSWORD_LENGTH = 4096
 

Protected Member Functions

 init ()
 
 isBcryptSupported ()
 
 encode (string $raw, string $userSecret)
 Generates a bcrypt encoded string. More...
 
 check (string $encoded, string $raw, string $salt)
 Verifies a bcrypt encoded string. More...
 
 init ()
 
- Protected Member Functions inherited from ilBasePasswordEncoder
 comparePasswords (string $knownString, string $userString)
 Compares two passwords. More...
 
 isPasswordTooLong (string $password)
 Checks if the password is too long. More...
 

Private Member Functions

 readClientSalt ()
 
 generateClientSalt ()
 
 storeClientSalt ()
 

Private Attributes

 $client_salt = null
 
 $is_security_flaw_ignored = false
 
 $backward_compatibility = false
 
 $data_directory = ''
 

Additional Inherited Members

- Protected Attributes inherited from ilBcryptPhpPasswordEncoder
 $costs = '08'
 

Detailed Description

Definition at line 12 of file class.ilBcryptPasswordEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

ilBcryptPasswordEncoder::__construct ( array  $config = [])
Parameters
array$config
Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 36 of file class.ilBcryptPasswordEncoder.php.

37 {
38 if (!empty($config)) {
39 foreach ($config as $key => $value) {
40 switch (strtolower($key)) {
41 case 'ignore_security_flaw':
42 $this->setIsSecurityFlawIgnored($value);
43 break;
44
45 case 'data_directory':
46 $this->setDataDirectory($value);
47 break;
48 }
49 }
50 }
51
53 }
setDataDirectory(string $data_directory)
setIsSecurityFlawIgnored(bool $is_security_flaw_ignored)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc

References $config, ILIAS\GlobalScreen\Provider\__construct(), setDataDirectory(), and setIsSecurityFlawIgnored().

+ Here is the call graph for this function:

Member Function Documentation

◆ check()

ilBcryptPasswordEncoder::check ( string  $encoded,
string  $raw,
string  $salt 
)
protected

Verifies a bcrypt encoded string.

Parameters
string$encoded
string$raw
string$salt
Returns
bool

Definition at line 245 of file class.ilBcryptPasswordEncoder.php.

245 : bool
246 {
247 $hashedPassword = hash_hmac(
248 'whirlpool',
249 str_pad($raw, strlen($raw) * 4, sha1($salt), STR_PAD_BOTH),
250 $this->getClientSalt(),
251 true
252 );
253
254 return $this->comparePasswords($encoded, crypt($hashedPassword, substr($encoded, 0, 30)));
255 }
comparePasswords(string $knownString, string $userString)
Compares two passwords.

References ilBasePasswordEncoder\comparePasswords(), and getClientSalt().

Referenced by isPasswordValid().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encode()

ilBcryptPasswordEncoder::encode ( string  $raw,
string  $userSecret 
)
protected

Generates a bcrypt encoded string.

Parameters
string$rawThe raw password
string$userSecretA randomly generated string (should be 16 ASCII chars)
Returns
string
Exceptions
ilPasswordException

Check for security flaw in the bcrypt implementation used by crypt()

See also
http://php.net/security/crypt_blowfish.php

Definition at line 197 of file class.ilBcryptPasswordEncoder.php.

197 : string
198 {
199 $clientSecret = $this->getClientSalt();
200 $hashedPassword = hash_hmac(
201 'whirlpool',
202 str_pad($raw, strlen($raw) * 4, sha1($userSecret), STR_PAD_BOTH),
203 $clientSecret,
204 true
205 );
206 $salt = substr(
207 str_shuffle(str_repeat('./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 22)),
208 0,
209 22
210 );
211
216 if ($this->isBcryptSupported() && !$this->isBackwardCompatibilityEnabled()) {
217 $prefix = '$2y$';
218 } else {
219 $prefix = '$2a$';
220 // check if the password contains 8-bit character
221 if (!$this->isSecurityFlawIgnored() && preg_match('/[\x80-\xFF]/', $raw)) {
222 throw new ilPasswordException(
223 'The bcrypt implementation used by PHP can contain a security flaw ' .
224 'using passwords with 8-bit characters. ' .
225 'We suggest to upgrade to PHP 5.3.7+ or use passwords with only 7-bit characters.'
226 );
227 }
228 }
229
230 $saltedPassword = crypt($hashedPassword, $prefix . $this->getCosts() . '$' . $salt);
231 if (strlen($saltedPassword) <= 13) {
232 throw new ilPasswordException('Error during the bcrypt generation');
233 }
234
235 return $saltedPassword;
236 }
Class for user password exception handling in ILIAS.

References getClientSalt(), ilBcryptPhpPasswordEncoder\getCosts(), isBackwardCompatibilityEnabled(), isBcryptSupported(), and isSecurityFlawIgnored().

Referenced by encodePassword().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encodePassword()

ilBcryptPasswordEncoder::encodePassword ( string  $raw,
string  $salt 
)

@inheritDoc

Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 140 of file class.ilBcryptPasswordEncoder.php.

140 : string
141 {
142 if (!$this->getClientSalt()) {
143 throw new ilPasswordException('Missing client salt.');
144 }
145
146 if ($this->isPasswordTooLong($raw)) {
147 throw new ilPasswordException('Invalid password.');
148 }
149
150 return $this->encode($raw, $salt);
151 }
isPasswordTooLong(string $password)
Checks if the password is too long.
encode(string $raw, string $userSecret)
Generates a bcrypt encoded string.

References encode(), getClientSalt(), and ilBasePasswordEncoder\isPasswordTooLong().

Referenced by ilBcryptPasswordEncoderTest\testBackwardCompatibility(), ilBcryptPasswordEncoderTest\testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled(), ilBcryptPasswordEncoderTest\testExceptionIsRaisedIfSaltIsMissingIsOnEncoding(), and ilBcryptPasswordEncoderTest\testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateClientSalt()

ilBcryptPasswordEncoder::generateClientSalt ( )
private

Definition at line 284 of file class.ilBcryptPasswordEncoder.php.

284 : void
285 {
286 $this->setClientSalt(
287 substr(str_replace('+', '.', base64_encode(ilPasswordUtils::getBytes(self::MIN_SALT_SIZE))), 0, 22)
288 );
289 }
static getBytes($length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.

References ilPasswordUtils\getBytes(), and setClientSalt().

Referenced by readClientSalt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getClientSalt()

ilBcryptPasswordEncoder::getClientSalt ( )

◆ getClientSaltLocation()

ilBcryptPasswordEncoder::getClientSaltLocation ( )
Returns
string

Definition at line 260 of file class.ilBcryptPasswordEncoder.php.

References getDataDirectory(), and SALT_STORAGE_FILENAME.

Referenced by readClientSalt(), and storeClientSalt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getDataDirectory()

ilBcryptPasswordEncoder::getDataDirectory ( )
Returns
string

Definition at line 74 of file class.ilBcryptPasswordEncoder.php.

References $data_directory.

Referenced by getClientSaltLocation().

+ Here is the caller graph for this function:

◆ getName()

ilBcryptPasswordEncoder::getName ( )

@inheritDoc

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 169 of file class.ilBcryptPasswordEncoder.php.

169 : string
170 {
171 return 'bcrypt';
172 }

Referenced by ilBcryptPasswordEncoderTest\testNameShouldBeBcrypt().

+ Here is the caller graph for this function:

◆ init()

ilBcryptPasswordEncoder::init ( )
protected
Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 58 of file class.ilBcryptPasswordEncoder.php.

58 : void
59 {
60 $this->readClientSalt();
61 }

References readClientSalt().

+ Here is the call graph for this function:

◆ isBackwardCompatibilityEnabled()

ilBcryptPasswordEncoder::isBackwardCompatibilityEnabled ( )
Returns
boolean

Definition at line 90 of file class.ilBcryptPasswordEncoder.php.

90 : bool
91 {
93 }

References $backward_compatibility.

Referenced by encode(), and ilBcryptPasswordEncoderTest\testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet().

+ Here is the caller graph for this function:

◆ isBcryptSupported()

ilBcryptPasswordEncoder::isBcryptSupported ( )
protected
Returns
bool

Definition at line 66 of file class.ilBcryptPasswordEncoder.php.

66 : bool
67 {
68 return PHP_VERSION_ID >= 50307;
69 }

Referenced by encode().

+ Here is the caller graph for this function:

◆ isPasswordValid()

ilBcryptPasswordEncoder::isPasswordValid ( string  $encoded,
string  $raw,
string  $salt 
)

@inheritDoc

Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 157 of file class.ilBcryptPasswordEncoder.php.

157 : bool
158 {
159 if (!$this->getClientSalt()) {
160 throw new ilPasswordException('Missing client salt.');
161 }
162
163 return !$this->isPasswordTooLong($raw) && $this->check($encoded, $raw, $salt);
164 }
check(string $encoded, string $raw, string $salt)
Verifies a bcrypt encoded string.

References check(), getClientSalt(), and ilBasePasswordEncoder\isPasswordTooLong().

Referenced by ilBcryptPasswordEncoderTest\testBackwardCompatibility(), and ilBcryptPasswordEncoderTest\testExceptionIsRaisedIfSaltIsMissingIsOnVerification().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isSecurityFlawIgnored()

ilBcryptPasswordEncoder::isSecurityFlawIgnored ( )
Returns
boolean

Definition at line 107 of file class.ilBcryptPasswordEncoder.php.

107 : bool
108 {
110 }

References $is_security_flaw_ignored.

Referenced by encode().

+ Here is the caller graph for this function:

◆ readClientSalt()

ilBcryptPasswordEncoder::readClientSalt ( )
private
Exceptions
ilPasswordException

Definition at line 268 of file class.ilBcryptPasswordEncoder.php.

268 : void
269 {
270 if (is_file($this->getClientSaltLocation()) && is_readable($this->getClientSaltLocation())) {
271 $contents = file_get_contents($this->getClientSaltLocation());
272 if (strlen(trim($contents))) {
273 $this->setClientSalt($contents);
274 }
275 } else {
276 $this->generateClientSalt();
277 $this->storeClientSalt();
278 }
279 }

References generateClientSalt(), getClientSaltLocation(), setClientSalt(), and storeClientSalt().

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ requiresReencoding()

ilBcryptPasswordEncoder::requiresReencoding ( string  $encoded)

@inheritDoc

Reimplemented from ilBcryptPhpPasswordEncoder.

Definition at line 185 of file class.ilBcryptPasswordEncoder.php.

185 : bool
186 {
187 return false;
188 }

Referenced by ilBcryptPasswordEncoderTest\testEncoderDoesNotSupportReencoding().

+ Here is the caller graph for this function:

◆ requiresSalt()

ilBcryptPasswordEncoder::requiresSalt ( )

@inheritDoc

Reimplemented from ilBasePasswordEncoder.

Definition at line 177 of file class.ilBcryptPasswordEncoder.php.

177 : bool
178 {
179 return true;
180 }

Referenced by ilBcryptPasswordEncoderTest\testEncoderReliesOnSalts().

+ Here is the caller graph for this function:

◆ setBackwardCompatibility()

ilBcryptPasswordEncoder::setBackwardCompatibility ( bool  $backward_compatibility)

◆ setClientSalt()

◆ setDataDirectory()

ilBcryptPasswordEncoder::setDataDirectory ( string  $data_directory)
Parameters
string$data_directory

Definition at line 82 of file class.ilBcryptPasswordEncoder.php.

82 : void
83 {
84 $this->data_directory = $data_directory;
85 }

References $data_directory.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ setIsSecurityFlawIgnored()

ilBcryptPasswordEncoder::setIsSecurityFlawIgnored ( bool  $is_security_flaw_ignored)
Parameters
boolean$is_security_flaw_ignored

Definition at line 115 of file class.ilBcryptPasswordEncoder.php.

115 : void
116 {
117 $this->is_security_flaw_ignored = (bool) $is_security_flaw_ignored;
118 }

References $is_security_flaw_ignored.

Referenced by __construct(), and ilBcryptPasswordEncoderTest\testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw().

+ Here is the caller graph for this function:

◆ storeClientSalt()

ilBcryptPasswordEncoder::storeClientSalt ( )
private
Exceptions
ilPasswordException

Definition at line 294 of file class.ilBcryptPasswordEncoder.php.

294 : void
295 {
296 $result = @file_put_contents($this->getClientSaltLocation(), $this->getClientSalt());
297 if (!$result) {
298 throw new ilPasswordException(sprintf(
299 "Could not store the client salt in: %s. Please contact an administrator.",
300 $this->getClientSaltLocation()
301 ));
302 }
303 }
$result

References $result, getClientSalt(), and getClientSaltLocation().

Referenced by readClientSalt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $backward_compatibility

ilBcryptPasswordEncoder::$backward_compatibility = false
private

◆ $client_salt

ilBcryptPasswordEncoder::$client_salt = null
private

Definition at line 21 of file class.ilBcryptPasswordEncoder.php.

Referenced by getClientSalt(), and setClientSalt().

◆ $data_directory

ilBcryptPasswordEncoder::$data_directory = ''
private

Definition at line 30 of file class.ilBcryptPasswordEncoder.php.

Referenced by getDataDirectory(), and setDataDirectory().

◆ $is_security_flaw_ignored

ilBcryptPasswordEncoder::$is_security_flaw_ignored = false
private

◆ MIN_SALT_SIZE

const ilBcryptPasswordEncoder::MIN_SALT_SIZE = 16

Definition at line 15 of file class.ilBcryptPasswordEncoder.php.

◆ SALT_STORAGE_FILENAME

const ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME = 'pwsalt.txt'

The documentation for this class was generated from the following file: