SSH/Agent: make it so identities include key comments and add new

findIdentityByPublicKey() method
This commit is contained in:
terrafrost 2024-08-03 09:47:18 -05:00
parent 45b98d8cb3
commit 10075ea57e
2 changed files with 49 additions and 3 deletions

View File

@ -33,6 +33,7 @@
namespace phpseclib3\System\SSH; namespace phpseclib3\System\SSH;
use phpseclib3\Common\Functions\Strings; use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA; use phpseclib3\Crypt\RSA;
use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Exception\BadConfigurationException;
@ -192,7 +193,8 @@ class Agent
if (isset($key)) { if (isset($key)) {
$identity = (new Identity($this->fsock)) $identity = (new Identity($this->fsock))
->withPublicKey($key) ->withPublicKey($key)
->withPublicKeyBlob($key_blob); ->withPublicKeyBlob($key_blob)
->withComment($comment);
$identities[] = $identity; $identities[] = $identity;
unset($key); unset($key);
} }
@ -201,6 +203,24 @@ class Agent
return $identities; 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 * Signal that agent forwarding should
* be requested when a channel is opened * be requested when a channel is opened

View File

@ -79,6 +79,13 @@ class Identity implements PrivateKey
*/ */
private $flags = 0; private $flags = 0;
/**
* Comment
*
* @var null|string
*/
private $comment;
/** /**
* Curve Aliases * Curve Aliases
* *
@ -141,10 +148,9 @@ class Identity implements PrivateKey
* *
* Wrapper for $this->key->getPublicKey() * Wrapper for $this->key->getPublicKey()
* *
* @param string $type optional
* @return mixed * @return mixed
*/ */
public function getPublicKey($type = 'PKCS8') public function getPublicKey()
{ {
return $this->key; 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'); 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;
}
} }