|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Backend; |
|
6
|
|
|
|
|
7
|
|
|
use SimpleSAML\Assert\Assert; |
|
8
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
|
9
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
|
10
|
|
|
use SimpleSAML\XMLSecurity\Key\KeyInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function hash_equals; |
|
13
|
|
|
use function hash_hmac; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Backend for digital signatures based on hash-based message authentication codes. |
|
17
|
|
|
* |
|
18
|
|
|
* @package SimpleSAML\XMLSecurity\Backend |
|
19
|
|
|
*/ |
|
20
|
|
|
final class HMAC implements SignatureBackend |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected string $digest; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Build an HMAC backend. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->digest = C::$DIGEST_ALGORITHMS[C::DIGEST_SHA256]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set the digest algorithm to be used by this backend. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $digest The identifier of the digest algorithm. |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If the given digest is not valid. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setDigestAlg(string $digest): void |
|
43
|
|
|
{ |
|
44
|
|
|
Assert::keyExists( |
|
45
|
|
|
C::$DIGEST_ALGORITHMS, |
|
46
|
|
|
$digest, |
|
47
|
|
|
'Unknown digest or non-cryptographic hash function.', |
|
48
|
|
|
InvalidArgumentException::class, |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$this->digest = C::$DIGEST_ALGORITHMS[$digest]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sign a given plaintext with this cipher and a given key. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The key to use to sign. |
|
59
|
|
|
* @param string $plaintext The original text to sign. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string The (binary) signature corresponding to the given plaintext. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function sign( |
|
64
|
|
|
#[\SensitiveParameter] |
|
65
|
|
|
KeyInterface $key, |
|
66
|
|
|
string $plaintext, |
|
67
|
|
|
): string { |
|
68
|
|
|
return hash_hmac($this->digest, $plaintext, $key->getMaterial(), true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Verify a signature with this cipher and a given key. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The key to use to verify the signature. |
|
76
|
|
|
* @param string $plaintext The original signed text. |
|
77
|
|
|
* @param string $signature The (binary) signature to verify. |
|
78
|
|
|
* |
|
79
|
|
|
* @return boolean True if the signature can be verified, false otherwise. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function verify( |
|
82
|
|
|
#[\SensitiveParameter] |
|
83
|
|
|
KeyInterface $key, |
|
84
|
|
|
string $plaintext, |
|
85
|
|
|
string $signature, |
|
86
|
|
|
): bool { |
|
87
|
|
|
return hash_equals(hash_hmac($this->digest, $plaintext, $key->getMaterial(), true), $signature); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|