2016-12-23 17:02:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PuTTY Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2019-06-25 05:44:10 +02:00
|
|
|
namespace phpseclib\Crypt\RSA\Formats\Keys;
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
use phpseclib\Math\BigInteger;
|
|
|
|
use phpseclib\Common\Functions\Strings;
|
2019-06-25 05:44:10 +02:00
|
|
|
use phpseclib\Crypt\Common\Formats\Keys\PuTTY as Progenitor;
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PuTTY Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* @package RSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
abstract class PuTTY extends Progenitor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Public Handler
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2019-06-25 05:44:10 +02:00
|
|
|
const PUBLIC_HANDLER = 'phpseclib\Crypt\RSA\Formats\Keys\OpenSSH';
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Algorithm Identifier
|
|
|
|
*
|
2018-10-25 03:00:37 +02:00
|
|
|
* @var array
|
2016-12-23 17:02:07 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2018-10-25 03:00:37 +02:00
|
|
|
protected static $types = ['ssh-rsa'];
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = '')
|
|
|
|
{
|
|
|
|
static $one;
|
|
|
|
if (!isset($one)) {
|
|
|
|
$one = new BigInteger(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$components = parent::load($key, $password);
|
2018-10-25 03:00:37 +02:00
|
|
|
if (!isset($components['private'])) {
|
2016-12-23 17:02:07 +01:00
|
|
|
return $components;
|
|
|
|
}
|
2018-10-25 03:00:37 +02:00
|
|
|
extract($components);
|
|
|
|
unset($components['public'], $components['private']);
|
2016-12-23 17:02:07 +01:00
|
|
|
|
|
|
|
$isPublicKey = false;
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
$result = Strings::unpackSSH2('ii', $public);
|
2016-12-23 17:02:07 +01:00
|
|
|
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;
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
$result = Strings::unpackSSH2('iiii', $private);
|
2016-12-23 17:02:07 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
$primes = $coefficients = [];
|
|
|
|
list($privateExponent, $primes[1], $primes[2], $coefficients[2]) = $result;
|
|
|
|
|
|
|
|
$temp = $primes[1]->subtract($one);
|
|
|
|
$exponents = [1 => $publicExponent->modInverse($temp)];
|
|
|
|
$temp = $primes[2]->subtract($one);
|
|
|
|
$exponents[] = $publicExponent->modInverse($temp);
|
|
|
|
|
|
|
|
return compact('publicExponent', 'modulus', 'privateExponent', 'primes', 'coefficients', 'exponents', 'comment', 'isPublicKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a private key to the appropriate format.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\BigInteger $n
|
|
|
|
* @param \phpseclib\Math\BigInteger $e
|
|
|
|
* @param \phpseclib\Math\BigInteger $d
|
|
|
|
* @param array $primes
|
|
|
|
* @param array $exponents
|
|
|
|
* @param array $coefficients
|
|
|
|
* @param string $password optional
|
2019-06-01 20:23:11 +02:00
|
|
|
* @param array $options optional
|
2016-12-23 17:02:07 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-02 17:02:30 +02:00
|
|
|
public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = [])
|
2016-12-23 17:02:07 +01:00
|
|
|
{
|
|
|
|
if (count($primes) != 2) {
|
|
|
|
throw new \InvalidArgumentException('PuTTY does not support multi-prime RSA keys');
|
|
|
|
}
|
|
|
|
|
|
|
|
$public = Strings::packSSH2('ii', $e, $n);
|
|
|
|
$private = Strings::packSSH2('iiii', $d, $primes[1], $primes[2], $coefficients[2]);
|
|
|
|
|
2019-06-01 20:23:11 +02:00
|
|
|
return self::wrapPrivateKey($public, $private, 'ssh-rsa', $password, $options);
|
2016-12-23 17:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a public key to the appropriate format
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\BigInteger $n
|
|
|
|
* @param \phpseclib\Math\BigInteger $e
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function savePublicKey(BigInteger $n, BigInteger $e)
|
|
|
|
{
|
2018-10-25 03:00:37 +02:00
|
|
|
return self::wrapPublicKey(Strings::packSSH2($e, $n), 'ssh-rsa');
|
2016-12-23 17:02:07 +01:00
|
|
|
}
|
|
|
|
}
|