|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of SwiftMailer. |
|
5
|
|
|
* (c) 2004-2009 Chris Corbyn |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* DKIM Signer used to apply DKIM Signature to a message |
|
13
|
|
|
* Takes advantage of pecl extension. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Xavier De Cock <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_peclLoaded = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var null|OpenDKIMSign |
|
26
|
|
|
*/ |
|
27
|
|
|
private $_dkimHandler = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
private $dropFirstLF = true; |
|
33
|
|
|
|
|
34
|
|
|
const CANON_RELAXED = 1; |
|
35
|
|
|
const CANON_SIMPLE = 2; |
|
36
|
|
|
const SIG_RSA_SHA1 = 3; |
|
37
|
|
|
const SIG_RSA_SHA256 = 4; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Swift_Signers_OpenDKIMSigner constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $privateKey |
|
43
|
|
|
* @param string $domainName |
|
44
|
|
|
* @param string $selector |
|
45
|
|
|
* |
|
46
|
|
|
* @throws Swift_SwiftException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($privateKey, $domainName, $selector) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!extension_loaded('opendkim')) { |
|
51
|
|
|
throw new Swift_SwiftException('php-opendkim extension not found'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->_peclLoaded = true; |
|
55
|
|
|
|
|
56
|
|
|
parent::__construct($privateKey, $domainName, $selector); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $privateKey |
|
61
|
|
|
* @param string $domainName |
|
62
|
|
|
* @param string $selector |
|
63
|
|
|
* |
|
64
|
|
|
* @return static |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function newInstance($privateKey, $domainName, $selector) |
|
67
|
|
|
{ |
|
68
|
|
|
return new static($privateKey, $domainName, $selector); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Swift_Mime_HeaderSet $headers |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
* @throws Swift_SwiftException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addSignature(Swift_Mime_HeaderSet $headers) |
|
78
|
|
|
{ |
|
79
|
|
|
$header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature'); |
|
80
|
|
|
$headerVal = $this->_dkimHandler->getSignatureHeader(); |
|
81
|
|
|
if (!$headerVal) { |
|
82
|
|
|
throw new Swift_SwiftException('OpenDKIM Error: ' . $this->_dkimHandler->getError()); |
|
83
|
|
|
} |
|
84
|
|
|
$header->setValue($headerVal); |
|
85
|
|
|
$headers->set($header); |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Swift_Mime_HeaderSet $headers |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
* @throws Swift_SwiftException |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setHeaders(Swift_Mime_HeaderSet $headers) |
|
97
|
|
|
{ |
|
98
|
|
|
$bodyLen = $this->_bodyLen; |
|
99
|
|
|
if (is_bool($bodyLen)) { |
|
100
|
|
|
$bodyLen = -1; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$hash = $this->_hashAlgorithm === 'rsa-sha1' ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256; |
|
104
|
|
|
$bodyCanon = $this->_bodyCanon === 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED; |
|
105
|
|
|
$headerCanon = $this->_headerCanon === 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED; |
|
106
|
|
|
$this->_dkimHandler = new OpenDKIMSign($this->_privateKey, $this->_selector, $this->_domainName, $headerCanon, $bodyCanon, $hash, $bodyLen); |
|
107
|
|
|
|
|
108
|
|
|
// Hardcode signature Margin for now |
|
109
|
|
|
$this->_dkimHandler->setMargin(78); |
|
110
|
|
|
|
|
111
|
|
|
if (!is_numeric($this->_signatureTimestamp)) { |
|
112
|
|
|
OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time()); |
|
113
|
|
|
} else { |
|
114
|
|
|
if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->_signatureTimestamp)) { |
|
115
|
|
|
throw new Swift_SwiftException('Unable to force signature timestamp [' . openssl_error_string() . ']'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (isset($this->_signerIdentity)) { |
|
120
|
|
|
$this->_dkimHandler->setSigner($this->_signerIdentity); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$listHeaders = $headers->listAll(); |
|
124
|
|
View Code Duplication |
foreach ($listHeaders as $hName) { |
|
|
|
|
|
|
125
|
|
|
// Check if we need to ignore Header |
|
126
|
|
|
if (!isset($this->_ignoredHeaders[Swift::strtolowerWithStaticCache($hName)])) { |
|
127
|
|
|
$tmp = $headers->getAll($hName); |
|
128
|
|
|
if ($headers->has($hName)) { |
|
129
|
|
|
foreach ($tmp as $header) { |
|
130
|
|
|
if ($header->getFieldBody() != '') { |
|
131
|
|
|
$htosign = $header->toString(); |
|
132
|
|
|
$this->_dkimHandler->header($htosign); |
|
133
|
|
|
$this->_signedHeaders[] = $header->getFieldName(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return $this|Swift_Signers_HeaderSigner|void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function startBody() |
|
147
|
|
|
{ |
|
148
|
|
|
if (!$this->_peclLoaded) { |
|
149
|
|
|
return parent::startBody(); |
|
150
|
|
|
} |
|
151
|
|
|
$this->dropFirstLF = true; |
|
152
|
|
|
$this->_dkimHandler->eoh(); |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return $this|Swift_Signers_HeaderSigner|void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function endBody() |
|
161
|
|
|
{ |
|
162
|
|
|
if (!$this->_peclLoaded) { |
|
163
|
|
|
return parent::endBody(); |
|
164
|
|
|
} |
|
165
|
|
|
$this->_dkimHandler->eom(); |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
|
|
public function reset() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->_dkimHandler = null; |
|
176
|
|
|
parent::reset(); |
|
177
|
|
|
|
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Set the signature timestamp. |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $time |
|
185
|
|
|
* |
|
186
|
|
|
* @return $this |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setSignatureTimestamp($time) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->_signatureTimestamp = $time; |
|
191
|
|
|
|
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set the signature expiration timestamp. |
|
197
|
|
|
* |
|
198
|
|
|
* @param int $time |
|
199
|
|
|
* |
|
200
|
|
|
* @return $this |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setSignatureExpiration($time) |
|
203
|
|
|
{ |
|
204
|
|
|
$this->_signatureExpiration = $time; |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Enable / disable the DebugHeaders. |
|
211
|
|
|
* |
|
212
|
|
|
* @param bool $debug |
|
213
|
|
|
* |
|
214
|
|
|
* @return $this |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setDebugHeaders($debug) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->_debugHeaders = (bool) $debug; |
|
219
|
|
|
|
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
// Protected |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param $string |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function _canonicalizeBody($string) |
|
229
|
|
|
{ |
|
230
|
|
|
if (!$this->_peclLoaded) { |
|
231
|
|
|
return parent::_canonicalizeBody($string); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if (false && $this->dropFirstLF === true) { |
|
235
|
|
|
if ( |
|
236
|
|
|
$string[0] === "\r" |
|
237
|
|
|
&& |
|
238
|
|
|
$string[1] === "\n" |
|
239
|
|
|
) { |
|
240
|
|
|
$string = substr($string, 2); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$this->dropFirstLF = false; |
|
245
|
|
|
|
|
246
|
|
|
if ($string !== '') { |
|
247
|
|
|
$this->_dkimHandler->body($string); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.