diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 95617510..a0fbc285 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -742,18 +742,18 @@ class Crypt_RSA */ function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients) { - $unsigned = $this->privateKeyFormat == CRYPT_RSA_PRIVATE_FORMAT_XML; + $signed = $this->privateKeyFormat != CRYPT_RSA_PRIVATE_FORMAT_XML; $num_primes = count($primes); $raw = array( 'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi - 'modulus' => $n->toBytes($unsigned), - 'publicExponent' => $e->toBytes($unsigned), - 'privateExponent' => $d->toBytes($unsigned), - 'prime1' => $primes[1]->toBytes($unsigned), - 'prime2' => $primes[2]->toBytes($unsigned), - 'exponent1' => $exponents[1]->toBytes($unsigned), - 'exponent2' => $exponents[2]->toBytes($unsigned), - 'coefficient' => $coefficients[2]->toBytes($unsigned) + 'modulus' => $n->toBytes($signed), + 'publicExponent' => $e->toBytes($signed), + 'privateExponent' => $d->toBytes($signed), + 'prime1' => $primes[1]->toBytes($signed), + 'prime2' => $primes[2]->toBytes($signed), + 'exponent1' => $exponents[1]->toBytes($signed), + 'exponent2' => $exponents[2]->toBytes($signed), + 'coefficient' => $coefficients[2]->toBytes($signed) ); // if the format in question does not support multi-prime rsa and multi-prime rsa was used, @@ -942,10 +942,10 @@ class Crypt_RSA */ function _convertPublicKey($n, $e) { - $unsigned = $this->publicKeyFormat == CRYPT_RSA_PUBLIC_FORMAT_XML; + $signed = $this->publicKeyFormat != CRYPT_RSA_PUBLIC_FORMAT_XML; - $modulus = $n->toBytes($unsigned); - $publicExponent = $e->toBytes($unsigned); + $modulus = $n->toBytes($signed); + $publicExponent = $e->toBytes($signed); switch ($this->publicKeyFormat) { case CRYPT_RSA_PUBLIC_FORMAT_RAW: diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index ff5d332e..b1b078ad 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -270,13 +270,35 @@ Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB $rsa = new Crypt_RSA(); $key = ' -v5OxcEgxPUfa701NpxnScCmlRkbwSGBiTWobHkIWZEB+AlRTHaVoZg/D8l6YzR7VdQidG6gF+nuUMjY75dBXgY/XcyVq0Hccf1jTfgARuNuq4GGG3hnCJVi2QsOgcf9R7TeXn+p1RKIhjQoWCiEQeEBTotNbJhcabNcPGSEJw+s= -AQAB + v5OxcEgxPUfa701NpxnScCmlRkbwSGBiTWobHkIWZEB+AlRTHaVoZg/D8l6YzR7VdQidG6gF+nuUMjY75dBXgY/XcyVq0Hccf1jTfgARuNuq4GGG3hnCJVi2QsOgcf9R7TeXn+p1RKIhjQoWCiEQeEBTotNbJhcabNcPGSEJw+s= + AQAB '; $rsa->loadKey($key); + $rsa->setPublicKey(); $newkey = $rsa->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_XML); $this->assertSame($key, $newkey); } + + /** + * @group github468 + */ + public function testSignedPKCS1() + { + $rsa = new Crypt_RSA(); + + $key = '-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/k7FwSDE9R9rvTU2nGdJwKaVG +RvBIYGJNahseQhZkQH4CVFMdpWhmD8PyXpjNHtV1CJ0bqAX6e5QyNjvl0FeBj9dz +JWrQdxx/WNN+ABG426rgYYbeGcIlWLZCw6Bx/1HtN5ef6nVEoiGNChYKIRB4QFOi +01smFxps1w8ZIQnD6wIDAQAB +-----END PUBLIC KEY-----'; + + $rsa->loadKey($key); + $rsa->setPublicKey(); + $newkey = $rsa->getPublicKey(); + + $this->assertSame($key, $newkey); + } }