.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 |