|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\KeyConverter; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
use Base64Url\Base64Url; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This class will help you to load an EC key or a RSA key/certificate (private or public) and get values to create a JWK object. |
|
19
|
|
|
*/ |
|
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
|
|
|
throw new \InvalidArgumentException('Unable to load the certificate'); |
|
89
|
|
|
} |
|
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) |
|
100
|
|
|
{ |
|
101
|
|
|
$content = file_get_contents($file); |
|
102
|
|
|
|
|
103
|
|
|
return self::loadFromKey($content, $password); |
|
104
|
|
|
} |
|
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) |
|
115
|
|
|
{ |
|
116
|
|
|
try { |
|
117
|
|
|
return self::loadKeyFromDER($key, $password); |
|
118
|
|
|
} catch (\Exception $e) { |
|
119
|
|
|
return self::loadKeyFromPEM($key, $password); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
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) |
|
132
|
|
|
{ |
|
133
|
|
|
$pem = self::convertDerToPem($der); |
|
134
|
|
|
|
|
135
|
|
|
return self::loadKeyFromPEM($pem, $password); |
|
136
|
|
|
} |
|
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) |
|
147
|
|
|
{ |
|
148
|
|
|
if (preg_match('#DEK-Info: (.+),(.+)#', $pem, $matches)) { |
|
149
|
|
|
$pem = self::decodePem($pem, $matches, $password); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
self::sanitizePEM($pem); |
|
153
|
|
|
|
|
154
|
|
|
$res = openssl_pkey_get_private($pem); |
|
155
|
|
|
if ($res === false) { |
|
156
|
|
|
$res = openssl_pkey_get_public($pem); |
|
157
|
|
|
} |
|
158
|
|
|
Assertion::false($res === false, 'Unable to load the key'); |
|
159
|
|
|
|
|
160
|
|
|
$details = openssl_pkey_get_details($res); |
|
161
|
|
|
Assertion::isArray($details, 'Unable to get details of the key'); |
|
162
|
|
|
Assertion::keyExists($details, 'type', 'Unable to get details of the key'); |
|
163
|
|
|
|
|
164
|
|
|
switch ($details['type']) { |
|
165
|
|
|
case OPENSSL_KEYTYPE_EC: |
|
166
|
|
|
$ec_key = new ECKey($pem); |
|
167
|
|
|
|
|
168
|
|
|
return $ec_key->toArray(); |
|
169
|
|
|
case OPENSSL_KEYTYPE_RSA: |
|
170
|
|
|
$rsa_key = new RSAKey($pem); |
|
171
|
|
|
|
|
172
|
|
|
return $rsa_key->toArray(); |
|
173
|
|
|
default: |
|
174
|
|
|
throw new \InvalidArgumentException('Unsupported key type'); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* This method modify the PEM to get 64 char lines and fix bug with old OpenSSL versions |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $pem |
|
182
|
|
|
*/ |
|
183
|
|
|
private static function sanitizePEM(&$pem) |
|
184
|
|
|
{ |
|
185
|
|
|
preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER); |
|
186
|
|
|
$ciphertext = preg_replace('#-.*-|\r|\n| #', '', $pem); |
|
187
|
|
|
|
|
188
|
|
|
$pem = $matches[0][0].PHP_EOL; |
|
189
|
|
|
$pem .= chunk_split($ciphertext, 64, PHP_EOL); |
|
190
|
|
|
$pem .= $matches[0][1].PHP_EOL; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param array $x5c |
|
195
|
|
|
* |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function loadFromX5C(array $x5c) |
|
199
|
|
|
{ |
|
200
|
|
|
$certificate = null; |
|
201
|
|
|
$last_issuer = null; |
|
202
|
|
|
$last_subject = null; |
|
203
|
|
|
foreach ($x5c as $cert) { |
|
204
|
|
|
$current_cert = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$cert.PHP_EOL.'-----END CERTIFICATE-----'; |
|
205
|
|
|
$x509 = openssl_x509_read($current_cert); |
|
206
|
|
|
if (false === $x509) { |
|
207
|
|
|
$last_issuer = null; |
|
208
|
|
|
$last_subject = null; |
|
209
|
|
|
break; |
|
210
|
|
|
} |
|
211
|
|
|
$parsed = openssl_x509_parse($x509); |
|
212
|
|
|
|
|
213
|
|
|
openssl_x509_free($x509); |
|
214
|
|
|
if (false === $parsed) { |
|
215
|
|
|
$last_issuer = null; |
|
216
|
|
|
$last_subject = null; |
|
217
|
|
|
break; |
|
218
|
|
|
} |
|
219
|
|
|
if (null === $last_subject) { |
|
220
|
|
|
$last_subject = $parsed['subject']; |
|
221
|
|
|
$last_issuer = $parsed['issuer']; |
|
222
|
|
|
$certificate = $current_cert; |
|
223
|
|
|
} else { |
|
224
|
|
|
if (json_encode($last_issuer) === json_encode($parsed['subject'])) { |
|
225
|
|
|
$last_subject = $parsed['subject']; |
|
226
|
|
|
$last_issuer = $parsed['issuer']; |
|
227
|
|
|
} else { |
|
228
|
|
|
$last_issuer = null; |
|
229
|
|
|
$last_subject = null; |
|
230
|
|
|
break; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
Assertion::false( |
|
235
|
|
|
null === $last_issuer || json_encode($last_issuer) !== json_encode($last_subject), |
|
236
|
|
|
'Invalid certificate chain.' |
|
237
|
|
|
); |
|
238
|
|
|
|
|
239
|
|
|
return self::loadKeyFromCertificate($certificate); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param string $pem |
|
244
|
|
|
* @param string[] $matches |
|
245
|
|
|
* @param null|string $password |
|
246
|
|
|
* |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
private static function decodePem($pem, array $matches, $password = null) |
|
250
|
|
|
{ |
|
251
|
|
|
Assertion::notNull($password, 'Password required for encrypted keys.'); |
|
252
|
|
|
|
|
253
|
|
|
$iv = pack('H*', trim($matches[2])); |
|
254
|
|
|
$iv_sub = mb_substr($iv, 0, 8, '8bit'); |
|
255
|
|
|
$symkey = pack('H*', md5($password.$iv_sub)); |
|
256
|
|
|
$symkey .= pack('H*', md5($symkey.$password.$iv_sub)); |
|
257
|
|
|
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $pem); |
|
258
|
|
|
$ciphertext = base64_decode(preg_replace('#-.*-|\r|\n#', '', $key)); |
|
259
|
|
|
|
|
260
|
|
|
$decoded = openssl_decrypt($ciphertext, strtolower($matches[1]), $symkey, true, $iv); |
|
261
|
|
|
|
|
262
|
|
|
$number = preg_match_all('#-{5}.*-{5}#', $pem, $result); |
|
263
|
|
|
Assertion::eq($number, 2, 'Unable to load the key'); |
|
264
|
|
|
|
|
265
|
|
|
$pem = $result[0][0].PHP_EOL; |
|
266
|
|
|
$pem .= chunk_split(base64_encode($decoded), 64); |
|
267
|
|
|
$pem .= $result[0][1].PHP_EOL; |
|
268
|
|
|
|
|
269
|
|
|
return $pem; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param string $der_data |
|
274
|
|
|
* |
|
275
|
|
|
* @return string |
|
276
|
|
|
*/ |
|
277
|
|
|
private static function convertDerToPem($der_data) |
|
278
|
|
|
{ |
|
279
|
|
|
$pem = chunk_split(base64_encode($der_data), 64, PHP_EOL); |
|
280
|
|
|
$pem = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$pem.'-----END CERTIFICATE-----'.PHP_EOL; |
|
281
|
|
|
|
|
282
|
|
|
return $pem; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $pem |
|
287
|
|
|
* @param string $algorithm |
|
288
|
|
|
* @param bool $binary |
|
289
|
|
|
* |
|
290
|
|
|
* @return string |
|
291
|
|
|
*/ |
|
292
|
|
|
private static function calculateX509Fingerprint($pem, $algorithm, $binary = false) |
|
293
|
|
|
{ |
|
294
|
|
|
$pem = preg_replace('#-.*-|\r|\n#', '', $pem); |
|
295
|
|
|
$bin = base64_decode($pem); |
|
296
|
|
|
|
|
297
|
|
|
return hash($algorithm, $bin, $binary); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|