1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cryptography key holder. |
4
|
|
|
* |
5
|
|
|
* @author Julián Gutiérrez <[email protected]> |
6
|
|
|
* @copyright Copyright (c) Alex Bilbie |
7
|
|
|
* @license http://mit-license.org/ |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\OAuth2\Server; |
13
|
|
|
|
14
|
|
|
use Lcobucci\JWT\Signer\Key; |
15
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
16
|
|
|
use Lcobucci\JWT\Signer\Key\LocalFileReference; |
17
|
|
|
|
18
|
|
|
use LogicException; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
class CryptKey |
22
|
|
|
{ |
23
|
|
|
const RSA_KEY_PATTERN = |
24
|
|
|
'/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Key |
28
|
|
|
*/ |
29
|
|
|
protected $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $keyPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
56 |
|
* @var null|string |
38
|
|
|
*/ |
39
|
56 |
|
protected $passPhrase; |
40
|
1 |
|
|
41
|
55 |
|
/** |
42
|
1 |
|
* @param string $keyPath |
43
|
1 |
|
* @param null|string $passPhrase |
44
|
|
|
* @param bool $keyPermissionsCheck |
45
|
|
|
*/ |
46
|
|
|
public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true) |
47
|
55 |
|
{ |
48
|
2 |
|
if ($rsaMatch = \preg_match(static::RSA_KEY_PATTERN, $keyPath)) { |
49
|
|
|
$this->key = InMemory::plainText($keyPath); |
50
|
|
|
} elseif ($rsaMatch === false) { |
51
|
55 |
|
throw new \RuntimeException( |
52
|
1 |
|
\sprintf('PCRE error [%d] encountered during key match attempt', \preg_last_error()) |
53
|
|
|
); |
54
|
|
|
} |
55
|
54 |
|
else |
56
|
|
|
{ |
57
|
54 |
|
if (\strpos($keyPath, 'file://') !== 0) { |
58
|
54 |
|
$keyPath = 'file://' . $keyPath; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!\file_exists($keyPath) || !\is_readable($keyPath)) { |
62
|
|
|
throw new LogicException(\sprintf('Key path "%s" does not exist or is not readable', $keyPath)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($keyPermissionsCheck === true) { |
66
|
|
|
// Verify the permissions of the key |
67
|
54 |
|
$keyPathPerms = \decoct(\fileperms($keyPath) & 0777); |
68
|
54 |
|
if (\in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) { |
69
|
54 |
|
\trigger_error(\sprintf( |
70
|
|
|
'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s', |
71
|
|
|
$keyPath, |
72
|
|
|
$keyPathPerms |
73
|
|
|
), E_USER_NOTICE); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->keyPath = $keyPath; |
78
|
1 |
|
$this->passPhrase = $passPhrase; |
79
|
|
|
$this->key = LocalFileReference::file($this->keyPath, $this->passPhrase ?? ''); |
80
|
1 |
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
|
84
|
|
|
/** |
85
|
|
|
* Get key |
86
|
|
|
* |
87
|
1 |
|
* @return Key |
88
|
|
|
*/ |
89
|
|
|
public function getKey(): Key |
90
|
|
|
{ |
91
|
|
|
return $this->key; |
92
|
|
|
} |
93
|
1 |
|
|
94
|
|
|
/** |
95
|
|
|
* Retrieve key path. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function getKeyPath() |
100
|
|
|
{ |
101
|
|
|
return $this->keyPath; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieve key pass phrase. |
106
|
|
|
* |
107
|
19 |
|
* @return null|string |
108
|
|
|
*/ |
109
|
19 |
|
public function getPassPhrase() |
110
|
|
|
{ |
111
|
|
|
return $this->passPhrase; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|