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#8 Formatted RSA Key Handler
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set)
|
|
|
|
*
|
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 ENCRYPTED PRIVATE KEY-----
|
|
|
|
* -----BEGIN PRIVATE KEY-----
|
2016-04-30 23:23:35 +02:00
|
|
|
* -----BEGIN PUBLIC KEY-----
|
|
|
|
*
|
|
|
|
* Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8
|
|
|
|
* is specific to private keys it's basically creating a DER-encoded wrapper
|
|
|
|
* for keys. This just extends that same concept to public keys (much like ssh-keygen)
|
|
|
|
*
|
|
|
|
* @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;
|
2016-04-30 23:23:35 +02:00
|
|
|
|
|
|
|
use phpseclib\Math\BigInteger;
|
2019-06-25 05:44:10 +02:00
|
|
|
use phpseclib\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
|
2016-10-19 14:45:42 +02:00
|
|
|
use phpseclib\File\ASN1;
|
2016-04-30 23:23:35 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-29 07:32:53 +02:00
|
|
|
* PKCS#8 Formatted RSA Key Handler
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
|
|
|
* @package RSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
abstract class PKCS8 extends Progenitor
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
/**
|
|
|
|
* OID Name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
const OID_NAME = 'rsaEncryption';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OID Value
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
const OID_VALUE = '1.2.840.113549.1.1.1';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Child OIDs loaded
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected static $childOIDsLoaded = false;
|
|
|
|
|
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
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param string $password optional
|
2018-10-25 03:00:37 +02:00
|
|
|
* @return array
|
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-12-23 17:02:07 +01:00
|
|
|
if (!is_string($key)) {
|
2018-10-25 03:00:37 +02:00
|
|
|
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
|
2016-12-23 17:02:07 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$components = ['isPublicKey' => strpos($key, 'PUBLIC') !== false];
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$key = parent::load($key, $password);
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$type = isset($key['privateKey']) ? 'private' : 'public';
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$result = $components + PKCS1::load($key[$type . 'Key']);
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
if (isset($key['meta'])) {
|
|
|
|
$result['meta'] = $key['meta'];
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-06-01 20:23:11 +02:00
|
|
|
* @param array $options optional
|
2016-10-19 14:45:42 +02:00
|
|
|
* @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 = [])
|
2016-10-19 14:45:42 +02:00
|
|
|
{
|
|
|
|
$key = PKCS1::savePrivateKey($n, $e, $d, $primes, $exponents, $coefficients);
|
|
|
|
$key = ASN1::extractBER($key);
|
2019-06-01 20:23:11 +02:00
|
|
|
return self::wrapPrivateKey($key, [], null, $password, $options);
|
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
|
2019-06-01 20:23:11 +02:00
|
|
|
* @param array $options optional
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-02 17:02:30 +02:00
|
|
|
public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = [])
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-10-19 14:45:42 +02:00
|
|
|
$key = PKCS1::savePublicKey($n, $e);
|
|
|
|
$key = ASN1::extractBER($key);
|
2016-12-23 17:02:07 +01:00
|
|
|
return self::wrapPublicKey($key, null);
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
2016-08-18 21:53:21 +02:00
|
|
|
}
|