@@ -19,692 +19,692 @@ |
||
| 19 | 19 | class EE_Encryption implements InterminableInterface |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * key used for saving the encryption key to the wp_options table |
|
| 24 | - */ |
|
| 25 | - const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * the OPENSSL cipher method used |
|
| 29 | - */ |
|
| 30 | - const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * WP "options_name" used to store a verified available cipher method |
|
| 34 | - */ |
|
| 35 | - const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * the OPENSSL digest method used |
|
| 39 | - */ |
|
| 40 | - const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * separates the encrypted text from the initialization vector |
|
| 44 | - */ |
|
| 45 | - const OPENSSL_IV_DELIMITER = ':iv:'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * appended to text encrypted using the acme encryption |
|
| 49 | - */ |
|
| 50 | - const ACME_ENCRYPTION_FLAG = '::ae'; |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * instance of the EE_Encryption object |
|
| 56 | - */ |
|
| 57 | - protected static $_instance; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var string $_encryption_key |
|
| 61 | - */ |
|
| 62 | - protected $_encryption_key; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var string $cipher_method |
|
| 66 | - */ |
|
| 67 | - private $cipher_method = ''; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var array $cipher_methods |
|
| 71 | - */ |
|
| 72 | - private $cipher_methods = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var array $digest_methods |
|
| 76 | - */ |
|
| 77 | - private $digest_methods = array(); |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @var boolean $_use_openssl_encrypt |
|
| 81 | - */ |
|
| 82 | - protected $_use_openssl_encrypt = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @var boolean $_use_mcrypt |
|
| 86 | - */ |
|
| 87 | - protected $_use_mcrypt = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @var boolean $_use_base64_encode |
|
| 91 | - */ |
|
| 92 | - protected $_use_base64_encode = false; |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * protected constructor to prevent direct creation |
|
| 98 | - */ |
|
| 99 | - protected function __construct() |
|
| 100 | - { |
|
| 101 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 102 | - define('ESPRESSO_ENCRYPT', true); |
|
| 103 | - } |
|
| 104 | - if (extension_loaded('openssl')) { |
|
| 105 | - $this->_use_openssl_encrypt = true; |
|
| 106 | - } else if (extension_loaded('mcrypt')) { |
|
| 107 | - $this->_use_mcrypt = true; |
|
| 108 | - } |
|
| 109 | - if (function_exists('base64_encode')) { |
|
| 110 | - $this->_use_base64_encode = true; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * singleton method used to instantiate class object |
|
| 118 | - * |
|
| 119 | - * @return EE_Encryption |
|
| 120 | - */ |
|
| 121 | - public static function instance() |
|
| 122 | - { |
|
| 123 | - // check if class object is instantiated |
|
| 124 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 125 | - EE_Encryption::$_instance = new self(); |
|
| 126 | - } |
|
| 127 | - return EE_Encryption::$_instance; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * get encryption key |
|
| 134 | - * |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - public function get_encryption_key() |
|
| 138 | - { |
|
| 139 | - // if encryption key has not been set |
|
| 140 | - if (empty($this->_encryption_key)) { |
|
| 141 | - // retrieve encryption_key from db |
|
| 142 | - $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
| 143 | - // WHAT?? No encryption_key in the db ?? |
|
| 144 | - if ($this->_encryption_key === '') { |
|
| 145 | - // let's make one. And md5 it to make it just the right size for a key |
|
| 146 | - $new_key = md5($this->generate_random_string()); |
|
| 147 | - // now save it to the db for later |
|
| 148 | - add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
| 149 | - // here's the key - FINALLY ! |
|
| 150 | - $this->_encryption_key = $new_key; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - return $this->_encryption_key; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * encrypts data |
|
| 160 | - * |
|
| 161 | - * @param string $text_string - the text to be encrypted |
|
| 162 | - * @return string |
|
| 163 | - * @throws RuntimeException |
|
| 164 | - */ |
|
| 165 | - public function encrypt($text_string = '') |
|
| 166 | - { |
|
| 167 | - // you give me nothing??? GET OUT ! |
|
| 168 | - if (empty($text_string)) { |
|
| 169 | - return $text_string; |
|
| 170 | - } |
|
| 171 | - if ($this->_use_openssl_encrypt) { |
|
| 172 | - $encrypted_text = $this->openssl_encrypt($text_string); |
|
| 173 | - } else { |
|
| 174 | - $encrypted_text = $this->acme_encrypt($text_string); |
|
| 175 | - } |
|
| 176 | - return $encrypted_text; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * decrypts data |
|
| 183 | - * |
|
| 184 | - * @param string $encrypted_text - the text to be decrypted |
|
| 185 | - * @return string |
|
| 186 | - * @throws RuntimeException |
|
| 187 | - */ |
|
| 188 | - public function decrypt($encrypted_text = '') |
|
| 189 | - { |
|
| 190 | - // you give me nothing??? GET OUT ! |
|
| 191 | - if (empty($encrypted_text)) { |
|
| 192 | - return $encrypted_text; |
|
| 193 | - } |
|
| 194 | - // if PHP's mcrypt functions are installed then we'll use them |
|
| 195 | - if ($this->_use_openssl_encrypt) { |
|
| 196 | - $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
| 197 | - } else { |
|
| 198 | - $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
| 199 | - } |
|
| 200 | - return $decrypted_text; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * encodes string with PHP's base64 encoding |
|
| 207 | - * |
|
| 208 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 209 | - * @param string $text_string the text to be encoded |
|
| 210 | - * @return string |
|
| 211 | - */ |
|
| 212 | - public function base64_string_encode($text_string = '') |
|
| 213 | - { |
|
| 214 | - // you give me nothing??? GET OUT ! |
|
| 215 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 216 | - return $text_string; |
|
| 217 | - } |
|
| 218 | - // encode |
|
| 219 | - return base64_encode($text_string); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * decodes string that has been encoded with PHP's base64 encoding |
|
| 225 | - * |
|
| 226 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 227 | - * @param string $encoded_string the text to be decoded |
|
| 228 | - * @return string |
|
| 229 | - * @throws RuntimeException |
|
| 230 | - */ |
|
| 231 | - public function base64_string_decode($encoded_string = '') |
|
| 232 | - { |
|
| 233 | - // you give me nothing??? GET OUT ! |
|
| 234 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 235 | - return $encoded_string; |
|
| 236 | - } |
|
| 237 | - // decode |
|
| 238 | - $decoded_string = base64_decode($encoded_string); |
|
| 239 | - if ($decoded_string === false) { |
|
| 240 | - throw new RuntimeException( |
|
| 241 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - return $decoded_string; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * encodes url string with PHP's base64 encoding |
|
| 251 | - * |
|
| 252 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 253 | - * @param string $text_string the text to be encoded |
|
| 254 | - * @return string |
|
| 255 | - */ |
|
| 256 | - public function base64_url_encode($text_string = '') |
|
| 257 | - { |
|
| 258 | - // you give me nothing??? GET OUT ! |
|
| 259 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 260 | - return $text_string; |
|
| 261 | - } |
|
| 262 | - // encode |
|
| 263 | - $encoded_string = base64_encode($text_string); |
|
| 264 | - // remove chars to make encoding more URL friendly |
|
| 265 | - return strtr($encoded_string, '+/=', '-_,'); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
| 271 | - * |
|
| 272 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 273 | - * @param string $encoded_string the text to be decoded |
|
| 274 | - * @return string |
|
| 275 | - * @throws RuntimeException |
|
| 276 | - */ |
|
| 277 | - public function base64_url_decode($encoded_string = '') |
|
| 278 | - { |
|
| 279 | - // you give me nothing??? GET OUT ! |
|
| 280 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 281 | - return $encoded_string; |
|
| 282 | - } |
|
| 283 | - // replace previously removed characters |
|
| 284 | - $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
| 285 | - // decode |
|
| 286 | - $decoded_string = base64_decode($encoded_string); |
|
| 287 | - if ($decoded_string === false) { |
|
| 288 | - throw new RuntimeException( |
|
| 289 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - return $decoded_string; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * encrypts data using PHP's openssl functions |
|
| 298 | - * |
|
| 299 | - * @param string $text_string the text to be encrypted |
|
| 300 | - * @param string $cipher_method |
|
| 301 | - * @param string $encryption_key |
|
| 302 | - * @return string |
|
| 303 | - * @throws RuntimeException |
|
| 304 | - */ |
|
| 305 | - protected function openssl_encrypt( |
|
| 306 | - $text_string = '', |
|
| 307 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 308 | - $encryption_key = '' |
|
| 309 | - ) { |
|
| 310 | - // you give me nothing??? GET OUT ! |
|
| 311 | - if (empty($text_string)) { |
|
| 312 | - return $text_string; |
|
| 313 | - } |
|
| 314 | - $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
| 315 | - // get initialization vector size |
|
| 316 | - $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
| 317 | - // generate initialization vector. |
|
| 318 | - // The second parameter ("crypto_strong") is passed by reference, |
|
| 319 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 320 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 321 | - $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
| 322 | - if ($iv === false || $is_strong === false) { |
|
| 323 | - throw new RuntimeException( |
|
| 324 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 325 | - ); |
|
| 326 | - } |
|
| 327 | - // encrypt it |
|
| 328 | - $encrypted_text = openssl_encrypt( |
|
| 329 | - $text_string, |
|
| 330 | - $this->cipher_method, |
|
| 331 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 332 | - 0, |
|
| 333 | - $iv |
|
| 334 | - ); |
|
| 335 | - // append the initialization vector |
|
| 336 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 337 | - // trim and maybe encode |
|
| 338 | - return $this->_use_base64_encode |
|
| 339 | - ? trim(base64_encode($encrypted_text)) |
|
| 340 | - : trim($encrypted_text); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Returns a cipher method that has been verified to work. |
|
| 347 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
| 348 | - * Then tests the incoming default and returns that if it's good. |
|
| 349 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
| 350 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 351 | - * to see what is available on the server, and returns the results. |
|
| 352 | - * |
|
| 353 | - * @param string $cipher_method |
|
| 354 | - * @return string |
|
| 355 | - * @throws RuntimeException |
|
| 356 | - */ |
|
| 357 | - protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
| 358 | - { |
|
| 359 | - if($this->cipher_method !== ''){ |
|
| 360 | - return $this->cipher_method; |
|
| 361 | - } |
|
| 362 | - // verify that the default cipher method can produce an initialization vector |
|
| 363 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 364 | - // nope? okay let's get what we found in the past to work |
|
| 365 | - $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
| 366 | - // oops... haven't tested available cipher methods yet |
|
| 367 | - if($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 368 | - $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - return $cipher_method; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param string $cipher_method |
|
| 378 | - * @return string |
|
| 379 | - * @throws \RuntimeException |
|
| 380 | - */ |
|
| 381 | - protected function getAvailableCipherMethod($cipher_method) |
|
| 382 | - { |
|
| 383 | - // verify that the incoming cipher method can produce an initialization vector |
|
| 384 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 385 | - // nope? then check the next cipher in the list of available cipher methods |
|
| 386 | - $cipher_method = next($this->cipher_methods); |
|
| 387 | - // what? there's no list? then generate that list and cache it, |
|
| 388 | - if (empty($this->cipher_methods)) { |
|
| 389 | - $this->cipher_methods = openssl_get_cipher_methods(); |
|
| 390 | - // then grab the first item from the list |
|
| 391 | - $cipher_method = reset($this->cipher_methods); |
|
| 392 | - } |
|
| 393 | - if($cipher_method === false){ |
|
| 394 | - throw new RuntimeException( |
|
| 395 | - esc_html__( |
|
| 396 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 397 | - 'event_espresso' |
|
| 398 | - ) |
|
| 399 | - ); |
|
| 400 | - } |
|
| 401 | - // verify that the next cipher method works |
|
| 402 | - return $this->getAvailableCipherMethod($cipher_method); |
|
| 403 | - } |
|
| 404 | - // if we've gotten this far, then we found an available cipher method that works |
|
| 405 | - // so save that for next time |
|
| 406 | - update_option( |
|
| 407 | - EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
| 408 | - $cipher_method |
|
| 409 | - ); |
|
| 410 | - return $cipher_method; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * decrypts data that has been encrypted with PHP's openssl functions |
|
| 416 | - * |
|
| 417 | - * @param string $encrypted_text the text to be decrypted |
|
| 418 | - * @param string $cipher_method |
|
| 419 | - * @param string $encryption_key |
|
| 420 | - * @return string |
|
| 421 | - * @throws RuntimeException |
|
| 422 | - */ |
|
| 423 | - protected function openssl_decrypt( |
|
| 424 | - $encrypted_text = '', |
|
| 425 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 426 | - $encryption_key = '' |
|
| 427 | - ) { |
|
| 428 | - // you give me nothing??? GET OUT ! |
|
| 429 | - if (empty($encrypted_text)) { |
|
| 430 | - return $encrypted_text; |
|
| 431 | - } |
|
| 432 | - // decode |
|
| 433 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 434 | - ? $this->base64_url_decode($encrypted_text) |
|
| 435 | - : $encrypted_text; |
|
| 436 | - $encrypted_components = explode( |
|
| 437 | - EE_Encryption::OPENSSL_IV_DELIMITER, |
|
| 438 | - $encrypted_text, |
|
| 439 | - 2 |
|
| 440 | - ); |
|
| 441 | - // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
| 442 | - if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
| 443 | - return $this->m_decrypt($encrypted_text); |
|
| 444 | - } |
|
| 445 | - // decrypt it |
|
| 446 | - $decrypted_text = openssl_decrypt( |
|
| 447 | - $encrypted_components[0], |
|
| 448 | - $this->getCipherMethod($cipher_method), |
|
| 449 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD,$encryption_key), |
|
| 450 | - 0, |
|
| 451 | - $encrypted_components[1] |
|
| 452 | - ); |
|
| 453 | - $decrypted_text = trim($decrypted_text); |
|
| 454 | - return $decrypted_text; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Computes the digest hash value using the specified digest method. |
|
| 460 | - * If that digest method fails to produce a valid hash value, |
|
| 461 | - * then we'll grab the next digest method and recursively try again until something works. |
|
| 462 | - * |
|
| 463 | - * @param string $digest_method |
|
| 464 | - * @param string $encryption_key |
|
| 465 | - * @return string |
|
| 466 | - * @throws RuntimeException |
|
| 467 | - */ |
|
| 468 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = ''){ |
|
| 469 | - $encryption_key = $encryption_key !== '' |
|
| 470 | - ? $encryption_key |
|
| 471 | - : $this->get_encryption_key(); |
|
| 472 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
| 473 | - if ($digest_hash_value === false) { |
|
| 474 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 475 | - } |
|
| 476 | - return $digest_hash_value; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Returns the NEXT element in the $digest_methods array. |
|
| 483 | - * If the $digest_methods array is empty, then we populate it |
|
| 484 | - * with the available values returned from openssl_get_md_methods(). |
|
| 485 | - * |
|
| 486 | - * @return string |
|
| 487 | - * @throws \RuntimeException |
|
| 488 | - */ |
|
| 489 | - protected function getDigestMethod(){ |
|
| 490 | - $digest_method = prev($this->digest_methods); |
|
| 491 | - if (empty($this->digest_methods)) { |
|
| 492 | - $this->digest_methods = openssl_get_md_methods(); |
|
| 493 | - $digest_method = end($this->digest_methods); |
|
| 494 | - } |
|
| 495 | - if ($digest_method === false) { |
|
| 496 | - throw new RuntimeException( |
|
| 497 | - esc_html__( |
|
| 498 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 499 | - 'event_espresso' |
|
| 500 | - ) |
|
| 501 | - ); |
|
| 502 | - } |
|
| 503 | - return $digest_method; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 509 | - * |
|
| 510 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 511 | - * @param string $text_string the text to be decrypted |
|
| 512 | - * @return string |
|
| 513 | - */ |
|
| 514 | - protected function acme_encrypt($text_string = '') |
|
| 515 | - { |
|
| 516 | - // you give me nothing??? GET OUT ! |
|
| 517 | - if (empty($text_string)) { |
|
| 518 | - return $text_string; |
|
| 519 | - } |
|
| 520 | - $key_bits = str_split( |
|
| 521 | - str_pad( |
|
| 522 | - '', |
|
| 523 | - strlen($text_string), |
|
| 524 | - $this->get_encryption_key(), |
|
| 525 | - STR_PAD_RIGHT |
|
| 526 | - ) |
|
| 527 | - ); |
|
| 528 | - $string_bits = str_split($text_string); |
|
| 529 | - foreach ($string_bits as $k => $v) { |
|
| 530 | - $temp = ord($v) + ord($key_bits[$k]); |
|
| 531 | - $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 532 | - } |
|
| 533 | - $encrypted_text = implode('', $string_bits); |
|
| 534 | - $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
| 535 | - return $this->_use_base64_encode |
|
| 536 | - ? base64_encode($encrypted_text) |
|
| 537 | - : $encrypted_text; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 544 | - * |
|
| 545 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 546 | - * @param string $encrypted_text the text to be decrypted |
|
| 547 | - * @return string |
|
| 548 | - * @throws RuntimeException |
|
| 549 | - */ |
|
| 550 | - protected function acme_decrypt($encrypted_text = '') |
|
| 551 | - { |
|
| 552 | - // you give me nothing??? GET OUT ! |
|
| 553 | - if (empty($encrypted_text)) { |
|
| 554 | - return $encrypted_text; |
|
| 555 | - } |
|
| 556 | - // decode the data ? |
|
| 557 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 558 | - ? $this->base64_url_decode($encrypted_text) |
|
| 559 | - : $encrypted_text; |
|
| 560 | - if ( |
|
| 561 | - $this->_use_mcrypt |
|
| 562 | - && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
| 563 | - ){ |
|
| 564 | - return $this->m_decrypt($encrypted_text); |
|
| 565 | - } |
|
| 566 | - $encrypted_text = substr($encrypted_text, 0, -4); |
|
| 567 | - $key_bits = str_split( |
|
| 568 | - str_pad( |
|
| 569 | - '', |
|
| 570 | - strlen($encrypted_text), |
|
| 571 | - $this->get_encryption_key(), |
|
| 572 | - STR_PAD_RIGHT |
|
| 573 | - ) |
|
| 574 | - ); |
|
| 575 | - $string_bits = str_split($encrypted_text); |
|
| 576 | - foreach ($string_bits as $k => $v) { |
|
| 577 | - $temp = ord($v) - ord($key_bits[$k]); |
|
| 578 | - $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 579 | - } |
|
| 580 | - return implode('', $string_bits); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
| 587 | - * @param $string |
|
| 588 | - * @return bool |
|
| 589 | - */ |
|
| 590 | - protected function valid_base_64($string) |
|
| 591 | - { |
|
| 592 | - // ensure data is a string |
|
| 593 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 594 | - return false; |
|
| 595 | - } |
|
| 596 | - $decoded = base64_decode($string, true); |
|
| 597 | - // Check if there is no invalid character in string |
|
| 598 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 599 | - return false; |
|
| 600 | - } |
|
| 601 | - // Decode the string in strict mode and send the response |
|
| 602 | - if (! base64_decode($string, true)) { |
|
| 603 | - return false; |
|
| 604 | - } |
|
| 605 | - // Encode and compare it to original one |
|
| 606 | - return base64_encode($decoded) === $string; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * generate random string |
|
| 613 | - * |
|
| 614 | - * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 615 | - * @param int $length number of characters for random string |
|
| 616 | - * @return string |
|
| 617 | - */ |
|
| 618 | - public function generate_random_string($length = 40) |
|
| 619 | - { |
|
| 620 | - $iterations = ceil($length / 40); |
|
| 621 | - $random_string = ''; |
|
| 622 | - for ($i = 0; $i < $iterations; $i++) { |
|
| 623 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 624 | - } |
|
| 625 | - $random_string = substr($random_string, 0, $length); |
|
| 626 | - return $random_string; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * encrypts data using PHP's mcrypt functions |
|
| 633 | - * |
|
| 634 | - * @deprecated 4.9.39 |
|
| 635 | - * @param string $text_string |
|
| 636 | - * @internal param $string - the text to be encrypted |
|
| 637 | - * @return string |
|
| 638 | - * @throws RuntimeException |
|
| 639 | - */ |
|
| 640 | - protected function m_encrypt($text_string = '') |
|
| 641 | - { |
|
| 642 | - // you give me nothing??? GET OUT ! |
|
| 643 | - if (empty($text_string)) { |
|
| 644 | - return $text_string; |
|
| 645 | - } |
|
| 646 | - // get the initialization vector size |
|
| 647 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 648 | - // initialization vector |
|
| 649 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 650 | - if ($iv === false) { |
|
| 651 | - throw new RuntimeException( |
|
| 652 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 653 | - ); |
|
| 654 | - } |
|
| 655 | - // encrypt it |
|
| 656 | - $encrypted_text = mcrypt_encrypt( |
|
| 657 | - MCRYPT_RIJNDAEL_256, |
|
| 658 | - $this->get_encryption_key(), |
|
| 659 | - $text_string, |
|
| 660 | - MCRYPT_MODE_ECB, |
|
| 661 | - $iv |
|
| 662 | - ); |
|
| 663 | - // trim and maybe encode |
|
| 664 | - return $this->_use_base64_encode |
|
| 665 | - ? trim(base64_encode($encrypted_text)) |
|
| 666 | - : trim($encrypted_text); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * decrypts data that has been encrypted with PHP's mcrypt functions |
|
| 673 | - * |
|
| 674 | - * @deprecated 4.9.39 |
|
| 675 | - * @param string $encrypted_text the text to be decrypted |
|
| 676 | - * @return string |
|
| 677 | - * @throws RuntimeException |
|
| 678 | - */ |
|
| 679 | - protected function m_decrypt($encrypted_text = '') |
|
| 680 | - { |
|
| 681 | - // you give me nothing??? GET OUT ! |
|
| 682 | - if (empty($encrypted_text)) { |
|
| 683 | - return $encrypted_text; |
|
| 684 | - } |
|
| 685 | - // decode |
|
| 686 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 687 | - ? $this->base64_url_decode($encrypted_text) |
|
| 688 | - : $encrypted_text; |
|
| 689 | - // get the initialization vector size |
|
| 690 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 691 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 692 | - if ($iv === false) { |
|
| 693 | - throw new RuntimeException( |
|
| 694 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 695 | - ); |
|
| 696 | - } |
|
| 697 | - // decrypt it |
|
| 698 | - $decrypted_text = mcrypt_decrypt( |
|
| 699 | - MCRYPT_RIJNDAEL_256, |
|
| 700 | - $this->get_encryption_key(), |
|
| 701 | - $encrypted_text, |
|
| 702 | - MCRYPT_MODE_ECB, |
|
| 703 | - $iv |
|
| 704 | - ); |
|
| 705 | - $decrypted_text = trim($decrypted_text); |
|
| 706 | - return $decrypted_text; |
|
| 707 | - } |
|
| 22 | + /** |
|
| 23 | + * key used for saving the encryption key to the wp_options table |
|
| 24 | + */ |
|
| 25 | + const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * the OPENSSL cipher method used |
|
| 29 | + */ |
|
| 30 | + const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * WP "options_name" used to store a verified available cipher method |
|
| 34 | + */ |
|
| 35 | + const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * the OPENSSL digest method used |
|
| 39 | + */ |
|
| 40 | + const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * separates the encrypted text from the initialization vector |
|
| 44 | + */ |
|
| 45 | + const OPENSSL_IV_DELIMITER = ':iv:'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * appended to text encrypted using the acme encryption |
|
| 49 | + */ |
|
| 50 | + const ACME_ENCRYPTION_FLAG = '::ae'; |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * instance of the EE_Encryption object |
|
| 56 | + */ |
|
| 57 | + protected static $_instance; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var string $_encryption_key |
|
| 61 | + */ |
|
| 62 | + protected $_encryption_key; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var string $cipher_method |
|
| 66 | + */ |
|
| 67 | + private $cipher_method = ''; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var array $cipher_methods |
|
| 71 | + */ |
|
| 72 | + private $cipher_methods = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var array $digest_methods |
|
| 76 | + */ |
|
| 77 | + private $digest_methods = array(); |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @var boolean $_use_openssl_encrypt |
|
| 81 | + */ |
|
| 82 | + protected $_use_openssl_encrypt = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @var boolean $_use_mcrypt |
|
| 86 | + */ |
|
| 87 | + protected $_use_mcrypt = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @var boolean $_use_base64_encode |
|
| 91 | + */ |
|
| 92 | + protected $_use_base64_encode = false; |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * protected constructor to prevent direct creation |
|
| 98 | + */ |
|
| 99 | + protected function __construct() |
|
| 100 | + { |
|
| 101 | + if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 102 | + define('ESPRESSO_ENCRYPT', true); |
|
| 103 | + } |
|
| 104 | + if (extension_loaded('openssl')) { |
|
| 105 | + $this->_use_openssl_encrypt = true; |
|
| 106 | + } else if (extension_loaded('mcrypt')) { |
|
| 107 | + $this->_use_mcrypt = true; |
|
| 108 | + } |
|
| 109 | + if (function_exists('base64_encode')) { |
|
| 110 | + $this->_use_base64_encode = true; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * singleton method used to instantiate class object |
|
| 118 | + * |
|
| 119 | + * @return EE_Encryption |
|
| 120 | + */ |
|
| 121 | + public static function instance() |
|
| 122 | + { |
|
| 123 | + // check if class object is instantiated |
|
| 124 | + if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 125 | + EE_Encryption::$_instance = new self(); |
|
| 126 | + } |
|
| 127 | + return EE_Encryption::$_instance; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * get encryption key |
|
| 134 | + * |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + public function get_encryption_key() |
|
| 138 | + { |
|
| 139 | + // if encryption key has not been set |
|
| 140 | + if (empty($this->_encryption_key)) { |
|
| 141 | + // retrieve encryption_key from db |
|
| 142 | + $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
| 143 | + // WHAT?? No encryption_key in the db ?? |
|
| 144 | + if ($this->_encryption_key === '') { |
|
| 145 | + // let's make one. And md5 it to make it just the right size for a key |
|
| 146 | + $new_key = md5($this->generate_random_string()); |
|
| 147 | + // now save it to the db for later |
|
| 148 | + add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
| 149 | + // here's the key - FINALLY ! |
|
| 150 | + $this->_encryption_key = $new_key; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + return $this->_encryption_key; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * encrypts data |
|
| 160 | + * |
|
| 161 | + * @param string $text_string - the text to be encrypted |
|
| 162 | + * @return string |
|
| 163 | + * @throws RuntimeException |
|
| 164 | + */ |
|
| 165 | + public function encrypt($text_string = '') |
|
| 166 | + { |
|
| 167 | + // you give me nothing??? GET OUT ! |
|
| 168 | + if (empty($text_string)) { |
|
| 169 | + return $text_string; |
|
| 170 | + } |
|
| 171 | + if ($this->_use_openssl_encrypt) { |
|
| 172 | + $encrypted_text = $this->openssl_encrypt($text_string); |
|
| 173 | + } else { |
|
| 174 | + $encrypted_text = $this->acme_encrypt($text_string); |
|
| 175 | + } |
|
| 176 | + return $encrypted_text; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * decrypts data |
|
| 183 | + * |
|
| 184 | + * @param string $encrypted_text - the text to be decrypted |
|
| 185 | + * @return string |
|
| 186 | + * @throws RuntimeException |
|
| 187 | + */ |
|
| 188 | + public function decrypt($encrypted_text = '') |
|
| 189 | + { |
|
| 190 | + // you give me nothing??? GET OUT ! |
|
| 191 | + if (empty($encrypted_text)) { |
|
| 192 | + return $encrypted_text; |
|
| 193 | + } |
|
| 194 | + // if PHP's mcrypt functions are installed then we'll use them |
|
| 195 | + if ($this->_use_openssl_encrypt) { |
|
| 196 | + $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
| 197 | + } else { |
|
| 198 | + $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
| 199 | + } |
|
| 200 | + return $decrypted_text; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * encodes string with PHP's base64 encoding |
|
| 207 | + * |
|
| 208 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 209 | + * @param string $text_string the text to be encoded |
|
| 210 | + * @return string |
|
| 211 | + */ |
|
| 212 | + public function base64_string_encode($text_string = '') |
|
| 213 | + { |
|
| 214 | + // you give me nothing??? GET OUT ! |
|
| 215 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 216 | + return $text_string; |
|
| 217 | + } |
|
| 218 | + // encode |
|
| 219 | + return base64_encode($text_string); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * decodes string that has been encoded with PHP's base64 encoding |
|
| 225 | + * |
|
| 226 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 227 | + * @param string $encoded_string the text to be decoded |
|
| 228 | + * @return string |
|
| 229 | + * @throws RuntimeException |
|
| 230 | + */ |
|
| 231 | + public function base64_string_decode($encoded_string = '') |
|
| 232 | + { |
|
| 233 | + // you give me nothing??? GET OUT ! |
|
| 234 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 235 | + return $encoded_string; |
|
| 236 | + } |
|
| 237 | + // decode |
|
| 238 | + $decoded_string = base64_decode($encoded_string); |
|
| 239 | + if ($decoded_string === false) { |
|
| 240 | + throw new RuntimeException( |
|
| 241 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + return $decoded_string; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * encodes url string with PHP's base64 encoding |
|
| 251 | + * |
|
| 252 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 253 | + * @param string $text_string the text to be encoded |
|
| 254 | + * @return string |
|
| 255 | + */ |
|
| 256 | + public function base64_url_encode($text_string = '') |
|
| 257 | + { |
|
| 258 | + // you give me nothing??? GET OUT ! |
|
| 259 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 260 | + return $text_string; |
|
| 261 | + } |
|
| 262 | + // encode |
|
| 263 | + $encoded_string = base64_encode($text_string); |
|
| 264 | + // remove chars to make encoding more URL friendly |
|
| 265 | + return strtr($encoded_string, '+/=', '-_,'); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
| 271 | + * |
|
| 272 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 273 | + * @param string $encoded_string the text to be decoded |
|
| 274 | + * @return string |
|
| 275 | + * @throws RuntimeException |
|
| 276 | + */ |
|
| 277 | + public function base64_url_decode($encoded_string = '') |
|
| 278 | + { |
|
| 279 | + // you give me nothing??? GET OUT ! |
|
| 280 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 281 | + return $encoded_string; |
|
| 282 | + } |
|
| 283 | + // replace previously removed characters |
|
| 284 | + $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
| 285 | + // decode |
|
| 286 | + $decoded_string = base64_decode($encoded_string); |
|
| 287 | + if ($decoded_string === false) { |
|
| 288 | + throw new RuntimeException( |
|
| 289 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + return $decoded_string; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * encrypts data using PHP's openssl functions |
|
| 298 | + * |
|
| 299 | + * @param string $text_string the text to be encrypted |
|
| 300 | + * @param string $cipher_method |
|
| 301 | + * @param string $encryption_key |
|
| 302 | + * @return string |
|
| 303 | + * @throws RuntimeException |
|
| 304 | + */ |
|
| 305 | + protected function openssl_encrypt( |
|
| 306 | + $text_string = '', |
|
| 307 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 308 | + $encryption_key = '' |
|
| 309 | + ) { |
|
| 310 | + // you give me nothing??? GET OUT ! |
|
| 311 | + if (empty($text_string)) { |
|
| 312 | + return $text_string; |
|
| 313 | + } |
|
| 314 | + $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
| 315 | + // get initialization vector size |
|
| 316 | + $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
| 317 | + // generate initialization vector. |
|
| 318 | + // The second parameter ("crypto_strong") is passed by reference, |
|
| 319 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 320 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 321 | + $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
| 322 | + if ($iv === false || $is_strong === false) { |
|
| 323 | + throw new RuntimeException( |
|
| 324 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 325 | + ); |
|
| 326 | + } |
|
| 327 | + // encrypt it |
|
| 328 | + $encrypted_text = openssl_encrypt( |
|
| 329 | + $text_string, |
|
| 330 | + $this->cipher_method, |
|
| 331 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 332 | + 0, |
|
| 333 | + $iv |
|
| 334 | + ); |
|
| 335 | + // append the initialization vector |
|
| 336 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 337 | + // trim and maybe encode |
|
| 338 | + return $this->_use_base64_encode |
|
| 339 | + ? trim(base64_encode($encrypted_text)) |
|
| 340 | + : trim($encrypted_text); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Returns a cipher method that has been verified to work. |
|
| 347 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
| 348 | + * Then tests the incoming default and returns that if it's good. |
|
| 349 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
| 350 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 351 | + * to see what is available on the server, and returns the results. |
|
| 352 | + * |
|
| 353 | + * @param string $cipher_method |
|
| 354 | + * @return string |
|
| 355 | + * @throws RuntimeException |
|
| 356 | + */ |
|
| 357 | + protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
| 358 | + { |
|
| 359 | + if($this->cipher_method !== ''){ |
|
| 360 | + return $this->cipher_method; |
|
| 361 | + } |
|
| 362 | + // verify that the default cipher method can produce an initialization vector |
|
| 363 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 364 | + // nope? okay let's get what we found in the past to work |
|
| 365 | + $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
| 366 | + // oops... haven't tested available cipher methods yet |
|
| 367 | + if($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 368 | + $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + return $cipher_method; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param string $cipher_method |
|
| 378 | + * @return string |
|
| 379 | + * @throws \RuntimeException |
|
| 380 | + */ |
|
| 381 | + protected function getAvailableCipherMethod($cipher_method) |
|
| 382 | + { |
|
| 383 | + // verify that the incoming cipher method can produce an initialization vector |
|
| 384 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 385 | + // nope? then check the next cipher in the list of available cipher methods |
|
| 386 | + $cipher_method = next($this->cipher_methods); |
|
| 387 | + // what? there's no list? then generate that list and cache it, |
|
| 388 | + if (empty($this->cipher_methods)) { |
|
| 389 | + $this->cipher_methods = openssl_get_cipher_methods(); |
|
| 390 | + // then grab the first item from the list |
|
| 391 | + $cipher_method = reset($this->cipher_methods); |
|
| 392 | + } |
|
| 393 | + if($cipher_method === false){ |
|
| 394 | + throw new RuntimeException( |
|
| 395 | + esc_html__( |
|
| 396 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 397 | + 'event_espresso' |
|
| 398 | + ) |
|
| 399 | + ); |
|
| 400 | + } |
|
| 401 | + // verify that the next cipher method works |
|
| 402 | + return $this->getAvailableCipherMethod($cipher_method); |
|
| 403 | + } |
|
| 404 | + // if we've gotten this far, then we found an available cipher method that works |
|
| 405 | + // so save that for next time |
|
| 406 | + update_option( |
|
| 407 | + EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
| 408 | + $cipher_method |
|
| 409 | + ); |
|
| 410 | + return $cipher_method; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * decrypts data that has been encrypted with PHP's openssl functions |
|
| 416 | + * |
|
| 417 | + * @param string $encrypted_text the text to be decrypted |
|
| 418 | + * @param string $cipher_method |
|
| 419 | + * @param string $encryption_key |
|
| 420 | + * @return string |
|
| 421 | + * @throws RuntimeException |
|
| 422 | + */ |
|
| 423 | + protected function openssl_decrypt( |
|
| 424 | + $encrypted_text = '', |
|
| 425 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 426 | + $encryption_key = '' |
|
| 427 | + ) { |
|
| 428 | + // you give me nothing??? GET OUT ! |
|
| 429 | + if (empty($encrypted_text)) { |
|
| 430 | + return $encrypted_text; |
|
| 431 | + } |
|
| 432 | + // decode |
|
| 433 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 434 | + ? $this->base64_url_decode($encrypted_text) |
|
| 435 | + : $encrypted_text; |
|
| 436 | + $encrypted_components = explode( |
|
| 437 | + EE_Encryption::OPENSSL_IV_DELIMITER, |
|
| 438 | + $encrypted_text, |
|
| 439 | + 2 |
|
| 440 | + ); |
|
| 441 | + // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
| 442 | + if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
| 443 | + return $this->m_decrypt($encrypted_text); |
|
| 444 | + } |
|
| 445 | + // decrypt it |
|
| 446 | + $decrypted_text = openssl_decrypt( |
|
| 447 | + $encrypted_components[0], |
|
| 448 | + $this->getCipherMethod($cipher_method), |
|
| 449 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD,$encryption_key), |
|
| 450 | + 0, |
|
| 451 | + $encrypted_components[1] |
|
| 452 | + ); |
|
| 453 | + $decrypted_text = trim($decrypted_text); |
|
| 454 | + return $decrypted_text; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Computes the digest hash value using the specified digest method. |
|
| 460 | + * If that digest method fails to produce a valid hash value, |
|
| 461 | + * then we'll grab the next digest method and recursively try again until something works. |
|
| 462 | + * |
|
| 463 | + * @param string $digest_method |
|
| 464 | + * @param string $encryption_key |
|
| 465 | + * @return string |
|
| 466 | + * @throws RuntimeException |
|
| 467 | + */ |
|
| 468 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = ''){ |
|
| 469 | + $encryption_key = $encryption_key !== '' |
|
| 470 | + ? $encryption_key |
|
| 471 | + : $this->get_encryption_key(); |
|
| 472 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
| 473 | + if ($digest_hash_value === false) { |
|
| 474 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 475 | + } |
|
| 476 | + return $digest_hash_value; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Returns the NEXT element in the $digest_methods array. |
|
| 483 | + * If the $digest_methods array is empty, then we populate it |
|
| 484 | + * with the available values returned from openssl_get_md_methods(). |
|
| 485 | + * |
|
| 486 | + * @return string |
|
| 487 | + * @throws \RuntimeException |
|
| 488 | + */ |
|
| 489 | + protected function getDigestMethod(){ |
|
| 490 | + $digest_method = prev($this->digest_methods); |
|
| 491 | + if (empty($this->digest_methods)) { |
|
| 492 | + $this->digest_methods = openssl_get_md_methods(); |
|
| 493 | + $digest_method = end($this->digest_methods); |
|
| 494 | + } |
|
| 495 | + if ($digest_method === false) { |
|
| 496 | + throw new RuntimeException( |
|
| 497 | + esc_html__( |
|
| 498 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 499 | + 'event_espresso' |
|
| 500 | + ) |
|
| 501 | + ); |
|
| 502 | + } |
|
| 503 | + return $digest_method; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 509 | + * |
|
| 510 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 511 | + * @param string $text_string the text to be decrypted |
|
| 512 | + * @return string |
|
| 513 | + */ |
|
| 514 | + protected function acme_encrypt($text_string = '') |
|
| 515 | + { |
|
| 516 | + // you give me nothing??? GET OUT ! |
|
| 517 | + if (empty($text_string)) { |
|
| 518 | + return $text_string; |
|
| 519 | + } |
|
| 520 | + $key_bits = str_split( |
|
| 521 | + str_pad( |
|
| 522 | + '', |
|
| 523 | + strlen($text_string), |
|
| 524 | + $this->get_encryption_key(), |
|
| 525 | + STR_PAD_RIGHT |
|
| 526 | + ) |
|
| 527 | + ); |
|
| 528 | + $string_bits = str_split($text_string); |
|
| 529 | + foreach ($string_bits as $k => $v) { |
|
| 530 | + $temp = ord($v) + ord($key_bits[$k]); |
|
| 531 | + $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 532 | + } |
|
| 533 | + $encrypted_text = implode('', $string_bits); |
|
| 534 | + $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
| 535 | + return $this->_use_base64_encode |
|
| 536 | + ? base64_encode($encrypted_text) |
|
| 537 | + : $encrypted_text; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 544 | + * |
|
| 545 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 546 | + * @param string $encrypted_text the text to be decrypted |
|
| 547 | + * @return string |
|
| 548 | + * @throws RuntimeException |
|
| 549 | + */ |
|
| 550 | + protected function acme_decrypt($encrypted_text = '') |
|
| 551 | + { |
|
| 552 | + // you give me nothing??? GET OUT ! |
|
| 553 | + if (empty($encrypted_text)) { |
|
| 554 | + return $encrypted_text; |
|
| 555 | + } |
|
| 556 | + // decode the data ? |
|
| 557 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 558 | + ? $this->base64_url_decode($encrypted_text) |
|
| 559 | + : $encrypted_text; |
|
| 560 | + if ( |
|
| 561 | + $this->_use_mcrypt |
|
| 562 | + && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
| 563 | + ){ |
|
| 564 | + return $this->m_decrypt($encrypted_text); |
|
| 565 | + } |
|
| 566 | + $encrypted_text = substr($encrypted_text, 0, -4); |
|
| 567 | + $key_bits = str_split( |
|
| 568 | + str_pad( |
|
| 569 | + '', |
|
| 570 | + strlen($encrypted_text), |
|
| 571 | + $this->get_encryption_key(), |
|
| 572 | + STR_PAD_RIGHT |
|
| 573 | + ) |
|
| 574 | + ); |
|
| 575 | + $string_bits = str_split($encrypted_text); |
|
| 576 | + foreach ($string_bits as $k => $v) { |
|
| 577 | + $temp = ord($v) - ord($key_bits[$k]); |
|
| 578 | + $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 579 | + } |
|
| 580 | + return implode('', $string_bits); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
| 587 | + * @param $string |
|
| 588 | + * @return bool |
|
| 589 | + */ |
|
| 590 | + protected function valid_base_64($string) |
|
| 591 | + { |
|
| 592 | + // ensure data is a string |
|
| 593 | + if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 594 | + return false; |
|
| 595 | + } |
|
| 596 | + $decoded = base64_decode($string, true); |
|
| 597 | + // Check if there is no invalid character in string |
|
| 598 | + if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 599 | + return false; |
|
| 600 | + } |
|
| 601 | + // Decode the string in strict mode and send the response |
|
| 602 | + if (! base64_decode($string, true)) { |
|
| 603 | + return false; |
|
| 604 | + } |
|
| 605 | + // Encode and compare it to original one |
|
| 606 | + return base64_encode($decoded) === $string; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * generate random string |
|
| 613 | + * |
|
| 614 | + * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 615 | + * @param int $length number of characters for random string |
|
| 616 | + * @return string |
|
| 617 | + */ |
|
| 618 | + public function generate_random_string($length = 40) |
|
| 619 | + { |
|
| 620 | + $iterations = ceil($length / 40); |
|
| 621 | + $random_string = ''; |
|
| 622 | + for ($i = 0; $i < $iterations; $i++) { |
|
| 623 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 624 | + } |
|
| 625 | + $random_string = substr($random_string, 0, $length); |
|
| 626 | + return $random_string; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * encrypts data using PHP's mcrypt functions |
|
| 633 | + * |
|
| 634 | + * @deprecated 4.9.39 |
|
| 635 | + * @param string $text_string |
|
| 636 | + * @internal param $string - the text to be encrypted |
|
| 637 | + * @return string |
|
| 638 | + * @throws RuntimeException |
|
| 639 | + */ |
|
| 640 | + protected function m_encrypt($text_string = '') |
|
| 641 | + { |
|
| 642 | + // you give me nothing??? GET OUT ! |
|
| 643 | + if (empty($text_string)) { |
|
| 644 | + return $text_string; |
|
| 645 | + } |
|
| 646 | + // get the initialization vector size |
|
| 647 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 648 | + // initialization vector |
|
| 649 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 650 | + if ($iv === false) { |
|
| 651 | + throw new RuntimeException( |
|
| 652 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 653 | + ); |
|
| 654 | + } |
|
| 655 | + // encrypt it |
|
| 656 | + $encrypted_text = mcrypt_encrypt( |
|
| 657 | + MCRYPT_RIJNDAEL_256, |
|
| 658 | + $this->get_encryption_key(), |
|
| 659 | + $text_string, |
|
| 660 | + MCRYPT_MODE_ECB, |
|
| 661 | + $iv |
|
| 662 | + ); |
|
| 663 | + // trim and maybe encode |
|
| 664 | + return $this->_use_base64_encode |
|
| 665 | + ? trim(base64_encode($encrypted_text)) |
|
| 666 | + : trim($encrypted_text); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * decrypts data that has been encrypted with PHP's mcrypt functions |
|
| 673 | + * |
|
| 674 | + * @deprecated 4.9.39 |
|
| 675 | + * @param string $encrypted_text the text to be decrypted |
|
| 676 | + * @return string |
|
| 677 | + * @throws RuntimeException |
|
| 678 | + */ |
|
| 679 | + protected function m_decrypt($encrypted_text = '') |
|
| 680 | + { |
|
| 681 | + // you give me nothing??? GET OUT ! |
|
| 682 | + if (empty($encrypted_text)) { |
|
| 683 | + return $encrypted_text; |
|
| 684 | + } |
|
| 685 | + // decode |
|
| 686 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 687 | + ? $this->base64_url_decode($encrypted_text) |
|
| 688 | + : $encrypted_text; |
|
| 689 | + // get the initialization vector size |
|
| 690 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 691 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 692 | + if ($iv === false) { |
|
| 693 | + throw new RuntimeException( |
|
| 694 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 695 | + ); |
|
| 696 | + } |
|
| 697 | + // decrypt it |
|
| 698 | + $decrypted_text = mcrypt_decrypt( |
|
| 699 | + MCRYPT_RIJNDAEL_256, |
|
| 700 | + $this->get_encryption_key(), |
|
| 701 | + $encrypted_text, |
|
| 702 | + MCRYPT_MODE_ECB, |
|
| 703 | + $iv |
|
| 704 | + ); |
|
| 705 | + $decrypted_text = trim($decrypted_text); |
|
| 706 | + return $decrypted_text; |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | 709 | } |
| 710 | 710 | /* End of file EE_Encryption.class.php */ |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | protected function __construct() |
| 100 | 100 | { |
| 101 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 101 | + if ( ! defined('ESPRESSO_ENCRYPT')) { |
|
| 102 | 102 | define('ESPRESSO_ENCRYPT', true); |
| 103 | 103 | } |
| 104 | 104 | if (extension_loaded('openssl')) { |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | public static function instance() |
| 122 | 122 | { |
| 123 | 123 | // check if class object is instantiated |
| 124 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 124 | + if ( ! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 125 | 125 | EE_Encryption::$_instance = new self(); |
| 126 | 126 | } |
| 127 | 127 | return EE_Encryption::$_instance; |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | $iv |
| 334 | 334 | ); |
| 335 | 335 | // append the initialization vector |
| 336 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 336 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER.$iv; |
|
| 337 | 337 | // trim and maybe encode |
| 338 | 338 | return $this->_use_base64_encode |
| 339 | 339 | ? trim(base64_encode($encrypted_text)) |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | */ |
| 357 | 357 | protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
| 358 | 358 | { |
| 359 | - if($this->cipher_method !== ''){ |
|
| 359 | + if ($this->cipher_method !== '') { |
|
| 360 | 360 | return $this->cipher_method; |
| 361 | 361 | } |
| 362 | 362 | // verify that the default cipher method can produce an initialization vector |
@@ -364,7 +364,7 @@ discard block |
||
| 364 | 364 | // nope? okay let's get what we found in the past to work |
| 365 | 365 | $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
| 366 | 366 | // oops... haven't tested available cipher methods yet |
| 367 | - if($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 367 | + if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 368 | 368 | $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
| 369 | 369 | } |
| 370 | 370 | } |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | // then grab the first item from the list |
| 391 | 391 | $cipher_method = reset($this->cipher_methods); |
| 392 | 392 | } |
| 393 | - if($cipher_method === false){ |
|
| 393 | + if ($cipher_method === false) { |
|
| 394 | 394 | throw new RuntimeException( |
| 395 | 395 | esc_html__( |
| 396 | 396 | 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | $decrypted_text = openssl_decrypt( |
| 447 | 447 | $encrypted_components[0], |
| 448 | 448 | $this->getCipherMethod($cipher_method), |
| 449 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD,$encryption_key), |
|
| 449 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 450 | 450 | 0, |
| 451 | 451 | $encrypted_components[1] |
| 452 | 452 | ); |
@@ -465,7 +465,7 @@ discard block |
||
| 465 | 465 | * @return string |
| 466 | 466 | * @throws RuntimeException |
| 467 | 467 | */ |
| 468 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = ''){ |
|
| 468 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') { |
|
| 469 | 469 | $encryption_key = $encryption_key !== '' |
| 470 | 470 | ? $encryption_key |
| 471 | 471 | : $this->get_encryption_key(); |
@@ -486,7 +486,7 @@ discard block |
||
| 486 | 486 | * @return string |
| 487 | 487 | * @throws \RuntimeException |
| 488 | 488 | */ |
| 489 | - protected function getDigestMethod(){ |
|
| 489 | + protected function getDigestMethod() { |
|
| 490 | 490 | $digest_method = prev($this->digest_methods); |
| 491 | 491 | if (empty($this->digest_methods)) { |
| 492 | 492 | $this->digest_methods = openssl_get_md_methods(); |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | if ( |
| 561 | 561 | $this->_use_mcrypt |
| 562 | 562 | && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
| 563 | - ){ |
|
| 563 | + ) { |
|
| 564 | 564 | return $this->m_decrypt($encrypted_text); |
| 565 | 565 | } |
| 566 | 566 | $encrypted_text = substr($encrypted_text, 0, -4); |
@@ -590,16 +590,16 @@ discard block |
||
| 590 | 590 | protected function valid_base_64($string) |
| 591 | 591 | { |
| 592 | 592 | // ensure data is a string |
| 593 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 593 | + if ( ! is_string($string) || ! $this->_use_base64_encode) { |
|
| 594 | 594 | return false; |
| 595 | 595 | } |
| 596 | 596 | $decoded = base64_decode($string, true); |
| 597 | 597 | // Check if there is no invalid character in string |
| 598 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 598 | + if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 599 | 599 | return false; |
| 600 | 600 | } |
| 601 | 601 | // Decode the string in strict mode and send the response |
| 602 | - if (! base64_decode($string, true)) { |
|
| 602 | + if ( ! base64_decode($string, true)) { |
|
| 603 | 603 | return false; |
| 604 | 604 | } |
| 605 | 605 | // Encode and compare it to original one |
@@ -620,7 +620,7 @@ discard block |
||
| 620 | 620 | $iterations = ceil($length / 40); |
| 621 | 621 | $random_string = ''; |
| 622 | 622 | for ($i = 0; $i < $iterations; $i++) { |
| 623 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 623 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
| 624 | 624 | } |
| 625 | 625 | $random_string = substr($random_string, 0, $length); |
| 626 | 626 | return $random_string; |