.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
OpenSSH.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * OpenSSH Formatted DSA Key Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * Place in $HOME/.ssh/authorized_keys |
| 9 | * |
| 10 | * @author Jim Wigginton <terrafrost@php.net> |
| 11 | * @copyright 2015 Jim Wigginton |
| 12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 13 | * @link http://phpseclib.sourceforge.net |
| 14 | */ |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | namespace phpseclib3\Crypt\DSA\Formats\Keys; |
| 19 | |
| 20 | use phpseclib3\Common\Functions\Strings; |
| 21 | use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH as Progenitor; |
| 22 | use phpseclib3\Exception\InvalidArgumentException; |
| 23 | use phpseclib3\Exception\RuntimeException; |
| 24 | use phpseclib3\Math\BigInteger; |
| 25 | |
| 26 | /** |
| 27 | * OpenSSH Formatted DSA Key Handler |
| 28 | * |
| 29 | * @author Jim Wigginton <terrafrost@php.net> |
| 30 | */ |
| 31 | abstract class OpenSSH extends Progenitor |
| 32 | { |
| 33 | /** |
| 34 | * Supported Key Types |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | protected static $types = ['ssh-dss']; |
| 39 | |
| 40 | /** |
| 41 | * Break a public or private key down into its constituent components |
| 42 | * |
| 43 | * @param string|array $key |
| 44 | */ |
| 45 | public static function load($key, ?string $password = null): array |
| 46 | { |
| 47 | $parsed = parent::load($key, $password); |
| 48 | |
| 49 | if (isset($parsed['paddedKey'])) { |
| 50 | [$type] = Strings::unpackSSH2('s', $parsed['paddedKey']); |
| 51 | if ($type != $parsed['type']) { |
| 52 | throw new RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])"); |
| 53 | } |
| 54 | |
| 55 | [$p, $q, $g, $y, $x, $comment] = Strings::unpackSSH2('i5s', $parsed['paddedKey']); |
| 56 | |
| 57 | return compact('p', 'q', 'g', 'y', 'x', 'comment'); |
| 58 | } |
| 59 | |
| 60 | [$p, $q, $g, $y] = Strings::unpackSSH2('iiii', $parsed['publicKey']); |
| 61 | |
| 62 | $comment = $parsed['comment']; |
| 63 | |
| 64 | return compact('p', 'q', 'g', 'y', 'comment'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Convert a public key to the appropriate format |
| 69 | * |
| 70 | * @param array $options optional |
| 71 | */ |
| 72 | public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, array $options = []): string |
| 73 | { |
| 74 | if ($q->getLength() != 160) { |
| 75 | throw new InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160'); |
| 76 | } |
| 77 | |
| 78 | // from <http://tools.ietf.org/html/rfc4253#page-15>: |
| 79 | // string "ssh-dss" |
| 80 | // mpint p |
| 81 | // mpint q |
| 82 | // mpint g |
| 83 | // mpint y |
| 84 | $DSAPublicKey = Strings::packSSH2('siiii', 'ssh-dss', $p, $q, $g, $y); |
| 85 | |
| 86 | if ($options['binary'] ?? self::$binary) { |
| 87 | return $DSAPublicKey; |
| 88 | } |
| 89 | |
| 90 | $comment = $options['comment'] ?? self::$comment; |
| 91 | $DSAPublicKey = 'ssh-dss ' . base64_encode($DSAPublicKey) . ' ' . $comment; |
| 92 | |
| 93 | return $DSAPublicKey; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Convert a private key to the appropriate format. |
| 98 | */ |
| 99 | public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, ?string $password = null, array $options = []): string |
| 100 | { |
| 101 | $publicKey = self::savePublicKey($p, $q, $g, $y, ['binary' => true]); |
| 102 | $privateKey = Strings::packSSH2('si5', 'ssh-dss', $p, $q, $g, $y, $x); |
| 103 | |
| 104 | return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); |
| 105 | } |
| 106 | } |
| 107 |