2009-12-03 09:19:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP PKCS#1 (v2.1) compliant implementation of RSA.
|
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* Here's an example of how to encrypt and decrypt text with this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2016-04-30 23:23:35 +02:00
|
|
|
* include 'vendor/autoload.php';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* extract(\phpseclib\Crypt\RSA::createKey());
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* $plaintext = 'terrafrost';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* $ciphertext = $publickey->encrypt($plaintext);
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* echo $privatekey->decrypt($ciphertext);
|
2009-12-03 09:19:00 +01:00
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Here's an example of how to create signatures and verify signatures with this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2016-04-30 23:23:35 +02:00
|
|
|
* include 'vendor/autoload.php';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* extract(\phpseclib\Crypt\RSA::createKey());
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* $plaintext = 'terrafrost';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* $signature = $privatekey->sign($plaintext);
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* echo $publickey->verify($plaintext, $signature) ? 'verified' : 'unverified';
|
2009-12-03 09:19:00 +01:00
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package RSA
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2009 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
namespace phpseclib\Crypt;
|
2014-06-02 20:09:47 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
use phpseclib\File\ASN1;
|
2016-01-18 05:05:31 +01:00
|
|
|
use phpseclib\Math\BigInteger;
|
2016-07-31 05:18:06 +02:00
|
|
|
use phpseclib\Common\Functions\Strings;
|
2016-11-23 06:55:33 +01:00
|
|
|
use phpseclib\File\ASN1\Maps\DigestInfo;
|
2016-12-23 17:02:07 +01:00
|
|
|
use phpseclib\Crypt\Common\AsymmetricKey;
|
|
|
|
use phpseclib\Exception\UnsupportedAlgorithmException;
|
2018-02-15 12:29:14 +01:00
|
|
|
use phpseclib\Exception\UnsupportedOperationException;
|
|
|
|
use phpseclib\Exception\NoKeyLoadedException;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP PKCS#1 compliant implementation of RSA.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package RSA
|
2009-12-03 09:19:00 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-23 17:02:07 +01:00
|
|
|
class RSA extends AsymmetricKey
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
/**
|
|
|
|
* Algorithm Name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
const ALGORITHM = 'RSA';
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding}
|
|
|
|
* (OAEP) for encryption / decryption.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Uses sha256 by default
|
2014-12-09 19:46:30 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setHash()
|
|
|
|
* @see self::setMGFHash()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
const PADDING_OAEP = 1;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Use PKCS#1 padding.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Although self::PADDING_OAEP / self::PADDING_PSS offers more security, including PKCS#1 padding is necessary for purposes of backwards
|
2014-12-09 19:46:30 +01:00
|
|
|
* compatibility with protocols (like SSH-1) written before OAEP's introduction.
|
2016-04-10 18:30:59 +02:00
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
const PADDING_PKCS1 = 2;
|
2015-05-26 05:36:10 +02:00
|
|
|
/**
|
|
|
|
* Do not use any padding
|
|
|
|
*
|
|
|
|
* Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy
|
|
|
|
* stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc.
|
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
const PADDING_NONE = 3;
|
|
|
|
/**
|
2016-07-31 14:41:23 +02:00
|
|
|
* Use PKCS#1 padding with PKCS1 v1.5 compatibility
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
|
|
|
* A PKCS1 v2.1 encrypted message may not successfully decrypt with a PKCS1 v1.5 implementation (such as OpenSSL).
|
|
|
|
*/
|
|
|
|
const PADDING_PKCS15_COMPAT = 6;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::sign()
|
|
|
|
* @see self::verify()
|
|
|
|
* @see self::setHash()
|
2016-04-30 23:23:35 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Use the Probabilistic Signature Scheme for signing
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Uses sha256 and 0 as the salt length
|
2014-12-09 19:46:30 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setSaltLength()
|
|
|
|
* @see self::setMGFHash()
|
2016-04-30 23:23:35 +02:00
|
|
|
* @see self::setHash()
|
2016-04-10 18:30:59 +02:00
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
const PADDING_PSS = 4;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Use a relaxed version of PKCS#1 padding for signature verification
|
2016-04-10 18:30:59 +02:00
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
const PADDING_RELAXED_PKCS1 = 5;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Modulus (ie. n)
|
|
|
|
*
|
2014-06-02 20:09:47 +02:00
|
|
|
* @var \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $modulus;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Modulus length
|
|
|
|
*
|
2014-06-02 20:09:47 +02:00
|
|
|
* @var \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $k;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponent (ie. e or d)
|
|
|
|
*
|
2014-06-02 20:09:47 +02:00
|
|
|
* @var \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $exponent;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Primes for Chinese Remainder Theorem (ie. p and q)
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $primes;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponents for Chinese Remainder Theorem (ie. dP and dQ)
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $exponents;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Coefficients for Chinese Remainder Theorem (ie. qInv)
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $coefficients;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash name
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $hashName;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of hash function output
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $hLen;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of salt
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $sLen;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function for the Mask Generation Function
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @var \phpseclib\Crypt\Hash
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $mgfHash;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2009-12-08 15:18:59 +01:00
|
|
|
/**
|
|
|
|
* Length of MGF hash function output
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2009-12-08 15:18:59 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $mgfHLen;
|
2009-12-08 15:18:59 +01:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
2016-04-10 18:30:59 +02:00
|
|
|
* Public Exponent
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var mixed
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private $publicExponent = false;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
/**
|
|
|
|
* Public exponent
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @link http://en.wikipedia.org/wiki/65537_%28number%29
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private static $defaultExponent = 65537;
|
2016-10-19 14:45:42 +02:00
|
|
|
|
2018-02-15 12:29:14 +01:00
|
|
|
/**
|
|
|
|
* Is the loaded key a public key?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $isPublic = false;
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
/**
|
|
|
|
* Smallest Prime
|
|
|
|
*
|
|
|
|
* Per <http://cseweb.ucsd.edu/~hovav/dist/survey.pdf#page=5>, this number ought not result in primes smaller
|
|
|
|
* than 256 bits. As a consequence if the key you're trying to create is 1024 bits and you've set smallestPrime
|
|
|
|
* to 384 bits then you're going to get a 384 bit prime and a 640 bit prime (384 + 1024 % 384). At least if
|
|
|
|
* engine is set to self::ENGINE_INTERNAL. If Engine is set to self::ENGINE_OPENSSL then smallest Prime is
|
|
|
|
* ignored (ie. multi-prime RSA support is more intended as a way to speed up RSA key generation when there's
|
|
|
|
* a chance neither gmp nor OpenSSL are installed)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private static $smallestPrime = 4096;
|
2016-10-19 14:45:42 +02:00
|
|
|
|
2017-06-27 06:19:30 +02:00
|
|
|
/**
|
|
|
|
* Enable Blinding?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private static $enableBlinding = true;
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* The constructor
|
|
|
|
*
|
|
|
|
* If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason
|
2014-12-17 01:16:54 +01:00
|
|
|
* \phpseclib\Crypt\RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires
|
2009-12-03 09:19:00 +01:00
|
|
|
* openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @return \phpseclib\Crypt\RSA
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function __construct()
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
parent::__construct();
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
//$this->hash = new Hash('sha256');
|
2016-10-19 14:45:42 +02:00
|
|
|
$this->hLen = $this->hash->getLengthInBytes();
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->hashName = 'sha256';
|
|
|
|
$this->mgfHash = new Hash('sha256');
|
2016-10-19 14:45:42 +02:00
|
|
|
$this->mgfHLen = $this->mgfHash->getLengthInBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the public exponent
|
|
|
|
*
|
|
|
|
* This will be 65537 unless changed.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $val
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function setExponent($val)
|
2016-10-19 14:45:42 +02:00
|
|
|
{
|
|
|
|
self::$defaultExponent = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the smallest prime number in bits
|
|
|
|
*
|
|
|
|
* This will be 4096 unless changed.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $val
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function setSmallestPrime($val)
|
2016-10-19 14:45:42 +02:00
|
|
|
{
|
|
|
|
self::$smallestPrime = $val;
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* Create public / private key pair
|
|
|
|
*
|
2016-12-23 17:02:07 +01:00
|
|
|
* Returns an array with the following two elements:
|
2016-04-30 23:23:35 +02:00
|
|
|
* - 'privatekey': The private key.
|
|
|
|
* - 'publickey': The public key.
|
|
|
|
*
|
2017-10-25 11:44:14 +02:00
|
|
|
* @return array
|
2016-04-30 23:23:35 +02:00
|
|
|
* @access public
|
|
|
|
* @param int $bits
|
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public static function createKey($bits = 2048)
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-12-09 23:46:34 +01:00
|
|
|
self::initialize_static_variables();
|
2012-12-10 12:07:49 +01:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
static $e;
|
|
|
|
if (!isset($e)) {
|
2016-10-19 14:45:42 +02:00
|
|
|
$e = new BigInteger(self::$defaultExponent);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$regSize = $bits >> 1; // divide by two to see how many bits P and Q would be
|
|
|
|
if ($regSize > self::$smallestPrime) {
|
|
|
|
$num_primes = floor($bits / self::$smallestPrime);
|
|
|
|
$regSize = self::$smallestPrime;
|
2009-12-03 09:19:00 +01:00
|
|
|
} else {
|
|
|
|
$num_primes = 2;
|
|
|
|
}
|
2016-04-28 22:34:10 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
$n = clone self::$one;
|
2016-11-30 22:19:23 +01:00
|
|
|
$exponents = $coefficients = $primes = [];
|
|
|
|
$lcm = [
|
2016-10-19 14:45:42 +02:00
|
|
|
'top' => clone self::$one,
|
|
|
|
'bottom' => false
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
do {
|
2016-10-19 14:45:42 +02:00
|
|
|
for ($i = 1; $i <= $num_primes; $i++) {
|
|
|
|
if ($i != $num_primes) {
|
|
|
|
$primes[$i] = BigInteger::randomPrime($regSize);
|
|
|
|
} else {
|
|
|
|
extract(BigInteger::minMaxBits($bits));
|
2017-12-04 11:11:43 +01:00
|
|
|
/** @var BigInteger $min
|
|
|
|
* @var BigInteger $max
|
|
|
|
*/
|
2016-10-19 14:45:42 +02:00
|
|
|
list($min) = $min->divide($n);
|
|
|
|
$min = $min->add(self::$one);
|
|
|
|
list($max) = $max->divide($n);
|
|
|
|
$primes[$i] = BigInteger::randomRangePrime($min, $max);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// the first coefficient is calculated differently from the rest
|
|
|
|
// ie. instead of being $primes[1]->modInverse($primes[2]), it's $primes[2]->modInverse($primes[1])
|
|
|
|
if ($i > 2) {
|
|
|
|
$coefficients[$i] = $n->modInverse($primes[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$n = $n->multiply($primes[$i]);
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
$temp = $primes[$i]->subtract(self::$one);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// textbook RSA implementations use Euler's totient function instead of the least common multiple.
|
|
|
|
// see http://en.wikipedia.org/wiki/Euler%27s_totient_function
|
|
|
|
$lcm['top'] = $lcm['top']->multiply($temp);
|
|
|
|
$lcm['bottom'] = $lcm['bottom'] === false ? $temp : $lcm['bottom']->gcd($temp);
|
|
|
|
}
|
|
|
|
|
2013-08-01 04:50:40 +02:00
|
|
|
list($temp) = $lcm['top']->divide($lcm['bottom']);
|
|
|
|
$gcd = $temp->gcd($e);
|
2009-12-03 09:19:00 +01:00
|
|
|
$i0 = 1;
|
2016-04-30 23:23:35 +02:00
|
|
|
} while (!$gcd->equals(self::$one));
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$coefficients[2] = $primes[2]->modInverse($primes[1]);
|
|
|
|
|
2013-08-01 04:50:40 +02:00
|
|
|
$d = $e->modInverse($temp);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
foreach ($primes as $i => $prime) {
|
|
|
|
$temp = $prime->subtract(self::$one);
|
|
|
|
$exponents[$i] = $e->modInverse($temp);
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// from <http://tools.ietf.org/html/rfc3447#appendix-A.1.2>:
|
|
|
|
// RSAPrivateKey ::= SEQUENCE {
|
|
|
|
// version Version,
|
|
|
|
// modulus INTEGER, -- n
|
|
|
|
// publicExponent INTEGER, -- e
|
|
|
|
// privateExponent INTEGER, -- d
|
|
|
|
// prime1 INTEGER, -- p
|
|
|
|
// prime2 INTEGER, -- q
|
|
|
|
// exponent1 INTEGER, -- d mod (p-1)
|
|
|
|
// exponent2 INTEGER, -- d mod (q-1)
|
|
|
|
// coefficient INTEGER, -- (inverse of q) mod p
|
|
|
|
// otherPrimeInfos OtherPrimeInfos OPTIONAL
|
|
|
|
// }
|
2016-04-30 23:23:35 +02:00
|
|
|
$privatekey = new RSA();
|
|
|
|
$privatekey->modulus = $n;
|
|
|
|
$privatekey->k = $bits >> 3;
|
|
|
|
$privatekey->publicExponent = $e;
|
|
|
|
$privatekey->exponent = $d;
|
|
|
|
$privatekey->privateExponent = $e;
|
|
|
|
$privatekey->primes = $primes;
|
|
|
|
$privatekey->exponents = $exponents;
|
|
|
|
$privatekey->coefficients = $coefficients;
|
|
|
|
|
|
|
|
$publickey = new RSA();
|
|
|
|
$publickey->modulus = $n;
|
|
|
|
$publickey->k = $bits >> 3;
|
|
|
|
$publickey->exponent = $e;
|
2016-12-23 17:02:07 +01:00
|
|
|
$publickey->publicExponent = $e;
|
2018-02-15 12:29:14 +01:00
|
|
|
$publickey->isPublic = true;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
return compact('privatekey', 'publickey');
|
2015-10-22 17:57:05 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Loads a public or private key
|
|
|
|
*
|
2009-12-08 00:22:05 +01:00
|
|
|
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
|
|
|
|
*
|
2017-11-05 21:35:27 +01:00
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2018-04-25 22:18:51 +02:00
|
|
|
* @param string|RSA|array $key
|
2019-03-08 15:02:54 +01:00
|
|
|
* @param int|bool $type optional
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function load($key, $type = false)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2014-12-24 22:05:56 +01:00
|
|
|
if ($key instanceof RSA) {
|
2013-10-18 20:24:06 +02:00
|
|
|
$this->privateKeyFormat = $key->privateKeyFormat;
|
|
|
|
$this->publicKeyFormat = $key->publicKeyFormat;
|
2016-12-23 17:02:07 +01:00
|
|
|
$this->format = $key->format;
|
2013-10-18 20:24:06 +02:00
|
|
|
$this->k = $key->k;
|
|
|
|
$this->hLen = $key->hLen;
|
|
|
|
$this->sLen = $key->sLen;
|
|
|
|
$this->mgfHLen = $key->mgfHLen;
|
|
|
|
$this->password = $key->password;
|
2018-02-15 12:29:14 +01:00
|
|
|
$this->isPublic = $key->isPublic;
|
2013-10-18 20:24:06 +02:00
|
|
|
|
|
|
|
if (is_object($key->hash)) {
|
2018-10-25 03:00:37 +02:00
|
|
|
$this->hashName = $key->hash->getHash();
|
|
|
|
$this->hash = new Hash($this->hashName);
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
if (is_object($key->mgfHash)) {
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->mgfHash = new Hash($key->mgfHash->getHash());
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($key->modulus)) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->modulus = clone $key->modulus;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
if (is_object($key->exponent)) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->exponent = clone $key->exponent;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
if (is_object($key->publicExponent)) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->publicExponent = clone $key->publicExponent;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:19:23 +01:00
|
|
|
$this->primes = [];
|
|
|
|
$this->exponents = [];
|
|
|
|
$this->coefficients = [];
|
2013-10-18 20:24:06 +02:00
|
|
|
|
|
|
|
foreach ($this->primes as $prime) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->primes[] = clone $prime;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
foreach ($this->exponents as $exponent) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->exponents[] = clone $exponent;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
foreach ($this->coefficients as $coefficient) {
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->coefficients[] = clone $coefficient;
|
2013-10-18 20:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
$components = parent::load($key, $type);
|
2009-12-06 08:26:52 +01:00
|
|
|
if ($components === false) {
|
2017-05-29 13:31:54 +02:00
|
|
|
$this->comment = null;
|
|
|
|
$this->modulus = null;
|
|
|
|
$this->k = null;
|
|
|
|
$this->exponent = null;
|
|
|
|
$this->primes = null;
|
|
|
|
$this->exponents = null;
|
|
|
|
$this->coefficients = null;
|
|
|
|
$this->publicExponent = null;
|
2018-10-25 03:00:37 +02:00
|
|
|
$this->isPublic = false;
|
2017-05-29 13:31:54 +02:00
|
|
|
|
2009-12-06 08:26:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:29:14 +01:00
|
|
|
$this->isPublic = false;
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->modulus = $components['modulus'];
|
2016-10-19 14:45:42 +02:00
|
|
|
$this->k = $this->modulus->getLengthInBytes();
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent'];
|
|
|
|
if (isset($components['primes'])) {
|
|
|
|
$this->primes = $components['primes'];
|
|
|
|
$this->exponents = $components['exponents'];
|
|
|
|
$this->coefficients = $components['coefficients'];
|
|
|
|
$this->publicExponent = $components['publicExponent'];
|
|
|
|
} else {
|
2016-11-30 22:19:23 +01:00
|
|
|
$this->primes = [];
|
|
|
|
$this->exponents = [];
|
|
|
|
$this->coefficients = [];
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->publicExponent = false;
|
|
|
|
}
|
2009-12-08 00:22:05 +01:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($components['isPublicKey']) {
|
|
|
|
$this->setPublicKey();
|
2014-04-17 17:30:32 +02:00
|
|
|
}
|
|
|
|
|
2009-12-08 00:22:05 +01:00
|
|
|
return true;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the private key
|
|
|
|
*
|
|
|
|
* The private key is only returned if the currently loaded key contains the constituent prime numbers.
|
|
|
|
*
|
|
|
|
* @see self::getPublicKey()
|
|
|
|
* @access public
|
|
|
|
* @param string $type optional
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-12-23 17:02:07 +01:00
|
|
|
public function getPrivateKey($type = 'PKCS8')
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
$type = self::validatePlugin('Keys', $type, 'savePrivateKey');
|
|
|
|
if ($type === false) {
|
2016-04-30 23:23:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->primes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:08:47 +02:00
|
|
|
return $type::savePrivateKey($this->modulus, $this->publicExponent, $this->exponent, $this->primes, $this->exponents, $this->coefficients, $this->password);
|
2016-10-19 14:45:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
$key = $type::savePrivateKey($this->modulus, $this->publicExponent, $this->exponent, $this->primes, $this->exponents, $this->coefficients, $this->password);
|
|
|
|
if ($key !== false || count($this->primes) == 2) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nSize = $this->getSize() >> 1;
|
|
|
|
|
|
|
|
$primes = [1 => clone self::$one, clone self::$one];
|
|
|
|
$i = 1;
|
|
|
|
foreach ($this->primes as $prime) {
|
|
|
|
$primes[$i] = $primes[$i]->multiply($prime);
|
|
|
|
if ($primes[$i]->getLength() >= $nSize) {
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$exponents = [];
|
|
|
|
$coefficients = [2 => $primes[2]->modInverse($primes[1])];
|
|
|
|
|
|
|
|
foreach ($primes as $i => $prime) {
|
|
|
|
$temp = $prime->subtract(self::$one);
|
|
|
|
$exponents[$i] = $this->modulus->modInverse($temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type::savePrivateKey($this->modulus, $this->publicExponent, $this->exponent, $primes, $exponents, $coefficients, $this->password);
|
|
|
|
*/
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-23 17:02:07 +01:00
|
|
|
* Returns a minimalistic private key
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
2018-03-03 13:07:14 +01:00
|
|
|
* Returns the private key without the prime number constituents. Structurally identical to a public key that
|
2016-12-23 17:02:07 +01:00
|
|
|
* hasn't been set as the public key
|
2016-04-30 23:23:35 +02:00
|
|
|
*
|
2016-12-23 17:02:07 +01:00
|
|
|
* @see self::getPrivateKey()
|
|
|
|
* @access private
|
|
|
|
* @param string $type optional
|
|
|
|
* @return mixed
|
2016-04-30 23:23:35 +02:00
|
|
|
*/
|
2016-12-23 17:02:07 +01:00
|
|
|
protected function getPrivatePublicKey($type = 'PKCS8')
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
$type = self::validatePlugin('Keys', $type, 'savePublicKey');
|
|
|
|
if ($type === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldFormat = $this->publicKeyFormat;
|
|
|
|
$this->publicKeyFormat = $type;
|
|
|
|
$temp = $type::savePublicKey($this->modulus, $this->exponent);
|
|
|
|
$this->publicKeyFormat = $oldFormat;
|
|
|
|
return $temp;
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
2016-12-23 17:02:07 +01:00
|
|
|
* Returns the key size
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-12-23 17:02:07 +01:00
|
|
|
* More specifically, this returns the size of the modulo in bits.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2016-12-23 17:02:07 +01:00
|
|
|
* @return int
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-23 17:02:07 +01:00
|
|
|
public function getLength()
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-12-23 17:02:07 +01:00
|
|
|
return !isset($this->modulus) ? 0 : $this->modulus->getLength();
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the current engine being used
|
|
|
|
*
|
|
|
|
* @see self::useInternalEngine()
|
|
|
|
* @see self::useBestEngine()
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEngine()
|
|
|
|
{
|
|
|
|
return 'PHP';
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Defines the public key
|
|
|
|
*
|
|
|
|
* Some private key formats define the public exponent and some don't. Those that don't define it are problematic when
|
|
|
|
* used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a
|
|
|
|
* message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys
|
|
|
|
* and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public
|
2014-04-17 17:30:32 +02:00
|
|
|
* exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used
|
|
|
|
* is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being
|
|
|
|
* public.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* Do note that when a new key is loaded the index will be cleared.
|
|
|
|
*
|
|
|
|
* Returns true on success, false on failure
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::getPublicKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2017-11-05 21:35:27 +01:00
|
|
|
* @param string|bool $key optional
|
|
|
|
* @param int|bool $type optional
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function setPublicKey($key = false, $type = false)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2013-10-06 04:30:57 +02:00
|
|
|
// if a public key has already been loaded return false
|
2013-10-06 09:23:57 +02:00
|
|
|
if (!empty($this->publicExponent)) {
|
2013-10-06 04:30:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:21:42 +02:00
|
|
|
if ($key === false && !empty($this->modulus)) {
|
2018-02-15 12:29:14 +01:00
|
|
|
$this->isPublic = true;
|
2012-04-17 08:21:42 +02:00
|
|
|
$this->publicExponent = $this->exponent;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
$components = parent::setPublicKey($key, $type);
|
2012-04-17 08:21:42 +02:00
|
|
|
if ($components === false) {
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-19 14:01:21 +02:00
|
|
|
|
2012-04-17 08:21:42 +02:00
|
|
|
if (empty($this->modulus) || !$this->modulus->equals($components['modulus'])) {
|
|
|
|
$this->modulus = $components['modulus'];
|
|
|
|
$this->exponent = $this->publicExponent = $components['publicExponent'];
|
2018-02-15 12:29:14 +01:00
|
|
|
$this->isPublic = true;
|
2012-04-17 08:21:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-04 21:50:21 +01:00
|
|
|
$this->publicExponent = $components['publicExponent'];
|
2010-06-19 14:01:21 +02:00
|
|
|
|
|
|
|
return true;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:29:14 +01:00
|
|
|
/**
|
2018-10-25 03:00:37 +02:00
|
|
|
* Is the key a public key?
|
2018-02-15 12:29:14 +01:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPublicKey()
|
|
|
|
{
|
2018-10-25 03:00:37 +02:00
|
|
|
return $this->isPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the key a private key?
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPrivateKey()
|
|
|
|
{
|
|
|
|
return !$this->isPublic && isset($this->modulus);
|
2018-02-15 12:29:14 +01:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Defines the private key
|
|
|
|
*
|
|
|
|
* If phpseclib guessed a private key was a public key and loaded it as such it might be desirable to force
|
|
|
|
* phpseclib to treat the key as a private key. This function will do that.
|
|
|
|
*
|
|
|
|
* Do note that when a new key is loaded the index will be cleared.
|
|
|
|
*
|
|
|
|
* Returns true on success, false on failure
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::getPublicKey()
|
2014-04-17 17:30:32 +02:00
|
|
|
* @access public
|
2017-11-05 21:35:27 +01:00
|
|
|
* @param string|bool $key optional
|
|
|
|
* @param int|bool $type optional
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2014-04-17 17:30:32 +02:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function setPrivateKey($key = false, $type = false)
|
2014-04-17 17:30:32 +02:00
|
|
|
{
|
|
|
|
if ($key === false && !empty($this->publicExponent)) {
|
2016-04-10 18:30:59 +02:00
|
|
|
$this->publicExponent = false;
|
2018-10-25 03:00:37 +02:00
|
|
|
$this->isPublic = false;
|
2014-04-17 17:30:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
$rsa = new RSA();
|
2016-04-30 23:23:35 +02:00
|
|
|
if (!$rsa->load($key, $type)) {
|
2014-04-17 17:30:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-10 18:30:59 +02:00
|
|
|
$rsa->publicExponent = false;
|
2018-10-25 03:00:37 +02:00
|
|
|
$rsa->isPublic = false;
|
2014-04-17 17:30:32 +02:00
|
|
|
|
|
|
|
// don't overwrite the old key if the new key is invalid
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->load($rsa);
|
2014-04-17 17:30:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Returns the public key
|
|
|
|
*
|
|
|
|
* The public key is only returned under two circumstances - if the private key had the public key embedded within it
|
|
|
|
* or if the public key was set via setPublicKey(). If the currently loaded key is supposed to be the public key this
|
|
|
|
* function won't return it since this library, for the most part, doesn't distinguish between public and private keys.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* @see self::getPrivateKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param string $type optional
|
|
|
|
* @return mixed
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2018-10-25 03:00:37 +02:00
|
|
|
public function getPublicKey($type = null)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2018-10-25 03:00:37 +02:00
|
|
|
$returnObj = false;
|
|
|
|
if ($type === null) {
|
|
|
|
$returnObj = true;
|
|
|
|
$type = 'PKCS8';
|
|
|
|
}
|
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
$type = self::validatePlugin('Keys', $type, 'savePublicKey');
|
|
|
|
if ($type === false) {
|
2016-04-30 23:23:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->publicExponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
$key = $type::savePublicKey($this->modulus, $this->publicExponent);
|
|
|
|
if (!$returnObj) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$public = clone $this;
|
|
|
|
$public->load($key, 'PKCS8');
|
|
|
|
|
|
|
|
return $public;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2012-07-26 20:14:18 +02:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* __toString() magic method
|
2012-07-26 20:14:18 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2012-07-26 20:14:18 +02:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function __toString()
|
2012-07-26 20:14:18 +02:00
|
|
|
{
|
2016-10-19 14:45:42 +02:00
|
|
|
try {
|
|
|
|
$key = $this->getPrivateKey($this->privateKeyFormat);
|
|
|
|
if (is_string($key)) {
|
|
|
|
return $key;
|
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
$key = $this->getPrivatePublicKey($this->publicKeyFormat);
|
2016-10-19 14:45:42 +02:00
|
|
|
return is_string($key) ? $key : '';
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return '';
|
2012-07-26 20:14:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Determines which hashing function should be used
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Used with signature production / verification and (if the encryption mode is self::PADDING_OAEP) encryption and
|
2016-12-23 17:02:07 +01:00
|
|
|
* decryption.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $hash
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function setHash($hash)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2014-12-17 01:16:54 +01:00
|
|
|
// \phpseclib\Crypt\Hash supports algorithms that PKCS#1 doesn't support. md5-96 and sha1-96, for example.
|
2016-12-23 17:02:07 +01:00
|
|
|
switch (strtolower($hash)) {
|
2009-12-03 09:19:00 +01:00
|
|
|
case 'md2':
|
|
|
|
case 'md5':
|
|
|
|
case 'sha1':
|
|
|
|
case 'sha256':
|
|
|
|
case 'sha384':
|
|
|
|
case 'sha512':
|
2016-04-30 23:23:35 +02:00
|
|
|
case 'sha224':
|
|
|
|
case 'sha512/224':
|
|
|
|
case 'sha512/256':
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->hash = new Hash($hash);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->hashName = $hash;
|
|
|
|
break;
|
|
|
|
default:
|
2016-12-23 17:02:07 +01:00
|
|
|
throw new UnsupportedAlgorithmException(
|
|
|
|
'The only supported hash algorithms are: md2, md5, sha1, sha256, sha384, sha512, sha224, sha512/224, sha512/256'
|
|
|
|
);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-10-19 14:45:42 +02:00
|
|
|
$this->hLen = $this->hash->getLengthInBytes();
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which hashing function should be used for the mask generation function
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* The mask generation function is used by self::PADDING_OAEP and self::PADDING_PSS and although it's
|
2009-12-03 09:19:00 +01:00
|
|
|
* best if Hash and MGFHash are set to the same thing this is not a requirement.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $hash
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function setMGFHash($hash)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2014-12-17 01:16:54 +01:00
|
|
|
// \phpseclib\Crypt\Hash supports algorithms that PKCS#1 doesn't support. md5-96 and sha1-96, for example.
|
2009-12-03 09:19:00 +01:00
|
|
|
switch ($hash) {
|
|
|
|
case 'md2':
|
|
|
|
case 'md5':
|
|
|
|
case 'sha1':
|
|
|
|
case 'sha256':
|
|
|
|
case 'sha384':
|
|
|
|
case 'sha512':
|
2016-04-30 23:23:35 +02:00
|
|
|
case 'sha224':
|
|
|
|
case 'sha512/224':
|
|
|
|
case 'sha512/256':
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->mgfHash = new Hash($hash);
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->mgfHash = new Hash('sha256');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-10-19 14:45:42 +02:00
|
|
|
$this->mgfHLen = $this->mgfHash->getLengthInBytes();
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the salt length
|
|
|
|
*
|
|
|
|
* To quote from {@link http://tools.ietf.org/html/rfc3447#page-38 RFC3447#page-38}:
|
|
|
|
*
|
|
|
|
* Typical salt lengths in octets are hLen (the length of the output
|
|
|
|
* of the hash function Hash) and 0.
|
|
|
|
*
|
|
|
|
* @access public
|
2017-11-05 21:35:27 +01:00
|
|
|
* @param int $sLen
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function setSaltLength($sLen)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
$this->sLen = $sLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Integer-to-Octet-String primitive
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param bool|\phpseclib\Math\BigInteger $x
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $xLen
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function i2osp($x, $xLen)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($x === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
$x = $x->toBytes();
|
|
|
|
if (strlen($x) > $xLen) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
return str_pad($x, $xLen, chr(0), STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Octet-String-to-Integer primitive
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-4.2 RFC3447#section-4.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $x
|
2014-06-02 20:09:47 +02:00
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function os2ip($x)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2014-06-02 20:09:47 +02:00
|
|
|
return new BigInteger($x, 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponentiate with or without Chinese Remainder Theorem
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $x
|
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function exponentiate($x)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-05-09 20:44:48 +02:00
|
|
|
switch (true) {
|
|
|
|
case empty($this->primes):
|
2016-06-05 06:46:40 +02:00
|
|
|
case $this->primes[1]->equals(self::$zero):
|
2016-05-09 20:44:48 +02:00
|
|
|
case empty($this->coefficients):
|
2016-06-05 06:46:40 +02:00
|
|
|
case $this->coefficients[2]->equals(self::$zero):
|
2016-05-09 20:44:48 +02:00
|
|
|
case empty($this->exponents):
|
2016-06-05 06:46:40 +02:00
|
|
|
case $this->exponents[1]->equals(self::$zero):
|
2016-05-09 20:44:48 +02:00
|
|
|
return $x->modPow($this->exponent, $this->modulus);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$num_primes = count($this->primes);
|
|
|
|
|
2017-06-27 06:19:30 +02:00
|
|
|
if (!static::$enableBlinding) {
|
2016-11-30 22:19:23 +01:00
|
|
|
$m_i = [
|
2010-02-28 06:28:38 +01:00
|
|
|
1 => $x->modPow($this->exponents[1], $this->primes[1]),
|
|
|
|
2 => $x->modPow($this->exponents[2], $this->primes[2])
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2010-02-28 06:28:38 +01:00
|
|
|
$h = $m_i[1]->subtract($m_i[2]);
|
|
|
|
$h = $h->multiply($this->coefficients[2]);
|
|
|
|
list(, $h) = $h->divide($this->primes[1]);
|
|
|
|
$m = $m_i[2]->add($h->multiply($this->primes[2]));
|
|
|
|
|
|
|
|
$r = $this->primes[1];
|
|
|
|
for ($i = 3; $i <= $num_primes; $i++) {
|
|
|
|
$m_i = $x->modPow($this->exponents[$i], $this->primes[$i]);
|
|
|
|
|
|
|
|
$r = $r->multiply($this->primes[$i - 1]);
|
|
|
|
|
|
|
|
$h = $m_i->subtract($m);
|
|
|
|
$h = $h->multiply($this->coefficients[$i]);
|
|
|
|
list(, $h) = $h->divide($this->primes[$i]);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
$m = $m->add($r->multiply($h));
|
|
|
|
}
|
|
|
|
} else {
|
2010-03-01 18:28:19 +01:00
|
|
|
$smallest = $this->primes[1];
|
|
|
|
for ($i = 2; $i <= $num_primes; $i++) {
|
|
|
|
if ($smallest->compare($this->primes[$i]) > 0) {
|
|
|
|
$smallest = $this->primes[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 04:48:51 +02:00
|
|
|
$r = BigInteger::randomRange(self::$one, $smallest->subtract(self::$one));
|
2010-03-01 18:28:19 +01:00
|
|
|
|
2016-11-30 22:19:23 +01:00
|
|
|
$m_i = [
|
2016-12-09 23:46:34 +01:00
|
|
|
1 => $this->blind($x, $r, 1),
|
|
|
|
2 => $this->blind($x, $r, 2)
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2010-02-28 06:28:38 +01:00
|
|
|
$h = $m_i[1]->subtract($m_i[2]);
|
|
|
|
$h = $h->multiply($this->coefficients[2]);
|
|
|
|
list(, $h) = $h->divide($this->primes[1]);
|
|
|
|
$m = $m_i[2]->add($h->multiply($this->primes[2]));
|
|
|
|
|
|
|
|
$r = $this->primes[1];
|
|
|
|
for ($i = 3; $i <= $num_primes; $i++) {
|
2016-12-09 23:46:34 +01:00
|
|
|
$m_i = $this->blind($x, $r, $i);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
$r = $r->multiply($this->primes[$i - 1]);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
$h = $m_i->subtract($m);
|
|
|
|
$h = $h->multiply($this->coefficients[$i]);
|
|
|
|
list(, $h) = $h->divide($this->primes[$i]);
|
|
|
|
|
|
|
|
$m = $m->add($r->multiply($h));
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $m;
|
|
|
|
}
|
|
|
|
|
2017-06-27 06:19:30 +02:00
|
|
|
/**
|
|
|
|
* Enable RSA Blinding
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public static function enableBlinding()
|
|
|
|
{
|
|
|
|
static::$enableBlinding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable RSA Blinding
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public static function disableBlinding()
|
|
|
|
{
|
|
|
|
static::$enableBlinding = false;
|
|
|
|
}
|
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
/**
|
|
|
|
* Performs RSA Blinding
|
|
|
|
*
|
|
|
|
* Protects against timing attacks by employing RSA Blinding.
|
|
|
|
* Returns $x->modPow($this->exponents[$i], $this->primes[$i])
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $x
|
|
|
|
* @param \phpseclib\Math\BigInteger $r
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $i
|
2014-06-02 20:09:47 +02:00
|
|
|
* @return \phpseclib\Math\BigInteger
|
2010-02-28 06:28:38 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function blind($x, $r, $i)
|
2010-02-28 06:28:38 +01:00
|
|
|
{
|
|
|
|
$x = $x->multiply($r->modPow($this->publicExponent, $this->primes[$i]));
|
|
|
|
$x = $x->modPow($this->exponents[$i], $this->primes[$i]);
|
|
|
|
|
|
|
|
$r = $r->modInverse($this->primes[$i]);
|
|
|
|
$x = $x->multiply($r);
|
|
|
|
list(, $x) = $x->divide($this->primes[$i]);
|
|
|
|
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* RSAEP
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|\phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsaep($m)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($m->compare(self::$zero) < 0 || $m->compare($this->modulus) > 0) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->exponentiate($m);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSADP
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $c
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|\phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsadp($c)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($c->compare(self::$zero) < 0 || $c->compare($this->modulus) > 0) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->exponentiate($c);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASP1
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|\phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsasp1($m)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($m->compare(self::$zero) < 0 || $m->compare($this->modulus) > 0) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->exponentiate($m);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSAVP1
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $s
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|\phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsavp1($s)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($s->compare(self::$zero) < 0 || $s->compare($this->modulus) > 0) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->exponentiate($s);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MGF1
|
|
|
|
*
|
2009-12-08 15:18:59 +01:00
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#appendix-B.2.1 RFC3447#appendix-B.2.1}.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $mgfSeed
|
2017-10-25 11:44:14 +02:00
|
|
|
* @param int $maskLen
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function mgf1($mgfSeed, $maskLen)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// if $maskLen would yield strings larger than 4GB, PKCS#1 suggests a "Mask too long" error be output.
|
|
|
|
|
|
|
|
$t = '';
|
2009-12-08 15:18:59 +01:00
|
|
|
$count = ceil($maskLen / $this->mgfHLen);
|
2009-12-03 09:19:00 +01:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$c = pack('N', $i);
|
|
|
|
$t.= $this->mgfHash->hash($mgfSeed . $c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($t, 0, $maskLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSAES-OAEP-ENCRYPT
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-7.1.1 RFC3447#section-7.1.1} and
|
|
|
|
* {http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding OAES}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $l
|
2018-12-31 16:06:12 +01:00
|
|
|
* @throws \LengthException if strlen($m) > $this->k - 2 * $this->hLen - 2
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsaes_oaep_encrypt($m, $l = '')
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
$mLen = strlen($m);
|
|
|
|
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
// if $l is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error
|
|
|
|
// be output.
|
|
|
|
|
|
|
|
if ($mLen > $this->k - 2 * $this->hLen - 2) {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new \LengthException('Message too long');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EME-OAEP encoding
|
|
|
|
|
|
|
|
$lHash = $this->hash->hash($l);
|
|
|
|
$ps = str_repeat(chr(0), $this->k - $mLen - 2 * $this->hLen - 2);
|
|
|
|
$db = $lHash . $ps . chr(1) . $m;
|
2014-12-02 18:20:40 +01:00
|
|
|
$seed = Random::string($this->hLen);
|
2016-12-09 23:46:34 +01:00
|
|
|
$dbMask = $this->mgf1($seed, $this->k - $this->hLen - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
$maskedDB = $db ^ $dbMask;
|
2016-12-09 23:46:34 +01:00
|
|
|
$seedMask = $this->mgf1($maskedDB, $this->hLen);
|
2009-12-03 09:19:00 +01:00
|
|
|
$maskedSeed = $seed ^ $seedMask;
|
|
|
|
$em = chr(0) . $maskedSeed . $maskedDB;
|
|
|
|
|
|
|
|
// RSA encryption
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$m = $this->os2ip($em);
|
|
|
|
$c = $this->rsaep($m);
|
|
|
|
$c = $this->i2osp($c, $this->k);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// Output the ciphertext C
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSAES-OAEP-DECRYPT
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-7.1.2 RFC3447#section-7.1.2}. The fact that the error
|
|
|
|
* messages aren't distinguishable from one another hinders debugging, but, to quote from RFC3447#section-7.1.2:
|
2013-12-26 00:33:08 +01:00
|
|
|
*
|
2009-12-03 09:19:00 +01:00
|
|
|
* Note. Care must be taken to ensure that an opponent cannot
|
|
|
|
* distinguish the different error conditions in Step 3.g, whether by
|
|
|
|
* error message or timing, or, more generally, learn partial
|
|
|
|
* information about the encoded message EM. Otherwise an opponent may
|
|
|
|
* be able to obtain useful information about the decryption of the
|
|
|
|
* ciphertext C, leading to a chosen-ciphertext attack such as the one
|
|
|
|
* observed by Manger [36].
|
|
|
|
*
|
|
|
|
* As for $l... to quote from {@link http://tools.ietf.org/html/rfc3447#page-17 RFC3447#page-17}:
|
|
|
|
*
|
|
|
|
* Both the encryption and the decryption operations of RSAES-OAEP take
|
|
|
|
* the value of a label L as input. In this version of PKCS #1, L is
|
|
|
|
* the empty string; other uses of the label are outside the scope of
|
|
|
|
* this document.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $c
|
|
|
|
* @param string $l
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsaes_oaep_decrypt($c, $l = '')
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
// if $l is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error
|
|
|
|
// be output.
|
|
|
|
|
|
|
|
if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RSA decryption
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$c = $this->os2ip($c);
|
|
|
|
$m = $this->rsadp($c);
|
|
|
|
$em = $this->i2osp($m, $this->k);
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($em === false) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EME-OAEP decoding
|
|
|
|
|
|
|
|
$lHash = $this->hash->hash($l);
|
|
|
|
$y = ord($em[0]);
|
|
|
|
$maskedSeed = substr($em, 1, $this->hLen);
|
|
|
|
$maskedDB = substr($em, $this->hLen + 1);
|
2016-12-09 23:46:34 +01:00
|
|
|
$seedMask = $this->mgf1($maskedDB, $this->hLen);
|
2009-12-03 09:19:00 +01:00
|
|
|
$seed = $maskedSeed ^ $seedMask;
|
2016-12-09 23:46:34 +01:00
|
|
|
$dbMask = $this->mgf1($seed, $this->k - $this->hLen - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
$db = $maskedDB ^ $dbMask;
|
|
|
|
$lHash2 = substr($db, 0, $this->hLen);
|
|
|
|
$m = substr($db, $this->hLen);
|
2019-03-08 15:02:54 +01:00
|
|
|
$hashesMatch = Strings::equals($lHash, $lHash2);
|
2019-03-08 14:27:04 +01:00
|
|
|
$leadingZeros = 1;
|
|
|
|
$patternMatch = 0;
|
|
|
|
$offset = 0;
|
|
|
|
for ($i = 0; $i < strlen($m); $i++) {
|
|
|
|
$patternMatch|= $leadingZeros & ($m[$i] === "\1");
|
|
|
|
$leadingZeros&= $m[$i] === "\0";
|
|
|
|
$offset+= $patternMatch ? 0 : 1;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2019-03-08 14:27:04 +01:00
|
|
|
|
|
|
|
// we do & instead of && to avoid https://en.wikipedia.org/wiki/Short-circuit_evaluation
|
|
|
|
// to protect against timing attacks
|
|
|
|
if (!$hashesMatch & !$patternMatch) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output the message M
|
|
|
|
|
2019-03-08 14:27:04 +01:00
|
|
|
return substr($m, $offset + 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 05:30:38 +02:00
|
|
|
/**
|
|
|
|
* Raw Encryption / Decryption
|
|
|
|
*
|
|
|
|
* Doesn't use padding and is not recommended.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2018-12-31 16:06:12 +01:00
|
|
|
* @throws \LengthException if strlen($m) > $this->k
|
2015-05-26 05:30:38 +02:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function raw_encrypt($m)
|
2015-05-26 05:30:38 +02:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if (strlen($m) > $this->k) {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new \LengthException('Message too long');
|
2016-04-30 23:23:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$temp = $this->os2ip($m);
|
|
|
|
$temp = $this->rsaep($temp);
|
|
|
|
return $this->i2osp($temp, $this->k);
|
2015-05-26 05:30:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* RSAES-PKCS1-V1_5-ENCRYPT
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-7.2.1 RFC3447#section-7.2.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param bool $pkcs15_compat optional
|
2018-12-31 16:06:12 +01:00
|
|
|
* @throws \LengthException if strlen($m) > $this->k - 11
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsaes_pkcs1_v1_5_encrypt($m, $pkcs15_compat = false)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
$mLen = strlen($m);
|
|
|
|
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if ($mLen > $this->k - 11) {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new \LengthException('Message too long');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EME-PKCS1-v1_5 encoding
|
2013-03-23 20:13:24 +01:00
|
|
|
|
2012-12-16 09:20:16 +01:00
|
|
|
$psLen = $this->k - $mLen - 3;
|
|
|
|
$ps = '';
|
|
|
|
while (strlen($ps) != $psLen) {
|
2014-12-02 18:20:40 +01:00
|
|
|
$temp = Random::string($psLen - strlen($ps));
|
2012-12-16 09:20:16 +01:00
|
|
|
$temp = str_replace("\x00", '', $temp);
|
|
|
|
$ps.= $temp;
|
|
|
|
}
|
2013-03-23 20:13:24 +01:00
|
|
|
$type = 2;
|
|
|
|
// see the comments of _rsaes_pkcs1_v1_5_decrypt() to understand why this is being done
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($pkcs15_compat && (!isset($this->publicExponent) || $this->exponent !== $this->publicExponent)) {
|
2013-03-23 20:13:24 +01:00
|
|
|
$type = 1;
|
|
|
|
// "The padding string PS shall consist of k-3-||D|| octets. ... for block type 01, they shall have value FF"
|
|
|
|
$ps = str_repeat("\xFF", $psLen);
|
|
|
|
}
|
|
|
|
$em = chr(0) . chr($type) . $ps . chr(0) . $m;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// RSA encryption
|
2016-12-09 23:46:34 +01:00
|
|
|
$m = $this->os2ip($em);
|
|
|
|
$c = $this->rsaep($m);
|
|
|
|
$c = $this->i2osp($c, $this->k);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// Output the ciphertext C
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSAES-PKCS1-V1_5-DECRYPT
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-7.2.2 RFC3447#section-7.2.2}.
|
|
|
|
*
|
2014-03-06 00:41:20 +01:00
|
|
|
* For compatibility purposes, this function departs slightly from the description given in RFC3447.
|
2010-04-10 17:57:02 +02:00
|
|
|
* The reason being that RFC2313#section-8.1 (PKCS#1 v1.5) states that ciphertext's encrypted by the
|
|
|
|
* private key should have the second byte set to either 0 or 1 and that ciphertext's encrypted by the
|
|
|
|
* public key should have the second byte set to 2. In RFC3447 (PKCS#1 v2.1), the second byte is supposed
|
2014-03-06 00:41:20 +01:00
|
|
|
* to be 2 regardless of which key is used. For compatibility purposes, we'll just check to make sure the
|
2010-04-10 17:57:02 +02:00
|
|
|
* second byte is 2 or less. If it is, we'll accept the decrypted string as valid.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* As a consequence of this, a private key encrypted ciphertext produced with \phpseclib\Crypt\RSA may not decrypt
|
2010-04-10 17:57:02 +02:00
|
|
|
* with a strictly PKCS#1 v1.5 compliant RSA implementation. Public key encrypted ciphertext's should but
|
|
|
|
* not private key encrypted ciphertext's.
|
|
|
|
*
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $c
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsaes_pkcs1_v1_5_decrypt($c)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($c) != $this->k) { // or if k < 11
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RSA decryption
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$c = $this->os2ip($c);
|
|
|
|
$m = $this->rsadp($c);
|
|
|
|
$em = $this->i2osp($m, $this->k);
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($em === false) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EME-PKCS1-v1_5 decoding
|
|
|
|
|
2010-04-10 17:57:02 +02:00
|
|
|
if (ord($em[0]) != 0 || ord($em[1]) > 2) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$ps = substr($em, 2, strpos($em, chr(0), 2) - 2);
|
|
|
|
$m = substr($em, strlen($ps) + 3);
|
|
|
|
|
|
|
|
if (strlen($ps) < 8) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output M
|
|
|
|
|
|
|
|
return $m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EMSA-PSS-ENCODE
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}.
|
|
|
|
*
|
2017-11-05 21:35:27 +01:00
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \RuntimeException on encoding error
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $emBits
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function emsa_pss_encode($m, $emBits)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// if $m is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error
|
|
|
|
// be output.
|
|
|
|
|
|
|
|
$emLen = ($emBits + 1) >> 3; // ie. ceil($emBits / 8)
|
2016-06-21 09:56:57 +02:00
|
|
|
$sLen = $this->sLen !== null ? $this->sLen : $this->hLen;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$mHash = $this->hash->hash($m);
|
|
|
|
if ($emLen < $this->hLen + $sLen + 2) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2014-12-02 18:20:40 +01:00
|
|
|
$salt = Random::string($sLen);
|
2009-12-03 09:19:00 +01:00
|
|
|
$m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt;
|
|
|
|
$h = $this->hash->hash($m2);
|
|
|
|
$ps = str_repeat(chr(0), $emLen - $sLen - $this->hLen - 2);
|
|
|
|
$db = $ps . chr(1) . $salt;
|
2016-12-09 23:46:34 +01:00
|
|
|
$dbMask = $this->mgf1($h, $emLen - $this->hLen - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
$maskedDB = $db ^ $dbMask;
|
|
|
|
$maskedDB[0] = ~chr(0xFF << ($emBits & 7)) & $maskedDB[0];
|
|
|
|
$em = $maskedDB . $h . chr(0xBC);
|
|
|
|
|
|
|
|
return $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EMSA-PSS-VERIFY
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-9.1.2 RFC3447#section-9.1.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $em
|
|
|
|
* @param int $emBits
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function emsa_pss_verify($m, $em, $emBits)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// if $m is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error
|
|
|
|
// be output.
|
|
|
|
|
|
|
|
$emLen = ($emBits + 1) >> 3; // ie. ceil($emBits / 8);
|
2016-06-21 09:56:57 +02:00
|
|
|
$sLen = $this->sLen !== null ? $this->sLen : $this->hLen;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$mHash = $this->hash->hash($m);
|
|
|
|
if ($emLen < $this->hLen + $sLen + 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($em[strlen($em) - 1] != chr(0xBC)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-19 04:32:22 +01:00
|
|
|
$maskedDB = substr($em, 0, -$this->hLen - 1);
|
|
|
|
$h = substr($em, -$this->hLen - 1, $this->hLen);
|
2009-12-03 09:19:00 +01:00
|
|
|
$temp = chr(0xFF << ($emBits & 7));
|
|
|
|
if ((~$maskedDB[0] & $temp) != $temp) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
$dbMask = $this->mgf1($h, $emLen - $this->hLen - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
$db = $maskedDB ^ $dbMask;
|
|
|
|
$db[0] = ~chr(0xFF << ($emBits & 7)) & $db[0];
|
|
|
|
$temp = $emLen - $this->hLen - $sLen - 2;
|
|
|
|
if (substr($db, 0, $temp) != str_repeat(chr(0), $temp) || ord($db[$temp]) != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$salt = substr($db, $temp + 1); // should be $sLen long
|
|
|
|
$m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt;
|
|
|
|
$h2 = $this->hash->hash($m2);
|
2016-12-23 17:02:07 +01:00
|
|
|
return Strings::equals($h, $h2);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASSA-PSS-SIGN
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-8.1.1 RFC3447#section-8.1.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsassa_pss_sign($m)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// EMSA-PSS encoding
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$em = $this->emsa_pss_encode($m, 8 * $this->k - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// RSA signature
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$m = $this->os2ip($em);
|
|
|
|
$s = $this->rsasp1($m);
|
|
|
|
$s = $this->i2osp($s, $this->k);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// Output the signature S
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASSA-PSS-VERIFY
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-8.1.2 RFC3447#section-8.1.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $s
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsassa_pss_verify($m, $s)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($s) != $this->k) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RSA verification
|
|
|
|
|
|
|
|
$modBits = 8 * $this->k;
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$s2 = $this->os2ip($s);
|
|
|
|
$m2 = $this->rsavp1($s2);
|
|
|
|
$em = $this->i2osp($m2, $modBits >> 3);
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($em === false) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EMSA-PSS verification
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->emsa_pss_verify($m, $em, $modBits - 1);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EMSA-PKCS1-V1_5-ENCODE
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-9.2 RFC3447#section-9.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param int $emLen
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the intended encoded message length is too short
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function emsa_pkcs1_v1_5_encode($m, $emLen)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
$h = $this->hash->hash($m);
|
|
|
|
|
|
|
|
// see http://tools.ietf.org/html/rfc3447#page-43
|
|
|
|
switch ($this->hashName) {
|
|
|
|
case 'md2':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x02\x05\x00\x04\x10";
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
case 'md5':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10";
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
case 'sha1':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14";
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
case 'sha256':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20";
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
case 'sha384':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30";
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
case 'sha512':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40";
|
2016-04-30 23:23:35 +02:00
|
|
|
break;
|
|
|
|
// from https://www.emc.com/collateral/white-papers/h11300-pkcs-1v2-2-rsa-cryptography-standard-wp.pdf#page=40
|
|
|
|
case 'sha224':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04\x05\x00\x04\x1c";
|
2016-04-30 23:23:35 +02:00
|
|
|
break;
|
|
|
|
case 'sha512/224':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x05\x05\x00\x04\x1c";
|
2016-04-30 23:23:35 +02:00
|
|
|
break;
|
|
|
|
case 'sha512/256':
|
2016-06-05 05:31:23 +02:00
|
|
|
$t = "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x06\x05\x00\x04\x20";
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
$t.= $h;
|
|
|
|
$tLen = strlen($t);
|
|
|
|
|
|
|
|
if ($emLen < $tLen + 11) {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \LengthException('Intended encoded message length too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$ps = str_repeat(chr(0xFF), $emLen - $tLen - 3);
|
|
|
|
|
|
|
|
$em = "\0\1$ps\0$t";
|
|
|
|
|
|
|
|
return $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASSA-PKCS1-V1_5-SIGN
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-8.2.1 RFC3447#section-8.2.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the RSA modulus is too short
|
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsassa_pkcs1_v1_5_sign($m)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// EMSA-PKCS1-v1_5 encoding
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
// If the encoding operation outputs "intended encoded message length too short," output "RSA modulus
|
|
|
|
// too short" and stop.
|
|
|
|
try {
|
2016-12-09 23:46:34 +01:00
|
|
|
$em = $this->emsa_pkcs1_v1_5_encode($m, $this->k);
|
2016-04-30 23:23:35 +02:00
|
|
|
} catch (\LengthException $e) {
|
|
|
|
throw new \LengthException('RSA modulus too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RSA signature
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$m = $this->os2ip($em);
|
|
|
|
$s = $this->rsasp1($m);
|
|
|
|
$s = $this->i2osp($s, $this->k);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
// Output the signature S
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RSASSA-PKCS1-V1_5-VERIFY
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-8.2.2 RFC3447#section-8.2.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $m
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param string $s
|
|
|
|
* @throws \LengthException if the RSA modulus is too short
|
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsassa_pkcs1_v1_5_verify($m, $s)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($s) != $this->k) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RSA verification
|
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$s = $this->os2ip($s);
|
|
|
|
$m2 = $this->rsavp1($s);
|
|
|
|
$em = $this->i2osp($m2, $this->k);
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($em === false) {
|
2015-12-24 17:58:06 +01:00
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EMSA-PKCS1-v1_5 encoding
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
// If the encoding operation outputs "intended encoded message length too short," output "RSA modulus
|
|
|
|
// too short" and stop.
|
|
|
|
try {
|
2016-12-09 23:46:34 +01:00
|
|
|
$em2 = $this->emsa_pkcs1_v1_5_encode($m, $this->k);
|
2016-04-30 23:23:35 +02:00
|
|
|
} catch (\LengthException $e) {
|
|
|
|
throw new \LengthException('RSA modulus too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare
|
2016-12-23 17:02:07 +01:00
|
|
|
return Strings::equals($em, $em2);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* RSASSA-PKCS1-V1_5-VERIFY (relaxed matching)
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Per {@link http://tools.ietf.org/html/rfc3447#page-43 RFC3447#page-43} PKCS1 v1.5
|
|
|
|
* specified the use BER encoding rather than DER encoding that PKCS1 v2.0 specified.
|
|
|
|
* This means that under rare conditions you can have a perfectly valid v1.5 signature
|
|
|
|
* that fails to validate with _rsassa_pkcs1_v1_5_verify(). PKCS1 v2.1 also recommends
|
|
|
|
* that if you're going to validate these types of signatures you "should indicate
|
|
|
|
* whether the underlying BER encoding is a DER encoding and hence whether the signature
|
|
|
|
* is valid with respect to the specification given in [PKCS1 v2.0+]". so if you do
|
|
|
|
* $rsa->getLastPadding() and get RSA::PADDING_RELAXED_PKCS1 back instead of
|
|
|
|
* RSA::PADDING_PKCS1... that means BER encoding was used.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* @access private
|
|
|
|
* @param string $m
|
|
|
|
* @param string $s
|
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
private function rsassa_pkcs1_v1_5_relaxed_verify($m, $s)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
// Length checking
|
2016-04-10 18:30:59 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
if (strlen($s) != $this->k) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-10 18:30:59 +02:00
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
// RSA verification
|
2016-04-10 18:30:59 +02:00
|
|
|
|
2016-12-09 23:46:34 +01:00
|
|
|
$s = $this->os2ip($s);
|
|
|
|
$m2 = $this->rsavp1($s);
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($m2 === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-09 23:46:34 +01:00
|
|
|
$em = $this->i2osp($m2, $this->k);
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($em === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-31 05:18:06 +02:00
|
|
|
if (Strings::shift($em, 2) != "\0\1") {
|
2016-04-30 23:23:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$em = ltrim($em, "\xFF");
|
2016-07-31 05:18:06 +02:00
|
|
|
if (Strings::shift($em) != "\0") {
|
2016-04-30 23:23:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2016-04-30 23:23:35 +02:00
|
|
|
if (!is_array($decoded) || empty($decoded[0]) || strlen($em) > $decoded[0]['length']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
static $oids;
|
|
|
|
if (!isset($oids)) {
|
|
|
|
$oids = [
|
2018-10-25 03:00:37 +02:00
|
|
|
'md2' => '1.2.840.113549.2.2',
|
|
|
|
'md4' => '1.2.840.113549.2.4', // from PKCS1 v1.5
|
|
|
|
'md5' => '1.2.840.113549.2.5',
|
|
|
|
'id-sha1' => '1.3.14.3.2.26',
|
|
|
|
'id-sha256' => '2.16.840.1.101.3.4.2.1',
|
|
|
|
'id-sha384' => '2.16.840.1.101.3.4.2.2',
|
|
|
|
'id-sha512' => '2.16.840.1.101.3.4.2.3',
|
2016-11-23 06:55:33 +01:00
|
|
|
// from PKCS1 v2.2
|
2018-10-25 03:00:37 +02:00
|
|
|
'id-sha224' => '2.16.840.1.101.3.4.2.4',
|
|
|
|
'id-sha512/224' => '2.16.840.1.101.3.4.2.5',
|
|
|
|
'id-sha512/256' => '2.16.840.1.101.3.4.2.6',
|
2016-11-23 06:55:33 +01:00
|
|
|
];
|
|
|
|
ASN1::loadOIDs($oids);
|
|
|
|
}
|
|
|
|
|
|
|
|
$decoded = ASN1::asn1map($decoded[0], DigestInfo::MAP);
|
2016-04-30 23:23:35 +02:00
|
|
|
if (!isset($decoded) || $decoded === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
if (!isset($oids[$decoded['digestAlgorithm']['algorithm']])) {
|
2016-04-30 23:23:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:33 +01:00
|
|
|
$hash = $decoded['digestAlgorithm']['algorithm'];
|
|
|
|
$hash = substr($hash, 0, 3) == 'id-' ?
|
|
|
|
substr($hash, 3) :
|
|
|
|
$hash;
|
|
|
|
$hash = new Hash($hash);
|
2016-04-30 23:23:35 +02:00
|
|
|
$em = $hash->hash($m);
|
2016-11-29 01:50:21 +01:00
|
|
|
$em2 = $decoded['digest'];
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2016-12-23 17:02:07 +01:00
|
|
|
return Strings::equals($em, $em2);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encryption
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Both self::PADDING_OAEP and self::PADDING_PKCS1 both place limits on how long $plaintext can be.
|
2009-12-03 09:19:00 +01:00
|
|
|
* If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will
|
|
|
|
* be concatenated together.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::decrypt()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $plaintext
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param int $padding optional
|
|
|
|
* @return bool|string
|
|
|
|
* @throws \LengthException if the RSA modulus is too short
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function encrypt($plaintext, $padding = self::PADDING_OAEP)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2018-02-15 12:29:14 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
throw new NoKeyLoadedException('No key has been loaded');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isPublic) {
|
|
|
|
throw new UnsupportedOperationException('phpseclib does not allow the use of private keys to encrypt data');
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
switch ($padding) {
|
|
|
|
case self::PADDING_NONE:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->raw_encrypt($plaintext);
|
2016-04-30 23:23:35 +02:00
|
|
|
case self::PADDING_PKCS15_COMPAT:
|
|
|
|
case self::PADDING_PKCS1:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsaes_pkcs1_v1_5_encrypt($plaintext, $padding == self::PADDING_PKCS15_COMPAT);
|
2016-04-30 23:23:35 +02:00
|
|
|
//case self::PADDING_OAEP:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsaes_oaep_encrypt($plaintext);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decryption
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2017-10-25 11:44:14 +02:00
|
|
|
* @param string $ciphertext
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param int $padding optional
|
|
|
|
* @return bool|string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function decrypt($ciphertext, $padding = self::PADDING_OAEP)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2018-02-15 12:29:14 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
throw new NoKeyLoadedException('No key has been loaded');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isPublic) {
|
|
|
|
throw new UnsupportedOperationException('phpseclib does not allow the use of public keys to decrypt data');
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
switch ($padding) {
|
|
|
|
case self::PADDING_NONE:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->raw_encrypt($ciphertext);
|
2016-04-30 23:23:35 +02:00
|
|
|
case self::PADDING_PKCS1:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsaes_pkcs1_v1_5_decrypt($ciphertext);
|
2016-04-30 23:23:35 +02:00
|
|
|
//case self::PADDING_OAEP:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsaes_oaep_decrypt($ciphertext);
|
2009-12-06 08:26:52 +01:00
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a signature
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::verify()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $message
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param int $padding optional
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function sign($message, $padding = self::PADDING_PSS)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
2018-02-15 12:29:14 +01:00
|
|
|
throw new NoKeyLoadedException('No key has been loaded');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isPublic) {
|
|
|
|
throw new UnsupportedOperationException('phpseclib does not allow the use of public keys to sign data');
|
2009-12-06 08:26:52 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
switch ($padding) {
|
|
|
|
case self::PADDING_PKCS1:
|
|
|
|
case self::PADDING_RELAXED_PKCS1:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsassa_pkcs1_v1_5_sign($message);
|
2016-04-30 23:23:35 +02:00
|
|
|
//case self::PADDING_PSS:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsassa_pss_sign($message);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies a signature
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::sign()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $message
|
|
|
|
* @param string $signature
|
2016-04-30 23:23:35 +02:00
|
|
|
* @param int $padding optional
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2016-12-09 23:46:34 +01:00
|
|
|
public function verify($message, $signature, $padding = self::PADDING_PSS)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
2018-02-15 12:29:14 +01:00
|
|
|
throw new NoKeyLoadedException('No key has been loaded');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isPublic) {
|
|
|
|
throw new UnsupportedOperationException('phpseclib does not allow the use of private keys to verify data');
|
2009-12-06 08:26:52 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
switch ($padding) {
|
|
|
|
case self::PADDING_RELAXED_PKCS1:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsassa_pkcs1_v1_5_relaxed_verify($message, $signature);
|
2016-04-30 23:23:35 +02:00
|
|
|
case self::PADDING_PKCS1:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsassa_pkcs1_v1_5_verify($message, $signature);
|
2016-04-30 23:23:35 +02:00
|
|
|
//case self::PADDING_PSS:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2016-12-09 23:46:34 +01:00
|
|
|
return $this->rsassa_pss_verify($message, $signature);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-16 17:31:43 +02:00
|
|
|
}
|