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 / JWK.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
JWK.php
185 lines
1 <?php
2
3 /**
4 * JSON Web Key (RFC7517 / RFC8037) Formatted EC Handler
5 *
6 * PHP version 5
7 *
8 * @author Jim Wigginton <terrafrost@php.net>
9 * @copyright 2015 Jim Wigginton
10 * @license http://www.opensource.org/licenses/mit-license.html MIT License
11 * @link http://phpseclib.sourceforge.net
12 */
13
14 declare(strict_types=1);
15
16 namespace phpseclib3\Crypt\EC\Formats\Keys;
17
18 use phpseclib3\Common\Functions\Strings;
19 use phpseclib3\Crypt\Common\Formats\Keys\JWK as Progenitor;
20 use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
21 use phpseclib3\Crypt\EC\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
22 use phpseclib3\Crypt\EC\Curves\Ed25519;
23 use phpseclib3\Crypt\EC\Curves\secp256k1;
24 use phpseclib3\Crypt\EC\Curves\secp256r1;
25 use phpseclib3\Crypt\EC\Curves\secp384r1;
26 use phpseclib3\Crypt\EC\Curves\secp521r1;
27 use phpseclib3\Exception\UnsupportedCurveException;
28 use phpseclib3\Math\BigInteger;
29
30 /**
31 * JWK Formatted EC Handler
32 *
33 * @author Jim Wigginton <terrafrost@php.net>
34 */
35 abstract class JWK extends Progenitor
36 {
37 use Common;
38
39 /**
40 * Break a public or private key down into its constituent components
41 *
42 * @param string|array $key
43 */
44 public static function load($key, ?string $password = null): array
45 {
46 $key = parent::loadHelper($key);
47
48 switch ($key->kty) {
49 case 'EC':
50 switch ($key->crv) {
51 case 'P-256':
52 case 'P-384':
53 case 'P-521':
54 case 'secp256k1':
55 break;
56 default:
57 throw new UnsupportedCurveException('Only P-256, P-384, P-521 and secp256k1 curves are accepted (' . $key->crv . ' provided)');
58 }
59 break;
60 case 'OKP':
61 switch ($key->crv) {
62 case 'Ed25519':
63 case 'Ed448':
64 break;
65 default:
66 throw new UnsupportedCurveException('Only Ed25519 and Ed448 curves are accepted (' . $key->crv . ' provided)');
67 }
68 break;
69 default:
70 throw new \Exception('Only EC and OKP JWK keys are supported');
71 }
72
73 $curve = '\phpseclib3\Crypt\EC\Curves\\' . str_replace('P-', 'nistp', $key->crv);
74 $curve = new $curve();
75
76 if ($curve instanceof TwistedEdwardsCurve) {
77 $QA = self::extractPoint(Strings::base64url_decode($key->x), $curve);
78 if (!isset($key->d)) {
79 return compact('curve', 'QA');
80 }
81 $arr = $curve->extractSecret(Strings::base64url_decode($key->d));
82 return compact('curve', 'QA') + $arr;
83 }
84
85 $QA = [
86 $curve->convertInteger(new BigInteger(Strings::base64url_decode($key->x), 256)),
87 $curve->convertInteger(new BigInteger(Strings::base64url_decode($key->y), 256)),
88 ];
89
90 if (!$curve->verifyPoint($QA)) {
91 throw new \RuntimeException('Unable to verify that point exists on curve');
92 }
93
94 if (!isset($key->d)) {
95 return compact('curve', 'QA');
96 }
97
98 $dA = new BigInteger(Strings::base64url_decode($key->d), 256);
99
100 $curve->rangeCheck($dA);
101
102 return compact('curve', 'dA', 'QA');
103 }
104
105 /**
106 * Returns the alias that corresponds to a curve
107 *
108 * @return string
109 */
110 private static function getAlias(BaseCurve $curve)
111 {
112 switch (true) {
113 case $curve instanceof secp256r1:
114 return 'P-256';
115 case $curve instanceof secp384r1:
116 return 'P-384';
117 case $curve instanceof secp521r1:
118 return 'P-521';
119 case $curve instanceof secp256k1:
120 return 'secp256k1';
121 }
122
123 $reflect = new \ReflectionClass($curve);
124 $curveName = $reflect->isFinal() ?
125 $reflect->getParentClass()->getShortName() :
126 $reflect->getShortName();
127 throw new UnsupportedCurveException("$curveName is not a supported curve");
128 }
129
130 /**
131 * Return the array superstructure for an EC public key
132 *
133 * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
134 */
135 private static function savePublicKeyHelper(BaseCurve $curve, array $publicKey): array
136 {
137 if ($curve instanceof TwistedEdwardsCurve) {
138 return [
139 'kty' => 'OKP',
140 'crv' => $curve instanceof Ed25519 ? 'Ed25519' : 'Ed448',
141 'x' => Strings::base64url_encode($curve->encodePoint($publicKey)),
142 ];
143 }
144
145 return [
146 'kty' => 'EC',
147 'crv' => self::getAlias($curve),
148 'x' => Strings::base64url_encode($publicKey[0]->toBytes()),
149 'y' => Strings::base64url_encode($publicKey[1]->toBytes()),
150 ];
151 }
152
153 /**
154 * Convert an EC public key to the appropriate format
155 *
156 * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
157 */
158 public static function savePublicKey(BaseCurve $curve, array $publicKey, array $options = []): string
159 {
160 $key = self::savePublicKeyHelper($curve, $publicKey);
161
162 return self::wrapKey($key, $options);
163 }
164
165 /**
166 * Convert a private key to the appropriate format.
167 *
168 * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
169 */
170 public static function savePrivateKey(
171 BigInteger $privateKey,
172 BaseCurve $curve,
173 array $publicKey,
174 ?string $secret = null,
175 ?string $password = null,
176 array $options = []
177 ): string {
178 $key = self::savePublicKeyHelper($curve, $publicKey);
179 $key['d'] = $curve instanceof TwistedEdwardsCurve ? $secret : $privateKey->toBytes();
180 $key['d'] = Strings::base64url_encode($key['d']);
181
182 return self::wrapKey($key, $options);
183 }
184 }
185