PKCS1.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * PKCS#1 Formatted DSA Key Handler
  4. *
  5. * PHP version 5
  6. *
  7. * Used by File/X509.php
  8. *
  9. * Processes keys with the following headers:
  10. *
  11. * -----BEGIN DSA PRIVATE KEY-----
  12. * -----BEGIN DSA PUBLIC KEY-----
  13. * -----BEGIN DSA PARAMETERS-----
  14. *
  15. * Analogous to ssh-keygen's pem format (as specified by -m)
  16. *
  17. * @category Crypt
  18. * @package DSA
  19. * @author Jim Wigginton <terrafrost@php.net>
  20. * @copyright 2015 Jim Wigginton
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link http://phpseclib.sourceforge.net
  23. */
  24. namespace phpseclib\Crypt\DSA\Keys;
  25. use phpseclib\Math\BigInteger;
  26. use phpseclib\Crypt\Common\Keys\PKCS1 as Progenitor;
  27. use phpseclib\File\ASN1;
  28. use phpseclib\File\ASN1\Maps;
  29. use ParagonIE\ConstantTime\Base64;
  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 PKCS1 extends Progenitor
  38. {
  39. /**
  40. * Break a public or private key down into its constituent components
  41. *
  42. * @access public
  43. * @param string $key
  44. * @param string $password optional
  45. * @return array
  46. */
  47. public static function load($key, $password = '')
  48. {
  49. if (!is_string($key)) {
  50. return false;
  51. }
  52. $key = parent::load($key, $password);
  53. if ($key === false) {
  54. return false;
  55. }
  56. $decoded = ASN1::decodeBER($key);
  57. if (empty($decoded)) {
  58. return false;
  59. }
  60. $key = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP);
  61. if (is_array($key)) {
  62. return $key;
  63. }
  64. $key = ASN1::asn1map($decoded[0], Maps\DSAPrivateKey::MAP);
  65. if (is_array($key)) {
  66. return $key;
  67. }
  68. $key = ASN1::asn1map($decoded[0], Maps\DSAPublicKey::MAP);
  69. return is_array($key) ? $key : false;
  70. }
  71. /**
  72. * Convert DSA parameters to the appropriate format
  73. *
  74. * @access public
  75. * @param \phpseclib\Math\BigInteger $p
  76. * @param \phpseclib\Math\BigInteger $q
  77. * @param \phpseclib\Math\BigInteger $g
  78. * @return string
  79. */
  80. public static function saveParameters($p, $q, $g)
  81. {
  82. $key = [
  83. 'p' => $p,
  84. 'q' => $q,
  85. 'g' => $g
  86. ];
  87. $key = ASN1::encodeDER($key, Maps\DSAParams::MAP);
  88. return "-----BEGIN DSA PARAMETERS-----\r\n" .
  89. chunk_split(Base64::encode($key), 64) .
  90. "-----END DSA PARAMETERS-----\r\n";
  91. }
  92. /**
  93. * Convert a private key to the appropriate format.
  94. *
  95. * @access public
  96. * @param \phpseclib\Math\BigInteger $p
  97. * @param \phpseclib\Math\BigInteger $q
  98. * @param \phpseclib\Math\BigInteger $g
  99. * @param \phpseclib\Math\BigInteger $x
  100. * @param \phpseclib\Math\BigInteger $y
  101. * @param string $password optional
  102. * @return string
  103. */
  104. public static function savePrivateKey($p, $q, $g, $y, $x, $password = '')
  105. {
  106. $key = [
  107. 'version' => 0,
  108. 'p' => $p,
  109. 'q' => $q,
  110. 'g' => $g,
  111. 'y' => $y,
  112. 'x' => $x
  113. ];
  114. $key = ASN1::encodeDER($key, Maps\DSAPrivateKey::MAP);
  115. return self::wrapPrivateKey($key, 'DSA', $password);
  116. }
  117. /**
  118. * Convert a public key to the appropriate format
  119. *
  120. * @access public
  121. * @param \phpseclib\Math\BigInteger $p
  122. * @param \phpseclib\Math\BigInteger $q
  123. * @param \phpseclib\Math\BigInteger $g
  124. * @param \phpseclib\Math\BigInteger $y
  125. * @return string
  126. */
  127. public static function savePublicKey($p, $q, $g, $y)
  128. {
  129. $key = ASN1::encodeDER($y, Maps\DSAPublicKey::MAP);
  130. return self::wrapPublicKey($key, 'DSA');
  131. }
  132. }