10000 [Mime] add check for openssl when using SMime · symfony/symfony@f33c67b · GitHub
[go: up one dir, main page]

Skip to content

Commit f33c67b

Browse files
[Mime] add check for openssl when using SMime
1 parent 09e3cef commit f33c67b

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

src/Symfony/Component/ErrorCatcher/Exception/FlattenException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class FlattenException
3636
private $file;
3737
private $line;
3838

39-
public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = []): self
39+
public static function createFromThrowable(\Throwable $exception, int $statusCode = null, array $headers = []): self
4040
{
4141
$e = new static();
4242
$e->setMessage($exception->getMessage());

src/Symfony/Component/Mime/Crypto/SMimeEncrypter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ final class SMimeEncrypter extends SMime
2424

2525
/**
2626
* @param string|string[] $certificate The path (or array of paths) of the file(s) containing the X.509 certificate(s)
27-
* @param int $cipher A set of algorithms used to encrypt the message. Must be one of these PHP constants: https://www.php.net/manual/en/openssl.ciphers.php
27+
* @param int|null $cipher A set of algorithms used to encrypt the message. Must be one of these PHP constants: https://www.php.net/manual/en/openssl.ciphers.php
2828
*/
29-
public function __construct($certificate, int $cipher = OPENSSL_CIPHER_AES_256_CBC)
29+
public function __construct($certificate, int $cipher = null)
3030
{
31+
if (!\extension_loaded('openssl')) {
32+
throw new \LogicException('PHP extension "openssl" is required to use SMime.');
33+
}
34+
3135
if (\is_array($certificate)) {
3236
$this->certs = array_map([$this, 'normalizeFilePath'], $certificate);
3337
} else {
3438
$this->certs = $this->normalizeFilePath($certificate);
3539
}
3640

37-
$this->cipher = $cipher;
41+
$this->cipher = $cipher ?? OPENSSL_CIPHER_AES_256_CBC;
3842
}
3943

4044
public function encrypt(Message $message): Message

src/Symfony/Component/Mime/Crypto/SMimeSigner.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ final class SMimeSigner extends SMime
3434
* @param string $privateKey The path of the file containing the private key (in PEM format)
3535
* @param string|null $privateKeyPassphrase A passphrase of the private key (if any)
3636
* @param string|null $extraCerts The path of the file containing intermediate certificates (in PEM format) needed by the signing certificate
37-
* @param int $signOptions Bitwise operator options for openssl_pkcs7_sign() (@see https://secure.php.net/manual/en/openssl.pkcs7.flags.php)
37+
* @param int|null $signOptions Bitwise operator options for openssl_pkcs7_sign() (@see https://secure.php.net/manual/en/openssl.pkcs7.flags.php)
3838
*/
39-
public function __construct(string $certificate, string $privateKey, ?string $privateKeyPassphrase = null, ?string $extraCerts = null, int $signOptions = PKCS7_DETACHED)
39+
public function __construct(string $certificate, string $privateKey, string $privateKeyPassphrase = null, string $extraCerts = null, int $signOptions = null)
4040
{
41+
if (!\extension_loaded('openssl')) {
42+
throw new \LogicException('PHP extension "openssl" is required to use SMime.');
43+
}
44+
4145
$this->signCertificate = $this->normalizeFilePath($certificate);
4246

4347
if (null !== $privateKeyPassphrase) {
@@ -46,7 +50,7 @@ public function __construct(string $certificate, string $privateKey, ?string $pr
4650
$this->signPrivateKey = $this->normalizeFilePath($privateKey);
4751
}
4852

49-
$this->signOptions = $signOptions;
53+
$this->signOptions = $signOptions ?? PKCS7_DETACHED;
5054
$this->extraCerts = $extraCerts ? realpath($extraCerts) : null;
5155
$this->privateKeyPassphrase = $privateKeyPassphrase;
5256
}

src/Symfony/Component/Mime/Tests/Crypto/SMimeEncryptorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\Mime\Email;
1717
use Symfony\Component\Mime\Message;
1818

19+
/**
20+
* @requires extension openssl
21+
*/
1922
class SMimeEncryptorTest extends SMimeTestCase
2023
{
2124
public function testEncryptMessage()

src/Symfony/Component/Mime/Tests/Crypto/SMimeSignerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Symfony\Component\Mime\Message;
1919
use Symfony\Component\Mime\Part\TextPart;
2020

21+
/**
22+
* @requires extension openssl
23+
*/
2124
class SMimeSignerTest extends SMimeTestCase
2225
{
2326
public function testSignedMessage()

0 commit comments

Comments
 (0)
0