PKCS8.php 3.3 KB

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