Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Mime\Crypto;
13
 
14
use Symfony\Component\Mime\Exception\RuntimeException;
15
use Symfony\Component\Mime\Message;
16
 
17
/**
18
 * @author Sebastiaan Stok <s.stok@rollerscapes.net>
19
 */
20
final class SMimeSigner extends SMime
21
{
22
    private string $signCertificate;
23
    private string|array $signPrivateKey;
24
    private int $signOptions;
25
    private ?string $extraCerts;
26
 
27
    /**
28
     * @param string      $certificate          The path of the file containing the signing certificate (in PEM format)
29
     * @param string      $privateKey           The path of the file containing the private key (in PEM format)
30
     * @param string|null $privateKeyPassphrase A passphrase of the private key (if any)
31
     * @param string|null $extraCerts           The path of the file containing intermediate certificates (in PEM format) needed by the signing certificate
32
     * @param int|null    $signOptions          Bitwise operator options for openssl_pkcs7_sign() (@see https://secure.php.net/manual/en/openssl.pkcs7.flags.php)
33
     */
34
    public function __construct(string $certificate, string $privateKey, string $privateKeyPassphrase = null, string $extraCerts = null, int $signOptions = null)
35
    {
36
        if (!\extension_loaded('openssl')) {
37
            throw new \LogicException('PHP extension "openssl" is required to use SMime.');
38
        }
39
 
40
        $this->signCertificate = $this->normalizeFilePath($certificate);
41
 
42
        if (null !== $privateKeyPassphrase) {
43
            $this->signPrivateKey = [$this->normalizeFilePath($privateKey), $privateKeyPassphrase];
44
        } else {
45
            $this->signPrivateKey = $this->normalizeFilePath($privateKey);
46
        }
47
 
48
        $this->signOptions = $signOptions ?? \PKCS7_DETACHED;
49
        $this->extraCerts = $extraCerts ? realpath($extraCerts) : null;
50
    }
51
 
52
    public function sign(Message $message): Message
53
    {
54
        $bufferFile = tmpfile();
55
        $outputFile = tmpfile();
56
 
57
        $this->iteratorToFile($message->getBody()->toIterable(), $bufferFile);
58
 
59
        if (!@openssl_pkcs7_sign(stream_get_meta_data($bufferFile)['uri'], stream_get_meta_data($outputFile)['uri'], $this->signCertificate, $this->signPrivateKey, [], $this->signOptions, $this->extraCerts)) {
60
            throw new RuntimeException(sprintf('Failed to sign S/Mime message. Error: "%s".', openssl_error_string()));
61
        }
62
 
63
        return new Message($message->getHeaders(), $this->convertMessageToSMimePart($outputFile, 'multipart', 'signed'));
64
    }
65
}