PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.23.3
JetBackup – Backup, Restore & Migrate v3.1.23.3
3.1.23.3 3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / DH / Formats / Keys / PKCS8.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / DH / Formats / Keys Last commit date
.htaccess 1 year ago PKCS1.php 1 year ago PKCS8.php 1 year ago index.html 1 year ago web.config 1 year ago
PKCS8.php
122 lines
1 <?php
2
3 /**
4 * PKCS#8 Formatted DH Key Handler
5 *
6 * PHP version 5
7 *
8 * Processes keys with the following headers:
9 *
10 * -----BEGIN ENCRYPTED PRIVATE KEY-----
11 * -----BEGIN PRIVATE KEY-----
12 * -----BEGIN PUBLIC KEY-----
13 *
14 * @author Jim Wigginton <terrafrost@php.net>
15 * @copyright 2015 Jim Wigginton
16 * @license http://www.opensource.org/licenses/mit-license.html MIT License
17 * @link http://phpseclib.sourceforge.net
18 */
19
20 declare(strict_types=1);
21
22 namespace phpseclib3\Crypt\DH\Formats\Keys;
23
24 use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
25 use phpseclib3\Exception\RuntimeException;
26 use phpseclib3\File\ASN1;
27 use phpseclib3\File\ASN1\Maps;
28 use phpseclib3\Math\BigInteger;
29
30 /**
31 * PKCS#8 Formatted DH Key Handler
32 *
33 * @author Jim Wigginton <terrafrost@php.net>
34 */
35 abstract class PKCS8 extends Progenitor
36 {
37 /**
38 * OID Name
39 *
40 * @var string
41 */
42 public const OID_NAME = 'dhKeyAgreement';
43
44 /**
45 * OID Value
46 *
47 * @var string
48 */
49 public const OID_VALUE = '1.2.840.113549.1.3.1';
50
51 /**
52 * Child OIDs loaded
53 *
54 * @var bool
55 */
56 protected static $childOIDsLoaded = false;
57
58 /**
59 * Break a public or private key down into its constituent components
60 *
61 * @param string|array $key
62 */
63 public static function load($key, ?string $password = null): array
64 {
65 $key = parent::load($key, $password);
66
67 $type = isset($key['privateKey']) ? 'privateKey' : 'publicKey';
68
69 $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element);
70 if (empty($decoded)) {
71 throw new RuntimeException('Unable to decode BER of parameters');
72 }
73 $components = ASN1::asn1map($decoded[0], Maps\DHParameter::MAP);
74 if (!is_array($components)) {
75 throw new RuntimeException('Unable to perform ASN1 mapping on parameters');
76 }
77
78 $decoded = ASN1::decodeBER($key[$type]);
79 switch (true) {
80 case !isset($decoded):
81 case !isset($decoded[0]['content']):
82 case !$decoded[0]['content'] instanceof BigInteger:
83 throw new RuntimeException('Unable to decode BER of parameters');
84 }
85 $components[$type] = $decoded[0]['content'];
86
87 return $components;
88 }
89
90 /**
91 * Convert a private key to the appropriate format.
92 */
93 public static function savePrivateKey(BigInteger $prime, BigInteger $base, BigInteger $privateKey, BigInteger $publicKey, ?string $password = null, array $options = []): string
94 {
95 $params = [
96 'prime' => $prime,
97 'base' => $base,
98 ];
99 $params = ASN1::encodeDER($params, Maps\DHParameter::MAP);
100 $params = new ASN1\Element($params);
101 $key = ASN1::encodeDER($privateKey, ['type' => ASN1::TYPE_INTEGER]);
102 return self::wrapPrivateKey($key, [], $params, $password, null, '', $options);
103 }
104
105 /**
106 * Convert a public key to the appropriate format
107 *
108 * @param array $options optional
109 */
110 public static function savePublicKey(BigInteger $prime, BigInteger $base, BigInteger $publicKey, array $options = []): string
111 {
112 $params = [
113 'prime' => $prime,
114 'base' => $base,
115 ];
116 $params = ASN1::encodeDER($params, Maps\DHParameter::MAP);
117 $params = new ASN1\Element($params);
118 $key = ASN1::encodeDER($publicKey, ['type' => ASN1::TYPE_INTEGER]);
119 return self::wrapPublicKey($key, $params);
120 }
121 }
122