author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 9 | 177826044cd9 |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* Random_* Compatibility Library |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* for using the new PHP 7 random_* API in PHP 5 projects |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* |
19 | 6 |
* @version 2.0.17 |
7 |
* @released 2018-07-04 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
* The MIT License (MIT) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
* |
19 | 11 |
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
* of this software and associated documentation files (the "Software"), to deal |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
* in the Software without restriction, including without limitation the rights |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
* copies of the Software, and to permit persons to whom the Software is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
* furnished to do so, subject to the following conditions: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
* The above copyright notice and this permission notice shall be included in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
* all copies or substantial portions of the Software. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
* SOFTWARE. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
if (!defined('PHP_VERSION_ID')) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
// This constant was introduced in PHP 5.2.7 |
9 | 34 |
$RandomCompatversion = array_map('intval', explode('.', PHP_VERSION)); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
define( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
'PHP_VERSION_ID', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
$RandomCompatversion[0] * 10000 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
+ $RandomCompatversion[1] * 100 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
+ $RandomCompatversion[2] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
$RandomCompatversion = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
|
9 | 44 |
/** |
45 |
* PHP 7.0.0 and newer have these functions natively. |
|
46 |
*/ |
|
47 |
if (PHP_VERSION_ID >= 70000) { |
|
48 |
return; |
|
49 |
} |
|
50 |
||
51 |
if (!defined('RANDOM_COMPAT_READ_BUFFER')) { |
|
52 |
define('RANDOM_COMPAT_READ_BUFFER', 8); |
|
53 |
} |
|
54 |
||
55 |
$RandomCompatDIR = dirname(__FILE__); |
|
56 |
||
19 | 57 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'byte_safe_strings.php'; |
58 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'cast_to_int.php'; |
|
59 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'error_polyfill.php'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
|
9 | 61 |
if (!is_callable('random_bytes')) { |
62 |
/** |
|
63 |
* PHP 5.2.0 - 5.6.x way to implement random_bytes() |
|
64 |
* |
|
65 |
* We use conditional statements here to define the function in accordance |
|
66 |
* to the operating environment. It's a micro-optimization. |
|
67 |
* |
|
68 |
* In order of preference: |
|
69 |
* 1. Use libsodium if available. |
|
70 |
* 2. fread() /dev/urandom if available (never on Windows) |
|
71 |
* 3. mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM) |
|
72 |
* 4. COM('CAPICOM.Utilities.1')->GetRandom() |
|
73 |
* |
|
74 |
* See RATIONALE.md for our reasoning behind this particular order |
|
75 |
*/ |
|
76 |
if (extension_loaded('libsodium')) { |
|
77 |
// See random_bytes_libsodium.php |
|
78 |
if (PHP_VERSION_ID >= 50300 && is_callable('\\Sodium\\randombytes_buf')) { |
|
19 | 79 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_bytes_libsodium.php'; |
9 | 80 |
} elseif (method_exists('Sodium', 'randombytes_buf')) { |
19 | 81 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_bytes_libsodium_legacy.php'; |
9 | 82 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
|
9 | 85 |
/** |
86 |
* Reading directly from /dev/urandom: |
|
87 |
*/ |
|
88 |
if (DIRECTORY_SEPARATOR === '/') { |
|
89 |
// DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast |
|
90 |
// way to exclude Windows. |
|
91 |
$RandomCompatUrandom = true; |
|
92 |
$RandomCompat_basedir = ini_get('open_basedir'); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
|
9 | 94 |
if (!empty($RandomCompat_basedir)) { |
95 |
$RandomCompat_open_basedir = explode( |
|
96 |
PATH_SEPARATOR, |
|
97 |
strtolower($RandomCompat_basedir) |
|
98 |
); |
|
99 |
$RandomCompatUrandom = (array() !== array_intersect( |
|
100 |
array('/dev', '/dev/', '/dev/urandom'), |
|
101 |
$RandomCompat_open_basedir |
|
102 |
)); |
|
103 |
$RandomCompat_open_basedir = null; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
if ( |
9 | 107 |
!is_callable('random_bytes') |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
&& |
9 | 109 |
$RandomCompatUrandom |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
&& |
9 | 111 |
@is_readable('/dev/urandom') |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
) { |
9 | 113 |
// Error suppression on is_readable() in case of an open_basedir |
114 |
// or safe_mode failure. All we care about is whether or not we |
|
115 |
// can read it at this point. If the PHP environment is going to |
|
116 |
// panic over trying to see if the file can be read in the first |
|
117 |
// place, that is not helpful to us here. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
|
9 | 119 |
// See random_bytes_dev_urandom.php |
19 | 120 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_bytes_dev_urandom.php'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
} |
9 | 122 |
// Unset variables after use |
123 |
$RandomCompat_basedir = null; |
|
124 |
} else { |
|
125 |
$RandomCompatUrandom = false; |
|
126 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
127 |
|
9 | 128 |
/** |
129 |
* mcrypt_create_iv() |
|
130 |
* |
|
131 |
* We only want to use mcypt_create_iv() if: |
|
132 |
* |
|
133 |
* - random_bytes() hasn't already been defined |
|
134 |
* - the mcrypt extensions is loaded |
|
135 |
* - One of these two conditions is true: |
|
136 |
* - We're on Windows (DIRECTORY_SEPARATOR !== '/') |
|
137 |
* - We're not on Windows and /dev/urandom is readabale |
|
138 |
* (i.e. we're not in a chroot jail) |
|
139 |
* - Special case: |
|
140 |
* - If we're not on Windows, but the PHP version is between |
|
141 |
* 5.6.10 and 5.6.12, we don't want to use mcrypt. It will |
|
142 |
* hang indefinitely. This is bad. |
|
143 |
* - If we're on Windows, we want to use PHP >= 5.3.7 or else |
|
144 |
* we get insufficient entropy errors. |
|
145 |
*/ |
|
146 |
if ( |
|
147 |
!is_callable('random_bytes') |
|
148 |
&& |
|
149 |
// Windows on PHP < 5.3.7 is broken, but non-Windows is not known to be. |
|
150 |
(DIRECTORY_SEPARATOR === '/' || PHP_VERSION_ID >= 50307) |
|
151 |
&& |
|
152 |
// Prevent this code from hanging indefinitely on non-Windows; |
|
153 |
// see https://bugs.php.net/bug.php?id=69833 |
|
154 |
( |
|
155 |
DIRECTORY_SEPARATOR !== '/' || |
|
156 |
(PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613) |
|
157 |
) |
|
158 |
&& |
|
159 |
extension_loaded('mcrypt') |
|
160 |
) { |
|
161 |
// See random_bytes_mcrypt.php |
|
19 | 162 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_bytes_mcrypt.php'; |
9 | 163 |
} |
164 |
$RandomCompatUrandom = null; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
165 |
|
9 | 166 |
/** |
167 |
* This is a Windows-specific fallback, for when the mcrypt extension |
|
168 |
* isn't loaded. |
|
169 |
*/ |
|
170 |
if ( |
|
171 |
!is_callable('random_bytes') |
|
172 |
&& |
|
173 |
extension_loaded('com_dotnet') |
|
174 |
&& |
|
175 |
class_exists('COM') |
|
176 |
) { |
|
177 |
$RandomCompat_disabled_classes = preg_split( |
|
178 |
'#\s*,\s*#', |
|
179 |
strtolower(ini_get('disable_classes')) |
|
180 |
); |
|
181 |
||
182 |
if (!in_array('com', $RandomCompat_disabled_classes)) { |
|
183 |
try { |
|
184 |
$RandomCompatCOMtest = new COM('CAPICOM.Utilities.1'); |
|
19 | 185 |
/** @psalm-suppress TypeDoesNotContainType */ |
186 |
if (is_callable(array($RandomCompatCOMtest, 'GetRandom'))) { |
|
9 | 187 |
// See random_bytes_com_dotnet.php |
19 | 188 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_bytes_com_dotnet.php'; |
9 | 189 |
} |
190 |
} catch (com_exception $e) { |
|
191 |
// Don't try to use it. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
193 |
} |
9 | 194 |
$RandomCompat_disabled_classes = null; |
195 |
$RandomCompatCOMtest = null; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
196 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
197 |
|
9 | 198 |
/** |
199 |
* throw new Exception |
|
200 |
*/ |
|
201 |
if (!is_callable('random_bytes')) { |
|
202 |
/** |
|
203 |
* We don't have any more options, so let's throw an exception right now |
|
204 |
* and hope the developer won't let it fail silently. |
|
205 |
* |
|
206 |
* @param mixed $length |
|
19 | 207 |
* @psalm-suppress InvalidReturnType |
9 | 208 |
* @throws Exception |
19 | 209 |
* @return string |
9 | 210 |
*/ |
211 |
function random_bytes($length) |
|
212 |
{ |
|
213 |
unset($length); // Suppress "variable not used" warnings. |
|
214 |
throw new Exception( |
|
215 |
'There is no suitable CSPRNG installed on your system' |
|
216 |
); |
|
19 | 217 |
return ''; |
9 | 218 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
219 |
} |
9 | 220 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
221 |
|
9 | 222 |
if (!is_callable('random_int')) { |
19 | 223 |
require_once $RandomCompatDIR.DIRECTORY_SEPARATOR.'random_int.php'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
224 |
} |
9 | 225 |
|
226 |
$RandomCompatDIR = null; |