.htaccess
1 year ago
Common.php
1 year ago
JWK.php
1 year ago
MontgomeryPrivate.php
1 year ago
MontgomeryPublic.php
1 year ago
OpenSSH.php
1 year ago
PKCS1.php
1 year ago
PKCS8.php
1 year ago
PuTTY.php
1 year ago
XML.php
1 year ago
index.html
1 year ago
libsodium.php
1 year ago
web.config
1 year ago
OpenSSH.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * OpenSSH Formatted EC 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\EC\Formats\Keys; |
| 19 | |
| 20 | use phpseclib3\Common\Functions\Strings; |
| 21 | use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH as Progenitor; |
| 22 | use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve; |
| 23 | use phpseclib3\Crypt\EC\Curves\Ed25519; |
| 24 | use phpseclib3\Exception\RuntimeException; |
| 25 | use phpseclib3\Exception\UnsupportedCurveException; |
| 26 | use phpseclib3\Math\BigInteger; |
| 27 | use phpseclib3\Math\Common\FiniteField\Integer; |
| 28 | |
| 29 | /** |
| 30 | * OpenSSH Formatted EC Key Handler |
| 31 | * |
| 32 | * @author Jim Wigginton <terrafrost@php.net> |
| 33 | */ |
| 34 | abstract class OpenSSH extends Progenitor |
| 35 | { |
| 36 | use Common; |
| 37 | |
| 38 | /** |
| 39 | * Supported Key Types |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | protected static $types = [ |
| 44 | 'ecdsa-sha2-nistp256', |
| 45 | 'ecdsa-sha2-nistp384', |
| 46 | 'ecdsa-sha2-nistp521', |
| 47 | 'ssh-ed25519', |
| 48 | ]; |
| 49 | |
| 50 | /** |
| 51 | * Break a public or private key down into its constituent components |
| 52 | * |
| 53 | * @param string|array $key |
| 54 | */ |
| 55 | public static function load($key, ?string $password = null): array |
| 56 | { |
| 57 | $parsed = parent::load($key, $password); |
| 58 | |
| 59 | if (isset($parsed['paddedKey'])) { |
| 60 | $paddedKey = $parsed['paddedKey']; |
| 61 | [$type] = Strings::unpackSSH2('s', $paddedKey); |
| 62 | if ($type != $parsed['type']) { |
| 63 | throw new RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])"); |
| 64 | } |
| 65 | if ($type == 'ssh-ed25519') { |
| 66 | [, $key, $comment] = Strings::unpackSSH2('sss', $paddedKey); |
| 67 | $key = libsodium::load($key); |
| 68 | $key['comment'] = $comment; |
| 69 | return $key; |
| 70 | } |
| 71 | [$curveName, $publicKey, $privateKey, $comment] = Strings::unpackSSH2('ssis', $paddedKey); |
| 72 | $curve = self::loadCurveByParam(['namedCurve' => $curveName]); |
| 73 | $curve->rangeCheck($privateKey); |
| 74 | return [ |
| 75 | 'curve' => $curve, |
| 76 | 'dA' => $privateKey, |
| 77 | 'QA' => self::extractPoint("\0$publicKey", $curve), |
| 78 | 'comment' => $comment, |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | if ($parsed['type'] == 'ssh-ed25519') { |
| 83 | if (Strings::shift($parsed['publicKey'], 4) != "\0\0\0\x20") { |
| 84 | throw new RuntimeException('Length of ssh-ed25519 key should be 32'); |
| 85 | } |
| 86 | |
| 87 | $curve = new Ed25519(); |
| 88 | $qa = self::extractPoint($parsed['publicKey'], $curve); |
| 89 | } else { |
| 90 | [$curveName, $publicKey] = Strings::unpackSSH2('ss', $parsed['publicKey']); |
| 91 | $curveName = '\phpseclib3\Crypt\EC\Curves\\' . $curveName; |
| 92 | $curve = new $curveName(); |
| 93 | |
| 94 | $qa = self::extractPoint("\0" . $publicKey, $curve); |
| 95 | } |
| 96 | |
| 97 | return [ |
| 98 | 'curve' => $curve, |
| 99 | 'QA' => $qa, |
| 100 | 'comment' => $parsed['comment'], |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the alias that corresponds to a curve |
| 106 | */ |
| 107 | private static function getAlias(BaseCurve $curve): string |
| 108 | { |
| 109 | self::initialize_static_variables(); |
| 110 | |
| 111 | $reflect = new \ReflectionClass($curve); |
| 112 | $name = $reflect->getShortName(); |
| 113 | |
| 114 | $oid = self::$curveOIDs[$name]; |
| 115 | $aliases = array_filter(self::$curveOIDs, fn ($v) => $v == $oid); |
| 116 | $aliases = array_keys($aliases); |
| 117 | |
| 118 | for ($i = 0; $i < count($aliases); $i++) { |
| 119 | if (in_array('ecdsa-sha2-' . $aliases[$i], self::$types)) { |
| 120 | $alias = $aliases[$i]; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!isset($alias)) { |
| 126 | throw new UnsupportedCurveException($name . ' is not a curve that the OpenSSH plugin supports'); |
| 127 | } |
| 128 | |
| 129 | return $alias; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Convert an EC public key to the appropriate format |
| 134 | * |
| 135 | * @param Integer[] $publicKey |
| 136 | * @param array $options optional |
| 137 | */ |
| 138 | public static function savePublicKey(BaseCurve $curve, array $publicKey, array $options = []): string |
| 139 | { |
| 140 | $comment = $options['comment'] ?? self::$comment; |
| 141 | |
| 142 | if ($curve instanceof Ed25519) { |
| 143 | $key = Strings::packSSH2('ss', 'ssh-ed25519', $curve->encodePoint($publicKey)); |
| 144 | |
| 145 | if ($options['binary'] ?? self::$binary) { |
| 146 | return $key; |
| 147 | } |
| 148 | |
| 149 | $key = 'ssh-ed25519 ' . base64_encode($key) . ' ' . $comment; |
| 150 | return $key; |
| 151 | } |
| 152 | |
| 153 | $alias = self::getAlias($curve); |
| 154 | |
| 155 | $points = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes(); |
| 156 | $key = Strings::packSSH2('sss', 'ecdsa-sha2-' . $alias, $alias, $points); |
| 157 | |
| 158 | if ($options['binary'] ?? self::$binary) { |
| 159 | return $key; |
| 160 | } |
| 161 | |
| 162 | $key = 'ecdsa-sha2-' . $alias . ' ' . base64_encode($key) . ' ' . $comment; |
| 163 | |
| 164 | return $key; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Convert a private key to the appropriate format. |
| 169 | * |
| 170 | * @param Ed25519 $curve |
| 171 | * @param Integer[] $publicKey |
| 172 | * @param string|false $password |
| 173 | * @param array $options optional |
| 174 | */ |
| 175 | public static function savePrivateKey( |
| 176 | BigInteger $privateKey, |
| 177 | BaseCurve $curve, |
| 178 | array $publicKey, |
| 179 | ?string $secret = null, |
| 180 | ?string $password = null, |
| 181 | array $options = [] |
| 182 | ): string { |
| 183 | if ($curve instanceof Ed25519) { |
| 184 | if (!isset($secret)) { |
| 185 | throw new RuntimeException('Private Key does not have a secret set'); |
| 186 | } |
| 187 | if (strlen($secret) != 32) { |
| 188 | throw new RuntimeException('Private Key secret is not of the correct length'); |
| 189 | } |
| 190 | |
| 191 | $pubKey = $curve->encodePoint($publicKey); |
| 192 | |
| 193 | $publicKey = Strings::packSSH2('ss', 'ssh-ed25519', $pubKey); |
| 194 | $privateKey = Strings::packSSH2('sss', 'ssh-ed25519', $pubKey, $secret . $pubKey); |
| 195 | |
| 196 | return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); |
| 197 | } |
| 198 | |
| 199 | $alias = self::getAlias($curve); |
| 200 | |
| 201 | $points = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes(); |
| 202 | $publicKey = self::savePublicKey($curve, $publicKey, ['binary' => true]); |
| 203 | |
| 204 | $privateKey = Strings::packSSH2('sssi', 'ecdsa-sha2-' . $alias, $alias, $points, $privateKey); |
| 205 | |
| 206 | return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); |
| 207 | } |
| 208 | } |
| 209 |