OpenSSH.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * OpenSSH Key Handler
  4. *
  5. * PHP version 5
  6. *
  7. * Place in $HOME/.ssh/authorized_keys
  8. *
  9. * @category Crypt
  10. * @package Common
  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\Common\Keys;
  17. use ParagonIE\ConstantTime\Base64;
  18. use phpseclib\Common\Functions\Strings;
  19. /**
  20. * OpenSSH Formatted RSA Key Handler
  21. *
  22. * @package Common
  23. * @author Jim Wigginton <terrafrost@php.net>
  24. * @access public
  25. */
  26. abstract class OpenSSH
  27. {
  28. /**
  29. * Default comment
  30. *
  31. * @var string
  32. * @access private
  33. */
  34. protected static $comment = 'phpseclib-generated-key';
  35. /**
  36. * Binary key flag
  37. *
  38. * @var bool
  39. * @access private
  40. */
  41. protected static $binary = false;
  42. /**
  43. * Sets the default comment
  44. *
  45. * @access public
  46. * @param string $comment
  47. */
  48. public static function setComment($comment)
  49. {
  50. self::$comment = str_replace(["\r", "\n"], '', $comment);
  51. }
  52. /**
  53. * Break a public or private key down into its constituent components
  54. *
  55. * $type can be either ssh-dss or ssh-rsa
  56. *
  57. * @access public
  58. * @param string $key
  59. * @param string $type
  60. * @return array
  61. */
  62. public static function load($key, $type)
  63. {
  64. if (!is_string($key)) {
  65. return false;
  66. }
  67. $parts = explode(' ', $key, 3);
  68. if (!isset($parts[1])) {
  69. $key = Base64::decode($parts[0]);
  70. $comment = isset($parts[1]) ? $parts[1] : false;
  71. } else {
  72. if ($parts[0] != $type) {
  73. return false;
  74. }
  75. $key = Base64::decode($parts[1]);
  76. $comment = isset($parts[2]) ? $parts[2] : false;
  77. }
  78. if ($key === false) {
  79. return false;
  80. }
  81. if (substr($key, 0, 11) != "\0\0\0\7$type") {
  82. return false;
  83. }
  84. Strings::shift($key, 11);
  85. if (strlen($key) <= 4) {
  86. return false;
  87. }
  88. return $key;
  89. }
  90. /**
  91. * Returns the comment for the key
  92. *
  93. * @access public
  94. * @return mixed
  95. */
  96. public static function getComment($key)
  97. {
  98. $parts = explode(' ', $key, 3);
  99. return isset($parts[2]) ? $parts[2] : false;
  100. }
  101. /**
  102. * Toggle between binary and printable keys
  103. *
  104. * Printable keys are what are generated by default. These are the ones that go in
  105. * $HOME/.ssh/authorized_key.
  106. *
  107. * @access public
  108. * @param bool $enabled
  109. */
  110. public static function setBinaryOutput($enabled)
  111. {
  112. self::$binary = $enabled;
  113. }
  114. /**
  115. * Returns the current binary output value
  116. *
  117. * @access public
  118. * @return bool
  119. */
  120. public static function getBinaryOutput()
  121. {
  122. return (bool) self::$binary;
  123. }
  124. }