ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 11 of file class.ilBcryptPasswordEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

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

Reimplemented from ilBcryptPhpPasswordEncoder.

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

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

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.

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

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.

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

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

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.

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 259 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 73 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 168 of file class.ilBcryptPasswordEncoder.php.

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

Referenced by ilBcryptPasswordEncoderTest\testNameShouldBeBcrypt().

+ Here is the caller graph for this function:

◆ init()

ilBcryptPasswordEncoder::init ( )
protected
Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

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

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

References readClientSalt().

+ Here is the call graph for this function:

◆ isBackwardCompatibilityEnabled()

ilBcryptPasswordEncoder::isBackwardCompatibilityEnabled ( )
Returns
boolean

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

89 : bool
90 {
92 }

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

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

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

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.

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

106 : bool
107 {
109 }

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

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 }

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

184 : bool
185 {
186 return false;
187 }

Referenced by ilBcryptPasswordEncoderTest\testEncoderDoesNotSupportReencoding().

+ Here is the caller graph for this function:

◆ requiresSalt()

ilBcryptPasswordEncoder::requiresSalt ( )

@inheritDoc

Reimplemented from ilBasePasswordEncoder.

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

176 : bool
177 {
178 return true;
179 }

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

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

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

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

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

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

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

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