1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Guarded Authentication package. |
4
|
|
|
* |
5
|
|
|
* (c) Jafar Jabr <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\OpenSSL; |
11
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\SignerInterface; |
14
|
|
|
use RuntimeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class HS512. |
18
|
|
|
* |
19
|
|
|
* @author Jafar Jabr <[email protected]> |
20
|
|
|
* Class handle sign inputs with the a public key algorithm, after hashing it. |
21
|
|
|
*/ |
22
|
|
|
abstract class PublicKey implements SignerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function sign($input, $key, $password = null) |
28
|
|
|
{ |
29
|
|
|
$keyResource = $this->getKeyResource($key, $password); |
30
|
|
|
if (!$this->supportsKey($keyResource)) { |
31
|
|
|
throw new InvalidArgumentException('Invalid key supplied.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$signature = null; |
35
|
|
|
openssl_sign($input, $signature, $keyResource, $this->getHashingAlgorithm()); |
|
|
|
|
36
|
|
|
|
37
|
|
|
return $signature; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function verify($key, $signature, $input) |
44
|
|
|
{ |
45
|
|
|
$keyResource = $this->getKeyResource($key); |
46
|
|
|
if (!$this->supportsKey($keyResource)) { |
47
|
|
|
throw new InvalidArgumentException('Invalid key supplied.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = openssl_verify($input, $signature, $keyResource, $this->getHashingAlgorithm()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if ($result === -1) { |
53
|
|
|
throw new RuntimeException('Unknown error during verification.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return (bool) $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Converts a string representation of a key into an OpenSSL resource. |
61
|
|
|
* |
62
|
|
|
* @param string|resource $key |
63
|
|
|
* @param string $password |
64
|
|
|
* |
65
|
|
|
* @return resource OpenSSL key resource |
66
|
|
|
*/ |
67
|
|
|
protected function getKeyResource($key, $password = null) |
68
|
|
|
{ |
69
|
|
|
if (is_resource($key)) { |
70
|
|
|
return $key; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$resource = openssl_pkey_get_public($key) ?: openssl_pkey_get_private($key, $password); |
74
|
|
|
if ($resource === false) { |
75
|
|
|
throw new RuntimeException('Could not read key resource: ' . openssl_error_string()); |
76
|
|
|
} |
77
|
|
|
return $resource; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check if the key is supported by this signer. |
82
|
|
|
* |
83
|
|
|
* @param resource $key Public or private key |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function supportsKey($key) |
88
|
|
|
{ |
89
|
|
|
// OpenSSL 0.9.8+ |
90
|
|
|
$keyDetails = openssl_pkey_get_details($key); |
91
|
|
|
|
92
|
|
|
return isset($keyDetails['type']) ? $this->getSupportedPrivateKeyType() === $keyDetails['type'] : false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the hashing algorithm used in this signer. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
abstract protected function getHashingAlgorithm(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the private key type supported in this signer. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
abstract protected function getSupportedPrivateKeyType(); |
108
|
|
|
} |
109
|
|
|
|