1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWS\Algorithm; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
8
|
|
|
use Sop\JWX\JWK\JWK; |
9
|
|
|
use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK; |
10
|
|
|
use Sop\JWX\JWS\SignatureAlgorithm; |
11
|
|
|
use Sop\JWX\JWT\Header\Header; |
12
|
|
|
use Sop\JWX\JWT\Parameter\AlgorithmParameter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base class for algorithms implementing HMAC signature. |
16
|
|
|
* |
17
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-3.2 |
18
|
|
|
*/ |
19
|
|
|
abstract class HMACAlgorithm extends SignatureAlgorithm |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Mapping from algorithm name to class name. |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public const MAP_ALGO_TO_CLASS = [ |
29
|
|
|
JWA::ALGO_HS256 => HS256Algorithm::class, |
30
|
|
|
JWA::ALGO_HS384 => HS384Algorithm::class, |
31
|
|
|
JWA::ALGO_HS512 => HS512Algorithm::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Shared secret key. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $_key; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $key Shared secret key |
45
|
|
|
*/ |
46
|
23 |
|
public function __construct(string $key) |
47
|
|
|
{ |
48
|
23 |
|
$this->_key = $key; |
49
|
23 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
13 |
|
public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
57
|
|
|
{ |
58
|
13 |
|
$jwk = SymmetricKeyJWK::fromJWK($jwk); |
59
|
13 |
|
$alg = JWA::deriveAlgorithmName($header, $jwk); |
60
|
13 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
61
|
1 |
|
throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
62
|
|
|
} |
63
|
12 |
|
$cls = self::MAP_ALGO_TO_CLASS[$alg]; |
64
|
12 |
|
return new $cls($jwk->key()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @throws \RuntimeException For generic errors |
71
|
|
|
*/ |
72
|
27 |
|
public function computeSignature(string $data): string |
73
|
|
|
{ |
74
|
27 |
|
$result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
75
|
27 |
|
if (false === $result) { |
76
|
1 |
|
$err = error_get_last(); |
77
|
1 |
|
$msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
78
|
1 |
|
throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
79
|
|
|
} |
80
|
26 |
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
12 |
|
public function validateSignature(string $data, string $signature): bool |
87
|
|
|
{ |
88
|
12 |
|
return $this->computeSignature($data) === $signature; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
11 |
|
public function headerParameters(): array |
95
|
|
|
{ |
96
|
11 |
|
return array_merge(parent::headerParameters(), |
97
|
11 |
|
[AlgorithmParameter::fromAlgorithm($this)]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get algorithm name recognized by the Hash extension. |
102
|
|
|
*/ |
103
|
|
|
abstract protected function _hashAlgo(): string; |
104
|
|
|
} |
105
|
|
|
|