2016-12-23 17:02:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenSSH Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* Place in $HOME/.ssh/authorized_keys
|
|
|
|
*
|
|
|
|
* @category Crypt
|
|
|
|
* @package RSA
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace phpseclib\Crypt\RSA\Keys;
|
|
|
|
|
|
|
|
use ParagonIE\ConstantTime\Base64;
|
|
|
|
use phpseclib\Math\BigInteger;
|
|
|
|
use phpseclib\Common\Functions\Strings;
|
|
|
|
use phpseclib\Crypt\Common\Keys\OpenSSH as Progenitor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenSSH Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* @package RSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
abstract class OpenSSH extends Progenitor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Break a public or private key down into its constituent components
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $key
|
|
|
|
* @param string $password optional
|
2018-10-25 03:00:37 +02:00
|
|
|
* @return array
|
2016-12-23 17:02:07 +01:00
|
|
|
*/
|
|
|
|
public static function load($key, $password = '')
|
|
|
|
{
|
|
|
|
$key = parent::load($key, 'ssh-rsa');
|
|
|
|
|
|
|
|
$result = Strings::unpackSSH2('ii', $key);
|
|
|
|
if ($result === false) {
|
2018-10-25 03:00:37 +02:00
|
|
|
throw new \UnexpectedValueException('Key appears to be malformed');
|
2016-12-23 17:02:07 +01:00
|
|
|
}
|
|
|
|
list($publicExponent, $modulus) = $result;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'isPublicKey' => true,
|
|
|
|
'modulus' => $modulus,
|
|
|
|
'publicExponent' => $publicExponent,
|
|
|
|
'comment' => parent::getComment($key)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a public key to the appropriate format
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\BigInteger $n
|
|
|
|
* @param \phpseclib\Math\BigInteger $e
|
2019-06-01 20:23:11 +02:00
|
|
|
* @param array $options optional
|
2016-12-23 17:02:07 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-01 20:23:11 +02:00
|
|
|
public static function savePublicKey(BigInteger $n, BigInteger $e, $options = [])
|
2016-12-23 17:02:07 +01:00
|
|
|
{
|
|
|
|
$RSAPublicKey = Strings::packSSH2('sii', 'ssh-rsa', $e, $n);
|
|
|
|
|
2019-06-01 20:23:11 +02:00
|
|
|
if (isset($options['binary']) ? $options['binary'] : self::$binary) {
|
2016-12-23 17:02:07 +01:00
|
|
|
return $RSAPublicKey;
|
|
|
|
}
|
|
|
|
|
2019-06-01 20:23:11 +02:00
|
|
|
$comment = isset($options['comment']) ? $options['comment'] : self::$comment;
|
|
|
|
$RSAPublicKey = 'ssh-rsa ' . Base64::encode($RSAPublicKey) . ' ' . $comment;
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
return $RSAPublicKey;
|
|
|
|
}
|
|
|
|
}
|