|
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
|
|
|
use LogicException; |
|
18
|
|
|
|
|
19
|
|
|
class CryptKey |
|
20
|
|
|
{ |
|
21
|
|
|
const RSA_KEY_PATTERN = |
|
22
|
|
|
'/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Key |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $key; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $keyPath; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var null|string |
|
36
|
|
|
*/ |
|
37
|
56 |
|
protected $passPhrase; |
|
38
|
|
|
|
|
39
|
56 |
|
/** |
|
40
|
1 |
|
* @param string $keyPath |
|
41
|
55 |
|
* @param null|string $passPhrase |
|
42
|
1 |
|
* @param bool $keyPermissionsCheck |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->passPhrase = $passPhrase; |
|
47
|
55 |
|
if ($rsaMatch = \preg_match(static::RSA_KEY_PATTERN, $keyPath)) { |
|
48
|
2 |
|
$this->key = InMemory::plainText($keyPath, $this->passPhrase ?? ''); |
|
49
|
|
|
} elseif ($rsaMatch === false) { |
|
50
|
|
|
throw new \RuntimeException( |
|
51
|
55 |
|
\sprintf('PCRE error [%d] encountered during key match attempt', \preg_last_error()) |
|
52
|
1 |
|
); |
|
53
|
|
|
} else { |
|
54
|
|
|
if (\strpos($keyPath, 'file://') !== 0) { |
|
55
|
54 |
|
$keyPath = 'file://' . $keyPath; |
|
56
|
|
|
} |
|
57
|
54 |
|
|
|
58
|
54 |
|
if (!\file_exists($keyPath) || !\is_readable($keyPath)) { |
|
59
|
|
|
throw new LogicException(\sprintf('Key path "%s" does not exist or is not readable', $keyPath)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($keyPermissionsCheck === true) { |
|
63
|
|
|
// Verify the permissions of the key |
|
64
|
|
|
$keyPathPerms = \decoct(\fileperms($keyPath) & 0777); |
|
65
|
|
|
if (\in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) { |
|
66
|
|
|
\trigger_error(\sprintf( |
|
67
|
54 |
|
'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s', |
|
68
|
54 |
|
$keyPath, |
|
69
|
54 |
|
$keyPathPerms |
|
70
|
|
|
), E_USER_NOTICE); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->keyPath = $keyPath; |
|
75
|
|
|
$this->key = LocalFileReference::file($this->keyPath, $this->passPhrase ?? ''); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
1 |
|
|
|
79
|
|
|
/** |
|
80
|
1 |
|
* Get key |
|
81
|
1 |
|
* |
|
82
|
|
|
* @return Key |
|
83
|
1 |
|
*/ |
|
84
|
|
|
public function getKey(): Key |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->key; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Retrieve key path. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
1 |
|
*/ |
|
94
|
|
|
public function getKeyPath() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->keyPath; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
/** |
|
100
|
|
|
* Retrieve key pass phrase. |
|
101
|
|
|
* |
|
102
|
|
|
* @return null|string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getPassPhrase() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->passPhrase; |
|
107
|
19 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|