ILIAS  release_7 Revision v7.30-3-g800a261c036
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 12 of file class.ilBcryptPasswordEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

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

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

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

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  }
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 245 of file class.ilBcryptPasswordEncoder.php.

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

Referenced by isPasswordValid().

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.
+ 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.

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

Referenced by encodePassword().

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.
+ 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 140 of file class.ilBcryptPasswordEncoder.php.

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

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

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  }
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 284 of file class.ilBcryptPasswordEncoder.php.

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

Referenced by readClientSalt().

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.
+ 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 123 of file class.ilBcryptPasswordEncoder.php.

References $client_salt.

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

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

◆ getClientSaltLocation()

ilBcryptPasswordEncoder::getClientSaltLocation ( )
Returns
string

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

References getDataDirectory().

Referenced by readClientSalt(), and storeClientSalt().

260  : string
261  {
262  return $this->getDataDirectory() . '/' . self::SALT_STORAGE_FILENAME;
263  }
+ 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().

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

◆ getName()

ilBcryptPasswordEncoder::getName ( )

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPasswordEncoderTest\testNameShouldBeBcrypt().

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

◆ init()

ilBcryptPasswordEncoder::init ( )
protected
Exceptions
ilPasswordException

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

References readClientSalt().

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

◆ isBackwardCompatibilityEnabled()

ilBcryptPasswordEncoder::isBackwardCompatibilityEnabled ( )
Returns
boolean

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

References $backward_compatibility.

Referenced by encode().

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

◆ isBcryptSupported()

ilBcryptPasswordEncoder::isBcryptSupported ( )
protected
Returns
bool

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

Referenced by encode().

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

◆ isPasswordValid()

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

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

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

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

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

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.
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 107 of file class.ilBcryptPasswordEncoder.php.

References $is_security_flaw_ignored.

Referenced by encode().

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

◆ readClientSalt()

ilBcryptPasswordEncoder::readClientSalt ( )
private
Exceptions
ilPasswordException

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

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

Referenced by init().

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  }
+ 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 185 of file class.ilBcryptPasswordEncoder.php.

Referenced by ilBcryptPasswordEncoderTest\testEncoderDoesNotSupportReencoding().

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

◆ requiresSalt()

ilBcryptPasswordEncoder::requiresSalt ( )

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPasswordEncoderTest\testEncoderReliesOnSalts().

177  : bool
178  {
179  return true;
180  }
+ 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 99 of file class.ilBcryptPasswordEncoder.php.

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

◆ setClientSalt()

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

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

References $client_salt.

Referenced by generateClientSalt(), and readClientSalt().

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

◆ setDataDirectory()

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

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

References $data_directory.

Referenced by __construct().

82  : void
83  {
84  $this->data_directory = $data_directory;
85  }
+ 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.

Referenced by __construct().

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

◆ storeClientSalt()

ilBcryptPasswordEncoder::storeClientSalt ( )
private
Exceptions
ilPasswordException

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

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

Referenced by readClientSalt().

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
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 27 of file class.ilBcryptPasswordEncoder.php.

Referenced by isBackwardCompatibilityEnabled().

◆ $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

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

Referenced by isSecurityFlawIgnored().

◆ 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: