1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-11 16:49:41 +01:00
tgseclib/phpseclib/Crypt/RSA/Formats/Keys/PKCS1.php

154 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* 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-07-29 05:14:37 +02:00
* -----BEGIN RSA PRIVATE KEY-----
* -----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
*/
2019-06-25 05:44:10 +02:00
namespace phpseclib\Crypt\RSA\Formats\Keys;
use phpseclib\Math\BigInteger;
2019-06-25 05:44:10 +02:00
use phpseclib\Crypt\Common\Formats\Keys\PKCS1 as Progenitor;
use phpseclib\File\ASN1;
2016-11-23 06:55:33 +01:00
use phpseclib\File\ASN1\Maps;
/**
* 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
{
/**
* Break a public or private key down into its constituent components
*
* @access public
* @param string $key
* @param string $password optional
2018-10-25 03:00:37 +02:00
* @return array
*/
2016-12-09 23:46:34 +01:00
public static function load($key, $password = '')
{
if (!is_string($key)) {
2018-10-25 03:00:37 +02:00
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}
$components = ['isPublicKey' => strpos($key, 'PUBLIC') !== false];
$key = parent::load($key, $password);
2016-11-23 06:55:33 +01:00
$decoded = ASN1::decodeBER($key);
if (empty($decoded)) {
2018-10-25 03:00:37 +02:00
throw new \RuntimeException('Unable to decode BER');
}
2016-11-23 06:55:33 +01:00
$key = ASN1::asn1map($decoded[0], Maps\RSAPrivateKey::MAP);
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);
2018-10-25 03:00:37 +02:00
if (!is_array($key)) {
throw new \RuntimeException('Unable to perform ASN1 mapping');
}
return $components + $key;
}
/**
* 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
* @param array $options optional
* @return string
*/
2019-06-02 17:02:30 +02:00
public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = [])
{
$num_primes = count($primes);
$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-11-23 06:55:33 +01:00
$key = ASN1::encodeDER($key, Maps\RSAPrivateKey::MAP);
return self::wrapPrivateKey($key, 'RSA', $password, $options);
}
/**
* 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)
{
$key = [
'modulus' => $n,
'publicExponent' => $e
];
2016-11-23 06:55:33 +01:00
$key = ASN1::encodeDER($key, Maps\RSAPublicKey::MAP);
return self::wrapPublicKey($key, 'RSA');
}
}