TYPO3  7.6
OpenDKIMSigner.php
Go to the documentation of this file.
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 
18 {
19  private $_peclLoaded = false;
20 
21  private $_dkimHandler = null;
22 
23  private $dropFirstLF = true;
24 
25  const CANON_RELAXED = 1;
26  const CANON_SIMPLE = 2;
27  const SIG_RSA_SHA1 = 3;
28  const SIG_RSA_SHA256 = 4;
29 
30  public function __construct($privateKey, $domainName, $selector)
31  {
32  if (extension_loaded('opendkim')) {
33  $this->_peclLoaded = true;
34  } else {
35  throw new Swift_SwiftException('php-opendkim extension not found');
36  }
37  parent::__construct($privateKey, $domainName, $selector);
38  }
39 
40  public static function newInstance($privateKey, $domainName, $selector)
41  {
42  return new static($privateKey, $domainName, $selector);
43  }
44 
45  public function addSignature(Swift_Mime_HeaderSet $headers)
46  {
47  $header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature');
48  $headerVal = $this->_dkimHandler->getSignatureHeader();
49  if (!$headerVal) {
50  throw new Swift_SwiftException('OpenDKIM Error: '.$this->_dkimHandler->getError());
51  }
52  $header->setValue($headerVal);
53  $headers->set($header);
54 
55  return $this;
56  }
57 
58  public function setHeaders(Swift_Mime_HeaderSet $headers)
59  {
60  $bodyLen = $this->_bodyLen;
61  if (is_bool($bodyLen)) {
62  $bodyLen = -1;
63  }
64  $hash = ($this->_hashAlgorithm == 'rsa-sha1') ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
65  $bodyCanon = ($this->_bodyCanon == 'simple') ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
66  $headerCanon = ($this->_headerCanon == 'simple') ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
67  $this->_dkimHandler = new OpenDKIMSign($this->_privateKey, $this->_selector, $this->_domainName, $headerCanon, $bodyCanon, $hash, $bodyLen);
68  // Hardcode signature Margin for now
69  $this->_dkimHandler->setMargin(78);
70 
71  if (!is_numeric($this->_signatureTimestamp)) {
72  OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time());
73  } else {
74  if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->_signatureTimestamp)) {
75  throw new Swift_SwiftException('Unable to force signature timestamp ['.openssl_error_string().']');
76  }
77  }
78  if (isset($this->_signerIdentity)) {
79  $this->_dkimHandler->setSigner($this->_signerIdentity);
80  }
81  $listHeaders = $headers->listAll();
82  foreach ($listHeaders as $hName) {
83  // Check if we need to ignore Header
84  if (!isset($this->_ignoredHeaders[strtolower($hName)])) {
85  $tmp = $headers->getAll($hName);
86  if ($headers->has($hName)) {
87  foreach ($tmp as $header) {
88  if ($header->getFieldBody() != '') {
89  $htosign = $header->toString();
90  $this->_dkimHandler->header($htosign);
91  $this->_signedHeaders[] = $header->getFieldName();
92  }
93  }
94  }
95  }
96  }
97 
98  return $this;
99  }
100 
101  public function startBody()
102  {
103  if (!$this->_peclLoaded) {
104  return parent::startBody();
105  }
106  $this->dropFirstLF = true;
107  $this->_dkimHandler->eoh();
108 
109  return $this;
110  }
111 
112  public function endBody()
113  {
114  if (!$this->_peclLoaded) {
115  return parent::endBody();
116  }
117  $this->_dkimHandler->eom();
118 
119  return $this;
120  }
121 
122  public function reset()
123  {
124  $this->_dkimHandler = null;
125  parent::reset();
126 
127  return $this;
128  }
129 
137  public function setSignatureTimestamp($time)
138  {
139  $this->_signatureTimestamp = $time;
140 
141  return $this;
142  }
143 
151  public function setSignatureExpiration($time)
152  {
153  $this->_signatureExpiration = $time;
154 
155  return $this;
156  }
157 
165  public function setDebugHeaders($debug)
166  {
167  $this->_debugHeaders = (bool) $debug;
168 
169  return $this;
170  }
171 
172  // Protected
173 
174  protected function _canonicalizeBody($string)
175  {
176  if (!$this->_peclLoaded) {
177  return parent::_canonicalizeBody($string);
178  }
179  if (false && $this->dropFirstLF === true) {
180  if ($string[0] == "\r" && $string[1] == "\n") {
181  $string = substr($string, 2);
182  }
183  }
184  $this->dropFirstLF = false;
185  if (strlen($string)) {
186  $this->_dkimHandler->body($string);
187  }
188  }
189 }