PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.23.3
JetBackup – Backup, Restore & Migrate v3.1.23.3
3.1.23.5 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 / RSA / Formats / Keys / JWK.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / RSA / Formats / Keys Last commit date
.htaccess 1 year ago JWK.php 1 year ago MSBLOB.php 1 year ago OpenSSH.php 1 year ago PKCS1.php 1 year ago PKCS8.php 1 year ago PSS.php 1 year ago PuTTY.php 1 year ago Raw.php 1 year ago XML.php 1 year ago index.html 1 year ago web.config 1 year ago
JWK.php
131 lines
1 <?php
2
3 /**
4 * JSON Web Key (RFC7517) Formatted RSA 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\RSA\Formats\Keys;
17
18 use phpseclib3\Common\Functions\Strings;
19 use phpseclib3\Crypt\Common\Formats\Keys\JWK as Progenitor;
20 use phpseclib3\Math\BigInteger;
21
22 /**
23 * JWK Formatted RSA Handler
24 *
25 * @author Jim Wigginton <terrafrost@php.net>
26 */
27 abstract class JWK extends Progenitor
28 {
29 /**
30 * Break a public or private key down into its constituent components
31 *
32 * @param string|array $key
33 */
34 public static function load($key, ?string $password = null): array
35 {
36 $key = parent::loadHelper($key);
37
38 if ($key->kty != 'RSA') {
39 throw new \RuntimeException('Only RSA JWK keys are supported');
40 }
41
42 $count = $publicCount = 0;
43 $vars = ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi'];
44 foreach ($vars as $var) {
45 if (!isset($key->$var) || !is_string($key->$var)) {
46 continue;
47 }
48 $count++;
49 $value = new BigInteger(Strings::base64url_decode($key->$var), 256);
50 switch ($var) {
51 case 'n':
52 $publicCount++;
53 $components['modulus'] = $value;
54 break;
55 case 'e':
56 $publicCount++;
57 $components['publicExponent'] = $value;
58 break;
59 case 'd':
60 $components['privateExponent'] = $value;
61 break;
62 case 'p':
63 $components['primes'][1] = $value;
64 break;
65 case 'q':
66 $components['primes'][2] = $value;
67 break;
68 case 'dp':
69 $components['exponents'][1] = $value;
70 break;
71 case 'dq':
72 $components['exponents'][2] = $value;
73 break;
74 case 'qi':
75 $components['coefficients'][2] = $value;
76 }
77 }
78
79 if ($count == count($vars)) {
80 return $components + ['isPublicKey' => false];
81 }
82
83 if ($count == 2 && $publicCount == 2) {
84 return $components + ['isPublicKey' => true];
85 }
86
87 throw new \UnexpectedValueException('Key does not have an appropriate number of RSA parameters');
88 }
89
90 /**
91 * Convert a private key to the appropriate format.
92 *
93 * @param string $password optional
94 * @param array $options optional
95 */
96 public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string
97 {
98 if (count($primes) != 2) {
99 throw new \InvalidArgumentException('JWK does not support multi-prime RSA keys');
100 }
101
102 $key = [
103 'kty' => 'RSA',
104 'n' => Strings::base64url_encode($n->toBytes()),
105 'e' => Strings::base64url_encode($e->toBytes()),
106 'd' => Strings::base64url_encode($d->toBytes()),
107 'p' => Strings::base64url_encode($primes[1]->toBytes()),
108 'q' => Strings::base64url_encode($primes[2]->toBytes()),
109 'dp' => Strings::base64url_encode($exponents[1]->toBytes()),
110 'dq' => Strings::base64url_encode($exponents[2]->toBytes()),
111 'qi' => Strings::base64url_encode($coefficients[2]->toBytes()),
112 ];
113
114 return self::wrapKey($key, $options);
115 }
116
117 /**
118 * Convert a public key to the appropriate format
119 */
120 public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string
121 {
122 $key = [
123 'kty' => 'RSA',
124 'n' => Strings::base64url_encode($n->toBytes()),
125 'e' => Strings::base64url_encode($e->toBytes()),
126 ];
127
128 return self::wrapKey($key, $options);
129 }
130 }
131