2016-04-30 23:23:35 +02:00
|
|
|
<?php
|
2016-07-31 15:54:57 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* PKCS#1 Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* Used by File/X509.php
|
|
|
|
*
|
2016-07-29 05:14:37 +02:00
|
|
|
* Processes keys with the following headers:
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
2016-07-29 05:14:37 +02:00
|
|
|
* -----BEGIN RSA PRIVATE KEY-----
|
2016-04-30 23:23:35 +02:00
|
|
|
* -----BEGIN RSA PUBLIC KEY-----
|
|
|
|
*
|
|
|
|
* Analogous to ssh-keygen's pem format (as specified by -m)
|
|
|
|
*
|
|
|
|
* @category Crypt
|
|
|
|
* @package RSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
namespace phpseclib\Crypt\RSA\Keys;
|
2016-04-30 23:23:35 +02:00
|
|
|
|
|
|
|
use phpseclib\Math\BigInteger;
|
2016-12-23 17:02:07 +01:00
|
|
|
use phpseclib\Crypt\Common\Keys\PKCS1 as Progenitor;
|
2016-10-19 14:45:42 +02:00
|
|
|
use phpseclib\File\ASN1;
|
2016-11-23 06:55:33 +01:00
|
|
|
use phpseclib\File\ASN1\Maps;
|
2016-10-19 14:45:42 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* PKCS#1 Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* @package RSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
abstract class PKCS1 extends Progenitor
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
|
|
|
/**
|
2016-10-19 14:45:42 +02:00
|
|
|
* Break a public or private key down into its constituent components
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-10-19 14:45:42 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param string $password optional
|
2017-08-03 09:14:32 +02:00
|
|
|
* @return array|bool
|
2016-04-30 23:23:35 +02:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function load($key, $password = '')
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-10-19 14:45:42 +02:00
|
|
|
if (!is_string($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$components = ['isPublicKey' => strpos($key, 'PUBLIC') !== false];
|
|
|
|
|
|
|
|
$key = parent::load($key, $password);
|
|
|
|
if ($key === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$decoded = ASN1::decodeBER($key);
|
2016-10-19 14:45:42 +02:00
|
|
|
if (empty($decoded)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$key = ASN1::asn1map($decoded[0], Maps\RSAPrivateKey::MAP);
|
2016-10-19 14:45:42 +02:00
|
|
|
if (is_array($key)) {
|
|
|
|
$components+= [
|
|
|
|
'modulus' => $key['modulus'],
|
|
|
|
'publicExponent' => $key['publicExponent'],
|
|
|
|
'privateExponent' => $key['privateExponent'],
|
|
|
|
'primes' => [1 => $key['prime1'], $key['prime2']],
|
|
|
|
'exponents' => [1 => $key['exponent1'], $key['exponent2']],
|
|
|
|
'coefficients' => [2 => $key['coefficient']]
|
|
|
|
];
|
|
|
|
if ($key['version'] == 'multi') {
|
|
|
|
foreach ($key['otherPrimeInfos'] as $primeInfo) {
|
|
|
|
$components['primes'][] = $primeInfo['prime'];
|
|
|
|
$components['exponents'][] = $primeInfo['exponent'];
|
|
|
|
$components['coefficients'][] = $primeInfo['coefficient'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $components;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$key = ASN1::asn1map($decoded[0], Maps\RSAPublicKey::MAP);
|
2016-10-19 14:45:42 +02:00
|
|
|
|
|
|
|
return is_array($key) ? $components + $key : false;
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a private key to the appropriate format.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\BigInteger $n
|
|
|
|
* @param \phpseclib\Math\BigInteger $e
|
|
|
|
* @param \phpseclib\Math\BigInteger $d
|
|
|
|
* @param array $primes
|
|
|
|
* @param array $exponents
|
|
|
|
* @param array $coefficients
|
|
|
|
* @param string $password optional
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, $primes, $exponents, $coefficients, $password = '')
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
|
|
|
$num_primes = count($primes);
|
2016-10-19 14:45:42 +02:00
|
|
|
$key = [
|
|
|
|
'version' => $num_primes == 2 ? 'two-prime' : 'multi',
|
|
|
|
'modulus' => $n,
|
|
|
|
'publicExponent' => $e,
|
|
|
|
'privateExponent' => $d,
|
|
|
|
'prime1' => $primes[1],
|
|
|
|
'prime2' => $primes[2],
|
|
|
|
'exponent1' => $exponents[1],
|
|
|
|
'exponent2' => $exponents[2],
|
|
|
|
'coefficient' => $coefficients[2]
|
|
|
|
];
|
|
|
|
for ($i = 3; $i <= $num_primes; $i++) {
|
|
|
|
$key['otherPrimeInfos'][] = [
|
|
|
|
'prime' => $primes[$i],
|
|
|
|
'exponent' => $exponents[$i],
|
|
|
|
'coefficient' => $coefficients[$i]
|
|
|
|
];
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$key = ASN1::encodeDER($key, Maps\RSAPrivateKey::MAP);
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
return self::wrapPrivateKey($key, 'RSA', $password);
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a public key to the appropriate format
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param \phpseclib\Math\BigInteger $n
|
|
|
|
* @param \phpseclib\Math\BigInteger $e
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function savePublicKey(BigInteger $n, BigInteger $e)
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-10-19 14:45:42 +02:00
|
|
|
$key = [
|
|
|
|
'modulus' => $n,
|
|
|
|
'publicExponent' => $e
|
|
|
|
];
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$key = ASN1::encodeDER($key, Maps\RSAPublicKey::MAP);
|
2016-10-19 14:45:42 +02:00
|
|
|
|
|
|
|
return self::wrapPublicKey($key, 'RSA');
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
}
|