[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PuTTY Formatted DSA Key Handler
   5   *
   6   * puttygen does not generate DSA keys with an N of anything other than 160, however,
   7   * it can still load them and convert them. PuTTY will load them, too, but SSH servers
   8   * won't accept them. Since PuTTY formatted keys are primarily used with SSH this makes
   9   * keys with N > 160 kinda useless, hence this handlers not supporting such keys.
  10   *
  11   * PHP version 5
  12   *
  13   * @author    Jim Wigginton <terrafrost@php.net>
  14   * @copyright 2015 Jim Wigginton
  15   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  16   * @link      http://phpseclib.sourceforge.net
  17   */
  18  
  19  namespace phpseclib3\Crypt\DSA\Formats\Keys;
  20  
  21  use phpseclib3\Common\Functions\Strings;
  22  use phpseclib3\Crypt\Common\Formats\Keys\PuTTY as Progenitor;
  23  use phpseclib3\Math\BigInteger;
  24  
  25  /**
  26   * PuTTY Formatted DSA Key Handler
  27   *
  28   * @author  Jim Wigginton <terrafrost@php.net>
  29   */
  30  abstract class PuTTY extends Progenitor
  31  {
  32      /**
  33       * Public Handler
  34       *
  35       * @var string
  36       */
  37      const PUBLIC_HANDLER = 'phpseclib3\Crypt\DSA\Formats\Keys\OpenSSH';
  38  
  39      /**
  40       * Algorithm Identifier
  41       *
  42       * @var array
  43       */
  44      protected static $types = ['ssh-dss'];
  45  
  46      /**
  47       * Break a public or private key down into its constituent components
  48       *
  49       * @param string $key
  50       * @param string $password optional
  51       * @return array
  52       */
  53      public static function load($key, $password = '')
  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          list($p, $q, $g, $y) = Strings::unpackSSH2('iiii', $public);
  63          list($x) = Strings::unpackSSH2('i', $private);
  64  
  65          return compact('p', 'q', 'g', 'y', 'x', 'comment');
  66      }
  67  
  68      /**
  69       * Convert a private key to the appropriate format.
  70       *
  71       * @param \phpseclib3\Math\BigInteger $p
  72       * @param \phpseclib3\Math\BigInteger $q
  73       * @param \phpseclib3\Math\BigInteger $g
  74       * @param \phpseclib3\Math\BigInteger $y
  75       * @param \phpseclib3\Math\BigInteger $x
  76       * @param string $password optional
  77       * @param array $options optional
  78       * @return string
  79       */
  80      public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, $password = false, array $options = [])
  81      {
  82          if ($q->getLength() != 160) {
  83              throw new \InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160');
  84          }
  85  
  86          $public = Strings::packSSH2('iiii', $p, $q, $g, $y);
  87          $private = Strings::packSSH2('i', $x);
  88  
  89          return self::wrapPrivateKey($public, $private, 'ssh-dss', $password, $options);
  90      }
  91  
  92      /**
  93       * Convert a public key to the appropriate format
  94       *
  95       * @param \phpseclib3\Math\BigInteger $p
  96       * @param \phpseclib3\Math\BigInteger $q
  97       * @param \phpseclib3\Math\BigInteger $g
  98       * @param \phpseclib3\Math\BigInteger $y
  99       * @return string
 100       */
 101      public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y)
 102      {
 103          if ($q->getLength() != 160) {
 104              throw new \InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160');
 105          }
 106  
 107          return self::wrapPublicKey(Strings::packSSH2('iiii', $p, $q, $g, $y), 'ssh-dss');
 108      }
 109  }