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 |
/** |
19 | 3 |
* Random_* Compatibility Library |
7
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 |
19 | 5 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* The MIT License (MIT) |
9 | 7 |
* |
19 | 8 |
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises |
9 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
* 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
|
11 |
* 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
|
12 |
* in the Software without restriction, including without limitation the rights |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
* 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
|
14 |
* 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
|
15 |
* furnished to do so, subject to the following conditions: |
19 | 16 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
* 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
|
18 |
* all copies or substantial portions of the Software. |
19 | 19 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
* 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
|
21 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
* 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
|
23 |
* 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
|
24 |
* 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
|
25 |
* 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
|
26 |
* SOFTWARE. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
if (!defined('RANDOM_COMPAT_READ_BUFFER')) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
define('RANDOM_COMPAT_READ_BUFFER', 8); |
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 |
|
9 | 33 |
if (!is_callable('random_bytes')) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
/** |
9 | 35 |
* Unless open_basedir is enabled, use /dev/urandom for |
36 |
* random numbers in accordance with best practices |
|
37 |
* |
|
38 |
* Why we use /dev/urandom and not /dev/random |
|
19 | 39 |
* @ref https://www.2uo.de/myths-about-urandom |
9 | 40 |
* @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers |
41 |
* |
|
42 |
* @param int $bytes |
|
43 |
* |
|
44 |
* @throws Exception |
|
45 |
* |
|
46 |
* @return string |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
*/ |
9 | 48 |
function random_bytes($bytes) |
49 |
{ |
|
19 | 50 |
/** @var resource $fp */ |
9 | 51 |
static $fp = null; |
19 | 52 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
/** |
9 | 54 |
* This block should only be run once |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
*/ |
9 | 56 |
if (empty($fp)) { |
57 |
/** |
|
19 | 58 |
* We don't want to ever read C:\dev\random, only /dev/urandom on |
59 |
* Unix-like operating systems. While we guard against this |
|
60 |
* condition in random.php, it doesn't hurt to be defensive in depth |
|
61 |
* here. |
|
62 |
* |
|
63 |
* To that end, we only try to open /dev/urandom if we're on a Unix- |
|
64 |
* like operating system (which means the directory separator is set |
|
65 |
* to "/" not "\". |
|
9 | 66 |
*/ |
19 | 67 |
if (DIRECTORY_SEPARATOR === '/') { |
68 |
if (!is_readable('/dev/urandom')) { |
|
69 |
throw new Exception( |
|
70 |
'Environment misconfiguration: ' . |
|
71 |
'/dev/urandom cannot be read.' |
|
72 |
); |
|
73 |
} |
|
74 |
/** |
|
75 |
* We use /dev/urandom if it is a char device. |
|
76 |
* We never fall back to /dev/random |
|
77 |
*/ |
|
78 |
/** @var resource|bool $fp */ |
|
79 |
$fp = fopen('/dev/urandom', 'rb'); |
|
80 |
if (is_resource($fp)) { |
|
81 |
/** @var array<string, int> $st */ |
|
82 |
$st = fstat($fp); |
|
83 |
if (($st['mode'] & 0170000) !== 020000) { |
|
84 |
fclose($fp); |
|
85 |
$fp = false; |
|
86 |
} |
|
9 | 87 |
} |
88 |
} |
|
89 |
||
19 | 90 |
if (is_resource($fp)) { |
9 | 91 |
/** |
92 |
* stream_set_read_buffer() does not exist in HHVM |
|
93 |
* |
|
94 |
* If we don't set the stream's read buffer to 0, PHP will |
|
95 |
* internally buffer 8192 bytes, which can waste entropy |
|
96 |
* |
|
97 |
* stream_set_read_buffer returns 0 on success |
|
98 |
*/ |
|
99 |
if (is_callable('stream_set_read_buffer')) { |
|
100 |
stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
101 |
} |
|
102 |
if (is_callable('stream_set_chunk_size')) { |
|
103 |
stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
104 |
} |
|
7
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 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
|
9 | 108 |
try { |
19 | 109 |
/** @var int $bytes */ |
9 | 110 |
$bytes = RandomCompat_intval($bytes); |
111 |
} catch (TypeError $ex) { |
|
112 |
throw new TypeError( |
|
113 |
'random_bytes(): $bytes must be an integer' |
|
114 |
); |
|
115 |
} |
|
116 |
||
117 |
if ($bytes < 1) { |
|
118 |
throw new Error( |
|
119 |
'Length must be greater than 0' |
|
120 |
); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* This if() block only runs if we managed to open a file handle |
|
125 |
* |
|
126 |
* It does not belong in an else {} block, because the above |
|
127 |
* if (empty($fp)) line is logic that should only be run once per |
|
128 |
* page load. |
|
129 |
*/ |
|
19 | 130 |
if (is_resource($fp)) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
131 |
/** |
9 | 132 |
* @var int |
133 |
*/ |
|
134 |
$remaining = $bytes; |
|
135 |
||
136 |
/** |
|
137 |
* @var string|bool |
|
138 |
*/ |
|
139 |
$buf = ''; |
|
140 |
||
141 |
/** |
|
142 |
* We use fread() in a loop to protect against partial reads |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
*/ |
9 | 144 |
do { |
145 |
/** |
|
146 |
* @var string|bool |
|
147 |
*/ |
|
148 |
$read = fread($fp, $remaining); |
|
149 |
if (!is_string($read)) { |
|
19 | 150 |
/** |
151 |
* We cannot safely read from the file. Exit the |
|
152 |
* do-while loop and trigger the exception condition |
|
153 |
* |
|
154 |
* @var string|bool |
|
155 |
*/ |
|
156 |
$buf = false; |
|
157 |
break; |
|
9 | 158 |
} |
159 |
/** |
|
160 |
* Decrease the number of bytes returned from remaining |
|
161 |
*/ |
|
162 |
$remaining -= RandomCompat_strlen($read); |
|
163 |
/** |
|
19 | 164 |
* @var string $buf |
9 | 165 |
*/ |
19 | 166 |
$buf .= $read; |
9 | 167 |
} while ($remaining > 0); |
168 |
||
169 |
/** |
|
170 |
* Is our result valid? |
|
19 | 171 |
* @var string|bool $buf |
9 | 172 |
*/ |
173 |
if (is_string($buf)) { |
|
174 |
if (RandomCompat_strlen($buf) === $bytes) { |
|
175 |
/** |
|
176 |
* Return our random entropy buffer here: |
|
177 |
*/ |
|
178 |
return $buf; |
|
179 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
180 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
181 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
182 |
|
9 | 183 |
/** |
184 |
* If we reach here, PHP has failed us. |
|
185 |
*/ |
|
186 |
throw new Exception( |
|
187 |
'Error reading from source device' |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
188 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
189 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
190 |
} |