ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilBcryptPasswordEncoder Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoder:
+ Collaboration diagram for ilBcryptPasswordEncoder:

Public Member Functions

 __construct (array $config=array())
 
 isBackwardCompatibilityEnabled ()
 
 setBackwardCompatibility ($backward_compatibility)
 Set the backward compatibility $2a$ instead of $2y$ for PHP 5.3.7+. More...
 
 isSecurityFlawIgnored ()
 
 setIsSecurityFlawIgnored ($is_security_flaw_ignored)
 
 getClientSalt ()
 
 setClientSalt ($client_salt)
 
 encodePassword ($raw, $salt)
 {{Encodes the raw password.
Parameters
string$rawThe password to encode
string$saltThe salt
Returns
string The encoded password
}
Exceptions
ilPasswordException
} More...
 
 isPasswordValid ($encoded, $raw, $salt)
 {{Checks a raw password against an encoded password.The raw password has to be injected into the encoder instance before.

Parameters
string$encodedAn encoded password
string$rawA raw password
string$saltThe salt
Returns
Boolean true if the password is valid, false otherwise
}} More...
 
 getName ()
 {
Returns
string
} More...
 
 requiresSalt ()
 {{Returns whether or not the encoder requires a salt.
Returns
boolean
}} More...
 
 requiresReencoding ($encoded)
 {{{Returns whether or not the a encoded password needs to be re-encoded.
Parameters
$encodedstring
Returns
boolean
}}} More...
 
 getClientSaltLocation ()
 
- Public Member Functions inherited from ilBcryptPhpPasswordEncoder
 __construct (array $config=array())
 
 benchmarkCost ($time_target=0.05)
 
 getName ()
 
 isSupportedByRuntime ()
 {{Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)
Returns
boolean
}} More...
 
 getCosts ()
 
 setCosts ($costs)
 
 encodePassword ($raw, $salt)
 {Encodes the raw password.
Parameters
string$rawThe password to encode
string$saltThe salt
Returns
string The encoded password
} More...
 
 isPasswordValid ($encoded, $raw, $salt)
 {Checks a raw password against an encoded password.The raw password has to be injected into the encoder instance before.

Parameters
string$encodedAn encoded password
string$rawA raw password
string$saltThe salt
Returns
Boolean true if the password is valid, false otherwise
} More...
 
 requiresReencoding ($encoded)
 {{Returns whether or not the a encoded password needs to be re-encoded.
Parameters
$encodedstring
Returns
boolean
}} More...
 
 isSupportedByRuntime ()
 {Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)
Returns
boolean
} More...
 
 requiresSalt ()
 {Returns whether or not the encoder requires a salt.
Returns
boolean
} More...
 
 requiresReencoding ($encoded)
 {Returns whether or not the a encoded password needs to be re-encoded.
Parameters
$encodedstring
Returns
boolean
} More...
 
 encodePassword ($raw, $salt)
 Encodes the raw password. More...
 
 isPasswordValid ($encoded, $raw, $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 ($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 ($raw, $user_secret)
 Generates a bcrypt encoded string. More...
 
 check ($encoded, $raw, $salt)
 Verifies a bcrypt encoded string. More...
 
 init ()
 
- Protected Member Functions inherited from ilBasePasswordEncoder
 comparePasswords ($known_string, $user_string)
 Compares two passwords. More...
 
 isPasswordTooLong ($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
 

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 = array())
Parameters
array$config
Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

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

43 {
44 if(!empty($config))
45 {
46 foreach($config as $key => $value)
47 {
48 switch(strtolower($key))
49 {
50 case 'ignore_security_flaw':
51 $this->setIsSecurityFlawIgnored($value);
52 break;
53 }
54 }
55 }
56
57 parent::__construct($config);
58 }
setIsSecurityFlawIgnored($is_security_flaw_ignored)

References $config, and setIsSecurityFlawIgnored().

+ Here is the call graph for this function:

Member Function Documentation

◆ check()

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

Verifies a bcrypt encoded string.

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

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

240 {
241 $hashed_password = hash_hmac('whirlpool', str_pad($raw, strlen($raw) * 4, sha1($salt), STR_PAD_BOTH), $this->getClientSalt(), true);
242 return crypt($hashed_password, substr($encoded, 0, 30)) == $encoded;
243 }

References getClientSalt().

Referenced by isPasswordValid().

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

◆ encode()

ilBcryptPasswordEncoder::encode (   $raw,
  $user_secret 
)
protected

Generates a bcrypt encoded string.

Parameters
string$rawThe raw password
string$user_secretA 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 193 of file class.ilBcryptPasswordEncoder.php.

194 {
195 $client_secret = $this->getClientSalt();
196 $hashed_password = hash_hmac('whirlpool', str_pad($raw, strlen($raw) * 4, sha1($user_secret), STR_PAD_BOTH), $client_secret, true);
197 $salt = substr(str_shuffle(str_repeat('./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 22)), 0, 22);
198
203 if($this->isBcryptSupported() && !$this->isBackwardCompatibilityEnabled())
204 {
205 $prefix = '$2y$';
206 }
207 else
208 {
209 $prefix = '$2a$';
210 // check if the password contains 8-bit character
211 if(!$this->isSecurityFlawIgnored() && preg_match('/[\x80-\xFF]/', $raw))
212 {
213 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
214 throw new ilPasswordException(
215 'The bcrypt implementation used by PHP can contain a security flaw ' .
216 'using passwords with 8-bit characters. ' .
217 'We suggest to upgrade to PHP 5.3.7+ or use passwords with only 7-bit characters.'
218 );
219 }
220 }
221
222 $salted_password = crypt($hashed_password, $prefix . $this->getCosts() . '$' . $salt);
223 if(strlen($salted_password) <= 13)
224 {
225 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
226 throw new ilPasswordException('Error during the bcrypt generation');
227 }
228
229 return $salted_password;
230 }
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 (   $raw,
  $salt 
)

{{Encodes the raw password.

Parameters
string$rawThe password to encode
string$saltThe salt
Returns
string The encoded password
}
Exceptions
ilPasswordException
}

Exceptions
ilPasswordException

Reimplemented from ilBcryptPhpPasswordEncoder.

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

130 {
131 if(!$this->getClientSalt())
132 {
133 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
134 throw new ilPasswordException('Missing client salt.');
135 }
136
137 if($this->isPasswordTooLong($raw))
138 {
139 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
140 throw new ilPasswordException('Invalid password.');
141 }
142
143 return $this->encode($raw, $salt);
144 }
isPasswordTooLong($password)
Checks if the password is too long.
encode($raw, $user_secret)
Generates a bcrypt encoded string.

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

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

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

◆ generateClientSalt()

ilBcryptPasswordEncoder::generateClientSalt ( )
private

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

277 {
278 require_once 'Services/Password/classes/class.ilPasswordUtils.php';
279 $this->setClientSalt(
280 substr(str_replace('+', '.', base64_encode(ilPasswordUtils::getBytes(self::MIN_SALT_SIZE))), 0, 22)
281 );
282 }
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 ( )
Returns
string|null

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

References $client_salt.

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

+ Here is the caller graph for this function:

◆ getClientSaltLocation()

ilBcryptPasswordEncoder::getClientSaltLocation ( )
Returns
string

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

249 {
251 }
static getDataDir()
get data directory (outside webspace)

References ilUtil\getDataDir(), 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:

◆ getName()

ilBcryptPasswordEncoder::getName ( )

{

Returns
string
}

Reimplemented from ilBcryptPhpPasswordEncoder.

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

164 {
165 return 'bcrypt';
166 }

Referenced by ilBcryptPasswordEncoderTest\testNameShouldBeBcrypt().

+ Here is the caller graph for this function:

◆ init()

ilBcryptPasswordEncoder::init ( )
protected

Reimplemented from ilBcryptPhpPasswordEncoder.

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

References readClientSalt().

+ Here is the call graph for this function:

◆ isBackwardCompatibilityEnabled()

ilBcryptPasswordEncoder::isBackwardCompatibilityEnabled ( )
Returns
boolean

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

References $backward_compatibility.

Referenced by encode().

+ Here is the caller graph for this function:

◆ isBcryptSupported()

ilBcryptPasswordEncoder::isBcryptSupported ( )
protected
Returns
bool

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

72 {
73 return PHP_VERSION_ID >= 50307;
74 }

Referenced by encode().

+ Here is the caller graph for this function:

◆ isPasswordValid()

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

{{Checks a raw password against an encoded password.The raw password has to be injected into the encoder instance before.

Parameters
string$encodedAn encoded password
string$rawA raw password
string$saltThe salt
Returns
Boolean true if the password is valid, false otherwise
}}

Reimplemented from ilBcryptPhpPasswordEncoder.

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

150 {
151 if(!$this->getClientSalt())
152 {
153 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
154 throw new ilPasswordException('Missing client salt.');
155 }
156
157 return !$this->isPasswordTooLong($raw) && $this->check($encoded, $raw, $salt);
158 }
check($encoded, $raw, $salt)
Verifies a bcrypt encoded string.

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

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

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

◆ isSecurityFlawIgnored()

ilBcryptPasswordEncoder::isSecurityFlawIgnored ( )
Returns
boolean

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

References $is_security_flaw_ignored.

Referenced by encode().

+ Here is the caller graph for this function:

◆ readClientSalt()

ilBcryptPasswordEncoder::readClientSalt ( )
private

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

257 {
258 if(is_file($this->getClientSaltLocation()) && is_readable($this->getClientSaltLocation()))
259 {
260 $contents = file_get_contents($this->getClientSaltLocation());
261 if(strlen(trim($contents)))
262 {
263 $this->setClientSalt($contents);
264 }
265 }
266 else
267 {
268 $this->generateClientSalt();
269 $this->storeClientSalt();
270 }
271 }

References $contents, 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 (   $encoded)

{{{Returns whether or not the a encoded password needs to be re-encoded.

Parameters
$encodedstring
Returns
boolean
}}}

Reimplemented from ilBcryptPhpPasswordEncoder.

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

182 {
183 return false;
184 }

Referenced by ilBcryptPasswordEncoderTest\testEncoderDoesNotSupportReencoding().

+ Here is the caller graph for this function:

◆ requiresSalt()

ilBcryptPasswordEncoder::requiresSalt ( )

{{Returns whether or not the encoder requires a salt.

Returns
boolean
}}

Reimplemented from ilBasePasswordEncoder.

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

172 {
173 return true;
174 }

Referenced by ilBcryptPasswordEncoderTest\testEncoderReliesOnSalts().

+ Here is the caller graph for this function:

◆ setBackwardCompatibility()

ilBcryptPasswordEncoder::setBackwardCompatibility (   $backward_compatibility)

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

Parameters
boolean$backward_compatibility

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

89 {
90 $this->backward_compatibility = (bool)$backward_compatibility;
91 }

References $backward_compatibility.

◆ setClientSalt()

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

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

121 {
122 $this->client_salt = $client_salt;
123 }

References $client_salt.

Referenced by generateClientSalt(), and readClientSalt().

+ Here is the caller graph for this function:

◆ setIsSecurityFlawIgnored()

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

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

105 {
106 $this->is_security_flaw_ignored = (bool)$is_security_flaw_ignored;
107 }

References $is_security_flaw_ignored.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ storeClientSalt()

ilBcryptPasswordEncoder::storeClientSalt ( )
private
Exceptions
ilPasswordException

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

288 {
289 $result = @file_put_contents($this->getClientSaltLocation(), $this->getClientSalt());
290 if(!$result)
291 {
292 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
293 throw new ilPasswordException(sprintf("Could not store the client salt in: %s. Please contact an administrator.", $this->getClientSaltLocation()));
294 }
295 }
sprintf('%.4f', $callTime)
$result

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

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

Referenced by getClientSalt(), and setClientSalt().

◆ $is_security_flaw_ignored

ilBcryptPasswordEncoder::$is_security_flaw_ignored = false
private

◆ MIN_SALT_SIZE

const ilBcryptPasswordEncoder::MIN_SALT_SIZE = 16

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