| 1 |
<?php |
| 2 |
/** |
| 3 |
* Based on the code from the following packages: |
| 4 |
* Class Google\Site_Kit\Core\Storage\Data_Encryption |
| 5 |
* |
| 6 |
* @package Google\Site_Kit |
| 7 |
* @copyright 2019 Google LLC |
| 8 |
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 |
* @link https://sitekit.withgoogle.com |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SureTriggers\Support; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class responsible for encrypting and decrypting data. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* @access private |
| 19 |
* @ignore |
| 20 |
*/ |
| 21 |
class Encryption { |
| 22 |
/** |
| 23 |
* Key to use for encryption. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $key; |
| 29 |
|
| 30 |
/** |
| 31 |
* Salt to use for encryption. |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $salt; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
final public function __construct() { |
| 44 |
$this->key = $this->get_default_key(); |
| 45 |
$this->salt = $this->get_default_salt(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Encrypts a value. |
| 50 |
* |
| 51 |
* If a user-based key is set, that key is used. Otherwise the default key is used. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @param string $value Value to encrypt. |
| 56 |
* @return string|bool Encrypted value, or false on failure. |
| 57 |
*/ |
| 58 |
protected function encrypt( $value ) { |
| 59 |
if ( ! extension_loaded( 'openssl' ) ) { |
| 60 |
return $value; |
| 61 |
} |
| 62 |
$method = 'aes-256-ctr'; |
| 63 |
$ivlen = openssl_cipher_iv_length( $method ); |
| 64 |
$iv = openssl_random_pseudo_bytes( $ivlen ); |
| 65 |
|
| 66 |
$raw_value = openssl_encrypt( $value . $this->salt, $method, $this->key, 0, $iv ); |
| 67 |
if ( ! $raw_value ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
return base64_encode( $iv . $raw_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Decrypts a value. |
| 76 |
* |
| 77 |
* If a user-based key is set, that key is used. Otherwise the default key is used. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param string $raw_value Value to decrypt. |
| 82 |
* @return string|bool Decrypted value, or false on failure. |
| 83 |
*/ |
| 84 |
protected function decrypt( $raw_value ) { |
| 85 |
if ( ! extension_loaded( 'openssl' ) ) { |
| 86 |
return $raw_value; |
| 87 |
} |
| 88 |
|
| 89 |
$raw_value = base64_decode( $raw_value, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 90 |
|
| 91 |
$method = 'aes-256-ctr'; |
| 92 |
$ivlen = openssl_cipher_iv_length( $method ); |
| 93 |
$iv = substr( $raw_value, 0, $ivlen ); |
| 94 |
|
| 95 |
$raw_value = substr( $raw_value, $ivlen ); |
| 96 |
|
| 97 |
$value = openssl_decrypt( $raw_value, $method, $this->key, 0, $iv ); |
| 98 |
if ( ! $value || substr( $value, - strlen( $this->salt ) ) !== $this->salt ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
return substr( $value, 0, - strlen( $this->salt ) ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Gets the default encryption key to use. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
* |
| 109 |
* @return string Default (not user-based) encryption key. |
| 110 |
*/ |
| 111 |
protected function get_default_key() { |
| 112 |
|
| 113 |
if ( defined( 'SURETRIGGERS_ENCRYPTION_KEY' ) && '' !== SURETRIGGERS_ENCRYPTION_KEY ) { |
| 114 |
return SURETRIGGERS_ENCRYPTION_KEY; |
| 115 |
} |
| 116 |
|
| 117 |
if ( defined( 'LOGGED_IN_KEY' ) && '' !== LOGGED_IN_KEY ) { |
| 118 |
return LOGGED_IN_KEY; |
| 119 |
} |
| 120 |
|
| 121 |
// If this is reached, you're either not on a live site or have a serious security issue. |
| 122 |
return 'this-is-fallback-key-for-encryption'; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Gets the default encryption salt to use. |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* |
| 130 |
* @return string Encryption salt. |
| 131 |
*/ |
| 132 |
private function get_default_salt() { |
| 133 |
if ( defined( 'SURETRIGGERS_ENCRYPTION_SALT' ) && '' !== SURETRIGGERS_ENCRYPTION_SALT ) { |
| 134 |
return SURETRIGGERS_ENCRYPTION_SALT; |
| 135 |
} |
| 136 |
|
| 137 |
if ( defined( 'LOGGED_IN_SALT' ) && '' !== LOGGED_IN_SALT ) { |
| 138 |
return LOGGED_IN_SALT; |
| 139 |
} |
| 140 |
|
| 141 |
// If this is reached, you're either not on a live site or have a serious security issue. |
| 142 |
return 'this-is-fallback-salt-for-encryption'; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Static Facade Accessor |
| 147 |
* |
| 148 |
* @param string $method Method to call. |
| 149 |
* @param mixed $params Method params. |
| 150 |
* |
| 151 |
* @return mixed |
| 152 |
*/ |
| 153 |
public static function __callStatic( $method, $params ) { |
| 154 |
/** |
| 155 |
* |
| 156 |
* Ignore line |
| 157 |
* |
| 158 |
* @phpstan-ignore-next-line |
| 159 |
*/ |
| 160 |
return call_user_func_array( [ new static(), $method ], $params ); |
| 161 |
} |
| 162 |
} |
| 163 |
|