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
|
2014-12-17 01:16:54 +01:00
|
|
|
* include 'vendor/autoload.php';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* $rsa = new \phpseclib\Crypt\RSA();
|
2009-12-03 09:19:00 +01:00
|
|
|
* extract($rsa->createKey());
|
|
|
|
*
|
|
|
|
* $plaintext = 'terrafrost';
|
|
|
|
*
|
|
|
|
* $rsa->loadKey($privatekey);
|
|
|
|
* $ciphertext = $rsa->encrypt($plaintext);
|
|
|
|
*
|
|
|
|
* $rsa->loadKey($publickey);
|
|
|
|
* echo $rsa->decrypt($ciphertext);
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Here's an example of how to create signatures and verify signatures with this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-12-17 01:16:54 +01:00
|
|
|
* include 'vendor/autoload.php';
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* $rsa = new \phpseclib\Crypt\RSA();
|
2009-12-03 09:19:00 +01:00
|
|
|
* extract($rsa->createKey());
|
|
|
|
*
|
|
|
|
* $plaintext = 'terrafrost';
|
|
|
|
*
|
|
|
|
* $rsa->loadKey($privatekey);
|
|
|
|
* $signature = $rsa->sign($plaintext);
|
|
|
|
*
|
|
|
|
* $rsa->loadKey($publickey);
|
|
|
|
* echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
|
|
|
|
* ?>
|
|
|
|
* </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
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
use phpseclib\Math\BigInteger;
|
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
|
|
|
|
*/
|
2014-12-17 01:16:54 +01:00
|
|
|
class RSA
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2015-10-12 21:10:26 +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.
|
|
|
|
*
|
|
|
|
* Uses sha1 by default.
|
|
|
|
*
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::setHash()
|
|
|
|
* @see self::setMGFHash()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
const ENCRYPTION_OAEP = 1;
|
|
|
|
/**
|
|
|
|
* Use PKCS#1 padding.
|
|
|
|
*
|
|
|
|
* Although self::ENCRYPTION_OAEP offers more security, including PKCS#1 padding is necessary for purposes of backwards
|
|
|
|
* compatibility with protocols (like SSH-1) written before OAEP's introduction.
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ENCRYPTION_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.
|
|
|
|
*/
|
|
|
|
const ENCRYPTION_NONE = 3;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::sign()
|
|
|
|
* @see self::verify()
|
|
|
|
* @see self::setHash()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Use the Probabilistic Signature Scheme for signing
|
|
|
|
*
|
|
|
|
* Uses sha1 by default.
|
|
|
|
*
|
2015-10-12 21:10:26 +02:00
|
|
|
* @see self::setSaltLength()
|
|
|
|
* @see self::setMGFHash()
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const SIGNATURE_PSS = 1;
|
|
|
|
/**
|
|
|
|
* Use the PKCS#1 scheme by default.
|
|
|
|
*
|
|
|
|
* Although self::SIGNATURE_PSS offers more security, including PKCS#1 signing is necessary for purposes of backwards
|
|
|
|
* compatibility with protocols (like SSH-2) written before PSS's introduction.
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const SIGNATURE_PKCS1 = 2;
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access private
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\RSA::createKey()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* ASN1 Integer
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ASN1_INTEGER = 2;
|
|
|
|
/**
|
|
|
|
* ASN1 Bit String
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ASN1_BITSTRING = 3;
|
|
|
|
/**
|
|
|
|
* ASN1 Octet String
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ASN1_OCTETSTRING = 4;
|
|
|
|
/**
|
|
|
|
* ASN1 Object Identifier
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ASN1_OBJECT = 6;
|
|
|
|
/**
|
|
|
|
* ASN1 Sequence (with the constucted bit set)
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ASN1_SEQUENCE = 48;
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access private
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\RSA::__construct()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* To use the pure-PHP implementation
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_INTERNAL = 1;
|
|
|
|
/**
|
|
|
|
* To use the OpenSSL library
|
|
|
|
*
|
|
|
|
* (if enabled; otherwise, the internal implementation will be used)
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_OPENSSL = 2;
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\RSA::createKey()
|
|
|
|
* @see \phpseclib\Crypt\RSA::setPrivateKeyFormat()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* PKCS#1 formatted private key
|
|
|
|
*
|
|
|
|
* Used by OpenSSH
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PRIVATE_FORMAT_PKCS1 = 0;
|
|
|
|
/**
|
|
|
|
* PuTTY formatted private key
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PRIVATE_FORMAT_PUTTY = 1;
|
|
|
|
/**
|
|
|
|
* XML formatted private key
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PRIVATE_FORMAT_XML = 2;
|
|
|
|
/**
|
|
|
|
* PKCS#8 formatted private key
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2015-12-08 06:53:09 +01:00
|
|
|
const PRIVATE_FORMAT_PKCS8 = 8;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\RSA::createKey()
|
|
|
|
* @see \phpseclib\Crypt\RSA::setPublicKeyFormat()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Raw public key
|
|
|
|
*
|
|
|
|
* An array containing two \phpseclib\Math\BigInteger objects.
|
|
|
|
*
|
|
|
|
* The exponent can be indexed with any of the following:
|
|
|
|
*
|
|
|
|
* 0, e, exponent, publicExponent
|
|
|
|
*
|
|
|
|
* The modulus can be indexed with any of the following:
|
|
|
|
*
|
|
|
|
* 1, n, modulo, modulus
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PUBLIC_FORMAT_RAW = 3;
|
|
|
|
/**
|
|
|
|
* PKCS#1 formatted public key (raw)
|
|
|
|
*
|
|
|
|
* Used by File/X509.php
|
|
|
|
*
|
|
|
|
* Has the following header:
|
|
|
|
*
|
|
|
|
* -----BEGIN RSA PUBLIC KEY-----
|
|
|
|
*
|
|
|
|
* Analogous to ssh-keygen's pem format (as specified by -m)
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PUBLIC_FORMAT_PKCS1 = 4;
|
|
|
|
const PUBLIC_FORMAT_PKCS1_RAW = 4;
|
|
|
|
/**
|
|
|
|
* XML formatted public key
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PUBLIC_FORMAT_XML = 5;
|
|
|
|
/**
|
|
|
|
* OpenSSH formatted public key
|
|
|
|
*
|
|
|
|
* Place in $HOME/.ssh/authorized_keys
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PUBLIC_FORMAT_OPENSSH = 6;
|
|
|
|
/**
|
|
|
|
* PKCS#1 formatted public key (encapsulated)
|
|
|
|
*
|
|
|
|
* Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set)
|
|
|
|
*
|
|
|
|
* Has the following header:
|
|
|
|
*
|
|
|
|
* -----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)
|
2015-09-14 17:30:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const PUBLIC_FORMAT_PKCS8 = 7;
|
|
|
|
/**#@-*/
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Precomputed Zero
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $zero;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Precomputed One
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $one;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private Key Format
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
var $privateKeyFormat = self::PRIVATE_FORMAT_PKCS1;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public Key Format
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
var $publicKeyFormat = self::PUBLIC_FORMAT_PKCS8;
|
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
|
|
|
|
*/
|
|
|
|
var $modulus;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modulus length
|
|
|
|
*
|
2014-06-02 20:09:47 +02:00
|
|
|
* @var \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $k;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
var $exponent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Primes for Chinese Remainder Theorem (ie. p and q)
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $primes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponents for Chinese Remainder Theorem (ie. dP and dQ)
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $exponents;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Coefficients for Chinese Remainder Theorem (ie. qInv)
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $coefficients;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash name
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var string
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hashName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @var \phpseclib\Crypt\Hash
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of hash function output
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hLen;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of salt
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $sLen;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
var $mgfHash;
|
|
|
|
|
2009-12-08 15:18:59 +01:00
|
|
|
/**
|
|
|
|
* Length of MGF hash function output
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-08 15:18:59 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $mgfHLen;
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Encryption mode
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
var $encryptionMode = self::ENCRYPTION_OAEP;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signature mode
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var int
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
var $signatureMode = self::SIGNATURE_PSS;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public Exponent
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var mixed
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $publicExponent = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Password
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var string
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2012-08-23 15:59:49 +02:00
|
|
|
var $password = false;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2011-04-18 14:17:40 +02:00
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* For use with parsing XML formatted keys. PHP's XML Parser functions use utilized - instead of PHP's DOM functions -
|
|
|
|
* because PHP's XML Parser functions work on PHP4 whereas PHP's DOM functions - although surperior - don't.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_start_element_handler()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var array
|
2011-04-18 14:17:40 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $components = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current String
|
|
|
|
*
|
|
|
|
* For use with parsing XML formatted keys.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_character_handler()
|
|
|
|
* @see self::_stop_element_handler()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var mixed
|
2011-04-18 14:17:40 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $current;
|
|
|
|
|
2012-12-10 12:07:49 +01:00
|
|
|
/**
|
|
|
|
* OpenSSL configuration file name.
|
|
|
|
*
|
2013-12-03 18:54:43 +01:00
|
|
|
* Set to null to use system configuration file.
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::createKey()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var mixed
|
2012-12-10 12:07:49 +01:00
|
|
|
* @Access public
|
|
|
|
*/
|
|
|
|
var $configFile;
|
|
|
|
|
2013-05-06 01:38:34 +02:00
|
|
|
/**
|
|
|
|
* Public key comment field.
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @var string
|
2013-05-06 01:38:34 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $comment = 'phpseclib-generated-key';
|
|
|
|
|
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
|
|
|
|
*/
|
2014-06-16 17:06:34 +02:00
|
|
|
function __construct()
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
$this->configFile = dirname(__FILE__) . '/../openssl.cnf';
|
2012-12-10 12:07:49 +01:00
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
if (!defined('CRYPT_RSA_MODE')) {
|
2014-08-25 14:03:24 +02:00
|
|
|
switch (true) {
|
|
|
|
// Math/BigInteger's openssl requirements are a little less stringent than Crypt/RSA's. in particular,
|
|
|
|
// Math/BigInteger doesn't require an openssl.cfg file whereas Crypt/RSA does. so if Math/BigInteger
|
|
|
|
// can't use OpenSSL it can be pretty trivially assumed, then, that Crypt/RSA can't either.
|
|
|
|
case defined('MATH_BIGINTEGER_OPENSSL_DISABLE'):
|
2014-12-09 19:46:30 +01:00
|
|
|
define('CRYPT_RSA_MODE', self::MODE_INTERNAL);
|
2014-08-25 14:03:24 +02:00
|
|
|
break;
|
2015-09-01 20:17:54 +02:00
|
|
|
case extension_loaded('openssl') && file_exists($this->configFile):
|
2013-09-10 18:07:56 +02:00
|
|
|
// some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
|
|
|
|
ob_start();
|
2014-03-30 17:15:08 +02:00
|
|
|
@phpinfo();
|
2013-09-10 18:07:56 +02:00
|
|
|
$content = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches);
|
|
|
|
|
|
|
|
$versions = array();
|
|
|
|
if (!empty($matches[1])) {
|
2015-01-06 09:27:30 +01:00
|
|
|
for ($i = 0; $i < count($matches[1]); $i++) {
|
|
|
|
$fullVersion = trim(str_replace('=>', '', strip_tags($matches[2][$i])));
|
|
|
|
|
|
|
|
// Remove letter part in OpenSSL version
|
|
|
|
if (!preg_match('/(\d+\.\d+\.\d+)/i', $fullVersion, $m)) {
|
|
|
|
$versions[$matches[1][$i]] = $fullVersion;
|
|
|
|
} else {
|
|
|
|
$versions[$matches[1][$i]] = $m[0];
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 18:07:56 +02:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:14:01 +02:00
|
|
|
// it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+
|
2013-09-10 18:07:56 +02:00
|
|
|
switch (true) {
|
|
|
|
case !isset($versions['Header']):
|
|
|
|
case !isset($versions['Library']):
|
2013-09-20 19:14:01 +02:00
|
|
|
case $versions['Header'] == $versions['Library']:
|
2014-12-09 19:46:30 +01:00
|
|
|
define('CRYPT_RSA_MODE', self::MODE_OPENSSL);
|
2013-09-10 18:07:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
2014-12-09 19:46:30 +01:00
|
|
|
define('CRYPT_RSA_MODE', self::MODE_INTERNAL);
|
2013-09-20 19:14:01 +02:00
|
|
|
define('MATH_BIGINTEGER_OPENSSL_DISABLE', true);
|
2013-09-10 18:07:56 +02:00
|
|
|
}
|
2012-07-01 19:07:42 +02:00
|
|
|
break;
|
2014-08-25 14:58:38 +02:00
|
|
|
default:
|
2014-12-09 19:46:30 +01:00
|
|
|
define('CRYPT_RSA_MODE', self::MODE_INTERNAL);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:09:47 +02:00
|
|
|
$this->zero = new BigInteger();
|
|
|
|
$this->one = new BigInteger(1);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->hash = new Hash('sha1');
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->hLen = $this->hash->getLength();
|
|
|
|
$this->hashName = 'sha1';
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->mgfHash = new Hash('sha1');
|
2010-01-25 08:42:45 +01:00
|
|
|
$this->mgfHLen = $this->mgfHash->getLength();
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create public / private key pair
|
|
|
|
*
|
|
|
|
* Returns an array with the following three elements:
|
|
|
|
* - 'privatekey': The private key.
|
|
|
|
* - 'publickey': The public key.
|
|
|
|
* - 'partialkey': A partially computed key (if the execution time exceeded $timeout).
|
2014-12-17 01:16:54 +01:00
|
|
|
* Will need to be passed back to \phpseclib\Crypt\RSA::createKey() as the third parameter for further processing.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $bits
|
|
|
|
* @param int $timeout
|
2015-09-02 01:41:38 +02:00
|
|
|
* @param array $p
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2010-01-21 01:52:11 +01:00
|
|
|
function createKey($bits = 1024, $timeout = false, $partial = array())
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2012-07-01 19:07:42 +02:00
|
|
|
if (!defined('CRYPT_RSA_EXPONENT')) {
|
|
|
|
// http://en.wikipedia.org/wiki/65537_%28number%29
|
|
|
|
define('CRYPT_RSA_EXPONENT', '65537');
|
|
|
|
}
|
|
|
|
// 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 CRYPT_RSA_SMALLEST_PRIME
|
|
|
|
// to 384 bits then you're going to get a 384 bit prime and a 640 bit prime (384 + 1024 % 384). at least if
|
2014-12-09 19:46:30 +01:00
|
|
|
// CRYPT_RSA_MODE is set to self::MODE_INTERNAL. if CRYPT_RSA_MODE is set to self::MODE_OPENSSL then
|
2012-07-01 19:07:42 +02:00
|
|
|
// CRYPT_RSA_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)
|
|
|
|
if (!defined('CRYPT_RSA_SMALLEST_PRIME')) {
|
|
|
|
define('CRYPT_RSA_SMALLEST_PRIME', 4096);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum
|
2015-07-17 13:36:18 +02:00
|
|
|
if (CRYPT_RSA_MODE == self::MODE_OPENSSL && $bits >= 384 && CRYPT_RSA_EXPONENT == 65537) {
|
2012-12-10 12:07:49 +01:00
|
|
|
$config = array();
|
|
|
|
if (isset($this->configFile)) {
|
|
|
|
$config['config'] = $this->configFile;
|
|
|
|
}
|
|
|
|
$rsa = openssl_pkey_new(array('private_key_bits' => $bits) + $config);
|
2013-12-03 18:54:43 +01:00
|
|
|
openssl_pkey_export($rsa, $privatekey, null, $config);
|
2009-12-03 09:19:00 +01:00
|
|
|
$publickey = openssl_pkey_get_details($rsa);
|
|
|
|
$publickey = $publickey['key'];
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
$privatekey = call_user_func_array(array($this, '_convertPrivateKey'), array_values($this->_parseKey($privatekey, self::PRIVATE_FORMAT_PKCS1)));
|
|
|
|
$publickey = call_user_func_array(array($this, '_convertPublicKey'), array_values($this->_parseKey($publickey, self::PUBLIC_FORMAT_PKCS1)));
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2012-07-01 19:07:42 +02:00
|
|
|
// clear the buffer of error strings stemming from a minimalistic openssl.cnf
|
2015-07-15 03:52:31 +02:00
|
|
|
while (openssl_error_string() !== false) {
|
|
|
|
}
|
2012-07-01 19:07:42 +02:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
return array(
|
|
|
|
'privatekey' => $privatekey,
|
|
|
|
'publickey' => $publickey,
|
|
|
|
'partialkey' => false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static $e;
|
|
|
|
if (!isset($e)) {
|
2014-06-02 20:09:47 +02:00
|
|
|
$e = new BigInteger(CRYPT_RSA_EXPONENT);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extract($this->_generateMinMax($bits));
|
|
|
|
$absoluteMin = $min;
|
2012-07-01 19:07:42 +02:00
|
|
|
$temp = $bits >> 1; // divide by two to see how many bits P and Q would be
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($temp > CRYPT_RSA_SMALLEST_PRIME) {
|
|
|
|
$num_primes = floor($bits / CRYPT_RSA_SMALLEST_PRIME);
|
|
|
|
$temp = CRYPT_RSA_SMALLEST_PRIME;
|
|
|
|
} else {
|
|
|
|
$num_primes = 2;
|
|
|
|
}
|
|
|
|
extract($this->_generateMinMax($temp + $bits % $temp));
|
|
|
|
$finalMax = $max;
|
|
|
|
extract($this->_generateMinMax($temp));
|
|
|
|
|
2014-06-02 20:09:47 +02:00
|
|
|
$generator = new BigInteger();
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$n = $this->one->copy();
|
2010-01-21 01:52:11 +01:00
|
|
|
if (!empty($partial)) {
|
|
|
|
extract(unserialize($partial));
|
|
|
|
} else {
|
|
|
|
$exponents = $coefficients = $primes = array();
|
|
|
|
$lcm = array(
|
|
|
|
'top' => $this->one->copy(),
|
|
|
|
'bottom' => false
|
|
|
|
);
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$start = time();
|
|
|
|
$i0 = count($primes) + 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
for ($i = $i0; $i <= $num_primes; $i++) {
|
|
|
|
if ($timeout !== false) {
|
|
|
|
$timeout-= time() - $start;
|
|
|
|
$start = time();
|
|
|
|
if ($timeout <= 0) {
|
2011-02-06 01:04:07 +01:00
|
|
|
return array(
|
2009-12-03 09:19:00 +01:00
|
|
|
'privatekey' => '',
|
|
|
|
'publickey' => '',
|
2011-02-06 01:04:07 +01:00
|
|
|
'partialkey' => serialize(array(
|
2010-01-21 01:52:11 +01:00
|
|
|
'primes' => $primes,
|
|
|
|
'coefficients' => $coefficients,
|
|
|
|
'lcm' => $lcm,
|
|
|
|
'exponents' => $exponents
|
2011-02-06 01:04:07 +01:00
|
|
|
))
|
|
|
|
);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-01 18:28:19 +01:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($i == $num_primes) {
|
|
|
|
list($min, $temp) = $absoluteMin->divide($n);
|
|
|
|
if (!$temp->equals($this->zero)) {
|
|
|
|
$min = $min->add($this->one); // ie. ceil()
|
|
|
|
}
|
|
|
|
$primes[$i] = $generator->randomPrime($min, $finalMax, $timeout);
|
|
|
|
} else {
|
|
|
|
$primes[$i] = $generator->randomPrime($min, $max, $timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($primes[$i] === false) { // if we've reached the timeout
|
2011-02-06 17:35:48 +01:00
|
|
|
if (count($primes) > 1) {
|
|
|
|
$partialkey = '';
|
|
|
|
} else {
|
|
|
|
array_pop($primes);
|
|
|
|
$partialkey = serialize(array(
|
|
|
|
'primes' => $primes,
|
2010-01-21 01:52:11 +01:00
|
|
|
'coefficients' => $coefficients,
|
|
|
|
'lcm' => $lcm,
|
|
|
|
'exponents' => $exponents
|
2011-02-06 17:35:48 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'privatekey' => '',
|
|
|
|
'publickey' => '',
|
|
|
|
'partialkey' => $partialkey
|
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]);
|
|
|
|
|
|
|
|
$temp = $primes[$i]->subtract($this->one);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
$exponents[$i] = $e->modInverse($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;
|
|
|
|
} while (!$gcd->equals($this->one));
|
|
|
|
|
2013-08-01 04:50:40 +02:00
|
|
|
$d = $e->modInverse($temp);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$coefficients[2] = $primes[2]->modInverse($primes[1]);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// }
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'privatekey' => $this->_convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients),
|
|
|
|
'publickey' => $this->_convertPublicKey($n, $e),
|
|
|
|
'partialkey' => false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a private key to the appropriate format.
|
|
|
|
*
|
|
|
|
* @access private
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::setPrivateKeyFormat()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $RSAPrivateKey
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients)
|
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
$signed = $this->privateKeyFormat != self::PRIVATE_FORMAT_XML;
|
2009-12-03 09:19:00 +01:00
|
|
|
$num_primes = count($primes);
|
|
|
|
$raw = array(
|
|
|
|
'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi
|
2014-09-09 07:28:38 +02:00
|
|
|
'modulus' => $n->toBytes($signed),
|
|
|
|
'publicExponent' => $e->toBytes($signed),
|
|
|
|
'privateExponent' => $d->toBytes($signed),
|
|
|
|
'prime1' => $primes[1]->toBytes($signed),
|
|
|
|
'prime2' => $primes[2]->toBytes($signed),
|
|
|
|
'exponent1' => $exponents[1]->toBytes($signed),
|
|
|
|
'exponent2' => $exponents[2]->toBytes($signed),
|
|
|
|
'coefficient' => $coefficients[2]->toBytes($signed)
|
2009-12-03 09:19:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// if the format in question does not support multi-prime rsa and multi-prime rsa was used,
|
|
|
|
// call _convertPublicKey() instead.
|
|
|
|
switch ($this->privateKeyFormat) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_XML:
|
2011-11-07 02:35:12 +01:00
|
|
|
if ($num_primes != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return "<RSAKeyValue>\r\n" .
|
|
|
|
' <Modulus>' . base64_encode($raw['modulus']) . "</Modulus>\r\n" .
|
|
|
|
' <Exponent>' . base64_encode($raw['publicExponent']) . "</Exponent>\r\n" .
|
|
|
|
' <P>' . base64_encode($raw['prime1']) . "</P>\r\n" .
|
|
|
|
' <Q>' . base64_encode($raw['prime2']) . "</Q>\r\n" .
|
|
|
|
' <DP>' . base64_encode($raw['exponent1']) . "</DP>\r\n" .
|
|
|
|
' <DQ>' . base64_encode($raw['exponent2']) . "</DQ>\r\n" .
|
|
|
|
' <InverseQ>' . base64_encode($raw['coefficient']) . "</InverseQ>\r\n" .
|
|
|
|
' <D>' . base64_encode($raw['privateExponent']) . "</D>\r\n" .
|
|
|
|
'</RSAKeyValue>';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_PUTTY:
|
2011-11-07 02:35:12 +01:00
|
|
|
if ($num_primes != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
|
2012-08-23 15:59:49 +02:00
|
|
|
$encryption = (!empty($this->password) || is_string($this->password)) ? 'aes256-cbc' : 'none';
|
2011-11-07 02:35:12 +01:00
|
|
|
$key.= $encryption;
|
2013-05-06 01:38:34 +02:00
|
|
|
$key.= "\r\nComment: " . $this->comment . "\r\n";
|
2015-07-15 03:52:31 +02:00
|
|
|
$public = pack(
|
|
|
|
'Na*Na*Na*',
|
|
|
|
strlen('ssh-rsa'),
|
|
|
|
'ssh-rsa',
|
|
|
|
strlen($raw['publicExponent']),
|
|
|
|
$raw['publicExponent'],
|
|
|
|
strlen($raw['modulus']),
|
|
|
|
$raw['modulus']
|
2011-11-07 02:35:12 +01:00
|
|
|
);
|
2015-07-15 03:52:31 +02:00
|
|
|
$source = pack(
|
|
|
|
'Na*Na*Na*Na*',
|
|
|
|
strlen('ssh-rsa'),
|
|
|
|
'ssh-rsa',
|
|
|
|
strlen($encryption),
|
|
|
|
$encryption,
|
|
|
|
strlen($this->comment),
|
|
|
|
$this->comment,
|
|
|
|
strlen($public),
|
|
|
|
$public
|
2011-11-07 02:35:12 +01:00
|
|
|
);
|
|
|
|
$public = base64_encode($public);
|
2013-11-11 05:27:48 +01:00
|
|
|
$key.= "Public-Lines: " . ((strlen($public) + 63) >> 6) . "\r\n";
|
2011-11-07 02:35:12 +01:00
|
|
|
$key.= chunk_split($public, 64);
|
2015-07-15 03:52:31 +02:00
|
|
|
$private = pack(
|
|
|
|
'Na*Na*Na*Na*',
|
|
|
|
strlen($raw['privateExponent']),
|
|
|
|
$raw['privateExponent'],
|
|
|
|
strlen($raw['prime1']),
|
|
|
|
$raw['prime1'],
|
|
|
|
strlen($raw['prime2']),
|
|
|
|
$raw['prime2'],
|
|
|
|
strlen($raw['coefficient']),
|
|
|
|
$raw['coefficient']
|
2011-11-07 02:35:12 +01:00
|
|
|
);
|
2012-08-23 15:59:49 +02:00
|
|
|
if (empty($this->password) && !is_string($this->password)) {
|
2011-11-07 02:35:12 +01:00
|
|
|
$source.= pack('Na*', strlen($private), $private);
|
|
|
|
$hashkey = 'putty-private-key-file-mac-key';
|
|
|
|
} else {
|
2014-12-02 18:20:40 +01:00
|
|
|
$private.= Random::string(16 - (strlen($private) & 15));
|
2011-11-07 02:35:12 +01:00
|
|
|
$source.= pack('Na*', strlen($private), $private);
|
|
|
|
$sequence = 0;
|
|
|
|
$symkey = '';
|
|
|
|
while (strlen($symkey) < 32) {
|
|
|
|
$temp = pack('Na*', $sequence++, $this->password);
|
|
|
|
$symkey.= pack('H*', sha1($temp));
|
|
|
|
}
|
|
|
|
$symkey = substr($symkey, 0, 32);
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new AES();
|
2011-11-07 02:35:12 +01:00
|
|
|
|
|
|
|
$crypto->setKey($symkey);
|
|
|
|
$crypto->disablePadding();
|
|
|
|
$private = $crypto->encrypt($private);
|
|
|
|
$hashkey = 'putty-private-key-file-mac-key' . $this->password;
|
|
|
|
}
|
|
|
|
|
|
|
|
$private = base64_encode($private);
|
2013-11-11 05:27:48 +01:00
|
|
|
$key.= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
|
2011-11-07 02:35:12 +01:00
|
|
|
$key.= chunk_split($private, 64);
|
2014-12-17 01:16:54 +01:00
|
|
|
$hash = new Hash('sha1');
|
2011-11-07 02:35:12 +01:00
|
|
|
$hash->setKey(pack('H*', sha1($hashkey)));
|
|
|
|
$key.= 'Private-MAC: ' . bin2hex($hash->hash($source)) . "\r\n";
|
|
|
|
|
|
|
|
return $key;
|
2014-12-09 19:46:30 +01:00
|
|
|
default: // eg. self::PRIVATE_FORMAT_PKCS1
|
2009-12-03 09:19:00 +01:00
|
|
|
$components = array();
|
|
|
|
foreach ($raw as $name => $value) {
|
2014-12-09 19:46:30 +01:00
|
|
|
$components[$name] = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($value)), $value);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$RSAPrivateKey = implode('', $components);
|
|
|
|
|
|
|
|
if ($num_primes > 2) {
|
|
|
|
$OtherPrimeInfos = '';
|
|
|
|
for ($i = 3; $i <= $num_primes; $i++) {
|
|
|
|
// OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
|
|
|
|
//
|
|
|
|
// OtherPrimeInfo ::= SEQUENCE {
|
|
|
|
// prime INTEGER, -- ri
|
|
|
|
// exponent INTEGER, -- di
|
|
|
|
// coefficient INTEGER -- ti
|
|
|
|
// }
|
2014-12-09 19:46:30 +01:00
|
|
|
$OtherPrimeInfo = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($primes[$i]->toBytes(true))), $primes[$i]->toBytes(true));
|
|
|
|
$OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($exponents[$i]->toBytes(true))), $exponents[$i]->toBytes(true));
|
|
|
|
$OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($coefficients[$i]->toBytes(true))), $coefficients[$i]->toBytes(true));
|
|
|
|
$OtherPrimeInfos.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfo)), $OtherPrimeInfo);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2014-12-09 19:46:30 +01:00
|
|
|
$RSAPrivateKey.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfos)), $OtherPrimeInfos);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
$RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->privateKeyFormat == self::PRIVATE_FORMAT_PKCS8) {
|
2014-06-13 05:03:00 +02:00
|
|
|
$rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
|
2015-07-15 03:52:31 +02:00
|
|
|
$RSAPrivateKey = pack(
|
|
|
|
'Ca*a*Ca*a*',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_INTEGER,
|
2015-07-15 03:52:31 +02:00
|
|
|
"\01\00",
|
|
|
|
$rsaOID,
|
|
|
|
4,
|
|
|
|
$this->_encodeLength(strlen($RSAPrivateKey)),
|
|
|
|
$RSAPrivateKey
|
2014-06-13 20:17:37 +02:00
|
|
|
);
|
2014-12-09 19:46:30 +01:00
|
|
|
$RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
|
2014-06-13 18:43:32 +02:00
|
|
|
if (!empty($this->password) || is_string($this->password)) {
|
2014-12-02 18:20:40 +01:00
|
|
|
$salt = Random::string(8);
|
2014-06-13 18:43:32 +02:00
|
|
|
$iterationCount = 2048;
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new DES();
|
2014-06-13 18:43:32 +02:00
|
|
|
$crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
|
|
|
|
$RSAPrivateKey = $crypto->encrypt($RSAPrivateKey);
|
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$parameters = pack(
|
|
|
|
'Ca*a*Ca*N',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_OCTETSTRING,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($salt)),
|
|
|
|
$salt,
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_INTEGER,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(4),
|
|
|
|
$iterationCount
|
2014-06-13 18:43:32 +02:00
|
|
|
);
|
|
|
|
$pbeWithMD5AndDES_CBC = "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03";
|
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$encryptionAlgorithm = pack(
|
|
|
|
'Ca*a*Ca*a*',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_OBJECT,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($pbeWithMD5AndDES_CBC)),
|
|
|
|
$pbeWithMD5AndDES_CBC,
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_SEQUENCE,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($parameters)),
|
|
|
|
$parameters
|
2014-06-13 18:43:32 +02:00
|
|
|
);
|
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$RSAPrivateKey = pack(
|
|
|
|
'Ca*a*Ca*a*',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_SEQUENCE,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($encryptionAlgorithm)),
|
|
|
|
$encryptionAlgorithm,
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_OCTETSTRING,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($RSAPrivateKey)),
|
|
|
|
$RSAPrivateKey
|
2014-06-13 18:43:32 +02:00
|
|
|
);
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
$RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
|
2014-06-13 18:43:32 +02:00
|
|
|
|
|
|
|
$RSAPrivateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" .
|
|
|
|
chunk_split(base64_encode($RSAPrivateKey), 64) .
|
|
|
|
'-----END ENCRYPTED PRIVATE KEY-----';
|
|
|
|
} else {
|
|
|
|
$RSAPrivateKey = "-----BEGIN PRIVATE KEY-----\r\n" .
|
|
|
|
chunk_split(base64_encode($RSAPrivateKey), 64) .
|
|
|
|
'-----END PRIVATE KEY-----';
|
|
|
|
}
|
2014-06-13 05:03:00 +02:00
|
|
|
return $RSAPrivateKey;
|
|
|
|
}
|
|
|
|
|
2012-08-23 15:59:49 +02:00
|
|
|
if (!empty($this->password) || is_string($this->password)) {
|
2014-12-02 18:20:40 +01:00
|
|
|
$iv = Random::string(8);
|
2009-12-03 09:19:00 +01:00
|
|
|
$symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key
|
|
|
|
$symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
|
2014-12-17 01:16:54 +01:00
|
|
|
$des = new TripleDES();
|
2009-12-03 09:19:00 +01:00
|
|
|
$des->setKey($symkey);
|
|
|
|
$des->setIV($iv);
|
|
|
|
$iv = strtoupper(bin2hex($iv));
|
|
|
|
$RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
|
|
|
|
"Proc-Type: 4,ENCRYPTED\r\n" .
|
|
|
|
"DEK-Info: DES-EDE3-CBC,$iv\r\n" .
|
|
|
|
"\r\n" .
|
2012-12-18 14:11:24 +01:00
|
|
|
chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) .
|
2009-12-03 09:19:00 +01:00
|
|
|
'-----END RSA PRIVATE KEY-----';
|
|
|
|
} else {
|
|
|
|
$RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
|
2012-12-18 14:11:24 +01:00
|
|
|
chunk_split(base64_encode($RSAPrivateKey), 64) .
|
2009-12-03 09:19:00 +01:00
|
|
|
'-----END RSA PRIVATE KEY-----';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $RSAPrivateKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a public key to the appropriate format
|
|
|
|
*
|
|
|
|
* @access private
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::setPublicKeyFormat()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $RSAPrivateKey
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _convertPublicKey($n, $e)
|
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
$signed = $this->publicKeyFormat != self::PUBLIC_FORMAT_XML;
|
2014-09-06 18:13:11 +02:00
|
|
|
|
2014-09-09 07:28:38 +02:00
|
|
|
$modulus = $n->toBytes($signed);
|
|
|
|
$publicExponent = $e->toBytes($signed);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
switch ($this->publicKeyFormat) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_RAW:
|
2009-12-03 09:19:00 +01:00
|
|
|
return array('e' => $e->copy(), 'n' => $n->copy());
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_XML:
|
2011-11-07 02:35:12 +01:00
|
|
|
return "<RSAKeyValue>\r\n" .
|
2011-11-07 09:25:59 +01:00
|
|
|
' <Modulus>' . base64_encode($modulus) . "</Modulus>\r\n" .
|
|
|
|
' <Exponent>' . base64_encode($publicExponent) . "</Exponent>\r\n" .
|
2011-11-07 02:35:12 +01:00
|
|
|
'</RSAKeyValue>';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_OPENSSH:
|
2009-12-03 09:19:00 +01:00
|
|
|
// from <http://tools.ietf.org/html/rfc4253#page-15>:
|
|
|
|
// string "ssh-rsa"
|
|
|
|
// mpint e
|
|
|
|
// mpint n
|
|
|
|
$RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
|
2013-05-06 01:38:34 +02:00
|
|
|
$RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $this->comment;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
return $RSAPublicKey;
|
2014-12-09 19:46:30 +01:00
|
|
|
default: // eg. self::PUBLIC_FORMAT_PKCS1_RAW or self::PUBLIC_FORMAT_PKCS1
|
2009-12-03 09:19:00 +01:00
|
|
|
// from <http://tools.ietf.org/html/rfc3447#appendix-A.1.1>:
|
|
|
|
// RSAPublicKey ::= SEQUENCE {
|
|
|
|
// modulus INTEGER, -- n
|
|
|
|
// publicExponent INTEGER -- e
|
|
|
|
// }
|
|
|
|
$components = array(
|
2014-12-09 19:46:30 +01:00
|
|
|
'modulus' => pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($modulus)), $modulus),
|
|
|
|
'publicExponent' => pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($publicExponent)), $publicExponent)
|
2009-12-03 09:19:00 +01:00
|
|
|
);
|
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$RSAPublicKey = pack(
|
|
|
|
'Ca*a*a*',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_SEQUENCE,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])),
|
|
|
|
$components['modulus'],
|
|
|
|
$components['publicExponent']
|
2009-12-03 09:19:00 +01:00
|
|
|
);
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->publicKeyFormat == self::PUBLIC_FORMAT_PKCS1_RAW) {
|
2014-06-11 05:16:59 +02:00
|
|
|
$RSAPublicKey = "-----BEGIN RSA PUBLIC KEY-----\r\n" .
|
|
|
|
chunk_split(base64_encode($RSAPublicKey), 64) .
|
|
|
|
'-----END RSA PUBLIC KEY-----';
|
|
|
|
} else {
|
2012-10-29 18:21:25 +01:00
|
|
|
// sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption.
|
2012-09-03 08:32:57 +02:00
|
|
|
$rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
|
|
|
|
$RSAPublicKey = chr(0) . $RSAPublicKey;
|
|
|
|
$RSAPublicKey = chr(3) . $this->_encodeLength(strlen($RSAPublicKey)) . $RSAPublicKey;
|
2012-08-26 08:36:34 +02:00
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$RSAPublicKey = pack(
|
|
|
|
'Ca*a*',
|
2015-07-17 13:36:18 +02:00
|
|
|
self::ASN1_SEQUENCE,
|
2015-07-15 03:52:31 +02:00
|
|
|
$this->_encodeLength(strlen($rsaOID . $RSAPublicKey)),
|
|
|
|
$rsaOID . $RSAPublicKey
|
2012-09-03 08:32:57 +02:00
|
|
|
);
|
2012-08-26 08:36:34 +02:00
|
|
|
|
2014-06-11 05:16:59 +02:00
|
|
|
$RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
|
|
|
|
chunk_split(base64_encode($RSAPublicKey), 64) .
|
|
|
|
'-----END PUBLIC KEY-----';
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
return $RSAPublicKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Break a public or private key down into its constituant components
|
|
|
|
*
|
|
|
|
* @access private
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_convertPublicKey()
|
|
|
|
* @see self::_convertPrivateKey()
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param int $type
|
|
|
|
* @return array
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _parseKey($key, $type)
|
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($type != self::PUBLIC_FORMAT_RAW && !is_string($key)) {
|
2011-07-11 02:02:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
switch ($type) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_RAW:
|
2009-12-03 09:19:00 +01:00
|
|
|
if (!is_array($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$components = array();
|
|
|
|
switch (true) {
|
|
|
|
case isset($key['e']):
|
|
|
|
$components['publicExponent'] = $key['e']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key['exponent']):
|
|
|
|
$components['publicExponent'] = $key['exponent']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key['publicExponent']):
|
|
|
|
$components['publicExponent'] = $key['publicExponent']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key[0]):
|
|
|
|
$components['publicExponent'] = $key[0]->copy();
|
|
|
|
}
|
|
|
|
switch (true) {
|
|
|
|
case isset($key['n']):
|
|
|
|
$components['modulus'] = $key['n']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key['modulo']):
|
|
|
|
$components['modulus'] = $key['modulo']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key['modulus']):
|
|
|
|
$components['modulus'] = $key['modulus']->copy();
|
|
|
|
break;
|
|
|
|
case isset($key[1]):
|
|
|
|
$components['modulus'] = $key[1]->copy();
|
|
|
|
}
|
2011-07-11 02:02:53 +02:00
|
|
|
return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_PKCS1:
|
|
|
|
case self::PRIVATE_FORMAT_PKCS8:
|
|
|
|
case self::PUBLIC_FORMAT_PKCS1:
|
2009-12-03 09:19:00 +01:00
|
|
|
/* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is
|
|
|
|
"outside the scope" of PKCS#1. PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to
|
|
|
|
protect private keys, however, that's not what OpenSSL* does. OpenSSL protects private keys by adding
|
|
|
|
two new "fields" to the key - DEK-Info and Proc-Type. These fields are discussed here:
|
|
|
|
|
|
|
|
http://tools.ietf.org/html/rfc1421#section-4.6.1.1
|
|
|
|
http://tools.ietf.org/html/rfc1421#section-4.6.1.3
|
|
|
|
|
|
|
|
DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell.
|
|
|
|
DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation
|
|
|
|
function. As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's
|
2013-12-26 00:33:08 +01:00
|
|
|
own implementation. ie. the implementation *is* the standard and any bugs that may exist in that
|
2009-12-03 09:19:00 +01:00
|
|
|
implementation are part of the standard, as well.
|
|
|
|
|
|
|
|
* OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */
|
2010-02-26 04:40:26 +01:00
|
|
|
if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
|
|
|
|
$iv = pack('H*', trim($matches[2]));
|
2010-09-05 05:04:29 +02:00
|
|
|
$symkey = pack('H*', md5($this->password . substr($iv, 0, 8))); // symkey is short for symmetric key
|
2013-02-28 03:00:20 +01:00
|
|
|
$symkey.= pack('H*', md5($symkey . $this->password . substr($iv, 0, 8)));
|
2013-12-25 01:37:43 +01:00
|
|
|
// remove the Proc-Type / DEK-Info sections as they're no longer needed
|
|
|
|
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
|
|
|
|
$ciphertext = $this->_extractBER($key);
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($ciphertext === false) {
|
2010-02-26 04:40:26 +01:00
|
|
|
$ciphertext = $key;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2010-02-26 04:40:26 +01:00
|
|
|
switch ($matches[1]) {
|
2013-02-28 03:00:20 +01:00
|
|
|
case 'AES-256-CBC':
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new AES();
|
2013-02-28 03:00:20 +01:00
|
|
|
break;
|
2010-09-05 05:04:29 +02:00
|
|
|
case 'AES-128-CBC':
|
|
|
|
$symkey = substr($symkey, 0, 16);
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new AES();
|
2010-09-05 05:04:29 +02:00
|
|
|
break;
|
2010-09-26 05:10:20 +02:00
|
|
|
case 'DES-EDE3-CFB':
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new TripleDES(Base::MODE_CFB);
|
2010-09-26 05:10:20 +02:00
|
|
|
break;
|
2010-02-26 04:40:26 +01:00
|
|
|
case 'DES-EDE3-CBC':
|
2013-02-28 03:00:20 +01:00
|
|
|
$symkey = substr($symkey, 0, 24);
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new TripleDES();
|
2010-02-26 04:40:26 +01:00
|
|
|
break;
|
|
|
|
case 'DES-CBC':
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new DES();
|
2010-02-26 04:40:26 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2010-02-26 04:40:26 +01:00
|
|
|
$crypto->setKey($symkey);
|
|
|
|
$crypto->setIV($iv);
|
|
|
|
$decoded = $crypto->decrypt($ciphertext);
|
2009-12-03 09:19:00 +01:00
|
|
|
} else {
|
2013-12-25 01:37:43 +01:00
|
|
|
$decoded = $this->_extractBER($key);
|
2009-12-06 08:26:52 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 04:40:26 +01:00
|
|
|
if ($decoded !== false) {
|
|
|
|
$key = $decoded;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$components = array();
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->_decodeLength($key) != strlen($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = ord($this->_string_shift($key));
|
2011-05-28 18:23:15 +02:00
|
|
|
/* intended for keys for which OpenSSL's asn1parse returns the following:
|
|
|
|
|
|
|
|
0:d=0 hl=4 l= 631 cons: SEQUENCE
|
|
|
|
4:d=1 hl=2 l= 1 prim: INTEGER :00
|
|
|
|
7:d=1 hl=2 l= 13 cons: SEQUENCE
|
|
|
|
9:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
|
|
|
|
20:d=2 hl=2 l= 0 prim: NULL
|
2014-06-13 05:03:00 +02:00
|
|
|
22:d=1 hl=4 l= 609 prim: OCTET STRING
|
|
|
|
|
|
|
|
ie. PKCS8 keys*/
|
2011-05-28 18:23:15 +02:00
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($tag == self::ASN1_INTEGER && substr($key, 0, 3) == "\x01\x00\x30") {
|
2011-05-28 18:23:15 +02:00
|
|
|
$this->_string_shift($key, 3);
|
2014-12-09 19:46:30 +01:00
|
|
|
$tag = self::ASN1_SEQUENCE;
|
2011-05-28 18:23:15 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($tag == self::ASN1_SEQUENCE) {
|
2014-06-13 15:56:00 +02:00
|
|
|
$temp = $this->_string_shift($key, $this->_decodeLength($key));
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($temp)) != self::ASN1_OBJECT) {
|
2014-06-13 15:56:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$length = $this->_decodeLength($temp);
|
|
|
|
switch ($this->_string_shift($temp, $length)) {
|
|
|
|
case "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01": // rsaEncryption
|
|
|
|
break;
|
|
|
|
case "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03": // pbeWithMD5AndDES-CBC
|
|
|
|
/*
|
|
|
|
PBEParameter ::= SEQUENCE {
|
|
|
|
salt OCTET STRING (SIZE(8)),
|
|
|
|
iterationCount INTEGER }
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($temp)) != self::ASN1_SEQUENCE) {
|
2014-06-13 15:56:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->_decodeLength($temp) != strlen($temp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->_string_shift($temp); // assume it's an octet string
|
|
|
|
$salt = $this->_string_shift($temp, $this->_decodeLength($temp));
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($temp)) != self::ASN1_INTEGER) {
|
2014-06-13 15:56:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->_decodeLength($temp);
|
|
|
|
list(, $iterationCount) = unpack('N', str_pad($temp, 4, chr(0), STR_PAD_LEFT));
|
|
|
|
$this->_string_shift($key); // assume it's an octet string
|
|
|
|
$length = $this->_decodeLength($key);
|
|
|
|
if (strlen($key) != $length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new DES();
|
2014-06-13 18:43:32 +02:00
|
|
|
$crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
|
2014-06-13 15:56:00 +02:00
|
|
|
$key = $crypto->decrypt($key);
|
|
|
|
if ($key === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-09 19:46:30 +01:00
|
|
|
return $this->_parseKey($key, self::PRIVATE_FORMAT_PKCS1);
|
2014-06-13 15:56:00 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-26 04:40:26 +01:00
|
|
|
/* intended for keys for which OpenSSL's asn1parse returns the following:
|
|
|
|
|
|
|
|
0:d=0 hl=4 l= 290 cons: SEQUENCE
|
|
|
|
4:d=1 hl=2 l= 13 cons: SEQUENCE
|
|
|
|
6:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
|
|
|
|
17:d=2 hl=2 l= 0 prim: NULL
|
|
|
|
19:d=1 hl=4 l= 271 prim: BIT STRING */
|
2011-05-28 18:23:15 +02:00
|
|
|
$tag = ord($this->_string_shift($key)); // skip over the BIT STRING / OCTET STRING tag
|
|
|
|
$this->_decodeLength($key); // skip over the BIT STRING / OCTET STRING length
|
2010-02-26 04:40:26 +01:00
|
|
|
// "The initial octet shall encode, as an unsigned binary integer wtih bit 1 as the least significant bit, the number of
|
2011-05-28 18:23:15 +02:00
|
|
|
// unused bits in the final subsequent octet. The number shall be in the range zero to seven."
|
2010-02-26 04:40:26 +01:00
|
|
|
// -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf (section 8.6.2.2)
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($tag == self::ASN1_BITSTRING) {
|
2011-05-28 18:23:15 +02:00
|
|
|
$this->_string_shift($key);
|
|
|
|
}
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->_decodeLength($key) != strlen($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$tag = ord($this->_string_shift($key));
|
|
|
|
}
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($tag != self::ASN1_INTEGER) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
$length = $this->_decodeLength($key);
|
|
|
|
$temp = $this->_string_shift($key, $length);
|
|
|
|
if (strlen($temp) != 1 || ord($temp) > 2) {
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['modulus'] = new BigInteger($temp, 256);
|
2014-12-09 19:46:30 +01:00
|
|
|
$this->_string_shift($key); // skip over self::ASN1_INTEGER
|
2009-12-03 09:19:00 +01:00
|
|
|
$length = $this->_decodeLength($key);
|
2014-12-09 19:46:30 +01:00
|
|
|
$components[$type == self::PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
return $components;
|
|
|
|
}
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($key)) != self::ASN1_INTEGER) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['modulus'] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['publicExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['primes'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['exponents'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['coefficients'] = array(2 => new BigInteger($this->_string_shift($key, $length), 256));
|
2010-02-28 06:28:38 +01:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
if (!empty($key)) {
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_decodeLength($key);
|
|
|
|
while (!empty($key)) {
|
2014-12-09 19:46:30 +01:00
|
|
|
if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
|
2010-02-26 04:40:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_decodeLength($key);
|
|
|
|
$key = substr($key, 1);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->_string_shift($key);
|
|
|
|
$length = $this->_decodeLength($key);
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['coefficients'][] = new BigInteger($this->_string_shift($key, $length), 256);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $components;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_OPENSSH:
|
2013-05-06 01:38:34 +02:00
|
|
|
$parts = explode(' ', $key, 3);
|
|
|
|
|
|
|
|
$key = isset($parts[1]) ? base64_decode($parts[1]) : false;
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($key === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-06 01:38:34 +02:00
|
|
|
$comment = isset($parts[2]) ? $parts[2] : false;
|
|
|
|
|
2010-02-26 04:40:26 +01:00
|
|
|
$cleanup = substr($key, 0, 11) == "\0\0\0\7ssh-rsa";
|
|
|
|
|
2011-07-11 02:02:53 +02:00
|
|
|
if (strlen($key) <= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($key, 4)));
|
2014-06-02 20:09:47 +02:00
|
|
|
$publicExponent = new BigInteger($this->_string_shift($key, $length), -256);
|
2011-07-11 02:02:53 +02:00
|
|
|
if (strlen($key) <= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($key, 4)));
|
2014-06-02 20:09:47 +02:00
|
|
|
$modulus = new BigInteger($this->_string_shift($key, $length), -256);
|
2009-12-03 09:19:00 +01:00
|
|
|
|
2010-02-26 04:40:26 +01:00
|
|
|
if ($cleanup && strlen($key)) {
|
2011-07-11 02:02:53 +02:00
|
|
|
if (strlen($key) <= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-26 04:40:26 +01:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($key, 4)));
|
2014-06-02 20:09:47 +02:00
|
|
|
$realModulus = new BigInteger($this->_string_shift($key, $length), -256);
|
2011-07-11 02:37:46 +02:00
|
|
|
return strlen($key) ? false : array(
|
2012-07-04 20:36:26 +02:00
|
|
|
'modulus' => $realModulus,
|
2013-05-06 01:38:34 +02:00
|
|
|
'publicExponent' => $modulus,
|
|
|
|
'comment' => $comment
|
2010-02-26 04:40:26 +01:00
|
|
|
);
|
|
|
|
} else {
|
2011-07-11 02:37:46 +02:00
|
|
|
return strlen($key) ? false : array(
|
2010-02-26 04:40:26 +01:00
|
|
|
'modulus' => $modulus,
|
2013-05-06 01:38:34 +02:00
|
|
|
'publicExponent' => $publicExponent,
|
|
|
|
'comment' => $comment
|
2010-02-26 04:40:26 +01:00
|
|
|
);
|
|
|
|
}
|
2011-04-18 14:17:40 +02:00
|
|
|
// http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
|
|
|
|
// http://en.wikipedia.org/wiki/XML_Signature
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_XML:
|
|
|
|
case self::PUBLIC_FORMAT_XML:
|
2011-04-18 14:17:40 +02:00
|
|
|
$this->components = array();
|
|
|
|
|
|
|
|
$xml = xml_parser_create('UTF-8');
|
|
|
|
xml_set_object($xml, $this);
|
|
|
|
xml_set_element_handler($xml, '_start_element_handler', '_stop_element_handler');
|
|
|
|
xml_set_character_data_handler($xml, '_data_handler');
|
2012-11-20 06:00:04 +01:00
|
|
|
// add <xml></xml> to account for "dangling" tags like <BitStrength>...</BitStrength> that are sometimes added
|
|
|
|
if (!xml_parse($xml, '<xml>' . $key . '</xml>')) {
|
2011-04-18 14:17:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-11 02:37:46 +02:00
|
|
|
return isset($this->components['modulus']) && isset($this->components['publicExponent']) ? $this->components : false;
|
2011-04-18 14:17:40 +02:00
|
|
|
// from PuTTY's SSHPUBK.C
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_PUTTY:
|
2011-04-18 14:17:40 +02:00
|
|
|
$components = array();
|
|
|
|
$key = preg_split('#\r\n|\r|\n#', $key);
|
|
|
|
$type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0]));
|
|
|
|
if ($type != 'ssh-rsa') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1]));
|
2013-05-06 01:38:34 +02:00
|
|
|
$comment = trim(preg_replace('#Comment: (.+)#', '$1', $key[2]));
|
2011-04-18 14:17:40 +02:00
|
|
|
|
|
|
|
$publicLength = trim(preg_replace('#Public-Lines: (\d+)#', '$1', $key[3]));
|
|
|
|
$public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
|
|
|
|
$public = substr($public, 11);
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($public, 4)));
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['publicExponent'] = new BigInteger($this->_string_shift($public, $length), -256);
|
2011-04-18 14:17:40 +02:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($public, 4)));
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['modulus'] = new BigInteger($this->_string_shift($public, $length), -256);
|
2011-04-18 14:17:40 +02:00
|
|
|
|
|
|
|
$privateLength = trim(preg_replace('#Private-Lines: (\d+)#', '$1', $key[$publicLength + 4]));
|
|
|
|
$private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
|
|
|
|
|
|
|
|
switch ($encryption) {
|
|
|
|
case 'aes256-cbc':
|
|
|
|
$symkey = '';
|
|
|
|
$sequence = 0;
|
|
|
|
while (strlen($symkey) < 32) {
|
|
|
|
$temp = pack('Na*', $sequence++, $this->password);
|
|
|
|
$symkey.= pack('H*', sha1($temp));
|
|
|
|
}
|
|
|
|
$symkey = substr($symkey, 0, 32);
|
2014-12-17 01:16:54 +01:00
|
|
|
$crypto = new AES();
|
2011-04-18 14:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($encryption != 'none') {
|
|
|
|
$crypto->setKey($symkey);
|
|
|
|
$crypto->disablePadding();
|
|
|
|
$private = $crypto->decrypt($private);
|
|
|
|
if ($private === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($private, 4)));
|
2011-08-17 02:12:42 +02:00
|
|
|
if (strlen($private) < $length) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['privateExponent'] = new BigInteger($this->_string_shift($private, $length), -256);
|
2011-04-18 14:17:40 +02:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($private, 4)));
|
2011-08-17 02:12:42 +02:00
|
|
|
if (strlen($private) < $length) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['primes'] = array(1 => new BigInteger($this->_string_shift($private, $length), -256));
|
2011-04-18 14:17:40 +02:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($private, 4)));
|
2011-08-17 02:12:42 +02:00
|
|
|
if (strlen($private) < $length) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['primes'][] = new BigInteger($this->_string_shift($private, $length), -256);
|
2011-04-18 14:17:40 +02:00
|
|
|
|
|
|
|
$temp = $components['primes'][1]->subtract($this->one);
|
|
|
|
$components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp));
|
|
|
|
$temp = $components['primes'][2]->subtract($this->one);
|
|
|
|
$components['exponents'][] = $components['publicExponent']->modInverse($temp);
|
|
|
|
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($private, 4)));
|
2011-08-17 02:12:42 +02:00
|
|
|
if (strlen($private) < $length) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-02 20:09:47 +02:00
|
|
|
$components['coefficients'] = array(2 => new BigInteger($this->_string_shift($private, $length), -256));
|
2011-04-18 14:17:40 +02:00
|
|
|
|
|
|
|
return $components;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 01:53:26 +02:00
|
|
|
/**
|
|
|
|
* Returns the key size
|
|
|
|
*
|
|
|
|
* More specifically, this returns the size of the modulo in bits.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @return int
|
2012-10-08 01:53:26 +02:00
|
|
|
*/
|
|
|
|
function getSize()
|
|
|
|
{
|
|
|
|
return !isset($this->modulus) ? 0 : strlen($this->modulus->toBits());
|
|
|
|
}
|
|
|
|
|
2011-04-18 14:17:40 +02:00
|
|
|
/**
|
|
|
|
* Start Element Handler
|
|
|
|
*
|
|
|
|
* Called by xml_set_element_handler()
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param resource $parser
|
|
|
|
* @param string $name
|
|
|
|
* @param array $attribs
|
2011-04-18 14:17:40 +02:00
|
|
|
*/
|
|
|
|
function _start_element_handler($parser, $name, $attribs)
|
|
|
|
{
|
|
|
|
//$name = strtoupper($name);
|
|
|
|
switch ($name) {
|
|
|
|
case 'MODULUS':
|
|
|
|
$this->current = &$this->components['modulus'];
|
|
|
|
break;
|
|
|
|
case 'EXPONENT':
|
|
|
|
$this->current = &$this->components['publicExponent'];
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
$this->current = &$this->components['primes'][1];
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
$this->current = &$this->components['primes'][2];
|
|
|
|
break;
|
|
|
|
case 'DP':
|
|
|
|
$this->current = &$this->components['exponents'][1];
|
|
|
|
break;
|
|
|
|
case 'DQ':
|
|
|
|
$this->current = &$this->components['exponents'][2];
|
|
|
|
break;
|
|
|
|
case 'INVERSEQ':
|
|
|
|
$this->current = &$this->components['coefficients'][2];
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
$this->current = &$this->components['privateExponent'];
|
|
|
|
}
|
|
|
|
$this->current = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop Element Handler
|
|
|
|
*
|
|
|
|
* Called by xml_set_element_handler()
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param resource $parser
|
|
|
|
* @param string $name
|
2011-04-18 14:17:40 +02:00
|
|
|
*/
|
|
|
|
function _stop_element_handler($parser, $name)
|
|
|
|
{
|
2013-10-24 22:48:15 +02:00
|
|
|
if (isset($this->current)) {
|
2014-06-02 20:09:47 +02:00
|
|
|
$this->current = new BigInteger(base64_decode($this->current), 256);
|
2013-10-24 22:48:15 +02:00
|
|
|
unset($this->current);
|
2011-04-18 14:17:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data Handler
|
|
|
|
*
|
|
|
|
* Called by xml_set_character_data_handler()
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param resource $parser
|
|
|
|
* @param string $data
|
2011-04-18 14:17:40 +02:00
|
|
|
*/
|
|
|
|
function _data_handler($parser, $data)
|
|
|
|
{
|
|
|
|
if (!isset($this->current) || is_object($this->current)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->current.= trim($data);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
*
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param int $type optional
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2011-04-18 14:17:40 +02:00
|
|
|
function loadKey($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;
|
|
|
|
$this->k = $key->k;
|
|
|
|
$this->hLen = $key->hLen;
|
|
|
|
$this->sLen = $key->sLen;
|
|
|
|
$this->mgfHLen = $key->mgfHLen;
|
|
|
|
$this->encryptionMode = $key->encryptionMode;
|
|
|
|
$this->signatureMode = $key->signatureMode;
|
|
|
|
$this->password = $key->password;
|
|
|
|
$this->configFile = $key->configFile;
|
|
|
|
$this->comment = $key->comment;
|
|
|
|
|
|
|
|
if (is_object($key->hash)) {
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->hash = new Hash($key->hash->getHash());
|
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)) {
|
|
|
|
$this->modulus = $key->modulus->copy();
|
|
|
|
}
|
|
|
|
if (is_object($key->exponent)) {
|
|
|
|
$this->exponent = $key->exponent->copy();
|
|
|
|
}
|
|
|
|
if (is_object($key->publicExponent)) {
|
|
|
|
$this->publicExponent = $key->publicExponent->copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->primes = array();
|
|
|
|
$this->exponents = array();
|
|
|
|
$this->coefficients = array();
|
|
|
|
|
|
|
|
foreach ($this->primes as $prime) {
|
|
|
|
$this->primes[] = $prime->copy();
|
|
|
|
}
|
|
|
|
foreach ($this->exponents as $exponent) {
|
|
|
|
$this->exponents[] = $exponent->copy();
|
|
|
|
}
|
|
|
|
foreach ($this->coefficients as $coefficient) {
|
|
|
|
$this->coefficients[] = $coefficient->copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-18 14:17:40 +02:00
|
|
|
if ($type === false) {
|
|
|
|
$types = array(
|
2014-12-09 19:46:30 +01:00
|
|
|
self::PUBLIC_FORMAT_RAW,
|
|
|
|
self::PRIVATE_FORMAT_PKCS1,
|
|
|
|
self::PRIVATE_FORMAT_XML,
|
|
|
|
self::PRIVATE_FORMAT_PUTTY,
|
|
|
|
self::PUBLIC_FORMAT_OPENSSH
|
2011-04-18 14:17:40 +02:00
|
|
|
);
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$components = $this->_parseKey($key, $type);
|
|
|
|
if ($components !== false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$components = $this->_parseKey($key, $type);
|
|
|
|
}
|
|
|
|
|
2009-12-06 08:26:52 +01:00
|
|
|
if ($components === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-06 01:38:34 +02:00
|
|
|
if (isset($components['comment']) && $components['comment'] !== false) {
|
|
|
|
$this->comment = $components['comment'];
|
|
|
|
}
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->modulus = $components['modulus'];
|
|
|
|
$this->k = strlen($this->modulus->toBytes());
|
|
|
|
$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 {
|
|
|
|
$this->primes = array();
|
|
|
|
$this->exponents = array();
|
|
|
|
$this->coefficients = array();
|
|
|
|
$this->publicExponent = false;
|
|
|
|
}
|
2009-12-08 00:22:05 +01:00
|
|
|
|
2014-04-17 17:30:32 +02:00
|
|
|
switch ($type) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PUBLIC_FORMAT_OPENSSH:
|
|
|
|
case self::PUBLIC_FORMAT_RAW:
|
2014-04-17 17:30:32 +02:00
|
|
|
$this->setPublicKey();
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::PRIVATE_FORMAT_PKCS1:
|
2014-04-17 17:30:32 +02:00
|
|
|
switch (true) {
|
|
|
|
case strpos($key, '-BEGIN PUBLIC KEY-') !== false:
|
|
|
|
case strpos($key, '-BEGIN RSA PUBLIC KEY-') !== false:
|
|
|
|
$this->setPublicKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 00:22:05 +01:00
|
|
|
return true;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the password
|
|
|
|
*
|
|
|
|
* Private keys can be encrypted with a password. To unset the password, pass in the empty string or false.
|
2012-08-23 15:59:49 +02:00
|
|
|
* Or rather, pass in $password such that empty($password) && !is_string($password) is true.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::createKey()
|
|
|
|
* @see self::loadKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $password
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2012-08-23 15:59:49 +02:00
|
|
|
function setPassword($password = false)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
|
|
|
$this->password = $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::getPublicKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key optional
|
|
|
|
* @param int $type optional
|
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2012-04-17 08:21:42 +02:00
|
|
|
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)) {
|
|
|
|
$this->publicExponent = $this->exponent;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:13:34 +01:00
|
|
|
if ($type === false) {
|
|
|
|
$types = array(
|
2014-12-09 19:46:30 +01:00
|
|
|
self::PUBLIC_FORMAT_RAW,
|
|
|
|
self::PUBLIC_FORMAT_PKCS1,
|
|
|
|
self::PUBLIC_FORMAT_XML,
|
|
|
|
self::PUBLIC_FORMAT_OPENSSH
|
2012-03-11 20:13:34 +01:00
|
|
|
);
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$components = $this->_parseKey($key, $type);
|
|
|
|
if ($components !== false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$components = $this->_parseKey($key, $type);
|
|
|
|
}
|
2010-07-11 04:33:13 +02:00
|
|
|
|
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'];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::getPublicKey()
|
2014-04-17 17:30:32 +02:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key optional
|
|
|
|
* @param int $type optional
|
|
|
|
* @return bool
|
2014-04-17 17:30:32 +02:00
|
|
|
*/
|
|
|
|
function setPrivateKey($key = false, $type = false)
|
|
|
|
{
|
|
|
|
if ($key === false && !empty($this->publicExponent)) {
|
|
|
|
unset($this->publicExponent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
$rsa = new RSA();
|
2014-04-17 17:30:32 +02:00
|
|
|
if (!$rsa->loadKey($key, $type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unset($rsa->publicExponent);
|
|
|
|
|
|
|
|
// don't overwrite the old key if the new key is invalid
|
|
|
|
$this->loadKey($rsa);
|
|
|
|
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.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::getPublicKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param int $type optional
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
function getPublicKey($type = self::PUBLIC_FORMAT_PKCS8)
|
2009-12-03 09:19:00 +01:00
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->publicExponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
$oldFormat = $this->publicKeyFormat;
|
|
|
|
$this->publicKeyFormat = $type;
|
|
|
|
$temp = $this->_convertPublicKey($this->modulus, $this->publicExponent);
|
|
|
|
$this->publicKeyFormat = $oldFormat;
|
|
|
|
return $temp;
|
|
|
|
}
|
|
|
|
|
2015-05-07 05:55:12 +02:00
|
|
|
/**
|
|
|
|
* Returns the public key's fingerprint
|
|
|
|
*
|
|
|
|
* The public key's fingerprint is returned, which is equivalent to running `ssh-keygen -lf rsa.pub`. If there is
|
|
|
|
* no public key currently loaded, false is returned.
|
|
|
|
* Example output (md5): "c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87" (as specified by RFC 4716)
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $algorithm The hashing algorithm to be used. Valid options are 'md5' and 'sha256'. False is returned
|
2015-05-07 05:55:12 +02:00
|
|
|
* for invalid values.
|
2015-10-05 17:08:46 +02:00
|
|
|
* @return mixed
|
2015-05-07 05:55:12 +02:00
|
|
|
*/
|
|
|
|
public function getPublicKeyFingerprint($algorithm = 'md5')
|
|
|
|
{
|
|
|
|
if (empty($this->modulus) || empty($this->publicExponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$modulus = $this->modulus->toBytes(true);
|
|
|
|
$publicExponent = $this->publicExponent->toBytes(true);
|
|
|
|
|
|
|
|
$RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
|
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
switch ($algorithm) {
|
2015-05-07 05:55:12 +02:00
|
|
|
case 'sha256':
|
2015-05-05 08:51:17 +02:00
|
|
|
$hash = new Hash('sha256');
|
2015-05-07 05:55:12 +02:00
|
|
|
$base = base64_encode($hash->hash($RSAPublicKey));
|
|
|
|
return substr($base, 0, strlen($base) - 1);
|
|
|
|
case 'md5':
|
|
|
|
return substr(chunk_split(md5($RSAPublicKey), 2, ':'), 0, -1);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 02:35:12 +01:00
|
|
|
/**
|
|
|
|
* Returns the private key
|
|
|
|
*
|
|
|
|
* The private key is only returned if the currently loaded key contains the constituent prime numbers.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::getPublicKey()
|
2011-11-07 02:35:12 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param int $type optional
|
2015-10-05 17:08:46 +02:00
|
|
|
* @return mixed
|
2011-11-07 02:35:12 +01:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
function getPrivateKey($type = self::PUBLIC_FORMAT_PKCS1)
|
2011-11-07 02:35:12 +01:00
|
|
|
{
|
|
|
|
if (empty($this->primes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldFormat = $this->privateKeyFormat;
|
|
|
|
$this->privateKeyFormat = $type;
|
|
|
|
$temp = $this->_convertPrivateKey($this->modulus, $this->publicExponent, $this->exponent, $this->primes, $this->exponents, $this->coefficients);
|
|
|
|
$this->privateKeyFormat = $oldFormat;
|
|
|
|
return $temp;
|
|
|
|
}
|
|
|
|
|
2012-08-26 08:36:34 +02:00
|
|
|
/**
|
|
|
|
* Returns a minimalistic private key
|
|
|
|
*
|
|
|
|
* Returns the private key without the prime number constituants. Structurally identical to a public key that
|
|
|
|
* hasn't been set as the public key
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::getPrivateKey()
|
2012-08-26 08:36:34 +02:00
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param int $type optional
|
2012-08-26 08:36:34 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
function _getPrivatePublicKey($mode = self::PUBLIC_FORMAT_PKCS8)
|
2012-08-26 08:36:34 +02:00
|
|
|
{
|
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldFormat = $this->publicKeyFormat;
|
|
|
|
$this->publicKeyFormat = $mode;
|
|
|
|
$temp = $this->_convertPublicKey($this->modulus, $this->exponent);
|
|
|
|
$this->publicKeyFormat = $oldFormat;
|
|
|
|
return $temp;
|
|
|
|
}
|
|
|
|
|
2012-07-26 20:14:18 +02:00
|
|
|
/**
|
|
|
|
* __toString() magic method
|
|
|
|
*
|
|
|
|
* @access public
|
2015-10-05 17:08:46 +02:00
|
|
|
* @return string
|
2012-07-26 20:14:18 +02:00
|
|
|
*/
|
|
|
|
function __toString()
|
|
|
|
{
|
|
|
|
$key = $this->getPrivateKey($this->privateKeyFormat);
|
|
|
|
if ($key !== false) {
|
|
|
|
return $key;
|
|
|
|
}
|
2012-08-26 08:36:34 +02:00
|
|
|
$key = $this->_getPrivatePublicKey($this->publicKeyFormat);
|
2012-07-26 20:14:18 +02:00
|
|
|
return $key !== false ? $key : '';
|
|
|
|
}
|
|
|
|
|
2013-10-18 20:24:06 +02:00
|
|
|
/**
|
|
|
|
* __clone() magic method
|
|
|
|
*
|
|
|
|
* @access public
|
2015-10-05 17:08:46 +02:00
|
|
|
* @return Crypt_RSA
|
2013-10-18 20:24:06 +02:00
|
|
|
*/
|
|
|
|
function __clone()
|
|
|
|
{
|
2014-12-17 01:16:54 +01:00
|
|
|
$key = new RSA();
|
2013-10-18 20:24:06 +02:00
|
|
|
$key->loadKey($this);
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Generates the smallest and largest numbers requiring $bits bits
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $bits
|
|
|
|
* @return array
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _generateMinMax($bits)
|
|
|
|
{
|
|
|
|
$bytes = $bits >> 3;
|
|
|
|
$min = str_repeat(chr(0), $bytes);
|
|
|
|
$max = str_repeat(chr(0xFF), $bytes);
|
2009-12-31 07:11:07 +01:00
|
|
|
$msb = $bits & 7;
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($msb) {
|
|
|
|
$min = chr(1 << ($msb - 1)) . $min;
|
|
|
|
$max = chr((1 << $msb) - 1) . $max;
|
|
|
|
} else {
|
|
|
|
$min[0] = chr(0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2014-06-02 20:09:47 +02:00
|
|
|
'min' => new BigInteger($min, 256),
|
|
|
|
'max' => new BigInteger($max, 256)
|
2009-12-03 09:19:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DER-decode the length
|
|
|
|
*
|
|
|
|
* DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
|
2013-02-20 19:25:47 +01:00
|
|
|
* {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $string
|
|
|
|
* @return int
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _decodeLength(&$string)
|
|
|
|
{
|
|
|
|
$length = ord($this->_string_shift($string));
|
2015-07-15 03:52:31 +02:00
|
|
|
if ($length & 0x80) { // definite length, long form
|
2009-12-03 09:19:00 +01:00
|
|
|
$length&= 0x7F;
|
|
|
|
$temp = $this->_string_shift($string, $length);
|
|
|
|
list(, $length) = unpack('N', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4));
|
|
|
|
}
|
|
|
|
return $length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DER-encode the length
|
|
|
|
*
|
|
|
|
* DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
|
2013-02-20 19:25:47 +01:00
|
|
|
* {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $length
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _encodeLength($length)
|
|
|
|
{
|
|
|
|
if ($length <= 0x7F) {
|
|
|
|
return chr($length);
|
|
|
|
}
|
|
|
|
|
|
|
|
$temp = ltrim(pack('N', $length), chr(0));
|
|
|
|
return pack('Ca*', 0x80 | strlen($temp), $temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String Shift
|
|
|
|
*
|
|
|
|
* Inspired by array_shift
|
|
|
|
*
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $string
|
|
|
|
* @param int $index
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _string_shift(&$string, $index = 1)
|
|
|
|
{
|
|
|
|
$substr = substr($string, 0, $index);
|
|
|
|
$string = substr($string, $index);
|
|
|
|
return $substr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the private key format
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::createKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $format
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setPrivateKeyFormat($format)
|
|
|
|
{
|
|
|
|
$this->privateKeyFormat = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the public key format
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::createKey()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $format
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setPublicKeyFormat($format)
|
|
|
|
{
|
|
|
|
$this->publicKeyFormat = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which hashing function should be used
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Used with signature production / verification and (if the encryption mode is self::ENCRYPTION_OAEP) encryption and
|
2009-12-04 22:05:32 +01:00
|
|
|
* decryption. If $hash isn't supported, sha1 is used.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $hash
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setHash($hash)
|
|
|
|
{
|
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':
|
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:
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->hash = new Hash('sha1');
|
2009-12-03 09:19:00 +01:00
|
|
|
$this->hashName = 'sha1';
|
|
|
|
}
|
2009-12-08 15:18:59 +01:00
|
|
|
$this->hLen = $this->hash->getLength();
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which hashing function should be used for the mask generation function
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* The mask generation function is used by self::ENCRYPTION_OAEP and self::SIGNATURE_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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $hash
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setMGFHash($hash)
|
|
|
|
{
|
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':
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->mgfHash = new Hash($hash);
|
2009-12-03 09:19:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2014-12-17 01:16:54 +01:00
|
|
|
$this->mgfHash = new Hash('sha1');
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2009-12-08 15:18:59 +01:00
|
|
|
$this->mgfHLen = $this->mgfHash->getLength();
|
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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $format
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setSaltLength($sLen)
|
|
|
|
{
|
|
|
|
$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
|
2014-06-02 20:09:47 +02:00
|
|
|
* @param \phpseclib\Math\BigInteger $x
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $xLen
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _i2osp($x, $xLen)
|
|
|
|
{
|
|
|
|
$x = $x->toBytes();
|
|
|
|
if (strlen($x) > $xLen) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Integer too large');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
2015-09-02 01:42:15 +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
|
|
|
*/
|
|
|
|
function _os2ip($x)
|
|
|
|
{
|
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
|
|
|
*/
|
|
|
|
function _exponentiate($x)
|
|
|
|
{
|
|
|
|
if (empty($this->primes) || empty($this->coefficients) || empty($this->exponents)) {
|
|
|
|
return $x->modPow($this->exponent, $this->modulus);
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_primes = count($this->primes);
|
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
if (defined('CRYPT_RSA_DISABLE_BLINDING')) {
|
|
|
|
$m_i = array(
|
|
|
|
1 => $x->modPow($this->exponents[1], $this->primes[1]),
|
|
|
|
2 => $x->modPow($this->exponents[2], $this->primes[2])
|
|
|
|
);
|
|
|
|
$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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:09:47 +02:00
|
|
|
$one = new BigInteger(1);
|
2010-03-01 18:28:19 +01:00
|
|
|
|
|
|
|
$r = $one->random($one, $smallest->subtract($one));
|
|
|
|
|
2010-02-28 06:28:38 +01:00
|
|
|
$m_i = array(
|
2010-03-01 18:28:19 +01:00
|
|
|
1 => $this->_blind($x, $r, 1),
|
|
|
|
2 => $this->_blind($x, $r, 2)
|
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++) {
|
2010-03-01 18:28:19 +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;
|
|
|
|
}
|
|
|
|
|
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
|
2015-09-02 01:42:15 +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
|
|
|
*/
|
2010-03-01 18:28:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-06 01:57:30 +02:00
|
|
|
/**
|
|
|
|
* Performs blinded RSA equality testing
|
|
|
|
*
|
|
|
|
* Protects against a particular type of timing attack described.
|
|
|
|
*
|
2013-02-20 19:25:47 +01:00
|
|
|
* See {@link http://codahale.com/a-lesson-in-timing-attacks/ A Lesson In Timing Attacks (or, Don't use MessageDigest.isEquals)}
|
2012-05-06 01:57:30 +02:00
|
|
|
*
|
|
|
|
* Thanks for the heads up singpolyma!
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $x
|
|
|
|
* @param string $y
|
|
|
|
* @return bool
|
2012-05-06 01:57:30 +02:00
|
|
|
*/
|
|
|
|
function _equals($x, $y)
|
|
|
|
{
|
2012-06-05 12:52:00 +02:00
|
|
|
if (strlen($x) != strlen($y)) {
|
2012-05-06 01:57:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = 0;
|
|
|
|
for ($i = 0; $i < strlen($x); $i++) {
|
2012-09-29 21:32:27 +02:00
|
|
|
$result |= ord($x[$i]) ^ ord($y[$i]);
|
2012-05-06 01:57:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result == 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsaep($m)
|
|
|
|
{
|
|
|
|
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Message representative out of range');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_exponentiate($m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsadp($c)
|
|
|
|
{
|
|
|
|
if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Ciphertext representative out of range');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_exponentiate($c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsasp1($m)
|
|
|
|
{
|
|
|
|
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Message representative out of range');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_exponentiate($m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return \phpseclib\Math\BigInteger
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsavp1($s)
|
|
|
|
{
|
|
|
|
if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Signature representative out of range');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_exponentiate($s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $mgfSeed
|
|
|
|
* @param int $mgfLen
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _mgf1($mgfSeed, $maskLen)
|
|
|
|
{
|
|
|
|
// 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $l
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsaes_oaep_encrypt($m, $l = '')
|
|
|
|
{
|
|
|
|
$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) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Message too long');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2009-12-03 09:19:00 +01:00
|
|
|
$dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1);
|
|
|
|
$maskedDB = $db ^ $dbMask;
|
|
|
|
$seedMask = $this->_mgf1($maskedDB, $this->hLen);
|
|
|
|
$maskedSeed = $seed ^ $seedMask;
|
|
|
|
$em = chr(0) . $maskedSeed . $maskedDB;
|
|
|
|
|
|
|
|
// RSA encryption
|
|
|
|
|
|
|
|
$m = $this->_os2ip($em);
|
|
|
|
$c = $this->_rsaep($m);
|
|
|
|
$c = $this->_i2osp($c, $this->k);
|
|
|
|
|
|
|
|
// 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $c
|
|
|
|
* @param string $l
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsaes_oaep_decrypt($c, $l = '')
|
|
|
|
{
|
|
|
|
// 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) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSA decryption
|
|
|
|
|
|
|
|
$c = $this->_os2ip($c);
|
|
|
|
$m = $this->_rsadp($c);
|
|
|
|
if ($m === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$em = $this->_i2osp($m, $this->k);
|
|
|
|
|
|
|
|
// EME-OAEP decoding
|
|
|
|
|
|
|
|
$lHash = $this->hash->hash($l);
|
|
|
|
$y = ord($em[0]);
|
|
|
|
$maskedSeed = substr($em, 1, $this->hLen);
|
|
|
|
$maskedDB = substr($em, $this->hLen + 1);
|
|
|
|
$seedMask = $this->_mgf1($maskedDB, $this->hLen);
|
|
|
|
$seed = $maskedSeed ^ $seedMask;
|
|
|
|
$dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1);
|
|
|
|
$db = $maskedDB ^ $dbMask;
|
|
|
|
$lHash2 = substr($db, 0, $this->hLen);
|
|
|
|
$m = substr($db, $this->hLen);
|
|
|
|
if ($lHash != $lHash2) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$m = ltrim($m, chr(0));
|
|
|
|
if (ord($m[0]) != 1) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the message M
|
|
|
|
|
|
|
|
return substr($m, 1);
|
|
|
|
}
|
|
|
|
|
2015-05-26 05:30:38 +02:00
|
|
|
/**
|
|
|
|
* Raw Encryption / Decryption
|
|
|
|
*
|
|
|
|
* Doesn't use padding and is not recommended.
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @return string
|
2015-05-26 05:30:38 +02:00
|
|
|
*/
|
|
|
|
function _raw_encrypt($m)
|
|
|
|
{
|
|
|
|
$temp = $this->_os2ip($m);
|
|
|
|
$temp = $this->_rsaep($temp);
|
|
|
|
return $this->_i2osp($temp, $this->k);
|
|
|
|
}
|
|
|
|
|
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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsaes_pkcs1_v1_5_encrypt($m)
|
|
|
|
{
|
|
|
|
$mLen = strlen($m);
|
|
|
|
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if ($mLen > $this->k - 11) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Message too long');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (defined('CRYPT_RSA_PKCS15_COMPAT') && (!isset($this->publicExponent) || $this->exponent !== $this->publicExponent)) {
|
|
|
|
$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
|
|
|
|
$m = $this->_os2ip($em);
|
|
|
|
$c = $this->_rsaep($m);
|
|
|
|
$c = $this->_i2osp($c, $this->k);
|
|
|
|
|
|
|
|
// 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $c
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsaes_pkcs1_v1_5_decrypt($c)
|
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($c) != $this->k) { // or if k < 11
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSA decryption
|
|
|
|
|
|
|
|
$c = $this->_os2ip($c);
|
|
|
|
$m = $this->_rsadp($c);
|
2010-11-20 03:13:39 +01:00
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
if ($m === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$em = $this->_i2osp($m, $this->k);
|
|
|
|
|
|
|
|
// EME-PKCS1-v1_5 decoding
|
|
|
|
|
2010-04-10 17:57:02 +02:00
|
|
|
if (ord($em[0]) != 0 || ord($em[1]) > 2) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ps = substr($em, 2, strpos($em, chr(0), 2) - 2);
|
|
|
|
$m = substr($em, strlen($ps) + 3);
|
|
|
|
|
|
|
|
if (strlen($ps) < 8) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Decryption error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output M
|
|
|
|
|
|
|
|
return $m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EMSA-PSS-ENCODE
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}.
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param int $emBits
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _emsa_pss_encode($m, $emBits)
|
|
|
|
{
|
|
|
|
// 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)
|
2015-08-11 14:27:56 +02:00
|
|
|
$sLen = $this->sLen ? $this->sLen : $this->hLen;
|
2009-12-03 09:19:00 +01:00
|
|
|
|
|
|
|
$mHash = $this->hash->hash($m);
|
|
|
|
if ($emLen < $this->hLen + $sLen + 2) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Encoding error');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$dbMask = $this->_mgf1($h, $emLen - $this->hLen - 1);
|
|
|
|
$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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $em
|
|
|
|
* @param int $emBits
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _emsa_pss_verify($m, $em, $emBits)
|
|
|
|
{
|
|
|
|
// 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);
|
2015-08-11 14:27:56 +02:00
|
|
|
$sLen = $this->sLen ? $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;
|
|
|
|
}
|
|
|
|
$dbMask = $this->_mgf1($h, $emLen - $this->hLen - 1);
|
|
|
|
$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);
|
2012-05-06 01:57:30 +02:00
|
|
|
return $this->_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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsassa_pss_sign($m)
|
|
|
|
{
|
|
|
|
// EMSA-PSS encoding
|
|
|
|
|
|
|
|
$em = $this->_emsa_pss_encode($m, 8 * $this->k - 1);
|
|
|
|
|
|
|
|
// RSA signature
|
|
|
|
|
|
|
|
$m = $this->_os2ip($em);
|
|
|
|
$s = $this->_rsasp1($m);
|
|
|
|
$s = $this->_i2osp($s, $this->k);
|
|
|
|
|
|
|
|
// 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param string $s
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsassa_pss_verify($m, $s)
|
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($s) != $this->k) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSA verification
|
|
|
|
|
|
|
|
$modBits = 8 * $this->k;
|
|
|
|
|
|
|
|
$s2 = $this->_os2ip($s);
|
|
|
|
$m2 = $this->_rsavp1($s2);
|
|
|
|
if ($m2 === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$em = $this->_i2osp($m2, $modBits >> 3);
|
|
|
|
if ($em === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// EMSA-PSS verification
|
|
|
|
|
|
|
|
return $this->_emsa_pss_verify($m, $em, $modBits - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EMSA-PKCS1-V1_5-ENCODE
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc3447#section-9.2 RFC3447#section-9.2}.
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @param int $emLen
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _emsa_pkcs1_v1_5_encode($m, $emLen)
|
|
|
|
{
|
|
|
|
$h = $this->hash->hash($m);
|
|
|
|
if ($h === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see http://tools.ietf.org/html/rfc3447#page-43
|
|
|
|
switch ($this->hashName) {
|
|
|
|
case 'md2':
|
|
|
|
$t = pack('H*', '3020300c06082a864886f70d020205000410');
|
|
|
|
break;
|
|
|
|
case 'md5':
|
|
|
|
$t = pack('H*', '3020300c06082a864886f70d020505000410');
|
|
|
|
break;
|
|
|
|
case 'sha1':
|
|
|
|
$t = pack('H*', '3021300906052b0e03021a05000414');
|
|
|
|
break;
|
|
|
|
case 'sha256':
|
|
|
|
$t = pack('H*', '3031300d060960864801650304020105000420');
|
|
|
|
break;
|
|
|
|
case 'sha384':
|
|
|
|
$t = pack('H*', '3041300d060960864801650304020205000430');
|
|
|
|
break;
|
|
|
|
case 'sha512':
|
|
|
|
$t = pack('H*', '3051300d060960864801650304020305000440');
|
|
|
|
}
|
|
|
|
$t.= $h;
|
|
|
|
$tLen = strlen($t);
|
|
|
|
|
|
|
|
if ($emLen < $tLen + 11) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Intended encoded message length too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsassa_pkcs1_v1_5_sign($m)
|
|
|
|
{
|
|
|
|
// EMSA-PKCS1-v1_5 encoding
|
|
|
|
|
|
|
|
$em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
|
|
|
|
if ($em === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('RSA modulus too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSA signature
|
|
|
|
|
|
|
|
$m = $this->_os2ip($em);
|
|
|
|
$s = $this->_rsasp1($m);
|
|
|
|
$s = $this->_i2osp($s, $this->k);
|
|
|
|
|
|
|
|
// 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
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $m
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function _rsassa_pkcs1_v1_5_verify($m, $s)
|
|
|
|
{
|
|
|
|
// Length checking
|
|
|
|
|
|
|
|
if (strlen($s) != $this->k) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSA verification
|
|
|
|
|
|
|
|
$s = $this->_os2ip($s);
|
|
|
|
$m2 = $this->_rsavp1($s);
|
|
|
|
if ($m2 === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$em = $this->_i2osp($m2, $this->k);
|
|
|
|
if ($em === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('Invalid signature');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// EMSA-PKCS1-v1_5 encoding
|
|
|
|
|
|
|
|
$em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
|
|
|
|
if ($em2 === false) {
|
2013-01-09 05:09:27 +01:00
|
|
|
user_error('RSA modulus too short');
|
2009-12-03 09:19:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare
|
2012-05-06 01:57:30 +02:00
|
|
|
return $this->_equals($em, $em2);
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Encryption Mode
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Valid values include self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1.
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $mode
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setEncryptionMode($mode)
|
|
|
|
{
|
|
|
|
$this->encryptionMode = $mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Signature Mode
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Valid values include self::SIGNATURE_PSS and self::SIGNATURE_PKCS1
|
2009-12-03 09:19:00 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param int $mode
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function setSignatureMode($mode)
|
|
|
|
{
|
|
|
|
$this->signatureMode = $mode;
|
|
|
|
}
|
|
|
|
|
2013-05-06 01:38:34 +02:00
|
|
|
/**
|
|
|
|
* Set public key comment.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $comment
|
2013-05-06 01:38:34 +02:00
|
|
|
*/
|
|
|
|
function setComment($comment)
|
|
|
|
{
|
|
|
|
$this->comment = $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get public key comment.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @return string
|
2013-05-06 01:38:34 +02:00
|
|
|
*/
|
|
|
|
function getComment()
|
|
|
|
{
|
|
|
|
return $this->comment;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
/**
|
|
|
|
* Encryption
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Both self::ENCRYPTION_OAEP and self::ENCRYPTION_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.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::decrypt()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function encrypt($plaintext)
|
|
|
|
{
|
|
|
|
switch ($this->encryptionMode) {
|
2015-05-26 05:36:10 +02:00
|
|
|
case self::ENCRYPTION_NONE:
|
2015-05-26 05:30:38 +02:00
|
|
|
$plaintext = str_split($plaintext, $this->k);
|
|
|
|
$ciphertext = '';
|
|
|
|
foreach ($plaintext as $m) {
|
|
|
|
$ciphertext.= $this->_raw_encrypt($m);
|
|
|
|
}
|
|
|
|
return $ciphertext;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::ENCRYPTION_PKCS1:
|
2009-12-06 08:26:52 +01:00
|
|
|
$length = $this->k - 11;
|
|
|
|
if ($length <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plaintext = str_split($plaintext, $length);
|
2009-12-03 09:19:00 +01:00
|
|
|
$ciphertext = '';
|
|
|
|
foreach ($plaintext as $m) {
|
|
|
|
$ciphertext.= $this->_rsaes_pkcs1_v1_5_encrypt($m);
|
|
|
|
}
|
|
|
|
return $ciphertext;
|
2014-12-09 19:46:30 +01:00
|
|
|
//case self::ENCRYPTION_OAEP:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2009-12-06 08:26:52 +01:00
|
|
|
$length = $this->k - 2 * $this->hLen - 2;
|
|
|
|
if ($length <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plaintext = str_split($plaintext, $length);
|
2009-12-03 09:19:00 +01:00
|
|
|
$ciphertext = '';
|
|
|
|
foreach ($plaintext as $m) {
|
|
|
|
$ciphertext.= $this->_rsaes_oaep_encrypt($m);
|
|
|
|
}
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decryption
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::encrypt()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function decrypt($ciphertext)
|
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if ($this->k <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ciphertext = str_split($ciphertext, $this->k);
|
2012-11-20 06:00:04 +01:00
|
|
|
$ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $this->k, chr(0), STR_PAD_LEFT);
|
|
|
|
|
2009-12-06 08:26:52 +01:00
|
|
|
$plaintext = '';
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
switch ($this->encryptionMode) {
|
2015-05-26 05:36:10 +02:00
|
|
|
case self::ENCRYPTION_NONE:
|
2015-05-26 05:30:38 +02:00
|
|
|
$decrypt = '_raw_encrypt';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::ENCRYPTION_PKCS1:
|
2009-12-06 08:26:52 +01:00
|
|
|
$decrypt = '_rsaes_pkcs1_v1_5_decrypt';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
//case self::ENCRYPTION_OAEP:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
2009-12-06 08:26:52 +01:00
|
|
|
$decrypt = '_rsaes_oaep_decrypt';
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
2009-12-06 08:26:52 +01:00
|
|
|
|
|
|
|
foreach ($ciphertext as $c) {
|
|
|
|
$temp = $this->$decrypt($c);
|
|
|
|
if ($temp === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$plaintext.= $temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plaintext;
|
2009-12-03 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a signature
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::verify()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $message
|
|
|
|
* @return string
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function sign($message)
|
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
switch ($this->signatureMode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::SIGNATURE_PKCS1:
|
2009-12-03 09:19:00 +01:00
|
|
|
return $this->_rsassa_pkcs1_v1_5_sign($message);
|
2014-12-09 19:46:30 +01:00
|
|
|
//case self::SIGNATURE_PSS:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
|
|
|
return $this->_rsassa_pss_sign($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies a signature
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::sign()
|
2009-12-03 09:19:00 +01:00
|
|
|
* @access public
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $message
|
|
|
|
* @param string $signature
|
|
|
|
* @return bool
|
2009-12-03 09:19:00 +01:00
|
|
|
*/
|
|
|
|
function verify($message, $signature)
|
|
|
|
{
|
2009-12-06 08:26:52 +01:00
|
|
|
if (empty($this->modulus) || empty($this->exponent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 09:19:00 +01:00
|
|
|
switch ($this->signatureMode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::SIGNATURE_PKCS1:
|
2009-12-03 09:19:00 +01:00
|
|
|
return $this->_rsassa_pkcs1_v1_5_verify($message, $signature);
|
2014-12-09 19:46:30 +01:00
|
|
|
//case self::SIGNATURE_PSS:
|
2009-12-03 09:19:00 +01:00
|
|
|
default:
|
|
|
|
return $this->_rsassa_pss_verify($message, $signature);
|
|
|
|
}
|
|
|
|
}
|
2013-12-25 01:37:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract raw BER from Base64 encoding
|
|
|
|
*
|
|
|
|
* @access private
|
2015-09-02 01:42:15 +02:00
|
|
|
* @param string $str
|
|
|
|
* @return string
|
2013-12-25 01:37:43 +01:00
|
|
|
*/
|
|
|
|
function _extractBER($str)
|
|
|
|
{
|
2013-12-26 08:46:58 +01:00
|
|
|
/* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them
|
|
|
|
* above and beyond the ceritificate.
|
|
|
|
* ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line:
|
|
|
|
*
|
|
|
|
* Bag Attributes
|
|
|
|
* localKeyID: 01 00 00 00
|
|
|
|
* subject=/O=organization/OU=org unit/CN=common name
|
|
|
|
* issuer=/O=organization/CN=common name
|
|
|
|
*/
|
2015-10-20 17:20:58 +02:00
|
|
|
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
|
2013-12-25 01:37:43 +01:00
|
|
|
// remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff
|
|
|
|
$temp = preg_replace('#-+[^-]+-+#', '', $temp);
|
|
|
|
// remove new lines
|
|
|
|
$temp = str_replace(array("\r", "\n", ' '), '', $temp);
|
|
|
|
$temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false;
|
|
|
|
return $temp != false ? $temp : $str;
|
|
|
|
}
|
2013-07-16 18:21:46 +02:00
|
|
|
}
|