[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/DH/Formats/Keys/ -> PKCS8.php (source)

   1  <?php
   2  
   3  /**
   4   * PKCS#8 Formatted DH Key Handler
   5   *
   6   * PHP version 5
   7   *
   8   * Processes keys with the following headers:
   9   *
  10   * -----BEGIN ENCRYPTED PRIVATE KEY-----
  11   * -----BEGIN PRIVATE KEY-----
  12   * -----BEGIN PUBLIC KEY-----
  13   *
  14   * @author    Jim Wigginton <terrafrost@php.net>
  15   * @copyright 2015 Jim Wigginton
  16   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  17   * @link      http://phpseclib.sourceforge.net
  18   */
  19  
  20  namespace phpseclib3\Crypt\DH\Formats\Keys;
  21  
  22  use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
  23  use phpseclib3\File\ASN1;
  24  use phpseclib3\File\ASN1\Maps;
  25  use phpseclib3\Math\BigInteger;
  26  
  27  /**
  28   * PKCS#8 Formatted DH Key Handler
  29   *
  30   * @author  Jim Wigginton <terrafrost@php.net>
  31   */
  32  abstract class PKCS8 extends Progenitor
  33  {
  34      /**
  35       * OID Name
  36       *
  37       * @var string
  38       */
  39      const OID_NAME = 'dhKeyAgreement';
  40  
  41      /**
  42       * OID Value
  43       *
  44       * @var string
  45       */
  46      const OID_VALUE = '1.2.840.113549.1.3.1';
  47  
  48      /**
  49       * Child OIDs loaded
  50       *
  51       * @var bool
  52       */
  53      protected static $childOIDsLoaded = false;
  54  
  55      /**
  56       * Break a public or private key down into its constituent components
  57       *
  58       * @param string $key
  59       * @param string $password optional
  60       * @return array
  61       */
  62      public static function load($key, $password = '')
  63      {
  64          $key = parent::load($key, $password);
  65  
  66          $type = isset($key['privateKey']) ? 'privateKey' : 'publicKey';
  67  
  68          $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element);
  69          if (empty($decoded)) {
  70              throw new \RuntimeException('Unable to decode BER of parameters');
  71          }
  72          $components = ASN1::asn1map($decoded[0], Maps\DHParameter::MAP);
  73          if (!is_array($components)) {
  74              throw new \RuntimeException('Unable to perform ASN1 mapping on parameters');
  75          }
  76  
  77          $decoded = ASN1::decodeBER($key[$type]);
  78          switch (true) {
  79              case !isset($decoded):
  80              case !isset($decoded[0]['content']):
  81              case !$decoded[0]['content'] instanceof BigInteger:
  82                  throw new \RuntimeException('Unable to decode BER of parameters');
  83          }
  84          $components[$type] = $decoded[0]['content'];
  85  
  86          return $components;
  87      }
  88  
  89      /**
  90       * Convert a private key to the appropriate format.
  91       *
  92       * @param \phpseclib3\Math\BigInteger $prime
  93       * @param \phpseclib3\Math\BigInteger $base
  94       * @param \phpseclib3\Math\BigInteger $privateKey
  95       * @param \phpseclib3\Math\BigInteger $publicKey
  96       * @param string $password optional
  97       * @param array $options optional
  98       * @return string
  99       */
 100      public static function savePrivateKey(BigInteger $prime, BigInteger $base, BigInteger $privateKey, BigInteger $publicKey, $password = '', array $options = [])
 101      {
 102          $params = [
 103              'prime' => $prime,
 104              'base' => $base
 105          ];
 106          $params = ASN1::encodeDER($params, Maps\DHParameter::MAP);
 107          $params = new ASN1\Element($params);
 108          $key = ASN1::encodeDER($privateKey, ['type' => ASN1::TYPE_INTEGER]);
 109          return self::wrapPrivateKey($key, [], $params, $password, null, '', $options);
 110      }
 111  
 112      /**
 113       * Convert a public key to the appropriate format
 114       *
 115       * @param \phpseclib3\Math\BigInteger $prime
 116       * @param \phpseclib3\Math\BigInteger $base
 117       * @param \phpseclib3\Math\BigInteger $publicKey
 118       * @param array $options optional
 119       * @return string
 120       */
 121      public static function savePublicKey(BigInteger $prime, BigInteger $base, BigInteger $publicKey, array $options = [])
 122      {
 123          $params = [
 124              'prime' => $prime,
 125              'base' => $base
 126          ];
 127          $params = ASN1::encodeDER($params, Maps\DHParameter::MAP);
 128          $params = new ASN1\Element($params);
 129          $key = ASN1::encodeDER($publicKey, ['type' => ASN1::TYPE_INTEGER]);
 130          return self::wrapPublicKey($key, $params);
 131      }
 132  }