RSA: make XML keys use unsigned integers

PKCS1 / PKCS8 keys need *signed* integers because of section 8.3.3
at http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=7
This commit is contained in:
terrafrost 2014-09-06 11:13:11 -05:00
parent 2ef5a00dff
commit 713393c8ad

View File

@ -742,17 +742,18 @@ class Crypt_RSA
*/ */
function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients) function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients)
{ {
$unsigned = $this->privateKeyFormat == CRYPT_RSA_PRIVATE_FORMAT_XML;
$num_primes = count($primes); $num_primes = count($primes);
$raw = array( $raw = array(
'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi 'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi
'modulus' => $n->toBytes(true), 'modulus' => $n->toBytes($unsigned),
'publicExponent' => $e->toBytes(true), 'publicExponent' => $e->toBytes($unsigned),
'privateExponent' => $d->toBytes(true), 'privateExponent' => $d->toBytes($unsigned),
'prime1' => $primes[1]->toBytes(true), 'prime1' => $primes[1]->toBytes($unsigned),
'prime2' => $primes[2]->toBytes(true), 'prime2' => $primes[2]->toBytes($unsigned),
'exponent1' => $exponents[1]->toBytes(true), 'exponent1' => $exponents[1]->toBytes($unsigned),
'exponent2' => $exponents[2]->toBytes(true), 'exponent2' => $exponents[2]->toBytes($unsigned),
'coefficient' => $coefficients[2]->toBytes(true) 'coefficient' => $coefficients[2]->toBytes($unsigned)
); );
// if the format in question does not support multi-prime rsa and multi-prime rsa was used, // if the format in question does not support multi-prime rsa and multi-prime rsa was used,
@ -941,8 +942,10 @@ class Crypt_RSA
*/ */
function _convertPublicKey($n, $e) function _convertPublicKey($n, $e)
{ {
$modulus = $n->toBytes(true); $unsigned = $this->publicKeyFormat == CRYPT_RSA_PUBLIC_FORMAT_XML;
$publicExponent = $e->toBytes(true);
$modulus = $n->toBytes($unsigned);
$publicExponent = $e->toBytes($unsigned);
switch ($this->publicKeyFormat) { switch ($this->publicKeyFormat) {
case CRYPT_RSA_PUBLIC_FORMAT_RAW: case CRYPT_RSA_PUBLIC_FORMAT_RAW: