diff --git a/lib/Service/SmimeService.php b/lib/Service/SmimeService.php index 5276e2682d..870d1aa79f 100644 --- a/lib/Service/SmimeService.php +++ b/lib/Service/SmimeService.php @@ -84,7 +84,7 @@ public function verifyMessage(string $message): bool { // smime/pkcs7 module. Unfortunately, it is only supported since php 8. // Ref https://www.php.net/manual/en/function.openssl-cms-verify.php - $messageTemp = $this->tempManager->getTemporaryFile(); + $messageTemp = $this->getTemporaryFileOrThrow(); $messageTempHandle = fopen($messageTemp, 'wb'); fwrite($messageTempHandle, $message); fclose($messageTempHandle); @@ -122,8 +122,8 @@ public function extractSignedContent(string $message): string { // smime/pkcs7 module. Unfortunately, it is only supported since php 8. // Ref https://www.php.net/manual/en/function.openssl-cms-verify.php - $verifiedContentTemp = $this->tempManager->getTemporaryFile(); - $messageTemp = $this->tempManager->getTemporaryFile(); + $verifiedContentTemp = $this->getTemporaryFileOrThrow(); + $messageTemp = $this->getTemporaryFileOrThrow(); $messageTempHandle = fopen($messageTemp, 'wb'); fwrite($messageTempHandle, $message); fclose($messageTempHandle); @@ -182,7 +182,7 @@ public function parseCertificate(string $certificate): SmimeCertificateInfo { } } - $decryptedCertificateFile = $this->tempManager->getTemporaryFile(); + $decryptedCertificateFile = $this->getTemporaryFileOrThrow(); file_put_contents($decryptedCertificateFile, $certificate); $caBundle = [$this->certificateManager->getAbsoluteBundlePath()]; @@ -379,11 +379,11 @@ public function signMimePart(Horde_Mime_Part $part, ); } - $decryptedCertificateFile = $this->tempManager->getTemporaryFile(); + $decryptedCertificateFile = $this->getTemporaryFileOrThrow(); file_put_contents($decryptedCertificateFile, $decryptedCertificate); - $inPath = $this->tempManager->getTemporaryFile(); - $outPath = $this->tempManager->getTemporaryFile(); + $inPath = $this->getTemporaryFileOrThrow(); + $outPath = $this->getTemporaryFileOrThrow(); file_put_contents($inPath, $part->toString([ 'canonical' => true, 'headers' => true, @@ -441,8 +441,8 @@ public function decryptMimePartText(string $mimePartText, ); } - $inPath = $this->tempManager->getTemporaryFile(); - $outPath = $this->tempManager->getTemporaryFile(); + $inPath = $this->getTemporaryFileOrThrow(); + $outPath = $this->getTemporaryFileOrThrow(); file_put_contents($inPath, $mimePartText); if (!openssl_pkcs7_decrypt($inPath, $outPath, $decryptedCertificate, $decryptedKey)) { throw new SmimeDecryptException('Failed to decrypt MIME part text'); @@ -597,8 +597,8 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Ho throw new ServiceException('Failed to decrypt certificate: ' . $e->getMessage(), 0, $e); } - $inPath = $this->tempManager->getTemporaryFile(); - $outPath = $this->tempManager->getTemporaryFile(); + $inPath = $this->getTemporaryFileOrThrow(); + $outPath = $this->getTemporaryFileOrThrow(); file_put_contents($inPath, $part->toString([ 'canonical' => true, 'headers' => true, @@ -626,4 +626,18 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Ho return $parsedPart; } + + /** + * Create a temporary file and return the path or throw if it could not be created. + * + * @throws ServiceException If the temporary file could not be created + */ + private function getTemporaryFileOrThrow(): string { + $file = $this->tempManager->getTemporaryFile(); + if ($file === false) { + throw new ServiceException('Failed to create temporary file'); + } + + return $file; + } }