Skip to content

Commit

Permalink
X509: fix niche issue with computeKeyIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Jan 25, 2021
1 parent e8b4e4d commit 3d47673
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 4 additions & 6 deletions phpseclib/File/X509.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use phpseclib3\File\ASN1\Element;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\PublicKeyLoader;

/**
* Pure-PHP X.509 Parser
Expand Down Expand Up @@ -3690,14 +3691,11 @@ public function computeKeyIdentifier($key = null, $method = 1)
return false;
}
// If the key is private, compute identifier from its corresponding public key.
$key = new RSA();
if (!$key->load($raw)) {
return false; // Not an unencrypted RSA key.
}
if ($key->getPrivateKey() !== false) { // If private.
$key = PublicKeyLoader::load($raw);
if ($key instanceof PrivateKey) { // If private.
return $this->computeKeyIdentifier($key, $method);
}
$key = $raw; // Is a public key.
$key = $raw; // Is a public key.
break;
case $key instanceof X509:
if (isset($key->publicKey)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/File/X509/X509Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1176,4 +1176,18 @@ public function testMultiCertPEM()

$this->assertIsArray($r);
}

/**
* @group github1586
*/
public function testComputeKeyIdentifier()
{
$key = RSA::createKey(512);
$key = ASN1::extractBER("$key");
$key = ASN1::encodeDER($key, ['type' => ASN1::TYPE_BIT_STRING]);
$key = new Element($key);

$x509 = new X509;
$this->assertIsString($x509->computeKeyIdentifier($key));
}
}

1 comment on commit 3d47673

@terrafrost
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1586

Please sign in to comment.