2014-12-02 04:18:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-12-03 17:59:01 +01:00
|
|
|
* Pure-PHP ssh-agent client.
|
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2014-12-03 17:59:01 +01:00
|
|
|
*
|
2014-12-02 04:18:58 +01:00
|
|
|
* @category System
|
2014-12-15 18:25:46 +01:00
|
|
|
* @package SSH\Agent
|
2014-12-02 04:18:58 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:07:46 +01:00
|
|
|
* @copyright 2009 Jim Wigginton
|
2014-12-02 04:18:58 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
* @internal See http://api.libssh.org/rfc/PROTOCOL.agent
|
|
|
|
*/
|
|
|
|
|
2014-12-15 18:25:46 +01:00
|
|
|
namespace phpseclib\System\SSH\Agent;
|
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
use phpseclib\Crypt\RSA;
|
|
|
|
use phpseclib\Exception\UnsupportedAlgorithmException;
|
2016-01-18 05:05:31 +01:00
|
|
|
use phpseclib\System\SSH\Agent;
|
2014-12-15 18:25:46 +01:00
|
|
|
|
2014-12-02 04:18:58 +01:00
|
|
|
/**
|
|
|
|
* Pure-PHP ssh-agent client identity object
|
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* Instantiation should only be performed by \phpseclib\System\SSH\Agent class.
|
2014-12-17 01:16:54 +01:00
|
|
|
* This could be thought of as implementing an interface that phpseclib\Crypt\RSA
|
2014-12-02 04:18:58 +01:00
|
|
|
* implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
|
2016-04-10 18:30:59 +02:00
|
|
|
* The methods in this interface would be getPublicKey and sign since those are the
|
|
|
|
* methods phpseclib looks for to perform public key authentication.
|
2014-12-02 04:18:58 +01:00
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* @package SSH\Agent
|
2014-12-02 04:18:58 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access internal
|
|
|
|
*/
|
2014-12-15 18:25:46 +01:00
|
|
|
class Identity
|
2014-12-02 04:18:58 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Key Object
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @var \phpseclib\Crypt\RSA
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::getPublicKey()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key Blob
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::sign()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $key_blob;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket Resource
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var resource
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::sign()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $fsock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param resource $fsock
|
2014-12-15 18:25:46 +01:00
|
|
|
* @return \phpseclib\System\SSH\Agent\Identity
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __construct($fsock)
|
|
|
|
{
|
|
|
|
$this->fsock = $fsock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Public Key
|
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* Called by \phpseclib\System\SSH\Agent::requestIdentities()
|
2014-12-02 04:18:58 +01:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @param \phpseclib\Crypt\RSA $key
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function setPublicKey($key)
|
|
|
|
{
|
|
|
|
$this->key = $key;
|
|
|
|
$this->key->setPublicKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Public Key
|
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* Called by \phpseclib\System\SSH\Agent::requestIdentities(). The key blob could be extracted from $this->key
|
2014-12-02 04:18:58 +01:00
|
|
|
* but this saves a small amount of computation.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $key_blob
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function setPublicKeyBlob($key_blob)
|
|
|
|
{
|
|
|
|
$this->key_blob = $key_blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Public Key
|
|
|
|
*
|
|
|
|
* Wrapper for $this->key->getPublicKey()
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $type optional
|
|
|
|
* @return mixed
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-04-10 18:30:59 +02:00
|
|
|
function getPublicKey($type = 'PKCS8')
|
2014-12-02 04:18:58 +01:00
|
|
|
{
|
2016-04-10 18:30:59 +02:00
|
|
|
return $this->key->getPublicKey($type);
|
2014-12-02 04:18:58 +01:00
|
|
|
}
|
|
|
|
|
2016-02-04 06:54:55 +01:00
|
|
|
/**
|
2016-04-10 18:30:59 +02:00
|
|
|
* Sets the hash
|
2016-02-04 06:54:55 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* ssh-agent only supports signatures with sha1 hashes but to maintain BC with RSA.php this function exists
|
2016-02-04 06:54:55 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $hash optional
|
|
|
|
* @throws \phpseclib\Exception\UnsupportedAlgorithmException if the algorithm is unsupported
|
2016-02-04 06:54:55 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-04-10 18:30:59 +02:00
|
|
|
function setHash($hash = 'sha1')
|
2016-02-04 06:54:55 +01:00
|
|
|
{
|
2016-04-10 18:30:59 +02:00
|
|
|
if ($hash != 'sha1') {
|
|
|
|
throw new UnsupportedAlgorithmException('ssh-agent can only be used with the sha1 hash');
|
|
|
|
}
|
2016-02-04 06:54:55 +01:00
|
|
|
}
|
|
|
|
|
2014-12-02 04:18:58 +01:00
|
|
|
/**
|
|
|
|
* Create a signature
|
|
|
|
*
|
|
|
|
* See "2.6.2 Protocol 2 private key signature request"
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $message
|
|
|
|
* @param int $padding optional
|
|
|
|
* @return string
|
|
|
|
* @throws \RuntimeException on connection errors
|
|
|
|
* @throws \phpseclib\Exception\UnsupportedAlgorithmException if the algorithm is unsupported
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-04-10 18:30:59 +02:00
|
|
|
function sign($message, $padding = RSA::PADDING_PKCS1)
|
2014-12-02 04:18:58 +01:00
|
|
|
{
|
2016-04-10 18:30:59 +02:00
|
|
|
if ($padding != RSA::PADDING_PKCS1 && $padding != RSA::PADDING_RELAXED_PKCS1) {
|
|
|
|
throw new UnsupportedAlgorithmException('ssh-agent can only create PKCS1 signatures');
|
|
|
|
}
|
|
|
|
|
2014-12-02 04:18:58 +01:00
|
|
|
// the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
|
2014-12-15 18:25:46 +01:00
|
|
|
$packet = pack('CNa*Na*N', Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
|
2014-12-02 04:18:58 +01:00
|
|
|
$packet = pack('Na*', strlen($packet), $packet);
|
|
|
|
if (strlen($packet) != fputs($this->fsock, $packet)) {
|
2016-04-10 18:30:59 +02:00
|
|
|
throw new \RuntimeException('Connection closed during signing');
|
2014-12-02 04:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$length = current(unpack('N', fread($this->fsock, 4)));
|
|
|
|
$type = ord(fread($this->fsock, 1));
|
2014-12-15 18:25:46 +01:00
|
|
|
if ($type != Agent::SSH_AGENT_SIGN_RESPONSE) {
|
2016-04-10 18:30:59 +02:00
|
|
|
throw new \RuntimeException('Unable to retreive signature');
|
2014-12-02 04:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$signature_blob = fread($this->fsock, $length - 1);
|
|
|
|
// the only other signature format defined - ssh-dss - is the same length as ssh-rsa
|
|
|
|
// the + 12 is for the other various SSH added length fields
|
|
|
|
return substr($signature_blob, strlen('ssh-rsa') + 12);
|
|
|
|
}
|
|
|
|
}
|