1 | <?php |
||
20 | final class KeyConverter |
||
21 | { |
||
22 | /** |
||
23 | * @param string $file |
||
24 | * |
||
25 | * @throws \InvalidArgumentException |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | public static function loadKeyFromCertificateFile($file) |
||
30 | { |
||
31 | Assertion::true(file_exists($file), sprintf('File "%s" does not exist.', $file)); |
||
32 | $content = file_get_contents($file); |
||
33 | |||
34 | return self::loadKeyFromCertificate($content); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param string $certificate |
||
39 | * |
||
40 | * @throws \InvalidArgumentException |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | public static function loadKeyFromCertificate($certificate) |
||
45 | { |
||
46 | try { |
||
47 | $res = openssl_x509_read($certificate); |
||
48 | } catch (\Exception $e) { |
||
49 | $certificate = self::convertDerToPem($certificate); |
||
50 | $res = openssl_x509_read($certificate); |
||
51 | } |
||
52 | Assertion::false(false === $res, 'Unable to load the certificate'); |
||
53 | |||
54 | $values = self::loadKeyFromX509Resource($res); |
||
55 | openssl_x509_free($res); |
||
56 | |||
57 | return $values; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param resource $res |
||
62 | * |
||
63 | * @throws \Exception |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public static function loadKeyFromX509Resource($res) |
||
68 | { |
||
69 | $key = openssl_get_publickey($res); |
||
70 | |||
71 | $details = openssl_pkey_get_details($key); |
||
72 | if (isset($details['key'])) { |
||
73 | $values = self::loadKeyFromPEM($details['key']); |
||
74 | openssl_x509_export($res, $out); |
||
75 | $values['x5c'] = [trim(preg_replace('#-.*-#', '', $out))]; |
||
76 | |||
77 | if (function_exists('openssl_x509_fingerprint')) { |
||
78 | $values['x5t'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha1', true)); |
||
79 | $values['x5t#256'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha256', true)); |
||
80 | } else { |
||
81 | openssl_x509_export($res, $pem); |
||
82 | $values['x5t'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha1', true)); |
||
83 | $values['x5t#256'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha256', true)); |
||
84 | } |
||
85 | |||
86 | return $values; |
||
87 | } |
||
88 | |||
89 | throw new \InvalidArgumentException('Unable to load the certificate'); |
||
90 | } |
||
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) |
||
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) |
||
178 | |||
179 | /** |
||
180 | * This method modify the PEM to get 64 char lines and fix bug with old OpenSSL versions. |
||
181 | * |
||
182 | * @param string $pem |
||
183 | */ |
||
184 | private static function sanitizePEM(&$pem) |
||
193 | |||
194 | /** |
||
195 | * @param array $x5c |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | public static function loadFromX5C(array $x5c) |
||
245 | |||
246 | /** |
||
247 | * @param string $pem |
||
248 | * @param string[] $matches |
||
249 | * @param null|string $password |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | private static function decodePem($pem, array $matches, $password = null) |
||
275 | |||
276 | /** |
||
277 | * @param string $der_data |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | private static function convertDerToPem($der_data) |
||
288 | |||
289 | /** |
||
290 | * @param string $pem |
||
291 | * @param string $algorithm |
||
292 | * @param bool $binary |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | private static function calculateX509Fingerprint($pem, $algorithm, $binary = false) |
||
303 | } |
||
304 |