dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/Signers/OpenDKIMSigner.php

184 lines
5.0 KiB
PHP
Raw Normal View History

2016-04-16 18:15:03 +02:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* DKIM Signer used to apply DKIM Signature to a message
* Takes advantage of pecl extension.
*
2018-01-21 15:55:56 +01:00
* @author Xavier De Cock <xdecock@gmail.com>
2021-12-07 17:11:34 +01:00
*
* @deprecated since SwiftMailer 6.1.0; use Swift_Signers_DKIMSigner instead.
2016-04-16 18:15:03 +02:00
*/
class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
{
2018-01-21 15:55:56 +01:00
private $peclLoaded = false;
2016-04-16 18:15:03 +02:00
2018-01-21 15:55:56 +01:00
private $dkimHandler = null;
2016-04-16 18:15:03 +02:00
private $dropFirstLF = true;
const CANON_RELAXED = 1;
const CANON_SIMPLE = 2;
const SIG_RSA_SHA1 = 3;
const SIG_RSA_SHA256 = 4;
public function __construct($privateKey, $domainName, $selector)
{
2021-12-07 17:11:34 +01:00
if (!\extension_loaded('opendkim')) {
2016-04-16 18:15:03 +02:00
throw new Swift_SwiftException('php-opendkim extension not found');
}
2018-01-21 15:55:56 +01:00
$this->peclLoaded = true;
2016-04-16 18:15:03 +02:00
parent::__construct($privateKey, $domainName, $selector);
}
2018-01-21 15:55:56 +01:00
public function addSignature(Swift_Mime_SimpleHeaderSet $headers)
2016-04-16 18:15:03 +02:00
{
$header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature');
2018-01-21 15:55:56 +01:00
$headerVal = $this->dkimHandler->getSignatureHeader();
2021-12-07 17:11:34 +01:00
if (false === $headerVal || \is_int($headerVal)) {
2018-01-21 15:55:56 +01:00
throw new Swift_SwiftException('OpenDKIM Error: '.$this->dkimHandler->getError());
2016-04-16 18:15:03 +02:00
}
$header->setValue($headerVal);
$headers->set($header);
return $this;
}
2018-01-21 15:55:56 +01:00
public function setHeaders(Swift_Mime_SimpleHeaderSet $headers)
2016-04-16 18:15:03 +02:00
{
2021-12-07 17:11:34 +01:00
$hash = 'rsa-sha1' == $this->hashAlgorithm ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
$bodyCanon = 'simple' == $this->bodyCanon ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
$headerCanon = 'simple' == $this->headerCanon ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
$this->dkimHandler = new OpenDKIMSign($this->privateKey, $this->selector, $this->domainName, $headerCanon, $bodyCanon, $hash, -1);
2016-04-16 18:15:03 +02:00
// Hardcode signature Margin for now
2018-01-21 15:55:56 +01:00
$this->dkimHandler->setMargin(78);
2016-04-16 18:15:03 +02:00
2018-01-21 15:55:56 +01:00
if (!is_numeric($this->signatureTimestamp)) {
2016-04-16 18:15:03 +02:00
OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time());
} else {
2018-01-21 15:55:56 +01:00
if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->signatureTimestamp)) {
2016-04-16 18:15:03 +02:00
throw new Swift_SwiftException('Unable to force signature timestamp ['.openssl_error_string().']');
}
}
2018-01-21 15:55:56 +01:00
if (isset($this->signerIdentity)) {
$this->dkimHandler->setSigner($this->signerIdentity);
2016-04-16 18:15:03 +02:00
}
$listHeaders = $headers->listAll();
foreach ($listHeaders as $hName) {
// Check if we need to ignore Header
2021-12-07 17:11:34 +01:00
if (!isset($this->ignoredHeaders[strtolower($hName ?? '')])) {
2016-04-16 18:15:03 +02:00
$tmp = $headers->getAll($hName);
if ($headers->has($hName)) {
foreach ($tmp as $header) {
2021-12-07 17:11:34 +01:00
if ('' != $header->getFieldBody()) {
2016-04-16 18:15:03 +02:00
$htosign = $header->toString();
2018-01-21 15:55:56 +01:00
$this->dkimHandler->header($htosign);
$this->signedHeaders[] = $header->getFieldName();
2016-04-16 18:15:03 +02:00
}
}
}
}
}
return $this;
}
public function startBody()
{
2018-01-21 15:55:56 +01:00
if (!$this->peclLoaded) {
2016-04-16 18:15:03 +02:00
return parent::startBody();
}
$this->dropFirstLF = true;
2018-01-21 15:55:56 +01:00
$this->dkimHandler->eoh();
2016-04-16 18:15:03 +02:00
return $this;
}
public function endBody()
{
2018-01-21 15:55:56 +01:00
if (!$this->peclLoaded) {
2016-04-16 18:15:03 +02:00
return parent::endBody();
}
2018-01-21 15:55:56 +01:00
$this->dkimHandler->eom();
2016-04-16 18:15:03 +02:00
return $this;
}
public function reset()
{
2018-01-21 15:55:56 +01:00
$this->dkimHandler = null;
2016-04-16 18:15:03 +02:00
parent::reset();
return $this;
}
/**
* Set the signature timestamp.
*
* @param int $time
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setSignatureTimestamp($time)
{
2018-01-21 15:55:56 +01:00
$this->signatureTimestamp = $time;
2016-04-16 18:15:03 +02:00
return $this;
}
/**
* Set the signature expiration timestamp.
*
* @param int $time
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setSignatureExpiration($time)
{
2018-01-21 15:55:56 +01:00
$this->signatureExpiration = $time;
2016-04-16 18:15:03 +02:00
return $this;
}
/**
* Enable / disable the DebugHeaders.
*
* @param bool $debug
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setDebugHeaders($debug)
{
2018-01-21 15:55:56 +01:00
$this->debugHeaders = (bool) $debug;
2016-04-16 18:15:03 +02:00
return $this;
}
// Protected
2018-01-21 15:55:56 +01:00
protected function canonicalizeBody($string)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (!$this->peclLoaded) {
return parent::canonicalizeBody($string);
2016-04-16 18:15:03 +02:00
}
2021-12-07 17:11:34 +01:00
if (true === $this->dropFirstLF) {
if ("\r" == $string[0] && "\n" == $string[1]) {
2016-04-16 18:15:03 +02:00
$string = substr($string, 2);
}
}
$this->dropFirstLF = false;
2021-12-07 17:11:34 +01:00
if (\strlen($string)) {
2018-01-21 15:55:56 +01:00
$this->dkimHandler->body($string);
2016-04-16 18:15:03 +02:00
}
}
}