1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Algorithm; |
4
|
|
|
|
5
|
|
|
use Gandung\JWT\Adapter\ECDSAAdapterInterface; |
6
|
|
|
use Gandung\JWT\SignerInterface; |
7
|
|
|
use Gandung\JWT\Manager\KeyManagerInterface; |
8
|
|
|
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface; |
9
|
|
|
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface; |
10
|
|
|
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer; |
11
|
|
|
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer; |
12
|
|
|
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer; |
13
|
|
|
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class ECDSA implements SignerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ECDSAAdapterInterface |
22
|
|
|
*/ |
23
|
|
|
private $adapter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var $private |
27
|
|
|
*/ |
28
|
|
|
private $private; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var $public |
32
|
|
|
*/ |
33
|
|
|
private $public; |
34
|
|
|
|
35
|
|
|
public function __construct(ECDSAAdapterInterface $adapter) |
36
|
|
|
{ |
37
|
|
|
$this->adapter = $adapter; |
38
|
|
|
$publicDer = new DerPublicKeySerializer($this->adapter->getMathAdapter()); |
39
|
|
|
$this->public = new PemPublicKeySerializer($publicDer); |
40
|
|
|
$this->private = new PemPrivateKeySerializer( |
41
|
|
|
new DerPrivateKeySerializer($this->adapter->getMathAdapter(), $publicDer) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function sign($payload, KeyManagerInterface $key) |
49
|
|
|
{ |
50
|
|
|
return $this->adapter->sign( |
51
|
|
|
$this->getPrivateKey($key), |
52
|
|
|
$this->adapter->createSigningHash($payload, $this->getAlgorithm()), |
|
|
|
|
53
|
|
|
$this->getAlgorithm() |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function verify($expected, $payload, KeyManagerInterface $key) |
61
|
|
|
{ |
62
|
|
|
return $this->adapter->verify( |
63
|
|
|
$expected, |
64
|
|
|
$this->getPublicKey($key), |
65
|
|
|
$this->adapter->createSigningHash($payload, $this->getAlgorithm()), |
|
|
|
|
66
|
|
|
$this->getAlgorithm() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get private key object from given certificate. |
72
|
|
|
* |
73
|
|
|
* @param string $key |
74
|
|
|
* @return PrivateKeyInterface |
75
|
|
|
*/ |
76
|
|
|
public function getPrivateKey(KeyManagerInterface $key) |
77
|
|
|
{ |
78
|
|
|
return $this->private->parse($key->getContent()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get public key object from given certificate. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @return PublicKeyInterface |
86
|
|
|
*/ |
87
|
|
|
public function getPublicKey(KeyManagerInterface $key) |
88
|
|
|
{ |
89
|
|
|
return $this->public->parse($key->getContent()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.