2018-10-25 03:00:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2019-06-28 02:10:40 +02:00
|
|
|
* OpenSSH Formatted EC Key Handler
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* Place in $HOME/.ssh/authorized_keys
|
|
|
|
*
|
|
|
|
* @category Crypt
|
2019-06-28 02:10:40 +02:00
|
|
|
* @package EC
|
2018-10-25 03:00:37 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
2019-06-28 02:10:40 +02:00
|
|
|
namespace phpseclib\Crypt\EC\Formats\Keys;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
use ParagonIE\ConstantTime\Base64;
|
|
|
|
use phpseclib\Math\BigInteger;
|
|
|
|
use phpseclib\Common\Functions\Strings;
|
2019-06-25 05:44:10 +02:00
|
|
|
use phpseclib\Crypt\Common\Formats\Keys\OpenSSH as Progenitor;
|
2019-06-28 02:10:40 +02:00
|
|
|
use phpseclib\Crypt\EC\BaseCurves\Base as BaseCurve;
|
2018-10-25 03:00:37 +02:00
|
|
|
use phpseclib\Exception\UnsupportedCurveException;
|
2019-06-28 02:10:40 +02:00
|
|
|
use phpseclib\Crypt\EC\Curves\Ed25519;
|
2018-10-25 03:00:37 +02:00
|
|
|
use phpseclib\Math\Common\FiniteField\Integer;
|
|
|
|
|
|
|
|
/**
|
2019-06-28 02:10:40 +02:00
|
|
|
* OpenSSH Formatted EC Key Handler
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-06-28 02:10:40 +02:00
|
|
|
* @package EC
|
2018-10-25 03:00:37 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
abstract class OpenSSH extends Progenitor
|
|
|
|
{
|
|
|
|
use Common;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Supported Key Types
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-06-08 13:22:57 +02:00
|
|
|
protected static $types = [
|
2018-10-25 03:00:37 +02:00
|
|
|
'ecdsa-sha2-nistp256',
|
|
|
|
'ecdsa-sha2-nistp384',
|
|
|
|
'ecdsa-sha2-nistp521',
|
|
|
|
'ssh-ed25519'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Break a public or private key down into its constituent components
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $key
|
|
|
|
* @param string $password optional
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function load($key, $password = '')
|
|
|
|
{
|
2019-06-08 13:22:57 +02:00
|
|
|
$parsed = parent::load($key, $password);
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
if (isset($parsed['paddedKey'])) {
|
|
|
|
$paddedKey = $parsed['paddedKey'];
|
|
|
|
list($type) = Strings::unpackSSH2('s', $paddedKey);
|
|
|
|
if ($type != $parsed['type']) {
|
|
|
|
throw new \RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])");
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
2019-06-08 13:22:57 +02:00
|
|
|
if ($type == 'ssh-ed25519' ) {
|
|
|
|
list(, $key, $comment) = Strings::unpackSSH2('sss', $paddedKey);
|
|
|
|
$key = libsodium::load($key);
|
|
|
|
$key['comment'] = $comment;
|
|
|
|
return $key;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
2019-06-08 13:22:57 +02:00
|
|
|
list($curveName, $publicKey, $privateKey, $comment) = Strings::unpackSSH2('ssis', $paddedKey);
|
|
|
|
$curve = self::loadCurveByParam(['namedCurve' => $curveName]);
|
2018-10-25 03:00:37 +02:00
|
|
|
return [
|
|
|
|
'curve' => $curve,
|
2019-06-08 13:22:57 +02:00
|
|
|
'dA' => $curve->convertInteger($privateKey),
|
|
|
|
'QA' => self::extractPoint("\0$publicKey", $curve),
|
2018-10-25 03:00:37 +02:00
|
|
|
'comment' => $comment
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
if ($parsed['type'] == 'ssh-ed25519') {
|
|
|
|
if (Strings::shift($parsed['publicKey'], 4) != "\0\0\0\x20") {
|
2018-10-25 03:00:37 +02:00
|
|
|
throw new \RuntimeException('Length of ssh-ed25519 key should be 32');
|
|
|
|
}
|
|
|
|
|
|
|
|
$curve = new Ed25519();
|
2019-06-08 13:22:57 +02:00
|
|
|
$qa = self::extractPoint($parsed['publicKey'], $curve);
|
2018-10-25 03:00:37 +02:00
|
|
|
} else {
|
2019-06-08 13:22:57 +02:00
|
|
|
list($curveName, $publicKey) = Strings::unpackSSH2('ss', $parsed['publicKey']);
|
2019-06-28 02:10:40 +02:00
|
|
|
$curveName = '\phpseclib\Crypt\EC\Curves\\' . $curveName;
|
2018-10-25 03:00:37 +02:00
|
|
|
$curve = new $curveName();
|
|
|
|
|
|
|
|
$qa = self::extractPoint("\0" . $publicKey, $curve);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'curve' => $curve,
|
|
|
|
'QA' => $qa,
|
2019-06-08 13:22:57 +02:00
|
|
|
'comment' => $parsed['comment']
|
2018-10-25 03:00:37 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-08 13:22:57 +02:00
|
|
|
* Returns the alias that corresponds to a curve
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-08 13:22:57 +02:00
|
|
|
private static function getAlias(BaseCurve $curve)
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
|
|
|
self::initialize_static_variables();
|
|
|
|
|
|
|
|
$reflect = new \ReflectionClass($curve);
|
|
|
|
$name = $reflect->getShortName();
|
|
|
|
|
|
|
|
$oid = self::$curveOIDs[$name];
|
|
|
|
$aliases = array_filter(self::$curveOIDs, function($v) use ($oid) {
|
|
|
|
return $v == $oid;
|
|
|
|
});
|
|
|
|
$aliases = array_keys($aliases);
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($aliases); $i++) {
|
|
|
|
if (in_array('ecdsa-sha2-' . $aliases[$i], self::$types)) {
|
|
|
|
$alias = $aliases[$i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($alias)) {
|
|
|
|
throw new UnsupportedCurveException($name . ' is not a curve that the OpenSSH plugin supports');
|
|
|
|
}
|
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
return $alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-28 02:10:40 +02:00
|
|
|
* Convert an EC public key to the appropriate format
|
2019-06-08 13:22:57 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2019-06-28 02:10:40 +02:00
|
|
|
* @param \phpseclib\Crypt\EC\BaseCurves\Base $curve
|
2019-06-08 13:22:57 +02:00
|
|
|
* @param \phpseclib\Math\Common\FiniteField\Integer[] $publicKey
|
|
|
|
* @param array $options optional
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function savePublicKey(BaseCurve $curve, array $publicKey, array $options = [])
|
|
|
|
{
|
|
|
|
$comment = isset($options['comment']) ? $options['comment'] : self::$comment;
|
|
|
|
|
|
|
|
if ($curve instanceof Ed25519) {
|
|
|
|
$key = Strings::packSSH2('ss', 'ssh-ed25519', $curve->encodePoint($publicKey));
|
|
|
|
|
|
|
|
if (self::$binary) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = 'ssh-ed25519 ' . base64_encode($key) . ' ' . $comment;
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$alias = self::getAlias($curve);
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
$points = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes();
|
|
|
|
$key = Strings::packSSH2('sss', 'ecdsa-sha2-' . $alias, $alias, $points);
|
|
|
|
|
2019-06-01 20:23:11 +02:00
|
|
|
if (isset($options['binary']) ? $options['binary'] : self::$binary) {
|
2018-10-25 03:00:37 +02:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
$key = 'ecdsa-sha2-' . $alias . ' ' . base64_encode($key) . ' ' . $comment;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a private key to the appropriate format.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\Common\FiniteField\Integer $privateKey
|
2019-06-28 02:10:40 +02:00
|
|
|
* @param \phpseclib\Crypt\EC\Curves\Ed25519 $curve
|
2018-10-25 03:00:37 +02:00
|
|
|
* @param \phpseclib\Math\Common\FiniteField\Integer[] $publicKey
|
|
|
|
* @param string $password optional
|
2019-06-08 13:22:57 +02:00
|
|
|
* @param array $options optional
|
2018-10-25 03:00:37 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-08 13:22:57 +02:00
|
|
|
public static function savePrivateKey(Integer $privateKey, BaseCurve $curve, array $publicKey, $password = '', array $options = [])
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
2019-06-08 13:22:57 +02:00
|
|
|
if ($curve instanceof Ed25519) {
|
|
|
|
if (!isset($privateKey->secret)) {
|
|
|
|
throw new \RuntimeException('Private Key does not have a secret set');
|
|
|
|
}
|
|
|
|
if (strlen($privateKey->secret) != 32) {
|
|
|
|
throw new \RuntimeException('Private Key secret is not of the correct length');
|
|
|
|
}
|
|
|
|
|
|
|
|
$pubKey = $curve->encodePoint($publicKey);
|
|
|
|
|
|
|
|
$publicKey = Strings::packSSH2('ss', 'ssh-ed25519', $pubKey);
|
|
|
|
$privateKey = Strings::packSSH2('sss', 'ssh-ed25519', $pubKey, $privateKey->secret . $pubKey);
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
return self::wrapPrivateKey($publicKey, $privateKey, $options);
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 13:22:57 +02:00
|
|
|
$alias = self::getAlias($curve);
|
|
|
|
|
|
|
|
$points = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes();
|
|
|
|
$publicKey = self::savePublicKey($curve, $publicKey, ['binary' => true]);
|
|
|
|
|
|
|
|
$privateKey = Strings::packSSH2('sssi', 'ecdsa-sha2-' . $alias, $alias, $points, $privateKey);
|
|
|
|
|
|
|
|
return self::wrapPrivateKey($publicKey, $privateKey, $options);
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
}
|