Encryption_Utility.php
265 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Utilities; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | |
| 30 | /** |
| 31 | * The encryption utility class. |
| 32 | * |
| 33 | * Requires OpenSSL by default. |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | */ |
| 37 | class Encryption_Utility { |
| 38 | |
| 39 | |
| 40 | /** @var string default cipher method */ |
| 41 | protected $default_cipher_method = 'AES-128-CBC'; |
| 42 | |
| 43 | /** @var string cipher method */ |
| 44 | protected $cipher_method; |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Constructs the class. |
| 49 | * |
| 50 | * @param string $preferred_cipher_method cipher method |
| 51 | */ |
| 52 | public function __construct( $preferred_cipher_method = '' ) { |
| 53 | |
| 54 | // bail entirely if openssl isn't available |
| 55 | if ( ! self::is_encryption_supported() ) { |
| 56 | wc_doing_it_wrong( __CLASS__, __( 'Encryption is not supported on this site.', 'woocommerce-square' ), Framework\SV_WC_Plugin::VERSION ); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $this->cipher_method = $this->get_default_cipher_method(); |
| 61 | |
| 62 | // if a preferred cipher method is set, check and set it |
| 63 | if ( is_string( $preferred_cipher_method ) && ! empty( $preferred_cipher_method ) ) { |
| 64 | |
| 65 | // only use what's preferred if it's supported |
| 66 | if ( $this->is_cipher_method_supported( $preferred_cipher_method ) ) { |
| 67 | |
| 68 | $this->cipher_method = $preferred_cipher_method; |
| 69 | |
| 70 | } else { // otherwise, throw a notice and continue with the default |
| 71 | |
| 72 | $message = sprintf( |
| 73 | __( '%1$s encryption is not available on this site. %2$s will be used instead.', 'woocommerce-square' ), |
| 74 | $preferred_cipher_method, |
| 75 | $this->cipher_method |
| 76 | ); |
| 77 | |
| 78 | wc_doing_it_wrong( __CLASS__, $message, Framework\SV_WC_Plugin::VERSION ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Encrypts data. |
| 86 | * |
| 87 | * @since 2.0.0 |
| 88 | * |
| 89 | * @param string|array $data data to encrypt |
| 90 | * @param string $key encryption key |
| 91 | * @return string |
| 92 | * @throws Framework\SV_WC_Plugin_Exception |
| 93 | */ |
| 94 | public function encrypt_data( $data, $key = '' ) { |
| 95 | |
| 96 | // sanity check to ensure encryption can happen |
| 97 | if ( ! $this->get_cipher_method() ) { |
| 98 | throw new Framework\SV_WC_Plugin_Exception( __( 'No encryption method available', 'woocommerce-square' ) ); |
| 99 | } |
| 100 | |
| 101 | if ( empty( $data ) || ( ! is_string( $data ) && ! is_array( $data ) ) ) { |
| 102 | throw new Framework\SV_WC_Plugin_Exception( __( 'Data must be a non-empty string or array', 'woocommerce-square' ) ); |
| 103 | } |
| 104 | |
| 105 | if ( ! is_string( $key ) ) { |
| 106 | throw new Framework\SV_WC_Plugin_Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) ); |
| 107 | } |
| 108 | |
| 109 | // default to the WP salt |
| 110 | if ( empty( $key ) ) { |
| 111 | $key = $this->get_default_key(); |
| 112 | } |
| 113 | |
| 114 | $vector = openssl_random_pseudo_bytes( $this->get_vector_length(), $crypto_strong ); |
| 115 | |
| 116 | // bail if a strong vector wasn't generated |
| 117 | if ( false === $vector || false === $crypto_strong ) { |
| 118 | throw new Framework\SV_WC_Plugin_Exception( __( 'Could not generate encryption vector.', 'woocommerce-square' ) ); |
| 119 | } |
| 120 | |
| 121 | $encrypted_data = openssl_encrypt( json_encode( $data ), $this->get_cipher_method(), $key, 0, $vector ); |
| 122 | |
| 123 | return base64_encode( $vector . $encrypted_data ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Decrypts data. |
| 129 | * |
| 130 | * @since 2.0.0 |
| 131 | * |
| 132 | * @param string $data data to decrypt |
| 133 | * @param string $key decryption key |
| 134 | * @return string|array |
| 135 | * @throws Framework\SV_WC_Plugin_Exception |
| 136 | */ |
| 137 | public function decrypt_data( $data, $key = '' ) { |
| 138 | |
| 139 | // sanity check to ensure decryption can happen |
| 140 | if ( ! $this->get_cipher_method() ) { |
| 141 | throw new Framework\SV_WC_Plugin_Exception( __( 'No decryption method available', 'woocommerce-square' ) ); |
| 142 | } |
| 143 | |
| 144 | if ( empty( $data ) || ! is_string( $data ) ) { |
| 145 | throw new Framework\SV_WC_Plugin_Exception( __( 'Data must be a non-empty string', 'woocommerce-square' ) ); |
| 146 | } |
| 147 | |
| 148 | if ( ! is_string( $key ) ) { |
| 149 | throw new Framework\SV_WC_Plugin_Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) ); |
| 150 | } |
| 151 | |
| 152 | // default to the WP salt |
| 153 | if ( empty( $key ) ) { |
| 154 | $key = $this->get_default_key(); |
| 155 | } |
| 156 | |
| 157 | $data = base64_decode( $data ); |
| 158 | |
| 159 | $vector_length = $this->get_vector_length(); |
| 160 | $vector = substr( $data, 0, $vector_length ); |
| 161 | $data = substr( $data, $vector_length ); |
| 162 | $data = openssl_decrypt( $data, $this->get_cipher_method(), $key, 0, $vector ); |
| 163 | |
| 164 | return json_decode( $data, true ); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Gets the vector length. |
| 170 | * |
| 171 | * @since 2.0.0 |
| 172 | * |
| 173 | * @return int |
| 174 | */ |
| 175 | protected function get_vector_length() { |
| 176 | |
| 177 | return openssl_cipher_iv_length( $this->get_cipher_method() ); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Determines if a cipher method is supported by the server. |
| 183 | * |
| 184 | * @since 2.0.0 |
| 185 | * |
| 186 | * @param string $method cipher method to check |
| 187 | * @return bool |
| 188 | */ |
| 189 | protected function is_cipher_method_supported( $method ) { |
| 190 | |
| 191 | return in_array( $method, $this->get_supported_cipher_methods(), true ); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Determines if encryption is supported at all. |
| 197 | * |
| 198 | * @since 2.0.0 |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | public static function is_encryption_supported() { |
| 203 | |
| 204 | return extension_loaded( 'openssl' ); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Gets the cipher method. |
| 210 | * |
| 211 | * @since 2.0.0 |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | protected function get_cipher_method() { |
| 216 | |
| 217 | return $this->cipher_method; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Gets the default cipher method. |
| 223 | * |
| 224 | * Checks the list of supported methods first, and if the default isn't supported, uses the first available. |
| 225 | * |
| 226 | * @since 2.0.0 |
| 227 | * |
| 228 | * @return string |
| 229 | */ |
| 230 | protected function get_default_cipher_method() { |
| 231 | |
| 232 | $available_methods = $this->get_supported_cipher_methods(); |
| 233 | |
| 234 | return in_array( $this->default_cipher_method, $available_methods, true ) ? $this->default_cipher_method : $available_methods[0]; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Gets the supported cipher methods. |
| 240 | * |
| 241 | * @since 2.0.0 |
| 242 | * |
| 243 | * @return array |
| 244 | */ |
| 245 | protected function get_supported_cipher_methods() { |
| 246 | |
| 247 | return openssl_get_cipher_methods(); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * Gets the default encryption key. |
| 253 | * |
| 254 | * @since 2.0.0 |
| 255 | * |
| 256 | * @return string |
| 257 | */ |
| 258 | protected function get_default_key() { |
| 259 | |
| 260 | return md5( wp_salt(), true ); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | } |
| 265 |