KeyConverter::loadKeyFromDER()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\KeyManagement\KeyConverter;
15
16
use Base64Url\Base64Url;
17
18
/**
19
 * 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.
20
 */
21
final class KeyConverter
22
{
23
    /**
24
     * @param string $file
25
     *
26
     * @throws \InvalidArgumentException
27
     *
28
     * @return array
29
     */
30
    public static function loadKeyFromCertificateFile(string $file): array
31
    {
32
        if (!file_exists($file)) {
33
            throw new \InvalidArgumentException(sprintf('File "%s" does not exist.', $file));
34
        }
35
        $content = file_get_contents($file);
36
37
        return self::loadKeyFromCertificate($content);
38
    }
39
40
    /**
41
     * @param string $certificate
42
     *
43
     * @throws \InvalidArgumentException
44
     *
45
     * @return array
46
     */
47
    public static function loadKeyFromCertificate(string $certificate): array
48
    {
49
        try {
50
            $res = openssl_x509_read($certificate);
51
        } catch (\Exception $e) {
52
            $certificate = self::convertDerToPem($certificate);
53
            $res = openssl_x509_read($certificate);
54
        }
55
        if (false === $res) {
56
            throw new \InvalidArgumentException('Unable to load the certificate.');
57
        }
58
59
        $values = self::loadKeyFromX509Resource($res);
60
        openssl_x509_free($res);
61
62
        return $values;
63
    }
64
65
    /**
66
     * @param resource $res
67
     *
68
     * @throws \Exception
69
     *
70
     * @return array
71
     */
72
    public static function loadKeyFromX509Resource($res): array
73
    {
74
        $key = openssl_get_publickey($res);
75
76
        $details = openssl_pkey_get_details($key);
77
        if (isset($details['key'])) {
78
            $values = self::loadKeyFromPEM($details['key']);
79
            openssl_x509_export($res, $out);
80
            $values['x5c'] = [trim(preg_replace('#-.*-#', '', $out))];
81
82
            if (function_exists('openssl_x509_fingerprint')) {
83
                $values['x5t'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha1', true));
84
                $values['x5t#256'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha256', true));
85
            } else {
86
                openssl_x509_export($res, $pem);
87
                $values['x5t'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha1', true));
88
                $values['x5t#256'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha256', true));
89
            }
90
91
            return $values;
92
        }
93
94
        throw new \InvalidArgumentException('Unable to load the certificate');
95
    }
96
97
    /**
98
     * @param string      $file
99
     * @param null|string $password
100
     *
101
     * @throws \Exception
102
     *
103
     * @return array
104
     */
105
    public static function loadFromKeyFile(string $file, ?string $password = null): array
106
    {
107
        $content = file_get_contents($file);
108
109
        return self::loadFromKey($content, $password);
110
    }
111
112
    /**
113
     * @param string      $key
114
     * @param null|string $password
115
     *
116
     * @throws \Exception
117
     *
118
     * @return array
119
     */
120
    public static function loadFromKey(string $key, ?string $password = null): array
121
    {
122
        try {
123
            return self::loadKeyFromDER($key, $password);
124
        } catch (\Exception $e) {
125
            return self::loadKeyFromPEM($key, $password);
126
        }
127
    }
128
129
    /**
130
     * @param string      $der
131
     * @param null|string $password
132
     *
133
     * @throws \Exception
134
     *
135
     * @return array
136
     */
137
    private static function loadKeyFromDER(string $der, ?string $password = null): array
138
    {
139
        $pem = self::convertDerToPem($der);
140
141
        return self::loadKeyFromPEM($pem, $password);
142
    }
143
144
    /**
145
     * @param string      $pem
146
     * @param null|string $password
147
     *
148
     * @throws \Exception
149
     *
150
     * @return array
151
     */
152
    private static function loadKeyFromPEM(string $pem, ?string $password = null): array
153
    {
154
        if (preg_match('#DEK-Info: (.+),(.+)#', $pem, $matches)) {
155
            $pem = self::decodePem($pem, $matches, $password);
156
        }
157
158
        self::sanitizePEM($pem);
159
160
        $res = openssl_pkey_get_private($pem);
161
        if ($res === false) {
162
            $res = openssl_pkey_get_public($pem);
163
        }
164
        if (false === $res) {
165
            throw new \InvalidArgumentException('Unable to load the key.');
166
        }
167
168
        $details = openssl_pkey_get_details($res);
169
        if (!is_array($details) || !array_key_exists('type', $details)) {
170
            throw new \InvalidArgumentException('Unable to get details of the key');
171
        }
172
173
        switch ($details['type']) {
174
            case OPENSSL_KEYTYPE_EC:
175
                $ec_key = ECKey::createFromPEM($pem);
176
177
                return $ec_key->toArray();
178
            case OPENSSL_KEYTYPE_RSA:
179
                 $rsa_key = RSAKey::createFromPEM($pem);
180
                $rsa_key->optimize();
181
182
                 return $rsa_key->toArray();
183
            default:
184
                throw new \InvalidArgumentException('Unsupported key type');
185
        }
186
    }
187
188
    /**
189
     * This method modifies the PEM to get 64 char lines and fix bug with old OpenSSL versions.
190
     *
191
     * @param string $pem
192
     */
193
    private static function sanitizePEM(string &$pem)
194
    {
195
        preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER);
196
        $ciphertext = preg_replace('#-.*-|\r|\n| #', '', $pem);
197
198
        $pem = $matches[0][0].PHP_EOL;
199
        $pem .= chunk_split($ciphertext, 64, PHP_EOL);
200
        $pem .= $matches[0][1].PHP_EOL;
201
    }
202
203
    /**
204
     * @param array $x5c
205
     *
206
     * @return array
207
     */
208
    public static function loadFromX5C(array $x5c): array
209
    {
210
        $certificate = null;
211
        $last_issuer = null;
212
        $last_subject = null;
213
        foreach ($x5c as $cert) {
214
            $current_cert = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$cert.PHP_EOL.'-----END CERTIFICATE-----';
215
            $x509 = openssl_x509_read($current_cert);
216
            if (false === $x509) {
217
                $last_issuer = null;
218
                $last_subject = null;
219
220
                break;
221
            }
222
            $parsed = openssl_x509_parse($x509);
223
224
            openssl_x509_free($x509);
225
            if (false === $parsed) {
226
                $last_issuer = null;
227
                $last_subject = null;
228
229
                break;
230
            }
231
            if (null === $last_subject) {
232
                $last_subject = $parsed['subject'];
233
                $last_issuer = $parsed['issuer'];
234
                $certificate = $current_cert;
235
            } else {
236
                if (json_encode($last_issuer) === json_encode($parsed['subject'])) {
237
                    $last_subject = $parsed['subject'];
238
                    $last_issuer = $parsed['issuer'];
239
                } else {
240
                    $last_issuer = null;
241
                    $last_subject = null;
242
243
                    break;
244
                }
245
            }
246
        }
247
        if (null !== $last_issuer && json_encode($last_issuer) !== json_encode($last_subject)) {
248
            throw new \InvalidArgumentException('Invalid certificate chain.');
249
        }
250
251
        return self::loadKeyFromCertificate($certificate);
252
    }
253
254
    /**
255
     * @param string      $pem
256
     * @param string[]    $matches
257
     * @param null|string $password
258
     *
259
     * @return string
260
     */
261
    private static function decodePem(string $pem, array $matches, ?string $password = null): string
262
    {
263
        if (null === $password) {
264
            throw new \InvalidArgumentException('Password required for encrypted keys.');
265
        }
266
267
        $iv = pack('H*', trim($matches[2]));
268
        $iv_sub = mb_substr($iv, 0, 8, '8bit');
269
        $symkey = pack('H*', md5($password.$iv_sub));
270
        $symkey .= pack('H*', md5($symkey.$password.$iv_sub));
271
        $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $pem);
272
        $ciphertext = base64_decode(preg_replace('#-.*-|\r|\n#', '', $key));
273
274
        $decoded = openssl_decrypt($ciphertext, strtolower($matches[1]), $symkey, OPENSSL_RAW_DATA, $iv);
275
276
        $number = preg_match_all('#-{5}.*-{5}#', $pem, $result);
277
        if (2 !== $number) {
278
            throw new \InvalidArgumentException('Unable to load the key');
279
        }
280
281
        $pem = $result[0][0].PHP_EOL;
282
        $pem .= chunk_split(base64_encode($decoded), 64);
283
        $pem .= $result[0][1].PHP_EOL;
284
285
        return $pem;
286
    }
287
288
    /**
289
     * @param string $der_data
290
     *
291
     * @return string
292
     */
293
    private static function convertDerToPem(string $der_data): string
294
    {
295
        $pem = chunk_split(base64_encode($der_data), 64, PHP_EOL);
296
        $pem = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$pem.'-----END CERTIFICATE-----'.PHP_EOL;
297
298
        return $pem;
299
    }
300
301
    /**
302
     * @param string $pem
303
     * @param string $algorithm
304
     * @param bool   $binary
305
     *
306
     * @return string
307
     */
308
    private static function calculateX509Fingerprint(string $pem, string $algorithm, bool $binary = false): string
309
    {
310
        $pem = preg_replace('#-.*-|\r|\n#', '', $pem);
311
        $bin = base64_decode($pem);
312
313
        return hash($algorithm, $bin, $binary);
314
    }
315
}
316