1 | <?php |
||
14 | class CryptKey |
||
15 | { |
||
16 | const RSA_KEY_PATTERN = |
||
17 | '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s'; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $keyPath; |
||
23 | |||
24 | /** |
||
25 | * @var null|string |
||
26 | */ |
||
27 | protected $passPhrase; |
||
28 | |||
29 | /** |
||
30 | * @param string $keyPath |
||
31 | * @param null|string $passPhrase |
||
32 | * @param bool $keyPermissionsCheck |
||
33 | */ |
||
34 | 22 | public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true) |
|
35 | { |
||
36 | 22 | if (preg_match(self::RSA_KEY_PATTERN, $keyPath)) { |
|
37 | $keyPath = $this->saveKeyToFile($keyPath); |
||
38 | } |
||
39 | |||
40 | 22 | if (strpos($keyPath, 'file://') !== 0) { |
|
41 | $keyPath = 'file://' . $keyPath; |
||
42 | } |
||
43 | |||
44 | 22 | if (!file_exists($keyPath) || !is_readable($keyPath)) { |
|
45 | throw new \LogicException(sprintf('Key path "%s" does not exist or is not readable', $keyPath)); |
||
46 | } |
||
47 | |||
48 | 22 | if ($keyPermissionsCheck === true) { |
|
49 | // Verify the permissions of the key |
||
50 | 22 | $keyPathPerms = decoct(fileperms($keyPath) & 0777); |
|
51 | 22 | if (in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) { |
|
52 | trigger_error(sprintf( |
||
53 | 'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s', |
||
54 | $keyPath, |
||
55 | $keyPathPerms |
||
56 | ), E_USER_NOTICE); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | 22 | $this->keyPath = $keyPath; |
|
61 | 22 | $this->passPhrase = $passPhrase; |
|
62 | 22 | } |
|
63 | |||
64 | /** |
||
65 | * @param string $key |
||
66 | * |
||
67 | * @throws \RuntimeException |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | private function saveKeyToFile($key) |
||
72 | { |
||
73 | $tmpDir = sys_get_temp_dir(); |
||
74 | $keyPath = $tmpDir . '/' . sha1($key) . '.key'; |
||
75 | |||
76 | if (file_exists($keyPath)) { |
||
77 | return 'file://' . $keyPath; |
||
78 | } |
||
79 | |||
80 | if (!touch($keyPath)) { |
||
81 | // @codeCoverageIgnoreStart |
||
82 | throw new \RuntimeException(sprintf('"%s" key file could not be created', $keyPath)); |
||
83 | // @codeCoverageIgnoreEnd |
||
84 | } |
||
85 | |||
86 | if (file_put_contents($keyPath, $key) === false) { |
||
87 | // @codeCoverageIgnoreStart |
||
88 | throw new \RuntimeException(sprintf('Unable to write key file to temporary directory "%s"', $tmpDir)); |
||
89 | // @codeCoverageIgnoreEnd |
||
90 | } |
||
91 | |||
92 | if (chmod($keyPath, 0600) === false) { |
||
93 | // @codeCoverageIgnoreStart |
||
94 | throw new \RuntimeException(sprintf('The key file "%s" file mode could not be changed with chmod to 600', $keyPath)); |
||
95 | // @codeCoverageIgnoreEnd |
||
96 | } |
||
97 | |||
98 | return 'file://' . $keyPath; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Retrieve key path. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 3 | public function getKeyPath() |
|
110 | |||
111 | /** |
||
112 | * Retrieve key pass phrase. |
||
113 | * |
||
114 | * @return null|string |
||
115 | */ |
||
116 | 2 | public function getPassPhrase() |
|
120 | } |
||
121 |