Failed Conditions
Push — v7 ( 838dcb )
by Florent
04:30
created

KeyConverter::loadKeyFromX509Resource()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
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(string $file): array
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(string $certificate): array
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): array
68
    {
69
        Assertion::true(is_resource($res));
70
        $key = openssl_get_publickey($res);
71
72
        $details = openssl_pkey_get_details($key);
73
        if (isset($details['key'])) {
74
            $values = self::loadKeyFromPEM($details['key']);
75
            openssl_x509_export($res, $out);
76
            $values['x5c'] = [trim(preg_replace('#-.*-#', '', $out))];
77
78
            if (function_exists('openssl_x509_fingerprint')) {
79
                $values['x5t'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha1', true));
80
                $values['x5t#256'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha256', true));
81
            } else {
82
                openssl_x509_export($res, $pem);
83
                $values['x5t'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha1', true));
84
                $values['x5t#256'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha256', true));
85
            }
86
87
            return $values;
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(string $file, ?string $password = null): array
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(string $key, ?string $password = null): array
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(string $der, ?string $password = null): array
133
    {
134
        $pem = self::convertDerToPem($der);
135
136
        return self::loadKeyFromPEM($pem, $password);
137
    }
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(string $pem, ?string $password = null): array
148
    {
149
        if (preg_match('#DEK-Info: (.+),(.+)#', $pem, $matches)) {
150
            $pem = self::decodePem($pem, $matches, $password);
151
        }
152
153
        self::sanitizePEM($pem);
154
155
        $res = openssl_pkey_get_private($pem);
156
        if ($res === false) {
157
            $res = openssl_pkey_get_public($pem);
158
        }
159
        Assertion::false($res === false, 'Unable to load the key');
160
161
        $details = openssl_pkey_get_details($res);
162
        Assertion::isArray($details, 'Unable to get details of the key');
163
        Assertion::keyExists($details, 'type', 'Unable to get details of the key');
164
165
        switch ($details['type']) {
166
            case OPENSSL_KEYTYPE_EC:
167
                $ec_key = new ECKey($pem);
168
169
                return $ec_key->toArray();
170
            case OPENSSL_KEYTYPE_RSA:
171
                 $rsa_key = new RSAKey($pem);
172
173
                 return $rsa_key->toArray();
174
            default:
175
                throw new \InvalidArgumentException('Unsupported key type');
176
        }
177
    }
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(string &$pem)
185
    {
186
        preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER);
187
        $ciphertext = preg_replace('#-.*-|\r|\n| #', '', $pem);
188
189
        $pem = $matches[0][0].PHP_EOL;
190
        $pem .= chunk_split($ciphertext, 64, PHP_EOL);
191
        $pem .= $matches[0][1].PHP_EOL;
192
    }
193
194
    /**
195
     * @param array $x5c
196
     *
197
     * @return array
198
     */
199
    public static function loadFromX5C(array $x5c): array
200
    {
201
        $certificate = null;
202
        $last_issuer = null;
203
        $last_subject = null;
204
        foreach ($x5c as $cert) {
205
            $current_cert = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$cert.PHP_EOL.'-----END CERTIFICATE-----';
206
            $x509 = openssl_x509_read($current_cert);
207
            if (false === $x509) {
208
                $last_issuer = null;
209
                $last_subject = null;
210
                break;
211
            }
212
            $parsed = openssl_x509_parse($x509);
213
214
            openssl_x509_free($x509);
215
            if (false === $parsed) {
216
                $last_issuer = null;
217
                $last_subject = null;
218
                break;
219
            }
220
            if (null === $last_subject) {
221
                $last_subject = $parsed['subject'];
222
                $last_issuer = $parsed['issuer'];
223
                $certificate = $current_cert;
224
            } else {
225
                if (json_encode($last_issuer) === json_encode($parsed['subject'])) {
226
                    $last_subject = $parsed['subject'];
227
                    $last_issuer = $parsed['issuer'];
228
                } else {
229
                    $last_issuer = null;
230
                    $last_subject = null;
231
                    break;
232
                }
233
            }
234
        }
235
        Assertion::false(
236
            null === $last_issuer || json_encode($last_issuer) !== json_encode($last_subject),
237
            'Invalid certificate chain.'
238
        );
239
240
        return self::loadKeyFromCertificate($certificate);
241
    }
242
243
    /**
244
     * @param string      $pem
245
     * @param string[]    $matches
246
     * @param null|string $password
247
     *
248
     * @return string
249
     */
250
    private static function decodePem(string $pem, array $matches, ?string $password = null): string
251
    {
252
        Assertion::notNull($password, 'Password required for encrypted keys.');
253
254
        $iv = pack('H*', trim($matches[2]));
255
        $iv_sub = mb_substr($iv, 0, 8, '8bit');
256
        $symkey = pack('H*', md5($password.$iv_sub));
257
        $symkey .= pack('H*', md5($symkey.$password.$iv_sub));
258
        $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $pem);
259
        $ciphertext = base64_decode(preg_replace('#-.*-|\r|\n#', '', $key));
260
261
        $decoded = openssl_decrypt($ciphertext, strtolower($matches[1]), $symkey, true, $iv);
262
263
        $number = preg_match_all('#-{5}.*-{5}#', $pem, $result);
264
        Assertion::eq($number, 2, 'Unable to load the key');
265
266
        $pem = $result[0][0].PHP_EOL;
267
        $pem .= chunk_split(base64_encode($decoded), 64);
268
        $pem .= $result[0][1].PHP_EOL;
269
270
        return $pem;
271
    }
272
273
    /**
274
     * @param string $der_data
275
     *
276
     * @return string
277
     */
278
    private static function convertDerToPem(string $der_data): string
279
    {
280
        $pem = chunk_split(base64_encode($der_data), 64, PHP_EOL);
281
        $pem = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$pem.'-----END CERTIFICATE-----'.PHP_EOL;
282
283
        return $pem;
284
    }
285
286
    /**
287
     * @param string $pem
288
     * @param string $algorithm
289
     * @param bool   $binary
290
     *
291
     * @return string
292
     */
293
    private static function calculateX509Fingerprint(string $pem, string $algorithm, bool $binary = false): string
294
    {
295
        $pem = preg_replace('#-.*-|\r|\n#', '', $pem);
296
        $bin = base64_decode($pem);
297
298
        return hash($algorithm, $bin, $binary);
299
    }
300
}
301