PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.5.0
WooCommerce Square v4.5.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Utilities / Encryption_Utility.php
woocommerce-square / includes / Utilities Last commit date
Array_Utility.php 3 years ago Encryption_Utility.php 3 years ago Helper.php 3 years ago Money_Utility.php 3 years ago String_Utility.php 3 years ago
Encryption_Utility.php
267 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 GNU General Public License v3.0 or later
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 or later
22 */
23
24 namespace WooCommerce\Square\Utilities;
25
26 use WooCommerce_Square_Loader;
27
28 defined( 'ABSPATH' ) || exit;
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' ), WooCommerce_Square_Loader::FRAMEWORK_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 /* translators: %1$s - Cipher method. %2$s - Preferred cipher method. */
74 __( '%1$s encryption is not available on this site. %2$s will be used instead.', 'woocommerce-square' ),
75 $preferred_cipher_method,
76 $this->cipher_method
77 );
78
79 wc_doing_it_wrong( __CLASS__, $message, WooCommerce_Square_Loader::FRAMEWORK_VERSION );
80 }
81 }
82 }
83
84
85 /**
86 * Encrypts data.
87 *
88 * @since 2.0.0
89 *
90 * @param string|array $data data to encrypt
91 * @param string $key encryption key
92 * @return string
93 * @throws \Exception
94 */
95 public function encrypt_data( $data, $key = '' ) {
96
97 // sanity check to ensure encryption can happen
98 if ( ! $this->get_cipher_method() ) {
99 throw new \Exception( __( 'No encryption method available', 'woocommerce-square' ) );
100 }
101
102 if ( empty( $data ) || ( ! is_string( $data ) && ! is_array( $data ) ) ) {
103 throw new \Exception( __( 'Data must be a non-empty string or array', 'woocommerce-square' ) );
104 }
105
106 if ( ! is_string( $key ) ) {
107 throw new \Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) );
108 }
109
110 // default to the WP salt
111 if ( empty( $key ) ) {
112 $key = $this->get_default_key();
113 }
114
115 $vector = openssl_random_pseudo_bytes( $this->get_vector_length(), $crypto_strong );
116
117 // bail if a strong vector wasn't generated
118 if ( false === $vector || false === $crypto_strong ) {
119 throw new \Exception( __( 'Could not generate encryption vector.', 'woocommerce-square' ) );
120 }
121
122 $encrypted_data = openssl_encrypt( wp_json_encode( $data ), $this->get_cipher_method(), $key, 0, $vector );
123
124 return base64_encode( $vector . $encrypted_data );
125 }
126
127
128 /**
129 * Decrypts data.
130 *
131 * @since 2.0.0
132 *
133 * @param string $data data to decrypt
134 * @param string $key decryption key
135 * @return string|array
136 * @throws \Exception
137 */
138 public function decrypt_data( $data, $key = '' ) {
139
140 // sanity check to ensure decryption can happen
141 if ( ! $this->get_cipher_method() ) {
142 throw new \Exception( __( 'No decryption method available', 'woocommerce-square' ) );
143 }
144
145 if ( empty( $data ) || ! is_string( $data ) ) {
146 throw new \Exception( __( 'Data must be a non-empty string', 'woocommerce-square' ) );
147 }
148
149 if ( ! is_string( $key ) ) {
150 throw new \Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) );
151 }
152
153 // default to the WP salt
154 if ( empty( $key ) ) {
155 $key = $this->get_default_key();
156 }
157
158 $data = base64_decode( $data );
159
160 $vector_length = $this->get_vector_length();
161 $vector = substr( $data, 0, $vector_length );
162 $data = substr( $data, $vector_length );
163 $data = openssl_decrypt( $data, $this->get_cipher_method(), $key, 0, $vector );
164
165 return json_decode( $data, true );
166 }
167
168
169 /**
170 * Gets the vector length.
171 *
172 * @since 2.0.0
173 *
174 * @return int
175 */
176 protected function get_vector_length() {
177
178 return openssl_cipher_iv_length( $this->get_cipher_method() );
179 }
180
181
182 /**
183 * Determines if a cipher method is supported by the server.
184 *
185 * @since 2.0.0
186 *
187 * @param string $method cipher method to check
188 * @return bool
189 */
190 protected function is_cipher_method_supported( $method ) {
191
192 return in_array( $method, $this->get_supported_cipher_methods(), true );
193 }
194
195
196 /**
197 * Determines if encryption is supported at all.
198 *
199 * @since 2.0.0
200 *
201 * @return bool
202 */
203 public static function is_encryption_supported() {
204
205 return extension_loaded( 'openssl' );
206 }
207
208
209 /**
210 * Gets the cipher method.
211 *
212 * @since 2.0.0
213 *
214 * @return string
215 */
216 protected function get_cipher_method() {
217
218 return $this->cipher_method;
219 }
220
221
222 /**
223 * Gets the default cipher method.
224 *
225 * Checks the list of supported methods first, and if the default isn't supported, uses the first available.
226 *
227 * @since 2.0.0
228 *
229 * @return string
230 */
231 protected function get_default_cipher_method() {
232
233 $available_methods = $this->get_supported_cipher_methods();
234
235 return in_array( $this->default_cipher_method, $available_methods, true ) ? $this->default_cipher_method : $available_methods[0];
236 }
237
238
239 /**
240 * Gets the supported cipher methods.
241 *
242 * @since 2.0.0
243 *
244 * @return array
245 */
246 protected function get_supported_cipher_methods() {
247
248 return openssl_get_cipher_methods();
249 }
250
251
252 /**
253 * Gets the default encryption key.
254 *
255 * @since 2.0.0
256 *
257 * @return string
258 */
259 protected function get_default_key() {
260 if ( wc_square()->get_settings_handler()->is_custom_square_auth_keys_set() ) {
261 return md5( SQUARE_ENCRYPTION_KEY . SQUARE_ENCRYPTION_SALT );
262 }
263
264 return md5( wp_salt(), true );
265 }
266 }
267