Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
p & q primes must be set for all RSA keys to avoid errors during PEM/…
Browse files Browse the repository at this point in the history
…DER conversions
  • Loading branch information
Florent Morselli committed Aug 4, 2016
1 parent 0099d2a commit 460a3b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 54 deletions.
9 changes: 4 additions & 5 deletions src/KeyConverter/RSAKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ private function loadJWK(array $jwk)
*/
private function populateCRT()
{
if (!array_key_exists('p', $this->values) && !array_key_exists('q', $this->values)) {
return;
}
Assertion::keyExists($this->values, 'p', 'The prime "p" is not available.');
Assertion::keyExists($this->values, 'q', 'The prime "q" is not available.');
if (array_key_exists('dp', $this->values) && array_key_exists('dq', $this->values) && array_key_exists('qi', $this->values)) {
return;
}
Expand Down Expand Up @@ -331,8 +330,8 @@ private function initPrivateKey()
$n = new Integer($this->fromBase64ToInteger($this->values['n']));
$e = new Integer($this->fromBase64ToInteger($this->values['e']));
$d = new Integer($this->fromBase64ToInteger($this->values['d']));
$p = array_key_exists('p', $this->values) ? new Integer($this->fromBase64ToInteger($this->values['p'])) : new Integer(0);
$q = array_key_exists('q', $this->values) ? new Integer($this->fromBase64ToInteger($this->values['q'])) : new Integer(0);
$p = new Integer($this->fromBase64ToInteger($this->values['p']));
$q = new Integer($this->fromBase64ToInteger($this->values['q']));
$dp = array_key_exists('dp', $this->values) ? new Integer($this->fromBase64ToInteger($this->values['dp'])) : new Integer(0);
$dq = array_key_exists('dq', $this->values) ? new Integer($this->fromBase64ToInteger($this->values['dq'])) : new Integer(0);
$qi = array_key_exists('qi', $this->values) ? new Integer($this->fromBase64ToInteger($this->values['qi'])) : new Integer(0);
Expand Down
49 changes: 0 additions & 49 deletions tests/Unit/Algorithm/RSAKeyWithoutAllPrimesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ public function testSignatureAlgorithms($signature_algorithm)
$this->assertInstanceOf(JWSInterface::class, $loader->loadAndVerifySignatureUsingKey($jwt, $key, [$signature_algorithm]));
}

/**
* @param string $signature_algorithm
*
* @dataProvider dataSignatureAlgorithmsWithSimpleKey
*/
public function testSignatureAlgorithmsWithSimpleKey($signature_algorithm)
{
$key = $this->getSimplePrivateKey();

$claims = ['foo' => 'bar'];

$jwt = JWSFactory::createJWSToCompactJSON($claims, $key, ['alg' => $signature_algorithm]);

$loader = new Loader();

$this->assertInstanceOf(JWSInterface::class, $loader->loadAndVerifySignatureUsingKey($jwt, $key, [$signature_algorithm]));
}

/**
* @return array
*/
Expand Down Expand Up @@ -105,24 +87,6 @@ public function testEncryptionAlgorithms($encryption_algorithm)
$this->assertInstanceOf(JWEInterface::class, $loader->loadAndDecryptUsingKey($jwt, $key, [$encryption_algorithm], ['A256GCM']));
}

/**
* @param string $encryption_algorithm
*
* @dataProvider dataEncryptionAlgorithmsWithSimpleKey
*/
public function testEncryptionAlgorithmsWithSimpleKey($encryption_algorithm)
{
$key = $this->getSimplePrivateKey();

$claims = ['foo' => 'bar'];

$jwt = JWEFactory::createJWEToCompactJSON($claims, $key, ['alg' => $encryption_algorithm, 'enc' => 'A256GCM']);

$loader = new Loader();

$this->assertInstanceOf(JWEInterface::class, $loader->loadAndDecryptUsingKey($jwt, $key, [$encryption_algorithm], ['A256GCM']));
}

/**
* @return array
*/
Expand Down Expand Up @@ -160,17 +124,4 @@ private function getPrivateKey()
]
);
}

private function getSimplePrivateKey()
{
return JWKFactory::createFromValues(
[
'kty' => 'RSA',
'kid' => 'private',
'n' => '2NRPORHXd7wPU6atHqmSfWgEPvsP8HVUkY2AwQQAc8x1J509X5HFxeSXnQym9eAnZHl0JCPbvHoPH4QHlvITYoh0MSgFm2aOPyqOD-XcNdKWtnNX2JIurUCyVlwSwtlmy2ZbCz8YuUmFO0iacahfK1wbWT5QoY-pU3UxnMzDhlBslZN5uL7nRE8Sh_8BthsrMdYeGIMY55kh-P7xTs3MHzpOKhFSrOhdN6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