KD2 Framework  Check-in [c3097ce38c]

Overview
Comment:Remove shims for PHP 5
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: c3097ce38c5d4febd98871be7d438d8113c6936e
User & Date: bohwaz on 2020-11-10 17:50:51
Other Links: branch diff | manifest | tags
Context
2020-11-11
12:45
Entity: Make sure an ID is set check-in: 839a28e75c user: bohwaz tags: 7.3
2020-11-10
17:50
Remove shims for PHP 5 check-in: c3097ce38c user: bohwaz tags: 7.3
03:48
Pie: Fix vertical alignment check-in: 608abf1de7 user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/Form.php from [bba35f434e] to [6e806c4799].

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
		if (is_null(self::$token_secret))
		{
			throw new \RuntimeException('No CSRF token secret has been set.');
		}

		$action = self::tokenAction($action);

		$random = Security::random_int();
		$expire = floor(time() / 3600) + $expire;
		$value = $expire . $random . $action;

		$hash = hash_hmac('sha256', $expire . $random . $action, self::$token_secret);

		return $hash . '/' . dechex($expire) . '/' . dechex($random);
	}







|







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
		if (is_null(self::$token_secret))
		{
			throw new \RuntimeException('No CSRF token secret has been set.');
		}

		$action = self::tokenAction($action);

		$random = random_int(0, PHP_INT_MAX);
		$expire = floor(time() / 3600) + $expire;
		$value = $expire . $random . $action;

		$hash = hash_hmac('sha256', $expire . $random . $action, self::$token_secret);

		return $hash . '/' . dechex($expire) . '/' . dechex($random);
	}

Modified src/lib/KD2/Security.php from [cb97126fca] to [c499e0aa87].

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
		}

		$ret = strlen($known_string) ^ strlen($user_string);
		$ret |= array_sum(unpack("C*", $known_string^$user_string));
		return !$ret;
	}

	/**
	 * Generates a random number between $min and $max
	 *
	 * The number will be crypto secure unless you set $insecure_fallback to TRUE,
	 * then it can provide a insecure number if no crypto source is available.
	 *
	 * @link https://codeascraft.com/2012/07/19/better-random-numbers-in-php-using-devurandom/
	 * @param  integer $min               Minimum number
	 * @param  integer $max               Maximum number
	 * @param  boolean $insecure_fallback Set to true to fallback to mt_rand()
	 * @return integer                    A random number
	 * @throws Exception If no secure random source is found and $insecure_fallback is set to false
	 */
	static public function random_int($min = 0, $max = PHP_INT_MAX, $insecure_fallback = false)
	{
		// Only one possible value, not random
		if ($max == $min)
		{
			return $min;
		}

		if ($min > $max)
		{
			throw new \Exception('Minimum value must be less than or equal to to the maximum value');
		}

		// Use the native PHP function for PHP 7+
		if (function_exists('random_int'))
		{
			return random_int($min, $max);
		}

		try {
			// Get some random bytes
			$bytes = self::random_bytes(PHP_INT_SIZE);
		}
		catch (\Exception $e)
		{
			// No crypto random found

			// For trivial stuff we can just use mt_rand() instead
			if ($insecure_fallback)
			{
				return mt_rand($min, min($max, mt_getrandmax()));
			}

			// But for crypto stuff you should expect this to fail
			throw $e;
		}

		// 64-bits
		if (PHP_INT_SIZE == 8)
		{
			list($higher, $lower) = array_values(unpack('N2', $bytes));
			$value = $higher << 32 | $lower;
		}
		// 32 bits
		else
		{
			list($value) = array_values(unpack('Nint', $bytes));
		}

		$value = $value & PHP_INT_MAX;
		$value = (float) $value / PHP_INT_MAX; // convert to [0,1]
		return (int) (round($value * ($max - $min)) + $min);
	}

	/**
	 * Returns a specified number of cryptographically secure random bytes
	 * @param  integer $length Number of bytes to return
	 * @return string Random bytes
	 * @throws Exception If an appropriate source of randomness cannot be found, an Exception will be thrown.
	 */
	static public function random_bytes($length)
	{
		$length = (int) $length;

		if (function_exists('random_bytes'))
		{
			return random_bytes($length);
		}

		if (function_exists('mcrypt_create_iv'))
		{
			return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
		} 

		if (file_exists('/dev/urandom') && is_readable('/dev/urandom'))
		{
			return file_get_contents('/dev/urandom', false, null, 0, $length);
		}

		if (function_exists('openssl_random_pseudo_bytes'))
		{
			return openssl_random_pseudo_bytes($length);
		}

		throw new \Exception('An appropriate source of randomness cannot be found.');
	}

	/**
	 * Returns a random password of $length characters, picked from $alphabet
	 * @param  integer $length  Length of password
	 * @param  string $alphabet Alphabet used for password generation
	 * @return string
	 */
	static public function getRandomPassword($length = 12, $alphabet = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789=/:!?-_')
	{
		$password = '';

		for ($i = 0; $i < (int)$length; $i++)
		{
			$pos = self::random_int(0, strlen($alphabet) - 1);
			$password .= $alphabet[$pos];
		}

		return $password;
	}

	/**







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<












|







69
70
71
72
73
74
75




































































































76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		}

		$ret = strlen($known_string) ^ strlen($user_string);
		$ret |= array_sum(unpack("C*", $known_string^$user_string));
		return !$ret;
	}





































































































	/**
	 * Returns a random password of $length characters, picked from $alphabet
	 * @param  integer $length  Length of password
	 * @param  string $alphabet Alphabet used for password generation
	 * @return string
	 */
	static public function getRandomPassword($length = 12, $alphabet = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789=/:!?-_')
	{
		$password = '';

		for ($i = 0; $i < (int)$length; $i++)
		{
			$pos = random_int(0, strlen($alphabet) - 1);
			$password .= $alphabet[$pos];
		}

		return $password;
	}

	/**
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
		while (count($selection) < (int) $words)
		{
			if ($i++ > $max)
			{
				throw new \Exception('Could not find a suitable combination of words.');
			}

			$rand = self::random_int(0, count($file) - 1);
			$w = trim($file[$rand]);

			if (!$character_match || preg_match('/^[' . $character_match . ']+$/U', $w))
			{
				if ($add_entropy)
				{
					$w[self::random_int(0, strlen($w) - 1)] = self::getRandomPassword(1, '23456789=/:!?-._');
				}

				$selection[] = $w;
			}
		}

		return implode(' ', $selection);







|






|







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		while (count($selection) < (int) $words)
		{
			if ($i++ > $max)
			{
				throw new \Exception('Could not find a suitable combination of words.');
			}

			$rand = random_int(0, count($file) - 1);
			$w = trim($file[$rand]);

			if (!$character_match || preg_match('/^[' . $character_match . ']+$/U', $w))
			{
				if ($add_entropy)
				{
					$w[random_int(0, strlen($w) - 1)] = self::getRandomPassword(1, '23456789=/:!?-._');
				}

				$selection[] = $w;
			}
		}

		return implode(' ', $selection);

Modified src/lib/KD2/Security_OTP.php from [727872fb5b] to [9c550d108c].

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	static public function getRandomSecret($length = 16)
	{
		$keys = array_merge(range('A', 'Z'), range(2, 7));
		$string = '';

		for ($i = 0; $i < $length; $i++)
		{
			// Try PHP7 random_int (or poyfill, see https://github.com/paragonie/random_compat)
			// or fallback to mt_rand() not secure but will work
			$rand = function_exists('random_int') ? random_int(0, 31) : mt_rand(0, 31);
			$string .= $keys[$rand];
		}

		return $string;
	}

	/**







<
<
|







186
187
188
189
190
191
192


193
194
195
196
197
198
199
200
	static public function getRandomSecret($length = 16)
	{
		$keys = array_merge(range('A', 'Z'), range(2, 7));
		$string = '';

		for ($i = 0; $i < $length; $i++)
		{


			$rand = random_int(0, 31);
			$string .= $keys[$rand];
		}

		return $string;
	}

	/**

Modified src/lib/KD2/UserSession.php from [2904203218] to [9b8b5bed5c].

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
	 * @link   https://paragonie.com/blog/2017/02/split-tokens-token-based-authentication-protocols-without-side-channels
	 * @link   http://jaspan.com/improved_persistent_login_cookie_best_practice
	 * @param  object $user
	 * @return boolean
	 */
	protected function createRememberMeSelector($user_id, $user_password)
	{
		$selector = hash($this::HASH_ALGO, Security::random_bytes(10));
		$verifier = hash($this::HASH_ALGO, Security::random_bytes(10));
		$expiry = (new \DateTime)->modify($this->remember_me_expiry);
		$expiry = $expiry->getTimestamp();

		$hash = hash($this::HASH_ALGO, $selector . $verifier . $user_password . $expiry);

		$this->storeRememberMeSelector($selector, $hash, $expiry, $user_id);








|
|







495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
	 * @link   https://paragonie.com/blog/2017/02/split-tokens-token-based-authentication-protocols-without-side-channels
	 * @link   http://jaspan.com/improved_persistent_login_cookie_best_practice
	 * @param  object $user
	 * @return boolean
	 */
	protected function createRememberMeSelector($user_id, $user_password)
	{
		$selector = hash($this::HASH_ALGO, random_bytes(10));
		$verifier = hash($this::HASH_ALGO, random_bytes(10));
		$expiry = (new \DateTime)->modify($this->remember_me_expiry);
		$expiry = $expiry->getTimestamp();

		$hash = hash($this::HASH_ALGO, $selector . $verifier . $user_password . $expiry);

		$this->storeRememberMeSelector($selector, $hash, $expiry, $user_id);