PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
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 / EC / Formats / Keys / PKCS1.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / EC / Formats / Keys Last commit date
.htaccess 1 year ago Common.php 1 year ago JWK.php 1 year ago MontgomeryPrivate.php 1 year ago MontgomeryPublic.php 1 year ago OpenSSH.php 1 year ago PKCS1.php 1 year ago PKCS8.php 1 year ago PuTTY.php 1 year ago XML.php 1 year ago index.html 1 year ago libsodium.php 1 year ago web.config 1 year ago
PKCS1.php
190 lines
1 <?php
2
3 /**
4 * "PKCS1" (RFC5915) Formatted EC Key Handler
5 *
6 * PHP version 5
7 *
8 * Used by File/X509.php
9 *
10 * Processes keys with the following headers:
11 *
12 * -----BEGIN EC PRIVATE KEY-----
13 * -----BEGIN EC PARAMETERS-----
14 *
15 * Technically, PKCS1 is for RSA keys, only, but we're using PKCS1 to describe
16 * DSA, whose format isn't really formally described anywhere, so might as well
17 * use it to describe this, too. PKCS1 is easier to remember than RFC5915, after
18 * all. I suppose this could also be named IETF but idk
19 *
20 * @author Jim Wigginton <terrafrost@php.net>
21 * @copyright 2015 Jim Wigginton
22 * @license http://www.opensource.org/licenses/mit-license.html MIT License
23 * @link http://phpseclib.sourceforge.net
24 */
25
26 declare(strict_types=1);
27
28 namespace phpseclib3\Crypt\EC\Formats\Keys;
29
30 use phpseclib3\Common\Functions\Strings;
31 use phpseclib3\Crypt\Common\Formats\Keys\PKCS1 as Progenitor;
32 use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
33 use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve;
34 use phpseclib3\Crypt\EC\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
35 use phpseclib3\Exception\RuntimeException;
36 use phpseclib3\Exception\UnexpectedValueException;
37 use phpseclib3\Exception\UnsupportedCurveException;
38 use phpseclib3\File\ASN1;
39 use phpseclib3\File\ASN1\Maps;
40 use phpseclib3\Math\BigInteger;
41 use phpseclib3\Math\Common\FiniteField\Integer;
42
43 /**
44 * "PKCS1" (RFC5915) Formatted EC Key Handler
45 *
46 * @author Jim Wigginton <terrafrost@php.net>
47 */
48 abstract class PKCS1 extends Progenitor
49 {
50 use Common;
51
52 /**
53 * Break a public or private key down into its constituent components
54 *
55 * @param string|array $key
56 */
57 public static function load($key, ?string $password = null): array
58 {
59 self::initialize_static_variables();
60
61 if (!Strings::is_stringable($key)) {
62 throw new UnexpectedValueException('Key should be a string - not a ' . gettype($key));
63 }
64
65 if (strpos($key, 'BEGIN EC PARAMETERS') && strpos($key, 'BEGIN EC PRIVATE KEY')) {
66 $components = [];
67
68 preg_match('#-*BEGIN EC PRIVATE KEY-*[^-]*-*END EC PRIVATE KEY-*#s', $key, $matches);
69 $decoded = parent::load($matches[0], $password);
70 $decoded = ASN1::decodeBER($decoded);
71 if (!$decoded) {
72 throw new RuntimeException('Unable to decode BER');
73 }
74
75 $ecPrivate = ASN1::asn1map($decoded[0], Maps\ECPrivateKey::MAP);
76 if (!is_array($ecPrivate)) {
77 throw new RuntimeException('Unable to perform ASN1 mapping');
78 }
79
80 if (isset($ecPrivate['parameters'])) {
81 $components['curve'] = self::loadCurveByParam($ecPrivate['parameters']);
82 }
83
84 preg_match('#-*BEGIN EC PARAMETERS-*[^-]*-*END EC PARAMETERS-*#s', $key, $matches);
85 $decoded = parent::load($matches[0], '');
86 $decoded = ASN1::decodeBER($decoded);
87 if (!$decoded) {
88 throw new RuntimeException('Unable to decode BER');
89 }
90 $ecParams = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP);
91 if (!is_array($ecParams)) {
92 throw new RuntimeException('Unable to perform ASN1 mapping');
93 }
94 $ecParams = self::loadCurveByParam($ecParams);
95
96 // comparing $ecParams and $components['curve'] directly won't work because they'll have different Math\Common\FiniteField classes
97 // even if the modulo is the same
98 if (isset($components['curve']) && self::encodeParameters($ecParams, false, []) != self::encodeParameters($components['curve'], false, [])) {
99 throw new RuntimeException('EC PARAMETERS does not correspond to EC PRIVATE KEY');
100 }
101
102 if (!isset($components['curve'])) {
103 $components['curve'] = $ecParams;
104 }
105
106 $components['dA'] = new BigInteger($ecPrivate['privateKey'], 256);
107 $components['curve']->rangeCheck($components['dA']);
108 $components['QA'] = isset($ecPrivate['publicKey']) ?
109 self::extractPoint($ecPrivate['publicKey'], $components['curve']) :
110 $components['curve']->multiplyPoint($components['curve']->getBasePoint(), $components['dA']);
111
112 return $components;
113 }
114
115 $key = parent::load($key, $password);
116
117 $decoded = ASN1::decodeBER($key);
118 if (!$decoded) {
119 throw new RuntimeException('Unable to decode BER');
120 }
121
122 $key = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP);
123 if (is_array($key)) {
124 return ['curve' => self::loadCurveByParam($key)];
125 }
126
127 $key = ASN1::asn1map($decoded[0], Maps\ECPrivateKey::MAP);
128 if (!is_array($key)) {
129 throw new RuntimeException('Unable to perform ASN1 mapping');
130 }
131 if (!isset($key['parameters'])) {
132 throw new RuntimeException('Key cannot be loaded without parameters');
133 }
134
135 $components = [];
136 $components['curve'] = self::loadCurveByParam($key['parameters']);
137 $components['dA'] = new BigInteger($key['privateKey'], 256);
138 $components['QA'] = isset($ecPrivate['publicKey']) ?
139 self::extractPoint($ecPrivate['publicKey'], $components['curve']) :
140 $components['curve']->multiplyPoint($components['curve']->getBasePoint(), $components['dA']);
141
142 return $components;
143 }
144
145 /**
146 * Convert EC parameters to the appropriate format
147 */
148 public static function saveParameters(BaseCurve $curve, array $options = []): string
149 {
150 self::initialize_static_variables();
151
152 if ($curve instanceof TwistedEdwardsCurve || $curve instanceof MontgomeryCurve) {
153 throw new UnsupportedCurveException('TwistedEdwards and Montgomery Curves are not supported');
154 }
155
156 $key = self::encodeParameters($curve, false, $options);
157
158 return "-----BEGIN EC PARAMETERS-----\r\n" .
159 chunk_split(Strings::base64_encode($key), 64) .
160 "-----END EC PARAMETERS-----\r\n";
161 }
162
163 /**
164 * Convert a private key to the appropriate format.
165 *
166 * @param Integer[] $publicKey
167 */
168 public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $secret = null, ?string $password = null, array $options = []): string
169 {
170 self::initialize_static_variables();
171
172 if ($curve instanceof TwistedEdwardsCurve || $curve instanceof MontgomeryCurve) {
173 throw new UnsupportedCurveException('TwistedEdwards Curves are not supported');
174 }
175
176 $publicKey = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes();
177
178 $key = [
179 'version' => 'ecPrivkeyVer1',
180 'privateKey' => $privateKey->toBytes(),
181 'parameters' => new ASN1\Element(self::encodeParameters($curve)),
182 'publicKey' => "\0" . $publicKey,
183 ];
184
185 $key = ASN1::encodeDER($key, Maps\ECPrivateKey::MAP);
186
187 return self::wrapPrivateKey($key, 'EC', $password, $options);
188 }
189 }
190