diff --git a/phpseclib/Crypt/Common/Formats/Keys/PKCS8.php b/phpseclib/Crypt/Common/Formats/Keys/PKCS8.php index 8d0f950e..f8c199de 100644 --- a/phpseclib/Crypt/Common/Formats/Keys/PKCS8.php +++ b/phpseclib/Crypt/Common/Formats/Keys/PKCS8.php @@ -513,7 +513,7 @@ abstract class PKCS8 extends PKCS return $public; } - return false; + throw new \RuntimeException('Unable to parse using either OneAsymmetricKey or PublicKeyInfo ASN1 maps'); } /** diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 431eb5fd..dd3c3e71 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -412,7 +412,7 @@ abstract class RSA extends AsymmetricKey $key->publicExponent = $components['publicExponent']; $key->k = $key->modulus->getLengthInBytes(); - if ($components['isPublicKey']) { + if ($components['isPublicKey'] || !isset($components['privateExponent'])) { $key->exponent = $key->publicExponent; } else { $key->privateExponent = $components['privateExponent']; diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index 48331e8c..f2fe3e0c 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -1033,4 +1033,16 @@ v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs $key = PublicKeyLoader::load($key); $key->withPassword('demo')->toString('XML'); } + + public function testPublicAsPrivatePKCS1() + { + $key = '-----BEGIN RSA PRIVATE KEY----- +MIGJAoGBANOV2sOh8KgK9ENJMCzkIQ+UogWU7GP4JMpGxT6aEoxE3O5zUo2D1asv +RrnqAxlf1zz+1dnRDU8EYbt+DJMLJ5pBeDbBuQzzV690+f7eporcZombSN2JoPAM +n9dyFZYXxil/cgFG/PDMnuXy1Wcl8hb8iwQag4Y7ohiLXVTJa/0BAgMBAAE= +-----END RSA PRIVATE KEY-----'; + $key = PublicKeyLoader::load($key); + $result = $key->toString('PKCS1'); + $this->assertInternalType('string', $result); + } }