Merge branch '3.0'

This commit is contained in:
terrafrost 2024-08-03 11:55:16 -05:00
commit cfaeb34813
4 changed files with 104 additions and 5 deletions

View File

@ -35,6 +35,7 @@ declare(strict_types=1);
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\Exception\BadConfigurationException; use phpseclib3\Exception\BadConfigurationException;
use phpseclib3\Exception\RuntimeException; use phpseclib3\Exception\RuntimeException;
@ -193,7 +194,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);
} }
@ -202,6 +204,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
@ -216,7 +236,7 @@ class Agent
/** /**
* Request agent forwarding of remote server * Request agent forwarding of remote server
*/ */
private function request_forwarding(SSH2 $ssh) private function request_forwarding(SSH2 $ssh): bool
{ {
if (!$ssh->requestAgentForwarding()) { if (!$ssh->requestAgentForwarding()) {
return false; return false;

View File

@ -82,6 +82,13 @@ class Identity implements PrivateKey
*/ */
private $flags = 0; private $flags = 0;
/**
* Comment
*
* @var null|string
*/
private $comment;
/** /**
* Curve Aliases * Curve Aliases
* *
@ -139,10 +146,8 @@ class Identity implements PrivateKey
* Get Public Key * Get Public Key
* *
* Wrapper for $this->key->getPublicKey() * Wrapper for $this->key->getPublicKey()
*
* @param string $type optional
*/ */
public function getPublicKey(string $type = 'PKCS8'): PublicKey public function getPublicKey(): PublicKey
{ {
return $this->key; return $this->key;
} }
@ -305,4 +310,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;
}
} }

View File

@ -0,0 +1,31 @@
<?php
/** @var iterable<SplFileInfo> $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($files as $file) {
if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) {
$fileContents = file_get_contents($file->getPathname());
if ($fileContents === false) {
throw new RuntimeException('file_get_contents() failed: ' . $file->getPathname());
}
$patternToReplacementMap = [
'~ function setUpBeforeClass\(\)~' => ' function setUpBeforeClass(): void',
'~ function setUp\(\)~' => ' function setUp(): void',
'~ function tearDown\(\)~' => ' function tearDown(): void',
'~ function assertIsArray\(\$actual, \$message = \'\'\)~' => ' function _assertIsArray($actual, string $message = \'\')',
'~ function assertIsResource\(\$actual, \$message = \'\'\)~' => ' function _assertIsResource($actual, string $message = \'\')',
'~ function assertIsObject\(\$actual, \$message = \'\'\)~' => ' function _assertIsObject($actual, string $message = \'\')',
'~ function assertIsString\(\$actual, \$message = \'\'\)~' => ' function _assertIsString($actual, string $message = \'\')',
'~ function assertStringContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function _assertStringContainsString(string $needle, string $haystack, string $message = \'\')',
'~ function assertStringNotContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function _assertStringNotContainsString(string $needle, string $haystack, string $message = \'\')'
];
$updatedFileContents = preg_replace(
array_keys($patternToReplacementMap),
array_values($patternToReplacementMap),
$fileContents
);
if (file_put_contents($file->getPathname(), $updatedFileContents) === false) {
throw new RuntimeException('file_put_contents() failed: ' . $file->getPathname());
}
}
}

View File

@ -0,0 +1,23 @@
<?php
/** @var iterable<SplFileInfo> $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($files as $file) {
if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) {
$fileContents = file_get_contents($file->getPathname());
if ($fileContents === false) {
throw new RuntimeException('file_get_contents() failed: ' . $file->getPathname());
}
$patternToReplacementMap = [
'~ function assertMatchesRegularExpression\(\$pattern, \$string, \$message = \'\'\)~' => ' function _assertMatchesRegularExpression(string $pattern, string $string, string $message = \'\')',
];
$updatedFileContents = preg_replace(
array_keys($patternToReplacementMap),
array_values($patternToReplacementMap),
$fileContents
);
if (file_put_contents($file->getPathname(), $updatedFileContents) === false) {
throw new RuntimeException('file_put_contents() failed: ' . $file->getPathname());
}
}
}