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-01-03 20:01:53 +01: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.
|
2015-12-24 17:58:06 +01: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
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::getPublicKey()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key Blob
|
|
|
|
*
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var string
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::sign()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $key_blob;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket Resource
|
|
|
|
*
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var resource
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access private
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::sign()
|
2014-12-02 04:18:58 +01:00
|
|
|
*/
|
|
|
|
var $fsock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2015-09-02 01:44:55 +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.
|
|
|
|
*
|
2015-09-02 01:44:55 +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-01-07 14:00:26 +01:00
|
|
|
* @param int $type optional
|
2015-09-02 01:44:55 +02:00
|
|
|
* @return mixed
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-01-07 14:00:26 +01:00
|
|
|
function getPublicKey($type = 'PKCS8')
|
2014-12-02 04:18:58 +01:00
|
|
|
{
|
2016-01-07 14:00:26 +01:00
|
|
|
return $this->key->getPublicKey($type);
|
2014-12-02 04:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a signature
|
|
|
|
*
|
|
|
|
* See "2.6.2 Protocol 2 private key signature request"
|
|
|
|
*
|
2015-09-02 01:44:55 +02:00
|
|
|
* @param string $message
|
2016-01-07 14:00:26 +01:00
|
|
|
* @param int $padding optional
|
2015-09-02 01:44:55 +02:00
|
|
|
* @return string
|
2015-09-01 06:36:56 +02:00
|
|
|
* @throws \RuntimeException on connection errors
|
2016-01-03 20:01:53 +01:00
|
|
|
* @throws \phpseclib\Exception\UnsupportedAlgorithmException if the algorithm is unsupported
|
2014-12-02 04:18:58 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-01-07 14:00:26 +01:00
|
|
|
function sign($message, $padding = RSA::PADDING_PKCS1)
|
2014-12-02 04:18:58 +01:00
|
|
|
{
|
2016-01-03 20:01:53 +01: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)) {
|
2015-09-01 06:36:56 +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) {
|
2015-09-01 06:36:56 +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);
|
|
|
|
}
|
|
|
|
}
|