1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Dkim; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\FailedToSignHeadersException; |
7
|
|
|
|
8
|
|
|
final class Sha256Signer implements SignInterface |
9
|
|
|
{ |
10
|
|
|
private const SIGN_ALGORITHM = OPENSSL_ALGO_SHA256; |
11
|
|
|
|
12
|
|
|
private const HASH_ALGORITHM = 'sha256'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var resource |
16
|
|
|
*/ |
17
|
|
|
private $privateKey; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param resource $key |
21
|
|
|
*/ |
22
|
6 |
|
public function __construct($key) |
23
|
|
|
{ |
24
|
6 |
|
if (!\is_resource($key) || \get_resource_type($key) !== 'OpenSSL key') { |
25
|
1 |
|
throw new \InvalidArgumentException('Expected a private key resource'); |
26
|
|
|
} |
27
|
|
|
|
28
|
5 |
|
$this->privateKey = $key; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $canonicalizedBody |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
4 |
|
public function hashBody(string $canonicalizedBody): string |
36
|
|
|
{ |
37
|
4 |
|
return \hash(self::HASH_ALGORITHM, $canonicalizedBody, true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $canonicalizedHeaders |
42
|
|
|
* @return string |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
4 |
|
public function signHeaders(string $canonicalizedHeaders): string |
46
|
|
|
{ |
47
|
4 |
|
if (\openssl_sign($canonicalizedHeaders, $signature, $this->privateKey, self::SIGN_ALGORITHM)) { |
48
|
4 |
|
return $signature; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// @codeCoverageIgnoreStart |
52
|
|
|
throw new FailedToSignHeadersException( |
53
|
|
|
\sprintf('Unable to sign DKIM Hash, openssl error: %s', \openssl_error_string()) |
54
|
|
|
); |
55
|
|
|
// @codeCoverageIgnoreEnd |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
2 |
|
public function name(): string |
62
|
|
|
{ |
63
|
2 |
|
return 'rsa-sha256'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $file |
68
|
|
|
* @param string $passphrase |
69
|
|
|
* @return Sha256Signer |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
7 |
|
public static function fromFile(string $file, string $passphrase = ''): self |
73
|
|
|
{ |
74
|
7 |
|
if (!\file_exists($file)) { |
75
|
1 |
|
throw new \InvalidArgumentException('File does not exist'); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$content = \file_get_contents($file); |
79
|
6 |
|
if ($content === false) { |
80
|
|
|
throw new \UnexpectedValueException('Cannot read file ' . $file); |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
return self::fromString($content, $passphrase); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $privateKeyString |
88
|
|
|
* @param string $passphrase |
89
|
|
|
* @return Sha256Signer |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
7 |
|
public static function fromString(string $privateKeyString, string $passphrase = ''): self |
93
|
|
|
{ |
94
|
7 |
|
$key = \openssl_pkey_get_private($privateKeyString, $passphrase); |
95
|
|
|
|
96
|
7 |
|
if ($key === false) { |
97
|
2 |
|
throw new \InvalidArgumentException('Cannot create resource from private key string'); |
98
|
|
|
} |
99
|
|
|
|
100
|
5 |
|
return new self($key); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|