Total Complexity | 117 |
Total Lines | 876 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Crypt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Crypt, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Crypt |
||
32 | { |
||
33 | /** |
||
34 | * mbstring.func_override flag |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected static $isFunctionOverride; |
||
39 | /** |
||
40 | * Encryption cipher |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $cipher = 'aes-128'; |
||
45 | /** |
||
46 | * Cipher mode |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $mode = 'cbc'; |
||
51 | /** |
||
52 | * Cipher handle |
||
53 | * |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $handle; |
||
57 | /** |
||
58 | * Encryption key |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $key; |
||
63 | /** |
||
64 | * PHP extension to be used |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $driver; |
||
69 | /** |
||
70 | * List of usable drivers (PHP extensions) |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $drivers = []; |
||
75 | /** |
||
76 | * List of available modes |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $modes |
||
81 | = [ |
||
82 | 'mcrypt' => [ |
||
83 | 'cbc' => 'cbc', |
||
84 | 'ecb' => 'ecb', |
||
85 | 'ofb' => 'nofb', |
||
86 | 'ofb8' => 'ofb', |
||
87 | 'cfb' => 'ncfb', |
||
88 | 'cfb8' => 'cfb', |
||
89 | 'ctr' => 'ctr', |
||
90 | 'stream' => 'stream', |
||
91 | ], |
||
92 | 'openssl' => [ |
||
93 | 'cbc' => 'cbc', |
||
94 | 'ecb' => 'ecb', |
||
95 | 'ofb' => 'ofb', |
||
96 | 'cfb' => 'cfb', |
||
97 | 'cfb8' => 'cfb8', |
||
98 | 'ctr' => 'ctr', |
||
99 | 'stream' => '', |
||
100 | 'xts' => 'xts', |
||
101 | ], |
||
102 | ]; |
||
103 | /** |
||
104 | * List of supported HMAC algorithms |
||
105 | * |
||
106 | * name => digest size pairs |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $digests |
||
111 | = [ |
||
112 | 'sha224' => 28, |
||
113 | 'sha256' => 32, |
||
114 | 'sha384' => 48, |
||
115 | 'sha512' => 64, |
||
116 | ]; |
||
117 | |||
118 | // -------------------------------------------------------------------- |
||
119 | |||
120 | /** |
||
121 | * Crypt::__construct |
||
122 | * |
||
123 | * @param array $params |
||
124 | * |
||
125 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
126 | */ |
||
127 | public function __construct(array $params = []) |
||
128 | { |
||
129 | $this->drivers = [ |
||
130 | 'mcrypt' => defined('MCRYPT_DEV_URANDOM'), |
||
131 | 'openssl' => extension_loaded('openssl'), |
||
132 | ]; |
||
133 | |||
134 | if ( ! $this->drivers[ 'mcrypt' ] && ! $this->drivers[ 'openssl' ]) { |
||
135 | //Encryption: Unable to find an available encryption driver. |
||
136 | throw new BadPhpExtensionCallException('E_SECURITY_CRYPT_UNABLE_TO_FIND_DRIVER'); |
||
137 | } |
||
138 | |||
139 | isset(self::$isFunctionOverride) OR |
||
140 | self::$isFunctionOverride = (extension_loaded('mbstring') && ini_get('mbstring.func_override')); |
||
141 | $this->initialize($params); |
||
142 | |||
143 | $key = get_called_class(); |
||
144 | |||
145 | if (class_exists('\O2System\Framework', false) or class_exists('\O2System\Reactor', false)) { |
||
146 | $key = config()->getItem('security')->offsetGet('encryptionKey'); |
||
|
|||
147 | } |
||
148 | |||
149 | if ( ! isset($this->key) && self::strlen($key) > 0) { |
||
150 | $this->key = $key; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | // -------------------------------------------------------------------- |
||
155 | |||
156 | /** |
||
157 | * Initialize |
||
158 | * |
||
159 | * @param array $params Configuration parameters |
||
160 | * |
||
161 | * @return static |
||
162 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
163 | */ |
||
164 | public function initialize(array $params) |
||
165 | { |
||
166 | if ( ! empty($params[ 'driver' ])) { |
||
167 | if (isset($this->drivers[ $params[ 'driver' ] ])) { |
||
168 | if ($this->drivers[ $params[ 'driver' ] ]) { |
||
169 | $this->driver = $params[ 'driver' ]; |
||
170 | } else { |
||
171 | //"Encryption: Driver '" . $params[ 'driver' ] . "' is not available." |
||
172 | throw new BadPhpExtensionCallException( |
||
173 | 'E_SECURITY_CRYPT_DRIVER_NOT_AVAILABLE', $params[ 'driver' ] |
||
174 | ); |
||
175 | } |
||
176 | } else { |
||
177 | // "Encryption: Unknown driver '" . $params[ 'driver' ] . "' cannot be configured." |
||
178 | throw new BadPhpExtensionCallException( |
||
179 | 'E_SECURITY_CRYPT_DRIVER_NOT_CONFIGURED', |
||
180 | 0, |
||
181 | [$params[ 'driver' ]] |
||
182 | ); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if (empty($this->driver)) { |
||
187 | $this->driver = ($this->drivers[ 'openssl' ] === true) |
||
188 | ? 'openssl' |
||
189 | : 'mcrypt'; |
||
190 | } |
||
191 | |||
192 | empty($params[ 'cipher' ]) && $params[ 'cipher' ] = $this->cipher; |
||
193 | empty($params[ 'key' ]) OR $this->key = $params[ 'key' ]; |
||
194 | $this->{$this->driver . 'Initialize'}($params); |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | // -------------------------------------------------------------------- |
||
200 | |||
201 | /** |
||
202 | * Byte-safe strlen() |
||
203 | * |
||
204 | * @param string $str |
||
205 | * |
||
206 | * @return int |
||
207 | */ |
||
208 | protected static function strlen($str) |
||
209 | { |
||
210 | return (self::$isFunctionOverride) |
||
211 | ? mb_strlen($str, '8bit') |
||
212 | : strlen($str); |
||
213 | } |
||
214 | |||
215 | // -------------------------------------------------------------------- |
||
216 | |||
217 | public function setKey($key) |
||
218 | { |
||
219 | $this->key = $key; |
||
220 | |||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | // -------------------------------------------------------------------- |
||
225 | |||
226 | /** |
||
227 | * Encrypt |
||
228 | * |
||
229 | * @param string $data Input data |
||
230 | * @param array $params Input parameters |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public function encrypt($data, array $params = null) |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get params |
||
261 | * |
||
262 | * @param array $params Input parameters |
||
263 | * |
||
264 | * @return array|bool |
||
265 | */ |
||
266 | protected function getParams($params) |
||
267 | { |
||
268 | if (empty($params)) { |
||
269 | return isset($this->cipher, $this->mode, $this->key, $this->handle) |
||
270 | ? [ |
||
271 | 'handle' => $this->handle, |
||
272 | 'cipher' => $this->cipher, |
||
273 | 'mode' => $this->mode, |
||
274 | 'key' => null, |
||
275 | 'base64' => true, |
||
276 | 'hmac_digest' => 'sha512', |
||
277 | 'hmac_key' => null, |
||
278 | ] |
||
279 | : false; |
||
280 | } elseif ( ! isset($params[ 'cipher' ], $params[ 'mode' ], $params[ 'key' ])) { |
||
281 | return false; |
||
282 | } |
||
283 | |||
284 | if (isset($params[ 'mode' ])) { |
||
285 | $params[ 'mode' ] = strtolower($params[ 'mode' ]); |
||
286 | if ( ! isset($this->modes[ $this->driver ][ $params[ 'mode' ] ])) { |
||
287 | return false; |
||
288 | } else { |
||
289 | $params[ 'mode' ] = $this->modes[ $this->driver ][ $params[ 'mode' ] ]; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | if (isset($params[ 'hmac' ]) && $params[ 'hmac' ] === false) { |
||
294 | $params[ 'hmac_digest' ] = $params[ 'hmac_key' ] = null; |
||
295 | } else { |
||
296 | if ( ! isset($params[ 'hmac_key' ])) { |
||
297 | return false; |
||
298 | } elseif (isset($params[ 'hmac_digest' ])) { |
||
299 | $params[ 'hmac_digest' ] = strtolower($params[ 'hmac_digest' ]); |
||
300 | if ( ! isset($this->digests[ $params[ 'hmac_digest' ] ])) { |
||
301 | return false; |
||
302 | } |
||
303 | } else { |
||
304 | $params[ 'hmac_digest' ] = 'sha512'; |
||
305 | } |
||
306 | } |
||
307 | |||
308 | $params = [ |
||
309 | 'handle' => null, |
||
310 | 'cipher' => $params[ 'cipher' ], |
||
311 | 'mode' => $params[ 'mode' ], |
||
312 | 'key' => $params[ 'key' ], |
||
313 | 'base64' => isset($params[ 'raw_data' ]) |
||
314 | ? ! $params[ 'raw_data' ] |
||
315 | : false, |
||
316 | 'hmac_digest' => $params[ 'hmac_digest' ], |
||
317 | 'hmac_key' => $params[ 'hmac_key' ], |
||
318 | ]; |
||
319 | |||
320 | $this->cipherAlias($params[ 'cipher' ]); |
||
321 | $params[ 'handle' ] = ($params[ 'cipher' ] !== $this->cipher OR $params[ 'mode' ] !== $this->mode) |
||
322 | ? $this->{$this->driver . 'GetHandle'}($params[ 'cipher' ], $params[ 'mode' ]) |
||
323 | : $this->handle; |
||
324 | |||
325 | return $params; |
||
326 | } |
||
327 | |||
328 | // -------------------------------------------------------------------- |
||
329 | |||
330 | /** |
||
331 | * Cipher alias |
||
332 | * |
||
333 | * Tries to translate cipher names between MCrypt and OpenSSL's "dialects". |
||
334 | * |
||
335 | * @param string $cipher Cipher name |
||
336 | * |
||
337 | * @return void |
||
338 | */ |
||
339 | protected function cipherAlias(&$cipher) |
||
340 | { |
||
341 | static $dictionary; |
||
342 | |||
343 | if (empty($dictionary)) { |
||
344 | $dictionary = [ |
||
345 | 'mcrypt' => [ |
||
346 | 'aes-128' => 'rijndael-128', |
||
347 | 'aes-192' => 'rijndael-128', |
||
348 | 'aes-256' => 'rijndael-128', |
||
349 | 'des3-ede3' => 'tripledes', |
||
350 | 'bf' => 'blowfish', |
||
351 | 'cast5' => 'cast-128', |
||
352 | 'rc4' => 'arcfour', |
||
353 | 'rc4-40' => 'arcfour', |
||
354 | ], |
||
355 | 'openssl' => [ |
||
356 | 'rijndael-128' => 'aes-128', |
||
357 | 'tripledes' => 'des-ede3', |
||
358 | 'blowfish' => 'bf', |
||
359 | 'cast-128' => 'cast5', |
||
360 | 'arcfour' => 'rc4-40', |
||
361 | 'rc4' => 'rc4-40', |
||
362 | ], |
||
363 | ]; |
||
364 | |||
365 | // Notes: |
||
366 | // |
||
367 | // - Rijndael-128 is, at the same time all three of AES-128, |
||
368 | // AES-192 and AES-256. The only difference between them is |
||
369 | // the key size. Rijndael-192, Rijndael-256 on the other hand |
||
370 | // also have different block sizes and are NOT AES-compatible. |
||
371 | // |
||
372 | // - Blowfish is said to be supporting key sizes between |
||
373 | // 4 and 56 bytes, but it appears that between MCrypt and |
||
374 | // OpenSSL, only those of 16 and more bytes are compatible. |
||
375 | // Also, don't know what MCrypt's 'blowfish-compat' is. |
||
376 | // |
||
377 | // - CAST-128/CAST5 produces a longer cipher when encrypted via |
||
378 | // OpenSSL, but (strangely enough) can be decrypted by either |
||
379 | // extension anyway. |
||
380 | // Also, it appears that OpenSSL uses 16 rounds regardless of |
||
381 | // the key size, while RFC2144 says that for key sizes lower |
||
382 | // than 11 bytes, only 12 rounds should be used. This makes |
||
383 | // it portable only with keys of between 11 and 16 bytes. |
||
384 | // |
||
385 | // - RC4 (ARCFour) has a strange implementation under OpenSSL. |
||
386 | // Its 'rc4-40' cipher method seems to work flawlessly, yet |
||
387 | // there's another one, 'rc4' that only works with a 16-byte key. |
||
388 | // |
||
389 | // - DES is compatible, but doesn't need an alias. |
||
390 | // |
||
391 | // Other seemingly matching ciphers between MCrypt, OpenSSL: |
||
392 | // |
||
393 | // - RC2 is NOT compatible and only an obscure forum post |
||
394 | // confirms that it is MCrypt's fault. |
||
395 | } |
||
396 | |||
397 | if (isset($dictionary[ $this->driver ][ $cipher ])) { |
||
398 | $cipher = $dictionary[ $this->driver ][ $cipher ]; |
||
399 | } |
||
400 | } |
||
401 | |||
402 | // -------------------------------------------------------------------- |
||
403 | |||
404 | /** |
||
405 | * HKDF |
||
406 | * |
||
407 | * @link https://tools.ietf.org/rfc/rfc5869.txt |
||
408 | * |
||
409 | * @param $key Input key |
||
410 | * @param $digest A SHA-2 hashing algorithm |
||
411 | * @param $salt Optional salt |
||
412 | * @param $length Output length (defaults to the selected digest size) |
||
413 | * @param $info Optional context/application-specific info |
||
414 | * |
||
415 | * @return string A pseudo-random key |
||
416 | */ |
||
417 | public function hkdf($key, $digest = 'sha512', $salt = null, $length = null, $info = '') |
||
418 | { |
||
419 | if ( ! isset($this->digests[ $digest ])) { |
||
420 | return false; |
||
421 | } |
||
422 | |||
423 | if (empty($length) OR ! is_int($length)) { |
||
424 | $length = $this->digests[ $digest ]; |
||
425 | } elseif ($length > (255 * $this->digests[ $digest ])) { |
||
426 | return false; |
||
427 | } |
||
428 | |||
429 | self::strlen($salt) OR $salt = str_repeat("\0", $this->digests[ $digest ]); |
||
430 | |||
431 | $prk = hash_hmac($digest, $key, $salt, true); |
||
432 | $key = ''; |
||
433 | for ($key_block = '', $block_index = 1; self::strlen($key) < $length; $block_index++) { |
||
434 | $key_block = hash_hmac($digest, $key_block . $info . chr($block_index), $prk, true); |
||
435 | $key .= $key_block; |
||
436 | } |
||
437 | |||
438 | return self::substr($key, 0, $length); |
||
439 | } |
||
440 | |||
441 | // -------------------------------------------------------------------- |
||
442 | |||
443 | /** |
||
444 | * Byte-safe substr() |
||
445 | * |
||
446 | * @param string $str |
||
447 | * @param int $start |
||
448 | * @param int $length |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | protected static function substr($str, $start, $length = null) |
||
453 | { |
||
454 | if (self::$isFunctionOverride) { |
||
455 | // mb_substr($str, $start, null, '8bit') returns an empty |
||
456 | // string on PHP 5.3 |
||
457 | isset($length) OR $length = ($start >= 0 |
||
458 | ? self::strlen($str) - $start |
||
459 | : -$start); |
||
460 | |||
461 | return mb_substr($str, $start, $length, '8bit'); |
||
462 | } |
||
463 | |||
464 | return isset($length) |
||
465 | ? substr($str, $start, $length) |
||
466 | : substr($str, $start); |
||
467 | } |
||
468 | |||
469 | // -------------------------------------------------------------------- |
||
470 | |||
471 | /** |
||
472 | * Decrypt |
||
473 | * |
||
474 | * @param string $data Encrypted data |
||
475 | * @param array $params Input parameters |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | public function decrypt($data, array $params = null) |
||
480 | { |
||
481 | if (($params = $this->getParams($params)) === false) { |
||
482 | return false; |
||
483 | } |
||
484 | |||
485 | if (isset($params[ 'hmac_digest' ])) { |
||
486 | // This might look illogical, but it is done during encryption as well ... |
||
487 | // The 'base64' value is effectively an inverted "raw data" parameter |
||
488 | $digest_size = ($params[ 'base64' ]) |
||
489 | ? $this->digests[ $params[ 'hmac_digest' ] ] * 2 |
||
490 | : $this->digests[ $params[ 'hmac_digest' ] ]; |
||
491 | |||
492 | if (self::strlen($data) <= $digest_size) { |
||
493 | return false; |
||
494 | } |
||
495 | |||
496 | $hmac_input = self::substr($data, 0, $digest_size); |
||
497 | $data = self::substr($data, $digest_size); |
||
498 | |||
499 | isset($params[ 'hmac_key' ]) OR |
||
500 | $params[ 'hmac_key' ] = $this->hkdf($this->key, 'sha512', null, null, 'authentication'); |
||
501 | $hmac_check = hash_hmac($params[ 'hmac_digest' ], $data, $params[ 'hmac_key' ], ! $params[ 'base64' ]); |
||
502 | |||
503 | // Time-attack-safe comparison |
||
504 | $diff = 0; |
||
505 | for ($i = 0; $i < $digest_size; $i++) { |
||
506 | $diff |= ord($hmac_input[ $i ]) ^ ord($hmac_check[ $i ]); |
||
507 | } |
||
508 | |||
509 | if ($diff !== 0) { |
||
510 | return false; |
||
511 | } |
||
512 | } |
||
513 | |||
514 | if ($params[ 'base64' ]) { |
||
515 | $data = base64_decode($data); |
||
516 | } |
||
517 | |||
518 | isset($params[ 'key' ]) OR |
||
519 | $params[ 'key' ] = $this->hkdf($this->key, 'sha512', null, self::strlen($this->key), 'encryption'); |
||
520 | |||
521 | return $this->{$this->driver . 'Decrypt'}($data, $params); |
||
522 | } |
||
523 | |||
524 | // -------------------------------------------------------------------- |
||
525 | |||
526 | /** |
||
527 | * __get() magic |
||
528 | * |
||
529 | * @param string $key Property name |
||
530 | * |
||
531 | * @return mixed |
||
532 | */ |
||
533 | public function __get($key) |
||
534 | { |
||
535 | // Because aliases |
||
536 | if ($key === 'mode') { |
||
537 | return array_search($this->mode, $this->modes[ $this->driver ], true); |
||
538 | } elseif (in_array($key, ['cipher', 'driver', 'drivers', 'digests'], true)) { |
||
539 | return $this->{'_' . $key}; |
||
540 | } |
||
541 | |||
542 | return null; |
||
543 | } |
||
544 | |||
545 | // -------------------------------------------------------------------- |
||
546 | |||
547 | /** |
||
548 | * Initialize MCrypt |
||
549 | * |
||
550 | * @param array $params Configuration parameters |
||
551 | * |
||
552 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCallException |
||
553 | */ |
||
554 | protected function mcryptInitialize($params) |
||
555 | { |
||
556 | if ( ! empty($params[ 'cipher' ])) { |
||
557 | $params[ 'cipher' ] = strtolower($params[ 'cipher' ]); |
||
558 | $this->cipherAlias($params[ 'cipher' ]); |
||
559 | |||
560 | if ( ! in_array($params[ 'cipher' ], mcrypt_list_algorithms(), true)) { |
||
561 | // 'Encryption: MCrypt cipher ' . strtoupper( $params[ 'cipher' ] ) . ' is not available.' |
||
562 | throw new BadFunctionCallException( |
||
563 | 'E_SECURITY_MCRYPT_CIPHER_UNAVAILABLE', |
||
564 | 0, |
||
565 | [strtoupper($params[ 'cipher' ])] |
||
566 | ); |
||
567 | } else { |
||
568 | $this->cipher = $params[ 'cipher' ]; |
||
569 | } |
||
570 | } |
||
571 | |||
572 | if ( ! empty($params[ 'mode' ])) { |
||
573 | $params[ 'mode' ] = strtolower($params[ 'mode' ]); |
||
574 | if ( ! isset($this->modes[ 'mcrypt' ][ $params[ 'mode' ] ])) { |
||
575 | // 'Encryption: MCrypt mode ' . strtoupper( $params[ 'mode' ] ) . ' is not available.' |
||
576 | throw new BadFunctionCallException( |
||
577 | 'E_SECURITY_MCRYPT_MODE_UNAVAILABLE', |
||
578 | 0, |
||
579 | [strtoupper($params[ 'mode' ])] |
||
580 | ); |
||
581 | } else { |
||
582 | $this->mode = $this->modes[ 'mcrypt' ][ $params[ 'mode' ] ]; |
||
583 | } |
||
584 | } |
||
585 | |||
586 | if (isset($this->cipher, $this->mode)) { |
||
587 | if (is_resource($this->handle) |
||
588 | && (strtolower(mcrypt_enc_get_algorithms_name($this->handle)) !== $this->cipher |
||
589 | OR strtolower(mcrypt_enc_get_modes_name($this->handle)) !== $this->mode) |
||
590 | ) { |
||
591 | mcrypt_module_close($this->handle); |
||
592 | } |
||
593 | |||
594 | if ($this->handle = mcrypt_module_open($this->cipher, '', $this->mode, '')) { |
||
595 | logger( |
||
596 | 'info', |
||
597 | 'LOG_SECURITY_MCRYPT_CIPHER_INITIALIZED', |
||
598 | [strtoupper($this->cipher), strtoupper($this->mode)] |
||
599 | ); |
||
600 | } else { |
||
601 | throw new BadFunctionCallException( |
||
602 | 'E_SECURITY_MCRYPT_UNABLE_TO_INITIALIZED', |
||
603 | 0, |
||
604 | [strtoupper($this->cipher), strtoupper($this->mode)] |
||
605 | ); |
||
606 | } |
||
607 | } |
||
608 | } |
||
609 | |||
610 | // -------------------------------------------------------------------- |
||
611 | |||
612 | /** |
||
613 | * Initialize OpenSSL |
||
614 | * |
||
615 | * @param array $params Configuration parameters |
||
616 | * |
||
617 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCallException |
||
618 | */ |
||
619 | protected function opensslInitialize($params) |
||
620 | { |
||
621 | if ( ! empty($params[ 'cipher' ])) { |
||
622 | $params[ 'cipher' ] = strtolower($params[ 'cipher' ]); |
||
623 | $this->cipherAlias($params[ 'cipher' ]); |
||
624 | $this->cipher = $params[ 'cipher' ]; |
||
625 | } |
||
626 | |||
627 | if ( ! empty($params[ 'mode' ])) { |
||
628 | $params[ 'mode' ] = strtolower($params[ 'mode' ]); |
||
629 | if ( ! isset($this->modes[ 'openssl' ][ $params[ 'mode' ] ])) { |
||
630 | // 'Encryption: OpenSSL mode ' . strtoupper( $params[ 'mode' ] ) . ' is not available.' |
||
631 | throw new BadFunctionCallException( |
||
632 | 'E_SECURITY_CRYPT_OPENSSL_MODE_UNAVAILABLE', |
||
633 | 0, |
||
634 | [strtoupper($params[ 'mode' ])] |
||
635 | ); |
||
636 | } else { |
||
637 | $this->mode = $this->modes[ 'openssl' ][ $params[ 'mode' ] ]; |
||
638 | } |
||
639 | } |
||
640 | |||
641 | if (isset($this->cipher, $this->mode)) { |
||
642 | // This is mostly for the stream mode, which doesn't get suffixed in OpenSSL |
||
643 | $handle = empty($this->mode) |
||
644 | ? $this->cipher |
||
645 | : $this->cipher . '-' . $this->mode; |
||
646 | |||
647 | if ( ! in_array($handle, openssl_get_cipher_methods(), true)) { |
||
648 | $this->handle = null; |
||
649 | // 'Encryption: Unable to initialize OpenSSL with method ' . strtoupper( $handle ) . '.' |
||
650 | throw new BadFunctionCallException( |
||
651 | 'E_SECURITY_CRYPT_OPENSSL_UNABLE_TO_INITIALIZED', |
||
652 | 0, |
||
653 | [strtoupper($handle)] |
||
654 | ); |
||
655 | } else { |
||
656 | $this->handle = $handle; |
||
657 | // 'Encryption: OpenSSL initialized with method ' . strtoupper( $handle ) . '.' |
||
658 | logger( |
||
659 | 'info', |
||
660 | 'LOG_SECURITY_CRYPT_OPENSSL_INITIALIZED', |
||
661 | [strtoupper($handle)] |
||
662 | ); |
||
663 | } |
||
664 | } |
||
665 | } |
||
666 | |||
667 | // -------------------------------------------------------------------- |
||
668 | |||
669 | /** |
||
670 | * Encrypt via MCrypt |
||
671 | * |
||
672 | * @param string $data Input data |
||
673 | * @param array $params Input parameters |
||
674 | * |
||
675 | * @return string |
||
676 | */ |
||
677 | protected function mcryptEncrypt($data, $params) |
||
678 | { |
||
679 | if ( ! is_resource($params[ 'handle' ])) { |
||
680 | return false; |
||
681 | } |
||
682 | |||
683 | // The greater-than-1 comparison is mostly a work-around for a bug, |
||
684 | // where 1 is returned for ARCFour instead of 0. |
||
685 | $iv = (($iv_size = mcrypt_enc_get_iv_size($params[ 'handle' ])) > 1) |
||
686 | ? $this->createKey($iv_size) |
||
687 | : null; |
||
688 | |||
689 | if (mcrypt_generic_init($params[ 'handle' ], $params[ 'key' ], $iv) < 0) { |
||
690 | if ($params[ 'handle' ] !== $this->handle) { |
||
691 | mcrypt_module_close($params[ 'handle' ]); |
||
692 | } |
||
693 | |||
694 | return false; |
||
695 | } |
||
696 | |||
697 | // Use PKCS#7 padding in order to ensure compatibility with OpenSSL |
||
698 | // and other implementations outside of PHP. |
||
699 | if (in_array(strtolower(mcrypt_enc_get_modes_name($params[ 'handle' ])), ['cbc', 'ecb'], true)) { |
||
700 | $block_size = mcrypt_enc_get_block_size($params[ 'handle' ]); |
||
701 | $pad = $block_size - (self::strlen($data) % $block_size); |
||
702 | $data .= str_repeat(chr($pad), $pad); |
||
703 | } |
||
704 | |||
705 | // Work-around for yet another strange behavior in MCrypt. |
||
706 | // |
||
707 | // When encrypting in ECB mode, the IV is ignored. Yet |
||
708 | // mcrypt_enc_get_iv_size() returns a value larger than 0 |
||
709 | // even if ECB is used AND mcrypt_generic_init() complains |
||
710 | // if you don't pass an IV with length equal to the said |
||
711 | // return value. |
||
712 | // |
||
713 | // This probably would've been fine (even though still wasteful), |
||
714 | // but OpenSSL isn't that dumb and we need to make the process |
||
715 | // portable, so ... |
||
716 | $data = (mcrypt_enc_get_modes_name($params[ 'handle' ]) !== 'ECB') |
||
717 | ? $iv . mcrypt_generic($params[ 'handle' ], $data) |
||
718 | : mcrypt_generic($params[ 'handle' ], $data); |
||
719 | |||
720 | mcrypt_generic_deinit($params[ 'handle' ]); |
||
721 | if ($params[ 'handle' ] !== $this->handle) { |
||
722 | mcrypt_module_close($params[ 'handle' ]); |
||
723 | } |
||
724 | |||
725 | return $data; |
||
726 | } |
||
727 | |||
728 | // -------------------------------------------------------------------- |
||
729 | |||
730 | /** |
||
731 | * Create a random key |
||
732 | * |
||
733 | * @param int $length Browser length |
||
734 | * |
||
735 | * @return string |
||
736 | */ |
||
737 | public function createKey($length) |
||
738 | { |
||
739 | if (function_exists('random_bytes')) { |
||
740 | try { |
||
741 | return random_bytes((int)$length); |
||
742 | } catch (\Exception $e) { |
||
743 | logger('error', $e->getMessage()); |
||
744 | |||
745 | return false; |
||
746 | } |
||
747 | } elseif (defined('MCRYPT_DEV_URANDOM')) { |
||
748 | return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); |
||
749 | } |
||
750 | |||
751 | $is_secure = null; |
||
752 | $key = openssl_random_pseudo_bytes($length, $is_secure); |
||
753 | |||
754 | return ($is_secure === true) |
||
755 | ? $key |
||
756 | : false; |
||
757 | } |
||
758 | |||
759 | // -------------------------------------------------------------------- |
||
760 | |||
761 | /** |
||
762 | * Encrypt via OpenSSL |
||
763 | * |
||
764 | * @param string $data Input data |
||
765 | * @param array $params Input parameters |
||
766 | * |
||
767 | * @return string |
||
768 | */ |
||
769 | protected function opensslEncrypt($data, $params) |
||
770 | { |
||
771 | if (empty($params[ 'handle' ])) { |
||
772 | return false; |
||
773 | } |
||
774 | |||
775 | $iv = ($iv_size = openssl_cipher_iv_length($params[ 'handle' ])) |
||
776 | ? $this->createKey($iv_size) |
||
777 | : null; |
||
778 | |||
779 | $data = openssl_encrypt( |
||
780 | $data, |
||
781 | $params[ 'handle' ], |
||
782 | $params[ 'key' ], |
||
783 | 1, // DO NOT TOUCH! |
||
784 | $iv |
||
785 | ); |
||
786 | |||
787 | if ($data === false) { |
||
788 | return false; |
||
789 | } |
||
790 | |||
791 | return $iv . $data; |
||
792 | } |
||
793 | |||
794 | // -------------------------------------------------------------------- |
||
795 | |||
796 | /** |
||
797 | * Decrypt via MCrypt |
||
798 | * |
||
799 | * @param string $data Encrypted data |
||
800 | * @param array $params Input parameters |
||
801 | * |
||
802 | * @return string |
||
803 | */ |
||
804 | protected function mcryptDecrypt($data, $params) |
||
805 | { |
||
806 | if ( ! is_resource($params[ 'handle' ])) { |
||
807 | return false; |
||
808 | } |
||
809 | |||
810 | // The greater-than-1 comparison is mostly a work-around for a bug, |
||
811 | // where 1 is returned for ARCFour instead of 0. |
||
812 | if (($iv_size = mcrypt_enc_get_iv_size($params[ 'handle' ])) > 1) { |
||
813 | if (mcrypt_enc_get_modes_name($params[ 'handle' ]) !== 'ECB') { |
||
814 | $iv = self::substr($data, 0, $iv_size); |
||
815 | $data = self::substr($data, $iv_size); |
||
816 | } else { |
||
817 | // MCrypt is dumb and this is ignored, only size matters |
||
818 | $iv = str_repeat("\x0", $iv_size); |
||
819 | } |
||
820 | } else { |
||
821 | $iv = null; |
||
822 | } |
||
823 | |||
824 | if (mcrypt_generic_init($params[ 'handle' ], $params[ 'key' ], $iv) < 0) { |
||
825 | if ($params[ 'handle' ] !== $this->handle) { |
||
826 | mcrypt_module_close($params[ 'handle' ]); |
||
827 | } |
||
828 | |||
829 | return false; |
||
830 | } |
||
831 | |||
832 | $data = mdecrypt_generic($params[ 'handle' ], $data); |
||
833 | // Remove PKCS#7 padding, if necessary |
||
834 | if (in_array(strtolower(mcrypt_enc_get_modes_name($params[ 'handle' ])), ['cbc', 'ecb'], true)) { |
||
835 | $data = self::substr($data, 0, -ord($data[ self::strlen($data) - 1 ])); |
||
836 | } |
||
837 | |||
838 | mcrypt_generic_deinit($params[ 'handle' ]); |
||
839 | if ($params[ 'handle' ] !== $this->handle) { |
||
840 | mcrypt_module_close($params[ 'handle' ]); |
||
841 | } |
||
842 | |||
843 | return $data; |
||
844 | } |
||
845 | |||
846 | // -------------------------------------------------------------------- |
||
847 | |||
848 | /** |
||
849 | * Decrypt via OpenSSL |
||
850 | * |
||
851 | * @param string $data Encrypted data |
||
852 | * @param array $params Input parameters |
||
853 | * |
||
854 | * @return string |
||
855 | */ |
||
856 | protected function opensslDecrypt($data, $params) |
||
857 | { |
||
858 | if ($iv_size = openssl_cipher_iv_length($params[ 'handle' ])) { |
||
859 | $iv = self::substr($data, 0, $iv_size); |
||
860 | $data = self::substr($data, $iv_size); |
||
861 | } else { |
||
862 | $iv = null; |
||
863 | } |
||
864 | |||
865 | return empty($params[ 'handle' ]) |
||
866 | ? false |
||
867 | : openssl_decrypt( |
||
868 | $data, |
||
869 | $params[ 'handle' ], |
||
870 | $params[ 'key' ], |
||
871 | 1, // DO NOT TOUCH! |
||
872 | $iv |
||
873 | ); |
||
874 | } |
||
875 | |||
876 | // -------------------------------------------------------------------- |
||
877 | |||
878 | /** |
||
879 | * Get MCrypt handle |
||
880 | * |
||
881 | * @param string $cipher Cipher name |
||
882 | * @param string $mode Encryption mode |
||
883 | * |
||
884 | * @return resource |
||
885 | */ |
||
886 | protected function mcryptGetHandle($cipher, $mode) |
||
889 | } |
||
890 | |||
891 | // -------------------------------------------------------------------- |
||
892 | |||
893 | /** |
||
894 | * Get OpenSSL handle |
||
895 | * |
||
896 | * @param string $cipher Cipher name |
||
897 | * @param string $mode Encryption mode |
||
898 | * |
||
899 | * @return string |
||
900 | */ |
||
901 | protected function opensslGetHandle($cipher, $mode) |
||
907 | } |
||
908 | } |