[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PKCS#8 Formatted RSA Key Handler
   5   *
   6   * PHP version 5
   7   *
   8   * Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set)
   9   *
  10   * Processes keys with the following headers:
  11   *
  12   * -----BEGIN ENCRYPTED PRIVATE KEY-----
  13   * -----BEGIN PRIVATE KEY-----
  14   * -----BEGIN PUBLIC KEY-----
  15   *
  16   * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8
  17   * is specific to private keys it's basically creating a DER-encoded wrapper
  18   * for keys. This just extends that same concept to public keys (much like ssh-keygen)
  19   *
  20   * @author    Jim Wigginton <terrafrost@php.net>
  21   * @copyright 2015 Jim Wigginton
  22   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  23   * @link      http://phpseclib.sourceforge.net
  24   */
  25  
  26  namespace phpseclib3\Crypt\RSA\Formats\Keys;
  27  
  28  use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
  29  use phpseclib3\File\ASN1;
  30  use phpseclib3\Math\BigInteger;
  31  
  32  /**
  33   * PKCS#8 Formatted RSA Key Handler
  34   *
  35   * @author  Jim Wigginton <terrafrost@php.net>
  36   */
  37  abstract class PKCS8 extends Progenitor
  38  {
  39      /**
  40       * OID Name
  41       *
  42       * @var string
  43       */
  44      const OID_NAME = 'rsaEncryption';
  45  
  46      /**
  47       * OID Value
  48       *
  49       * @var string
  50       */
  51      const OID_VALUE = '1.2.840.113549.1.1.1';
  52  
  53      /**
  54       * Child OIDs loaded
  55       *
  56       * @var bool
  57       */
  58      protected static $childOIDsLoaded = false;
  59  
  60      /**
  61       * Break a public or private key down into its constituent components
  62       *
  63       * @param string $key
  64       * @param string $password optional
  65       * @return array
  66       */
  67      public static function load($key, $password = '')
  68      {
  69          $key = parent::load($key, $password);
  70  
  71          if (isset($key['privateKey'])) {
  72              $components['isPublicKey'] = false;
  73              $type = 'private';
  74          } else {
  75              $components['isPublicKey'] = true;
  76              $type = 'public';
  77          }
  78  
  79          $result = $components + PKCS1::load($key[$type . 'Key']);
  80  
  81          if (isset($key['meta'])) {
  82              $result['meta'] = $key['meta'];
  83          }
  84  
  85          return $result;
  86      }
  87  
  88      /**
  89       * Convert a private key to the appropriate format.
  90       *
  91       * @param \phpseclib3\Math\BigInteger $n
  92       * @param \phpseclib3\Math\BigInteger $e
  93       * @param \phpseclib3\Math\BigInteger $d
  94       * @param array $primes
  95       * @param array $exponents
  96       * @param array $coefficients
  97       * @param string $password optional
  98       * @param array $options optional
  99       * @return string
 100       */
 101      public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = [])
 102      {
 103          $key = PKCS1::savePrivateKey($n, $e, $d, $primes, $exponents, $coefficients);
 104          $key = ASN1::extractBER($key);
 105          return self::wrapPrivateKey($key, [], null, $password, null, '', $options);
 106      }
 107  
 108      /**
 109       * Convert a public key to the appropriate format
 110       *
 111       * @param \phpseclib3\Math\BigInteger $n
 112       * @param \phpseclib3\Math\BigInteger $e
 113       * @param array $options optional
 114       * @return string
 115       */
 116      public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = [])
 117      {
 118          $key = PKCS1::savePublicKey($n, $e);
 119          $key = ASN1::extractBER($key);
 120          return self::wrapPublicKey($key, null);
 121      }
 122  }