From 3d4767301c8089da319a71e8cfdf6f6125e8d19f Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 25 Jan 2021 12:17:36 -0600 Subject: [PATCH] X509: fix niche issue with computeKeyIdentifier --- phpseclib/File/X509.php | 10 ++++------ tests/Unit/File/X509/X509Test.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 639a0233..16118029 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -43,6 +43,7 @@ use phpseclib3\Exception\UnsupportedAlgorithmException; use phpseclib3\File\ASN1\Element; use phpseclib3\File\ASN1\Maps; use phpseclib3\Math\BigInteger; +use phpseclib3\Crypt\PublicKeyLoader; /** * Pure-PHP X.509 Parser @@ -3690,14 +3691,11 @@ class X509 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)) { diff --git a/tests/Unit/File/X509/X509Test.php b/tests/Unit/File/X509/X509Test.php index d8b51313..51b8cb1a 100644 --- a/tests/Unit/File/X509/X509Test.php +++ b/tests/Unit/File/X509/X509Test.php @@ -1176,4 +1176,18 @@ qzFkAKWjJj4KjfrbZX4C0Spfxw== $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)); + } }