3rdparty
1 year ago
admin
1 year ago
cli
5 years ago
frontend
1 year ago
helpers
1 year ago
metaboxes
2 years ago
module
1 year ago
modules
1 year ago
opengraph
1 year ago
replace-variables
1 year ago
rest
1 year ago
settings
1 year ago
traits
1 year ago
updates
1 year ago
class-auto-updater.php
5 years ago
class-cmb2.php
2 years ago
class-common.php
1 year ago
class-compatibility.php
1 year ago
class-data-encryption.php
1 year ago
class-defaults.php
6 years ago
class-frontend-seo-score.php
1 year ago
class-helper.php
1 year ago
class-installer.php
1 year ago
class-json-manager.php
1 year ago
class-kb.php
1 year ago
class-metadata.php
1 year ago
class-post.php
1 year ago
class-rewrite.php
2 years ago
class-settings.php
1 year ago
class-term.php
1 year ago
class-thumbnail-overlay.php
1 year ago
class-update-email.php
3 years ago
class-updates.php
1 year ago
class-user.php
1 year ago
index.php
7 years ago
interface-runner.php
7 years ago
template-tags.php
1 year ago
class-data-encryption.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class handles the encryption/descryption of sensitive user data, like |
| 4 | * the Rank Math API key. |
| 5 | * |
| 6 | * Credits to Felix Arntz @ https://felix-arntz.me/ |
| 7 | * |
| 8 | * @since 0.9.0 |
| 9 | * @package RankMath |
| 10 | * @subpackage RankMath\Core |
| 11 | * @author Rank Math <support@rankmath.com> |
| 12 | */ |
| 13 | |
| 14 | namespace RankMath; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * The Data Encryption class. |
| 20 | */ |
| 21 | class Data_Encryption { |
| 22 | |
| 23 | /** |
| 24 | * Enryption available or not. |
| 25 | * |
| 26 | * @var bool |
| 27 | */ |
| 28 | private static $encryption_possible = null; |
| 29 | |
| 30 | /** |
| 31 | * Get encryption key. |
| 32 | * |
| 33 | * @return string Key. |
| 34 | */ |
| 35 | public static function get_key() { |
| 36 | if ( defined( 'RANK_MATH_ENCRYPTION_KEY' ) && '' !== RANK_MATH_ENCRYPTION_KEY ) { |
| 37 | return RANK_MATH_ENCRYPTION_KEY; |
| 38 | } |
| 39 | |
| 40 | if ( defined( 'LOGGED_IN_KEY' ) && '' !== LOGGED_IN_KEY ) { |
| 41 | return LOGGED_IN_KEY; |
| 42 | } |
| 43 | |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get salt. |
| 49 | * |
| 50 | * @return string Salt. |
| 51 | */ |
| 52 | public static function get_salt() { |
| 53 | if ( defined( 'RANK_MATH_ENCRYPTION_SALT' ) && '' !== RANK_MATH_ENCRYPTION_SALT ) { |
| 54 | return RANK_MATH_ENCRYPTION_SALT; |
| 55 | } |
| 56 | |
| 57 | if ( defined( 'LOGGED_IN_SALT' ) && '' !== LOGGED_IN_SALT ) { |
| 58 | return LOGGED_IN_SALT; |
| 59 | } |
| 60 | |
| 61 | return ''; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Encrypt data. |
| 66 | * |
| 67 | * @param mixed $value Original string. |
| 68 | * @return string Encrypted string. |
| 69 | */ |
| 70 | public static function encrypt( $value ) { |
| 71 | if ( ! self::is_available() ) { |
| 72 | return $value; |
| 73 | } |
| 74 | |
| 75 | $method = 'aes-256-ctr'; |
| 76 | $ciphers = openssl_get_cipher_methods(); |
| 77 | if ( ! in_array( $method, $ciphers, true ) ) { |
| 78 | $method = $ciphers[0]; |
| 79 | } |
| 80 | |
| 81 | $ivlen = openssl_cipher_iv_length( $method ); |
| 82 | $iv = openssl_random_pseudo_bytes( $ivlen ); |
| 83 | |
| 84 | $raw_value = openssl_encrypt( $value . self::get_salt(), $method, self::get_key(), 0, $iv ); |
| 85 | if ( ! $raw_value ) { |
| 86 | return $value; |
| 87 | } |
| 88 | |
| 89 | return base64_encode( $iv . $raw_value ); // phpcs:ignore -- Verified as safe usage. |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Decrypt string. |
| 94 | * |
| 95 | * @param string $raw_value Encrypted string. |
| 96 | * @return string Decrypted string. |
| 97 | */ |
| 98 | public static function decrypt( $raw_value ) { |
| 99 | if ( ! self::is_available() ) { |
| 100 | return $raw_value; |
| 101 | } |
| 102 | |
| 103 | $method = 'aes-256-ctr'; |
| 104 | $ciphers = openssl_get_cipher_methods(); |
| 105 | if ( ! in_array( $method, $ciphers, true ) ) { |
| 106 | $method = $ciphers[0]; |
| 107 | } |
| 108 | |
| 109 | $raw_value = base64_decode( $raw_value, true ); // phpcs:ignore -- Verified as safe usage. |
| 110 | |
| 111 | $ivlen = openssl_cipher_iv_length( $method ); |
| 112 | $iv = substr( $raw_value, 0, $ivlen ); |
| 113 | |
| 114 | $raw_value = substr( $raw_value, $ivlen ); |
| 115 | |
| 116 | if ( ! $raw_value || strlen( $iv ) !== $ivlen ) { |
| 117 | return $raw_value; |
| 118 | } |
| 119 | |
| 120 | $salt = self::get_salt(); |
| 121 | |
| 122 | $value = openssl_decrypt( $raw_value, $method, self::get_key(), 0, $iv ); |
| 123 | if ( ! $value || substr( $value, - strlen( $salt ) ) !== $salt ) { |
| 124 | return $raw_value; |
| 125 | } |
| 126 | |
| 127 | return substr( $value, 0, - strlen( $salt ) ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Recursively encrypt array of strings. |
| 132 | * |
| 133 | * @param mixed $data Original strings. |
| 134 | * @return string Encrypted strings. |
| 135 | */ |
| 136 | public static function deep_encrypt( $data ) { |
| 137 | if ( is_array( $data ) ) { |
| 138 | $encrypted = []; |
| 139 | foreach ( $data as $key => $value ) { |
| 140 | $encrypted[ self::encrypt( $key ) ] = self::deep_encrypt( $value ); |
| 141 | } |
| 142 | |
| 143 | return $encrypted; |
| 144 | } |
| 145 | |
| 146 | return self::encrypt( $data ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Recursively decrypt array of strings. |
| 151 | * |
| 152 | * @param string $data Encrypted strings. |
| 153 | * @return string Decrypted strings. |
| 154 | */ |
| 155 | public static function deep_decrypt( $data ) { |
| 156 | if ( is_array( $data ) ) { |
| 157 | $decrypted = []; |
| 158 | foreach ( $data as $key => $value ) { |
| 159 | $decrypted[ self::decrypt( $key ) ] = self::deep_decrypt( $value ); |
| 160 | } |
| 161 | |
| 162 | return $decrypted; |
| 163 | } |
| 164 | |
| 165 | return self::decrypt( $data ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Check if OpenSSL is available and encryption is not disabled with filter. |
| 170 | * |
| 171 | * @return bool Whether encryption is possible or not. |
| 172 | */ |
| 173 | public static function is_available() { |
| 174 | static $encryption_possible; |
| 175 | if ( null === $encryption_possible ) { |
| 176 | $encryption_possible = extension_loaded( 'openssl' ) && apply_filters( 'rank_math/admin/sensitive_data_encryption', true ) && self::get_key() && self::get_salt(); |
| 177 | } |
| 178 | |
| 179 | return (bool) $encryption_possible; |
| 180 | } |
| 181 | } |
| 182 |