Skip to content

Commit ae28de7

Browse files
committed
[Mime] Add DKIM support
1 parent 284f6f2 commit ae28de7

File tree

5 files changed

+483
-1
lines changed

5 files changed

+483
-1
lines changed

src/Symfony/Component/Mime/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* Add support for DKIM
8+
49
4.4.0
510
-----
611

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
/**
15+
* A helper providing autocompletion for available DkimSigner options.
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
final class DkimOptions
20+
{
21+
private $options = [];
22+
23+
public function toArray(): array
24+
{
25+
return $this->options;
26+
}
27+
28+
/**
29+
* @return $this
30+
*/
31+
public function algorithm(int $algo): self
32+
{
33+
$this->options['algorithm'] = $algo;
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* @return $this
40+
*/
41+
public function signatureExpirationDelay(int $show): self
42+
{
43+
$this->options['signature_expiration_delay'] = $show;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* @return $this
50+
*/
51+
public function bodyMaxLength(int $max): self
52+
{
53+
$this->options['body_max_length'] = $max;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* @return $this
60+
*/
61+
public function bodyShowLength(bool $show): self
62+
{
63+
$this->options['body_show_length'] = $show;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* @return $this
70+
*/
71+
public function headerCanon(string $canon): self
72+
{
73+
$this->options['header_canon'] = $canon;
74+
75+
return $this;
76+
}
77+
78+
/**
79+
* @return $this
80+
*/
81+
public function bodyCanon(string $canon): self
82+
{
83+
$this->options['body_canon'] = $canon;
84+
85+
return $this;
86+
}
87+
88+
/**
89+
* @return $this
90+
*/
91+
public function headersToIgnore(array $headers): self
92+
{
93+
$this->options['headers_to_ignore'] = $headers;
94+
95+
return $this;
96+
}
97+
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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\InvalidArgumentException;
15+
use Symfony\Component\Mime\Exception\RuntimeException;
16+
use Symfony\Component\Mime\Header\UnstructuredHeader;
17+
use Symfony\Component\Mime\Message;
18+
use Symfony\Component\Mime\Part\AbstractPart;
19+
20+
/**
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*
23+
* RFC 6376 and 8301
24+
*/
25+
final class DkimSigner
26+
{
27+
public const CANON_SIMPLE = 'simple';
28+
public const CANON_RELAXED = 'relaxed';
29+
30+
public const ALGO_SHA_256 = 'rsa-sha256';
31+
// RFC 8463
32+
public const ALGO_ED25519 = 'ed25519-sha256';
33+
34+
private $key;
35+
private $domainName;
36+
private $selector;
37+
private $defaultOptions;
38+
39+
/**
40+
* @param string $pk The private key as a string or the path to the file containing the private key, should be prefixed with file:// (in PEM format)
41+
* @param string $passphrase A passphrase of the private key (if any)
42+
*/
43+
public function __construct(string $pk, string $domainName, string $selector, array $defaultOptions = [], string $passphrase = '')
44+
{
45+
if (!\extension_loaded('openssl')) {
46+
throw new \LogicException('PHP extension "openssl" is required to use DKIM.');
47+
}
48+
if (!$this->key = openssl_pkey_get_private($pk, $passphrase)) {
49+
throw new InvalidArgumentException('Unable to load DKIM private key: '.openssl_error_string());
50+
}
51+
52+
$this->domainName = $domainName;
53+
$this->selector = $selector;
54+
$this->defaultOptions = $defaultOptions + [
55+
'algorithm' => self::ALGO_SHA_256,
56+
'signature_expiration_delay' => 0,
57+
'body_max_length' => PHP_INT_MAX,
58+
'body_show_length' => false,
59+
'header_canon' => self::CANON_RELAXED,
60+
'body_canon' => self::CANON_RELAXED,
61+
'headers_to_ignore' => [],
62+
];
63+
}
64+
65+
public function sign(Message $message, array $options = []): Message
66+
{
67+
$options += $this->defaultOptions;
68+
if (!\in_array($options['algorithm'], [self::ALGO_SHA_256, self::ALGO_ED25519], true)) {
69+
throw new InvalidArgumentException('Invalid DKIM signing algorithm "%s".', $options['algorithm']);
70+
}
71+
$headersToIgnore['return-path'] = true;
72+
foreach ($options['headers_to_ignore'] as $name) {
73+
$headersToIgnore[strtolower($name)] = true;
74+
}
75+
unset($headersToIgnore['from']);
76+
$signedHeaderNames = [];
77+
$headerCanonData = '';
78+
$headers = $message->getPreparedHeaders();
79+
foreach ($headers->getNames() as $name) {
80+
foreach ($headers->all($name) as $header) {
81+
if (isset($headersToIgnore[strtolower($header->getName())])) {
82+
continue;
83+
}
84+
85+
if ('' !== $header->getBodyAsString()) {
86+
$headerCanonData .= $this->canonicalizeHeader($header->toString(), $options['header_canon']);
87+
$signedHeaderNames[] = $header->getName();
88+
}
89+
}
90+
}
91+
92+
[$bodyHash, $bodyLength] = $this->hashBody($message->getBody(), $options['body_canon'], $options['body_max_length']);
93+
94+
$params = [
95+
'v' => '1',
96+
'q' => 'dns/txt',
97+
'a' => $options['algorithm'],
98+
'bh' => base64_encode($bodyHash),
99+
'd' => $this->domainName,
100+
'h' => implode(': ', $signedHeaderNames),
101+
'i' => '@'.$this->domainName,
102+
's' => $this->selector,
103+
't' => time(),
104+
'c' => $options['header_canon'].'/'.$options['body_canon'],
105+
];
106+
107+
if ($options['body_show_length']) {
108+
$params['l'] = $bodyLength;
109+
}
110+
if ($options['signature_expiration_delay']) {
111+
$params['x'] = $params['t'] + $options['signature_expiration_delay'];
112+
}
113+
$value = '';
114+
foreach ($params as $k => $v) {
115+
$value .= $k.'='.$v.'; ';
116+
}
117+
$value = trim($value);
118+
$header = new UnstructuredHeader('DKIM-Signature', $value);
119+
$headerCanonData .= rtrim($this->canonicalizeHeader($header->toString()."\r\n b=", $options['header_canon']));
120+
if (self::ALGO_SHA_256 === $options['algorithm']) {
121+
if (!openssl_sign($headerCanonData, $signature, $this->key, OPENSSL_ALGO_SHA256)) {
122+
throw new RuntimeException('Unable to sign DKIM hash: '.openssl_error_string());
123+
}
124+
} else {
125+
throw new \RuntimeException(sprintf('The "%s" DKIM signing algorithm is not supported yet.', self::ALGO_ED25519));
126+
}
127+
$header->setValue($value.' b='.trim(chunk_split(base64_encode($signature), 73, ' ')));
128+
$headers->add($header);
129+
130+
return new Message($headers, $message->getBody());
131+
}
132+
133+
private function canonicalizeHeader(string $header, string $headerCanon): string
134+
{
135+
if (self::CANON_RELAXED !== $headerCanon) {
136+
return $header."\r\n";
137+
}
138+
139+
$exploded = explode(':', $header, 2);
140+
$name = strtolower(trim($exploded[0]));
141+
$value = str_replace("\r\n", '', $exploded[1]);
142+
$value = trim(preg_replace("/[ \t][ \t]+/", ' ', $value));
143+
144+
return $name.':'.$value."\r\n";
145+
}
146+
147+
private function hashBody(AbstractPart $body, string $bodyCanon, int $maxLength): array
148+
{
149+
$hash = hash_init('sha256');
150+
$relaxed = self::CANON_RELAXED === $bodyCanon;
151+
$currentLine = '';
152+
$emptyCounter = 0;
153+
$isSpaceSequence = false;
154+
$length = 0;
155+
foreach ($body->bodyToIterable() as $chunk) {
156+
$canon = '';
157+
for ($i = 0, $len = \strlen($chunk); $i < $len; ++$i) {
158+
switch ($chunk[$i]) {
159+
case "\r":
160+
break;
161+
case "\n":
162+
// previous char is always \r
163+
if ($relaxed) {
164+
$isSpaceSequence = false;
165+
}
166+
if ('' === $currentLine) {
167+
++$emptyCounter;
168+
} else {
169+
$currentLine = '';
170+
$canon .= "\r\n";
171+
}
172+
break;
173+
case ' ':
174+
case "\t":
175+
if ($relaxed) {
176+
$isSpaceSequence = true;
177+
break;
178+
}
179+
// no break
180+
default:
181+
if ($emptyCounter > 0) {
182+
$canon .= str_repeat("\r\n", $emptyCounter);
183+
$emptyCounter = 0;
184+
}
185+
if ($isSpaceSequence) {
186+
$currentLine .= ' ';
187+
$canon .= ' ';
188+
$isSpaceSequence = false;
189+
}
190+
$currentLine .= $chunk[$i];
191+
$canon .= $chunk[$i];
192+
}
193+
}
194+
195+
if ($length + \strlen($canon) >= $maxLength) {
196+
$canon = substr($canon, 0, $maxLength - $length);
197+
$length += \strlen($canon);
198+
hash_update($hash, $canon);
199+
200+
break;
201+
}
202+
203+
$length += \strlen($canon);
204+
hash_update($hash, $canon);
205+
}
206+
207+
if (0 === $length) {
208+
hash_update($hash, "\r\n");
209+
$length = 2;
210+
}
211+
212+
return [hash_final($hash, true), $length];
213+
}
214+
}

src/Symfony/Component/Mime/Message.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public function getPreparedHeaders(): Headers
8080
$headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
8181
}
8282

83-
$headers->addTextHeader('MIME-Version', '1.0');
83+
if (!$headers->has('MIME-Version')) {
84+
$headers->addTextHeader('MIME-Version', '1.0');
85+
}
8486

8587
if (!$headers->has('Date')) {
8688
$headers->addDateHeader('Date', new \DateTimeImmutable());

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy