@@ -12,56 +12,56 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface EncryptionKeyManagerInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * add an encryption key |
|
| 17 | - * |
|
| 18 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
| 19 | - * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
| 20 | - * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
| 21 | - * @return bool |
|
| 22 | - */ |
|
| 23 | - public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false); |
|
| 15 | + /** |
|
| 16 | + * add an encryption key |
|
| 17 | + * |
|
| 18 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
| 19 | + * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
| 20 | + * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
| 21 | + * @return bool |
|
| 22 | + */ |
|
| 23 | + public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false); |
|
| 24 | 24 | |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * returns cryptographically secure passphrase. will use default if necessary |
|
| 28 | - * |
|
| 29 | - * @param string $encryption_key_identifier - used for saving encryption key. will use default if necessary |
|
| 30 | - * @param string $generate - will generate a new key if the requested one does not exist |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function getEncryptionKey($encryption_key_identifier = '', $generate = false); |
|
| 26 | + /** |
|
| 27 | + * returns cryptographically secure passphrase. will use default if necessary |
|
| 28 | + * |
|
| 29 | + * @param string $encryption_key_identifier - used for saving encryption key. will use default if necessary |
|
| 30 | + * @param string $generate - will generate a new key if the requested one does not exist |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function getEncryptionKey($encryption_key_identifier = '', $generate = false); |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * creates a new encryption key |
|
| 38 | - * |
|
| 39 | - * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function generateEncryptionKey($strong = true); |
|
| 36 | + /** |
|
| 37 | + * creates a new encryption key |
|
| 38 | + * |
|
| 39 | + * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function generateEncryptionKey($strong = true); |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function bitDepth(); |
|
| 45 | + /** |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function bitDepth(); |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param int $bit_depth options are 128, 192, or 256 |
|
| 53 | - */ |
|
| 54 | - public function setBitDepth($bit_depth); |
|
| 51 | + /** |
|
| 52 | + * @param int $bit_depth options are 128, 192, or 256 |
|
| 53 | + */ |
|
| 54 | + public function setBitDepth($bit_depth); |
|
| 55 | 55 | |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return int |
|
| 59 | - */ |
|
| 60 | - public function keyLength(); |
|
| 57 | + /** |
|
| 58 | + * @return int |
|
| 59 | + */ |
|
| 60 | + public function keyLength(); |
|
| 61 | 61 | |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @param int $key_length |
|
| 65 | - */ |
|
| 66 | - public function setKeyLength($key_length); |
|
| 63 | + /** |
|
| 64 | + * @param int $key_length |
|
| 65 | + */ |
|
| 66 | + public function setKeyLength($key_length); |
|
| 67 | 67 | } |
@@ -16,255 +16,255 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class EncryptionKeyManager implements EncryptionKeyManagerInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @var Base64Encoder |
|
| 21 | - */ |
|
| 22 | - protected $base64_encoder; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * name used for a default encryption key in case no others are set |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private $default_encryption_key_id; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * name used for saving encryption keys to the wp_options table |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - private $encryption_keys_option_name; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - private $encryption_keys = null; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * number of bits used when generating cryptographically secure keys |
|
| 45 | - * |
|
| 46 | - * @var int |
|
| 47 | - */ |
|
| 48 | - private $bit_depth = 128; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var int[] |
|
| 52 | - */ |
|
| 53 | - private $bit_depth_options = [64, 128, 192, 256]; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * number of characters used when generating cryptographically weak keys |
|
| 57 | - * |
|
| 58 | - * @var int |
|
| 59 | - */ |
|
| 60 | - private $key_length = 40; |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param Base64Encoder $base64_encoder |
|
| 65 | - * @param string $default_encryption_key_id |
|
| 66 | - * @param string $encryption_keys_option_name |
|
| 67 | - */ |
|
| 68 | - public function __construct(Base64Encoder $base64_encoder, $default_encryption_key_id, $encryption_keys_option_name) |
|
| 69 | - { |
|
| 70 | - $this->base64_encoder = $base64_encoder; |
|
| 71 | - $this->default_encryption_key_id = $default_encryption_key_id; |
|
| 72 | - $this->encryption_keys_option_name = $encryption_keys_option_name; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * add an encryption key |
|
| 78 | - * |
|
| 79 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
| 80 | - * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
| 81 | - * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
| 82 | - * @return bool |
|
| 83 | - * @throws Exception |
|
| 84 | - */ |
|
| 85 | - public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false) |
|
| 86 | - { |
|
| 87 | - // ensure keys are loaded |
|
| 88 | - $this->retrieveEncryptionKeys(); |
|
| 89 | - $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
| 90 | - if (isset($this->encryption_keys[ $encryption_key_identifier ]) && ! $overwrite) { |
|
| 91 | - // WOAH!!! that key already exists and we don't want to overwrite it |
|
| 92 | - throw new RuntimeException( |
|
| 93 | - sprintf( |
|
| 94 | - esc_html__( |
|
| 95 | - 'The "%1$s" encryption key already exists and can not be overwritten because previously encrypted values would no longer be capable of being decrypted.', |
|
| 96 | - 'event_espresso' |
|
| 97 | - ), |
|
| 98 | - $encryption_key_identifier |
|
| 99 | - ) |
|
| 100 | - ); |
|
| 101 | - } |
|
| 102 | - $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
| 103 | - return $this->saveEncryptionKeys(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * returns cryptographically secure passphrase. will use default if necessary |
|
| 109 | - * |
|
| 110 | - * @param string $encryption_key_identifier - used for saving encryption key. will use default if necessary |
|
| 111 | - * @param string $generate - will generate a new key if the requested one does not exist |
|
| 112 | - * @return string |
|
| 113 | - * @throws Exception |
|
| 114 | - * @throws OutOfBoundsException |
|
| 115 | - */ |
|
| 116 | - public function getEncryptionKey($encryption_key_identifier = '', $generate = true) |
|
| 117 | - { |
|
| 118 | - $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
| 119 | - // ensure keys are loaded |
|
| 120 | - $this->retrieveEncryptionKeys(); |
|
| 121 | - // if encryption key has not been set |
|
| 122 | - if (! isset($this->encryption_keys[ $encryption_key_identifier ])) { |
|
| 123 | - if ($generate) { |
|
| 124 | - $this->addEncryptionKey($encryption_key_identifier); |
|
| 125 | - } else { |
|
| 126 | - throw new OutOfBoundsException( |
|
| 127 | - sprintf( |
|
| 128 | - esc_html__('The "%1$s" encryption key was not found or is invalid.', 'event_espresso'), |
|
| 129 | - $encryption_key_identifier |
|
| 130 | - ) |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $this->encryption_keys[ $encryption_key_identifier ]; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * creates a new encryption key |
|
| 140 | - * |
|
| 141 | - * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
| 142 | - * @return string |
|
| 143 | - * @throws Exception |
|
| 144 | - */ |
|
| 145 | - public function generateEncryptionKey($strong = true) |
|
| 146 | - { |
|
| 147 | - return $strong && PHP_VERSION_ID >= 70100 |
|
| 148 | - ? $this->generateStrongEncryptionKey() |
|
| 149 | - : $this->generateWeakEncryptionKey(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * creates a new cryptographically secure encryption key |
|
| 155 | - * |
|
| 156 | - * @return string |
|
| 157 | - * @throws Exception |
|
| 158 | - */ |
|
| 159 | - protected function generateStrongEncryptionKey() |
|
| 160 | - { |
|
| 161 | - // bit_depth needs to be divided by 8 to convert to bytes |
|
| 162 | - return $this->base64_encoder->encodeString(random_bytes($this->bit_depth / 8)); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * creates a new encryption key that should not be trusted to be cryptographically secure |
|
| 168 | - * |
|
| 169 | - * @return string |
|
| 170 | - * @throws Exception |
|
| 171 | - */ |
|
| 172 | - protected function generateWeakEncryptionKey() |
|
| 173 | - { |
|
| 174 | - // @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 175 | - $iterations = ceil($this->key_length / 40); |
|
| 176 | - $random_string = ''; |
|
| 177 | - for ($i = 0; $i < $iterations; $i++) { |
|
| 178 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 179 | - } |
|
| 180 | - $random_string = (string) substr($random_string, 0, $this->key_length); |
|
| 181 | - return $this->base64_encoder->encodeString($random_string); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return int |
|
| 187 | - */ |
|
| 188 | - public function bitDepth() |
|
| 189 | - { |
|
| 190 | - return $this->bit_depth; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param int $bit_depth options are 64, 128, 192, or 256 |
|
| 196 | - */ |
|
| 197 | - public function setBitDepth($bit_depth) |
|
| 198 | - { |
|
| 199 | - $bit_depth = absint($bit_depth); |
|
| 200 | - $this->bit_depth = in_array($bit_depth, $this->bit_depth_options, true) ? $bit_depth : 128; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @return int |
|
| 206 | - */ |
|
| 207 | - public function keyLength() |
|
| 208 | - { |
|
| 209 | - return $this->key_length; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @param int $key_length |
|
| 215 | - */ |
|
| 216 | - public function setKeyLength($key_length) |
|
| 217 | - { |
|
| 218 | - // let's not let the key length go below 8 or above 128 |
|
| 219 | - $this->key_length = min(max(absint($key_length), 8), 128); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * retrieves encryption keys from db |
|
| 225 | - * |
|
| 226 | - * @return array |
|
| 227 | - * @throws Exception |
|
| 228 | - * @throws RuntimeException |
|
| 229 | - */ |
|
| 230 | - protected function retrieveEncryptionKeys() |
|
| 231 | - { |
|
| 232 | - // if encryption key has not been set |
|
| 233 | - if (empty($this->encryption_keys)) { |
|
| 234 | - // retrieve encryption_key from db |
|
| 235 | - $this->encryption_keys = get_option($this->encryption_keys_option_name, null); |
|
| 236 | - // WHAT?? No encryption keys in the db ?? |
|
| 237 | - if ($this->encryption_keys === null) { |
|
| 238 | - $this->encryption_keys = []; |
|
| 239 | - // let's create the default key and save it |
|
| 240 | - $new_key = $this->generateEncryptionKey(); |
|
| 241 | - $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
| 242 | - if (! $this->saveEncryptionKeys(true)) { |
|
| 243 | - throw new RuntimeException( |
|
| 244 | - sprintf( |
|
| 245 | - esc_html__( |
|
| 246 | - 'Failed to save the "%1$s" encryption keys array to the database.', |
|
| 247 | - 'event_espresso' |
|
| 248 | - ), |
|
| 249 | - $this->encryption_keys_option_name |
|
| 250 | - ) |
|
| 251 | - ); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - return $this->encryption_keys; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * saves encryption keys from db |
|
| 261 | - * |
|
| 262 | - * @return bool |
|
| 263 | - */ |
|
| 264 | - protected function saveEncryptionKeys($initialize = false) |
|
| 265 | - { |
|
| 266 | - return $initialize |
|
| 267 | - ? add_option($this->encryption_keys_option_name, $this->encryption_keys, '', false) |
|
| 268 | - : update_option($this->encryption_keys_option_name, $this->encryption_keys, false); |
|
| 269 | - } |
|
| 19 | + /** |
|
| 20 | + * @var Base64Encoder |
|
| 21 | + */ |
|
| 22 | + protected $base64_encoder; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * name used for a default encryption key in case no others are set |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private $default_encryption_key_id; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * name used for saving encryption keys to the wp_options table |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + private $encryption_keys_option_name; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + private $encryption_keys = null; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * number of bits used when generating cryptographically secure keys |
|
| 45 | + * |
|
| 46 | + * @var int |
|
| 47 | + */ |
|
| 48 | + private $bit_depth = 128; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var int[] |
|
| 52 | + */ |
|
| 53 | + private $bit_depth_options = [64, 128, 192, 256]; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * number of characters used when generating cryptographically weak keys |
|
| 57 | + * |
|
| 58 | + * @var int |
|
| 59 | + */ |
|
| 60 | + private $key_length = 40; |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param Base64Encoder $base64_encoder |
|
| 65 | + * @param string $default_encryption_key_id |
|
| 66 | + * @param string $encryption_keys_option_name |
|
| 67 | + */ |
|
| 68 | + public function __construct(Base64Encoder $base64_encoder, $default_encryption_key_id, $encryption_keys_option_name) |
|
| 69 | + { |
|
| 70 | + $this->base64_encoder = $base64_encoder; |
|
| 71 | + $this->default_encryption_key_id = $default_encryption_key_id; |
|
| 72 | + $this->encryption_keys_option_name = $encryption_keys_option_name; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * add an encryption key |
|
| 78 | + * |
|
| 79 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
| 80 | + * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
| 81 | + * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
| 82 | + * @return bool |
|
| 83 | + * @throws Exception |
|
| 84 | + */ |
|
| 85 | + public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false) |
|
| 86 | + { |
|
| 87 | + // ensure keys are loaded |
|
| 88 | + $this->retrieveEncryptionKeys(); |
|
| 89 | + $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
| 90 | + if (isset($this->encryption_keys[ $encryption_key_identifier ]) && ! $overwrite) { |
|
| 91 | + // WOAH!!! that key already exists and we don't want to overwrite it |
|
| 92 | + throw new RuntimeException( |
|
| 93 | + sprintf( |
|
| 94 | + esc_html__( |
|
| 95 | + 'The "%1$s" encryption key already exists and can not be overwritten because previously encrypted values would no longer be capable of being decrypted.', |
|
| 96 | + 'event_espresso' |
|
| 97 | + ), |
|
| 98 | + $encryption_key_identifier |
|
| 99 | + ) |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | + $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
| 103 | + return $this->saveEncryptionKeys(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * returns cryptographically secure passphrase. will use default if necessary |
|
| 109 | + * |
|
| 110 | + * @param string $encryption_key_identifier - used for saving encryption key. will use default if necessary |
|
| 111 | + * @param string $generate - will generate a new key if the requested one does not exist |
|
| 112 | + * @return string |
|
| 113 | + * @throws Exception |
|
| 114 | + * @throws OutOfBoundsException |
|
| 115 | + */ |
|
| 116 | + public function getEncryptionKey($encryption_key_identifier = '', $generate = true) |
|
| 117 | + { |
|
| 118 | + $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
| 119 | + // ensure keys are loaded |
|
| 120 | + $this->retrieveEncryptionKeys(); |
|
| 121 | + // if encryption key has not been set |
|
| 122 | + if (! isset($this->encryption_keys[ $encryption_key_identifier ])) { |
|
| 123 | + if ($generate) { |
|
| 124 | + $this->addEncryptionKey($encryption_key_identifier); |
|
| 125 | + } else { |
|
| 126 | + throw new OutOfBoundsException( |
|
| 127 | + sprintf( |
|
| 128 | + esc_html__('The "%1$s" encryption key was not found or is invalid.', 'event_espresso'), |
|
| 129 | + $encryption_key_identifier |
|
| 130 | + ) |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $this->encryption_keys[ $encryption_key_identifier ]; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * creates a new encryption key |
|
| 140 | + * |
|
| 141 | + * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
| 142 | + * @return string |
|
| 143 | + * @throws Exception |
|
| 144 | + */ |
|
| 145 | + public function generateEncryptionKey($strong = true) |
|
| 146 | + { |
|
| 147 | + return $strong && PHP_VERSION_ID >= 70100 |
|
| 148 | + ? $this->generateStrongEncryptionKey() |
|
| 149 | + : $this->generateWeakEncryptionKey(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * creates a new cryptographically secure encryption key |
|
| 155 | + * |
|
| 156 | + * @return string |
|
| 157 | + * @throws Exception |
|
| 158 | + */ |
|
| 159 | + protected function generateStrongEncryptionKey() |
|
| 160 | + { |
|
| 161 | + // bit_depth needs to be divided by 8 to convert to bytes |
|
| 162 | + return $this->base64_encoder->encodeString(random_bytes($this->bit_depth / 8)); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * creates a new encryption key that should not be trusted to be cryptographically secure |
|
| 168 | + * |
|
| 169 | + * @return string |
|
| 170 | + * @throws Exception |
|
| 171 | + */ |
|
| 172 | + protected function generateWeakEncryptionKey() |
|
| 173 | + { |
|
| 174 | + // @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 175 | + $iterations = ceil($this->key_length / 40); |
|
| 176 | + $random_string = ''; |
|
| 177 | + for ($i = 0; $i < $iterations; $i++) { |
|
| 178 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 179 | + } |
|
| 180 | + $random_string = (string) substr($random_string, 0, $this->key_length); |
|
| 181 | + return $this->base64_encoder->encodeString($random_string); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return int |
|
| 187 | + */ |
|
| 188 | + public function bitDepth() |
|
| 189 | + { |
|
| 190 | + return $this->bit_depth; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param int $bit_depth options are 64, 128, 192, or 256 |
|
| 196 | + */ |
|
| 197 | + public function setBitDepth($bit_depth) |
|
| 198 | + { |
|
| 199 | + $bit_depth = absint($bit_depth); |
|
| 200 | + $this->bit_depth = in_array($bit_depth, $this->bit_depth_options, true) ? $bit_depth : 128; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @return int |
|
| 206 | + */ |
|
| 207 | + public function keyLength() |
|
| 208 | + { |
|
| 209 | + return $this->key_length; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @param int $key_length |
|
| 215 | + */ |
|
| 216 | + public function setKeyLength($key_length) |
|
| 217 | + { |
|
| 218 | + // let's not let the key length go below 8 or above 128 |
|
| 219 | + $this->key_length = min(max(absint($key_length), 8), 128); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * retrieves encryption keys from db |
|
| 225 | + * |
|
| 226 | + * @return array |
|
| 227 | + * @throws Exception |
|
| 228 | + * @throws RuntimeException |
|
| 229 | + */ |
|
| 230 | + protected function retrieveEncryptionKeys() |
|
| 231 | + { |
|
| 232 | + // if encryption key has not been set |
|
| 233 | + if (empty($this->encryption_keys)) { |
|
| 234 | + // retrieve encryption_key from db |
|
| 235 | + $this->encryption_keys = get_option($this->encryption_keys_option_name, null); |
|
| 236 | + // WHAT?? No encryption keys in the db ?? |
|
| 237 | + if ($this->encryption_keys === null) { |
|
| 238 | + $this->encryption_keys = []; |
|
| 239 | + // let's create the default key and save it |
|
| 240 | + $new_key = $this->generateEncryptionKey(); |
|
| 241 | + $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
| 242 | + if (! $this->saveEncryptionKeys(true)) { |
|
| 243 | + throw new RuntimeException( |
|
| 244 | + sprintf( |
|
| 245 | + esc_html__( |
|
| 246 | + 'Failed to save the "%1$s" encryption keys array to the database.', |
|
| 247 | + 'event_espresso' |
|
| 248 | + ), |
|
| 249 | + $this->encryption_keys_option_name |
|
| 250 | + ) |
|
| 251 | + ); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + return $this->encryption_keys; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * saves encryption keys from db |
|
| 261 | + * |
|
| 262 | + * @return bool |
|
| 263 | + */ |
|
| 264 | + protected function saveEncryptionKeys($initialize = false) |
|
| 265 | + { |
|
| 266 | + return $initialize |
|
| 267 | + ? add_option($this->encryption_keys_option_name, $this->encryption_keys, '', false) |
|
| 268 | + : update_option($this->encryption_keys_option_name, $this->encryption_keys, false); |
|
| 269 | + } |
|
| 270 | 270 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | // ensure keys are loaded |
| 88 | 88 | $this->retrieveEncryptionKeys(); |
| 89 | 89 | $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
| 90 | - if (isset($this->encryption_keys[ $encryption_key_identifier ]) && ! $overwrite) { |
|
| 90 | + if (isset($this->encryption_keys[$encryption_key_identifier]) && ! $overwrite) { |
|
| 91 | 91 | // WOAH!!! that key already exists and we don't want to overwrite it |
| 92 | 92 | throw new RuntimeException( |
| 93 | 93 | sprintf( |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | ) |
| 100 | 100 | ); |
| 101 | 101 | } |
| 102 | - $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
| 102 | + $this->encryption_keys[$encryption_key_identifier] = $encryption_key ?: $this->generateEncryptionKey(); |
|
| 103 | 103 | return $this->saveEncryptionKeys(); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | // ensure keys are loaded |
| 120 | 120 | $this->retrieveEncryptionKeys(); |
| 121 | 121 | // if encryption key has not been set |
| 122 | - if (! isset($this->encryption_keys[ $encryption_key_identifier ])) { |
|
| 122 | + if ( ! isset($this->encryption_keys[$encryption_key_identifier])) { |
|
| 123 | 123 | if ($generate) { |
| 124 | 124 | $this->addEncryptionKey($encryption_key_identifier); |
| 125 | 125 | } else { |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | ); |
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | - return $this->encryption_keys[ $encryption_key_identifier ]; |
|
| 134 | + return $this->encryption_keys[$encryption_key_identifier]; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | $iterations = ceil($this->key_length / 40); |
| 176 | 176 | $random_string = ''; |
| 177 | 177 | for ($i = 0; $i < $iterations; $i++) { |
| 178 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 178 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
| 179 | 179 | } |
| 180 | 180 | $random_string = (string) substr($random_string, 0, $this->key_length); |
| 181 | 181 | return $this->base64_encoder->encodeString($random_string); |
@@ -238,8 +238,8 @@ discard block |
||
| 238 | 238 | $this->encryption_keys = []; |
| 239 | 239 | // let's create the default key and save it |
| 240 | 240 | $new_key = $this->generateEncryptionKey(); |
| 241 | - $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
| 242 | - if (! $this->saveEncryptionKeys(true)) { |
|
| 241 | + $this->encryption_keys[$this->default_encryption_key_id] = $new_key; |
|
| 242 | + if ( ! $this->saveEncryptionKeys(true)) { |
|
| 243 | 243 | throw new RuntimeException( |
| 244 | 244 | sprintf( |
| 245 | 245 | esc_html__( |
@@ -18,155 +18,155 @@ |
||
| 18 | 18 | class OpenSSLv1 extends OpenSSL |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * name used for a default encryption key in case no others are set |
|
| 23 | - */ |
|
| 24 | - const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v1_key'; |
|
| 21 | + /** |
|
| 22 | + * name used for a default encryption key in case no others are set |
|
| 23 | + */ |
|
| 24 | + const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v1_key'; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * name used for saving encryption keys to the wp_options table |
|
| 28 | - */ |
|
| 29 | - const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v1_encryption_keys'; |
|
| 26 | + /** |
|
| 27 | + * name used for saving encryption keys to the wp_options table |
|
| 28 | + */ |
|
| 29 | + const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v1_encryption_keys'; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * the OPENSSL cipher method used |
|
| 33 | - */ |
|
| 34 | - const CIPHER_METHOD = 'aes-128-cbc'; |
|
| 31 | + /** |
|
| 32 | + * the OPENSSL cipher method used |
|
| 33 | + */ |
|
| 34 | + const CIPHER_METHOD = 'aes-128-cbc'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * WP "options_name" used to store a verified available cipher method |
|
| 38 | - */ |
|
| 39 | - const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v1_cipher_method'; |
|
| 36 | + /** |
|
| 37 | + * WP "options_name" used to store a verified available cipher method |
|
| 38 | + */ |
|
| 39 | + const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v1_cipher_method'; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
| 44 | - * - extend this class |
|
| 45 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 46 | - * - pass those to this constructor, like so: |
|
| 47 | - * |
|
| 48 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
| 49 | - * parent::__construct( |
|
| 50 | - * $base64_encoder, |
|
| 51 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
| 52 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
| 53 | - * '7.1.0' |
|
| 54 | - * ); |
|
| 55 | - * } |
|
| 56 | - * |
|
| 57 | - * @param Base64Encoder $base64_encoder |
|
| 58 | - * @param CipherMethod|null $cipher_method |
|
| 59 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 60 | - * @param string $min_php_version defaults to 5.3.0 (when openssl added) |
|
| 61 | - */ |
|
| 62 | - public function __construct( |
|
| 63 | - Base64Encoder $base64_encoder, |
|
| 64 | - CipherMethod $cipher_method = null, |
|
| 65 | - EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
| 66 | - $min_php_version = '5.3.0' |
|
| 67 | - ) { |
|
| 68 | - parent::__construct( |
|
| 69 | - $base64_encoder, |
|
| 70 | - $cipher_method instanceof CipherMethod |
|
| 71 | - ? $cipher_method |
|
| 72 | - : new CipherMethod( |
|
| 73 | - OpenSSLv1::CIPHER_METHOD, |
|
| 74 | - OpenSSLv1::CIPHER_METHOD_OPTION_NAME |
|
| 75 | - ), |
|
| 76 | - $encryption_key_manager instanceof EncryptionKeyManager |
|
| 77 | - ? $encryption_key_manager |
|
| 78 | - : new EncryptionKeyManager( |
|
| 79 | - $base64_encoder, |
|
| 80 | - OpenSSLv1::DEFAULT_ENCRYPTION_KEY_ID, |
|
| 81 | - OpenSSLv1::ENCRYPTION_KEYS_OPTION_NAME |
|
| 82 | - ), |
|
| 83 | - $min_php_version |
|
| 84 | - ); |
|
| 85 | - } |
|
| 42 | + /** |
|
| 43 | + * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
| 44 | + * - extend this class |
|
| 45 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 46 | + * - pass those to this constructor, like so: |
|
| 47 | + * |
|
| 48 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
| 49 | + * parent::__construct( |
|
| 50 | + * $base64_encoder, |
|
| 51 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
| 52 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
| 53 | + * '7.1.0' |
|
| 54 | + * ); |
|
| 55 | + * } |
|
| 56 | + * |
|
| 57 | + * @param Base64Encoder $base64_encoder |
|
| 58 | + * @param CipherMethod|null $cipher_method |
|
| 59 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 60 | + * @param string $min_php_version defaults to 5.3.0 (when openssl added) |
|
| 61 | + */ |
|
| 62 | + public function __construct( |
|
| 63 | + Base64Encoder $base64_encoder, |
|
| 64 | + CipherMethod $cipher_method = null, |
|
| 65 | + EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
| 66 | + $min_php_version = '5.3.0' |
|
| 67 | + ) { |
|
| 68 | + parent::__construct( |
|
| 69 | + $base64_encoder, |
|
| 70 | + $cipher_method instanceof CipherMethod |
|
| 71 | + ? $cipher_method |
|
| 72 | + : new CipherMethod( |
|
| 73 | + OpenSSLv1::CIPHER_METHOD, |
|
| 74 | + OpenSSLv1::CIPHER_METHOD_OPTION_NAME |
|
| 75 | + ), |
|
| 76 | + $encryption_key_manager instanceof EncryptionKeyManager |
|
| 77 | + ? $encryption_key_manager |
|
| 78 | + : new EncryptionKeyManager( |
|
| 79 | + $base64_encoder, |
|
| 80 | + OpenSSLv1::DEFAULT_ENCRYPTION_KEY_ID, |
|
| 81 | + OpenSSLv1::ENCRYPTION_KEYS_OPTION_NAME |
|
| 82 | + ), |
|
| 83 | + $min_php_version |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * encrypts data |
|
| 90 | - * |
|
| 91 | - * @param string $text_to_encrypt - the text to be encrypted |
|
| 92 | - * @param string $encryption_key_identifier - cryptographically secure passphrase. will generate if necessary |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = '') |
|
| 96 | - { |
|
| 97 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 98 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 99 | - // get initialization vector size |
|
| 100 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 101 | - // generate initialization vector. |
|
| 102 | - // The second parameter ("crypto_strong") is passed by reference, |
|
| 103 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 104 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 105 | - $iv = openssl_random_pseudo_bytes($iv_length, $is_strong); |
|
| 106 | - if ($iv === false || $is_strong === false) { |
|
| 107 | - throw new RuntimeException( |
|
| 108 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - $key = $this->getDigestHashValue($encryption_key); |
|
| 112 | - // encrypt it |
|
| 113 | - $encrypted_text = openssl_encrypt( |
|
| 114 | - $this->base64_encoder->encodeString($text_to_encrypt), // encode to remove special characters |
|
| 115 | - $cipher_method, |
|
| 116 | - $key, |
|
| 117 | - 0, |
|
| 118 | - $iv |
|
| 119 | - ); |
|
| 120 | - $this->validateEncryption($encrypted_text); |
|
| 121 | - $encrypted_text = trim($encrypted_text); |
|
| 122 | - // hash the raw encrypted text |
|
| 123 | - $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
|
| 124 | - // concatenate everything into one big string and encode it again |
|
| 125 | - return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
| 126 | - } |
|
| 88 | + /** |
|
| 89 | + * encrypts data |
|
| 90 | + * |
|
| 91 | + * @param string $text_to_encrypt - the text to be encrypted |
|
| 92 | + * @param string $encryption_key_identifier - cryptographically secure passphrase. will generate if necessary |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = '') |
|
| 96 | + { |
|
| 97 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 98 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 99 | + // get initialization vector size |
|
| 100 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 101 | + // generate initialization vector. |
|
| 102 | + // The second parameter ("crypto_strong") is passed by reference, |
|
| 103 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 104 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 105 | + $iv = openssl_random_pseudo_bytes($iv_length, $is_strong); |
|
| 106 | + if ($iv === false || $is_strong === false) { |
|
| 107 | + throw new RuntimeException( |
|
| 108 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + $key = $this->getDigestHashValue($encryption_key); |
|
| 112 | + // encrypt it |
|
| 113 | + $encrypted_text = openssl_encrypt( |
|
| 114 | + $this->base64_encoder->encodeString($text_to_encrypt), // encode to remove special characters |
|
| 115 | + $cipher_method, |
|
| 116 | + $key, |
|
| 117 | + 0, |
|
| 118 | + $iv |
|
| 119 | + ); |
|
| 120 | + $this->validateEncryption($encrypted_text); |
|
| 121 | + $encrypted_text = trim($encrypted_text); |
|
| 122 | + // hash the raw encrypted text |
|
| 123 | + $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
|
| 124 | + // concatenate everything into one big string and encode it again |
|
| 125 | + return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * decrypts data |
|
| 131 | - * |
|
| 132 | - * @param string $encrypted_text - the text to be decrypted |
|
| 133 | - * @param string $encryption_key_identifier - cryptographically secure passphrase. will use default if necessary |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function decrypt($encrypted_text, $encryption_key_identifier = '') |
|
| 137 | - { |
|
| 138 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 139 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 140 | - $key = $this->getDigestHashValue($encryption_key); |
|
| 141 | - // decode our concatenated string |
|
| 142 | - $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
| 143 | - // get the string lengths used for the hash and iv |
|
| 144 | - $hash_length = $this->calculateHashLength($encryption_key); |
|
| 145 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 146 | - // use the above lengths to snip the required values from the decoded string |
|
| 147 | - $iv = substr($encrypted_text, 0, $iv_length); |
|
| 148 | - $hmac = substr($encrypted_text, $iv_length, $hash_length); |
|
| 149 | - $encrypted_text_raw = substr($encrypted_text, $iv_length + $hash_length); |
|
| 150 | - // rehash the original raw encrypted text |
|
| 151 | - $rehash_mac = hash_hmac($this->getHashAlgorithm(), $encrypted_text_raw, $key, true); |
|
| 152 | - // timing attack safe comparison to determine if anything has changed |
|
| 153 | - if (hash_equals($hmac, $rehash_mac)) { |
|
| 154 | - // looks good, decrypt it, trim it, and return it |
|
| 155 | - $decrypted_text = openssl_decrypt( |
|
| 156 | - $encrypted_text_raw, |
|
| 157 | - $this->cipher_method->getCipherMethod(), |
|
| 158 | - $key, |
|
| 159 | - 0, |
|
| 160 | - $iv |
|
| 161 | - ); |
|
| 162 | - $this->validateDecryption($decrypted_text); |
|
| 163 | - return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
| 164 | - } |
|
| 165 | - throw new RuntimeException( |
|
| 166 | - esc_html__( |
|
| 167 | - 'Decryption failed because a hash comparison of the original text and the decrypted text was not the same, meaning something in the system or the encrypted data has changed.', |
|
| 168 | - 'event_espresso' |
|
| 169 | - ) |
|
| 170 | - ); |
|
| 171 | - } |
|
| 129 | + /** |
|
| 130 | + * decrypts data |
|
| 131 | + * |
|
| 132 | + * @param string $encrypted_text - the text to be decrypted |
|
| 133 | + * @param string $encryption_key_identifier - cryptographically secure passphrase. will use default if necessary |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function decrypt($encrypted_text, $encryption_key_identifier = '') |
|
| 137 | + { |
|
| 138 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 139 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 140 | + $key = $this->getDigestHashValue($encryption_key); |
|
| 141 | + // decode our concatenated string |
|
| 142 | + $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
| 143 | + // get the string lengths used for the hash and iv |
|
| 144 | + $hash_length = $this->calculateHashLength($encryption_key); |
|
| 145 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 146 | + // use the above lengths to snip the required values from the decoded string |
|
| 147 | + $iv = substr($encrypted_text, 0, $iv_length); |
|
| 148 | + $hmac = substr($encrypted_text, $iv_length, $hash_length); |
|
| 149 | + $encrypted_text_raw = substr($encrypted_text, $iv_length + $hash_length); |
|
| 150 | + // rehash the original raw encrypted text |
|
| 151 | + $rehash_mac = hash_hmac($this->getHashAlgorithm(), $encrypted_text_raw, $key, true); |
|
| 152 | + // timing attack safe comparison to determine if anything has changed |
|
| 153 | + if (hash_equals($hmac, $rehash_mac)) { |
|
| 154 | + // looks good, decrypt it, trim it, and return it |
|
| 155 | + $decrypted_text = openssl_decrypt( |
|
| 156 | + $encrypted_text_raw, |
|
| 157 | + $this->cipher_method->getCipherMethod(), |
|
| 158 | + $key, |
|
| 159 | + 0, |
|
| 160 | + $iv |
|
| 161 | + ); |
|
| 162 | + $this->validateDecryption($decrypted_text); |
|
| 163 | + return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
| 164 | + } |
|
| 165 | + throw new RuntimeException( |
|
| 166 | + esc_html__( |
|
| 167 | + 'Decryption failed because a hash comparison of the original text and the decrypted text was not the same, meaning something in the system or the encrypted data has changed.', |
|
| 168 | + 'event_espresso' |
|
| 169 | + ) |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -122,7 +122,7 @@ |
||
| 122 | 122 | // hash the raw encrypted text |
| 123 | 123 | $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
| 124 | 124 | // concatenate everything into one big string and encode it again |
| 125 | - return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
| 125 | + return $this->base64_encoder->encodeString($iv.$hmac.$encrypted_text); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | |
@@ -18,266 +18,266 @@ |
||
| 18 | 18 | abstract class OpenSSL implements EncryptionMethodInterface |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * the default OPENSSL digest method to use |
|
| 23 | - */ |
|
| 24 | - const DEFAULT_DIGEST_METHOD = 'sha512'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * separates the encrypted text from the initialization vector |
|
| 28 | - */ |
|
| 29 | - const IV_DELIMITER = ':iv:'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Base64Encoder |
|
| 33 | - */ |
|
| 34 | - protected $base64_encoder; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var CipherMethod |
|
| 38 | - */ |
|
| 39 | - protected $cipher_method; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var array $digest_methods |
|
| 43 | - */ |
|
| 44 | - private $digest_methods = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var EncryptionKeyManagerInterface |
|
| 48 | - */ |
|
| 49 | - protected $encryption_key_manager; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var boolean |
|
| 53 | - */ |
|
| 54 | - private $openssl_installed; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - private $min_php_version; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - private $hash_algorithm; |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * To use custom a cipher method and/or encryption keys: |
|
| 69 | - * - extend this class |
|
| 70 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 71 | - * - pass those to this constructor, like so: |
|
| 72 | - * |
|
| 73 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
| 74 | - * parent::__construct( |
|
| 75 | - * $base64_encoder, |
|
| 76 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME) |
|
| 77 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME) |
|
| 78 | - * ); |
|
| 79 | - * } |
|
| 80 | - * |
|
| 81 | - * @param Base64Encoder $base64_encoder |
|
| 82 | - * @param CipherMethod $cipher_method |
|
| 83 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 84 | - * @param string $min_php_version |
|
| 85 | - */ |
|
| 86 | - protected function __construct( |
|
| 87 | - Base64Encoder $base64_encoder, |
|
| 88 | - CipherMethod $cipher_method, |
|
| 89 | - EncryptionKeyManagerInterface $encryption_key_manager, |
|
| 90 | - $min_php_version |
|
| 91 | - ) { |
|
| 92 | - $this->base64_encoder = $base64_encoder; |
|
| 93 | - $this->cipher_method = $cipher_method; |
|
| 94 | - $this->encryption_key_manager = $encryption_key_manager; |
|
| 95 | - $this->min_php_version = $min_php_version; |
|
| 96 | - $this->openssl_installed = extension_loaded('openssl'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function isCryptographicallySecure() |
|
| 104 | - { |
|
| 105 | - return true; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return bool |
|
| 111 | - */ |
|
| 112 | - public function canUse() |
|
| 113 | - { |
|
| 114 | - return $this->openssl_installed && version_compare(PHP_VERSION, $this->min_php_version, '>='); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return string |
|
| 120 | - */ |
|
| 121 | - public function canUseNotice() |
|
| 122 | - { |
|
| 123 | - if (! $this->openssl_installed) { |
|
| 124 | - return esc_html__( |
|
| 125 | - 'The PHP openssl server extension is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
| 126 | - 'event_espresso' |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - if (version_compare(PHP_VERSION, $this->min_php_version, '<')) { |
|
| 130 | - return sprintf( |
|
| 131 | - esc_html__( |
|
| 132 | - 'PHP version %1$s or greater is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
| 133 | - 'event_espresso' |
|
| 134 | - ), |
|
| 135 | - $this->min_php_version |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - return sprintf( |
|
| 139 | - esc_html__('OpenSSL v1 encryption using %1$S is available for use.', 'event_espresso'), |
|
| 140 | - OpenSSLv1::CIPHER_METHOD |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Computes the digest hash value using the specified digest method. |
|
| 147 | - * If that digest method fails to produce a valid hash value, |
|
| 148 | - * then we'll grab the next digest method and recursively try again until something works. |
|
| 149 | - * |
|
| 150 | - * @param string $encryption_key |
|
| 151 | - * @param string $digest_method |
|
| 152 | - * @param bool $return_raw_data |
|
| 153 | - * @return string |
|
| 154 | - * @throws RuntimeException |
|
| 155 | - */ |
|
| 156 | - protected function getDigestHashValue( |
|
| 157 | - $encryption_key, |
|
| 158 | - $digest_method = OpenSSL::DEFAULT_DIGEST_METHOD, |
|
| 159 | - $return_raw_data = false |
|
| 160 | - ) { |
|
| 161 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method, $return_raw_data); |
|
| 162 | - if ($digest_hash_value === false) { |
|
| 163 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 164 | - } |
|
| 165 | - return $digest_hash_value; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Returns the NEXT element in the $digest_methods array. |
|
| 171 | - * If the $digest_methods array is empty, then we populate it |
|
| 172 | - * with the available values returned from openssl_get_md_methods(). |
|
| 173 | - * |
|
| 174 | - * @return string |
|
| 175 | - * @throws RuntimeException |
|
| 176 | - */ |
|
| 177 | - private function getDigestMethod() |
|
| 178 | - { |
|
| 179 | - $digest_method = prev($this->digest_methods); |
|
| 180 | - if (empty($this->digest_methods)) { |
|
| 181 | - $this->digest_methods = openssl_get_md_methods(); |
|
| 182 | - $digest_method = end($this->digest_methods); |
|
| 183 | - } |
|
| 184 | - if ($digest_method === false) { |
|
| 185 | - throw new RuntimeException( |
|
| 186 | - esc_html__( |
|
| 187 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 188 | - 'event_espresso' |
|
| 189 | - ) |
|
| 190 | - ); |
|
| 191 | - } |
|
| 192 | - return $digest_method; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param string $encryption_key |
|
| 198 | - * @return int |
|
| 199 | - */ |
|
| 200 | - protected function calculateHashLength($encryption_key) |
|
| 201 | - { |
|
| 202 | - // get existing key length |
|
| 203 | - $prev_key_length = $this->encryption_key_manager->keyLength(); |
|
| 204 | - // set it to something HUGE |
|
| 205 | - $this->encryption_key_manager->setKeyLength(512); |
|
| 206 | - // generate a new weak key, which should just be a really long random string |
|
| 207 | - $test_text = $this->encryption_key_manager->generateEncryptionKey(false); |
|
| 208 | - // generate a hash using our test string and our real $encryption_key |
|
| 209 | - $hash = hash_hmac($this->getHashAlgorithm(), $test_text, $encryption_key, true); |
|
| 210 | - // reset key length back to original value |
|
| 211 | - $this->encryption_key_manager->setKeyLength($prev_key_length); |
|
| 212 | - // return the length of the hash |
|
| 213 | - return strlen($hash); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @return string |
|
| 219 | - */ |
|
| 220 | - protected function getHashAlgorithm() |
|
| 221 | - { |
|
| 222 | - if (! $this->hash_algorithm) { |
|
| 223 | - // get installed hashing algorithms |
|
| 224 | - $hash_algorithms = hash_algos(); |
|
| 225 | - // filter array for "sha" algorithms |
|
| 226 | - $hash_algorithms = preg_grep('/^sha\d{3}$/i', $hash_algorithms); |
|
| 227 | - // if no sha algorithms are installed, then just use md5 |
|
| 228 | - if (empty($hash_algorithms)) { |
|
| 229 | - $this->hash_algorithm = 'md5'; |
|
| 230 | - return $this->hash_algorithm; |
|
| 231 | - } |
|
| 232 | - // sort ascending using "natural ordering" |
|
| 233 | - sort($hash_algorithms, SORT_NATURAL); |
|
| 234 | - // return last item from array, which should be the strongest installed sha hash |
|
| 235 | - $this->hash_algorithm = array_pop($hash_algorithms); |
|
| 236 | - } |
|
| 237 | - return $this->hash_algorithm; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @param string $encrypted_text |
|
| 243 | - * @throws RuntimeException |
|
| 244 | - */ |
|
| 245 | - protected function validateEncryption($encrypted_text) |
|
| 246 | - { |
|
| 247 | - if ($encrypted_text === false) { |
|
| 248 | - throw new RuntimeException( |
|
| 249 | - sprintf( |
|
| 250 | - esc_html__('The following error occurred during OpenSSL encryption: %1$S', 'event_espresso'), |
|
| 251 | - $this->getOpenSslError() |
|
| 252 | - ) |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @return false|string |
|
| 260 | - */ |
|
| 261 | - private function getOpenSslError() |
|
| 262 | - { |
|
| 263 | - $error = openssl_error_string(); |
|
| 264 | - return $error ?: esc_html__('Unknown Error', 'event_espresso'); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param string $encrypted_text |
|
| 270 | - * @throws RuntimeException |
|
| 271 | - */ |
|
| 272 | - protected function validateDecryption($encrypted_text) |
|
| 273 | - { |
|
| 274 | - if ($encrypted_text === false) { |
|
| 275 | - throw new RuntimeException( |
|
| 276 | - sprintf( |
|
| 277 | - esc_html__('OpenSSL decryption failed for the following reason: %1$S', 'event_espresso'), |
|
| 278 | - $this->getOpenSslError() |
|
| 279 | - ) |
|
| 280 | - ); |
|
| 281 | - } |
|
| 282 | - } |
|
| 21 | + /** |
|
| 22 | + * the default OPENSSL digest method to use |
|
| 23 | + */ |
|
| 24 | + const DEFAULT_DIGEST_METHOD = 'sha512'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * separates the encrypted text from the initialization vector |
|
| 28 | + */ |
|
| 29 | + const IV_DELIMITER = ':iv:'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Base64Encoder |
|
| 33 | + */ |
|
| 34 | + protected $base64_encoder; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var CipherMethod |
|
| 38 | + */ |
|
| 39 | + protected $cipher_method; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var array $digest_methods |
|
| 43 | + */ |
|
| 44 | + private $digest_methods = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var EncryptionKeyManagerInterface |
|
| 48 | + */ |
|
| 49 | + protected $encryption_key_manager; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var boolean |
|
| 53 | + */ |
|
| 54 | + private $openssl_installed; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + private $min_php_version; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + private $hash_algorithm; |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * To use custom a cipher method and/or encryption keys: |
|
| 69 | + * - extend this class |
|
| 70 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 71 | + * - pass those to this constructor, like so: |
|
| 72 | + * |
|
| 73 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
| 74 | + * parent::__construct( |
|
| 75 | + * $base64_encoder, |
|
| 76 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME) |
|
| 77 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME) |
|
| 78 | + * ); |
|
| 79 | + * } |
|
| 80 | + * |
|
| 81 | + * @param Base64Encoder $base64_encoder |
|
| 82 | + * @param CipherMethod $cipher_method |
|
| 83 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 84 | + * @param string $min_php_version |
|
| 85 | + */ |
|
| 86 | + protected function __construct( |
|
| 87 | + Base64Encoder $base64_encoder, |
|
| 88 | + CipherMethod $cipher_method, |
|
| 89 | + EncryptionKeyManagerInterface $encryption_key_manager, |
|
| 90 | + $min_php_version |
|
| 91 | + ) { |
|
| 92 | + $this->base64_encoder = $base64_encoder; |
|
| 93 | + $this->cipher_method = $cipher_method; |
|
| 94 | + $this->encryption_key_manager = $encryption_key_manager; |
|
| 95 | + $this->min_php_version = $min_php_version; |
|
| 96 | + $this->openssl_installed = extension_loaded('openssl'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function isCryptographicallySecure() |
|
| 104 | + { |
|
| 105 | + return true; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return bool |
|
| 111 | + */ |
|
| 112 | + public function canUse() |
|
| 113 | + { |
|
| 114 | + return $this->openssl_installed && version_compare(PHP_VERSION, $this->min_php_version, '>='); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return string |
|
| 120 | + */ |
|
| 121 | + public function canUseNotice() |
|
| 122 | + { |
|
| 123 | + if (! $this->openssl_installed) { |
|
| 124 | + return esc_html__( |
|
| 125 | + 'The PHP openssl server extension is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
| 126 | + 'event_espresso' |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + if (version_compare(PHP_VERSION, $this->min_php_version, '<')) { |
|
| 130 | + return sprintf( |
|
| 131 | + esc_html__( |
|
| 132 | + 'PHP version %1$s or greater is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
| 133 | + 'event_espresso' |
|
| 134 | + ), |
|
| 135 | + $this->min_php_version |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + return sprintf( |
|
| 139 | + esc_html__('OpenSSL v1 encryption using %1$S is available for use.', 'event_espresso'), |
|
| 140 | + OpenSSLv1::CIPHER_METHOD |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Computes the digest hash value using the specified digest method. |
|
| 147 | + * If that digest method fails to produce a valid hash value, |
|
| 148 | + * then we'll grab the next digest method and recursively try again until something works. |
|
| 149 | + * |
|
| 150 | + * @param string $encryption_key |
|
| 151 | + * @param string $digest_method |
|
| 152 | + * @param bool $return_raw_data |
|
| 153 | + * @return string |
|
| 154 | + * @throws RuntimeException |
|
| 155 | + */ |
|
| 156 | + protected function getDigestHashValue( |
|
| 157 | + $encryption_key, |
|
| 158 | + $digest_method = OpenSSL::DEFAULT_DIGEST_METHOD, |
|
| 159 | + $return_raw_data = false |
|
| 160 | + ) { |
|
| 161 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method, $return_raw_data); |
|
| 162 | + if ($digest_hash_value === false) { |
|
| 163 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 164 | + } |
|
| 165 | + return $digest_hash_value; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Returns the NEXT element in the $digest_methods array. |
|
| 171 | + * If the $digest_methods array is empty, then we populate it |
|
| 172 | + * with the available values returned from openssl_get_md_methods(). |
|
| 173 | + * |
|
| 174 | + * @return string |
|
| 175 | + * @throws RuntimeException |
|
| 176 | + */ |
|
| 177 | + private function getDigestMethod() |
|
| 178 | + { |
|
| 179 | + $digest_method = prev($this->digest_methods); |
|
| 180 | + if (empty($this->digest_methods)) { |
|
| 181 | + $this->digest_methods = openssl_get_md_methods(); |
|
| 182 | + $digest_method = end($this->digest_methods); |
|
| 183 | + } |
|
| 184 | + if ($digest_method === false) { |
|
| 185 | + throw new RuntimeException( |
|
| 186 | + esc_html__( |
|
| 187 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 188 | + 'event_espresso' |
|
| 189 | + ) |
|
| 190 | + ); |
|
| 191 | + } |
|
| 192 | + return $digest_method; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param string $encryption_key |
|
| 198 | + * @return int |
|
| 199 | + */ |
|
| 200 | + protected function calculateHashLength($encryption_key) |
|
| 201 | + { |
|
| 202 | + // get existing key length |
|
| 203 | + $prev_key_length = $this->encryption_key_manager->keyLength(); |
|
| 204 | + // set it to something HUGE |
|
| 205 | + $this->encryption_key_manager->setKeyLength(512); |
|
| 206 | + // generate a new weak key, which should just be a really long random string |
|
| 207 | + $test_text = $this->encryption_key_manager->generateEncryptionKey(false); |
|
| 208 | + // generate a hash using our test string and our real $encryption_key |
|
| 209 | + $hash = hash_hmac($this->getHashAlgorithm(), $test_text, $encryption_key, true); |
|
| 210 | + // reset key length back to original value |
|
| 211 | + $this->encryption_key_manager->setKeyLength($prev_key_length); |
|
| 212 | + // return the length of the hash |
|
| 213 | + return strlen($hash); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @return string |
|
| 219 | + */ |
|
| 220 | + protected function getHashAlgorithm() |
|
| 221 | + { |
|
| 222 | + if (! $this->hash_algorithm) { |
|
| 223 | + // get installed hashing algorithms |
|
| 224 | + $hash_algorithms = hash_algos(); |
|
| 225 | + // filter array for "sha" algorithms |
|
| 226 | + $hash_algorithms = preg_grep('/^sha\d{3}$/i', $hash_algorithms); |
|
| 227 | + // if no sha algorithms are installed, then just use md5 |
|
| 228 | + if (empty($hash_algorithms)) { |
|
| 229 | + $this->hash_algorithm = 'md5'; |
|
| 230 | + return $this->hash_algorithm; |
|
| 231 | + } |
|
| 232 | + // sort ascending using "natural ordering" |
|
| 233 | + sort($hash_algorithms, SORT_NATURAL); |
|
| 234 | + // return last item from array, which should be the strongest installed sha hash |
|
| 235 | + $this->hash_algorithm = array_pop($hash_algorithms); |
|
| 236 | + } |
|
| 237 | + return $this->hash_algorithm; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @param string $encrypted_text |
|
| 243 | + * @throws RuntimeException |
|
| 244 | + */ |
|
| 245 | + protected function validateEncryption($encrypted_text) |
|
| 246 | + { |
|
| 247 | + if ($encrypted_text === false) { |
|
| 248 | + throw new RuntimeException( |
|
| 249 | + sprintf( |
|
| 250 | + esc_html__('The following error occurred during OpenSSL encryption: %1$S', 'event_espresso'), |
|
| 251 | + $this->getOpenSslError() |
|
| 252 | + ) |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @return false|string |
|
| 260 | + */ |
|
| 261 | + private function getOpenSslError() |
|
| 262 | + { |
|
| 263 | + $error = openssl_error_string(); |
|
| 264 | + return $error ?: esc_html__('Unknown Error', 'event_espresso'); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param string $encrypted_text |
|
| 270 | + * @throws RuntimeException |
|
| 271 | + */ |
|
| 272 | + protected function validateDecryption($encrypted_text) |
|
| 273 | + { |
|
| 274 | + if ($encrypted_text === false) { |
|
| 275 | + throw new RuntimeException( |
|
| 276 | + sprintf( |
|
| 277 | + esc_html__('OpenSSL decryption failed for the following reason: %1$S', 'event_espresso'), |
|
| 278 | + $this->getOpenSslError() |
|
| 279 | + ) |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | 283 | } |
@@ -15,206 +15,206 @@ |
||
| 15 | 15 | class CipherMethod |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $cipher_method_option_name; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * list of cipher methods that we consider usable, |
|
| 25 | - * essentially all of the installed_cipher_methods minus weak_algorithms |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $cipher_methods = []; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $default_cipher_method; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * list of ALL cipher methods available on the server |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $installed_cipher_methods; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * the OpenSSL cipher method to use. default: AES-128-CBC |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $validated_cipher_method; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based |
|
| 52 | - * and ECB mode should be avoided |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $weak_algorithms = ['des', 'ecb', 'md5', 'rc2', 'rc4']; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param string $default_cipher_method |
|
| 61 | - * @param string $cipher_method_option_name |
|
| 62 | - */ |
|
| 63 | - public function __construct($default_cipher_method, $cipher_method_option_name) |
|
| 64 | - { |
|
| 65 | - $this->default_cipher_method = $default_cipher_method; |
|
| 66 | - $this->cipher_method_option_name = $cipher_method_option_name; |
|
| 67 | - $this->installed_cipher_methods = openssl_get_cipher_methods(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Returns a cipher method that has been verified to work. |
|
| 73 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
| 74 | - * Then tests the incoming default and returns that if it's good. |
|
| 75 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
| 76 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 77 | - * to see what is available on the server, and returns the results. |
|
| 78 | - * |
|
| 79 | - * @param string $cipher_method |
|
| 80 | - * @param bool $load_alternate [optional] if TRUE, will load the default cipher method (or any strong algorithm) |
|
| 81 | - * if the requested cipher method is not installed or invalid. |
|
| 82 | - * if FALSE, will throw an exception if the requested cipher method is not valid. |
|
| 83 | - * @return string |
|
| 84 | - * @throws RuntimeException |
|
| 85 | - */ |
|
| 86 | - public function getCipherMethod($cipher_method = null, $load_alternate = true) |
|
| 87 | - { |
|
| 88 | - if (empty($cipher_method) && $this->validated_cipher_method !== null) { |
|
| 89 | - return $this->validated_cipher_method; |
|
| 90 | - } |
|
| 91 | - // if nothing specific was requested and it's ok to load an alternate, then grab the system default |
|
| 92 | - if (empty($cipher_method) && $load_alternate) { |
|
| 93 | - $cipher_method = $this->default_cipher_method; |
|
| 94 | - } |
|
| 95 | - // verify that the cipher method can produce an initialization vector. |
|
| 96 | - // but if the requested is invalid and we don't want to load an alternate, then throw an exception |
|
| 97 | - $throw_exception = ! $load_alternate; |
|
| 98 | - if ($this->validateCipherMethod($cipher_method, $throw_exception) === false) { |
|
| 99 | - // nope? ... ok let's see what we can find |
|
| 100 | - $cipher_method = $this->getAvailableCipherMethod(); |
|
| 101 | - } |
|
| 102 | - // if nothing has been previously validated, then save the currently requested cipher which appears to be good |
|
| 103 | - if ($this->validated_cipher_method === null) { |
|
| 104 | - $this->validated_cipher_method = $cipher_method; |
|
| 105 | - } |
|
| 106 | - return $cipher_method; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * returns true if the selected cipher method either uses Galois/Counter Mode (GCM) |
|
| 112 | - * or Counter with CBC-MAC (CCM) authenticated encryption modes |
|
| 113 | - * (also need to be using PHP 7.1 or greater to actually use authenticated encryption modes) |
|
| 114 | - * |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function usesAuthenticatedEncryptionMode() |
|
| 118 | - { |
|
| 119 | - return PHP_VERSION_ID >= 70100 |
|
| 120 | - && ( |
|
| 121 | - stripos($this->validated_cipher_method, 'gcm') !== false |
|
| 122 | - || stripos($this->validated_cipher_method, 'ccm') !== false |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param string $cipher_method |
|
| 129 | - * @return string |
|
| 130 | - * @throws RuntimeException |
|
| 131 | - */ |
|
| 132 | - protected function getAvailableCipherMethod($cipher_method = null) |
|
| 133 | - { |
|
| 134 | - // if nothing was supplied, the get what we found in the past to work |
|
| 135 | - $cipher_method_to_test = $cipher_method ?: get_option($this->cipher_method_option_name, ''); |
|
| 136 | - // verify that the incoming cipher method exists and can produce an initialization vector |
|
| 137 | - if ($this->validateCipherMethod($cipher_method_to_test) === false) { |
|
| 138 | - // what? there's no list? |
|
| 139 | - if (empty($this->cipher_methods)) { |
|
| 140 | - // generate that list and cache it |
|
| 141 | - $this->cipher_methods = $this->getAvailableStrongCipherMethods(); |
|
| 142 | - } |
|
| 143 | - // then grab the first item from the list (we'll add it back later if it is good) |
|
| 144 | - $cipher_method_to_test = array_shift($this->cipher_methods); |
|
| 145 | - if ($cipher_method_to_test === null) { |
|
| 146 | - throw new RuntimeException( |
|
| 147 | - esc_html__( |
|
| 148 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 149 | - 'event_espresso' |
|
| 150 | - ) |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - // verify that the next cipher method works |
|
| 154 | - return $this->getAvailableCipherMethod($cipher_method_to_test); |
|
| 155 | - } |
|
| 156 | - // if we've gotten this far, then we found an available cipher method that works |
|
| 157 | - // so save that for next time, if it's not the same as what's there already |
|
| 158 | - if ($cipher_method_to_test !== $cipher_method) { |
|
| 159 | - update_option($this->cipher_method_option_name, $cipher_method_to_test); |
|
| 160 | - } |
|
| 161 | - // if we previously removed this cipher method from the list of valid ones, then let's put it back |
|
| 162 | - if (! in_array($cipher_method_to_test, $this->cipher_methods, true)) { |
|
| 163 | - array_unshift($this->cipher_methods, $cipher_method_to_test); |
|
| 164 | - } |
|
| 165 | - return $cipher_method_to_test; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return array |
|
| 171 | - */ |
|
| 172 | - protected function getAvailableStrongCipherMethods() |
|
| 173 | - { |
|
| 174 | - return array_filter($this->installed_cipher_methods, [$this, 'weakAlgorithmFilter']); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param string $cipher_method |
|
| 180 | - * @param false $throw_exception |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - protected function validateCipherMethod($cipher_method, $throw_exception = false) |
|
| 184 | - { |
|
| 185 | - // verify that the requested cipher method is actually installed and can produce an initialization vector |
|
| 186 | - if ( |
|
| 187 | - in_array($cipher_method, $this->installed_cipher_methods, true) |
|
| 188 | - && openssl_cipher_iv_length($cipher_method) !== false |
|
| 189 | - ) { |
|
| 190 | - return true; |
|
| 191 | - } |
|
| 192 | - if (! $throw_exception) { |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - throw new RuntimeException( |
|
| 196 | - sprintf( |
|
| 197 | - esc_html__( |
|
| 198 | - 'The requested OpenSSL cipher method "%1$s" is invalid or not installed on the server. Please contact the server administrator.', |
|
| 199 | - 'event_espresso' |
|
| 200 | - ), |
|
| 201 | - $cipher_method |
|
| 202 | - ) |
|
| 203 | - ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @see https://www.php.net/manual/en/function.openssl-get-cipher-methods.php#example-890 |
|
| 209 | - * @param string $cipher_method |
|
| 210 | - */ |
|
| 211 | - protected function weakAlgorithmFilter($cipher_method) |
|
| 212 | - { |
|
| 213 | - foreach ($this->weak_algorithms as $weak_algorithm) { |
|
| 214 | - if (stripos($cipher_method, $weak_algorithm) !== false) { |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - return true; |
|
| 219 | - } |
|
| 18 | + /** |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $cipher_method_option_name; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * list of cipher methods that we consider usable, |
|
| 25 | + * essentially all of the installed_cipher_methods minus weak_algorithms |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $cipher_methods = []; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $default_cipher_method; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * list of ALL cipher methods available on the server |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $installed_cipher_methods; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * the OpenSSL cipher method to use. default: AES-128-CBC |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $validated_cipher_method; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based |
|
| 52 | + * and ECB mode should be avoided |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $weak_algorithms = ['des', 'ecb', 'md5', 'rc2', 'rc4']; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param string $default_cipher_method |
|
| 61 | + * @param string $cipher_method_option_name |
|
| 62 | + */ |
|
| 63 | + public function __construct($default_cipher_method, $cipher_method_option_name) |
|
| 64 | + { |
|
| 65 | + $this->default_cipher_method = $default_cipher_method; |
|
| 66 | + $this->cipher_method_option_name = $cipher_method_option_name; |
|
| 67 | + $this->installed_cipher_methods = openssl_get_cipher_methods(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Returns a cipher method that has been verified to work. |
|
| 73 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
| 74 | + * Then tests the incoming default and returns that if it's good. |
|
| 75 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
| 76 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 77 | + * to see what is available on the server, and returns the results. |
|
| 78 | + * |
|
| 79 | + * @param string $cipher_method |
|
| 80 | + * @param bool $load_alternate [optional] if TRUE, will load the default cipher method (or any strong algorithm) |
|
| 81 | + * if the requested cipher method is not installed or invalid. |
|
| 82 | + * if FALSE, will throw an exception if the requested cipher method is not valid. |
|
| 83 | + * @return string |
|
| 84 | + * @throws RuntimeException |
|
| 85 | + */ |
|
| 86 | + public function getCipherMethod($cipher_method = null, $load_alternate = true) |
|
| 87 | + { |
|
| 88 | + if (empty($cipher_method) && $this->validated_cipher_method !== null) { |
|
| 89 | + return $this->validated_cipher_method; |
|
| 90 | + } |
|
| 91 | + // if nothing specific was requested and it's ok to load an alternate, then grab the system default |
|
| 92 | + if (empty($cipher_method) && $load_alternate) { |
|
| 93 | + $cipher_method = $this->default_cipher_method; |
|
| 94 | + } |
|
| 95 | + // verify that the cipher method can produce an initialization vector. |
|
| 96 | + // but if the requested is invalid and we don't want to load an alternate, then throw an exception |
|
| 97 | + $throw_exception = ! $load_alternate; |
|
| 98 | + if ($this->validateCipherMethod($cipher_method, $throw_exception) === false) { |
|
| 99 | + // nope? ... ok let's see what we can find |
|
| 100 | + $cipher_method = $this->getAvailableCipherMethod(); |
|
| 101 | + } |
|
| 102 | + // if nothing has been previously validated, then save the currently requested cipher which appears to be good |
|
| 103 | + if ($this->validated_cipher_method === null) { |
|
| 104 | + $this->validated_cipher_method = $cipher_method; |
|
| 105 | + } |
|
| 106 | + return $cipher_method; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * returns true if the selected cipher method either uses Galois/Counter Mode (GCM) |
|
| 112 | + * or Counter with CBC-MAC (CCM) authenticated encryption modes |
|
| 113 | + * (also need to be using PHP 7.1 or greater to actually use authenticated encryption modes) |
|
| 114 | + * |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function usesAuthenticatedEncryptionMode() |
|
| 118 | + { |
|
| 119 | + return PHP_VERSION_ID >= 70100 |
|
| 120 | + && ( |
|
| 121 | + stripos($this->validated_cipher_method, 'gcm') !== false |
|
| 122 | + || stripos($this->validated_cipher_method, 'ccm') !== false |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param string $cipher_method |
|
| 129 | + * @return string |
|
| 130 | + * @throws RuntimeException |
|
| 131 | + */ |
|
| 132 | + protected function getAvailableCipherMethod($cipher_method = null) |
|
| 133 | + { |
|
| 134 | + // if nothing was supplied, the get what we found in the past to work |
|
| 135 | + $cipher_method_to_test = $cipher_method ?: get_option($this->cipher_method_option_name, ''); |
|
| 136 | + // verify that the incoming cipher method exists and can produce an initialization vector |
|
| 137 | + if ($this->validateCipherMethod($cipher_method_to_test) === false) { |
|
| 138 | + // what? there's no list? |
|
| 139 | + if (empty($this->cipher_methods)) { |
|
| 140 | + // generate that list and cache it |
|
| 141 | + $this->cipher_methods = $this->getAvailableStrongCipherMethods(); |
|
| 142 | + } |
|
| 143 | + // then grab the first item from the list (we'll add it back later if it is good) |
|
| 144 | + $cipher_method_to_test = array_shift($this->cipher_methods); |
|
| 145 | + if ($cipher_method_to_test === null) { |
|
| 146 | + throw new RuntimeException( |
|
| 147 | + esc_html__( |
|
| 148 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 149 | + 'event_espresso' |
|
| 150 | + ) |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + // verify that the next cipher method works |
|
| 154 | + return $this->getAvailableCipherMethod($cipher_method_to_test); |
|
| 155 | + } |
|
| 156 | + // if we've gotten this far, then we found an available cipher method that works |
|
| 157 | + // so save that for next time, if it's not the same as what's there already |
|
| 158 | + if ($cipher_method_to_test !== $cipher_method) { |
|
| 159 | + update_option($this->cipher_method_option_name, $cipher_method_to_test); |
|
| 160 | + } |
|
| 161 | + // if we previously removed this cipher method from the list of valid ones, then let's put it back |
|
| 162 | + if (! in_array($cipher_method_to_test, $this->cipher_methods, true)) { |
|
| 163 | + array_unshift($this->cipher_methods, $cipher_method_to_test); |
|
| 164 | + } |
|
| 165 | + return $cipher_method_to_test; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return array |
|
| 171 | + */ |
|
| 172 | + protected function getAvailableStrongCipherMethods() |
|
| 173 | + { |
|
| 174 | + return array_filter($this->installed_cipher_methods, [$this, 'weakAlgorithmFilter']); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param string $cipher_method |
|
| 180 | + * @param false $throw_exception |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + protected function validateCipherMethod($cipher_method, $throw_exception = false) |
|
| 184 | + { |
|
| 185 | + // verify that the requested cipher method is actually installed and can produce an initialization vector |
|
| 186 | + if ( |
|
| 187 | + in_array($cipher_method, $this->installed_cipher_methods, true) |
|
| 188 | + && openssl_cipher_iv_length($cipher_method) !== false |
|
| 189 | + ) { |
|
| 190 | + return true; |
|
| 191 | + } |
|
| 192 | + if (! $throw_exception) { |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + throw new RuntimeException( |
|
| 196 | + sprintf( |
|
| 197 | + esc_html__( |
|
| 198 | + 'The requested OpenSSL cipher method "%1$s" is invalid or not installed on the server. Please contact the server administrator.', |
|
| 199 | + 'event_espresso' |
|
| 200 | + ), |
|
| 201 | + $cipher_method |
|
| 202 | + ) |
|
| 203 | + ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @see https://www.php.net/manual/en/function.openssl-get-cipher-methods.php#example-890 |
|
| 209 | + * @param string $cipher_method |
|
| 210 | + */ |
|
| 211 | + protected function weakAlgorithmFilter($cipher_method) |
|
| 212 | + { |
|
| 213 | + foreach ($this->weak_algorithms as $weak_algorithm) { |
|
| 214 | + if (stripos($cipher_method, $weak_algorithm) !== false) { |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + return true; |
|
| 219 | + } |
|
| 220 | 220 | } |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | update_option($this->cipher_method_option_name, $cipher_method_to_test); |
| 160 | 160 | } |
| 161 | 161 | // if we previously removed this cipher method from the list of valid ones, then let's put it back |
| 162 | - if (! in_array($cipher_method_to_test, $this->cipher_methods, true)) { |
|
| 162 | + if ( ! in_array($cipher_method_to_test, $this->cipher_methods, true)) { |
|
| 163 | 163 | array_unshift($this->cipher_methods, $cipher_method_to_test); |
| 164 | 164 | } |
| 165 | 165 | return $cipher_method_to_test; |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | ) { |
| 190 | 190 | return true; |
| 191 | 191 | } |
| 192 | - if (! $throw_exception) { |
|
| 192 | + if ( ! $throw_exception) { |
|
| 193 | 193 | return false; |
| 194 | 194 | } |
| 195 | 195 | throw new RuntimeException( |
@@ -24,221 +24,221 @@ |
||
| 24 | 24 | class OpenSSLv2 extends OpenSSL |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * name used for a default encryption key in case no others are set |
|
| 29 | - */ |
|
| 30 | - const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * name used for saving encryption keys to the wp_options table |
|
| 34 | - */ |
|
| 35 | - const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * the OPENSSL cipher method used |
|
| 39 | - */ |
|
| 40 | - const CIPHER_METHOD = 'aes-256-gcm'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * WP "options_name" used to store a verified available cipher method |
|
| 44 | - */ |
|
| 45 | - const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
| 49 | - */ |
|
| 50 | - const AUTH_TAG_LENGTH = 16; |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
| 55 | - * - extend this class |
|
| 56 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 57 | - * - pass those to this constructor, like so: |
|
| 58 | - * |
|
| 59 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
| 60 | - * parent::__construct( |
|
| 61 | - * $base64_encoder, |
|
| 62 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
| 63 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
| 64 | - * '7.1.0' |
|
| 65 | - * ); |
|
| 66 | - * } |
|
| 67 | - * |
|
| 68 | - * @param Base64Encoder $base64_encoder |
|
| 69 | - * @param CipherMethod|null $cipher_method |
|
| 70 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 71 | - * @param string $min_php_version defaults to 7.1.0 |
|
| 72 | - * (when openssl auth tag and random_bytes() were added) |
|
| 73 | - */ |
|
| 74 | - public function __construct( |
|
| 75 | - Base64Encoder $base64_encoder, |
|
| 76 | - CipherMethod $cipher_method = null, |
|
| 77 | - EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
| 78 | - $min_php_version = '7.1.0' |
|
| 79 | - ) { |
|
| 80 | - parent::__construct( |
|
| 81 | - $base64_encoder, |
|
| 82 | - $cipher_method instanceof CipherMethod |
|
| 83 | - ? $cipher_method |
|
| 84 | - : new CipherMethod( |
|
| 85 | - OpenSSLv2::CIPHER_METHOD, |
|
| 86 | - OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
| 87 | - ), |
|
| 88 | - $encryption_key_manager instanceof EncryptionKeyManager |
|
| 89 | - ? $encryption_key_manager |
|
| 90 | - : new EncryptionKeyManager( |
|
| 91 | - $base64_encoder, |
|
| 92 | - OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
| 93 | - OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
| 94 | - ), |
|
| 95 | - $min_php_version |
|
| 96 | - ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * encrypts data |
|
| 102 | - * |
|
| 103 | - * @param string $text_to_encrypt - the text to be encrypted |
|
| 104 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
| 105 | - * @param string $aad - [optional] additional authentication data |
|
| 106 | - * @return string |
|
| 107 | - * @throws Exception |
|
| 108 | - */ |
|
| 109 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
| 110 | - { |
|
| 111 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 112 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 113 | - // generate initialization vector for the cipher method. |
|
| 114 | - $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
| 115 | - // encrypt it (encode to remove special characters) |
|
| 116 | - $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
| 117 | - $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
| 118 | - ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
| 119 | - : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
| 120 | - return $this->base64_encoder->encodeString($encrypted_text); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @param string $text_to_encrypt - the text to be encrypted |
|
| 126 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 127 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 128 | - * @param string $iv - the initialization vector |
|
| 129 | - * @param string $aad - additional authentication data |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
| 133 | - { |
|
| 134 | - $encrypted_text = openssl_encrypt( |
|
| 135 | - $text_to_encrypt, |
|
| 136 | - $cipher_method, |
|
| 137 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
| 138 | - 0, |
|
| 139 | - $iv, |
|
| 140 | - $tag, |
|
| 141 | - $aad, |
|
| 142 | - OpenSSLv2::AUTH_TAG_LENGTH |
|
| 143 | - ); |
|
| 144 | - $this->validateEncryption($encrypted_text); |
|
| 145 | - // concatenate everything into one big string |
|
| 146 | - return $iv . $tag . $encrypted_text; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param string $text_to_encrypt - the text to be encrypted |
|
| 152 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 153 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 154 | - * @param string $iv - the initialization vector |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
| 158 | - { |
|
| 159 | - $encrypted_text = openssl_encrypt( |
|
| 160 | - $text_to_encrypt, |
|
| 161 | - $cipher_method, |
|
| 162 | - $this->getDigestHashValue($encryption_key), |
|
| 163 | - 0, |
|
| 164 | - $iv |
|
| 165 | - ); |
|
| 166 | - $this->validateEncryption($encrypted_text); |
|
| 167 | - // prepend the initialization vector |
|
| 168 | - return $iv . $encrypted_text; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * decrypts data |
|
| 174 | - * |
|
| 175 | - * @param string $encrypted_text - the text to be decrypted |
|
| 176 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
| 177 | - * @param string $aad - [optional] additional authentication data |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
| 181 | - { |
|
| 182 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 183 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 184 | - // maybe decode |
|
| 185 | - $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
| 186 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 187 | - // use the iv length to snip it from the decoded string |
|
| 188 | - $iv = substr($encrypted_text, 0, $iv_length); |
|
| 189 | - // then remove it from the rest of the decoded string |
|
| 190 | - $encrypted_text = substr($encrypted_text, $iv_length); |
|
| 191 | - // decrypt it |
|
| 192 | - $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
| 193 | - ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
| 194 | - : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
| 195 | - |
|
| 196 | - $this->validateDecryption($decrypted_text); |
|
| 197 | - return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param string $encrypted_text - the text to be decrypted |
|
| 203 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 204 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 205 | - * @param string $iv - the initialization vector |
|
| 206 | - * @param string $aad - additional authentication data |
|
| 207 | - * @return string|false |
|
| 208 | - */ |
|
| 209 | - private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
| 210 | - { |
|
| 211 | - // use the tag length to snip it from the decoded string |
|
| 212 | - $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
| 213 | - // then remove it from the rest of the decoded string |
|
| 214 | - $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
| 215 | - return openssl_decrypt( |
|
| 216 | - $encrypted_text, |
|
| 217 | - $cipher_method, |
|
| 218 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
| 219 | - 0, |
|
| 220 | - $iv, |
|
| 221 | - $tag, |
|
| 222 | - $aad |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param string $encrypted_text - the text to be decrypted |
|
| 229 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 230 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 231 | - * @param string $iv - the initialization vector |
|
| 232 | - * @return string|false |
|
| 233 | - */ |
|
| 234 | - private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
| 235 | - { |
|
| 236 | - return openssl_decrypt( |
|
| 237 | - $encrypted_text, |
|
| 238 | - $cipher_method, |
|
| 239 | - $this->getDigestHashValue($encryption_key), |
|
| 240 | - 0, |
|
| 241 | - $iv |
|
| 242 | - ); |
|
| 243 | - } |
|
| 27 | + /** |
|
| 28 | + * name used for a default encryption key in case no others are set |
|
| 29 | + */ |
|
| 30 | + const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * name used for saving encryption keys to the wp_options table |
|
| 34 | + */ |
|
| 35 | + const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * the OPENSSL cipher method used |
|
| 39 | + */ |
|
| 40 | + const CIPHER_METHOD = 'aes-256-gcm'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * WP "options_name" used to store a verified available cipher method |
|
| 44 | + */ |
|
| 45 | + const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
| 49 | + */ |
|
| 50 | + const AUTH_TAG_LENGTH = 16; |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
| 55 | + * - extend this class |
|
| 56 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
| 57 | + * - pass those to this constructor, like so: |
|
| 58 | + * |
|
| 59 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
| 60 | + * parent::__construct( |
|
| 61 | + * $base64_encoder, |
|
| 62 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
| 63 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
| 64 | + * '7.1.0' |
|
| 65 | + * ); |
|
| 66 | + * } |
|
| 67 | + * |
|
| 68 | + * @param Base64Encoder $base64_encoder |
|
| 69 | + * @param CipherMethod|null $cipher_method |
|
| 70 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
| 71 | + * @param string $min_php_version defaults to 7.1.0 |
|
| 72 | + * (when openssl auth tag and random_bytes() were added) |
|
| 73 | + */ |
|
| 74 | + public function __construct( |
|
| 75 | + Base64Encoder $base64_encoder, |
|
| 76 | + CipherMethod $cipher_method = null, |
|
| 77 | + EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
| 78 | + $min_php_version = '7.1.0' |
|
| 79 | + ) { |
|
| 80 | + parent::__construct( |
|
| 81 | + $base64_encoder, |
|
| 82 | + $cipher_method instanceof CipherMethod |
|
| 83 | + ? $cipher_method |
|
| 84 | + : new CipherMethod( |
|
| 85 | + OpenSSLv2::CIPHER_METHOD, |
|
| 86 | + OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
| 87 | + ), |
|
| 88 | + $encryption_key_manager instanceof EncryptionKeyManager |
|
| 89 | + ? $encryption_key_manager |
|
| 90 | + : new EncryptionKeyManager( |
|
| 91 | + $base64_encoder, |
|
| 92 | + OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
| 93 | + OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
| 94 | + ), |
|
| 95 | + $min_php_version |
|
| 96 | + ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * encrypts data |
|
| 102 | + * |
|
| 103 | + * @param string $text_to_encrypt - the text to be encrypted |
|
| 104 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
| 105 | + * @param string $aad - [optional] additional authentication data |
|
| 106 | + * @return string |
|
| 107 | + * @throws Exception |
|
| 108 | + */ |
|
| 109 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
| 110 | + { |
|
| 111 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 112 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 113 | + // generate initialization vector for the cipher method. |
|
| 114 | + $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
| 115 | + // encrypt it (encode to remove special characters) |
|
| 116 | + $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
| 117 | + $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
| 118 | + ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
| 119 | + : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
| 120 | + return $this->base64_encoder->encodeString($encrypted_text); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param string $text_to_encrypt - the text to be encrypted |
|
| 126 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 127 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 128 | + * @param string $iv - the initialization vector |
|
| 129 | + * @param string $aad - additional authentication data |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
| 133 | + { |
|
| 134 | + $encrypted_text = openssl_encrypt( |
|
| 135 | + $text_to_encrypt, |
|
| 136 | + $cipher_method, |
|
| 137 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
| 138 | + 0, |
|
| 139 | + $iv, |
|
| 140 | + $tag, |
|
| 141 | + $aad, |
|
| 142 | + OpenSSLv2::AUTH_TAG_LENGTH |
|
| 143 | + ); |
|
| 144 | + $this->validateEncryption($encrypted_text); |
|
| 145 | + // concatenate everything into one big string |
|
| 146 | + return $iv . $tag . $encrypted_text; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param string $text_to_encrypt - the text to be encrypted |
|
| 152 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 153 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 154 | + * @param string $iv - the initialization vector |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
| 158 | + { |
|
| 159 | + $encrypted_text = openssl_encrypt( |
|
| 160 | + $text_to_encrypt, |
|
| 161 | + $cipher_method, |
|
| 162 | + $this->getDigestHashValue($encryption_key), |
|
| 163 | + 0, |
|
| 164 | + $iv |
|
| 165 | + ); |
|
| 166 | + $this->validateEncryption($encrypted_text); |
|
| 167 | + // prepend the initialization vector |
|
| 168 | + return $iv . $encrypted_text; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * decrypts data |
|
| 174 | + * |
|
| 175 | + * @param string $encrypted_text - the text to be decrypted |
|
| 176 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
| 177 | + * @param string $aad - [optional] additional authentication data |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
| 181 | + { |
|
| 182 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
| 183 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
| 184 | + // maybe decode |
|
| 185 | + $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
| 186 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
| 187 | + // use the iv length to snip it from the decoded string |
|
| 188 | + $iv = substr($encrypted_text, 0, $iv_length); |
|
| 189 | + // then remove it from the rest of the decoded string |
|
| 190 | + $encrypted_text = substr($encrypted_text, $iv_length); |
|
| 191 | + // decrypt it |
|
| 192 | + $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
| 193 | + ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
| 194 | + : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
| 195 | + |
|
| 196 | + $this->validateDecryption($decrypted_text); |
|
| 197 | + return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param string $encrypted_text - the text to be decrypted |
|
| 203 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 204 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 205 | + * @param string $iv - the initialization vector |
|
| 206 | + * @param string $aad - additional authentication data |
|
| 207 | + * @return string|false |
|
| 208 | + */ |
|
| 209 | + private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
| 210 | + { |
|
| 211 | + // use the tag length to snip it from the decoded string |
|
| 212 | + $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
| 213 | + // then remove it from the rest of the decoded string |
|
| 214 | + $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
| 215 | + return openssl_decrypt( |
|
| 216 | + $encrypted_text, |
|
| 217 | + $cipher_method, |
|
| 218 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
| 219 | + 0, |
|
| 220 | + $iv, |
|
| 221 | + $tag, |
|
| 222 | + $aad |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param string $encrypted_text - the text to be decrypted |
|
| 229 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
| 230 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
| 231 | + * @param string $iv - the initialization vector |
|
| 232 | + * @return string|false |
|
| 233 | + */ |
|
| 234 | + private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
| 235 | + { |
|
| 236 | + return openssl_decrypt( |
|
| 237 | + $encrypted_text, |
|
| 238 | + $cipher_method, |
|
| 239 | + $this->getDigestHashValue($encryption_key), |
|
| 240 | + 0, |
|
| 241 | + $iv |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | 244 | } |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | ); |
| 144 | 144 | $this->validateEncryption($encrypted_text); |
| 145 | 145 | // concatenate everything into one big string |
| 146 | - return $iv . $tag . $encrypted_text; |
|
| 146 | + return $iv.$tag.$encrypted_text; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | ); |
| 166 | 166 | $this->validateEncryption($encrypted_text); |
| 167 | 167 | // prepend the initialization vector |
| 168 | - return $iv . $encrypted_text; |
|
| 168 | + return $iv.$encrypted_text; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | |
@@ -15,148 +15,148 @@ |
||
| 15 | 15 | class Base64Encoder |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var boolean |
|
| 20 | - */ |
|
| 21 | - protected $use_base64_encode; |
|
| 22 | - |
|
| 23 | - |
|
| 24 | - public function __construct() |
|
| 25 | - { |
|
| 26 | - $this->use_base64_encode = function_exists('base64_encode'); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * encodes string with PHP's base64 encoding |
|
| 32 | - * |
|
| 33 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 34 | - * @param string $text_string the text to be encoded |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function encodeString($text_string = '') |
|
| 38 | - { |
|
| 39 | - // you give me nothing??? GET OUT ! |
|
| 40 | - if (empty($text_string) || ! $this->use_base64_encode) { |
|
| 41 | - return $text_string; |
|
| 42 | - } |
|
| 43 | - // encode |
|
| 44 | - return base64_encode($text_string); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * decodes string that has been encoded with PHP's base64 encoding |
|
| 50 | - * |
|
| 51 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 52 | - * @param string $encoded_string the text to be decoded |
|
| 53 | - * @return string |
|
| 54 | - * @throws RuntimeException |
|
| 55 | - */ |
|
| 56 | - public function decodeString($encoded_string = '') |
|
| 57 | - { |
|
| 58 | - // you give me nothing??? GET OUT ! |
|
| 59 | - if (empty($encoded_string)) { |
|
| 60 | - return $encoded_string; |
|
| 61 | - } |
|
| 62 | - $this->isValidBase64OrFail($encoded_string); |
|
| 63 | - return $this->decode($encoded_string); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $encoded_string the text to be decoded |
|
| 69 | - * @return string |
|
| 70 | - * @throws RuntimeException |
|
| 71 | - */ |
|
| 72 | - private function decode($encoded_string) |
|
| 73 | - { |
|
| 74 | - $decoded_string = base64_decode($encoded_string); |
|
| 75 | - if ($decoded_string === false) { |
|
| 76 | - throw new RuntimeException( |
|
| 77 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 78 | - ); |
|
| 79 | - } |
|
| 80 | - return $decoded_string; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * encodes url string with PHP's base64 encoding |
|
| 86 | - * |
|
| 87 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 88 | - * @param string $text_string the text to be encoded |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function encodeUrl($text_string = '') |
|
| 92 | - { |
|
| 93 | - // you give me nothing??? GET OUT ! |
|
| 94 | - if (empty($text_string) || ! $this->use_base64_encode) { |
|
| 95 | - return $text_string; |
|
| 96 | - } |
|
| 97 | - // encode |
|
| 98 | - $encoded_string = base64_encode($text_string); |
|
| 99 | - // remove some chars to make encoding more URL friendly |
|
| 100 | - return rtrim(strtr($encoded_string, '+/', '-_'), '='); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
| 106 | - * |
|
| 107 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 108 | - * @param string $encoded_string the text to be decoded |
|
| 109 | - * @return string |
|
| 110 | - * @throws RuntimeException |
|
| 111 | - */ |
|
| 112 | - public function decodeUrl($encoded_string = '') |
|
| 113 | - { |
|
| 114 | - // you give me nothing??? GET OUT ! |
|
| 115 | - if (empty($encoded_string)) { |
|
| 116 | - return $encoded_string; |
|
| 117 | - } |
|
| 118 | - // replace previously removed characters |
|
| 119 | - $encoded_string = strtr($encoded_string, '-_', '+/'); |
|
| 120 | - $encoded_string .= str_repeat('=', 3 - (3 + strlen($encoded_string)) % 4); |
|
| 121 | - $this->isValidBase64OrFail($encoded_string); |
|
| 122 | - return $this->decode($encoded_string); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $encoded_string the text to be decoded |
|
| 128 | - * @throws RuntimeException |
|
| 129 | - */ |
|
| 130 | - public function isValidBase64OrFail($encoded_string) |
|
| 131 | - { |
|
| 132 | - if (! $this->isValidBase64($encoded_string)) { |
|
| 133 | - throw new RuntimeException( |
|
| 134 | - esc_html__( |
|
| 135 | - 'Base 64 decoding failed because the supplied string is not valid or was not base64 encoded.', |
|
| 136 | - 'event_espresso' |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @see https://stackoverflow.com/a/51877882 |
|
| 145 | - * @param $string |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - public function isValidBase64($string) |
|
| 149 | - { |
|
| 150 | - // ensure data is a string |
|
| 151 | - if (! is_string($string) || ! $this->use_base64_encode) { |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - // first check if we're dealing with an actual valid base64 encoded string |
|
| 155 | - $decoded = base64_decode($string, true); |
|
| 156 | - if ($decoded === false) { |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - // finally, re-encode and compare it to original one |
|
| 160 | - return base64_encode($decoded) === $string; |
|
| 161 | - } |
|
| 18 | + /** |
|
| 19 | + * @var boolean |
|
| 20 | + */ |
|
| 21 | + protected $use_base64_encode; |
|
| 22 | + |
|
| 23 | + |
|
| 24 | + public function __construct() |
|
| 25 | + { |
|
| 26 | + $this->use_base64_encode = function_exists('base64_encode'); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * encodes string with PHP's base64 encoding |
|
| 32 | + * |
|
| 33 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 34 | + * @param string $text_string the text to be encoded |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function encodeString($text_string = '') |
|
| 38 | + { |
|
| 39 | + // you give me nothing??? GET OUT ! |
|
| 40 | + if (empty($text_string) || ! $this->use_base64_encode) { |
|
| 41 | + return $text_string; |
|
| 42 | + } |
|
| 43 | + // encode |
|
| 44 | + return base64_encode($text_string); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * decodes string that has been encoded with PHP's base64 encoding |
|
| 50 | + * |
|
| 51 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 52 | + * @param string $encoded_string the text to be decoded |
|
| 53 | + * @return string |
|
| 54 | + * @throws RuntimeException |
|
| 55 | + */ |
|
| 56 | + public function decodeString($encoded_string = '') |
|
| 57 | + { |
|
| 58 | + // you give me nothing??? GET OUT ! |
|
| 59 | + if (empty($encoded_string)) { |
|
| 60 | + return $encoded_string; |
|
| 61 | + } |
|
| 62 | + $this->isValidBase64OrFail($encoded_string); |
|
| 63 | + return $this->decode($encoded_string); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $encoded_string the text to be decoded |
|
| 69 | + * @return string |
|
| 70 | + * @throws RuntimeException |
|
| 71 | + */ |
|
| 72 | + private function decode($encoded_string) |
|
| 73 | + { |
|
| 74 | + $decoded_string = base64_decode($encoded_string); |
|
| 75 | + if ($decoded_string === false) { |
|
| 76 | + throw new RuntimeException( |
|
| 77 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 78 | + ); |
|
| 79 | + } |
|
| 80 | + return $decoded_string; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * encodes url string with PHP's base64 encoding |
|
| 86 | + * |
|
| 87 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 88 | + * @param string $text_string the text to be encoded |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function encodeUrl($text_string = '') |
|
| 92 | + { |
|
| 93 | + // you give me nothing??? GET OUT ! |
|
| 94 | + if (empty($text_string) || ! $this->use_base64_encode) { |
|
| 95 | + return $text_string; |
|
| 96 | + } |
|
| 97 | + // encode |
|
| 98 | + $encoded_string = base64_encode($text_string); |
|
| 99 | + // remove some chars to make encoding more URL friendly |
|
| 100 | + return rtrim(strtr($encoded_string, '+/', '-_'), '='); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
| 106 | + * |
|
| 107 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 108 | + * @param string $encoded_string the text to be decoded |
|
| 109 | + * @return string |
|
| 110 | + * @throws RuntimeException |
|
| 111 | + */ |
|
| 112 | + public function decodeUrl($encoded_string = '') |
|
| 113 | + { |
|
| 114 | + // you give me nothing??? GET OUT ! |
|
| 115 | + if (empty($encoded_string)) { |
|
| 116 | + return $encoded_string; |
|
| 117 | + } |
|
| 118 | + // replace previously removed characters |
|
| 119 | + $encoded_string = strtr($encoded_string, '-_', '+/'); |
|
| 120 | + $encoded_string .= str_repeat('=', 3 - (3 + strlen($encoded_string)) % 4); |
|
| 121 | + $this->isValidBase64OrFail($encoded_string); |
|
| 122 | + return $this->decode($encoded_string); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $encoded_string the text to be decoded |
|
| 128 | + * @throws RuntimeException |
|
| 129 | + */ |
|
| 130 | + public function isValidBase64OrFail($encoded_string) |
|
| 131 | + { |
|
| 132 | + if (! $this->isValidBase64($encoded_string)) { |
|
| 133 | + throw new RuntimeException( |
|
| 134 | + esc_html__( |
|
| 135 | + 'Base 64 decoding failed because the supplied string is not valid or was not base64 encoded.', |
|
| 136 | + 'event_espresso' |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @see https://stackoverflow.com/a/51877882 |
|
| 145 | + * @param $string |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + public function isValidBase64($string) |
|
| 149 | + { |
|
| 150 | + // ensure data is a string |
|
| 151 | + if (! is_string($string) || ! $this->use_base64_encode) { |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + // first check if we're dealing with an actual valid base64 encoded string |
|
| 155 | + $decoded = base64_decode($string, true); |
|
| 156 | + if ($decoded === false) { |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + // finally, re-encode and compare it to original one |
|
| 160 | + return base64_encode($decoded) === $string; |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public function isValidBase64OrFail($encoded_string) |
| 131 | 131 | { |
| 132 | - if (! $this->isValidBase64($encoded_string)) { |
|
| 132 | + if ( ! $this->isValidBase64($encoded_string)) { |
|
| 133 | 133 | throw new RuntimeException( |
| 134 | 134 | esc_html__( |
| 135 | 135 | 'Base 64 decoding failed because the supplied string is not valid or was not base64 encoded.', |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | public function isValidBase64($string) |
| 149 | 149 | { |
| 150 | 150 | // ensure data is a string |
| 151 | - if (! is_string($string) || ! $this->use_base64_encode) { |
|
| 151 | + if ( ! is_string($string) || ! $this->use_base64_encode) { |
|
| 152 | 152 | return false; |
| 153 | 153 | } |
| 154 | 154 | // first check if we're dealing with an actual valid base64 encoded string |
@@ -26,675 +26,675 @@ |
||
| 26 | 26 | class EE_Encryption implements InterminableInterface |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * key used for saving the encryption key to the wp_options table |
|
| 31 | - */ |
|
| 32 | - const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * the OPENSSL cipher method used |
|
| 36 | - */ |
|
| 37 | - const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * WP "options_name" used to store a verified available cipher method |
|
| 41 | - */ |
|
| 42 | - const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * the OPENSSL digest method used |
|
| 46 | - */ |
|
| 47 | - const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * separates the encrypted text from the initialization vector |
|
| 51 | - */ |
|
| 52 | - const OPENSSL_IV_DELIMITER = ':iv:'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * appended to text encrypted using the acme encryption |
|
| 56 | - */ |
|
| 57 | - const ACME_ENCRYPTION_FLAG = '::ae'; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * instance of the EE_Encryption object |
|
| 62 | - */ |
|
| 63 | - protected static $_instance; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var string $_encryption_key |
|
| 67 | - */ |
|
| 68 | - protected $_encryption_key; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var string $cipher_method |
|
| 72 | - */ |
|
| 73 | - private $cipher_method = ''; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var array $cipher_methods |
|
| 77 | - */ |
|
| 78 | - private $cipher_methods = []; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var array $digest_methods |
|
| 82 | - */ |
|
| 83 | - private $digest_methods = []; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @var boolean $_use_openssl_encrypt |
|
| 87 | - */ |
|
| 88 | - protected $_use_openssl_encrypt = false; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var boolean $_use_mcrypt |
|
| 92 | - */ |
|
| 93 | - protected $_use_mcrypt = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var boolean $_use_base64_encode |
|
| 97 | - */ |
|
| 98 | - protected $_use_base64_encode = false; |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * protected constructor to prevent direct creation |
|
| 103 | - */ |
|
| 104 | - protected function __construct() |
|
| 105 | - { |
|
| 106 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 107 | - define('ESPRESSO_ENCRYPT', true); |
|
| 108 | - } |
|
| 109 | - if (extension_loaded('openssl')) { |
|
| 110 | - $this->_use_openssl_encrypt = true; |
|
| 111 | - } elseif (extension_loaded('mcrypt')) { |
|
| 112 | - $this->_use_mcrypt = true; |
|
| 113 | - } |
|
| 114 | - if (function_exists('base64_encode')) { |
|
| 115 | - $this->_use_base64_encode = true; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * singleton method used to instantiate class object |
|
| 122 | - * |
|
| 123 | - * @return EE_Encryption |
|
| 124 | - */ |
|
| 125 | - public static function instance() |
|
| 126 | - { |
|
| 127 | - // check if class object is instantiated |
|
| 128 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 129 | - EE_Encryption::$_instance = new self(); |
|
| 130 | - } |
|
| 131 | - return EE_Encryption::$_instance; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * get encryption key |
|
| 137 | - * |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public function get_encryption_key() |
|
| 141 | - { |
|
| 142 | - // if encryption key has not been set |
|
| 143 | - if (empty($this->_encryption_key)) { |
|
| 144 | - // retrieve encryption_key from db |
|
| 145 | - $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
| 146 | - // WHAT?? No encryption_key in the db ?? |
|
| 147 | - if ($this->_encryption_key === '') { |
|
| 148 | - // let's make one. And md5 it to make it just the right size for a key |
|
| 149 | - $new_key = md5($this->generate_random_string()); |
|
| 150 | - // now save it to the db for later |
|
| 151 | - add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
| 152 | - // here's the key - FINALLY ! |
|
| 153 | - $this->_encryption_key = $new_key; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return $this->_encryption_key; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * encrypts data |
|
| 162 | - * |
|
| 163 | - * @param string $text_string - the text to be encrypted |
|
| 164 | - * @return string |
|
| 165 | - * @throws RuntimeException |
|
| 166 | - */ |
|
| 167 | - public function encrypt($text_string = '') |
|
| 168 | - { |
|
| 169 | - // you give me nothing??? GET OUT ! |
|
| 170 | - if (empty($text_string)) { |
|
| 171 | - return $text_string; |
|
| 172 | - } |
|
| 173 | - if ($this->_use_openssl_encrypt) { |
|
| 174 | - $encrypted_text = $this->openssl_encrypt($text_string); |
|
| 175 | - } else { |
|
| 176 | - $encrypted_text = $this->acme_encrypt($text_string); |
|
| 177 | - } |
|
| 178 | - return $encrypted_text; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * decrypts data |
|
| 184 | - * |
|
| 185 | - * @param string $encrypted_text - the text to be decrypted |
|
| 186 | - * @return string |
|
| 187 | - * @throws RuntimeException |
|
| 188 | - */ |
|
| 189 | - public function decrypt($encrypted_text = '') |
|
| 190 | - { |
|
| 191 | - // you give me nothing??? GET OUT ! |
|
| 192 | - if (empty($encrypted_text)) { |
|
| 193 | - return $encrypted_text; |
|
| 194 | - } |
|
| 195 | - // if PHP's mcrypt functions are installed then we'll use them |
|
| 196 | - if ($this->_use_openssl_encrypt) { |
|
| 197 | - $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
| 198 | - } else { |
|
| 199 | - $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
| 200 | - } |
|
| 201 | - return $decrypted_text; |
|
| 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 | - * encodes url string with PHP's base64 encoding |
|
| 250 | - * |
|
| 251 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 252 | - * @param string $text_string the text to be encoded |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public function base64_url_encode($text_string = '') |
|
| 256 | - { |
|
| 257 | - // you give me nothing??? GET OUT ! |
|
| 258 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 259 | - return $text_string; |
|
| 260 | - } |
|
| 261 | - // encode |
|
| 262 | - $encoded_string = base64_encode($text_string); |
|
| 263 | - // remove chars to make encoding more URL friendly |
|
| 264 | - return strtr($encoded_string, '+/=', '-_,'); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
| 270 | - * |
|
| 271 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
| 272 | - * @param string $encoded_string the text to be decoded |
|
| 273 | - * @return string |
|
| 274 | - * @throws RuntimeException |
|
| 275 | - */ |
|
| 276 | - public function base64_url_decode($encoded_string = '') |
|
| 277 | - { |
|
| 278 | - // replace previously removed characters |
|
| 279 | - $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
| 280 | - // you give me nothing??? GET OUT ! |
|
| 281 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 282 | - return $encoded_string; |
|
| 283 | - } |
|
| 284 | - // decode |
|
| 285 | - $decoded_string = base64_decode($encoded_string); |
|
| 286 | - if ($decoded_string === false) { |
|
| 287 | - throw new RuntimeException( |
|
| 288 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 289 | - ); |
|
| 290 | - } |
|
| 291 | - return $decoded_string; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * encrypts data using PHP's openssl functions |
|
| 297 | - * |
|
| 298 | - * @param string $text_string the text to be encrypted |
|
| 299 | - * @param string $cipher_method |
|
| 300 | - * @param string $encryption_key |
|
| 301 | - * @return string |
|
| 302 | - * @throws RuntimeException |
|
| 303 | - */ |
|
| 304 | - protected function openssl_encrypt( |
|
| 305 | - $text_string = '', |
|
| 306 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 307 | - $encryption_key = '' |
|
| 308 | - ) { |
|
| 309 | - // you give me nothing??? GET OUT ! |
|
| 310 | - if (empty($text_string)) { |
|
| 311 | - return $text_string; |
|
| 312 | - } |
|
| 313 | - $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
| 314 | - // get initialization vector size |
|
| 315 | - $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
| 316 | - // generate initialization vector. |
|
| 317 | - // The second parameter ("crypto_strong") is passed by reference, |
|
| 318 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 319 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 320 | - $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
| 321 | - if ($iv === false || $is_strong === false) { |
|
| 322 | - throw new RuntimeException( |
|
| 323 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 324 | - ); |
|
| 325 | - } |
|
| 326 | - // encrypt it |
|
| 327 | - $encrypted_text = openssl_encrypt( |
|
| 328 | - $text_string, |
|
| 329 | - $this->cipher_method, |
|
| 330 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 331 | - 0, |
|
| 332 | - $iv |
|
| 333 | - ); |
|
| 334 | - // append the initialization vector |
|
| 335 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 336 | - // trim and maybe encode |
|
| 337 | - return $this->_use_base64_encode |
|
| 338 | - ? trim(base64_encode($encrypted_text)) |
|
| 339 | - : trim($encrypted_text); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Returns a cipher method that has been verified to work. |
|
| 345 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
| 346 | - * Then tests the incoming default and returns that if it's good. |
|
| 347 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
| 348 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 349 | - * to see what is available on the server, and returns the results. |
|
| 350 | - * |
|
| 351 | - * @param string $cipher_method |
|
| 352 | - * @return string |
|
| 353 | - * @throws RuntimeException |
|
| 354 | - */ |
|
| 355 | - protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
| 356 | - { |
|
| 357 | - if ($this->cipher_method !== '') { |
|
| 358 | - return $this->cipher_method; |
|
| 359 | - } |
|
| 360 | - // verify that the default cipher method can produce an initialization vector |
|
| 361 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 362 | - // nope? okay let's get what we found in the past to work |
|
| 363 | - $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
| 364 | - // oops... haven't tested available cipher methods yet |
|
| 365 | - if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 366 | - $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - return $cipher_method; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @param string $cipher_method |
|
| 375 | - * @return string |
|
| 376 | - * @throws \RuntimeException |
|
| 377 | - */ |
|
| 378 | - protected function getAvailableCipherMethod($cipher_method) |
|
| 379 | - { |
|
| 380 | - // verify that the incoming cipher method can produce an initialization vector |
|
| 381 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 382 | - // nope? then check the next cipher in the list of available cipher methods |
|
| 383 | - $cipher_method = next($this->cipher_methods); |
|
| 384 | - // what? there's no list? then generate that list and cache it, |
|
| 385 | - if (empty($this->cipher_methods)) { |
|
| 386 | - $this->cipher_methods = openssl_get_cipher_methods(); |
|
| 387 | - // then grab the first item from the list |
|
| 388 | - $cipher_method = reset($this->cipher_methods); |
|
| 389 | - } |
|
| 390 | - if ($cipher_method === false) { |
|
| 391 | - throw new RuntimeException( |
|
| 392 | - esc_html__( |
|
| 393 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 394 | - 'event_espresso' |
|
| 395 | - ) |
|
| 396 | - ); |
|
| 397 | - } |
|
| 398 | - // verify that the next cipher method works |
|
| 399 | - return $this->getAvailableCipherMethod($cipher_method); |
|
| 400 | - } |
|
| 401 | - // if we've gotten this far, then we found an available cipher method that works |
|
| 402 | - // so save that for next time |
|
| 403 | - update_option( |
|
| 404 | - EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
| 405 | - $cipher_method |
|
| 406 | - ); |
|
| 407 | - return $cipher_method; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * decrypts data that has been encrypted with PHP's openssl functions |
|
| 413 | - * |
|
| 414 | - * @param string $encrypted_text the text to be decrypted |
|
| 415 | - * @param string $cipher_method |
|
| 416 | - * @param string $encryption_key |
|
| 417 | - * @return string |
|
| 418 | - * @throws RuntimeException |
|
| 419 | - */ |
|
| 420 | - protected function openssl_decrypt( |
|
| 421 | - $encrypted_text = '', |
|
| 422 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 423 | - $encryption_key = '' |
|
| 424 | - ) { |
|
| 425 | - // you give me nothing??? GET OUT ! |
|
| 426 | - if (empty($encrypted_text)) { |
|
| 427 | - return $encrypted_text; |
|
| 428 | - } |
|
| 429 | - // decode |
|
| 430 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 431 | - ? $this->base64_url_decode($encrypted_text) |
|
| 432 | - : $encrypted_text; |
|
| 433 | - $encrypted_components = explode( |
|
| 434 | - EE_Encryption::OPENSSL_IV_DELIMITER, |
|
| 435 | - $encrypted_text, |
|
| 436 | - 2 |
|
| 437 | - ); |
|
| 438 | - // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
| 439 | - if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
| 440 | - return $this->m_decrypt($encrypted_text); |
|
| 441 | - } |
|
| 442 | - // decrypt it |
|
| 443 | - $decrypted_text = openssl_decrypt( |
|
| 444 | - $encrypted_components[0], |
|
| 445 | - $this->getCipherMethod($cipher_method), |
|
| 446 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 447 | - 0, |
|
| 448 | - $encrypted_components[1] |
|
| 449 | - ); |
|
| 450 | - $decrypted_text = trim($decrypted_text); |
|
| 451 | - return $decrypted_text; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Computes the digest hash value using the specified digest method. |
|
| 457 | - * If that digest method fails to produce a valid hash value, |
|
| 458 | - * then we'll grab the next digest method and recursively try again until something works. |
|
| 459 | - * |
|
| 460 | - * @param string $digest_method |
|
| 461 | - * @param string $encryption_key |
|
| 462 | - * @return string |
|
| 463 | - * @throws RuntimeException |
|
| 464 | - */ |
|
| 465 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
| 466 | - { |
|
| 467 | - $encryption_key = $encryption_key !== '' |
|
| 468 | - ? $encryption_key |
|
| 469 | - : $this->get_encryption_key(); |
|
| 470 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
| 471 | - if ($digest_hash_value === false) { |
|
| 472 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 473 | - } |
|
| 474 | - return $digest_hash_value; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Returns the NEXT element in the $digest_methods array. |
|
| 480 | - * If the $digest_methods array is empty, then we populate it |
|
| 481 | - * with the available values returned from openssl_get_md_methods(). |
|
| 482 | - * |
|
| 483 | - * @return string |
|
| 484 | - * @throws \RuntimeException |
|
| 485 | - */ |
|
| 486 | - protected function getDigestMethod() |
|
| 487 | - { |
|
| 488 | - $digest_method = prev($this->digest_methods); |
|
| 489 | - if (empty($this->digest_methods)) { |
|
| 490 | - $this->digest_methods = openssl_get_md_methods(); |
|
| 491 | - $digest_method = end($this->digest_methods); |
|
| 492 | - } |
|
| 493 | - if ($digest_method === false) { |
|
| 494 | - throw new RuntimeException( |
|
| 495 | - esc_html__( |
|
| 496 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 497 | - 'event_espresso' |
|
| 498 | - ) |
|
| 499 | - ); |
|
| 500 | - } |
|
| 501 | - return $digest_method; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 507 | - * |
|
| 508 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 509 | - * @param string $text_string the text to be decrypted |
|
| 510 | - * @return string |
|
| 511 | - */ |
|
| 512 | - protected function acme_encrypt($text_string = '') |
|
| 513 | - { |
|
| 514 | - // you give me nothing??? GET OUT ! |
|
| 515 | - if (empty($text_string)) { |
|
| 516 | - return $text_string; |
|
| 517 | - } |
|
| 518 | - $key_bits = str_split( |
|
| 519 | - str_pad( |
|
| 520 | - '', |
|
| 521 | - strlen($text_string), |
|
| 522 | - $this->get_encryption_key(), |
|
| 523 | - STR_PAD_RIGHT |
|
| 524 | - ) |
|
| 525 | - ); |
|
| 526 | - $string_bits = str_split($text_string); |
|
| 527 | - foreach ($string_bits as $k => $v) { |
|
| 528 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
| 529 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 530 | - } |
|
| 531 | - $encrypted_text = implode('', $string_bits); |
|
| 532 | - $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
| 533 | - return $this->_use_base64_encode |
|
| 534 | - ? base64_encode($encrypted_text) |
|
| 535 | - : $encrypted_text; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 541 | - * |
|
| 542 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 543 | - * @param string $encrypted_text the text to be decrypted |
|
| 544 | - * @return string |
|
| 545 | - * @throws RuntimeException |
|
| 546 | - */ |
|
| 547 | - protected function acme_decrypt($encrypted_text = '') |
|
| 548 | - { |
|
| 549 | - // you give me nothing??? GET OUT ! |
|
| 550 | - if (empty($encrypted_text)) { |
|
| 551 | - return $encrypted_text; |
|
| 552 | - } |
|
| 553 | - // decode the data ? |
|
| 554 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 555 | - ? $this->base64_url_decode($encrypted_text) |
|
| 556 | - : $encrypted_text; |
|
| 557 | - if ($this->_use_mcrypt |
|
| 558 | - && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
| 559 | - ) { |
|
| 560 | - return $this->m_decrypt($encrypted_text); |
|
| 561 | - } |
|
| 562 | - $encrypted_text = substr($encrypted_text, 0, -4); |
|
| 563 | - $key_bits = str_split( |
|
| 564 | - str_pad( |
|
| 565 | - '', |
|
| 566 | - strlen($encrypted_text), |
|
| 567 | - $this->get_encryption_key(), |
|
| 568 | - STR_PAD_RIGHT |
|
| 569 | - ) |
|
| 570 | - ); |
|
| 571 | - $string_bits = str_split($encrypted_text); |
|
| 572 | - foreach ($string_bits as $k => $v) { |
|
| 573 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
| 574 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 575 | - } |
|
| 576 | - return implode('', $string_bits); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
| 582 | - * @param $string |
|
| 583 | - * @return bool |
|
| 584 | - */ |
|
| 585 | - protected function valid_base_64($string) |
|
| 586 | - { |
|
| 587 | - // ensure data is a string |
|
| 588 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 589 | - return false; |
|
| 590 | - } |
|
| 591 | - $decoded = base64_decode($string, true); |
|
| 592 | - // Check if there is no invalid character in string |
|
| 593 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 594 | - return false; |
|
| 595 | - } |
|
| 596 | - // Decode the string in strict mode and send the response |
|
| 597 | - if (! base64_decode($string, true)) { |
|
| 598 | - return false; |
|
| 599 | - } |
|
| 600 | - // Encode and compare it to original one |
|
| 601 | - return base64_encode($decoded) === $string; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * generate random string |
|
| 607 | - * |
|
| 608 | - * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 609 | - * @param int $length number of characters for random string |
|
| 610 | - * @return string |
|
| 611 | - */ |
|
| 612 | - public function generate_random_string($length = 40) |
|
| 613 | - { |
|
| 614 | - $iterations = ceil($length / 40); |
|
| 615 | - $random_string = ''; |
|
| 616 | - for ($i = 0; $i < $iterations; $i++) { |
|
| 617 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 618 | - } |
|
| 619 | - $random_string = substr($random_string, 0, $length); |
|
| 620 | - return $random_string; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * encrypts data using PHP's mcrypt functions |
|
| 626 | - * |
|
| 627 | - * @param string $text_string |
|
| 628 | - * @return string |
|
| 629 | - * @throws RuntimeException |
|
| 630 | - * @deprecated 4.9.39 |
|
| 631 | - * @internal param $string - the text to be encrypted |
|
| 632 | - */ |
|
| 633 | - protected function m_encrypt($text_string = '') |
|
| 634 | - { |
|
| 635 | - // you give me nothing??? GET OUT ! |
|
| 636 | - if (empty($text_string)) { |
|
| 637 | - return $text_string; |
|
| 638 | - } |
|
| 639 | - // get the initialization vector size |
|
| 640 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 641 | - // initialization vector |
|
| 642 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 643 | - if ($iv === false) { |
|
| 644 | - throw new RuntimeException( |
|
| 645 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 646 | - ); |
|
| 647 | - } |
|
| 648 | - // encrypt it |
|
| 649 | - $encrypted_text = mcrypt_encrypt( |
|
| 650 | - MCRYPT_RIJNDAEL_256, |
|
| 651 | - $this->get_encryption_key(), |
|
| 652 | - $text_string, |
|
| 653 | - MCRYPT_MODE_ECB, |
|
| 654 | - $iv |
|
| 655 | - ); |
|
| 656 | - // trim and maybe encode |
|
| 657 | - return $this->_use_base64_encode |
|
| 658 | - ? trim(base64_encode($encrypted_text)) |
|
| 659 | - : trim($encrypted_text); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * decrypts data that has been encrypted with PHP's mcrypt functions |
|
| 665 | - * |
|
| 666 | - * @param string $encrypted_text the text to be decrypted |
|
| 667 | - * @return string |
|
| 668 | - * @throws RuntimeException |
|
| 669 | - * @deprecated 4.9.39 |
|
| 670 | - */ |
|
| 671 | - protected function m_decrypt($encrypted_text = '') |
|
| 672 | - { |
|
| 673 | - // you give me nothing??? GET OUT ! |
|
| 674 | - if (empty($encrypted_text)) { |
|
| 675 | - return $encrypted_text; |
|
| 676 | - } |
|
| 677 | - // decode |
|
| 678 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 679 | - ? $this->base64_url_decode($encrypted_text) |
|
| 680 | - : $encrypted_text; |
|
| 681 | - // get the initialization vector size |
|
| 682 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 683 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 684 | - if ($iv === false) { |
|
| 685 | - throw new RuntimeException( |
|
| 686 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 687 | - ); |
|
| 688 | - } |
|
| 689 | - // decrypt it |
|
| 690 | - $decrypted_text = mcrypt_decrypt( |
|
| 691 | - MCRYPT_RIJNDAEL_256, |
|
| 692 | - $this->get_encryption_key(), |
|
| 693 | - $encrypted_text, |
|
| 694 | - MCRYPT_MODE_ECB, |
|
| 695 | - $iv |
|
| 696 | - ); |
|
| 697 | - $decrypted_text = trim($decrypted_text); |
|
| 698 | - return $decrypted_text; |
|
| 699 | - } |
|
| 29 | + /** |
|
| 30 | + * key used for saving the encryption key to the wp_options table |
|
| 31 | + */ |
|
| 32 | + const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * the OPENSSL cipher method used |
|
| 36 | + */ |
|
| 37 | + const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * WP "options_name" used to store a verified available cipher method |
|
| 41 | + */ |
|
| 42 | + const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * the OPENSSL digest method used |
|
| 46 | + */ |
|
| 47 | + const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * separates the encrypted text from the initialization vector |
|
| 51 | + */ |
|
| 52 | + const OPENSSL_IV_DELIMITER = ':iv:'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * appended to text encrypted using the acme encryption |
|
| 56 | + */ |
|
| 57 | + const ACME_ENCRYPTION_FLAG = '::ae'; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * instance of the EE_Encryption object |
|
| 62 | + */ |
|
| 63 | + protected static $_instance; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var string $_encryption_key |
|
| 67 | + */ |
|
| 68 | + protected $_encryption_key; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var string $cipher_method |
|
| 72 | + */ |
|
| 73 | + private $cipher_method = ''; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var array $cipher_methods |
|
| 77 | + */ |
|
| 78 | + private $cipher_methods = []; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var array $digest_methods |
|
| 82 | + */ |
|
| 83 | + private $digest_methods = []; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @var boolean $_use_openssl_encrypt |
|
| 87 | + */ |
|
| 88 | + protected $_use_openssl_encrypt = false; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var boolean $_use_mcrypt |
|
| 92 | + */ |
|
| 93 | + protected $_use_mcrypt = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var boolean $_use_base64_encode |
|
| 97 | + */ |
|
| 98 | + protected $_use_base64_encode = false; |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * protected constructor to prevent direct creation |
|
| 103 | + */ |
|
| 104 | + protected function __construct() |
|
| 105 | + { |
|
| 106 | + if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 107 | + define('ESPRESSO_ENCRYPT', true); |
|
| 108 | + } |
|
| 109 | + if (extension_loaded('openssl')) { |
|
| 110 | + $this->_use_openssl_encrypt = true; |
|
| 111 | + } elseif (extension_loaded('mcrypt')) { |
|
| 112 | + $this->_use_mcrypt = true; |
|
| 113 | + } |
|
| 114 | + if (function_exists('base64_encode')) { |
|
| 115 | + $this->_use_base64_encode = true; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * singleton method used to instantiate class object |
|
| 122 | + * |
|
| 123 | + * @return EE_Encryption |
|
| 124 | + */ |
|
| 125 | + public static function instance() |
|
| 126 | + { |
|
| 127 | + // check if class object is instantiated |
|
| 128 | + if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 129 | + EE_Encryption::$_instance = new self(); |
|
| 130 | + } |
|
| 131 | + return EE_Encryption::$_instance; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * get encryption key |
|
| 137 | + * |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public function get_encryption_key() |
|
| 141 | + { |
|
| 142 | + // if encryption key has not been set |
|
| 143 | + if (empty($this->_encryption_key)) { |
|
| 144 | + // retrieve encryption_key from db |
|
| 145 | + $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
| 146 | + // WHAT?? No encryption_key in the db ?? |
|
| 147 | + if ($this->_encryption_key === '') { |
|
| 148 | + // let's make one. And md5 it to make it just the right size for a key |
|
| 149 | + $new_key = md5($this->generate_random_string()); |
|
| 150 | + // now save it to the db for later |
|
| 151 | + add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
| 152 | + // here's the key - FINALLY ! |
|
| 153 | + $this->_encryption_key = $new_key; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return $this->_encryption_key; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * encrypts data |
|
| 162 | + * |
|
| 163 | + * @param string $text_string - the text to be encrypted |
|
| 164 | + * @return string |
|
| 165 | + * @throws RuntimeException |
|
| 166 | + */ |
|
| 167 | + public function encrypt($text_string = '') |
|
| 168 | + { |
|
| 169 | + // you give me nothing??? GET OUT ! |
|
| 170 | + if (empty($text_string)) { |
|
| 171 | + return $text_string; |
|
| 172 | + } |
|
| 173 | + if ($this->_use_openssl_encrypt) { |
|
| 174 | + $encrypted_text = $this->openssl_encrypt($text_string); |
|
| 175 | + } else { |
|
| 176 | + $encrypted_text = $this->acme_encrypt($text_string); |
|
| 177 | + } |
|
| 178 | + return $encrypted_text; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * decrypts data |
|
| 184 | + * |
|
| 185 | + * @param string $encrypted_text - the text to be decrypted |
|
| 186 | + * @return string |
|
| 187 | + * @throws RuntimeException |
|
| 188 | + */ |
|
| 189 | + public function decrypt($encrypted_text = '') |
|
| 190 | + { |
|
| 191 | + // you give me nothing??? GET OUT ! |
|
| 192 | + if (empty($encrypted_text)) { |
|
| 193 | + return $encrypted_text; |
|
| 194 | + } |
|
| 195 | + // if PHP's mcrypt functions are installed then we'll use them |
|
| 196 | + if ($this->_use_openssl_encrypt) { |
|
| 197 | + $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
| 198 | + } else { |
|
| 199 | + $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
| 200 | + } |
|
| 201 | + return $decrypted_text; |
|
| 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 | + * encodes url string with PHP's base64 encoding |
|
| 250 | + * |
|
| 251 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 252 | + * @param string $text_string the text to be encoded |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public function base64_url_encode($text_string = '') |
|
| 256 | + { |
|
| 257 | + // you give me nothing??? GET OUT ! |
|
| 258 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
| 259 | + return $text_string; |
|
| 260 | + } |
|
| 261 | + // encode |
|
| 262 | + $encoded_string = base64_encode($text_string); |
|
| 263 | + // remove chars to make encoding more URL friendly |
|
| 264 | + return strtr($encoded_string, '+/=', '-_,'); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
| 270 | + * |
|
| 271 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
| 272 | + * @param string $encoded_string the text to be decoded |
|
| 273 | + * @return string |
|
| 274 | + * @throws RuntimeException |
|
| 275 | + */ |
|
| 276 | + public function base64_url_decode($encoded_string = '') |
|
| 277 | + { |
|
| 278 | + // replace previously removed characters |
|
| 279 | + $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
| 280 | + // you give me nothing??? GET OUT ! |
|
| 281 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
| 282 | + return $encoded_string; |
|
| 283 | + } |
|
| 284 | + // decode |
|
| 285 | + $decoded_string = base64_decode($encoded_string); |
|
| 286 | + if ($decoded_string === false) { |
|
| 287 | + throw new RuntimeException( |
|
| 288 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
| 289 | + ); |
|
| 290 | + } |
|
| 291 | + return $decoded_string; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * encrypts data using PHP's openssl functions |
|
| 297 | + * |
|
| 298 | + * @param string $text_string the text to be encrypted |
|
| 299 | + * @param string $cipher_method |
|
| 300 | + * @param string $encryption_key |
|
| 301 | + * @return string |
|
| 302 | + * @throws RuntimeException |
|
| 303 | + */ |
|
| 304 | + protected function openssl_encrypt( |
|
| 305 | + $text_string = '', |
|
| 306 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 307 | + $encryption_key = '' |
|
| 308 | + ) { |
|
| 309 | + // you give me nothing??? GET OUT ! |
|
| 310 | + if (empty($text_string)) { |
|
| 311 | + return $text_string; |
|
| 312 | + } |
|
| 313 | + $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
| 314 | + // get initialization vector size |
|
| 315 | + $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
| 316 | + // generate initialization vector. |
|
| 317 | + // The second parameter ("crypto_strong") is passed by reference, |
|
| 318 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
| 319 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
| 320 | + $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
| 321 | + if ($iv === false || $is_strong === false) { |
|
| 322 | + throw new RuntimeException( |
|
| 323 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
| 324 | + ); |
|
| 325 | + } |
|
| 326 | + // encrypt it |
|
| 327 | + $encrypted_text = openssl_encrypt( |
|
| 328 | + $text_string, |
|
| 329 | + $this->cipher_method, |
|
| 330 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 331 | + 0, |
|
| 332 | + $iv |
|
| 333 | + ); |
|
| 334 | + // append the initialization vector |
|
| 335 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 336 | + // trim and maybe encode |
|
| 337 | + return $this->_use_base64_encode |
|
| 338 | + ? trim(base64_encode($encrypted_text)) |
|
| 339 | + : trim($encrypted_text); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Returns a cipher method that has been verified to work. |
|
| 345 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
| 346 | + * Then tests the incoming default and returns that if it's good. |
|
| 347 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
| 348 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
| 349 | + * to see what is available on the server, and returns the results. |
|
| 350 | + * |
|
| 351 | + * @param string $cipher_method |
|
| 352 | + * @return string |
|
| 353 | + * @throws RuntimeException |
|
| 354 | + */ |
|
| 355 | + protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
| 356 | + { |
|
| 357 | + if ($this->cipher_method !== '') { |
|
| 358 | + return $this->cipher_method; |
|
| 359 | + } |
|
| 360 | + // verify that the default cipher method can produce an initialization vector |
|
| 361 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 362 | + // nope? okay let's get what we found in the past to work |
|
| 363 | + $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
| 364 | + // oops... haven't tested available cipher methods yet |
|
| 365 | + if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
| 366 | + $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + return $cipher_method; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @param string $cipher_method |
|
| 375 | + * @return string |
|
| 376 | + * @throws \RuntimeException |
|
| 377 | + */ |
|
| 378 | + protected function getAvailableCipherMethod($cipher_method) |
|
| 379 | + { |
|
| 380 | + // verify that the incoming cipher method can produce an initialization vector |
|
| 381 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
| 382 | + // nope? then check the next cipher in the list of available cipher methods |
|
| 383 | + $cipher_method = next($this->cipher_methods); |
|
| 384 | + // what? there's no list? then generate that list and cache it, |
|
| 385 | + if (empty($this->cipher_methods)) { |
|
| 386 | + $this->cipher_methods = openssl_get_cipher_methods(); |
|
| 387 | + // then grab the first item from the list |
|
| 388 | + $cipher_method = reset($this->cipher_methods); |
|
| 389 | + } |
|
| 390 | + if ($cipher_method === false) { |
|
| 391 | + throw new RuntimeException( |
|
| 392 | + esc_html__( |
|
| 393 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
| 394 | + 'event_espresso' |
|
| 395 | + ) |
|
| 396 | + ); |
|
| 397 | + } |
|
| 398 | + // verify that the next cipher method works |
|
| 399 | + return $this->getAvailableCipherMethod($cipher_method); |
|
| 400 | + } |
|
| 401 | + // if we've gotten this far, then we found an available cipher method that works |
|
| 402 | + // so save that for next time |
|
| 403 | + update_option( |
|
| 404 | + EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
| 405 | + $cipher_method |
|
| 406 | + ); |
|
| 407 | + return $cipher_method; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * decrypts data that has been encrypted with PHP's openssl functions |
|
| 413 | + * |
|
| 414 | + * @param string $encrypted_text the text to be decrypted |
|
| 415 | + * @param string $cipher_method |
|
| 416 | + * @param string $encryption_key |
|
| 417 | + * @return string |
|
| 418 | + * @throws RuntimeException |
|
| 419 | + */ |
|
| 420 | + protected function openssl_decrypt( |
|
| 421 | + $encrypted_text = '', |
|
| 422 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
| 423 | + $encryption_key = '' |
|
| 424 | + ) { |
|
| 425 | + // you give me nothing??? GET OUT ! |
|
| 426 | + if (empty($encrypted_text)) { |
|
| 427 | + return $encrypted_text; |
|
| 428 | + } |
|
| 429 | + // decode |
|
| 430 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 431 | + ? $this->base64_url_decode($encrypted_text) |
|
| 432 | + : $encrypted_text; |
|
| 433 | + $encrypted_components = explode( |
|
| 434 | + EE_Encryption::OPENSSL_IV_DELIMITER, |
|
| 435 | + $encrypted_text, |
|
| 436 | + 2 |
|
| 437 | + ); |
|
| 438 | + // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
| 439 | + if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
| 440 | + return $this->m_decrypt($encrypted_text); |
|
| 441 | + } |
|
| 442 | + // decrypt it |
|
| 443 | + $decrypted_text = openssl_decrypt( |
|
| 444 | + $encrypted_components[0], |
|
| 445 | + $this->getCipherMethod($cipher_method), |
|
| 446 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
| 447 | + 0, |
|
| 448 | + $encrypted_components[1] |
|
| 449 | + ); |
|
| 450 | + $decrypted_text = trim($decrypted_text); |
|
| 451 | + return $decrypted_text; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Computes the digest hash value using the specified digest method. |
|
| 457 | + * If that digest method fails to produce a valid hash value, |
|
| 458 | + * then we'll grab the next digest method and recursively try again until something works. |
|
| 459 | + * |
|
| 460 | + * @param string $digest_method |
|
| 461 | + * @param string $encryption_key |
|
| 462 | + * @return string |
|
| 463 | + * @throws RuntimeException |
|
| 464 | + */ |
|
| 465 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
| 466 | + { |
|
| 467 | + $encryption_key = $encryption_key !== '' |
|
| 468 | + ? $encryption_key |
|
| 469 | + : $this->get_encryption_key(); |
|
| 470 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
| 471 | + if ($digest_hash_value === false) { |
|
| 472 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
| 473 | + } |
|
| 474 | + return $digest_hash_value; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Returns the NEXT element in the $digest_methods array. |
|
| 480 | + * If the $digest_methods array is empty, then we populate it |
|
| 481 | + * with the available values returned from openssl_get_md_methods(). |
|
| 482 | + * |
|
| 483 | + * @return string |
|
| 484 | + * @throws \RuntimeException |
|
| 485 | + */ |
|
| 486 | + protected function getDigestMethod() |
|
| 487 | + { |
|
| 488 | + $digest_method = prev($this->digest_methods); |
|
| 489 | + if (empty($this->digest_methods)) { |
|
| 490 | + $this->digest_methods = openssl_get_md_methods(); |
|
| 491 | + $digest_method = end($this->digest_methods); |
|
| 492 | + } |
|
| 493 | + if ($digest_method === false) { |
|
| 494 | + throw new RuntimeException( |
|
| 495 | + esc_html__( |
|
| 496 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
| 497 | + 'event_espresso' |
|
| 498 | + ) |
|
| 499 | + ); |
|
| 500 | + } |
|
| 501 | + return $digest_method; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 507 | + * |
|
| 508 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 509 | + * @param string $text_string the text to be decrypted |
|
| 510 | + * @return string |
|
| 511 | + */ |
|
| 512 | + protected function acme_encrypt($text_string = '') |
|
| 513 | + { |
|
| 514 | + // you give me nothing??? GET OUT ! |
|
| 515 | + if (empty($text_string)) { |
|
| 516 | + return $text_string; |
|
| 517 | + } |
|
| 518 | + $key_bits = str_split( |
|
| 519 | + str_pad( |
|
| 520 | + '', |
|
| 521 | + strlen($text_string), |
|
| 522 | + $this->get_encryption_key(), |
|
| 523 | + STR_PAD_RIGHT |
|
| 524 | + ) |
|
| 525 | + ); |
|
| 526 | + $string_bits = str_split($text_string); |
|
| 527 | + foreach ($string_bits as $k => $v) { |
|
| 528 | + $temp = ord($v) + ord($key_bits[ $k ]); |
|
| 529 | + $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 530 | + } |
|
| 531 | + $encrypted_text = implode('', $string_bits); |
|
| 532 | + $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
| 533 | + return $this->_use_base64_encode |
|
| 534 | + ? base64_encode($encrypted_text) |
|
| 535 | + : $encrypted_text; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
| 541 | + * |
|
| 542 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
| 543 | + * @param string $encrypted_text the text to be decrypted |
|
| 544 | + * @return string |
|
| 545 | + * @throws RuntimeException |
|
| 546 | + */ |
|
| 547 | + protected function acme_decrypt($encrypted_text = '') |
|
| 548 | + { |
|
| 549 | + // you give me nothing??? GET OUT ! |
|
| 550 | + if (empty($encrypted_text)) { |
|
| 551 | + return $encrypted_text; |
|
| 552 | + } |
|
| 553 | + // decode the data ? |
|
| 554 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 555 | + ? $this->base64_url_decode($encrypted_text) |
|
| 556 | + : $encrypted_text; |
|
| 557 | + if ($this->_use_mcrypt |
|
| 558 | + && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
| 559 | + ) { |
|
| 560 | + return $this->m_decrypt($encrypted_text); |
|
| 561 | + } |
|
| 562 | + $encrypted_text = substr($encrypted_text, 0, -4); |
|
| 563 | + $key_bits = str_split( |
|
| 564 | + str_pad( |
|
| 565 | + '', |
|
| 566 | + strlen($encrypted_text), |
|
| 567 | + $this->get_encryption_key(), |
|
| 568 | + STR_PAD_RIGHT |
|
| 569 | + ) |
|
| 570 | + ); |
|
| 571 | + $string_bits = str_split($encrypted_text); |
|
| 572 | + foreach ($string_bits as $k => $v) { |
|
| 573 | + $temp = ord($v) - ord($key_bits[ $k ]); |
|
| 574 | + $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 575 | + } |
|
| 576 | + return implode('', $string_bits); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
| 582 | + * @param $string |
|
| 583 | + * @return bool |
|
| 584 | + */ |
|
| 585 | + protected function valid_base_64($string) |
|
| 586 | + { |
|
| 587 | + // ensure data is a string |
|
| 588 | + if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 589 | + return false; |
|
| 590 | + } |
|
| 591 | + $decoded = base64_decode($string, true); |
|
| 592 | + // Check if there is no invalid character in string |
|
| 593 | + if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 594 | + return false; |
|
| 595 | + } |
|
| 596 | + // Decode the string in strict mode and send the response |
|
| 597 | + if (! base64_decode($string, true)) { |
|
| 598 | + return false; |
|
| 599 | + } |
|
| 600 | + // Encode and compare it to original one |
|
| 601 | + return base64_encode($decoded) === $string; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * generate random string |
|
| 607 | + * |
|
| 608 | + * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
| 609 | + * @param int $length number of characters for random string |
|
| 610 | + * @return string |
|
| 611 | + */ |
|
| 612 | + public function generate_random_string($length = 40) |
|
| 613 | + { |
|
| 614 | + $iterations = ceil($length / 40); |
|
| 615 | + $random_string = ''; |
|
| 616 | + for ($i = 0; $i < $iterations; $i++) { |
|
| 617 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 618 | + } |
|
| 619 | + $random_string = substr($random_string, 0, $length); |
|
| 620 | + return $random_string; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * encrypts data using PHP's mcrypt functions |
|
| 626 | + * |
|
| 627 | + * @param string $text_string |
|
| 628 | + * @return string |
|
| 629 | + * @throws RuntimeException |
|
| 630 | + * @deprecated 4.9.39 |
|
| 631 | + * @internal param $string - the text to be encrypted |
|
| 632 | + */ |
|
| 633 | + protected function m_encrypt($text_string = '') |
|
| 634 | + { |
|
| 635 | + // you give me nothing??? GET OUT ! |
|
| 636 | + if (empty($text_string)) { |
|
| 637 | + return $text_string; |
|
| 638 | + } |
|
| 639 | + // get the initialization vector size |
|
| 640 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 641 | + // initialization vector |
|
| 642 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 643 | + if ($iv === false) { |
|
| 644 | + throw new RuntimeException( |
|
| 645 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 646 | + ); |
|
| 647 | + } |
|
| 648 | + // encrypt it |
|
| 649 | + $encrypted_text = mcrypt_encrypt( |
|
| 650 | + MCRYPT_RIJNDAEL_256, |
|
| 651 | + $this->get_encryption_key(), |
|
| 652 | + $text_string, |
|
| 653 | + MCRYPT_MODE_ECB, |
|
| 654 | + $iv |
|
| 655 | + ); |
|
| 656 | + // trim and maybe encode |
|
| 657 | + return $this->_use_base64_encode |
|
| 658 | + ? trim(base64_encode($encrypted_text)) |
|
| 659 | + : trim($encrypted_text); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * decrypts data that has been encrypted with PHP's mcrypt functions |
|
| 665 | + * |
|
| 666 | + * @param string $encrypted_text the text to be decrypted |
|
| 667 | + * @return string |
|
| 668 | + * @throws RuntimeException |
|
| 669 | + * @deprecated 4.9.39 |
|
| 670 | + */ |
|
| 671 | + protected function m_decrypt($encrypted_text = '') |
|
| 672 | + { |
|
| 673 | + // you give me nothing??? GET OUT ! |
|
| 674 | + if (empty($encrypted_text)) { |
|
| 675 | + return $encrypted_text; |
|
| 676 | + } |
|
| 677 | + // decode |
|
| 678 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
| 679 | + ? $this->base64_url_decode($encrypted_text) |
|
| 680 | + : $encrypted_text; |
|
| 681 | + // get the initialization vector size |
|
| 682 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
| 683 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
| 684 | + if ($iv === false) { |
|
| 685 | + throw new RuntimeException( |
|
| 686 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
| 687 | + ); |
|
| 688 | + } |
|
| 689 | + // decrypt it |
|
| 690 | + $decrypted_text = mcrypt_decrypt( |
|
| 691 | + MCRYPT_RIJNDAEL_256, |
|
| 692 | + $this->get_encryption_key(), |
|
| 693 | + $encrypted_text, |
|
| 694 | + MCRYPT_MODE_ECB, |
|
| 695 | + $iv |
|
| 696 | + ); |
|
| 697 | + $decrypted_text = trim($decrypted_text); |
|
| 698 | + return $decrypted_text; |
|
| 699 | + } |
|
| 700 | 700 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | protected function __construct() |
| 105 | 105 | { |
| 106 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
| 106 | + if ( ! defined('ESPRESSO_ENCRYPT')) { |
|
| 107 | 107 | define('ESPRESSO_ENCRYPT', true); |
| 108 | 108 | } |
| 109 | 109 | if (extension_loaded('openssl')) { |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | public static function instance() |
| 126 | 126 | { |
| 127 | 127 | // check if class object is instantiated |
| 128 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 128 | + if ( ! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
| 129 | 129 | EE_Encryption::$_instance = new self(); |
| 130 | 130 | } |
| 131 | 131 | return EE_Encryption::$_instance; |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | $iv |
| 333 | 333 | ); |
| 334 | 334 | // append the initialization vector |
| 335 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
| 335 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER.$iv; |
|
| 336 | 336 | // trim and maybe encode |
| 337 | 337 | return $this->_use_base64_encode |
| 338 | 338 | ? trim(base64_encode($encrypted_text)) |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | if (empty($text_string)) { |
| 516 | 516 | return $text_string; |
| 517 | 517 | } |
| 518 | - $key_bits = str_split( |
|
| 518 | + $key_bits = str_split( |
|
| 519 | 519 | str_pad( |
| 520 | 520 | '', |
| 521 | 521 | strlen($text_string), |
@@ -525,8 +525,8 @@ discard block |
||
| 525 | 525 | ); |
| 526 | 526 | $string_bits = str_split($text_string); |
| 527 | 527 | foreach ($string_bits as $k => $v) { |
| 528 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
| 529 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 528 | + $temp = ord($v) + ord($key_bits[$k]); |
|
| 529 | + $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
| 530 | 530 | } |
| 531 | 531 | $encrypted_text = implode('', $string_bits); |
| 532 | 532 | $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
@@ -568,10 +568,10 @@ discard block |
||
| 568 | 568 | STR_PAD_RIGHT |
| 569 | 569 | ) |
| 570 | 570 | ); |
| 571 | - $string_bits = str_split($encrypted_text); |
|
| 571 | + $string_bits = str_split($encrypted_text); |
|
| 572 | 572 | foreach ($string_bits as $k => $v) { |
| 573 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
| 574 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 573 | + $temp = ord($v) - ord($key_bits[$k]); |
|
| 574 | + $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
| 575 | 575 | } |
| 576 | 576 | return implode('', $string_bits); |
| 577 | 577 | } |
@@ -585,16 +585,16 @@ discard block |
||
| 585 | 585 | protected function valid_base_64($string) |
| 586 | 586 | { |
| 587 | 587 | // ensure data is a string |
| 588 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
| 588 | + if ( ! is_string($string) || ! $this->_use_base64_encode) { |
|
| 589 | 589 | return false; |
| 590 | 590 | } |
| 591 | 591 | $decoded = base64_decode($string, true); |
| 592 | 592 | // Check if there is no invalid character in string |
| 593 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 593 | + if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
| 594 | 594 | return false; |
| 595 | 595 | } |
| 596 | 596 | // Decode the string in strict mode and send the response |
| 597 | - if (! base64_decode($string, true)) { |
|
| 597 | + if ( ! base64_decode($string, true)) { |
|
| 598 | 598 | return false; |
| 599 | 599 | } |
| 600 | 600 | // Encode and compare it to original one |
@@ -614,7 +614,7 @@ discard block |
||
| 614 | 614 | $iterations = ceil($length / 40); |
| 615 | 615 | $random_string = ''; |
| 616 | 616 | for ($i = 0; $i < $iterations; $i++) { |
| 617 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
| 617 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
| 618 | 618 | } |
| 619 | 619 | $random_string = substr($random_string, 0, $length); |
| 620 | 620 | return $random_string; |