$caller['function'] ?? 'unknown', 'class' => $caller['class'] ?? null, 'file' => $caller['file'] ?? null, 'line' => $caller['line'] ?? null ]; if (empty($data)) { vasp_log_warning('Encryption warning: empty data provided'); return vasp_create_error('VASP_ERROR_INVALID_DATA', [ 'context' => 'encryption', 'reason' => 'empty_data', 'caller' => $caller_info ]); } $key = vasp_ensure_encryption_key(); if (is_wp_error($key)) { vasp_log_error('Failed to ensure encryption key', [ 'error_code' => $key->get_error_code() ]); return $key; } $iv = random_bytes(16); $tag = ''; $ciphertext = openssl_encrypt( $data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag ); if ($ciphertext === false) { $error = openssl_error_string(); vasp_log_error('OpenSSL encryption failed: ' . $error); return vasp_create_error('VASP_ERROR_ENCRYPTION_FAILED', [ 'context' => 'encryption', 'openssl_error' => $error, 'caller' => $caller_info ]); } $encrypted = $iv . $tag . $ciphertext; $encoded = base64_encode($encrypted); return $encoded; } /** * Decrypt base64-encoded data using AES-256-GCM * * @since 1.0.0 * @param string $encrypted_data Base64-encoded encrypted data * @return string|WP_Error Decrypted data or WP_Error on failure */ function vasp_decrypt_data($encrypted_data) { // Get caller info for better error context $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $caller = $backtrace[1] ?? []; $caller_info = [ 'function' => $caller['function'] ?? 'unknown', 'class' => $caller['class'] ?? null, 'file' => $caller['file'] ?? null, 'line' => $caller['line'] ?? null ]; // Validate input if (empty($encrypted_data)) { vasp_log_error('Decryption failed: empty data provided'); return vasp_create_error('VASP_ERROR_INVALID_DATA', [ 'context' => 'decryption', 'reason' => 'empty_data', 'caller' => $caller_info ]); } if (!is_string($encrypted_data)) { vasp_log_error('Decryption failed: data is not a string', [ 'data_type' => gettype($encrypted_data) ]); return vasp_create_error('VASP_ERROR_INVALID_DATA_TYPE', [ 'context' => 'decryption', 'expected' => 'string', 'actual' => gettype($encrypted_data), 'caller' => $caller_info ]); } $data = base64_decode($encrypted_data, true); if ($data === false) { vasp_log_error('Base64 decoding failed: invalid format'); return vasp_create_error('VASP_ERROR_INVALID_DATA', [ 'context' => 'decryption', 'reason' => 'invalid_base64', 'caller' => $caller_info ]); } $key = vasp_ensure_encryption_key(); if (is_wp_error($key)) { vasp_log_error('Failed to ensure encryption key', [ 'error_code' => $key->get_error_code() ]); return $key; } $iv = substr($data, 0, 16); $tag = substr($data, 16, 16); $ciphertext = substr($data, 32); $decrypted = openssl_decrypt( $ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag ); if ($decrypted === false) { $error = openssl_error_string(); vasp_log_error('OpenSSL decryption failed: ' . $error); return vasp_create_error('VASP_ERROR_DECRYPTION_FAILED', [ 'context' => 'decryption', 'openssl_error' => $error, 'caller' => $caller_info ]); } return $decrypted; } /** * Ensure encryption key exists, generating if needed (ephemeral on-demand) * This is the recommended way to get an encryption key as it handles missing keys gracefully * * @since 1.0.11 * @return string|WP_Error Encryption key or WP_Error on failure */ function vasp_ensure_encryption_key() { // If the site defines VASP_ENCRYPTION_KEY in wp-config.php, use it. // This keeps the encryption key outside the database alongside ciphertext. if (defined('VASP_ENCRYPTION_KEY')) { if (vasp_is_encryption_key_valid(VASP_ENCRYPTION_KEY)) { return VASP_ENCRYPTION_KEY; } // Constant is defined but invalid — warn so the admin knows it was ignored. vasp_log_warning('VASP_ENCRYPTION_KEY is defined in wp-config.php but failed validation. Falling back to stored key.', [ 'key_length' => strlen(VASP_ENCRYPTION_KEY) ]); } $key = get_option(VASP_OPTION_ENCRYPTION_KEY); // If key doesn't exist, generate it if (empty($key)) { vasp_log_info('Encryption key not found, generating ephemeral key'); return vasp_issue_new_encryption_key(); } // Validate existing key if (!vasp_is_encryption_key_valid($key)) { vasp_log_error('Invalid encryption key format, regenerating', [ 'key_length' => strlen($key) ]); return vasp_issue_new_encryption_key(); } return $key; } /** * Issue a new encryption key and store it * This will replace any existing key * * @since 1.0.0 * @return string|WP_Error New encryption key or WP_Error on failure */ function vasp_issue_new_encryption_key() { // Get caller info for better error context $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $caller = $backtrace[1] ?? []; $caller_info = [ 'function' => $caller['function'] ?? 'unknown', 'class' => $caller['class'] ?? null, 'file' => $caller['file'] ?? null, 'line' => $caller['line'] ?? null ]; vasp_log_info('Generating new encryption key'); vasp_log_debug('Generating secure random key (256 bits)'); $bytes = random_bytes(32); $key = base64_encode($bytes); $updated = update_option(VASP_OPTION_ENCRYPTION_KEY, $key, false); if (!$updated) { vasp_log_error('Failed to store new encryption key'); return vasp_create_error('VASP_ERROR_KEY_UPDATE_FAILED', [ 'context' => 'encryption_key', 'option_name' => VASP_OPTION_ENCRYPTION_KEY, 'caller' => $caller_info ]); } vasp_log_info('New encryption key stored successfully'); return $key; } /** * Check if an encryption key is valid * * @since 1.0.0 * @param string $key Key to validate * @return bool True if key is valid */ function vasp_is_encryption_key_valid($key) { // First check if it's a string if (!is_string($key) || empty($key)) { return false; } // Check if it's a valid base64 string if (base64_encode(base64_decode($key, true)) === $key) { // Decode and check the binary length $decoded = base64_decode($key, true); return in_array(strlen($decoded), [32, 48, 64], true); } // If not base64, check if it's a raw binary key with valid length return in_array(strlen($key), [32, 48, 64], true); } // vasp_generate_secure_key function has been integrated directly into vasp_issue_new_encryption_key // to reduce redundancy and simplify the codebase /** * Encrypt a token with additional security measures * * @since 1.0.0 * @param string $token Token to encrypt * @return string|WP_Error Encrypted token or WP_Error on failure */ function vasp_encrypt_token($token) { if (!is_string($token) || empty($token)) { return vasp_create_error('VASP_ERROR_INVALID_TOKEN', ['context' => 'encryption', 'reason' => 'empty_or_invalid_token']); } vasp_log('Encrypting credential material', 'debug'); return vasp_encrypt_data($token); } /** * Decrypt a token * * @since 1.0.0 * @param string $encrypted_token Encrypted token * @return string|WP_Error Decrypted token or WP_Error on failure */ function vasp_decrypt_token($encrypted_token) { if (empty($encrypted_token)) { return vasp_create_error('VASP_ERROR_INVALID_TOKEN', ['context' => 'decryption', 'reason' => 'empty_token']); } $decrypted = vasp_decrypt_data($encrypted_token); if (is_wp_error($decrypted)) { return $decrypted; } // Token decryption successful (no log needed - happens frequently) return $decrypted; }