222 lines
5.4 KiB
PHP
222 lines
5.4 KiB
PHP
<?php
|
|
/**
|
|
* Encryption utility for ROBOTSTXT SMTP plugin.
|
|
*
|
|
* Provides AES-256-GCM encryption for sensitive data using WordPress authentication salts.
|
|
* Encryption keys are derived using PBKDF2 (10,000 iterations) for enhanced security.
|
|
*
|
|
* @package Robotstxt_SMTP
|
|
* @since 1.2.1
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class Robotstxt_SMTP_Encryption
|
|
*
|
|
* Handles encryption and decryption of sensitive credentials.
|
|
*
|
|
* @since 1.2.1
|
|
*/
|
|
class Robotstxt_SMTP_Encryption {
|
|
|
|
/**
|
|
* Encryption cipher method.
|
|
*
|
|
* @var string
|
|
*/
|
|
private const CIPHER = 'aes-256-gcm';
|
|
|
|
/**
|
|
* Prefix for encrypted values to identify them.
|
|
*
|
|
* @var string
|
|
*/
|
|
private const PREFIX = 'enc:v1:';
|
|
|
|
/**
|
|
* Encrypts a plaintext string using AES-256-GCM.
|
|
*
|
|
* Requires AUTH_KEY and AUTH_SALT to be defined in wp-config.php.
|
|
* Returns the original plaintext if encryption is not available.
|
|
*
|
|
* @since 1.2.1
|
|
*
|
|
* @param string $plaintext The plaintext to encrypt.
|
|
* @return string Encrypted string with prefix, or original plaintext if encryption unavailable.
|
|
*/
|
|
public static function encrypt( string $plaintext ): string {
|
|
// Return empty strings as-is.
|
|
if ( '' === $plaintext ) {
|
|
return '';
|
|
}
|
|
|
|
// Check if already encrypted.
|
|
if ( self::is_encrypted( $plaintext ) ) {
|
|
return $plaintext;
|
|
}
|
|
|
|
// Check if encryption is available.
|
|
if ( ! self::is_encryption_available() ) {
|
|
return $plaintext;
|
|
}
|
|
|
|
// Generate random IV (Initialization Vector).
|
|
$iv_length = openssl_cipher_iv_length( self::CIPHER );
|
|
$iv = openssl_random_pseudo_bytes( $iv_length );
|
|
|
|
// Derive encryption key from WordPress constants.
|
|
$key = self::get_encryption_key();
|
|
|
|
// Encrypt the data.
|
|
$tag = '';
|
|
$ciphertext = openssl_encrypt(
|
|
$plaintext,
|
|
self::CIPHER,
|
|
$key,
|
|
OPENSSL_RAW_DATA,
|
|
$iv,
|
|
$tag,
|
|
'',
|
|
16
|
|
);
|
|
|
|
if ( false === $ciphertext ) {
|
|
// Encryption failed, return plaintext.
|
|
return $plaintext;
|
|
}
|
|
|
|
// Combine IV + ciphertext + tag and encode.
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Used for encryption, not obfuscation.
|
|
$encrypted = base64_encode( $iv . $ciphertext . $tag );
|
|
|
|
return self::PREFIX . $encrypted;
|
|
}
|
|
|
|
/**
|
|
* Decrypts an encrypted string.
|
|
*
|
|
* Returns the original string if it's not encrypted or decryption fails.
|
|
*
|
|
* @since 1.2.1
|
|
*
|
|
* @param string $encrypted The encrypted string (with prefix).
|
|
* @return string Decrypted plaintext or original string if not encrypted.
|
|
*/
|
|
public static function decrypt( string $encrypted ): string {
|
|
// Return empty strings as-is.
|
|
if ( '' === $encrypted ) {
|
|
return '';
|
|
}
|
|
|
|
// Check if it's actually encrypted.
|
|
if ( ! self::is_encrypted( $encrypted ) ) {
|
|
return $encrypted;
|
|
}
|
|
|
|
// Check if decryption is available.
|
|
if ( ! self::is_encryption_available() ) {
|
|
// Can't decrypt without proper setup, return as-is.
|
|
return $encrypted;
|
|
}
|
|
|
|
// Remove prefix.
|
|
$encrypted_data = substr( $encrypted, strlen( self::PREFIX ) );
|
|
|
|
// Decode from base64.
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Used for decryption, not obfuscation.
|
|
$decoded = base64_decode( $encrypted_data, true );
|
|
|
|
if ( false === $decoded ) {
|
|
// Invalid base64, return original.
|
|
return $encrypted;
|
|
}
|
|
|
|
// Extract IV, ciphertext, and tag.
|
|
$iv_length = openssl_cipher_iv_length( self::CIPHER );
|
|
$tag_length = 16;
|
|
|
|
if ( strlen( $decoded ) < ( $iv_length + $tag_length + 1 ) ) {
|
|
// Data too short, corrupted.
|
|
return $encrypted;
|
|
}
|
|
|
|
$iv = substr( $decoded, 0, $iv_length );
|
|
$tag = substr( $decoded, -$tag_length );
|
|
$ciphertext = substr( $decoded, $iv_length, -$tag_length );
|
|
|
|
// Derive encryption key.
|
|
$key = self::get_encryption_key();
|
|
|
|
// Decrypt.
|
|
$plaintext = openssl_decrypt(
|
|
$ciphertext,
|
|
self::CIPHER,
|
|
$key,
|
|
OPENSSL_RAW_DATA,
|
|
$iv,
|
|
$tag
|
|
);
|
|
|
|
if ( false === $plaintext ) {
|
|
// Decryption failed (wrong key, corrupted data, etc).
|
|
return $encrypted;
|
|
}
|
|
|
|
return $plaintext;
|
|
}
|
|
|
|
/**
|
|
* Checks if a string is encrypted (has the encryption prefix).
|
|
*
|
|
* @since 1.2.1
|
|
*
|
|
* @param string $value The value to check.
|
|
* @return bool True if encrypted, false otherwise.
|
|
*/
|
|
public static function is_encrypted( string $value ): bool {
|
|
return 0 === strpos( $value, self::PREFIX );
|
|
}
|
|
|
|
/**
|
|
* Checks if encryption is available and properly configured.
|
|
*
|
|
* @since 1.2.1
|
|
*
|
|
* @return bool True if encryption is available, false otherwise.
|
|
*/
|
|
public static function is_encryption_available(): bool {
|
|
// Check if OpenSSL extension is loaded.
|
|
if ( ! extension_loaded( 'openssl' ) ) {
|
|
return false;
|
|
}
|
|
|
|
// Check if AES-256-GCM is supported.
|
|
if ( ! in_array( self::CIPHER, openssl_get_cipher_methods(), true ) ) {
|
|
return false;
|
|
}
|
|
|
|
// Check if WordPress AUTH_KEY and AUTH_SALT are defined.
|
|
if ( ! defined( 'AUTH_KEY' ) || '' === AUTH_KEY || ! defined( 'AUTH_SALT' ) || '' === AUTH_SALT ) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Derives a 256-bit encryption key from WordPress AUTH_KEY and AUTH_SALT.
|
|
*
|
|
* @since 1.2.1
|
|
*
|
|
* @return string Binary encryption key (32 bytes for AES-256).
|
|
*/
|
|
private static function get_encryption_key(): string {
|
|
$key_material = AUTH_KEY . AUTH_SALT;
|
|
$salt = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : 'robotstxt_smtp_salt';
|
|
|
|
return hash_pbkdf2( 'sha256', $key_material, $salt, 10000, 32, true );
|
|
}
|
|
}
|