mirror of
https://github.com/phpseclib/phpseclib.git
synced 2024-12-25 11:20:03 +00:00
SSH/Agent: make it so identities include key comments and add new
findIdentityByPublicKey() method
This commit is contained in:
parent
45b98d8cb3
commit
10075ea57e
@ -33,6 +33,7 @@
|
||||
namespace phpseclib3\System\SSH;
|
||||
|
||||
use phpseclib3\Common\Functions\Strings;
|
||||
use phpseclib3\Crypt\Common\PublicKey;
|
||||
use phpseclib3\Crypt\PublicKeyLoader;
|
||||
use phpseclib3\Crypt\RSA;
|
||||
use phpseclib3\Exception\BadConfigurationException;
|
||||
@ -192,7 +193,8 @@ class Agent
|
||||
if (isset($key)) {
|
||||
$identity = (new Identity($this->fsock))
|
||||
->withPublicKey($key)
|
||||
->withPublicKeyBlob($key_blob);
|
||||
->withPublicKeyBlob($key_blob)
|
||||
->withComment($comment);
|
||||
$identities[] = $identity;
|
||||
unset($key);
|
||||
}
|
||||
@ -201,6 +203,24 @@ class Agent
|
||||
return $identities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SSH Agent identity matching a given public key or null if no identity is found
|
||||
*
|
||||
* @return ?Identity
|
||||
*/
|
||||
public function findIdentityByPublicKey(PublicKey $key)
|
||||
{
|
||||
$identities = $this->requestIdentities();
|
||||
$key = (string) $key;
|
||||
foreach ($identities as $identity) {
|
||||
if (((string) $identity->getPublicKey()) == $key) {
|
||||
return $identity;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal that agent forwarding should
|
||||
* be requested when a channel is opened
|
||||
|
@ -79,6 +79,13 @@ class Identity implements PrivateKey
|
||||
*/
|
||||
private $flags = 0;
|
||||
|
||||
/**
|
||||
* Comment
|
||||
*
|
||||
* @var null|string
|
||||
*/
|
||||
private $comment;
|
||||
|
||||
/**
|
||||
* Curve Aliases
|
||||
*
|
||||
@ -141,10 +148,9 @@ class Identity implements PrivateKey
|
||||
*
|
||||
* Wrapper for $this->key->getPublicKey()
|
||||
*
|
||||
* @param string $type optional
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPublicKey($type = 'PKCS8')
|
||||
public function getPublicKey()
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
@ -317,4 +323,24 @@ class Identity implements PrivateKey
|
||||
{
|
||||
throw new \RuntimeException('ssh-agent does not provide a mechanism to get the private key');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment
|
||||
*/
|
||||
public function withComment($comment = null)
|
||||
{
|
||||
$new = clone $this;
|
||||
$new->comment = $comment;
|
||||
return $new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the comment
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function getComment()
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user