1 | <?php |
||
21 | final class KeyConverter |
||
22 | { |
||
23 | /** |
||
24 | * @param string $file |
||
25 | * |
||
26 | * @throws \InvalidArgumentException |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function loadKeyFromCertificateFile($file) |
||
37 | |||
38 | /** |
||
39 | * @param string $certificate |
||
40 | * |
||
41 | * @throws \InvalidArgumentException |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public static function loadKeyFromCertificate($certificate) |
||
60 | |||
61 | /** |
||
62 | * @param resource $res |
||
63 | * |
||
64 | * @throws \Exception |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public static function loadKeyFromX509Resource($res) |
||
91 | |||
92 | /** |
||
93 | * @param string $file |
||
94 | * @param null|string $password |
||
95 | * |
||
96 | * @throws \Exception |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public static function loadFromKeyFile($file, $password = null) |
||
101 | { |
||
102 | $content = file_get_contents($file); |
||
103 | |||
104 | return self::loadFromKey($content, $password); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $key |
||
109 | * @param null|string $password |
||
110 | * |
||
111 | * @throws \Exception |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | public static function loadFromKey($key, $password = null) |
||
116 | { |
||
117 | try { |
||
118 | return self::loadKeyFromDER($key, $password); |
||
119 | } catch (\Exception $e) { |
||
120 | return self::loadKeyFromPEM($key, $password); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $der |
||
126 | * @param null|string $password |
||
127 | * |
||
128 | * @throws \Exception |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | private static function loadKeyFromDER($der, $password = null) |
||
133 | { |
||
134 | $pem = self::convertDerToPem($der); |
||
135 | |||
136 | return self::loadKeyFromPEM($pem, $password); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $pem |
||
141 | * @param null|string $password |
||
142 | * |
||
143 | * @throws \Exception |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | private static function loadKeyFromPEM($pem, $password = null) |
||
148 | { |
||
149 | if (preg_match('#DEK-Info: (.+),(.+)#', $pem, $matches)) { |
||
150 | $pem = self::decodePem($pem, $matches, $password); |
||
151 | } |
||
152 | |||
153 | $res = openssl_pkey_get_private($pem); |
||
154 | if ($res === false) { |
||
155 | $res = openssl_pkey_get_public($pem); |
||
156 | } |
||
157 | Assertion::false($res === false, 'Unable to load the key'); |
||
158 | |||
159 | $details = openssl_pkey_get_details($res); |
||
160 | Assertion::isArray($details, 'Unable to get details of the key'); |
||
161 | Assertion::keyExists($details, 'type', 'Unable to get details of the key'); |
||
162 | |||
163 | switch ($details['type']) { |
||
164 | case OPENSSL_KEYTYPE_EC: |
||
165 | $ec_key = new ECKey($pem); |
||
166 | |||
167 | return $ec_key->toArray(); |
||
168 | case OPENSSL_KEYTYPE_RSA: |
||
169 | $rsa_key = new RSAKey($pem); |
||
170 | |||
171 | return $rsa_key->toArray(); |
||
172 | default: |
||
173 | throw new \InvalidArgumentException('Unsupported key type'); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param array $data |
||
179 | * |
||
180 | * @throws \Exception |
||
181 | * |
||
182 | * @return \phpseclib\Crypt\RSA |
||
183 | */ |
||
184 | public static function fromArrayToRSACrypt(array $data) |
||
192 | |||
193 | /** |
||
194 | * @param array $x5c |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | public static function loadFromX5C(array $x5c) |
||
241 | |||
242 | /** |
||
243 | * @param array $data |
||
244 | * |
||
245 | * @throws \Exception |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public static function fromArrayToXML(array $data) |
||
250 | { |
||
251 | $result = "<RSAKeyPair>\n"; |
||
252 | foreach ($data as $key => $value) { |
||
253 | $element = self::getElement($key); |
||
254 | $value = strtr($value, '-_', '+/'); |
||
255 | |||
256 | switch (mb_strlen($value, '8bit') % 4) { |
||
257 | case 0: |
||
258 | break; // No pad chars in this case |
||
259 | case 2: |
||
260 | $value .= '=='; |
||
261 | break; // Two pad chars |
||
262 | case 3: |
||
263 | $value .= '='; |
||
264 | break; // One pad char |
||
265 | default: |
||
266 | throw new \Exception('Invalid data'); |
||
267 | } |
||
268 | |||
269 | $result .= "\t<$element>$value</$element>\n"; |
||
270 | } |
||
271 | $result .= '</RSAKeyPair>'; |
||
272 | |||
273 | return $result; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param $key |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | private static function getElement($key) |
||
282 | { |
||
283 | $values = [ |
||
284 | 'n' => 'Modulus', |
||
285 | 'e' => 'Exponent', |
||
286 | 'p' => 'P', |
||
287 | 'd' => 'D', |
||
288 | 'q' => 'Q', |
||
289 | 'dp' => 'DP', |
||
290 | 'dq' => 'DQ', |
||
291 | 'qi' => 'InverseQ', |
||
292 | ]; |
||
293 | if (array_key_exists($key, $values)) { |
||
294 | return $values[$key]; |
||
295 | } else { |
||
296 | throw new \InvalidArgumentException('Unsupported key data'); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param string $pem |
||
302 | * @param string[] $matches |
||
303 | * @param null|string $password |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | private static function decodePem($pem, array $matches, $password = null) |
||
329 | |||
330 | /** |
||
331 | * @param string $der_data |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | private static function convertDerToPem($der_data) |
||
342 | |||
343 | /** |
||
344 | * @param string $pem |
||
345 | * @param string $algorithm |
||
346 | * @param bool $binary |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | private static function calculateX509Fingerprint($pem, $algorithm, $binary = false) |
||
357 | } |
||
358 |