dir = realpath($dir); $web = realpath($_SERVER["DOCUMENT_ROOT"] ?? ABSPATH) ?: $root; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- only resolved with realpath() to keep private storage outside the web root. foreach (array_unique([$root, $web]) as $public) { if ( $this->dir === $public || strpos($this->dir . "/", rtrim($public, "/") . "/") === 0 ) { throw new \RuntimeException( "LockerPad storage must be outside the public website folder.", ); } } chmod($this->dir, 0700); if (!$explicit && !get_option("lockerpad_private_path")) { add_option("lockerpad_private_path", $this->dir, "", false); } $path = defined("LOCKERPAD_MASTER_KEY_FILE") && !$explicit ? LOCKERPAD_MASTER_KEY_FILE : $this->dir . "/master.key"; $keyParent = realpath(dirname($path)); if (!$keyParent || is_link($path)) { throw new \RuntimeException( "The encryption key must be a regular file in a private directory.", ); } foreach (array_unique([$root, $web]) as $public) { if ( $keyParent === $public || strpos($keyParent . "/", rtrim($public, "/") . "/") === 0 ) { throw new \RuntimeException( "The encryption key must be outside the public website folder.", ); } } $this->keyPath = $path; if (!file_exists($path)) { $existing = (bool) glob($this->dir . "/*.lpf"); if (!$explicit && get_option("lockerpad_schema")) { global $wpdb; $existing = $existing || (bool) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i", Plugin::table("vaults"), ), ); if ($wpdb->last_error) { throw new \RuntimeException( "Cannot check existing vault data. Restore the key before proceeding.", ); } } if ( $existing || (!$explicit && defined("LOCKERPAD_MASTER_KEY_FILE")) ) { throw new \RuntimeException( "Encryption key is missing. Restore the original key from your backup. LockerPad will not replace it.", ); } $handle = @fopen($path, "x+b"); if ($handle) { chmod($path, 0600); $key = random_bytes(32); if (fwrite($handle, $key) !== 32) { fclose($handle); throw new \RuntimeException( "Cannot initialize encryption key.", ); } // phpcs:ignore PluginCheck.CodeAnalysis.WriteFile.ABSPATHDetected -- the master key goes in LockerPad's private folder, which directory() keeps outside the web root. fflush($handle); fclose($handle); } } $this->key = file_get_contents($path); if ( strlen($this->key) !== 32 || !in_array("aes-256-gcm", openssl_get_cipher_methods(), true) ) { throw new \RuntimeException("Encryption is unavailable."); } } public function directory() { return $this->dir; } public function key_path() { return $this->keyPath; } private function key($context) { return hash_hkdf("sha256", $this->key, 32, "lockerpad-v1:" . $context); } private function seal($text, $context) { $nonce = random_bytes(12); $cipher = openssl_encrypt( $text, "aes-256-gcm", $this->key($context), OPENSSL_RAW_DATA, $nonce, $tag, $context, 16, ); if ($cipher === false) { throw new \RuntimeException("Encryption failed."); } return $nonce . $tag . $cipher; } private function open($data, $context) { if (strlen($data) < 28) { throw new \RuntimeException("Encrypted data is incomplete."); } $text = openssl_decrypt( substr($data, 28), "aes-256-gcm", $this->key($context), OPENSSL_RAW_DATA, substr($data, 0, 12), substr($data, 12, 16), $context, ); if ($text === false) { throw new \RuntimeException( "Encrypted data failed authentication.", ); } return $text; } public function encrypt($text, $context) { return base64_encode($this->seal($text, $context)); } public function decrypt($data, $context) { $raw = base64_decode($data, true); if ($raw === false) { throw new \RuntimeException("Invalid encrypted data."); } return $this->open($raw, $context); } public function credential($text) { return hash_hmac("sha256", $text, $this->key("credentials")); } }