8000 merged branch fabpot/prng (PR #4763) · s7ntech/symfony@d21584e · GitHub
[go: up one dir, main page]

Skip to content

Commit d21584e

Browse files
committed
merged branch fabpot/prng (PR symfony#4763)
This PR was merged into the master branch. Commits ------- aecc9b1 fixed tests when OpenSsl is not enabled in PHP, renamed a missnamed test, added missing license doc blocks ca567b5 fixed CS 5cdf696 added a SecureRandomInterface 234f725 rename String to StringUtils 5849855 moved the secure random dep for remember me as a constructor argument 248703f renamed Prng to SecureRandom c0c8972 simplified the Prng code e5dc7af moved the secure random class from JMSSecurityExtraBundle to Symfony (closes symfony#3595) Discussion ---------- [2.2][Security] Add a PRNG (closes symfony#3595) As per symfony#3595, I have moved the secure random class from JMSSecurityExtraBundle to Symfony. It has more impact than I expected ;) As you will see, the implementation has been refactored a bit. The most notable change is that Doctrine support has been moved to the bridge with the addition of a proper Doctrine seed provider (Doctrine is not a special case anymore). The Doctrine configuration has been moved to the DoctrineBundle: doctrine/DoctrineBundle#91 schmittjoh/JMSSecurityExtraBundle#65 removes the code that has been moved. --------------------------------------------------------------------------- by Seldaek at 2012-07-05T13:26:01Z I'm all for more security features, and both the String class & the Prng class for wrapping openssl make a lot of sense IMO, but I fail to see the use of the rest. If we just want a seed to have a fallback in case openssl is missing, I'd rather have a secret in the config.yml than a million classes to store the same secret in the DB. Maybe I'm missing something though? /cc @schmittjoh --------------------------------------------------------------------------- by schmittjoh at 2012-07-05T16:32:10Z Having the configuration in different places (SecurityBundle & DoctrineBundle) feels a bit weird. I would prefer an approach similar to ACL, or the user provider/firewall section with factories. The latter being a bit more work to implement and the former potentially asking for complaints about too tight coupling to Doctrine. Regarding testing, we probably need to move the disableOpenSsl method to the SecureRandom class in order to allow OpenSSL to be disabled for testing and we also need to change the byte generation algorithm to produce the same output for the same starting seed. I agree that it does not make sense to introduce an interface for SecureRandom as only the seed providers should be replaced. As for the seed itself, it is constantly updated and does not stay the same as in the beginning. Thus, we need a provider that we can write to, and not only read from. I'm also not sure about using OpenSSL on Windows as I have read enough resources which claimed that the entropy on Windows is not always good (including OpenSSL docs). Always using the custom seed provider at least always ensured proper entropy even if OpenSSL's speed issues have been fixed in newer PHP versions. --------------------------------------------------------------------------- by stof at 2012-07-05T16:44:24Z @schmittjoh everything is in SecurityBundle now as it does not use a database anymore --------------------------------------------------------------------------- by stof at 2012-07-05T16:44:59Z and there is no seed provider anymore either --------------------------------------------------------------------------- by schmittjoh at 2012-07-05T16:53:39Z Not having a seed provider is not such a good idea, but having a file-based seed provider is. --------------------------------------------------------------------------- by Seldaek at 2012-07-05T17:01:18Z @schmittjoh why would you need to replace the seed provider? Don't you think that people serious about security to the point that they would want a stronger seed provider would enable openssl instead? --------------------------------------------------------------------------- by stof at 2012-07-05T17:06:50Z Well, what I meant is that there is no interchangeable provider anymore. The Prng class uses the file directly. And btw, I think the Prng class should be mockable for tests, so it should either have an interface or not be final (I vote for adding an interface) --------------------------------------------------------------------------- by jalliot at 2012-07-09T18:46:12Z @fabpot @schmittjoh What about using more fallbacks for `openssl_random_pseudo_bytes` like in @Seldaek's post ["Unpredictable hashes for humans"](http://seld.be/notes/unpredictable-hashes-for-humans)? Trying `mcrypt_create_iv` first might also be faster. --------------------------------------------------------------------------- by Seldaek at 2012-07-10T08:52:46Z @jalliot I think mcrypt should be after if you make it use /dev/urandom, not 100% sure but openssl is probably higher quality than urandom. --------------------------------------------------------------------------- by schmittjoh at 2012-07-10T09:12:07Z The fallback algorithm that I added should be enough (it passes the statistical randomness tests). On Tue, Jul 10, 2012 at 10:52 AM, Jordi Boggiano < reply@reply.github.com > wrote: > @jalliot I think mcrypt should be after if you make it use /dev/urandom, > not 100% sure but openssl is probably higher quality than urandom. > > --- > Reply to this email directly or view it on GitHub: > symfony#4763 (comment) > --------------------------------------------------------------------------- by stof at 2012-10-13T17:20:06Z @fabpot please send a PR to the doc so that this can be merged 😃 --------------------------------------------------------------------------- by stof at 2012-10-13T17:22:08Z hmm, actually, some comments have not been taken into account yet so it is not ready to be merged --------------------------------------------------------------------------- by stof at 2012-10-27T07:14:43Z you forgot the SecureRandom file --------------------------------------------------------------------------- by fabpot at 2012-10-27T08:49:54Z I think I've addressed all the comments. If everyone agree with the current implementation, I'm going to start updating the documentation. --------------------------------------------------------------------------- by fabpot at 2012-10-27T10:51:15Z I've fixed the remaining CS issues. --------------------------------------------------------------------------- by fabpot at 2012-10-28T07:00:31Z Documentation is here: symfony/symfony-docs#1858
2 parents eb05fb0 + aecc9b1 commit d21584e

File tree

12 files changed

+472
-37
lines changed

12 files changed

+472
-37
lines changed

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,12 @@
138138
<argument type="service" id="security.context" />
139139
<argument type="service" id="security.encoder_factory" />
140140
</service>
141+
142+
<!-- Pseudorandom Number Generator -->
143+
<service id="security.secure_random" class="Symfony\Component\Security\Core\Util\SecureRandom">
144+
<tag name="monolog.logger" channel="security" />
145+
<argument>%kernel.cache_dir%/secure_random.seed</argument>
146+
<argument type="service" id="logger" on-invalid="ignore" />
147+
</service>
141148
</services>
142149
</container>

src/Symfony/Bundle/SecurityBundle/Resources/config/security_rememberme.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
class="%security.authentication.rememberme.services.persistent.class%"
4646
parent="security.authentication.rememberme.services.abstract"
4747
abstract="true">
48+
<argument type="collection" /> <!-- User Providers -->
49+
<argument /> <!-- Shared Token Key -->
50+
<argument /> <!-- Shared Provider Key -->
51+
<argument type="collection" /> <!-- Options -->
52+
<argument type="service" id="logger" on-invalid="null" />
53+
<argument type="service" id="security.secure_random" />
4854
</service>
4955

5056
<service id="security.authentication.rememberme.services.simplehash"

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ CHANGELOG
44
2.2.0
55
-----
66

7-
* Added PBKDF2 Password encoder
7+
* added secure random number generator
8+
* added PBKDF2 Password encoder
89

910
2.1.0
1011
-----

src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Core\Encoder;
1313

14+
use Symfony\Component\Security\Core\Util\StringUtils;
15+
1416
/**
1517
* BasePasswordEncoder is the base class for all password encoders.
1618
*
@@ -77,15 +79,6 @@ protected function mergePasswordAndSalt($password, $salt)
7779
*/
7880
protected function comparePasswords($password1, $password2)
7981
{
80-
if (strlen($password1) !== strlen($password2)) {
81-
return false;
82-
}
83-
84-
$result = 0;
85-
for ($i = 0; $i < strlen($password1); $i++) {
86-
$result |= ord($password1[$i]) ^ ord($password2[$i]);
87-
}
88-
89-
return 0 === $result;
82+
return StringUtils::equals($password1, $password2);
9083
}
9184
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Util;
13+
14+
use Symfony\Component\HttpKernel\Log\LoggerInterface;
15+
16+
/**
17+
* A secure random number generator implementation.
18+
*
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
21+
*/
22+
final class SecureRandom implements SecureRandomInterface
23+
{
24+
private $logger;
25+
private $useOpenSsl;
26+
private $seed;
27+
private $seedUpdated;
28+
private $seedLastUpdatedAt;
29+
private $seedFile;
30+
31+
/**
32+
* Constructor.
33+
*
34+
* Be aware that a guessable seed will severely compromise the PRNG
35+
* algorithm that is employed.
36+
*
37+
* @param string $seedFile
38+
* @param LoggerInterface $logger
39+
*/
40+
public function __construct($seedFile = null, LoggerInterface $logger = null)
41+
{
42+
$this->seedFile = $seedFile;
43+
$this->logger = $logger;
44+
45+
// determine whether to use OpenSSL
46+
if (defined('PHP_WINDOWS_VERSION_BUILD') && version_compare(PHP_VERSION, '5.3.4', '<')) {
47+
$this->useOpenSsl = false;
48+
} elseif (!function_exists('openssl_random_pseudo_bytes')) {
49+
if (null !== $this->logger) {
50+
$this->logger->notice('It is recommended that you enable the "openssl" extension for random number generation.');
51+
}
52+
$this->useOpenSsl = false;
53+
} else {
54+
$this->useOpenSsl = true;
55+
}
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function nextBytes($nbBytes)
62+
{
63+
// try OpenSSL
64+
if ($this->useOpenSsl) {
65+
$bytes = openssl_random_pseudo_bytes($nbBytes, $strong);
66+
67+
if (false !== $bytes && true === $strong) {
68+
return $bytes;
69+
}
70+
71+
if (null !== $this->logger) {
72+
$this->logger->info('OpenSSL did not produce a secure random number.');
73+
}
74+
}
75+
76+
// initialize seed
77+
if (null === $this->seed) {
78+
if (null === $this->seedFile) {
79+
throw new \RuntimeException('You need to specify a file path to store the seed.');
80+
}
81+
82+
if (is_file($this->seedFile)) {
83+
list($this->seed, $this->seedLastUpdatedAt) = $this->readSeed();
84+
} else {
85+
$this->seed = uniqid(mt_rand(), true);
86+
$this->updateSeed();
87+
}
88+
}
89+
90+
$bytes = '';
91+
while (strlen($bytes) < $nbBytes) {
92+
static $incr = 1;
93+
$bytes .= hash('sha512', $incr++.$this->seed.uniqid(mt_rand(), true).$nbBytes, true);
94+
$this->seed = base64_encode(hash('sha512', $this->seed.$bytes.$nbBytes, true));
95+
$this->updateSeed();
96+
}
97+
98+
return substr($bytes, 0, $nbBytes);
99+
}
100+
101+
private function readSeed()
102+
{
103+
return json_decode(file_get_contents($this->seedFile));
104+
}
105+
106+
private function updateSeed()
107+
{
108+
if (!$this->seedUpdated && $this->seedLastUpdatedAt < time() - mt_rand(1, 10)) {
109+
file_put_contents($this->seedFile, json_encode(array($this->seed, microtime(true))));
110+
}
111+
112+
$this->seedUpdated = true;
113+
}
114+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Util;
13+
14+
use Symfony\Component\HttpKernel\Log\LoggerInterface;
15+
16+
/**
17+
* Interface that needs to be implemented by all secure random number generators.
18+
*
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
*/
21+
interface SecureRandomInterface
22+
{
23+
/**
24+
* Generates the specified number of secure random bytes.
25+
*
26+
* @param integer $nbBytes
27+
*
28+
* @return string
29+
*/
30+
public function nextBytes($nbBytes);
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Util;
13+
14+
/**
15+
* String utility functions.
16+
*
17+
* @author Fabi 57A7 en Potencier <fabien@symfony.com>
18+
*/
19+
final class StringUtils
20+
{
21+
final private function __construct()
22+
{
23+
}
24+
25+
/**
26+
* Compares two strings.
27+
*
28+
* This method implements a constant-time algorithm to compare strings.
29+
*
30+
* @param string $str1 The first string
31+
* @param string $str2 The second string
32+
*
33+
* @return Boolean true if the two strings are the same, false otherwise
34+
*/
35+
public static function equals($str1, $str2)
36+
{
37+
if (strlen($str1) !== $c = strlen($str2)) {
38+
return false;
39+
}
40+
41+
$result = 0;
42+
for ($i = 0; $i < $c; $i++) {
43+
$result |= ord($str1[$i]) ^ ord($str2[$i]);
44+
}
45+
46+
return 0 === $result;
47+
}
48+
}

src/Symfony/Component/Security/Http/RememberMe/PersistentTokenBasedRememberMeServices.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Core\Exception\CookieTheftException;
2020
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22+
use Symfony\Component\Security\Core\Util\SecureRandomInterface;
2223

2324
/**
2425
* Concrete implementation of the RememberMeServicesInterface which needs
@@ -30,6 +31,24 @@
3031
class PersistentTokenBasedRememberMeServices extends AbstractRememberMeServices
3132
{
3233
priv F438 ate $tokenProvider;
34+
private $secureRandom;
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @param array $userProviders
40+
* @param string $key
41+
* @param string $providerKey
42+
* @param array $options
43+
* @param LoggerInterface $logger
44+
* @param SecureRandomInterface $secureRandom
45+
*/
46+
public function __construct(array $userProviders, $key, $providerKey, array $options = array(), LoggerInterface $logger = null, SecureRandomInterface $secureRandom)
47+
{
48+
parent::__construct($userProviders, $key, $providerKey, $options, $logger);
49+
50+
$this->secureRandom = $secureRandom;
51+
}
3352

3453
/**
3554
* Sets the token provider
@@ -79,7 +98,7 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
7998
}
8099

81100
$series = $persistentToken->getSeries();
82-
$tokenValue = $this->generateRandomValue();
101+
$tokenValue = $this->secureRandom->nextBytes(64);
83102
$this->tokenProvider->updateToken($series, $tokenValue, new \DateTime());
84103
$request->attributes->set(self::COOKIE_ATTR_NAME,
85104
new Cookie(
@@ -101,8 +120,8 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
101120
*/
102121
protected function onLoginSuccess(Request $request, Response $response, TokenInterface $token)
103122
{
104-
$series = $this->generateRandomValue();
105-
$tokenValue = $this->generateRandomValue();
123+
$series = $this->secureRandom->nextBytes(64);
124+
$tokenValue = $this->secureRandom->nextBytes(64);
106125

107126
$this->tokenProvider->createNewToken(
108127
new PersistentToken(
@@ -126,26 +145,4 @@ protected function onLoginSuccess(Request $request, Response $response, TokenInt
126145
)
127146
);
128147
}
129-
130-
/**
131-
* Generates a cryptographically strong random value
132-
*
133-
* @return string
134-
*/
135-
protected function generateRandomValue()
136-
{
137-
if (function_exists('openssl_random_pseudo_bytes')) {
138-
$bytes = openssl_random_pseudo_bytes(64, $strong);
139-
140-
if (true === $strong && false !== $bytes) {
141-
return base64_encode($bytes);
142-
}
143-
}
144-
145-
if (null !== $this->logger) {
146-
$this->logger->warn('Could not produce a cryptographically strong random value. Please install/update the OpenSSL extension.');
147-
}
148-
149-
return base64_encode(hash('sha512', uniqid(mt_rand(), true), true));
150-
}
151148
}

src/Symfony/Component/Security/Tests/Core/Util/ClassUtilsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Security\Tests\Core\Util
413
{
514
use Symfony\Component\Security\Core\Util\ClassUtils;

0 commit comments

Comments
 (0)
0