ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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)
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 
 getName ()
 
 requiresSalt ()
 
 requiresReencoding (string $encoded)
 
 getClientSaltLocation ()
 
- Public Member Functions inherited from ilBcryptPhpPasswordEncoder
 __construct (array $config=[])
 
 benchmarkCost (float $time_target=0.05)
 
 getName ()
 
 isSupportedByRuntime ()
 
 getCosts ()
 
 setCosts (string $costs)
 
 encodePassword (string $raw, string $salt)
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 
 requiresReencoding (string $encoded)
 
- Public Member Functions inherited from ilBasePasswordEncoder
 isSupportedByRuntime ()
 
 requiresSalt ()
 
 requiresReencoding (string $encoded)
 

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...
 
- Protected Member Functions inherited from ilBcryptPhpPasswordEncoder
 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 11 of file class.ilBcryptPasswordEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

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

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

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

36  {
37  if (!empty($config)) {
38  foreach ($config as $key => $value) {
39  switch (strtolower($key)) {
40  case 'ignore_security_flaw':
41  $this->setIsSecurityFlawIgnored($value);
42  break;
43 
44  case 'data_directory':
45  $this->setDataDirectory($value);
46  break;
47  }
48  }
49  }
50 
52  }
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
setIsSecurityFlawIgnored(bool $is_security_flaw_ignored)
setDataDirectory(string $data_directory)
__construct(Container $dic, ilPlugin $plugin)
+ 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 244 of file class.ilBcryptPasswordEncoder.php.

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

Referenced by isPasswordValid().

244  : bool
245  {
246  $hashedPassword = hash_hmac(
247  'whirlpool',
248  str_pad($raw, strlen($raw) * 4, sha1($salt), STR_PAD_BOTH),
249  $this->getClientSalt(),
250  true
251  );
252 
253  return $this->comparePasswords($encoded, crypt($hashedPassword, substr($encoded, 0, 30)));
254  }
comparePasswords(string $knownString, string $userString)
Compares two passwords.
+ 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 196 of file class.ilBcryptPasswordEncoder.php.

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

Referenced by encodePassword().

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

◆ encodePassword()

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

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

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

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

Referenced by ilBcryptPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), and ilBcryptPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified().

139  : string
140  {
141  if (!$this->getClientSalt()) {
142  throw new ilPasswordException('Missing client salt.');
143  }
144 
145  if ($this->isPasswordTooLong($raw)) {
146  throw new ilPasswordException('Invalid password.');
147  }
148 
149  return $this->encode($raw, $salt);
150  }
encode(string $raw, string $userSecret)
Generates a bcrypt encoded string.
Class for user password exception handling in ILIAS.
isPasswordTooLong(string $password)
Checks if the password is too long.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateClientSalt()

ilBcryptPasswordEncoder::generateClientSalt ( )
private

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

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

Referenced by readClientSalt().

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

◆ getClientSalt()

ilBcryptPasswordEncoder::getClientSalt ( )
Returns
string|null

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

References $client_salt.

Referenced by check(), encode(), encodePassword(), isPasswordValid(), and storeClientSalt().

122  : ?string
123  {
124  return $this->client_salt;
125  }
+ Here is the caller graph for this function:

◆ getClientSaltLocation()

ilBcryptPasswordEncoder::getClientSaltLocation ( )
Returns
string

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

References getDataDirectory().

Referenced by readClientSalt(), and storeClientSalt().

259  : string
260  {
261  return $this->getDataDirectory() . '/' . self::SALT_STORAGE_FILENAME;
262  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getDataDirectory()

ilBcryptPasswordEncoder::getDataDirectory ( )
Returns
string

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

References $data_directory.

Referenced by getClientSaltLocation().

73  : string
74  {
75  return $this->data_directory;
76  }
+ Here is the caller graph for this function:

◆ getName()

ilBcryptPasswordEncoder::getName ( )

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPasswordEncoderTest\testNameShouldBeBcrypt().

168  : string
169  {
170  return 'bcrypt';
171  }
+ Here is the caller graph for this function:

◆ init()

ilBcryptPasswordEncoder::init ( )
protected
Exceptions
ilPasswordException

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

References readClientSalt().

57  : void
58  {
59  $this->readClientSalt();
60  }
+ Here is the call graph for this function:

◆ isBackwardCompatibilityEnabled()

ilBcryptPasswordEncoder::isBackwardCompatibilityEnabled ( )
Returns
boolean

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

References $backward_compatibility.

Referenced by encode().

89  : bool
90  {
91  return (bool) $this->backward_compatibility;
92  }
+ Here is the caller graph for this function:

◆ isBcryptSupported()

ilBcryptPasswordEncoder::isBcryptSupported ( )
protected
Returns
bool

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

Referenced by encode().

65  : bool
66  {
67  return PHP_VERSION_ID >= 50307;
68  }
+ Here is the caller graph for this function:

◆ isPasswordValid()

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

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

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

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

Referenced by ilBcryptPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), and ilBcryptPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength().

156  : bool
157  {
158  if (!$this->getClientSalt()) {
159  throw new ilPasswordException('Missing client salt.');
160  }
161 
162  return !$this->isPasswordTooLong($raw) && $this->check($encoded, $raw, $salt);
163  }
check(string $encoded, string $raw, string $salt)
Verifies a bcrypt encoded string.
Class for user password exception handling in ILIAS.
isPasswordTooLong(string $password)
Checks if the password is too long.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isSecurityFlawIgnored()

ilBcryptPasswordEncoder::isSecurityFlawIgnored ( )
Returns
boolean

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

References $is_security_flaw_ignored.

Referenced by encode().

106  : bool
107  {
108  return (bool) $this->is_security_flaw_ignored;
109  }
+ Here is the caller graph for this function:

◆ readClientSalt()

ilBcryptPasswordEncoder::readClientSalt ( )
private
Exceptions
ilPasswordException

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

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

Referenced by init().

267  : void
268  {
269  if (is_file($this->getClientSaltLocation()) && is_readable($this->getClientSaltLocation())) {
270  $contents = file_get_contents($this->getClientSaltLocation());
271  if (strlen(trim($contents))) {
272  $this->setClientSalt($contents);
273  }
274  } else {
275  $this->generateClientSalt();
276  $this->storeClientSalt();
277  }
278  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ requiresReencoding()

ilBcryptPasswordEncoder::requiresReencoding ( string  $encoded)

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPasswordEncoderTest\testEncoderDoesNotSupportReencoding().

184  : bool
185  {
186  return false;
187  }
+ Here is the caller graph for this function:

◆ requiresSalt()

ilBcryptPasswordEncoder::requiresSalt ( )

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPasswordEncoderTest\testEncoderReliesOnSalts().

176  : bool
177  {
178  return true;
179  }
+ Here is the caller graph for this function:

◆ setBackwardCompatibility()

ilBcryptPasswordEncoder::setBackwardCompatibility ( bool  $backward_compatibility)

Set the backward compatibility $2a$ instead of $2y$ for PHP 5.3.7+.

Parameters
boolean$backward_compatibility

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

98  : void
99  {
100  $this->backward_compatibility = (bool) $backward_compatibility;
101  }

◆ setClientSalt()

ilBcryptPasswordEncoder::setClientSalt ( ?string  $client_salt)
Parameters
string | null$client_salt

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

References $client_salt.

Referenced by generateClientSalt(), and readClientSalt().

131  {
132  $this->client_salt = $client_salt;
133  }
+ Here is the caller graph for this function:

◆ setDataDirectory()

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

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

References $data_directory.

Referenced by __construct().

81  : void
82  {
83  $this->data_directory = $data_directory;
84  }
+ 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 114 of file class.ilBcryptPasswordEncoder.php.

Referenced by __construct().

114  : void
115  {
116  $this->is_security_flaw_ignored = (bool) $is_security_flaw_ignored;
117  }
+ Here is the caller graph for this function:

◆ storeClientSalt()

ilBcryptPasswordEncoder::storeClientSalt ( )
private
Exceptions
ilPasswordException

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

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

Referenced by readClientSalt().

293  : void
294  {
295  $result = @file_put_contents($this->getClientSaltLocation(), $this->getClientSalt());
296  if (!$result) {
297  throw new ilPasswordException(sprintf(
298  "Could not store the client salt in: %s. Please contact an administrator.",
299  $this->getClientSaltLocation()
300  ));
301  }
302  }
$result
Class for user password exception handling in ILIAS.
+ 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

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

Referenced by isBackwardCompatibilityEnabled().

◆ $client_salt

ilBcryptPasswordEncoder::$client_salt = null
private

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

Referenced by getClientSalt(), and setClientSalt().

◆ $data_directory

ilBcryptPasswordEncoder::$data_directory = ''
private

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

Referenced by getDataDirectory(), and setDataDirectory().

◆ $is_security_flaw_ignored

ilBcryptPasswordEncoder::$is_security_flaw_ignored = false
private

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

Referenced by isSecurityFlawIgnored().

◆ MIN_SALT_SIZE

const ilBcryptPasswordEncoder::MIN_SALT_SIZE = 16

Definition at line 14 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: