Brak opisu

Identity.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Pure-PHP ssh-agent client.
  4. *
  5. * PHP version 5
  6. *
  7. * @category System
  8. * @package SSH\Agent
  9. * @author Jim Wigginton <terrafrost@php.net>
  10. * @copyright 2009 Jim Wigginton
  11. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  12. * @link http://phpseclib.sourceforge.net
  13. * @internal See http://api.libssh.org/rfc/PROTOCOL.agent
  14. */
  15. namespace phpseclib\System\SSH\Agent;
  16. use phpseclib\System\SSH\Agent;
  17. /**
  18. * Pure-PHP ssh-agent client identity object
  19. *
  20. * Instantiation should only be performed by \phpseclib\System\SSH\Agent class.
  21. * This could be thought of as implementing an interface that phpseclib\Crypt\RSA
  22. * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
  23. * The methods in this interface would be getPublicKey and sign since those are the
  24. * methods phpseclib looks for to perform public key authentication.
  25. *
  26. * @package SSH\Agent
  27. * @author Jim Wigginton <terrafrost@php.net>
  28. * @access internal
  29. */
  30. class Identity
  31. {
  32. /**
  33. * Key Object
  34. *
  35. * @var \phpseclib\Crypt\RSA
  36. * @access private
  37. * @see self::getPublicKey()
  38. */
  39. var $key;
  40. /**
  41. * Key Blob
  42. *
  43. * @var string
  44. * @access private
  45. * @see self::sign()
  46. */
  47. var $key_blob;
  48. /**
  49. * Socket Resource
  50. *
  51. * @var resource
  52. * @access private
  53. * @see self::sign()
  54. */
  55. var $fsock;
  56. /**
  57. * Default Constructor.
  58. *
  59. * @param resource $fsock
  60. * @return \phpseclib\System\SSH\Agent\Identity
  61. * @access private
  62. */
  63. function __construct($fsock)
  64. {
  65. $this->fsock = $fsock;
  66. }
  67. /**
  68. * Set Public Key
  69. *
  70. * Called by \phpseclib\System\SSH\Agent::requestIdentities()
  71. *
  72. * @param \phpseclib\Crypt\RSA $key
  73. * @access private
  74. */
  75. function setPublicKey($key)
  76. {
  77. $this->key = $key;
  78. $this->key->setPublicKey();
  79. }
  80. /**
  81. * Set Public Key
  82. *
  83. * Called by \phpseclib\System\SSH\Agent::requestIdentities(). The key blob could be extracted from $this->key
  84. * but this saves a small amount of computation.
  85. *
  86. * @param string $key_blob
  87. * @access private
  88. */
  89. function setPublicKeyBlob($key_blob)
  90. {
  91. $this->key_blob = $key_blob;
  92. }
  93. /**
  94. * Get Public Key
  95. *
  96. * Wrapper for $this->key->getPublicKey()
  97. *
  98. * @param int $format optional
  99. * @return mixed
  100. * @access public
  101. */
  102. function getPublicKey($format = null)
  103. {
  104. return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
  105. }
  106. /**
  107. * Set Signature Mode
  108. *
  109. * Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
  110. * ssh-agent's only supported mode is \phpseclib\Crypt\RSA::SIGNATURE_PKCS1
  111. *
  112. * @param int $mode
  113. * @access public
  114. */
  115. function setSignatureMode($mode)
  116. {
  117. }
  118. /**
  119. * Create a signature
  120. *
  121. * See "2.6.2 Protocol 2 private key signature request"
  122. *
  123. * @param string $message
  124. * @return string
  125. * @access public
  126. */
  127. function sign($message)
  128. {
  129. // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
  130. $packet = pack('CNa*Na*N', Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
  131. $packet = pack('Na*', strlen($packet), $packet);
  132. if (strlen($packet) != fputs($this->fsock, $packet)) {
  133. user_error('Connection closed during signing');
  134. }
  135. $length = current(unpack('N', fread($this->fsock, 4)));
  136. $type = ord(fread($this->fsock, 1));
  137. if ($type != Agent::SSH_AGENT_SIGN_RESPONSE) {
  138. user_error('Unable to retrieve signature');
  139. }
  140. $signature_blob = fread($this->fsock, $length - 1);
  141. // the only other signature format defined - ssh-dss - is the same length as ssh-rsa
  142. // the + 12 is for the other various SSH added length fields
  143. return substr($signature_blob, strlen('ssh-rsa') + 12);
  144. }
  145. }