.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
PuTTY.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PuTTY Formatted DSA Key Handler |
| 5 | * |
| 6 | * puttygen does not generate DSA keys with an N of anything other than 160, however, |
| 7 | * it can still load them and convert them. PuTTY will load them, too, but SSH servers |
| 8 | * won't accept them. Since PuTTY formatted keys are primarily used with SSH this makes |
| 9 | * keys with N > 160 kinda useless, hence this handlers not supporting such keys. |
| 10 | * |
| 11 | * PHP version 5 |
| 12 | * |
| 13 | * @author Jim Wigginton <terrafrost@php.net> |
| 14 | * @copyright 2015 Jim Wigginton |
| 15 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 16 | * @link http://phpseclib.sourceforge.net |
| 17 | */ |
| 18 | |
| 19 | declare(strict_types=1); |
| 20 | |
| 21 | namespace phpseclib3\Crypt\DSA\Formats\Keys; |
| 22 | |
| 23 | use phpseclib3\Common\Functions\Strings; |
| 24 | use phpseclib3\Crypt\Common\Formats\Keys\PuTTY as Progenitor; |
| 25 | use phpseclib3\Exception\InvalidArgumentException; |
| 26 | use phpseclib3\Math\BigInteger; |
| 27 | |
| 28 | /** |
| 29 | * PuTTY Formatted DSA Key Handler |
| 30 | * |
| 31 | * @author Jim Wigginton <terrafrost@php.net> |
| 32 | */ |
| 33 | abstract class PuTTY extends Progenitor |
| 34 | { |
| 35 | /** |
| 36 | * Public Handler |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public const PUBLIC_HANDLER = 'phpseclib3\Crypt\DSA\Formats\Keys\OpenSSH'; |
| 41 | |
| 42 | /** |
| 43 | * Algorithm Identifier |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected static $types = ['ssh-dss']; |
| 48 | |
| 49 | /** |
| 50 | * Break a public or private key down into its constituent components |
| 51 | * |
| 52 | * @param array|string $key |
| 53 | * @param string|false $password |
| 54 | * @return array|false |
| 55 | */ |
| 56 | public static function load($key, $password) |
| 57 | { |
| 58 | $components = parent::load($key, $password); |
| 59 | if (!isset($components['private'])) { |
| 60 | return $components; |
| 61 | } |
| 62 | extract($components); |
| 63 | unset($components['public'], $components['private']); |
| 64 | |
| 65 | [$p, $q, $g, $y] = Strings::unpackSSH2('iiii', $public); |
| 66 | [$x] = Strings::unpackSSH2('i', $private); |
| 67 | |
| 68 | return compact('p', 'q', 'g', 'y', 'x', 'comment'); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Convert a private key to the appropriate format. |
| 73 | */ |
| 74 | public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, ?string $password = null, array $options = []): string |
| 75 | { |
| 76 | if ($q->getLength() != 160) { |
| 77 | throw new InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160'); |
| 78 | } |
| 79 | |
| 80 | $public = Strings::packSSH2('iiii', $p, $q, $g, $y); |
| 81 | $private = Strings::packSSH2('i', $x); |
| 82 | |
| 83 | return self::wrapPrivateKey($public, $private, 'ssh-dss', $password, $options); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Convert a public key to the appropriate format |
| 88 | */ |
| 89 | public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y): string |
| 90 | { |
| 91 | if ($q->getLength() != 160) { |
| 92 | throw new InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160'); |
| 93 | } |
| 94 | |
| 95 | return self::wrapPublicKey(Strings::packSSH2('iiii', $p, $q, $g, $y), 'ssh-dss'); |
| 96 | } |
| 97 | } |
| 98 |