protected function newFilteredStream() { $messageStream = new Swift_ByteStream_TemporaryFileByteStream(); $messageStream->addFilter($this->replacementFactory->createFilter("\r\n", "\n"), 'CRLF to LF'); $messageStream->addFilter($this->replacementFactory->createFilter("\n", "\r\n"), 'LF to CRLF'); return $messageStream; }
/** * Merges an OutputByteStream to Swift_Message. * * @param Swift_OutputByteStream $fromStream * @param Swift_Message $message */ protected function streamToMime(Swift_OutputByteStream $fromStream, Swift_Message $message) { $bufferLength = 78; $headerData = ''; $fromStream->setReadPointer(0); while (($buffer = $fromStream->read($bufferLength)) !== false) { $headerData .= $buffer; if (false !== strpos($buffer, "\r\n\r\n")) { break; } } $headersPosEnd = strpos($headerData, "\r\n\r\n"); $headerData = trim($headerData); $headerData = substr($headerData, 0, $headersPosEnd); $headerLines = explode("\r\n", $headerData); unset($headerData); $headers = array(); $currentHeaderName = ''; foreach ($headerLines as $headerLine) { // Line separated if (ctype_space($headerLines[0]) || false === strpos($headerLine, ':')) { $headers[$currentHeaderName] .= ' ' . trim($headerLine); continue; } $header = explode(':', $headerLine, 2); $currentHeaderName = strtolower($header[0]); $headers[$currentHeaderName] = trim($header[1]); } $messageStream = new Swift_ByteStream_TemporaryFileByteStream(); $messageStream->addFilter($this->replacementFactory->createFilter("\r\n", "\n"), 'CRLF to LF'); $messageStream->addFilter($this->replacementFactory->createFilter("\n", "\r\n"), 'LF to CRLF'); $messageHeaders = $message->getHeaders(); // No need to check for 'application/pkcs7-mime', as this is always base64 if ('multipart/signed;' === substr($headers['content-type'], 0, 17)) { if (!preg_match('/boundary=("[^"]+"|(?:[^\\s]+|$))/is', $headers['content-type'], $contentTypeData)) { throw new Swift_SwiftException('Failed to find Boundary parameter'); } $boundary = trim($contentTypeData['1'], '"'); $boundaryLen = strlen($boundary); // Skip the header and CRLF CRLF $fromStream->setReadPointer($headersPosEnd + 4); while (false !== ($buffer = $fromStream->read($bufferLength))) { $messageStream->write($buffer); } $messageStream->commit(); $messageHeaders->remove('Content-Transfer-Encoding'); $message->setContentType($headers['content-type']); $message->setBoundary($boundary); $message->setBody($messageStream); } else { $fromStream->setReadPointer($headersPosEnd + 4); if (null === $this->headerFactory) { $this->headerFactory = Swift_DependencyContainer::getInstance()->lookup('mime.headerfactory'); } $message->setContentType($headers['content-type']); $messageHeaders->set($this->headerFactory->createTextHeader('Content-Transfer-Encoding', $headers['content-transfer-encoding'])); $messageHeaders->set($this->headerFactory->createTextHeader('Content-Disposition', $headers['content-disposition'])); while (false !== ($buffer = $fromStream->read($bufferLength))) { $messageStream->write($buffer); } $messageStream->commit(); $message->setBody($messageStream); } }
public function testEncryptThenSignMessage() { $message = Swift_SignedMessage::newInstance('Wonderful Subject')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo(array('*****@*****.**', '*****@*****.**' => 'A name'))->setBody('Here is the message itself'); $originalMessage = $this->cleanMessage($message->toString()); $signer = Swift_Signers_SMimeSigner::newInstance(); $signer->setSignCertificate($this->samplesDir . 'smime/sign.crt', $this->samplesDir . 'smime/sign.key'); $signer->setEncryptCertificate($this->samplesDir . 'smime/encrypt.crt'); $signer->setSignThenEncrypt(false); $message->attachSigner($signer); $messageStream = $this->newFilteredStream(); $message->toByteStream($messageStream); $messageStream->commit(); $entityString = $messageStream->getContent(); $headers = self::getHeadersOfMessage($entityString); if (!($boundary = $this->getBoundary($headers['content-type']))) { return false; } $expectedBody = <<<OEL This is an S/MIME signed message --{$boundary} (?P<encrypted_message>MIME-Version: 1\\.0 Content-Disposition: attachment; filename="smime\\.p7m" Content-Type: application/(x\\-)?pkcs7-mime; smime-type=enveloped-data; name="smime\\.p7m" Content-Transfer-Encoding: base64 (?:^[a-zA-Z0-9\\/\\r\\n+]*={0,2}) )--{$boundary} Content-Type: application/(x\\-)?pkcs7-signature; name="smime\\.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime\\.p7s" (?:^[a-zA-Z0-9\\/\\r\\n+]*={0,2}) --{$boundary}-- OEL; if (!$this->assertValidVerify($expectedBody, $messageStream)) { return false; } $expectedBody = str_replace("\n", "\r\n", $expectedBody); if (!preg_match('%' . $expectedBody . '*%m', $entityString, $entities)) { $this->fail('Failed regex match.'); return false; } $messageStreamClean = new Swift_ByteStream_TemporaryFileByteStream(); $messageStreamClean->write($entities['encrypted_message']); $decryptedMessageStream = new Swift_ByteStream_TemporaryFileByteStream(); if (!openssl_pkcs7_decrypt($messageStreamClean->getPath(), $decryptedMessageStream->getPath(), 'file://' . $this->samplesDir . 'smime/encrypt.crt', array('file://' . $this->samplesDir . 'smime/encrypt.key', 'swift'))) { $this->fail(sprintf('Decrypt of the message failed. Internal error "%s".', openssl_error_string())); } $this->assertEquals($originalMessage, $decryptedMessageStream->getContent()); unset($messageStreamClean, $messageStream, $decryptedMessageStream); }