PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
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 Coupon_Utility.php 5 months ago Encryption_Utility.php 2 years ago Helper.php 4 months ago Money_Utility.php 3 years ago Order_Ajax_Authorization.php 2 months ago Performance_Logger.php 1 year ago String_Utility.php 9 months ago Token_Scope_Utility.php 5 months ago
Encryption_Utility.php
268 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( esc_html__( 'No encryption method available', 'woocommerce-square' ) );
100 }
101
102 if ( empty( $data ) || ( ! is_string( $data ) && ! is_array( $data ) ) ) {
103 throw new \Exception( esc_html__( 'Data must be a non-empty string or array', 'woocommerce-square' ) );
104 }
105
106 if ( ! is_string( $key ) ) {
107 throw new \Exception( esc_html__( '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( esc_html__( '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 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
125 return base64_encode( $vector . $encrypted_data );
126 }
127
128
129 /**
130 * Decrypts data.
131 *
132 * @since 2.0.0
133 *
134 * @param string $data data to decrypt
135 * @param string $key decryption key
136 * @return string|array
137 * @throws \Exception
138 */
139 public function decrypt_data( $data, $key = '' ) {
140
141 // sanity check to ensure decryption can happen
142 if ( ! $this->get_cipher_method() ) {
143 throw new \Exception( esc_html__( 'No decryption method available', 'woocommerce-square' ) );
144 }
145
146 if ( empty( $data ) || ! is_string( $data ) ) {
147 throw new \Exception( esc_html__( 'Data must be a non-empty string', 'woocommerce-square' ) );
148 }
149
150 if ( ! is_string( $key ) ) {
151 throw new \Exception( esc_html__( 'Encryption key must be a string', 'woocommerce-square' ) );
152 }
153
154 // default to the WP salt
155 if ( empty( $key ) ) {
156 $key = $this->get_default_key();
157 }
158
159 $data = base64_decode( $data );
160
161 $vector_length = $this->get_vector_length();
162 $vector = substr( $data, 0, $vector_length );
163 $data = substr( $data, $vector_length );
164 $data = openssl_decrypt( $data, $this->get_cipher_method(), $key, 0, $vector );
165
166 return json_decode( $data, true );
167 }
168
169
170 /**
171 * Gets the vector length.
172 *
173 * @since 2.0.0
174 *
175 * @return int
176 */
177 protected function get_vector_length() {
178
179 return openssl_cipher_iv_length( $this->get_cipher_method() );
180 }
181
182
183 /**
184 * Determines if a cipher method is supported by the server.
185 *
186 * @since 2.0.0
187 *
188 * @param string $method cipher method to check
189 * @return bool
190 */
191 protected function is_cipher_method_supported( $method ) {
192
193 return in_array( $method, $this->get_supported_cipher_methods(), true );
194 }
195
196
197 /**
198 * Determines if encryption is supported at all.
199 *
200 * @since 2.0.0
201 *
202 * @return bool
203 */
204 public static function is_encryption_supported() {
205
206 return extension_loaded( 'openssl' );
207 }
208
209
210 /**
211 * Gets the cipher method.
212 *
213 * @since 2.0.0
214 *
215 * @return string
216 */
217 protected function get_cipher_method() {
218
219 return $this->cipher_method;
220 }
221
222
223 /**
224 * Gets the default cipher method.
225 *
226 * Checks the list of supported methods first, and if the default isn't supported, uses the first available.
227 *
228 * @since 2.0.0
229 *
230 * @return string
231 */
232 protected function get_default_cipher_method() {
233
234 $available_methods = $this->get_supported_cipher_methods();
235
236 return in_array( $this->default_cipher_method, $available_methods, true ) ? $this->default_cipher_method : $available_methods[0];
237 }
238
239
240 /**
241 * Gets the supported cipher methods.
242 *
243 * @since 2.0.0
244 *
245 * @return array
246 */
247 protected function get_supported_cipher_methods() {
248
249 return openssl_get_cipher_methods();
250 }
251
252
253 /**
254 * Gets the default encryption key.
255 *
256 * @since 2.0.0
257 *
258 * @return string
259 */
260 protected function get_default_key() {
261 if ( wc_square()->get_settings_handler()->is_custom_square_auth_keys_set() ) {
262 return md5( SQUARE_ENCRYPTION_KEY . SQUARE_ENCRYPTION_SALT );
263 }
264
265 return md5( wp_salt(), true );
266 }
267 }
268