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 / DSA / Formats / Keys / XML.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / DSA / Formats / Keys Last commit date
.htaccess 1 year ago OpenSSH.php 1 year ago PKCS1.php 1 year ago PKCS8.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
126 lines
1 <?php
2
3 /**
4 * XML Formatted DSA Key Handler
5 *
6 * While XKMS defines a private key format for RSA it does not do so for DSA. Quoting that standard:
7 *
8 * "[XKMS] does not specify private key parameters for the DSA signature algorithm since the algorithm only
9 * supports signature modes and so the application of server generated keys and key recovery is of limited
10 * value"
11 *
12 * PHP version 5
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\DSA\Formats\Keys;
23
24 use phpseclib3\Common\Functions\Strings;
25 use phpseclib3\Exception\BadConfigurationException;
26 use phpseclib3\Exception\UnexpectedValueException;
27 use phpseclib3\Math\BigInteger;
28
29 /**
30 * XML Formatted DSA Key Handler
31 *
32 * @author Jim Wigginton <terrafrost@php.net>
33 */
34 abstract class XML
35 {
36 /**
37 * Break a public or private key down into its constituent components
38 */
39 public static function load(string $key, ?string $password = null): array
40 {
41 if (!Strings::is_stringable($key)) {
42 throw new UnexpectedValueException('Key should be a string - not a ' . gettype($key));
43 }
44
45 if (!class_exists('DOMDocument')) {
46 throw new BadConfigurationException('The dom extension is not setup correctly on this system');
47 }
48
49 $use_errors = libxml_use_internal_errors(true);
50
51 $dom = new \DOMDocument();
52 if (substr($key, 0, 5) != '<?xml') {
53 $key = '<xml>' . $key . '</xml>';
54 }
55 if (!$dom->loadXML($key)) {
56 libxml_use_internal_errors($use_errors);
57 throw new UnexpectedValueException('Key does not appear to contain XML');
58 }
59 $xpath = new \DOMXPath($dom);
60 $keys = ['p', 'q', 'g', 'y', 'j', 'seed', 'pgencounter'];
61 foreach ($keys as $key) {
62 // $dom->getElementsByTagName($key) is case-sensitive
63 $temp = $xpath->query("//*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$key']");
64 if (!$temp->length) {
65 continue;
66 }
67 $value = new BigInteger(Strings::base64_decode($temp->item(0)->nodeValue), 256);
68 switch ($key) {
69 case 'p': // a prime modulus meeting the [DSS] requirements
70 // Parameters P, Q, and G can be public and common to a group of users. They might be known
71 // from application context. As such, they are optional but P and Q must either both appear
72 // or both be absent
73 $components['p'] = $value;
74 break;
75 case 'q': // an integer in the range 2**159 < Q < 2**160 which is a prime divisor of P-1
76 $components['q'] = $value;
77 break;
78 case 'g': // an integer with certain properties with respect to P and Q
79 $components['g'] = $value;
80 break;
81 case 'y': // G**X mod P (where X is part of the private key and not made public)
82 $components['y'] = $value;
83 // the remaining options do not do anything
84 case 'j': // (P - 1) / Q
85 // Parameter J is available for inclusion solely for efficiency as it is calculatable from
86 // P and Q
87 case 'seed': // a DSA prime generation seed
88 // Parameters seed and pgenCounter are used in the DSA prime number generation algorithm
89 // specified in [DSS]. As such, they are optional but must either both be present or both
90 // be absent
91 case 'pgencounter': // a DSA prime generation counter
92 }
93 }
94
95 libxml_use_internal_errors($use_errors);
96
97 if (!isset($components['y'])) {
98 throw new UnexpectedValueException('Key is missing y component');
99 }
100
101 switch (true) {
102 case !isset($components['p']):
103 case !isset($components['q']):
104 case !isset($components['g']):
105 return ['y' => $components['y']];
106 }
107
108 return $components;
109 }
110
111 /**
112 * Convert a public key to the appropriate format
113 *
114 * See https://www.w3.org/TR/xmldsig-core/#sec-DSAKeyValue
115 */
116 public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y): string
117 {
118 return "<DSAKeyValue>\r\n" .
119 ' <P>' . Strings::base64_encode($p->toBytes()) . "</P>\r\n" .
120 ' <Q>' . Strings::base64_encode($q->toBytes()) . "</Q>\r\n" .
121 ' <G>' . Strings::base64_encode($g->toBytes()) . "</G>\r\n" .
122 ' <Y>' . Strings::base64_encode($y->toBytes()) . "</Y>\r\n" .
123 '</DSAKeyValue>';
124 }
125 }
126