[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA/Formats/Keys/ -> PuTTY.php (source)

   1  <?php
   2  
   3  /**
   4   * PuTTY Formatted RSA Key Handler
   5   *
   6   * PHP version 5
   7   *
   8   * @author    Jim Wigginton <terrafrost@php.net>
   9   * @copyright 2015 Jim Wigginton
  10   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  11   * @link      http://phpseclib.sourceforge.net
  12   */
  13  
  14  namespace phpseclib3\Crypt\RSA\Formats\Keys;
  15  
  16  use phpseclib3\Common\Functions\Strings;
  17  use phpseclib3\Crypt\Common\Formats\Keys\PuTTY as Progenitor;
  18  use phpseclib3\Math\BigInteger;
  19  
  20  /**
  21   * PuTTY Formatted RSA Key Handler
  22   *
  23   * @author  Jim Wigginton <terrafrost@php.net>
  24   */
  25  abstract class PuTTY extends Progenitor
  26  {
  27      /**
  28       * Public Handler
  29       *
  30       * @var string
  31       */
  32      const PUBLIC_HANDLER = 'phpseclib3\Crypt\RSA\Formats\Keys\OpenSSH';
  33  
  34      /**
  35       * Algorithm Identifier
  36       *
  37       * @var array
  38       */
  39      protected static $types = ['ssh-rsa'];
  40  
  41      /**
  42       * Break a public or private key down into its constituent components
  43       *
  44       * @param string $key
  45       * @param string $password optional
  46       * @return array
  47       */
  48      public static function load($key, $password = '')
  49      {
  50          static $one;
  51          if (!isset($one)) {
  52              $one = new BigInteger(1);
  53          }
  54  
  55          $components = parent::load($key, $password);
  56          if (!isset($components['private'])) {
  57              return $components;
  58          }
  59          extract($components);
  60          unset($components['public'], $components['private']);
  61  
  62          $isPublicKey = false;
  63  
  64          $result = Strings::unpackSSH2('ii', $public);
  65          if ($result === false) {
  66              throw new \UnexpectedValueException('Key appears to be malformed');
  67          }
  68          list($publicExponent, $modulus) = $result;
  69  
  70          $result = Strings::unpackSSH2('iiii', $private);
  71          if ($result === false) {
  72              throw new \UnexpectedValueException('Key appears to be malformed');
  73          }
  74          $primes = $coefficients = [];
  75          list($privateExponent, $primes[1], $primes[2], $coefficients[2]) = $result;
  76  
  77          $temp = $primes[1]->subtract($one);
  78          $exponents = [1 => $publicExponent->modInverse($temp)];
  79          $temp = $primes[2]->subtract($one);
  80          $exponents[] = $publicExponent->modInverse($temp);
  81  
  82          return compact('publicExponent', 'modulus', 'privateExponent', 'primes', 'coefficients', 'exponents', 'comment', 'isPublicKey');
  83      }
  84  
  85      /**
  86       * Convert a private key to the appropriate format.
  87       *
  88       * @param \phpseclib3\Math\BigInteger $n
  89       * @param \phpseclib3\Math\BigInteger $e
  90       * @param \phpseclib3\Math\BigInteger $d
  91       * @param array $primes
  92       * @param array $exponents
  93       * @param array $coefficients
  94       * @param string $password optional
  95       * @param array $options optional
  96       * @return string
  97       */
  98      public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = [])
  99      {
 100          if (count($primes) != 2) {
 101              throw new \InvalidArgumentException('PuTTY does not support multi-prime RSA keys');
 102          }
 103  
 104          $public =  Strings::packSSH2('ii', $e, $n);
 105          $private = Strings::packSSH2('iiii', $d, $primes[1], $primes[2], $coefficients[2]);
 106  
 107          return self::wrapPrivateKey($public, $private, 'ssh-rsa', $password, $options);
 108      }
 109  
 110      /**
 111       * Convert a public key to the appropriate format
 112       *
 113       * @param \phpseclib3\Math\BigInteger $n
 114       * @param \phpseclib3\Math\BigInteger $e
 115       * @return string
 116       */
 117      public static function savePublicKey(BigInteger $n, BigInteger $e)
 118      {
 119          return self::wrapPublicKey(Strings::packSSH2('ii', $e, $n), 'ssh-rsa');
 120      }
 121  }