Completed
Push — master ( be3b3f...d1dc3f )
by Ismayil
13:04
created

ElggCrypto   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 42.48%

Importance

Changes 0
Metric Value
dl 0
loc 305
ccs 48
cts 113
cp 0.4248
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
F getRandomBytes() 0 120 22
A getHmac() 0 6 2
B getRandomString() 0 36 6
A areEqual() 0 14 3
A strlen() 0 6 2
1
<?php
2
3
use \Elgg\Database\SiteSecret;
4
5
/**
6
 * \ElggCrypto
7
 *
8
 * @package    Elgg.Core
9
 * @subpackage Crypto
10
 *
11
 * @access private
12
 */
13
class ElggCrypto {
14
15
	/**
16
	 * Character set for temp passwords (no risk of embedded profanity/glyphs that look similar)
17
	 */
18
	const CHARS_PASSWORD = 'bcdfghjklmnpqrstvwxyz2346789';
19
20
	/**
21
	 * Character set for hexadecimal
22
	 */
23
	const CHARS_HEX = '0123456789abcdef';
24
25
	/**
26
	 * @var SiteSecret
27
	 */
28
	private $site_secret;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param SiteSecret $site_secret Secret service
34
	 */
35 122
	public function __construct(SiteSecret $site_secret = null) {
36 122
		$this->site_secret = $site_secret;
37 122
	}
38
39
	/**
40
	 * Generate a string of highly randomized bytes (over the full 8-bit range).
41
	 *
42
	 * @param int $length Number of bytes needed
43
	 * @return string Random bytes
44
	 *
45
	 * @author George Argyros <[email protected]>
46
	 * @copyright 2012, George Argyros. All rights reserved.
47
	 * @license Modified BSD
48
	 * @link https://github.com/GeorgeArgyros/Secure-random-bytes-in-PHP/blob/master/srand.php Original
49
	 *
50
	 * Redistribution and use in source and binary forms, with or without
51
	 * modification, are permitted provided that the following conditions are met:
52
	 *    * Redistributions of source code must retain the above copyright
53
	 *      notice, this list of conditions and the following disclaimer.
54
	 *    * Redistributions in binary form must reproduce the above copyright
55
	 *      notice, this list of conditions and the following disclaimer in the
56
	 *      documentation and/or other materials provided with the distribution.
57
	 *    * Neither the name of the <organization> nor the
58
	 *      names of its contributors may be used to endorse or promote products
59
	 *      derived from this software without specific prior written permission.
60
	 *
61
	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
62
	 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63
	 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64
	 * DISCLAIMED. IN NO EVENT SHALL GEORGE ARGYROS BE LIABLE FOR ANY
65
	 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66
	 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67
	 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68
	 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69
	 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
70
	 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71
	 */
72 337
	public function getRandomBytes($length) {
0 ignored issues
show
Coding Style introduced by
getRandomBytes uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getRandomBytes uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
73 337
		if (is_callable('random_bytes')) {
74
			try {
75
				return random_bytes($length);
76
			} catch (\Exception $e) {}
77
		}
78
79 337
		$SSLstr = '4'; // http://xkcd.com/221/
80
81
		/**
82
		 * Our primary choice for a cryptographic strong randomness function is
83
		 * openssl_random_pseudo_bytes.
84
		 */
85 337
		if (function_exists('openssl_random_pseudo_bytes') && substr(PHP_OS, 0, 3) !== 'WIN') {
86 337
			$SSLstr = openssl_random_pseudo_bytes($length, $strong);
87 337
			if ($strong) {
88 337
				return $SSLstr;
89
			}
90
		}
91
92
		/**
93
		 * If mcrypt extension is available then we use it to gather entropy from
94
		 * the operating system's PRNG. This is better than reading /dev/urandom
95
		 * directly since it avoids reading larger blocks of data than needed.
96
		 */
97
		if (function_exists('mcrypt_create_iv') && substr(PHP_OS, 0, 3) !== 'WIN') {
98
			$str = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
99
			if ($str !== false) {
100
				return $str;
101
			}
102
		}
103
104
		/**
105
		 * No build-in crypto randomness function found. We collect any entropy
106
		 * available in the PHP core PRNGs along with some filesystem info and memory
107
		 * stats. To make this data cryptographically strong we add data either from
108
		 * /dev/urandom or if its unavailable, we gather entropy by measuring the
109
		 * time needed to compute a number of SHA-1 hashes.
110
		 */
111
		$str = '';
112
		$bits_per_round = 2; // bits of entropy collected in each clock drift round
113
		$msec_per_round = 400; // expected running time of each round in microseconds
114
		$hash_len = 20; // SHA-1 Hash length
115
		$total = $length; // total bytes of entropy to collect
116
117
		$handle = @fopen('/dev/urandom', 'rb');
118
		if ($handle && function_exists('stream_set_read_buffer')) {
119
			@stream_set_read_buffer($handle, 0);
120
		}
121
122
		do {
123
			$bytes = ($total > $hash_len) ? $hash_len : $total;
124
			$total -= $bytes;
125
126
			//collect any entropy available from the PHP system and filesystem
127
			$entropy = rand() . uniqid(mt_rand(), true) . $SSLstr;
128
			$entropy .= implode('', @fstat(@fopen(__FILE__, 'r')));
129
			$entropy .= memory_get_usage() . getmypid();
130
			$entropy .= serialize($_ENV) . serialize($_SERVER);
131
			if (function_exists('posix_times')) {
132
				$entropy .= serialize(posix_times());
133
			}
134
			if (function_exists('zend_thread_id')) {
135
				$entropy .= zend_thread_id();
136
			}
137
138
			if ($handle) {
139
				$entropy .= @fread($handle, $bytes);
140
			} else {
141
				// In the future we should consider outright refusing to generate bytes without an
142
				// official random source, as many consider hacks like this irresponsible.
143
144
				// Measure the time that the operations will take on average
145
				for ($i = 0; $i < 3; $i++) {
146
					$c1 = microtime(true);
147
					$var = sha1(mt_rand());
148
					for ($j = 0; $j < 50; $j++) {
149
						$var = sha1($var);
150
					}
151
					$c2 = microtime(true);
152
					$entropy .= $c1 . $c2;
153
				}
154
155
				// Based on the above measurement determine the total rounds
156
				// in order to bound the total running time.
157
				if ($c2 - $c1 == 0) {
158
					// This occurs on some Windows systems. On a late 2013 MacBook Pro, 2.6 GHz Intel Core i7
159
					// this averaged 400, so we're just going with that. With all the other entropy gathered
160
					// this should be sufficient.
161
					$rounds = 400;
162
				} else {
163
					$rounds = (int) ($msec_per_round * 50 / (int) (($c2 - $c1) * 1000000));
0 ignored issues
show
Bug introduced by
The variable $c2 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $c1 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
164
				}
165
166
				// Take the additional measurements. On average we can expect
167
				// at least $bits_per_round bits of entropy from each measurement.
168
				$iter = $bytes * (int) (ceil(8 / $bits_per_round));
169
170
				for ($i = 0; $i < $iter; $i++) {
171
					$c1 = microtime();
172
					$var = sha1(mt_rand());
173
					for ($j = 0; $j < $rounds; $j++) {
174
						$var = sha1($var);
175
					}
176
					$c2 = microtime();
177
					$entropy .= $c1 . $c2;
178
				}
179
			}
180
181
			// We assume sha1 is a deterministic extractor for the $entropy variable.
182
			$str .= sha1($entropy, true);
183
184
		} while ($length > strlen($str));
185
186
		if ($handle) {
187
			@fclose($handle);
188
		}
189
190
		return substr($str, 0, $length);
191
	}
192
193
	/**
194
	 * Get an HMAC token builder/validator object
195
	 *
196
	 * @param mixed  $data HMAC data or serializable data
197
	 * @param string $algo Hash algorithm
198
	 * @param string $key  Optional key (default uses site secret)
199
	 *
200
	 * @return \Elgg\Security\Hmac
201
	 */
202 59
	public function getHmac($data, $algo = 'sha256', $key = '') {
203 59
		if (!$key) {
204 52
			$key = $this->site_secret->get(true);
205 52
		}
206 59
		return new Elgg\Security\Hmac($key, [$this, 'areEqual'], $data, $algo);
207
	}
208
209
	/**
210
	 * Generate a random string of specified length.
211
	 *
212
	 * Uses supplied character list for generating the new string.
213
	 * If no character list provided - uses Base64 URL character set.
214
	 *
215
	 * @param int         $length Desired length of the string
216
	 * @param string|null $chars  Characters to be chosen from randomly. If not given, the Base64 URL
217
	 *                            charset will be used.
218
	 *
219
	 * @return string The random string
220
	 *
221
	 * @throws InvalidArgumentException
222
	 *
223
	 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
224
	 * @license   http://framework.zend.com/license/new-bsd New BSD License
225
	 *
226
	 * @see https://github.com/zendframework/zf2/blob/master/library/Zend/Math/Rand.php#L179
227
	 */
228 341
	public function getRandomString($length, $chars = null) {
229 341
		if ($length < 1) {
230
			throw new \InvalidArgumentException('Length should be >= 1');
231
		}
232
233 341
		if (empty($chars)) {
234 338
			$numBytes = ceil($length * 0.75);
235 338
			$bytes    = $this->getRandomBytes($numBytes);
236 338
			$string = substr(rtrim(base64_encode($bytes), '='), 0, $length);
237
238
			// Base64 URL
239 338
			return strtr($string, '+/', '-_');
240
		}
241
242 3
		if ($chars == self::CHARS_HEX) {
243
			// hex is easy
244 1
			$bytes = $this->getRandomBytes(ceil($length / 2));
245 1
			return substr(bin2hex($bytes), 0, $length);
246
		}
247
248 2
		$listLen = strlen($chars);
249
250 2
		if ($listLen == 1) {
251
			return str_repeat($chars, $length);
252
		}
253
254 2
		$bytes  = $this->getRandomBytes($length);
255 2
		$pos    = 0;
256 2
		$result = '';
257 2
		for ($i = 0; $i < $length; $i++) {
258 2
			$pos     = ($pos + ord($bytes[$i])) % $listLen;
259 2
			$result .= $chars[$pos];
260 2
		}
261
262 2
		return $result;
263
	}
264
265
	/**
266
	 * Are two strings equal (compared in constant time)?
267
	 *
268
	 * @param string $str1 First string to compare
269
	 * @param string $str2 Second string to compare
270
	 *
271
	 * @return bool
272
	 *
273
	 * Based on password_verify in PasswordCompat
274
	 * @author Anthony Ferrara <[email protected]>
275
	 * @license http://www.opensource.org/licenses/mit-license.html MIT License
276
	 * @copyright 2012 The Authors
277
	 */
278 50
	public function areEqual($str1, $str2) {
279 50
		$len1 = $this->strlen($str1);
280 50
		$len2 = $this->strlen($str2);
281 50
		if ($len1 !== $len2) {
282 2
			return false;
283
		}
284
285 49
		$status = 0;
286 49
		for ($i = 0; $i < $len1; $i++) {
287 49
			$status |= (ord($str1[$i]) ^ ord($str2[$i]));
288 49
		}
289
290 49
		return $status === 0;
291
	}
292
293
	/**
294
	 * Count the number of bytes in a string
295
	 *
296
	 * We cannot simply use strlen() for this, because it might be overwritten by the mbstring extension.
297
	 * In this case, strlen() will count the number of *characters* based on the internal encoding. A
298
	 * sequence of bytes might be regarded as a single multibyte character.
299
	 *
300
	 * Use elgg_strlen() to count UTF-characters instead of bytes.
301
	 *
302
	 * @param string $binary_string The input string
303
	 *
304
	 * @return int The number of bytes
305
	 *
306
	 * From PasswordCompat\binary\_strlen
307
	 * @author Anthony Ferrara <[email protected]>
308
	 * @license http://www.opensource.org/licenses/mit-license.html MIT License
309
	 * @copyright 2012 The Authors
310
	 */
311 50
	protected function strlen($binary_string) {
312 50
		if (function_exists('mb_strlen')) {
313 50
			return mb_strlen($binary_string, '8bit');
314
		}
315
		return strlen($binary_string);
316
	}
317
}
318