phpseclib/phpseclib/Crypt/RSA/Formats/Keys/PuTTY.php

112 lines
3.3 KiB
PHP
Raw Normal View History

2016-12-23 16:02:07 +00:00
<?php
/**
* PuTTY Formatted RSA Key Handler
*
* PHP version 5
*
* @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
*/
2022-06-04 15:31:21 +00:00
declare(strict_types=1);
namespace phpseclib3\Crypt\RSA\Formats\Keys;
2016-12-23 16:02:07 +00:00
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\Common\Formats\Keys\PuTTY as Progenitor;
use phpseclib3\Exception\InvalidArgumentException;
use phpseclib3\Exception\UnexpectedValueException;
2022-01-30 15:34:42 +00:00
use phpseclib3\Math\BigInteger;
2016-12-23 16:02:07 +00:00
/**
* PuTTY Formatted RSA Key Handler
*
* @author Jim Wigginton <terrafrost@php.net>
*/
abstract class PuTTY extends Progenitor
{
/**
* Public Handler
*
* @var string
*/
2022-07-07 01:43:09 +00:00
public const PUBLIC_HANDLER = 'phpseclib3\Crypt\RSA\Formats\Keys\OpenSSH';
2016-12-23 16:02:07 +00:00
/**
* Algorithm Identifier
*
2018-10-25 01:00:37 +00:00
* @var array
2016-12-23 16:02:07 +00:00
*/
2018-10-25 01:00:37 +00:00
protected static $types = ['ssh-rsa'];
2016-12-23 16:02:07 +00:00
/**
* Break a public or private key down into its constituent components
*
2022-06-04 15:31:21 +00:00
* @param array|string $key
* @param string|false $password
* @return array|false
2016-12-23 16:02:07 +00:00
*/
2022-06-04 15:31:21 +00:00
public static function load($key, $password)
2016-12-23 16:02:07 +00:00
{
static $one;
if (!isset($one)) {
$one = new BigInteger(1);
}
$components = parent::load($key, $password);
2018-10-25 01:00:37 +00:00
if (!isset($components['private'])) {
2016-12-23 16:02:07 +00:00
return $components;
}
2018-10-25 01:00:37 +00:00
extract($components);
unset($components['public'], $components['private']);
2016-12-23 16:02:07 +00:00
$isPublicKey = false;
2018-10-25 01:00:37 +00:00
$result = Strings::unpackSSH2('ii', $public);
2016-12-23 16:02:07 +00:00
if ($result === false) {
throw new UnexpectedValueException('Key appears to be malformed');
2016-12-23 16:02:07 +00:00
}
2022-06-04 15:31:21 +00:00
[$publicExponent, $modulus] = $result;
2016-12-23 16:02:07 +00:00
2018-10-25 01:00:37 +00:00
$result = Strings::unpackSSH2('iiii', $private);
2016-12-23 16:02:07 +00:00
if ($result === false) {
throw new UnexpectedValueException('Key appears to be malformed');
2016-12-23 16:02:07 +00:00
}
$primes = $coefficients = [];
2022-06-04 15:31:21 +00:00
[$privateExponent, $primes[1], $primes[2], $coefficients[2]] = $result;
2016-12-23 16:02:07 +00:00
$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.
*/
2022-07-09 02:42:28 +00:00
public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string
2016-12-23 16:02:07 +00:00
{
if (count($primes) != 2) {
throw new InvalidArgumentException('PuTTY does not support multi-prime RSA keys');
2016-12-23 16:02:07 +00:00
}
$public = Strings::packSSH2('ii', $e, $n);
$private = Strings::packSSH2('iiii', $d, $primes[1], $primes[2], $coefficients[2]);
return self::wrapPrivateKey($public, $private, 'ssh-rsa', $password, $options);
2016-12-23 16:02:07 +00:00
}
/**
* Convert a public key to the appropriate format
*/
2022-06-04 15:31:21 +00:00
public static function savePublicKey(BigInteger $n, BigInteger $e): string
2016-12-23 16:02:07 +00:00
{
return self::wrapPublicKey(Strings::packSSH2('ii', $e, $n), 'ssh-rsa');
2016-12-23 16:02:07 +00:00
}
}