1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\KeyManagement\KeyConverter;
use Base64Url\Base64Url;
/**
* @internal
*/
class KeyConverter
{
/**
* @throws \InvalidArgumentException
*/
public static function loadKeyFromCertificateFile(string $file): array
{
if (!\file_exists($file)) {
throw new \InvalidArgumentException(\sprintf('File "%s" does not exist.', $file));
}
$content = \file_get_contents($file);
return self::loadKeyFromCertificate($content);
}
/**
* @throws \InvalidArgumentException
*/
public static function loadKeyFromCertificate(string $certificate): array
{
try {
$res = \openssl_x509_read($certificate);
} catch (\Exception $e) {
$certificate = self::convertDerToPem($certificate);
$res = \openssl_x509_read($certificate);
}
if (false === $res) {
throw new \InvalidArgumentException('Unable to load the certificate.');
}
$values = self::loadKeyFromX509Resource($res);
\openssl_x509_free($res);
return $values;
}
/**
* @param resource $res
*
* @throws \Exception
*/
public static function loadKeyFromX509Resource($res): array
{
$key = \openssl_get_publickey($res);
$details = \openssl_pkey_get_details($key);
if (isset($details['key'])) {
$values = self::loadKeyFromPEM($details['key']);
\openssl_x509_export($res, $out);
$x5c = \preg_replace('#-.*-#', '', $out);
$x5c = \preg_replace('~\R~', PHP_EOL, $x5c);
$x5c = \trim($x5c);
$values['x5c'] = [$x5c];
$values['x5t'] = Base64Url::encode(\openssl_x509_fingerprint($res, 'sha1', true));
$values['x5t#256'] = Base64Url::encode(\openssl_x509_fingerprint($res, 'sha256', true));
return $values;
}
throw new \InvalidArgumentException('Unable to load the certificate');
}
/**
* @throws \Exception
*/
public static function loadFromKeyFile(string $file, ?string $password = null): array
{
$content = \file_get_contents($file);
return self::loadFromKey($content, $password);
}
/**
* @throws \Exception
*/
public static function loadFromKey(string $key, ?string $password = null): array
{
try {
return self::loadKeyFromDER($key, $password);
} catch (\Exception $e) {
return self::loadKeyFromPEM($key, $password);
}
}
/**
* @throws \Exception
*/
private static function loadKeyFromDER(string $der, ?string $password = null): array
{
$pem = self::convertDerToPem($der);
return self::loadKeyFromPEM($pem, $password);
}
/**
* @throws \Exception
*/
private static function loadKeyFromPEM(string $pem, ?string $password = null): array
{
if (\preg_match('#DEK-Info: (.+),(.+)#', $pem, $matches)) {
$pem = self::decodePem($pem, $matches, $password);
}
self::sanitizePEM($pem);
$res = \openssl_pkey_get_private($pem);
if (false === $res) {
$res = \openssl_pkey_get_public($pem);
}
if (false === $res) {
throw new \InvalidArgumentException('Unable to load the key.');
}
$details = \openssl_pkey_get_details($res);
if (!\is_array($details) || !\array_key_exists('type', $details)) {
throw new \InvalidArgumentException('Unable to get details of the key');
}
switch ($details['type']) {
case OPENSSL_KEYTYPE_EC:
$ec_key = ECKey::createFromPEM($pem);
return $ec_key->toArray();
case OPENSSL_KEYTYPE_RSA:
$rsa_key = RSAKey::createFromPEM($pem);
$rsa_key->optimize();
return $rsa_key->toArray();
default:
throw new \InvalidArgumentException('Unsupported key type');
}
}
/**
* This method modifies the PEM to get 64 char lines and fix bug with old OpenSSL versions.
*/
private static function sanitizePEM(string &$pem)
{
\preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER);
$ciphertext = \preg_replace('#-.*-|\r|\n| #', '', $pem);
$pem = $matches[0][0].PHP_EOL;
$pem .= \chunk_split($ciphertext, 64, PHP_EOL);
$pem .= $matches[0][1].PHP_EOL;
}
/**
* Be careful! The certificate chain is loaded, but it is NOT VERIFIED by any mean!
* It is mandatory to verify the root CA or intermediate CA are trusted.
* If not done, it may lead to potential security issues.
*/
public static function loadFromX5C(array $x5c): array
{
$certificate = null;
$last_issuer = null;
$last_subject = null;
foreach ($x5c as $cert) {
$current_cert = '-----BEGIN CERTIFICATE-----'.PHP_EOL.\chunk_split($cert,64,PHP_EOL).'-----END CERTIFICATE-----';
$x509 = \openssl_x509_read($current_cert);
if (false === $x509) {
$last_issuer = null;
$last_subject = null;
break;
}
$parsed = \openssl_x509_parse($x509);
\openssl_x509_free($x509);
if (false === $parsed) {
$last_issuer = null;
$last_subject = null;
break;
}
if (null === $last_subject) {
$last_subject = $parsed['subject'];
$last_issuer = $parsed['issuer'];
$certificate = $current_cert;
} else {
if (\json_encode($last_issuer) === \json_encode($parsed['subject'])) {
$last_subject = $parsed['subject'];
$last_issuer = $parsed['issuer'];
} else {
$last_issuer = null;
$last_subject = null;
break;
}
}
}
return self::loadKeyFromCertificate($certificate);
}
/**
* @param string[] $matches
*/
private static function decodePem(string $pem, array $matches, ?string $password = null): string
{
if (null === $password) {
throw new \InvalidArgumentException('Password required for encrypted keys.');
}
$iv = \pack('H*', \trim($matches[2]));
$iv_sub = \mb_substr($iv, 0, 8, '8bit');
$symkey = \pack('H*', \md5($password.$iv_sub));
$symkey .= \pack('H*', \md5($symkey.$password.$iv_sub));
$key = \preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $pem);
$ciphertext = \base64_decode(\preg_replace('#-.*-|\r|\n#', '', $key), true);
$decoded = \openssl_decrypt($ciphertext, \mb_strtolower($matches[1]), $symkey, OPENSSL_RAW_DATA, $iv);
if (!\is_string($decoded)) {
throw new \InvalidArgumentException('Incorrect password. Key decryption failed.');
}
$number = \preg_match_all('#-{5}.*-{5}#', $pem, $result);
if (2 !== $number) {
throw new \InvalidArgumentException('Unable to load the key');
}
$pem = $result[0][0].PHP_EOL;
$pem .= \chunk_split(\base64_encode($decoded), 64);
$pem .= $result[0][1].PHP_EOL;
return $pem;
}
private static function convertDerToPem(string $der_data): string
{
$pem = \chunk_split(\base64_encode($der_data), 64, PHP_EOL);
$pem = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$pem.'-----END CERTIFICATE-----'.PHP_EOL;
return $pem;
}
}
|