OpenSSH.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * OpenSSH Formatted RSA Key Handler
  4. *
  5. * PHP version 5
  6. *
  7. * Place in $HOME/.ssh/authorized_keys
  8. *
  9. * @category Crypt
  10. * @package RSA
  11. * @author Jim Wigginton <terrafrost@php.net>
  12. * @copyright 2015 Jim Wigginton
  13. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  14. * @link http://phpseclib.sourceforge.net
  15. */
  16. namespace phpseclib\Crypt\RSA;
  17. use ParagonIE\ConstantTime\Base64;
  18. use phpseclib\Math\BigInteger;
  19. /**
  20. * OpenSSH Formatted RSA Key Handler
  21. *
  22. * @package RSA
  23. * @author Jim Wigginton <terrafrost@php.net>
  24. * @access public
  25. */
  26. class OpenSSH
  27. {
  28. /**
  29. * Default comment
  30. *
  31. * @var string
  32. * @access private
  33. */
  34. static $comment = 'phpseclib-generated-key';
  35. /**
  36. * Sets the default comment
  37. *
  38. * @access public
  39. * @param string $comment
  40. */
  41. static function setComment($comment)
  42. {
  43. self::$comment = str_replace(array("\r", "\n"), '', $comment);
  44. }
  45. /**
  46. * Break a public or private key down into its constituent components
  47. *
  48. * @access public
  49. * @param string $key
  50. * @param string $password optional
  51. * @return array
  52. */
  53. static function load($key, $password = '')
  54. {
  55. if (!is_string($key)) {
  56. return false;
  57. }
  58. $parts = explode(' ', $key, 3);
  59. $key = isset($parts[1]) ? Base64::decode($parts[1]) : Base64::decode($parts[0]);
  60. if ($key === false) {
  61. return false;
  62. }
  63. $comment = isset($parts[2]) ? $parts[2] : false;
  64. if (substr($key, 0, 11) != "\0\0\0\7ssh-rsa") {
  65. return false;
  66. }
  67. self::_string_shift($key, 11);
  68. if (strlen($key) <= 4) {
  69. return false;
  70. }
  71. extract(unpack('Nlength', self::_string_shift($key, 4)));
  72. if (strlen($key) <= $length) {
  73. return false;
  74. }
  75. $publicExponent = new BigInteger(self::_string_shift($key, $length), -256);
  76. if (strlen($key) <= 4) {
  77. return false;
  78. }
  79. extract(unpack('Nlength', self::_string_shift($key, 4)));
  80. if (strlen($key) != $length) {
  81. return false;
  82. }
  83. $modulus = new BigInteger(self::_string_shift($key, $length), -256);
  84. return array(
  85. 'isPublicKey' => true,
  86. 'modulus' => $modulus,
  87. 'publicExponent' => $publicExponent,
  88. 'comment' => $comment
  89. );
  90. }
  91. /**
  92. * Convert a public key to the appropriate format
  93. *
  94. * @access public
  95. * @param \phpseclib\Math\BigInteger $n
  96. * @param \phpseclib\Math\BigInteger $e
  97. * @return string
  98. */
  99. static function savePublicKey(BigInteger $n, BigInteger $e)
  100. {
  101. $publicExponent = $e->toBytes(true);
  102. $modulus = $n->toBytes(true);
  103. // from <http://tools.ietf.org/html/rfc4253#page-15>:
  104. // string "ssh-rsa"
  105. // mpint e
  106. // mpint n
  107. $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
  108. $RSAPublicKey = 'ssh-rsa ' . Base64::encode($RSAPublicKey) . ' ' . self::$comment;
  109. return $RSAPublicKey;
  110. }
  111. /**
  112. * String Shift
  113. *
  114. * Inspired by array_shift
  115. *
  116. * @param string $string
  117. * @param int $index
  118. * @return string
  119. * @access private
  120. */
  121. static function _string_shift(&$string, $index = 1)
  122. {
  123. $substr = substr($string, 0, $index);
  124. $string = substr($string, $index);
  125. return $substr;
  126. }
  127. }