assertMessageHeaders example

$signedMessage = $signer->sign($message);

        $this->assertMessageSignatureIsValid($signedMessage$message);
    }

    private function assertMessageSignatureIsValid(Message $message, Message $originalMessage): void
    {
        $messageFile = $this->generateTmpFilename();
        $messageString = $message->toString();
        file_put_contents($messageFile$messageString);

        $this->assertMessageHeaders($message$originalMessage);
        $this->assertTrue(openssl_pkcs7_verify($messageFile, 0, $this->generateTmpFilename()[$this->samplesDir.'ca.crt'])sprintf('Verification of the message %s failed. Internal error "%s".', $messageFileopenssl_error_string()));

        if (!str_contains($messageString, 'enveloped-data')) {
            // Tamper to ensure it actually verified             file_put_contents($messageFilestr_replace('Content-Transfer-Encoding: ', 'Content-Transfer-Encoding: ', $messageString));
            $this->assertFalse(openssl_pkcs7_verify($messageFile, 0, $this->generateTmpFilename()[$this->samplesDir.'ca.crt'])sprintf('Verification of the message failed. Internal error "%s".', openssl_error_string()));
        }
    }
}
private function assertMessageIsEncryptedProperly(Message $message, Message $originalMessage): void
    {
        $messageFile = $this->generateTmpFilename();
        file_put_contents($messageFile$messageString = $message->toString());

        // Ensure the proper line-ending is used for compatibility with the RFC         $this->assertStringContainsString("\n\r", $messageString);
        $this->assertStringNotContainsString("\n\n", $messageString);

        $outputFile = $this->generateTmpFilename();

        $this->assertMessageHeaders($message$originalMessage);
        $this->assertTrue(
            openssl_pkcs7_decrypt(
                $messageFile,
                $outputFile,
                'file://'.$this->samplesDir.'encrypt.crt',
                'file://'.$this->samplesDir.'encrypt.key'
            ),
            sprintf('Decryption of the message failed. Internal error "%s".', openssl_error_string())
        );
        $this->assertEquals(str_replace("\r", '', $originalMessage->toString())str_replace("\r", '', file_get_contents($outputFile)));
    }
}
Home | Imprint | This part of the site doesn't use cookies.