RSA/Keys/Raw: add support for private keys

This commit is contained in:
terrafrost 2021-11-21 21:22:34 -06:00
parent ea0e71977e
commit 84295e2fc2
2 changed files with 132 additions and 34 deletions

View File

@ -49,48 +49,125 @@ abstract class Raw
if (!is_array($key)) { if (!is_array($key)) {
throw new \UnexpectedValueException('Key should be a array - not a ' . gettype($key)); throw new \UnexpectedValueException('Key should be a array - not a ' . gettype($key));
} }
if (isset($key['isPublicKey']) && isset($key['modulus'])) {
if (isset($key['privateExponent']) || isset($key['publicExponent'])) { $key = array_change_key_case($key, CASE_LOWER);
if (!isset($key['primes'])) {
return $key; $components = ['isPublicKey' => false];
foreach (['e', 'exponent', 'publicexponent', 0, 'privateexponent', 'd'] as $index) {
if (isset($key[$index])) {
$components['publicExponent'] = $key[$index];
break;
} }
if (isset($key['exponents']) && isset($key['coefficients']) && isset($key['publicExponent']) && isset($key['privateExponent'])) { }
return $key;
foreach (['n', 'modulo', 'modulus', 1] as $index) {
if (isset($key[$index])) {
$components['modulus'] = $key[$index];
break;
}
}
if (!isset($components['publicExponent']) || !isset($components['modulus'])) {
throw new \UnexpectedValueException('Modulus / exponent not present');
}
if (isset($key['primes'])) {
$components['primes'] = $key['primes'];
} else if (isset($key['p']) && isset($key['q'])) {
$indices = [
['p', 'q'],
['prime1', 'prime2']
];
foreach ($indices as $index) {
list($i0, $i1) = $index;
if (isset($key[$i0]) && isset($key[$i1])) {
$components['primes'] = [1 => $key[$i0], $key[$i1]];
} }
} }
} }
$components = ['isPublicKey' => true];
switch (true) { if (isset($key['exponents'])) {
case isset($key['e']): $components['exponents'] = $key['exponents'];
$components['publicExponent'] = $key['e']; } else {
break; $indices = [
case isset($key['exponent']): ['dp', 'dq'],
$components['publicExponent'] = $key['exponent']; ['exponent1', 'exponent2']
break; ];
case isset($key['publicExponent']): foreach ($indices as $index) {
$components['publicExponent'] = $key['publicExponent']; list($i0, $i1) = $index;
break; if (isset($key[$i0]) && isset($key[$i1])) {
case isset($key[0]): $components['exponents'] = [1 => $key[$i0], $key[$i1]];
$components['publicExponent'] = $key[0];
} }
switch (true) {
case isset($key['n']):
$components['modulus'] = $key['n'];
break;
case isset($key['modulo']):
$components['modulus'] = $key['modulo'];
break;
case isset($key['modulus']):
$components['modulus'] = $key['modulus'];
break;
case isset($key[1]):
$components['modulus'] = $key[1];
} }
if (isset($components['modulus']) && isset($components['publicExponent'])) { }
if (isset($key['coefficients'])) {
$components['coefficients'] = $key['coefficients'];
} else {
foreach (['inverseq', 'q\'', 'coefficient'] as $index) {
if (isset($key[$index])) {
$components['coefficients'] = [2 => $key[$index]];
}
}
}
if (!isset($components['primes'])) {
$components['isPublicKey'] = true;
return $components; return $components;
} }
throw new \UnexpectedValueException('Modulus / exponent not present'); if (!isset($components['exponents'])) {
$one = new BigInteger(1);
$temp = $components['primes'][1]->subtract($one);
$exponents = [1 => $components['publicExponent']->modInverse($temp)];
$temp = $components['primes'][2]->subtract($one);
$exponents[] = $components['publicExponent']->modInverse($temp);
$components['exponents'] = $exponents;
}
if (!isset($components['coefficients'])) {
$components['coefficients'] = [2 => $components['primes'][2]->modInverse($components['primes'][1])];
}
foreach (['privateexponent', 'd'] as $index) {
if (isset($key[$index])) {
$components['privateExponent'] = $key[$index];
break;
}
}
return $components;
}
/**
* Convert a private key to the appropriate format.
*
* @access public
* @param \phpseclib3\Math\BigInteger $n
* @param \phpseclib3\Math\BigInteger $e
* @param \phpseclib3\Math\BigInteger $d
* @param array $primes
* @param array $exponents
* @param array $coefficients
* @param string $password optional
* @param array $options optional
* @return array
*/
public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = [])
{
if (!empty($password) && is_string($password)) {
throw new UnsupportedFormatException('Raw private keys do not support encryption');
}
return [
'e' => clone $e,
'n' => clone $n,
'd' => clone $d,
'primes' => array_map(function($var) { return clone $var; }, $primes),
'exponents' => array_map(function($var) { return clone $var; }, $exponents),
'coefficients' => array_map(function($var) { return clone $var; }, $coefficients)
];
} }
/** /**

View File

@ -1126,4 +1126,25 @@ n9dyFZYXxil/cgFG/PDMnuXy1Wcl8hb8iwQag4Y7ohiLXVTJa/0BAgMBAAE=
$key = PublicKeyLoader::load(hex2bin($key)); $key = PublicKeyLoader::load(hex2bin($key));
$this->assertInstanceOf(PublicKey::class, $key); $this->assertInstanceOf(PublicKey::class, $key);
} }
/**
* @group github1711
*/
public function testRawPrivateKey()
{
$key = RSA::createKey(512);
$str1 = "$key";
$key = $key->toString('Raw');
$key = [
'e' => $key['e'],
'n' => $key['n'],
'd' => $key['d'],
'p' => $key['primes'][1],
'q' => $key['primes'][2]
];
$key = PublicKeyLoader::loadPrivateKey($key);
$str2 = "$key";
$this->assertSame($str1, $str2);
}
} }