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) |
||
90 | |||
91 | /** |
||
92 | * @param string $file |
||
93 | * @param null|string $password |
||
94 | * |
||
95 | * @throws \Exception |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public static function loadFromKeyFile($file, $password = null) |
||
105 | |||
106 | /** |
||
107 | * @param string $key |
||
108 | * @param null|string $password |
||
109 | * |
||
110 | * @throws \Exception |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | public static function loadFromKey($key, $password = null) |
||
122 | |||
123 | /** |
||
124 | * @param string $der |
||
125 | * @param null|string $password |
||
126 | * |
||
127 | * @throws \Exception |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | private static function loadKeyFromDER($der, $password = null) |
||
137 | |||
138 | /** |
||
139 | * @param string $pem |
||
140 | * @param null|string $password |
||
141 | * |
||
142 | * @throws \Exception |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | private static function loadKeyFromPEM($pem, $password = null) |
||
176 | |||
177 | /** |
||
178 | * @param array $x5c |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public static function loadFromX5C(array $x5c) |
||
225 | |||
226 | /** |
||
227 | * @param string $pem |
||
228 | * @param string[] $matches |
||
229 | * @param null|string $password |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | private static function decodePem($pem, array $matches, $password = null) |
||
255 | |||
256 | /** |
||
257 | * @param string $der_data |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | private static function convertDerToPem($der_data) |
||
268 | |||
269 | /** |
||
270 | * @param string $pem |
||
271 | * @param string $algorithm |
||
272 | * @param bool $binary |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | private static function calculateX509Fingerprint($pem, $algorithm, $binary = false) |
||
283 | } |
||
284 |