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 / RSA / Formats / Keys / XML.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
XML.php
163 lines
1 <?php
2
3 /**
4 * XML Formatted RSA Key Handler
5 *
6 * More info:
7 *
8 * http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
9 * http://www.w3.org/TR/xkms2/#XKMS_2_0_Paragraph_269
10 * http://en.wikipedia.org/wiki/XML_Signature
11 * http://en.wikipedia.org/wiki/XKMS
12 *
13 * PHP version 5
14 *
15 * @author Jim Wigginton <terrafrost@php.net>
16 * @copyright 2015 Jim Wigginton
17 * @license http://www.opensource.org/licenses/mit-license.html MIT License
18 * @link http://phpseclib.sourceforge.net
19 */
20
21 declare(strict_types=1);
22
23 namespace phpseclib3\Crypt\RSA\Formats\Keys;
24
25 use phpseclib3\Common\Functions\Strings;
26 use phpseclib3\Exception\BadConfigurationException;
27 use phpseclib3\Exception\InvalidArgumentException;
28 use phpseclib3\Exception\UnexpectedValueException;
29 use phpseclib3\Exception\UnsupportedFormatException;
30 use phpseclib3\Math\BigInteger;
31
32 /**
33 * XML Formatted RSA Key Handler
34 *
35 * @author Jim Wigginton <terrafrost@php.net>
36 */
37 abstract class XML
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): array
45 {
46 if (!Strings::is_stringable($key)) {
47 throw new UnexpectedValueException('Key should be a string - not a ' . gettype($key));
48 }
49
50 if (!class_exists('DOMDocument')) {
51 throw new BadConfigurationException('The dom extension is not setup correctly on this system');
52 }
53
54 $components = [
55 'isPublicKey' => false,
56 'primes' => [],
57 'exponents' => [],
58 'coefficients' => [],
59 ];
60
61 $use_errors = libxml_use_internal_errors(true);
62
63 $dom = new \DOMDocument();
64 if (substr($key, 0, 5) != '<?xml') {
65 $key = '<xml>' . $key . '</xml>';
66 }
67 if (!$dom->loadXML($key)) {
68 libxml_use_internal_errors($use_errors);
69 throw new UnexpectedValueException('Key does not appear to contain XML');
70 }
71 $xpath = new \DOMXPath($dom);
72 $keys = ['modulus', 'exponent', 'p', 'q', 'dp', 'dq', 'inverseq', 'd'];
73 foreach ($keys as $key) {
74 // $dom->getElementsByTagName($key) is case-sensitive
75 $temp = $xpath->query("//*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$key']");
76 if (!$temp->length) {
77 continue;
78 }
79 $value = new BigInteger(Strings::base64_decode($temp->item(0)->nodeValue), 256);
80 switch ($key) {
81 case 'modulus':
82 $components['modulus'] = $value;
83 break;
84 case 'exponent':
85 $components['publicExponent'] = $value;
86 break;
87 case 'p':
88 $components['primes'][1] = $value;
89 break;
90 case 'q':
91 $components['primes'][2] = $value;
92 break;
93 case 'dp':
94 $components['exponents'][1] = $value;
95 break;
96 case 'dq':
97 $components['exponents'][2] = $value;
98 break;
99 case 'inverseq':
100 $components['coefficients'][2] = $value;
101 break;
102 case 'd':
103 $components['privateExponent'] = $value;
104 }
105 }
106
107 libxml_use_internal_errors($use_errors);
108
109 foreach ($components as $key => $value) {
110 if (is_array($value) && !count($value)) {
111 unset($components[$key]);
112 }
113 }
114
115 if (isset($components['modulus']) && isset($components['publicExponent'])) {
116 if (count($components) == 3) {
117 $components['isPublicKey'] = true;
118 }
119 return $components;
120 }
121
122 throw new UnexpectedValueException('Modulus / exponent not present');
123 }
124
125 /**
126 * Convert a private key to the appropriate format.
127 *
128 * @param string $password optional
129 */
130 public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, string $password = ''): string
131 {
132 if (count($primes) != 2) {
133 throw new InvalidArgumentException('XML does not support multi-prime RSA keys');
134 }
135
136 if (!empty($password) && is_string($password)) {
137 throw new UnsupportedFormatException('XML private keys do not support encryption');
138 }
139
140 return "<RSAKeyPair>\r\n" .
141 ' <Modulus>' . Strings::base64_encode($n->toBytes()) . "</Modulus>\r\n" .
142 ' <Exponent>' . Strings::base64_encode($e->toBytes()) . "</Exponent>\r\n" .
143 ' <P>' . Strings::base64_encode($primes[1]->toBytes()) . "</P>\r\n" .
144 ' <Q>' . Strings::base64_encode($primes[2]->toBytes()) . "</Q>\r\n" .
145 ' <DP>' . Strings::base64_encode($exponents[1]->toBytes()) . "</DP>\r\n" .
146 ' <DQ>' . Strings::base64_encode($exponents[2]->toBytes()) . "</DQ>\r\n" .
147 ' <InverseQ>' . Strings::base64_encode($coefficients[2]->toBytes()) . "</InverseQ>\r\n" .
148 ' <D>' . Strings::base64_encode($d->toBytes()) . "</D>\r\n" .
149 '</RSAKeyPair>';
150 }
151
152 /**
153 * Convert a public key to the appropriate format
154 */
155 public static function savePublicKey(BigInteger $n, BigInteger $e): string
156 {
157 return "<RSAKeyValue>\r\n" .
158 ' <Modulus>' . Strings::base64_encode($n->toBytes()) . "</Modulus>\r\n" .
159 ' <Exponent>' . Strings::base64_encode($e->toBytes()) . "</Exponent>\r\n" .
160 '</RSAKeyValue>';
161 }
162 }
163